Making dflat32 a dll. Its a little broken ATM.
[reactos.git] / rosapps / lib / dflat32 / video.c
1 /* --------------------- video.c -------------------- */
2
3 #include <dflat32/dflat.h>
4
5 #define clr(fg,bg) ((fg)|((bg)<<4))
6
7 BOOL ClipString;
8
9 /* -- read a rectangle of video memory into a save buffer -- */
10 void GetVideo(DFRECT rc, PCHAR_INFO bf)
11 {
12 COORD Size;
13 COORD Pos;
14 SMALL_RECT Rect;
15
16 Size.X = RectRight(rc) - RectLeft(rc) + 1;
17 Size.Y = RectBottom(rc) - RectTop(rc) + 1;
18
19 Pos.X = 0;
20 Pos.Y = 0;
21
22 Rect.Left = RectLeft(rc);
23 Rect.Top = RectTop(rc);
24 Rect.Right = RectRight(rc);
25 Rect.Bottom = RectBottom(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 StoreVideo(DFRECT rc, PCHAR_INFO bf)
36 {
37 COORD Size;
38 COORD Pos;
39 SMALL_RECT Rect;
40
41 Size.X = RectRight(rc) - RectLeft(rc) + 1;
42 Size.Y = RectBottom(rc) - RectTop(rc) + 1;
43
44 Pos.X = 0;
45 Pos.Y = 0;
46
47 Rect.Left = RectLeft(rc);
48 Rect.Top = RectTop(rc);
49 Rect.Right = RectRight(rc);
50 Rect.Bottom = RectBottom(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 GetVideoChar(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 PutVideoChar(int x, int y, int ch)
80 {
81 COORD pos;
82 DWORD dwWritten;
83
84 if (x < sScreenWidth && y < sScreenHeight)
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 CharInView(DFWINDOW wnd, int x, int y)
98 {
99 DFWINDOW nwnd = NextWindow(wnd);
100 DFWINDOW pwnd;
101 DFRECT rc;
102 int x1 = GetLeft(wnd)+x;
103 int y1 = GetTop(wnd)+y;
104
105 if (!TestAttribute(wnd, VISIBLE))
106 return FALSE;
107 if (!TestAttribute(wnd, NOCLIP))
108 {
109 DFWINDOW wnd1 = GetParent(wnd);
110 while (wnd1 != NULL)
111 {
112 /* clip character to parent's borders */
113 if (!TestAttribute(wnd1, VISIBLE))
114 return FALSE;
115 if (!InsideRect(x1, y1, ClientRect(wnd1)))
116 return FALSE;
117 wnd1 = GetParent(wnd1);
118 }
119 }
120 while (nwnd != NULL)
121 {
122 if (!isHidden(nwnd) && !isAncestor(wnd, nwnd))
123 {
124 rc = WindowRect(nwnd);
125 if (TestAttribute(nwnd, SHADOW))
126 {
127 RectBottom(rc)++;
128 RectRight(rc)++;
129 }
130 if (!TestAttribute(nwnd, NOCLIP))
131 {
132 pwnd = nwnd;
133 while (GetParent(pwnd))
134 {
135 pwnd = GetParent(pwnd);
136 rc = subRectangle(rc, ClientRect(pwnd));
137 }
138 }
139 if (InsideRect(x1,y1,rc))
140 return FALSE;
141 }
142 nwnd = NextWindow(nwnd);
143 }
144 return (x1 < sScreenWidth && y1 < sScreenHeight);
145 }
146
147 /* -------- write a character to a window ------- */
148 void wputch(DFWINDOW wnd, int c, int x, int y)
149 {
150 if (CharInView(wnd, x, y))
151 {
152 DWORD dwWritten;
153 COORD pos;
154 WORD Attr;
155
156 pos.X = GetLeft(wnd)+x;
157 pos.Y = GetTop(wnd)+y;
158
159 Attr = clr(foreground, background);
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 wputs(DFWINDOW wnd, void *s, int x, int y)
177 {
178
179 int x1 = GetLeft(wnd)+x;
180 int x2 = x1;
181 int y1 = GetTop(wnd)+y;
182
183 if (x1 < sScreenWidth && y1 < sScreenHeight && isVisible(wnd))
184 {
185 char ln[200];
186 WORD attr[200];
187 char *cp = ln;
188 WORD *ap = attr;
189 unsigned char *str = s;
190 int fg = foreground;
191 int bg = background;
192 int len;
193 int off = 0;
194 while (*str)
195 {
196 if (*str == CHANGECOLOR)
197 {
198 str++;
199 foreground = (*str++) & 0x7f;
200 background = (*str++) & 0x7f;
201 continue;
202 }
203
204 if (*str == RESETCOLOR)
205 {
206 foreground = fg & 0x7f;
207 background = bg & 0x7f;
208 str++;
209 continue;
210 }
211 *cp = (*str & 255);
212 *ap = (WORD)clr(foreground, background);
213 // *cp1 = (*str & 255) | (clr(foreground, background) << 8);
214 // if (ClipString)
215 // if (!CharInView(wnd, x, y))
216 // *cp1 = peek(video_address, vad(x2,y1));
217 cp++;
218 ap++;
219 str++;
220 x++;
221 x2++;
222 }
223 foreground = fg;
224 background = bg;
225 len = (int)(cp-ln);
226 if (x1+len > sScreenWidth)
227 len = sScreenWidth-x1;
228
229 if (!ClipString && !TestAttribute(wnd, NOCLIP))
230 {
231 /* -- clip the line to within ancestor windows -- */
232 DFRECT rc = WindowRect(wnd);
233 DFWINDOW nwnd = GetParent(wnd);
234 while (len > 0 && nwnd != NULL)
235 {
236 if (!isVisible(nwnd))
237 {
238 len = 0;
239 break;
240 }
241 rc = subRectangle(rc, ClientRect(nwnd));
242 nwnd = GetParent(nwnd);
243 }
244 while (len > 0 && !InsideRect(x1+off,y1,rc))
245 {
246 off++;
247 --len;
248 }
249 if (len > 0)
250 {
251 x2 = x1+len-1;
252 while (len && !InsideRect(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 scroll_window(DFWINDOW wnd, DFRECT rc, int d)
284 {
285 if (RectTop(rc) != RectBottom(rc))
286 {
287 CHAR_INFO ciFill;
288 SMALL_RECT rcScroll;
289 SMALL_RECT rcClip;
290 COORD pos;
291
292 ciFill.Attributes = clr(WndForeground(wnd),WndBackground(wnd));
293 ciFill.Char.AsciiChar = ' ';
294
295 rcScroll.Left = RectLeft(rc);
296 rcScroll.Right = RectRight(rc);
297 rcScroll.Top = RectTop(rc);
298 rcScroll.Bottom = RectBottom(rc);
299
300 rcClip = rcScroll;
301
302 pos.X = RectLeft(rc);
303
304 if (d == 0)
305 {
306 /* scroll 1 line down */
307 pos.Y = RectTop(rc)+1;
308 }
309 else
310 {
311 /* scroll 1 line up */
312 pos.Y = RectTop(rc)-1;
313 }
314
315 ScrollConsoleScreenBuffer (GetStdHandle(STD_OUTPUT_HANDLE),
316 &rcScroll,
317 &rcClip,
318 pos,
319 &ciFill);
320 }
321 }
322
323 /* EOF */