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