- Fix some small formatting issues.
[reactos.git] / rosapps / dflat32 / video.c
1 /* --------------------- video.c -------------------- */
2
3 #include "dflat.h"
4
5 #define clr(fg,bg) ((fg)|((bg)<<4))
6
7 BOOL DfClipString;
8
9 /* -- read a rectangle of video memory into a save buffer -- */
10 void DfGetVideo(DFRECT rc, PCHAR_INFO bf)
11 {
12 COORD Size;
13 COORD Pos;
14 SMALL_RECT Rect;
15
16 Size.X = DfRectRight(rc) - DfRectLeft(rc) + 1;
17 Size.Y = DfRectBottom(rc) - DfRectTop(rc) + 1;
18
19 Pos.X = 0;
20 Pos.Y = 0;
21
22 Rect.Left = DfRectLeft(rc);
23 Rect.Top = DfRectTop(rc);
24 Rect.Right = DfRectRight(rc);
25 Rect.Bottom = DfRectBottom(rc);
26
27 ReadConsoleOutput (GetStdHandle (STD_OUTPUT_HANDLE),
28 bf,
29 Size,
30 Pos,
31 &Rect);
32 }
33
34 /* -- write a rectangle of video memory from a save buffer -- */
35 void DfStoreVideo(DFRECT rc, PCHAR_INFO bf)
36 {
37 COORD Size;
38 COORD Pos;
39 SMALL_RECT Rect;
40
41 Size.X = DfRectRight(rc) - DfRectLeft(rc) + 1;
42 Size.Y = DfRectBottom(rc) - DfRectTop(rc) + 1;
43
44 Pos.X = 0;
45 Pos.Y = 0;
46
47 Rect.Left = DfRectLeft(rc);
48 Rect.Top = DfRectTop(rc);
49 Rect.Right = DfRectRight(rc);
50 Rect.Bottom = DfRectBottom(rc);
51
52 WriteConsoleOutput (GetStdHandle (STD_OUTPUT_HANDLE),
53 bf,
54 Size,
55 Pos,
56 &Rect);
57 }
58
59 /* -------- read a character of video memory ------- */
60 char DfGetVideoChar(int x, int y)
61 {
62 COORD pos;
63 DWORD dwRead;
64 char ch;
65
66 pos.X = x;
67 pos.Y = y;
68
69 ReadConsoleOutputCharacter (GetStdHandle(STD_OUTPUT_HANDLE),
70 &ch,
71 1,
72 pos,
73 &dwRead);
74
75 return ch;
76 }
77
78 /* -------- write a character of video memory ------- */
79 void DfPutVideoChar(int x, int y, int ch)
80 {
81 COORD pos;
82 DWORD dwWritten;
83
84 if (x < DfScreenWidth && y < DfScreenHeight)
85 {
86 pos.X = x;
87 pos.Y = y;
88
89 WriteConsoleOutputCharacter (GetStdHandle(STD_OUTPUT_HANDLE),
90 (char *)&ch,
91 1,
92 pos,
93 &dwWritten);
94 }
95 }
96
97 BOOL DfCharInView(DFWINDOW wnd, int x, int y)
98 {
99 DFWINDOW nwnd = DfNextWindow(wnd);
100 DFWINDOW pwnd;
101 DFRECT rc;
102 int x1 = DfGetLeft(wnd)+x;
103 int y1 = DfGetTop(wnd)+y;
104
105 if (!DfTestAttribute(wnd, DF_VISIBLE))
106 return FALSE;
107 if (!DfTestAttribute(wnd, DF_NOCLIP))
108 {
109 DFWINDOW wnd1 = DfGetParent(wnd);
110 while (wnd1 != NULL)
111 {
112 /* clip character to parent's borders */
113 if (!DfTestAttribute(wnd1, DF_VISIBLE))
114 return FALSE;
115 if (!DfInsideRect(x1, y1, DfClientRect(wnd1)))
116 return FALSE;
117 wnd1 = DfGetParent(wnd1);
118 }
119 }
120 while (nwnd != NULL)
121 {
122 if (!isHidden(nwnd) && !DfIsAncestor(wnd, nwnd))
123 {
124 rc = DfWindowRect(nwnd);
125 if (DfTestAttribute(nwnd, DF_SHADOW))
126 {
127 DfRectBottom(rc)++;
128 DfRectRight(rc)++;
129 }
130 if (!DfTestAttribute(nwnd, DF_NOCLIP))
131 {
132 pwnd = nwnd;
133 while (DfGetParent(pwnd))
134 {
135 pwnd = DfGetParent(pwnd);
136 rc = DfSubRectangle(rc, DfClientRect(pwnd));
137 }
138 }
139 if (DfInsideRect(x1,y1,rc))
140 return FALSE;
141 }
142 nwnd = DfNextWindow(nwnd);
143 }
144 return (x1 < DfScreenWidth && y1 < DfScreenHeight);
145 }
146
147 /* -------- write a character to a window ------- */
148 void DfWPutch(DFWINDOW wnd, int c, int x, int y)
149 {
150 if (DfCharInView(wnd, x, y))
151 {
152 DWORD dwWritten;
153 COORD pos;
154 WORD Attr;
155
156 pos.X = DfGetLeft(wnd)+x;
157 pos.Y = DfGetTop(wnd)+y;
158
159 Attr = clr(DfForeground, DfBackground);
160
161 WriteConsoleOutputAttribute (GetStdHandle(STD_OUTPUT_HANDLE),
162 &Attr,
163 1,
164 pos,
165 &dwWritten);
166
167 WriteConsoleOutputCharacter (GetStdHandle(STD_OUTPUT_HANDLE),
168 (char *)&c,
169 1,
170 pos,
171 &dwWritten);
172 }
173 }
174
175 /* ------- write a string to a window ---------- */
176 void DfWPuts(DFWINDOW wnd, void *s, int x, int y)
177 {
178
179 int x1 = DfGetLeft(wnd)+x;
180 int x2 = x1;
181 int y1 = DfGetTop(wnd)+y;
182
183 if (x1 < DfScreenWidth && y1 < DfScreenHeight && DfIsVisible(wnd))
184 {
185 char ln[200];
186 WORD attr[200];
187 char *cp = ln;
188 WORD *ap = attr;
189 char *str = s;
190 int fg = DfForeground;
191 int bg = DfBackground;
192 int len;
193 int off = 0;
194 while (*str)
195 {
196 if (*str == DF_CHANGECOLOR)
197 {
198 str++;
199 DfForeground = (*str++) & 0x7f;
200 DfBackground = (*str++) & 0x7f;
201 continue;
202 }
203
204 if (*str == DF_RESETCOLOR)
205 {
206 DfForeground = fg & 0x7f;
207 DfBackground = bg & 0x7f;
208 str++;
209 continue;
210 }
211 *cp = (*str & 255);
212 *ap = (WORD)clr(DfForeground, DfBackground);
213 // *cp1 = (*str & 255) | (clr(DfForeground, DfBackground) << 8);
214 // if (DfClipString)
215 // if (!DfCharInView(wnd, x, y))
216 // *cp1 = peek(video_address, vad(x2,y1));
217 cp++;
218 ap++;
219 str++;
220 x++;
221 x2++;
222 }
223 DfForeground = fg;
224 DfBackground = bg;
225 len = (int)(cp-ln);
226 if (x1+len > DfScreenWidth)
227 len = DfScreenWidth-x1;
228
229 if (!DfClipString && !DfTestAttribute(wnd, DF_NOCLIP))
230 {
231 /* -- clip the line to DfWithin ancestor windows -- */
232 DFRECT rc = DfWindowRect(wnd);
233 DFWINDOW nwnd = DfGetParent(wnd);
234 while (len > 0 && nwnd != NULL)
235 {
236 if (!DfIsVisible(nwnd))
237 {
238 len = 0;
239 break;
240 }
241 rc = DfSubRectangle(rc, DfClientRect(nwnd));
242 nwnd = DfGetParent(nwnd);
243 }
244 while (len > 0 && !DfInsideRect(x1+off,y1,rc))
245 {
246 off++;
247 --len;
248 }
249 if (len > 0)
250 {
251 x2 = x1+len-1;
252 while (len && !DfInsideRect(x2,y1,rc))
253 {
254 --x2;
255 --len;
256 }
257 }
258 }
259 if (len > 0)
260 {
261 COORD pos;
262 DWORD dwWritten;
263
264 pos.X = x1;
265 pos.Y = y1;
266
267 WriteConsoleOutputAttribute (GetStdHandle(STD_OUTPUT_HANDLE),
268 attr,
269 len,
270 pos,
271 &dwWritten);
272
273 WriteConsoleOutputCharacter (GetStdHandle(STD_OUTPUT_HANDLE),
274 ln,
275 len,
276 pos,
277 &dwWritten);
278 }
279 }
280 }
281
282 /* --------- scroll the window. d: 1 = up, 0 = dn ---------- */
283 void DfScrollWindow(DFWINDOW wnd, DFRECT rc, int d)
284 {
285 if (DfRectTop(rc) != DfRectBottom(rc))
286 {
287 CHAR_INFO ciFill;
288 SMALL_RECT rcScroll;
289 SMALL_RECT rcClip;
290 COORD pos;
291
292 ciFill.Attributes = clr(DfWndForeground(wnd),DfWndBackground(wnd));
293 ciFill.Char.AsciiChar = ' ';
294
295 rcScroll.Left = DfRectLeft(rc);
296 rcScroll.Right = DfRectRight(rc);
297 rcScroll.Top = DfRectTop(rc);
298 rcScroll.Bottom = DfRectBottom(rc);
299
300 rcClip = rcScroll;
301
302 pos.X = DfRectLeft(rc);
303
304 if (d == 0)
305 {
306 /* scroll 1 line down */
307 pos.Y = DfRectTop(rc)+1;
308 }
309 else
310 {
311 /* scroll 1 line up */
312 pos.Y = DfRectTop(rc)-1;
313 }
314
315 ScrollConsoleScreenBuffer (GetStdHandle(STD_OUTPUT_HANDLE),
316 &rcScroll,
317 &rcClip,
318 pos,
319 &ciFill);
320 }
321 }
322
323 /* EOF */