Release 960811
[wine/hacks.git] / controls / status.c
blob0b6eb80d008d163c980c2e1b035ea76ca3ac6b2d
1 /*
2 * Interface code to StatusWindow widget/control
4 * Copyright 1996 Bruce Milner
5 */
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"
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
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
30 static STATUSWINDOWINFO *GetStatusInfo(HWND32 hwnd)
32 WND *wndPtr;
34 wndPtr = WIN_FindWndPtr(hwnd);
35 return ((STATUSWINDOWINFO *) &wndPtr->wExtra[0]);
38 /***********************************************************************
39 * DrawStatusText32A (COMCTL32.3)
41 void DrawStatusText32A( HDC32 hdc, LPRECT32 lprc, LPCSTR text, UINT32 style )
43 RECT32 r, rt;
44 int oldbkmode;
46 r = *lprc;
48 if (style == 0 ||
49 style == SBT_POPOUT) {
50 InflateRect32(&r, -1, -1);
51 SelectObject(hdc, sysColorObjects.hbrushScrollbar);
52 Rectangle(hdc, r.left, r.top, r.right, r.bottom);
54 /* draw border */
55 SelectObject(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);
61 else if (style == SBT_NOBORDERS) {
62 SelectObject(hdc, sysColorObjects.hbrushScrollbar);
63 Rectangle(hdc, r.left, r.top, r.right, r.bottom);
65 else { /* fixme for SBT_OWNERDRAW, SBT_RTLREADING */
69 /* now draw text */
70 if ((style != SBT_OWNERDRAW) && text) {
71 SelectObject(hdc, sysColorObjects.hpenWindowText);
72 oldbkmode = SetBkMode(hdc, TRANSPARENT);
73 rt = r;
74 rt.left += 3;
75 DrawText32A(hdc, text, lstrlen32A(text),
76 &rt, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
78 if (oldbkmode != TRANSPARENT)
79 SetBkMode(hdc, oldbkmode);
83 static BOOL32 SW_Refresh( HWND32 hwnd, HDC32 hdc, STATUSWINDOWINFO *self )
85 int i;
87 if (!IsWindowVisible(hwnd)) {
88 return (TRUE);
91 if (self->simple) {
92 DrawStatusText32A(hdc,
93 &self->part0.bound,
94 self->part0.text,
95 self->part0.style);
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);
106 return TRUE;
110 static LRESULT
111 SW_GetBorders(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
113 LPINT32 out;
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;
123 static void
124 SW_SetPartBounds(HWND32 hwnd, STATUSWINDOWINFO *self)
126 int i;
127 RECT32 rect, *r;
128 STATUSWINDOWPART *part;
129 int sep = 1;
131 /* get our window size */
132 GetClientRect32(hwnd, &rect);
134 /* set bounds for simple rectangle */
135 self->part0.bound = rect;
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;
154 static LRESULT
155 SW_SetText(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
157 int part_num;
158 int style;
159 LPSTR text;
160 int len;
161 STATUSWINDOWPART *part;
163 text = (LPSTR) lParam;
164 part_num = ((INT32) wParam) & 0x00ff;
165 style = ((INT32) wParam) & 0xff00;
167 if (part_num > 255)
168 return FALSE;
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;
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);
188 InvalidateRect32(hwnd, &part->bound, FALSE);
189 return TRUE;
192 static LRESULT
193 SW_SetParts(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
195 HDC32 hdc;
196 LPINT32 parts;
197 STATUSWINDOWPART * tmp;
198 int i;
199 int oldNumParts;
201 if (self->simple) {
202 self->simple = FALSE;
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);
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];
219 if (self->parts)
220 HeapFree(SystemHeap, 0, self->parts);
221 self->parts = tmp;
224 for (i = 0; i < self->numParts; i++) {
225 self->parts[i].x = parts[i];
227 SW_SetPartBounds(hwnd, self);
229 hdc = GetDC(hwnd);
230 SW_Refresh(hwnd, hdc, self);
231 ReleaseDC(hwnd, hdc);
232 return TRUE;
235 static LRESULT
236 SW_GetParts(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
238 LPINT32 parts;
239 INT32 num_parts;
240 int i;
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;
251 return (self->numParts);
254 static LRESULT
255 SW_Create(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
257 RECT32 rect;
258 LPCREATESTRUCT32A lpCreate = (LPCREATESTRUCT32A) lParam;
259 int height, width;
260 HDC32 hdc;
261 HWND32 parent;
263 self->numParts = 0;
264 self->parts = 0;
265 self->simple = TRUE;
266 GetClientRect32(hwnd, &rect);
268 /* initialize simple case */
269 self->part0.bound = rect;
270 self->part0.text = 0;
271 self->part0.x = 0;
272 self->part0.style = 0;
274 height = 40;
275 if ((hdc = GetDC(0))) {
276 TEXTMETRIC32A tm;
277 GetTextMetrics32A(hdc, &tm);
278 self->textHeight = tm.tmHeight;
279 ReleaseDC(0, hdc);
282 parent = GetParent(hwnd);
283 GetClientRect32(parent, &rect);
284 width = rect.right - rect.left;
285 height = (self->textHeight * 3)/2;
286 MoveWindow(hwnd, lpCreate->x, lpCreate->y-1, width, height, FALSE);
287 SW_SetPartBounds(hwnd, self);
288 return 0;
291 static LRESULT
292 SW_GetRect(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
294 int part_num;
295 LPRECT32 rect;
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;
306 static LRESULT
307 SW_GetText(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
309 int part_num;
310 LRESULT result;
311 STATUSWINDOWPART *part;
312 LPSTR out_text;
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];
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);
330 return result;
333 static LRESULT
334 SW_GetTextLength(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
336 int part_num;
337 STATUSWINDOWPART *part;
338 DWORD result;
340 part_num = ((INT32) wParam) & 0x00ff;
342 if (self->simple)
343 part = &self->part0;
344 else
345 part = &self->parts[part_num];
347 if (part->text)
348 result = lstrlen32A(part->text);
349 else
350 result = 0;
352 result |= (part->style << 16);
353 return result;
356 static LRESULT
357 SW_SetMinHeight(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
359 /* FIXME */
360 /* size is wParam | 2*pixels_of_horz_border */
361 return TRUE;
364 static LRESULT
365 SW_Simple(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
367 BOOL32 simple;
368 HDC32 hdc;
370 simple = (BOOL32) wParam;
371 self->simple = simple;
372 hdc = GetDC(hwnd);
373 SW_Refresh(hwnd, hdc, self);
374 ReleaseDC(hwnd, hdc);
375 return TRUE;
378 static LRESULT
379 SW_Size(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
381 /* Need to resize width to match parent */
382 INT32 width, height, x, y;
383 RECT32 parent_rect;
384 HWND32 parent;
386 INT32 flags;
388 flags = (INT32) wParam;
390 /* FIXME for flags =
391 * SIZE_MAXIMIZED, SIZE_MAXSHOW, SIZE_MINIMIZED, SIZE_RESTORED
394 if (flags == SIZE_RESTORED) {
395 /* width and height don't apply */
396 parent = GetParent(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 MoveWindow(hwnd, parent_rect.left, parent_rect.bottom - height - 1,
403 width, height, TRUE);
404 SW_SetPartBounds(hwnd, self);
406 return 0;
409 static LRESULT
410 SW_Destroy(STATUSWINDOWINFO *self, HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
412 int i;
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);
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;
426 static LRESULT
427 SW_Paint(STATUSWINDOWINFO *self, HWND32 hwnd)
429 HDC32 hdc;
430 PAINTSTRUCT32 ps;
432 hdc = BeginPaint32(hwnd, &ps);
433 SW_Refresh(hwnd, hdc, self);
434 EndPaint32(hwnd, &ps);
435 return 0;
438 LRESULT StatusWindowProc( HWND32 hwnd, UINT32 msg,
439 WPARAM32 wParam, LPARAM lParam )
441 STATUSWINDOWINFO *self;
443 self = GetStatusInfo(hwnd);
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);
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);
476 return 0;
480 /***********************************************************************
481 * CreateStatusWindow32A (COMCTL32.4)
483 HWND32 CreateStatusWindow32A( INT32 style, LPCSTR text, HWND32 parent,
484 UINT32 wid )
486 HWND32 ret;
487 ATOM atom;
489 atom = GlobalFindAtom32A(STATUSCLASSNAME32A);
490 if (!atom) {
491 /* Some apps don't call InitCommonControls */
492 InitCommonControls();
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);