Release 970329
[wine] / controls / status.c
1 /*
2  * Interface code to StatusWindow widget/control
3  *
4  * Copyright 1996 Bruce Milner
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "windows.h"
10 #include "status.h"
11 #include "commctrl.h"
12 #include "heap.h"
13 #include "syscolor.h"
14 #include "win.h"
15
16 /*
17  * Run tests using Waite Group Windows95 API Bible Vol. 1&2
18  * The second cdrom contains executables drawstat.exe,gettext.exe,
19  * simple.exe, getparts.exe, setparts.exe, statwnd.exe
20  */
21
22 /*
23  * Fixme/Todo
24  * 1) Add size grip to status bar - SBARS_SIZEGRIP
25  * 2) Don't hard code bar to bottom of window, allow CCS_TOP also
26  * 3) Fix SBT_OWNERDRAW
27  * 4) Add DrawStatusText32A funtion
28  */
29
30 static STATUSWINDOWINFO *GetStatusInfo(HWND32 hwnd)
31 {
32     WND *wndPtr;
33
34     wndPtr = WIN_FindWndPtr(hwnd);
35     return ((STATUSWINDOWINFO *) &wndPtr->wExtra[0]);
36 }
37
38 /***********************************************************************
39  *           DrawStatusText32A   (COMCTL32.3)
40  */
41 void DrawStatusText32A( HDC32 hdc, LPRECT32 lprc, LPCSTR text, UINT32 style )
42 {
43     RECT32              r, rt;
44     int oldbkmode;
45
46     r = *lprc;
47
48     if (style == 0 ||
49         style == SBT_POPOUT) {
50         InflateRect32(&r, -1, -1);
51         SelectObject32(hdc, sysColorObjects.hbrushScrollbar);
52         Rectangle32(hdc, r.left, r.top, r.right, r.bottom);
53
54         /* draw border */
55         SelectObject32(hdc, sysColorObjects.hpenWindowFrame);
56         if (style == 0)
57             DrawEdge32(hdc, &r, EDGE_SUNKEN, BF_RECT);
58         else
59             DrawEdge32(hdc, &r, EDGE_RAISED, BF_RECT);
60     }
61     else if (style == SBT_NOBORDERS) {
62         SelectObject32(hdc, sysColorObjects.hbrushScrollbar);
63         Rectangle32(hdc, r.left, r.top, r.right, r.bottom);
64     }
65     else {      /* fixme for SBT_OWNERDRAW, SBT_RTLREADING */
66         
67     }
68
69     /* now draw text */
70     if ((style != SBT_OWNERDRAW) && text) {
71         SelectObject32(hdc, sysColorObjects.hpenWindowText);
72         oldbkmode = SetBkMode32(hdc, TRANSPARENT);
73         rt = r;
74         rt.left += 3;
75         DrawText32A(hdc, text, lstrlen32A(text),
76                     &rt, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
77
78         if (oldbkmode != TRANSPARENT)
79             SetBkMode32(hdc, oldbkmode);
80     }
81 }
82
83 static BOOL32 SW_Refresh( HWND32 hwnd, HDC32 hdc, STATUSWINDOWINFO *self )
84 {
85         int     i;
86
87         if (!IsWindowVisible32(hwnd)) {
88             return (TRUE);
89         }
90
91         if (self->simple) {
92             DrawStatusText32A(hdc,
93                               &self->part0.bound,
94                               self->part0.text,
95                               self->part0.style);
96         }
97         else {
98             for (i = 0; i < self->numParts; i++) {
99                 DrawStatusText32A(hdc,
100                                   &self->parts[i].bound,
101                                   self->parts[i].text,
102                                   self->parts[i].style);
103             }
104         }
105
106         return TRUE;
107 }
108
109
110 static LRESULT
111 SW_GetBorders(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
112 {
113     LPINT32     out;
114
115     /* FIXME for sizegrips */
116     out = (LPINT32) lParam;
117     out[0] = 1; /* vertical border width */
118     out[1] = 1; /* horizontal border width */
119     out[2] = 1; /* width of border between rectangles */
120     return TRUE;
121 }
122
123 static void
124 SW_SetPartBounds(HWND32 hwnd, STATUSWINDOWINFO *self)
125 {
126     int i;
127     RECT32      rect, *r;
128     STATUSWINDOWPART *part;
129     int sep = 1;
130
131     /* get our window size */
132     GetClientRect32(hwnd, &rect);
133
134     /* set bounds for simple rectangle */
135     self->part0.bound = rect;
136
137     /* set bounds for non-simple rectangles */
138     for (i = 0; i < self->numParts; i++) {
139         part = &self->parts[i];
140         r = &self->parts[i].bound;
141         r->top = rect.top;
142         r->bottom = rect.bottom;
143         if (i == 0)
144             r->left = 0;
145         else
146             r->left = self->parts[i-1].bound.right+sep;
147         if (part->x == -1)
148             r->right = rect.right;
149         else
150             r->right = part->x;
151     }
152 }
153
154 static LRESULT
155 SW_SetText(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
156 {
157     int part_num;
158     int style;
159     LPSTR       text;
160     int len;
161     STATUSWINDOWPART *part;
162
163     text = (LPSTR) lParam;
164     part_num = ((INT32) wParam) & 0x00ff;
165     style = ((INT32) wParam) & 0xff00;
166
167     if (part_num > 255)
168         return FALSE;
169
170     if (self->simple)
171         part = &self->part0;
172     else
173         part = &self->parts[part_num];
174     part->style = style;
175     if (style == SBT_OWNERDRAW) {
176         part->text = text;
177     }
178     else {
179         /* duplicate string */
180         if (part->text)
181             HeapFree(SystemHeap, 0, part->text);
182         part->text = 0;
183         if (text && (len = lstrlen32A(text))) {
184             part->text = HeapAlloc(SystemHeap, 0, len+1);
185             lstrcpy32A(part->text, text);
186         }
187     }
188     InvalidateRect32(hwnd, &part->bound, FALSE);
189     return TRUE;
190 }
191
192 static LRESULT
193 SW_SetParts(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
194 {
195     HDC32       hdc;
196     LPINT32 parts;
197     STATUSWINDOWPART *  tmp;
198     int i;
199     int oldNumParts;
200
201     if (self->simple) {
202         self->simple = FALSE;
203     }
204     oldNumParts = self->numParts;
205     self->numParts = (INT32) wParam;
206     parts = (LPINT32) lParam;
207     if (oldNumParts > self->numParts) {
208         for (i = self->numParts ; i < oldNumParts; i++) {
209             if (self->parts[i].text && (self->parts[i].style != SBT_OWNERDRAW))
210                 HeapFree(SystemHeap, 0, self->parts[i].text);
211         }
212     }
213     else if (oldNumParts < self->numParts) {
214         tmp = HeapAlloc(SystemHeap, HEAP_ZERO_MEMORY,
215                         sizeof(STATUSWINDOWPART) * self->numParts);
216         for (i = 0; i < oldNumParts; i++) {
217             tmp[i] = self->parts[i];
218         }
219         if (self->parts)
220             HeapFree(SystemHeap, 0, self->parts);
221         self->parts = tmp;
222     }
223     
224     for (i = 0; i < self->numParts; i++) {
225         self->parts[i].x = parts[i];
226     }
227     SW_SetPartBounds(hwnd, self);
228
229     hdc = GetDC32(hwnd);
230     SW_Refresh(hwnd, hdc, self);
231     ReleaseDC32(hwnd, hdc);
232     return TRUE;
233 }
234
235 static LRESULT
236 SW_GetParts(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
237 {
238     LPINT32 parts;
239     INT32       num_parts;
240     int i;
241
242     self = GetStatusInfo(hwnd);
243     num_parts = (INT32) wParam;
244     parts = (LPINT32) lParam;
245     if (parts) {
246         return (self->numParts);
247         for (i = 0; i < num_parts; i++) {
248             parts[i] = self->parts[i].x;
249         }
250     }
251     return (self->numParts);
252 }
253
254 static LRESULT
255 SW_Create(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
256 {
257     RECT32      rect;
258     LPCREATESTRUCT32A lpCreate = (LPCREATESTRUCT32A) lParam;
259     int height, width;
260     HDC32       hdc;
261     HWND32      parent;
262
263     self->numParts = 0;
264     self->parts = 0;
265     self->simple = TRUE;
266     GetClientRect32(hwnd, &rect);
267
268     /* initialize simple case */
269     self->part0.bound = rect;
270     self->part0.text = 0;
271     self->part0.x = 0;
272     self->part0.style = 0;
273
274     height = 40;
275     if ((hdc = GetDC32(0))) {
276         TEXTMETRIC32A tm;
277         GetTextMetrics32A(hdc, &tm);
278         self->textHeight = tm.tmHeight;
279         ReleaseDC32(0, hdc);
280     }
281
282     parent = GetParent32(hwnd);
283     GetClientRect32(parent, &rect);
284     width = rect.right - rect.left;
285     height = (self->textHeight * 3)/2;
286     MoveWindow32(hwnd, lpCreate->x, lpCreate->y-1, width, height, FALSE);
287     SW_SetPartBounds(hwnd, self);
288     return 0;
289 }
290
291 static LRESULT
292 SW_GetRect(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
293 {
294     int part_num;
295     LPRECT32  rect;
296
297     part_num = ((INT32) wParam) & 0x00ff;
298     rect = (LPRECT32) lParam;
299     if (self->simple)
300         *rect = self->part0.bound;
301     else
302         *rect = self->parts[part_num].bound;
303     return TRUE;
304 }
305
306 static LRESULT
307 SW_GetText(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
308 {
309     int part_num;
310     LRESULT     result;
311     STATUSWINDOWPART *part;
312     LPSTR       out_text;
313
314     part_num = ((INT32) wParam) & 0x00ff;
315     out_text = (LPSTR) lParam;
316     if (self->simple)
317         part = &self->part0;
318     else
319         part = &self->parts[part_num];
320
321     if (part->style == SBT_OWNERDRAW)
322         result = (LRESULT) part->text;
323     else {
324         result = part->text ? lstrlen32A(part->text) : 0;
325         result |= (part->style << 16);
326         if (out_text) {
327             lstrcpy32A(out_text, part->text);
328         }
329     }
330     return result;
331 }
332
333 static LRESULT
334 SW_GetTextLength(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
335 {
336     int part_num;
337     STATUSWINDOWPART *part;
338     DWORD       result;
339
340     part_num = ((INT32) wParam) & 0x00ff;
341
342     if (self->simple)
343         part = &self->part0;
344     else
345         part = &self->parts[part_num];
346
347     if (part->text)
348         result = lstrlen32A(part->text);
349     else
350         result = 0;
351
352     result |= (part->style << 16);
353     return result;
354 }
355
356 static LRESULT
357 SW_SetMinHeight(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
358 {
359     /* FIXME */
360     /* size is wParam | 2*pixels_of_horz_border */
361     return TRUE;
362 }
363
364 static LRESULT
365 SW_Simple(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
366 {
367     BOOL32 simple;
368     HDC32       hdc;
369
370     simple = (BOOL32) wParam;
371     self->simple = simple;
372     hdc = GetDC32(hwnd);
373     SW_Refresh(hwnd, hdc, self);
374     ReleaseDC32(hwnd, hdc);
375     return TRUE;
376 }
377
378 static LRESULT
379 SW_Size(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
380 {
381     /* Need to resize width to match parent */
382     INT32       width, height, x, y;
383     RECT32      parent_rect;
384     HWND32      parent;
385
386     INT32       flags;
387
388     flags = (INT32) wParam;
389
390     /* FIXME for flags =
391      * SIZE_MAXIMIZED, SIZE_MAXSHOW, SIZE_MINIMIZED, SIZE_RESTORED
392      */
393
394     if (flags == SIZE_RESTORED) {
395         /* width and height don't apply */
396         parent = GetParent32(hwnd);
397         GetClientRect32(parent, &parent_rect);
398         height = (self->textHeight * 3)/2;
399         width = parent_rect.right - parent_rect.left;
400         x = parent_rect.left;
401         y = parent_rect.bottom - height;
402         MoveWindow32(hwnd, parent_rect.left, parent_rect.bottom - height - 1,
403                      width, height, TRUE);
404         SW_SetPartBounds(hwnd, self);
405     }
406     return 0;
407 }
408
409 static LRESULT
410 SW_Destroy(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
411 {
412     int i;
413
414     for (i = 0; i < self->numParts; i++) {
415         if (self->parts[i].text && (self->parts[i].style != SBT_OWNERDRAW))
416             HeapFree(SystemHeap, 0, self->parts[i].text);
417     }
418     if (self->part0.text && (self->part0.style != SBT_OWNERDRAW))
419         HeapFree(SystemHeap, 0, self->part0.text);
420     HeapFree(SystemHeap, 0, self->parts);
421     return 0;
422 }
423
424
425
426 static LRESULT
427 SW_Paint(STATUSWINDOWINFO *self, HWND32 hwnd)
428 {
429     HDC32 hdc;
430     PAINTSTRUCT32       ps;
431
432     hdc = BeginPaint32(hwnd, &ps);
433     SW_Refresh(hwnd, hdc, self);
434     EndPaint32(hwnd, &ps);
435     return 0;
436 }
437
438 LRESULT StatusWindowProc( HWND32 hwnd, UINT32 msg,
439                           WPARAM32 wParam, LPARAM lParam )
440 {
441     STATUSWINDOWINFO *self;
442
443     self = GetStatusInfo(hwnd);
444
445     switch (msg) {
446     case SB_GETBORDERS:
447         return SW_GetBorders(self, hwnd, wParam, lParam);
448     case SB_GETPARTS:
449         return SW_GetParts(self, hwnd, wParam, lParam);
450     case SB_GETRECT:
451         return SW_GetRect(self, hwnd, wParam, lParam);
452     case SB_GETTEXT32A:
453         return SW_GetText(self, hwnd, wParam, lParam);
454     case SB_GETTEXTLENGTH32A:
455         return SW_GetTextLength(self, hwnd, wParam, lParam);
456     case SB_SETMINHEIGHT:
457         return SW_SetMinHeight(self, hwnd, wParam, lParam);
458     case SB_SETPARTS:   
459         return SW_SetParts(self, hwnd, wParam, lParam);
460     case SB_SETTEXT32A:
461         return SW_SetText(self, hwnd, wParam, lParam);
462     case SB_SIMPLE:
463         return SW_Simple(self, hwnd, wParam, lParam);
464
465     case WM_CREATE:
466         return SW_Create(self, hwnd, wParam, lParam);
467     case WM_DESTROY:
468         return SW_Destroy(self, hwnd, wParam, lParam);
469     case WM_PAINT:
470         return SW_Paint(self, hwnd);
471     case WM_SIZE:
472         return SW_Size(self, hwnd, wParam, lParam);
473     default:
474         return DefWindowProc32A(hwnd, msg, wParam, lParam);
475     }
476     return 0;
477 }
478
479
480 /***********************************************************************
481  *           CreateStatusWindow32A   (COMCTL32.4)
482  */
483 HWND32 CreateStatusWindow32A( INT32 style, LPCSTR text, HWND32 parent,
484                               UINT32 wid )
485 {
486     HWND32 ret;
487     ATOM atom;
488
489     atom = GlobalFindAtom32A(STATUSCLASSNAME32A);
490     if (!atom) {
491         /* Some apps don't call InitCommonControls */
492         InitCommonControls();
493     }
494
495     ret = CreateWindowEx32A(0, STATUSCLASSNAME32A, "Status Window",
496                             style, CW_USEDEFAULT32, CW_USEDEFAULT32,
497                             CW_USEDEFAULT32, CW_USEDEFAULT32, parent, 0, 0, 0);
498     return (ret);
499 }