Making dflat32 a dll. Its a little broken ATM.
[reactos.git] / rosapps / lib / dflat32 / window.c
1 /* ---------- window.c ------------- */
2
3 #include <dflat32/dflat.h>
4
5 DFWINDOW inFocus = NULL;
6
7 int foreground, background; /* current video colors */
8
9 static void TopLine(DFWINDOW, int, DFRECT);
10
11 /* --------- create a window ------------ */
12 DFWINDOW DfCreateWindow(
13 DFCLASS class, /* class of this window */
14 char *ttl, /* title or NULL */
15 int left, int top, /* upper left coordinates */
16 int height, int width, /* dimensions */
17 void *extension, /* pointer to additional data */
18 DFWINDOW parent, /* parent of this window */
19 int (*wndproc)(struct window *,enum messages,PARAM,PARAM),
20 int attrib) /* window attribute */
21 {
22 DFWINDOW wnd = DFcalloc(1, sizeof(struct window));
23 if (wnd != NULL) {
24 int base;
25 /* ----- height, width = -1: fill the screen ------- */
26 if (height == -1)
27 height = sScreenHeight;
28 if (width == -1)
29 width = sScreenWidth;
30 /* ----- coordinates -1, -1 = center the window ---- */
31 if (left == -1)
32 wnd->rc.lf = (sScreenWidth-width)/2;
33 else
34 wnd->rc.lf = left;
35 if (top == -1)
36 wnd->rc.tp = (sScreenHeight-height)/2;
37 else
38 wnd->rc.tp = top;
39 wnd->attrib = attrib;
40 if (ttl != NULL)
41 AddAttribute(wnd, HASTITLEBAR);
42 if (wndproc == NULL)
43 wnd->wndproc = classdefs[class].wndproc;
44 else
45 wnd->wndproc = wndproc;
46 /* ---- derive attributes of base classes ---- */
47 base = class;
48 while (base != -1) {
49 AddAttribute(wnd, classdefs[base].attrib);
50 base = classdefs[base].base;
51 }
52 if (parent)
53 {
54 if (!TestAttribute(wnd, NOCLIP))
55 {
56 /* -- keep upper left within borders of parent - */
57 wnd->rc.lf = max(wnd->rc.lf,GetClientLeft(parent));
58 wnd->rc.tp = max(wnd->rc.tp,GetClientTop(parent));
59 }
60 }
61 else
62 parent = ApplicationWindow;
63
64 wnd->class = class;
65 wnd->extension = extension;
66 wnd->rc.rt = GetLeft(wnd)+width-1;
67 wnd->rc.bt = GetTop(wnd)+height-1;
68 wnd->ht = height;
69 wnd->wd = width;
70 if (ttl != NULL)
71 InsertTitle(wnd, ttl);
72 wnd->parent = parent;
73 wnd->oldcondition = wnd->condition = ISRESTORED;
74 wnd->RestoredRC = wnd->rc;
75 InitWindowColors(wnd);
76 DfSendMessage(wnd, CREATE_WINDOW, 0, 0);
77 if (isVisible(wnd))
78 DfSendMessage(wnd, SHOW_WINDOW, 0, 0);
79 }
80 return wnd;
81 }
82
83 /* -------- add a title to a window --------- */
84 void AddTitle(DFWINDOW wnd, char *ttl)
85 {
86 InsertTitle(wnd, ttl);
87 DfSendMessage(wnd, BORDER, 0, 0);
88 }
89
90 /* ----- insert a title into a window ---------- */
91 void InsertTitle(DFWINDOW wnd, char *ttl)
92 {
93 wnd->title=DFrealloc(wnd->title,strlen(ttl)+1);
94 strcpy(wnd->title, ttl);
95 }
96
97 static unsigned char line[300];
98
99 /* ------ write a line to video window client area ------ */
100 void writeline(DFWINDOW wnd, char *str, int x, int y, BOOL pad)
101 {
102 char *cp;
103 int len;
104 int dif;
105 char wline[200];
106
107 memset(wline, 0, 200);
108 len = LineLength(str);
109 dif = strlen(str) - len;
110 strncpy(wline, str, ClientWidth(wnd) + dif);
111 if (pad) {
112 cp = wline+strlen(wline);
113 while (len++ < ClientWidth(wnd)-x)
114 *cp++ = ' ';
115 }
116 wputs(wnd, wline, x, y);
117 }
118
119 DFRECT AdjustRectangle(DFWINDOW wnd, DFRECT rc)
120 {
121 /* -------- adjust the rectangle ------- */
122 if (TestAttribute(wnd, HASBORDER)) {
123 if (RectLeft(rc) == 0)
124 --rc.rt;
125 else if (RectLeft(rc) < RectRight(rc) &&
126 RectLeft(rc) < WindowWidth(wnd)+1)
127 --rc.lf;
128 }
129 if (TestAttribute(wnd, HASBORDER | HASTITLEBAR)) {
130 if (RectTop(rc) == 0)
131 --rc.bt;
132 else if (RectTop(rc) < RectBottom(rc) &&
133 RectTop(rc) < WindowHeight(wnd)+1)
134 --rc.tp;
135 }
136 RectRight(rc) = max(RectLeft(rc),
137 min(RectRight(rc),WindowWidth(wnd)));
138 RectBottom(rc) = max(RectTop(rc),
139 min(RectBottom(rc),WindowHeight(wnd)));
140 return rc;
141 }
142
143 /* -------- display a window's title --------- */
144 void DisplayTitle(DFWINDOW wnd, DFRECT *rcc)
145 {
146 if (GetTitle(wnd) != NULL)
147 {
148 int tlen = min((int)strlen(GetTitle(wnd)), (int)WindowWidth(wnd)-2);
149 int tend = WindowWidth(wnd)-3-BorderAdj(wnd);
150 DFRECT rc;
151
152 if (rcc == NULL)
153 rc = RelativeWindowRect(wnd, WindowRect(wnd));
154 else
155 rc = *rcc;
156 rc = AdjustRectangle(wnd, rc);
157
158 if (DfSendMessage(wnd, TITLE, (PARAM) rcc, 0))
159 {
160 if (wnd == inFocus)
161 {
162 foreground = cfg.clr[TITLEBAR] [HILITE_COLOR] [FG];
163 background = cfg.clr[TITLEBAR] [HILITE_COLOR] [BG];
164 }
165 else
166 {
167 foreground = cfg.clr[TITLEBAR] [STD_COLOR] [FG];
168 background = cfg.clr[TITLEBAR] [STD_COLOR] [BG];
169 }
170 memset(line,' ',WindowWidth(wnd));
171 #ifdef INCLUDE_MINIMIZE
172 if (wnd->condition != ISMINIMIZED)
173 #endif
174 strncpy (line + ((WindowWidth(wnd)-2 - tlen) / 2),
175 wnd->title, tlen);
176 if (TestAttribute(wnd, CONTROLBOX))
177 line[2-BorderAdj(wnd)] = CONTROLBOXCHAR;
178 if (TestAttribute(wnd, MINMAXBOX))
179 {
180 switch (wnd->condition)
181 {
182 case ISRESTORED:
183 #ifdef INCLUDE_MAXIMIZE
184 line[tend+1] = MAXPOINTER;
185 #endif
186 #ifdef INCLUDE_MINIMIZE
187 line[tend] = MINPOINTER;
188 #endif
189 break;
190 #ifdef INCLUDE_MINIMIZE
191 case ISMINIMIZED:
192 line[tend+1] = MAXPOINTER;
193 break;
194 #endif
195 #ifdef INCLUDE_MAXIMIZE
196 case ISMAXIMIZED:
197 #ifdef INCLUDE_MINIMIZE
198 line[tend] = MINPOINTER;
199 #endif
200 #ifdef INCLUDE_RESTORE
201 line[tend+1] = RESTOREPOINTER;
202 #endif
203 break;
204 #endif
205 default:
206 break;
207 }
208 }
209 line[RectRight(rc)+1] = line[tend+3] = '\0';
210 if (wnd != inFocus)
211 ClipString++;
212 writeline(wnd, line+RectLeft(rc),
213 RectLeft(rc)+BorderAdj(wnd),
214 0,
215 FALSE);
216 ClipString = 0;
217 }
218 }
219 }
220
221 /* --- display right border shadow character of a window --- */
222 static void shadow_char(DFWINDOW wnd, int y)
223 {
224 int fg = foreground;
225 int bg = background;
226 int x = WindowWidth(wnd);
227 char c = videochar(GetLeft(wnd)+x, GetTop(wnd)+y);
228
229 if (TestAttribute(wnd, SHADOW) == 0)
230 return;
231 foreground = DARKGRAY;
232 background = BLACK;
233 wputch(wnd, c, x, y);
234 foreground = fg;
235 background = bg;
236 }
237
238 /* --- display the bottom border shadow line for a window -- */
239 static void shadowline(DFWINDOW wnd, DFRECT rc)
240 {
241 int i;
242 int y = GetBottom(wnd)+1;
243 int fg = foreground;
244 int bg = background;
245
246 if ((TestAttribute(wnd, SHADOW)) == 0)
247 return;
248 for (i = 0; i < WindowWidth(wnd)+1; i++)
249 line[i] = videochar(GetLeft(wnd)+i, y);
250 line[i] = '\0';
251 foreground = DARKGRAY;
252 background = BLACK;
253 line[RectRight(rc)+1] = '\0';
254 if (RectLeft(rc) == 0)
255 rc.lf++;
256 ClipString++;
257 wputs(wnd, line+RectLeft(rc), RectLeft(rc),
258 WindowHeight(wnd));
259 --ClipString;
260 foreground = fg;
261 background = bg;
262 }
263
264 static DFRECT ParamRect(DFWINDOW wnd, DFRECT *rcc)
265 {
266 DFRECT rc;
267 if (rcc == NULL) {
268 rc = RelativeWindowRect(wnd, WindowRect(wnd));
269 if (TestAttribute(wnd, SHADOW)) {
270 rc.rt++;
271 rc.bt++;
272 }
273 }
274 else
275 rc = *rcc;
276 return rc;
277 }
278
279 void PaintShadow(DFWINDOW wnd)
280 {
281 int y;
282 DFRECT rc = ParamRect(wnd, NULL);
283 for (y = 1; y < WindowHeight(wnd); y++)
284 shadow_char(wnd, y);
285 shadowline(wnd, rc);
286 }
287
288 /* ------- display a window's border ----- */
289 void RepaintBorder(DFWINDOW wnd, DFRECT *rcc)
290 {
291 int y;
292 char lin, side, ne, nw, se, sw;
293 DFRECT rc, clrc;
294
295 if (!TestAttribute(wnd, HASBORDER))
296 return;
297 rc = ParamRect(wnd, rcc);
298 clrc = AdjustRectangle(wnd, rc);
299
300 if (wnd == inFocus) {
301 lin = FOCUS_LINE;
302 side = FOCUS_SIDE;
303 ne = FOCUS_NE;
304 nw = FOCUS_NW;
305 se = FOCUS_SE;
306 sw = FOCUS_SW;
307 }
308 else {
309 lin = LINE;
310 side = SIDE;
311 ne = NE;
312 nw = NW;
313 se = SE;
314 sw = SW;
315 }
316 line[WindowWidth(wnd)] = '\0';
317 /* ---------- window title ------------ */
318 if (TestAttribute(wnd, HASTITLEBAR))
319 if (RectTop(rc) == 0)
320 if (RectLeft(rc) < WindowWidth(wnd)-BorderAdj(wnd))
321 DisplayTitle(wnd, &rc);
322 foreground = FrameForeground(wnd);
323 background = FrameBackground(wnd);
324 /* -------- top frame corners --------- */
325 if (RectTop(rc) == 0) {
326 if (RectLeft(rc) == 0)
327 wputch(wnd, nw, 0, 0);
328 if (RectLeft(rc) < WindowWidth(wnd)) {
329 if (RectRight(rc) >= WindowWidth(wnd)-1)
330 wputch(wnd, ne, WindowWidth(wnd)-1, 0);
331 TopLine(wnd, lin, clrc);
332 }
333 }
334
335 /* ----------- window body ------------ */
336 for (y = RectTop(rc); y <= RectBottom(rc); y++) {
337 char ch;
338 if (y == 0 || y >= WindowHeight(wnd)-1)
339 continue;
340 if (RectLeft(rc) == 0)
341 wputch(wnd, side, 0, y);
342 if (RectLeft(rc) < WindowWidth(wnd) &&
343 RectRight(rc) >= WindowWidth(wnd)-1) {
344 if (TestAttribute(wnd, VSCROLLBAR))
345 ch = ( y == 1 ? UPSCROLLBOX :
346 y == WindowHeight(wnd)-2 ?
347 DOWNSCROLLBOX :
348 y-1 == wnd->VScrollBox ?
349 SCROLLBOXCHAR :
350 SCROLLBARCHAR );
351 else
352 ch = side;
353 wputch(wnd, ch, WindowWidth(wnd)-1, y);
354 }
355 if (RectRight(rc) == WindowWidth(wnd))
356 shadow_char(wnd, y);
357 }
358
359 if (RectTop(rc) <= WindowHeight(wnd)-1 &&
360 RectBottom(rc) >= WindowHeight(wnd)-1) {
361 /* -------- bottom frame corners ---------- */
362 if (RectLeft(rc) == 0)
363 wputch(wnd, sw, 0, WindowHeight(wnd)-1);
364 if (RectLeft(rc) < WindowWidth(wnd) &&
365 RectRight(rc) >= WindowWidth(wnd)-1)
366 wputch(wnd, se, WindowWidth(wnd)-1,
367 WindowHeight(wnd)-1);
368
369
370 if (wnd->StatusBar == NULL) {
371 /* ----------- bottom line ------------- */
372 memset(line,lin,WindowWidth(wnd)-1);
373 if (TestAttribute(wnd, HSCROLLBAR)) {
374 line[0] = LEFTSCROLLBOX;
375 line[WindowWidth(wnd)-3] = RIGHTSCROLLBOX;
376 memset(line+1, SCROLLBARCHAR, WindowWidth(wnd)-4);
377 line[wnd->HScrollBox] = SCROLLBOXCHAR;
378 }
379 line[WindowWidth(wnd)-2] = line[RectRight(rc)] = '\0';
380 if (RectLeft(rc) != RectRight(rc) ||
381 (RectLeft(rc) && RectLeft(rc) < WindowWidth(wnd)-1))
382 {
383 if (wnd != inFocus)
384 ClipString++;
385 writeline(wnd,
386 line+(RectLeft(clrc)),
387 RectLeft(clrc)+1,
388 WindowHeight(wnd)-1,
389 FALSE);
390 ClipString = 0;
391 }
392 }
393 if (RectRight(rc) == WindowWidth(wnd))
394 shadow_char(wnd, WindowHeight(wnd)-1);
395 }
396 if (RectBottom(rc) == WindowHeight(wnd))
397 /* ---------- bottom shadow ------------- */
398 shadowline(wnd, rc);
399 }
400
401 static void TopLine(DFWINDOW wnd, int lin, DFRECT rc)
402 {
403 if (TestAttribute(wnd, HASMENUBAR))
404 return;
405 if (TestAttribute(wnd, HASTITLEBAR) && GetTitle(wnd))
406 return;
407 if (RectLeft(rc) == 0) {
408 RectLeft(rc) += BorderAdj(wnd);
409 RectRight(rc) += BorderAdj(wnd);
410 }
411 if (RectRight(rc) < WindowWidth(wnd)-1)
412 RectRight(rc)++;
413
414 if (RectLeft(rc) < RectRight(rc)) {
415 /* ----------- top line ------------- */
416 memset(line,lin,WindowWidth(wnd)-1);
417 if (TestAttribute(wnd, CONTROLBOX)) {
418 strncpy(line+1, " ", 3);
419 *(line+2) = CONTROLBOXCHAR;
420 }
421 line[RectRight(rc)] = '\0';
422 writeline(wnd, line+RectLeft(rc),
423 RectLeft(rc), 0, FALSE);
424 }
425 }
426
427 /* ------ clear the data space of a window -------- */
428 void ClearWindow(DFWINDOW wnd, DFRECT *rcc, int clrchar)
429 {
430 if (isVisible(wnd)) {
431 int y;
432 DFRECT rc;
433
434 if (rcc == NULL)
435 rc = RelativeWindowRect(wnd, WindowRect(wnd));
436 else
437 rc = *rcc;
438
439 if (RectLeft(rc) == 0)
440 RectLeft(rc) = BorderAdj(wnd);
441 if (RectRight(rc) > WindowWidth(wnd)-1)
442 RectRight(rc) = WindowWidth(wnd)-1;
443 SetStandardColor(wnd);
444 memset(line, clrchar, sizeof line);
445 line[RectRight(rc)+1] = '\0';
446 for (y = RectTop(rc); y <= RectBottom(rc); y++)
447 {
448 if (y < TopBorderAdj(wnd) ||
449 y > ClientHeight(wnd)+
450 (TestAttribute(wnd, HASMENUBAR) ? 1 : 0))
451 continue;
452 writeline(wnd,
453 line+(RectLeft(rc)),
454 RectLeft(rc),
455 y,
456 FALSE);
457 }
458 }
459 }
460
461 /* ------ compute the logical line length of a window ------ */
462 int LineLength(char *ln)
463 {
464 int len = strlen(ln);
465 char *cp = ln;
466 while ((cp = strchr(cp, CHANGECOLOR)) != NULL)
467 {
468 cp++;
469 len -= 3;
470 }
471 cp = ln;
472 while ((cp = strchr(cp, RESETCOLOR)) != NULL)
473 {
474 cp++;
475 --len;
476 }
477 return len;
478 }
479
480 void InitWindowColors(DFWINDOW wnd)
481 {
482 int fbg,col;
483 int cls = GetClass(wnd);
484 /* window classes without assigned colors inherit parent's colors */
485 if (cfg.clr[cls][0][0] == 0xff && GetParent(wnd) != NULL)
486 cls = GetClass(GetParent(wnd));
487 /* ---------- set the colors ---------- */
488 for (fbg = 0; fbg < 2; fbg++)
489 for (col = 0; col < 4; col++)
490 wnd->WindowColors[col][fbg] = cfg.clr[cls][col][fbg];
491 }
492
493 void PutWindowChar(DFWINDOW wnd, char c, int x, int y)
494 {
495 if (x < ClientWidth(wnd) && y < ClientHeight(wnd))
496 wputch(wnd, c, x+BorderAdj(wnd), y+TopBorderAdj(wnd));
497 }
498
499 void PutWindowLine(DFWINDOW wnd, void *s, int x, int y)
500 {
501 int saved = FALSE;
502 int sv = 0;
503
504 if (x < ClientWidth(wnd) && y < ClientHeight(wnd))
505 {
506 char *en = (char *)s+ClientWidth(wnd)-x;
507 if ((int)(strlen(s)+x) > (int)ClientWidth(wnd))
508 {
509 sv = *en;
510 *en = '\0';
511 saved = TRUE;
512 }
513 ClipString++;
514 wputs(wnd, s, x+BorderAdj(wnd), y+TopBorderAdj(wnd));
515 --ClipString;
516 if (saved)
517 *en = sv;
518 }
519 }
520
521 /* EOF */