Added a framework for testing CreateProcess and a few tests.
[wine] / dlls / comctl32 / status.c
1 /*
2  * Interface code to StatusWindow widget/control
3  *
4  * Copyright 1996 Bruce Milner
5  * Copyright 1998, 1999 Eric Kohl
6  * Copyright 2002 Dimitrie O. Paun
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * FIXME:
23  * 1) Implement all CCS_* styles.
24  * 2) Send WM_QUERYFORMAT
25  * 3) Use DrawEdge to draw the SizeGrip
26  * 4) Implement DrawStatusText and use it.
27  *
28  */
29
30 #include <string.h>
31
32 #include "winbase.h"
33 #include "wine/unicode.h"
34 #include "commctrl.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(statusbar);
38
39 typedef struct
40 {
41     INT         x;
42     INT         style;
43     RECT        bound;
44     LPWSTR      text;
45     HICON       hIcon;
46 } STATUSWINDOWPART;
47
48 typedef struct
49 {
50     HWND              Self;
51     WORD              numParts;
52     UINT              height;
53     BOOL              simple;
54     HWND              hwndToolTip;
55     HFONT             hFont;
56     HFONT             hDefaultFont;
57     COLORREF          clrBk;            /* background color */
58     BOOL              bUnicode;         /* unicode flag */
59     STATUSWINDOWPART  part0;            /* simple window */
60     STATUSWINDOWPART* parts;
61 } STATUSWINDOWINFO;
62
63 /*
64  * Run tests using Waite Group Windows95 API Bible Vol. 1&2
65  * The second cdrom contains executables drawstat.exe, gettext.exe,
66  * simple.exe, getparts.exe, setparts.exe, statwnd.exe
67  */
68
69 #define HORZ_BORDER 0
70 #define VERT_BORDER 2
71 #define HORZ_GAP    2
72
73 #define STATUSBAR_GetInfoPtr(hwnd) ((STATUSWINDOWINFO *)GetWindowLongW (hwnd, 0))
74
75 /* prototype */
76 static void
77 STATUSBAR_SetPartBounds (STATUSWINDOWINFO *infoPtr);
78
79 static inline LPCSTR debugstr_t(LPCWSTR text, BOOL isW)
80 {
81   return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
82 }
83
84 static void
85 STATUSBAR_DrawSizeGrip (HDC hdc, LPRECT lpRect)
86 {
87     HPEN hOldPen;
88     POINT pt;
89     INT i;
90
91     TRACE("draw size grip %d,%d - %d,%d\n", lpRect->left, lpRect->top, lpRect->right, lpRect->bottom);
92
93     //FIXME: use DrawEdge to draw this
94     pt.x = lpRect->right - 1;
95     pt.y = lpRect->bottom - 1;
96
97     hOldPen = SelectObject (hdc, GetSysColorPen (COLOR_3DFACE));
98     MoveToEx (hdc, pt.x - 12, pt.y, NULL);
99     LineTo (hdc, pt.x, pt.y);
100     LineTo (hdc, pt.x, pt.y - 12);
101
102     pt.x--;
103     pt.y--;
104
105     SelectObject (hdc, GetSysColorPen (COLOR_3DSHADOW));
106     for (i = 1; i < 11; i += 4) {
107         MoveToEx (hdc, pt.x - i, pt.y, NULL);
108         LineTo (hdc, pt.x, pt.y - i);
109
110         MoveToEx (hdc, pt.x - i-1, pt.y, NULL);
111         LineTo (hdc, pt.x, pt.y - i-1);
112     }
113
114     SelectObject (hdc, GetSysColorPen (COLOR_3DHIGHLIGHT));
115     for (i = 3; i < 13; i += 4) {
116         MoveToEx (hdc, pt.x - i, pt.y, NULL);
117         LineTo (hdc, pt.x, pt.y - i);
118     }
119
120     SelectObject (hdc, hOldPen);
121 }
122
123
124 static void 
125 STATUSBAR_DrawPart (HDC hdc, STATUSWINDOWPART *part)
126 {
127     RECT r = part->bound;
128     UINT border = BDR_SUNKENOUTER;
129
130     TRACE("part bound %d,%d - %d,%d\n", r.left, r.top, r.right, r.bottom);
131     if (part->style & SBT_POPOUT)
132         border = BDR_RAISEDOUTER;
133     else if (part->style & SBT_NOBORDERS)
134         border = 0;
135
136     DrawEdge(hdc, &r, border, BF_RECT|BF_ADJUST);
137
138     /* draw the icon */
139     if (part->hIcon) {
140         INT cy = r.bottom - r.top;
141
142         r.left += 2;
143         DrawIconEx (hdc, r.left, r.top, part->hIcon, cy, cy, 0, 0, DI_NORMAL);
144         r.left += cy;
145     }
146
147     /* now draw text */
148     if (part->text) {
149         int oldbkmode = SetBkMode(hdc, TRANSPARENT);
150         LPWSTR p = (LPWSTR)part->text;
151         UINT align = DT_LEFT;
152         if (*p == L'\t') {
153             p++;
154             align = DT_CENTER;
155
156             if (*p == L'\t') {
157                 p++;
158                 align = DT_RIGHT;
159             }
160         }
161         r.left += 3;
162         TRACE("%s at %d,%d - %d,%d\n", debugstr_w(p), r.left, r.top, r.right, r.bottom);
163         DrawTextW (hdc, p, -1, &r, align|DT_VCENTER|DT_SINGLELINE);
164         SetBkMode(hdc, oldbkmode);
165     }
166 }
167
168
169 static void
170 STATUSBAR_RefreshPart (STATUSWINDOWINFO *infoPtr, STATUSWINDOWPART *part, HDC hdc, int itemID)
171 {
172     HBRUSH hbrBk;
173     HFONT  hOldFont;
174
175     TRACE("item %d\n", itemID);
176     if (!IsWindowVisible (infoPtr->Self))
177         return;
178
179     if (part->bound.right < part->bound.left) return;
180
181     if (infoPtr->clrBk != CLR_DEFAULT)
182         hbrBk = CreateSolidBrush (infoPtr->clrBk);
183     else
184         hbrBk = GetSysColorBrush (COLOR_3DFACE);
185     FillRect(hdc, &part->bound, hbrBk);
186
187     hOldFont = SelectObject (hdc, infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont);
188
189     if (part->style & SBT_OWNERDRAW) {
190         DRAWITEMSTRUCT dis;
191
192         dis.CtlID = GetWindowLongW (infoPtr->Self, GWL_ID);
193         dis.itemID = itemID;
194         dis.hwndItem = infoPtr->Self;
195         dis.hDC = hdc;
196         dis.rcItem = part->bound;
197         dis.itemData = (INT)part->text;
198         SendMessageW (GetParent (infoPtr->Self), WM_DRAWITEM,
199                 (WPARAM)dis.CtlID, (LPARAM)&dis);
200     } else
201         STATUSBAR_DrawPart (hdc, part);
202
203     SelectObject (hdc, hOldFont);
204
205     if (infoPtr->clrBk != CLR_DEFAULT)
206         DeleteObject (hbrBk);
207
208     if (GetWindowLongW (infoPtr->Self, GWL_STYLE) & SBARS_SIZEGRIP) {
209         RECT rect;
210
211         GetClientRect (infoPtr->Self, &rect);
212         STATUSBAR_DrawSizeGrip (hdc, &rect);
213     }
214 }
215
216
217 static LRESULT
218 STATUSBAR_Refresh (STATUSWINDOWINFO *infoPtr, HDC hdc)
219 {
220     int      i;
221     RECT   rect;
222     HBRUSH hbrBk;
223     HFONT  hOldFont;
224
225     TRACE("\n");
226     if (!IsWindowVisible(infoPtr->Self))
227         return 0;
228
229     STATUSBAR_SetPartBounds(infoPtr);
230
231     GetClientRect (infoPtr->Self, &rect);
232
233     if (infoPtr->clrBk != CLR_DEFAULT)
234         hbrBk = CreateSolidBrush (infoPtr->clrBk);
235     else
236         hbrBk = GetSysColorBrush (COLOR_3DFACE);
237     FillRect(hdc, &rect, hbrBk);
238
239     hOldFont = SelectObject (hdc, infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont);
240
241     if (infoPtr->simple) {
242         STATUSBAR_RefreshPart (infoPtr, &infoPtr->part0, hdc, 0);
243     } else {
244         for (i = 0; i < infoPtr->numParts; i++) {
245             if (infoPtr->parts[i].style & SBT_OWNERDRAW) {
246                 DRAWITEMSTRUCT dis;
247
248                 dis.CtlID = GetWindowLongW (infoPtr->Self, GWL_ID);
249                 dis.itemID = i;
250                 dis.hwndItem = infoPtr->Self;
251                 dis.hDC = hdc;
252                 dis.rcItem = infoPtr->parts[i].bound;
253                 dis.itemData = (INT)infoPtr->parts[i].text;
254                 SendMessageW (GetParent (infoPtr->Self), WM_DRAWITEM,
255                         (WPARAM)dis.CtlID, (LPARAM)&dis);
256             } else
257                 STATUSBAR_RefreshPart (infoPtr, &infoPtr->parts[i], hdc, i);
258         }
259     }
260
261     SelectObject (hdc, hOldFont);
262
263     if (infoPtr->clrBk != CLR_DEFAULT)
264         DeleteObject (hbrBk);
265
266     if (GetWindowLongW (infoPtr->Self, GWL_STYLE) & SBARS_SIZEGRIP)
267         STATUSBAR_DrawSizeGrip (hdc, &rect);
268
269     return 0;
270 }
271
272
273 static void
274 STATUSBAR_SetPartBounds (STATUSWINDOWINFO *infoPtr)
275 {
276     STATUSWINDOWPART *part;
277     RECT rect, *r;
278     int i;
279
280     /* get our window size */
281     GetClientRect (infoPtr->Self, &rect);
282     TRACE("client wnd size is %d,%d - %d,%d\n", rect.left, rect.top, rect.right, rect.bottom);
283
284     rect.top += VERT_BORDER;
285
286     /* set bounds for simple rectangle */
287     infoPtr->part0.bound = rect;
288
289     /* set bounds for non-simple rectangles */
290     for (i = 0; i < infoPtr->numParts; i++) {
291         part = &infoPtr->parts[i];
292         r = &infoPtr->parts[i].bound;
293         r->top = rect.top;
294         r->bottom = rect.bottom;
295         if (i == 0)
296             r->left = 0;
297         else
298             r->left = infoPtr->parts[i-1].bound.right + HORZ_GAP;
299         if (part->x == -1)
300             r->right = rect.right;
301         else
302             r->right = part->x;
303
304         if (infoPtr->hwndToolTip) {
305             TTTOOLINFOW ti;
306
307             ti.cbSize = sizeof(TTTOOLINFOW);
308             ti.hwnd = infoPtr->Self;
309             ti.uId = i;
310             ti.rect = *r;
311             SendMessageW (infoPtr->hwndToolTip, TTM_NEWTOOLRECTW,
312                             0, (LPARAM)&ti);
313         }
314     }
315 }
316
317
318 static LRESULT
319 STATUSBAR_Relay2Tip (STATUSWINDOWINFO *infoPtr, UINT uMsg,
320                      WPARAM wParam, LPARAM lParam)
321 {
322     MSG msg;
323
324     msg.hwnd = infoPtr->Self;
325     msg.message = uMsg;
326     msg.wParam = wParam;
327     msg.lParam = lParam;
328     msg.time = GetMessageTime ();
329     msg.pt.x = LOWORD(GetMessagePos ());
330     msg.pt.y = HIWORD(GetMessagePos ());
331
332     return SendMessageW (infoPtr->hwndToolTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
333 }
334
335
336 static BOOL
337 STATUSBAR_GetBorders (INT out[])
338 {
339     TRACE("\n");
340     out[0] = HORZ_BORDER; /* horizontal border width */
341     out[1] = VERT_BORDER; /* vertical border width */
342     out[2] = HORZ_GAP; /* width of border between rectangles */
343
344     return TRUE;
345 }
346
347
348 static HICON
349 STATUSBAR_GetIcon (STATUSWINDOWINFO *infoPtr, INT nPart)
350 {
351     TRACE("%d\n", nPart);
352     if ((nPart < -1) || (nPart >= infoPtr->numParts))
353         return 0;
354
355     if (nPart == -1)
356         return (infoPtr->part0.hIcon);
357     else
358         return (infoPtr->parts[nPart].hIcon);
359 }
360
361
362 static INT
363 STATUSBAR_GetParts (STATUSWINDOWINFO *infoPtr, INT num_parts, INT parts[])
364 {
365     INT   i;
366
367     TRACE("(%d)\n", num_parts);
368     if (parts) {
369         for (i = 0; i < num_parts; i++) {
370             parts[i] = infoPtr->parts[i].x;
371         }
372     }
373     return infoPtr->numParts;
374 }
375
376
377 static BOOL
378 STATUSBAR_GetRect (STATUSWINDOWINFO *infoPtr, INT nPart, LPRECT rect)
379 {
380     TRACE("part %d\n", nPart);
381     if (infoPtr->simple)
382         *rect = infoPtr->part0.bound;
383     else
384         *rect = infoPtr->parts[nPart].bound;
385     return TRUE;
386 }
387
388
389 static LRESULT
390 STATUSBAR_GetTextA (STATUSWINDOWINFO *infoPtr, INT nPart, LPSTR buf)
391 {
392     STATUSWINDOWPART *part;
393     LRESULT result;
394
395     TRACE("part %d\n", nPart);
396     if (nPart < 0 || nPart >= infoPtr->numParts) return 0;
397
398     if (infoPtr->simple)
399         part = &infoPtr->part0;
400     else
401         part = &infoPtr->parts[nPart];
402
403     if (part->style & SBT_OWNERDRAW)
404         result = (LRESULT)part->text;
405     else {
406         DWORD len = part->text ? WideCharToMultiByte( CP_ACP, 0, part->text, -1,
407                                                       NULL, 0, NULL, NULL ) - 1 : 0;
408         result = MAKELONG( len, part->style );
409         if (part->text && buf)
410             WideCharToMultiByte( CP_ACP, 0, part->text, -1, buf, len+1, NULL, NULL );
411     }
412     return result;
413 }
414
415
416 static LRESULT
417 STATUSBAR_GetTextW (STATUSWINDOWINFO *infoPtr, INT nPart, LPWSTR buf)
418 {
419     STATUSWINDOWPART *part;
420     LRESULT result;
421
422     TRACE("part %d\n", nPart);
423     if (nPart < 0 || nPart >= infoPtr->numParts) return 0;
424
425     if (infoPtr->simple)
426         part = &infoPtr->part0;
427     else
428         part = &infoPtr->parts[nPart];
429
430     if (part->style & SBT_OWNERDRAW)
431         result = (LRESULT)part->text;
432     else {
433         result = part->text ? strlenW (part->text) : 0;
434         result |= (part->style << 16);
435         if (part->text && buf)
436             strcpyW (buf, part->text);
437     }
438     return result;
439 }
440
441
442 static LRESULT
443 STATUSBAR_GetTextLength (STATUSWINDOWINFO *infoPtr, INT nPart)
444 {
445     STATUSWINDOWPART *part;
446     DWORD result;
447
448     TRACE("part %d\n", nPart);
449     if (infoPtr->simple)
450         part = &infoPtr->part0;
451     else
452         part = &infoPtr->parts[nPart];
453
454     if (part->text)
455         result = strlenW(part->text);
456     else
457         result = 0;
458
459     result |= (part->style << 16);
460     return result;
461 }
462
463 static LRESULT
464 STATUSBAR_GetTipTextA (STATUSWINDOWINFO *infoPtr, INT id, LPSTR tip, INT size)
465 {
466     TRACE("\n");
467     if (tip) {
468         CHAR buf[INFOTIPSIZE];
469         buf[0]='\0';
470
471         if (infoPtr->hwndToolTip) {
472             TTTOOLINFOA ti;
473             ti.cbSize = sizeof(TTTOOLINFOA);
474             ti.hwnd = infoPtr->Self;
475             ti.uId = id;
476             ti.lpszText = buf;
477             SendMessageA (infoPtr->hwndToolTip, TTM_GETTEXTA, 0, (LPARAM)&ti);
478         }
479         lstrcpynA (tip, buf, size);
480     }
481     return 0;
482 }
483
484
485 static LRESULT
486 STATUSBAR_GetTipTextW (STATUSWINDOWINFO *infoPtr, INT id, LPWSTR tip, INT size)
487 {
488     TRACE("\n");
489     if (tip) {
490         WCHAR buf[INFOTIPSIZE];
491         buf[0]=0;
492
493         if (infoPtr->hwndToolTip) {
494             TTTOOLINFOW ti;
495             ti.cbSize = sizeof(TTTOOLINFOW);
496             ti.hwnd = infoPtr->Self;
497             ti.uId = id;
498             ti.lpszText = buf;
499             SendMessageW(infoPtr->hwndToolTip, TTM_GETTEXTW, 0, (LPARAM)&ti);
500         }
501         lstrcpynW(tip, buf, size);
502     }
503
504     return 0;
505 }
506
507
508 static COLORREF
509 STATUSBAR_SetBkColor (STATUSWINDOWINFO *infoPtr, COLORREF color)
510 {
511     COLORREF oldBkColor;
512
513     oldBkColor = infoPtr->clrBk;
514     infoPtr->clrBk = color;
515     InvalidateRect(infoPtr->Self, NULL, FALSE);
516
517     TRACE("CREF: %08lx -> %08lx\n", oldBkColor, infoPtr->clrBk);
518     return oldBkColor;
519 }
520
521
522 static BOOL
523 STATUSBAR_SetIcon (STATUSWINDOWINFO *infoPtr, INT nPart, HICON hIcon)
524 {
525     if ((nPart < -1) || (nPart >= infoPtr->numParts))
526         return FALSE;
527
528     TRACE("setting part %d\n", nPart);
529
530     if (nPart == -1) {
531         if (infoPtr->part0.hIcon == hIcon) /* same as - no redraw */
532             return TRUE;
533         infoPtr->part0.hIcon = hIcon;
534         if (infoPtr->simple)
535             InvalidateRect(infoPtr->Self, &infoPtr->part0.bound, FALSE);
536     } else {
537         if (infoPtr->parts[nPart].hIcon == hIcon) /* same as - no redraw */
538             return TRUE;
539
540         infoPtr->parts[nPart].hIcon = hIcon;
541         if (!(infoPtr->simple))
542             InvalidateRect(infoPtr->Self, &infoPtr->parts[nPart].bound, FALSE);
543     }
544     return TRUE;
545 }
546
547
548 static BOOL
549 STATUSBAR_SetMinHeight (STATUSWINDOWINFO *infoPtr, INT height)
550 {
551
552     TRACE("(height=%d)\n", height);
553     if (IsWindowVisible (infoPtr->Self)) {
554         INT  width, x, y;
555         RECT parent_rect;
556
557         GetClientRect (GetParent (infoPtr->Self), &parent_rect);
558         infoPtr->height = height + VERT_BORDER;
559         width = parent_rect.right - parent_rect.left;
560         x = parent_rect.left;
561         y = parent_rect.bottom - infoPtr->height;
562         MoveWindow (infoPtr->Self, parent_rect.left,
563                       parent_rect.bottom - infoPtr->height,
564                       width, infoPtr->height, TRUE);
565         STATUSBAR_SetPartBounds (infoPtr);
566     }
567
568     return TRUE;
569 }
570
571
572 static BOOL
573 STATUSBAR_SetParts (STATUSWINDOWINFO *infoPtr, INT count, LPINT parts)
574 {
575     STATUSWINDOWPART *tmp;
576     int i, oldNumParts;
577
578     TRACE("(%d,%p)\n", count, parts);
579
580     oldNumParts = infoPtr->numParts;
581     infoPtr->numParts = count;
582     if (oldNumParts > infoPtr->numParts) {
583         for (i = infoPtr->numParts ; i < oldNumParts; i++) {
584             if (infoPtr->parts[i].text && !(infoPtr->parts[i].style & SBT_OWNERDRAW))
585                 COMCTL32_Free (infoPtr->parts[i].text);
586         }
587     } else if (oldNumParts < infoPtr->numParts) {
588         tmp = COMCTL32_Alloc (sizeof(STATUSWINDOWPART) * infoPtr->numParts);
589         if (!tmp) return FALSE;
590         for (i = 0; i < oldNumParts; i++) {
591             tmp[i] = infoPtr->parts[i];
592         }
593         if (infoPtr->parts)
594             COMCTL32_Free (infoPtr->parts);
595         infoPtr->parts = tmp;
596     }
597     if (oldNumParts == infoPtr->numParts) {
598         for (i=0; i < oldNumParts; i++)
599             if (infoPtr->parts[i].x != parts[i])
600                 break;
601         if (i==oldNumParts) /* Unchanged? no need to redraw! */
602             return TRUE;
603     }
604     
605     for (i = 0; i < infoPtr->numParts; i++)
606         infoPtr->parts[i].x = parts[i];
607
608     if (infoPtr->hwndToolTip) {
609         INT nTipCount, i;
610         TTTOOLINFOW ti;
611
612         ZeroMemory (&ti, sizeof(TTTOOLINFOW));
613         ti.cbSize = sizeof(TTTOOLINFOW);
614         ti.hwnd = infoPtr->Self;
615
616         nTipCount = SendMessageW (infoPtr->hwndToolTip, TTM_GETTOOLCOUNT, 0, 0);
617         if (nTipCount < infoPtr->numParts) {
618             /* add tools */
619             for (i = nTipCount; i < infoPtr->numParts; i++) {
620                 TRACE("add tool %d\n", i);
621                 ti.uId = i;
622                 SendMessageW (infoPtr->hwndToolTip, TTM_ADDTOOLW,
623                                 0, (LPARAM)&ti);
624             }
625         }
626         else if (nTipCount > infoPtr->numParts) {
627             /* delete tools */
628             for (i = nTipCount - 1; i >= infoPtr->numParts; i--) {
629                 TRACE("delete tool %d\n", i);
630                 ti.uId = i;
631                 SendMessageW (infoPtr->hwndToolTip, TTM_DELTOOLW,
632                                 0, (LPARAM)&ti);
633             }
634         }
635     }
636     STATUSBAR_SetPartBounds (infoPtr);
637     InvalidateRect(infoPtr->Self, NULL, FALSE);
638     return TRUE;
639 }
640
641
642 static BOOL
643 STATUSBAR_SetTextT (STATUSWINDOWINFO *infoPtr, INT nPart, WORD style, 
644                     LPCWSTR text, BOOL isW)
645 {
646     STATUSWINDOWPART *part=NULL;
647     BOOL changed = FALSE;
648
649     TRACE("part %d, text %s\n", nPart, debugstr_t(text, isW));
650     if (nPart < 0 || nPart >= infoPtr->numParts) return FALSE;
651
652     if (nPart == 0x00ff)
653         part = &infoPtr->part0;
654     else if (!infoPtr->simple && infoPtr->parts)
655         part = &infoPtr->parts[nPart];
656     if (!part) return FALSE;
657
658     if (part->style != style)
659         changed = TRUE;
660
661     part->style = style;
662     if (style & SBT_OWNERDRAW) {
663         if (part->text == text)
664             return TRUE;
665         part->text = (LPWSTR)text;
666     } else {
667         LPWSTR ntext;
668
669         if (text && !isW) {
670             LPCSTR atxt = (LPCSTR)text;
671             DWORD len = MultiByteToWideChar( CP_ACP, 0, atxt, -1, NULL, 0 );
672             ntext = COMCTL32_Alloc( (len + 1)*sizeof(WCHAR) );
673             if (!ntext) return FALSE;
674             MultiByteToWideChar( CP_ACP, 0, atxt, -1, ntext, len );
675         } else if (text) {
676             ntext = COMCTL32_Alloc( (strlenW(text) + 1)*sizeof(WCHAR) );
677             if (!ntext) return FALSE;
678             strcpyW (ntext, text);
679         } else ntext = 0;
680
681         /* check if text is unchanged -> no need to redraw */
682         if (text) {
683             if (!changed && part->text && !lstrcmpW(ntext, part->text)) {
684                 if (!isW) COMCTL32_Free(ntext);
685                 return TRUE;
686             }
687         } else {
688             if (!changed && !part->text) 
689                 return TRUE;
690         }
691
692         if (part->text)
693             COMCTL32_Free (part->text);
694         part->text = ntext;
695     }
696     InvalidateRect(infoPtr->Self, &part->bound, FALSE);
697
698     return TRUE;
699 }
700
701
702 static LRESULT
703 STATUSBAR_SetTipTextA (STATUSWINDOWINFO *infoPtr, INT id, LPSTR text)
704 {
705     TRACE("part %d: \"%s\"\n", id, text);
706     if (infoPtr->hwndToolTip) {
707         TTTOOLINFOA ti;
708         ti.cbSize = sizeof(TTTOOLINFOA);
709         ti.hwnd = infoPtr->Self;
710         ti.uId = id;
711         ti.hinst = 0;
712         ti.lpszText = text;
713         SendMessageA (infoPtr->hwndToolTip, TTM_UPDATETIPTEXTA,
714                         0, (LPARAM)&ti);
715     }
716
717     return 0;
718 }
719
720
721 static LRESULT
722 STATUSBAR_SetTipTextW (STATUSWINDOWINFO *infoPtr, INT id, LPWSTR text)
723 {
724     TRACE("part %d: \"%s\"\n", id, debugstr_w(text));
725     if (infoPtr->hwndToolTip) {
726         TTTOOLINFOW ti;
727         ti.cbSize = sizeof(TTTOOLINFOW);
728         ti.hwnd = infoPtr->Self;
729         ti.uId = id;
730         ti.hinst = 0;
731         ti.lpszText = text;
732         SendMessageW (infoPtr->hwndToolTip, TTM_UPDATETIPTEXTW,
733                         0, (LPARAM)&ti);
734     }
735
736     return 0;
737 }
738
739
740 inline static LRESULT
741 STATUSBAR_SetUnicodeFormat (STATUSWINDOWINFO *infoPtr, BOOL bUnicode)
742 {
743     BOOL bOld = infoPtr->bUnicode;
744
745     TRACE("(0x%x)\n", bUnicode);
746     infoPtr->bUnicode = bUnicode;
747
748     return bOld;
749 }
750
751
752 static BOOL
753 STATUSBAR_Simple (STATUSWINDOWINFO *infoPtr, BOOL simple)
754 {
755     NMHDR  nmhdr;
756
757     TRACE("(simple=%d)\n", simple);
758     if (infoPtr->simple == simple) /* no need to change */
759         return TRUE;
760
761     infoPtr->simple = simple;
762
763     /* send notification */
764     nmhdr.hwndFrom = infoPtr->Self;
765     nmhdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
766     nmhdr.code = SBN_SIMPLEMODECHANGE;
767     SendMessageW (GetParent (infoPtr->Self), WM_NOTIFY, 0, (LPARAM)&nmhdr);
768     InvalidateRect(infoPtr->Self, NULL, FALSE);
769     return TRUE;
770 }
771
772
773 static LRESULT
774 STATUSBAR_WMDestroy (STATUSWINDOWINFO *infoPtr)
775 {
776     int i;
777
778     TRACE("\n");
779     for (i = 0; i < infoPtr->numParts; i++) {
780         if (infoPtr->parts[i].text && !(infoPtr->parts[i].style & SBT_OWNERDRAW))
781             COMCTL32_Free (infoPtr->parts[i].text);
782     }
783     if (infoPtr->part0.text && !(infoPtr->part0.style & SBT_OWNERDRAW))
784         COMCTL32_Free (infoPtr->part0.text);
785     COMCTL32_Free (infoPtr->parts);
786
787     /* delete default font */
788     if (infoPtr->hDefaultFont)
789         DeleteObject (infoPtr->hDefaultFont);
790
791     /* delete tool tip control */
792     if (infoPtr->hwndToolTip)
793         DestroyWindow (infoPtr->hwndToolTip);
794
795     COMCTL32_Free (infoPtr);
796     SetWindowLongW(infoPtr->Self, 0, 0);
797     return 0;
798 }
799
800
801 static LRESULT
802 STATUSBAR_WMCreate (HWND hwnd, LPCREATESTRUCTA lpCreate)
803 {
804     STATUSWINDOWINFO *infoPtr;
805     NONCLIENTMETRICSW nclm;
806     DWORD dwStyle;
807     RECT rect;
808     int width, len, textHeight = 0;
809     HDC hdc;
810
811     TRACE("\n");
812     infoPtr = (STATUSWINDOWINFO*)COMCTL32_Alloc (sizeof(STATUSWINDOWINFO));
813     if (!infoPtr) goto create_fail;
814     SetWindowLongW (hwnd, 0, (DWORD)infoPtr);
815
816     infoPtr->Self = hwnd;
817     infoPtr->numParts = 1;
818     infoPtr->parts = 0;
819     infoPtr->simple = FALSE;
820     infoPtr->clrBk = CLR_DEFAULT;
821     infoPtr->hFont = 0;
822
823     /* FIXME: send unicode parent notification query (WM_QUERYFORMAT) here */
824
825     GetClientRect (hwnd, &rect);
826     InvalidateRect (hwnd, &rect, 0);
827     UpdateWindow(hwnd);
828
829     ZeroMemory (&nclm, sizeof(nclm));
830     nclm.cbSize = sizeof(nclm);
831     SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, nclm.cbSize, &nclm, 0);
832     infoPtr->hDefaultFont = CreateFontIndirectW (&nclm.lfStatusFont);
833
834     /* initialize simple case */
835     infoPtr->part0.bound = rect;
836     infoPtr->part0.text = 0;
837     infoPtr->part0.x = 0;
838     infoPtr->part0.style = 0;
839     infoPtr->part0.hIcon = 0;
840
841     /* initialize first part */
842     infoPtr->parts = COMCTL32_Alloc (sizeof(STATUSWINDOWPART));
843     if (!infoPtr->parts) goto create_fail;
844     infoPtr->parts[0].bound = rect;
845     infoPtr->parts[0].text = 0;
846     infoPtr->parts[0].x = -1;
847     infoPtr->parts[0].style = 0;
848     infoPtr->parts[0].hIcon = 0;
849
850     if (IsWindowUnicode (hwnd)) {
851         infoPtr->bUnicode = TRUE;
852         if (lpCreate->lpszName &&
853             (len = strlenW ((LPCWSTR)lpCreate->lpszName))) {
854             infoPtr->parts[0].text = COMCTL32_Alloc ((len + 1)*sizeof(WCHAR));
855             if (!infoPtr->parts[0].text) goto create_fail;
856             strcpyW (infoPtr->parts[0].text, (LPCWSTR)lpCreate->lpszName);
857         }
858     }
859     else {
860         if (lpCreate->lpszName &&
861             (len = strlen((LPCSTR)lpCreate->lpszName))) {
862             DWORD lenW = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)lpCreate->lpszName, -1, NULL, 0 );
863             infoPtr->parts[0].text = COMCTL32_Alloc (lenW*sizeof(WCHAR));
864             if (!infoPtr->parts[0].text) goto create_fail;
865             MultiByteToWideChar( CP_ACP, 0, (LPCSTR)lpCreate->lpszName, -1,
866                                  infoPtr->parts[0].text, lenW );
867         }
868     }
869
870     dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
871
872     /* statusbars on managed windows should not have SIZEGRIP style */
873     if ((dwStyle & SBARS_SIZEGRIP) && lpCreate->hwndParent)
874         if (GetWindowLongW (lpCreate->hwndParent, GWL_EXSTYLE) & WS_EX_MANAGED)
875             SetWindowLongW (hwnd, GWL_STYLE, dwStyle & ~SBARS_SIZEGRIP);
876
877     if ((hdc = GetDC (0))) {
878         TEXTMETRICW tm;
879         HFONT hOldFont;
880
881         hOldFont = SelectObject (hdc, infoPtr->hDefaultFont);
882         GetTextMetricsW (hdc, &tm);
883         textHeight = tm.tmHeight;
884         SelectObject (hdc, hOldFont);
885         ReleaseDC (0, hdc);
886     }
887     TRACE("    textHeight=%d\n", textHeight);
888
889     if (dwStyle & SBT_TOOLTIPS) {
890         infoPtr->hwndToolTip =
891             CreateWindowExW (0, TOOLTIPS_CLASSW, NULL, 0,
892                                CW_USEDEFAULT, CW_USEDEFAULT,
893                                CW_USEDEFAULT, CW_USEDEFAULT,
894                              hwnd, 0,
895                              GetWindowLongW (hwnd, GWL_HINSTANCE), NULL);
896
897         if (infoPtr->hwndToolTip) {
898             NMTOOLTIPSCREATED nmttc;
899
900             nmttc.hdr.hwndFrom = hwnd;
901             nmttc.hdr.idFrom = GetWindowLongW (hwnd, GWL_ID);
902             nmttc.hdr.code = NM_TOOLTIPSCREATED;
903             nmttc.hwndToolTips = infoPtr->hwndToolTip;
904
905             SendMessageW (lpCreate->hwndParent, WM_NOTIFY,
906                             (WPARAM)nmttc.hdr.idFrom, (LPARAM)&nmttc);
907         } 
908     }
909
910     if (!(dwStyle & CCS_NORESIZE)) { /* don't resize wnd if it doesn't want it ! */
911         GetClientRect (GetParent (hwnd), &rect);
912         width = rect.right - rect.left;
913         infoPtr->height = textHeight + 4 + VERT_BORDER;
914         SetWindowPos(hwnd, 0, lpCreate->x, lpCreate->y - 1,
915                         width, infoPtr->height, SWP_NOZORDER);
916         STATUSBAR_SetPartBounds (infoPtr);
917     }
918
919     return 0;
920     
921 create_fail:
922     TRACE("    failed!\n");
923     if (infoPtr) STATUSBAR_WMDestroy(infoPtr);
924     return -1;
925 }
926
927
928 /* in contrast to SB_GETTEXT*, WM_GETTEXT handles the text
929  * of the first part only (usual behaviour) */
930 static INT
931 STATUSBAR_WMGetText (STATUSWINDOWINFO *infoPtr, INT size, LPWSTR buf)
932 {
933     INT len;
934
935     TRACE("\n");
936     if (!(infoPtr->parts[0].text))
937         return 0;
938     if (infoPtr->bUnicode)
939         len = strlenW (infoPtr->parts[0].text);
940     else
941         len = WideCharToMultiByte( CP_ACP, 0, infoPtr->parts[0].text, -1, NULL, 0, NULL, NULL )-1;
942
943     if (size > len) {
944         if (infoPtr->bUnicode)
945             strcpyW (buf, infoPtr->parts[0].text);
946         else
947             WideCharToMultiByte( CP_ACP, 0, infoPtr->parts[0].text, -1,
948                                  (LPSTR)buf, len+1, NULL, NULL );
949         return len;
950     }
951
952     return -1;
953 }
954
955
956 static BOOL
957 STATUSBAR_WMNCHitTest (STATUSWINDOWINFO *infoPtr, INT x, INT y)
958 {
959     if (GetWindowLongW (infoPtr->Self, GWL_STYLE) & SBARS_SIZEGRIP) {
960         RECT  rect;
961         POINT pt;
962
963         GetClientRect (infoPtr->Self, &rect);
964
965         pt.x = x;
966         pt.y = y;
967         ScreenToClient (infoPtr->Self, &pt);
968
969         rect.left = rect.right - 13;
970         rect.top += 2;
971
972         if (PtInRect (&rect, pt))
973             return HTBOTTOMRIGHT;
974     }
975
976     return HTERROR;
977 }
978
979
980 static LRESULT
981 STATUSBAR_WMPaint (STATUSWINDOWINFO *infoPtr, HDC hdc)
982 {
983     PAINTSTRUCT ps;
984
985     TRACE("\n");
986     if (hdc) return STATUSBAR_Refresh (infoPtr, hdc);
987     hdc = BeginPaint (infoPtr->Self, &ps);
988     STATUSBAR_Refresh (infoPtr, hdc);
989     EndPaint (infoPtr->Self, &ps);
990
991     return 0;
992 }
993
994
995 static LRESULT
996 STATUSBAR_WMSetFont (STATUSWINDOWINFO *infoPtr, HFONT font, BOOL redraw)
997 {
998     infoPtr->hFont = font;
999     TRACE("%04x\n", infoPtr->hFont);
1000     if (redraw)
1001         InvalidateRect(infoPtr->Self, NULL, FALSE);
1002
1003     return 0;
1004 }
1005
1006
1007 static BOOL
1008 STATUSBAR_WMSetText (STATUSWINDOWINFO *infoPtr, LPCSTR text)
1009 {
1010     STATUSWINDOWPART *part;
1011     int len;
1012
1013     TRACE("\n");
1014     if (infoPtr->numParts == 0)
1015         return FALSE;
1016
1017     part = &infoPtr->parts[0];
1018     /* duplicate string */
1019     if (part->text)
1020         COMCTL32_Free (part->text);
1021     part->text = 0;
1022     if (infoPtr->bUnicode) {
1023         if (text && (len = strlenW((LPCWSTR)text))) {
1024             part->text = COMCTL32_Alloc ((len+1)*sizeof(WCHAR));
1025             if (!part->text) return FALSE;
1026             strcpyW (part->text, (LPCWSTR)text);
1027         }
1028     }
1029     else {
1030         if (text && (len = lstrlenA(text))) {
1031             DWORD lenW = MultiByteToWideChar( CP_ACP, 0, text, -1, NULL, 0 );
1032             part->text = COMCTL32_Alloc (lenW*sizeof(WCHAR));
1033             if (!part->text) return FALSE;
1034             MultiByteToWideChar( CP_ACP, 0, text, -1, part->text, lenW );
1035         }
1036     }
1037
1038     InvalidateRect(infoPtr->Self, &part->bound, FALSE);
1039
1040     return TRUE;
1041 }
1042
1043
1044 static BOOL
1045 STATUSBAR_WMSize (STATUSWINDOWINFO *infoPtr, WORD flags)
1046 {
1047     INT  width, x, y;
1048     RECT parent_rect;
1049
1050     /* Need to resize width to match parent */
1051     TRACE("flags %04x\n", flags);
1052
1053     if (flags != SIZE_RESTORED) {
1054         WARN("flags MUST be SIZE_RESTORED\n");
1055         return FALSE;
1056     }
1057     if (GetWindowLongW(infoPtr->Self, GWL_STYLE) & CCS_NORESIZE) return FALSE;
1058     
1059     /* width and height don't apply */
1060     GetClientRect (GetParent(infoPtr->Self), &parent_rect);
1061     width = parent_rect.right - parent_rect.left;
1062     x = parent_rect.left;
1063     y = parent_rect.bottom - infoPtr->height;
1064     MoveWindow (infoPtr->Self, parent_rect.left, 
1065                 parent_rect.bottom - infoPtr->height,
1066                 width, infoPtr->height, TRUE);
1067     STATUSBAR_SetPartBounds (infoPtr);
1068     return TRUE;
1069 }
1070
1071
1072 static LRESULT
1073 STATUSBAR_SendNotify (HWND hwnd, UINT code)
1074 {
1075     NMHDR  nmhdr;
1076
1077     TRACE("code %04x\n", code);
1078     nmhdr.hwndFrom = hwnd;
1079     nmhdr.idFrom = GetWindowLongW (hwnd, GWL_ID);
1080     nmhdr.code = code;
1081     SendMessageW (GetParent (hwnd), WM_NOTIFY, 0, (LPARAM)&nmhdr);
1082     return 0;
1083 }
1084
1085
1086
1087 static LRESULT WINAPI
1088 StatusWindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1089 {
1090     STATUSWINDOWINFO *infoPtr = STATUSBAR_GetInfoPtr(hwnd);
1091     INT nPart = ((INT) wParam) & 0x00ff;
1092     LRESULT res;
1093
1094     TRACE("hwnd=%x msg=%x wparam=%x lparam=%lx\n", hwnd, msg, wParam, lParam);
1095     if (!infoPtr && msg != WM_CREATE)
1096         return DefWindowProcW (hwnd, msg, wParam, lParam);
1097
1098     switch (msg) {
1099         case SB_GETBORDERS:
1100             return STATUSBAR_GetBorders ((INT *)lParam);
1101
1102         case SB_GETICON:
1103             return STATUSBAR_GetIcon (infoPtr, nPart);
1104
1105         case SB_GETPARTS:
1106             return STATUSBAR_GetParts (infoPtr, (INT)wParam, (INT *)lParam);
1107
1108         case SB_GETRECT:
1109             return STATUSBAR_GetRect (infoPtr, nPart, (LPRECT)lParam);
1110
1111         case SB_GETTEXTA:
1112             return STATUSBAR_GetTextA (infoPtr, nPart, (LPSTR)lParam);
1113
1114         case SB_GETTEXTW:
1115             return STATUSBAR_GetTextW (infoPtr, nPart, (LPWSTR)lParam);
1116
1117         case SB_GETTEXTLENGTHA:
1118         case SB_GETTEXTLENGTHW:
1119             return STATUSBAR_GetTextLength (infoPtr, nPart);
1120
1121         case SB_GETTIPTEXTA:
1122             return STATUSBAR_GetTipTextA (infoPtr,  LOWORD(wParam), (LPSTR)lParam,  HIWORD(wParam));
1123
1124         case SB_GETTIPTEXTW:
1125             return STATUSBAR_GetTipTextW (infoPtr,  LOWORD(wParam), (LPWSTR)lParam,  HIWORD(wParam));
1126
1127         case SB_GETUNICODEFORMAT:
1128             return infoPtr->bUnicode;
1129
1130         case SB_ISSIMPLE:
1131             return infoPtr->simple;
1132
1133         case SB_SETBKCOLOR:
1134             return STATUSBAR_SetBkColor (infoPtr, (COLORREF)lParam);
1135
1136         case SB_SETICON:
1137             return STATUSBAR_SetIcon (infoPtr, nPart, (HICON)lParam);
1138
1139         case SB_SETMINHEIGHT:
1140             return STATUSBAR_SetMinHeight (infoPtr, (INT)wParam);
1141
1142         case SB_SETPARTS:       
1143             return STATUSBAR_SetParts (infoPtr, (INT)wParam, (LPINT)lParam);
1144
1145         case SB_SETTEXTA:
1146             return STATUSBAR_SetTextT (infoPtr, nPart, wParam & 0xff00, (LPCWSTR)lParam, FALSE);
1147
1148         case SB_SETTEXTW:
1149             return STATUSBAR_SetTextT (infoPtr, nPart, wParam & 0xff00, (LPCWSTR)lParam, TRUE);
1150
1151         case SB_SETTIPTEXTA:
1152             return STATUSBAR_SetTipTextA (infoPtr, (INT)wParam, (LPSTR)lParam);
1153
1154         case SB_SETTIPTEXTW:
1155             return STATUSBAR_SetTipTextW (infoPtr, (INT)wParam, (LPWSTR)lParam);
1156
1157         case SB_SETUNICODEFORMAT:
1158             return STATUSBAR_SetUnicodeFormat (infoPtr, (BOOL)wParam);
1159
1160         case SB_SIMPLE:
1161             return STATUSBAR_Simple (infoPtr, (BOOL)wParam);
1162
1163         case WM_CREATE:
1164             return STATUSBAR_WMCreate (hwnd, (LPCREATESTRUCTA)lParam);
1165
1166         case WM_DESTROY:
1167             return STATUSBAR_WMDestroy (infoPtr);
1168
1169         case WM_GETFONT:
1170             return infoPtr->hFont? infoPtr->hFont : infoPtr->hDefaultFont;
1171
1172         case WM_GETTEXT:
1173             return STATUSBAR_WMGetText (infoPtr, (INT)wParam, (LPWSTR)lParam);
1174
1175         case WM_GETTEXTLENGTH:
1176             return STATUSBAR_GetTextLength (infoPtr, 0);
1177
1178         case WM_LBUTTONDBLCLK:
1179             return STATUSBAR_SendNotify (hwnd, NM_DBLCLK);
1180
1181         case WM_LBUTTONUP:
1182             return STATUSBAR_SendNotify (hwnd, NM_CLICK);
1183
1184         case WM_MOUSEMOVE:
1185             return STATUSBAR_Relay2Tip (infoPtr, msg, wParam, lParam);
1186
1187         case WM_NCHITTEST:
1188             res = STATUSBAR_WMNCHitTest(infoPtr, (INT)LOWORD(lParam),
1189                                         (INT)HIWORD(lParam));
1190             if (res != HTERROR) return res;
1191             return DefWindowProcW (hwnd, msg, wParam, lParam);
1192
1193         case WM_NCLBUTTONUP:
1194         case WM_NCLBUTTONDOWN:
1195             PostMessageW (GetParent (hwnd), msg, wParam, lParam);
1196             return 0;
1197
1198         case WM_PAINT:
1199             return STATUSBAR_WMPaint (infoPtr, (HDC)wParam);
1200
1201         case WM_RBUTTONDBLCLK:
1202             return STATUSBAR_SendNotify (hwnd, NM_RDBLCLK);
1203
1204         case WM_RBUTTONUP:
1205             return STATUSBAR_SendNotify (hwnd, NM_RCLICK);
1206
1207         case WM_SETFONT:
1208             return STATUSBAR_WMSetFont (infoPtr, (HFONT)wParam, LOWORD(lParam));
1209
1210         case WM_SETTEXT:
1211             return STATUSBAR_WMSetText (infoPtr, (LPCSTR)lParam);
1212
1213
1214         case WM_SIZE:
1215             if (STATUSBAR_WMSize (infoPtr, (WORD)wParam)) return 0;
1216             return DefWindowProcW (hwnd, msg, wParam, lParam);
1217
1218         default:
1219             if (msg >= WM_USER)
1220                 ERR("unknown msg %04x wp=%04x lp=%08lx\n",
1221                      msg, wParam, lParam);
1222             return DefWindowProcW (hwnd, msg, wParam, lParam);
1223     }
1224     return 0;
1225 }
1226
1227
1228 /***********************************************************************
1229  * STATUS_Register [Internal]
1230  *
1231  * Registers the status window class.
1232  */
1233
1234 void
1235 STATUS_Register (void)
1236 {
1237     WNDCLASSW wndClass;
1238
1239     ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1240     wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS | CS_VREDRAW;
1241     wndClass.lpfnWndProc   = (WNDPROC)StatusWindowProc;
1242     wndClass.cbClsExtra    = 0;
1243     wndClass.cbWndExtra    = sizeof(STATUSWINDOWINFO *);
1244     wndClass.hCursor       = LoadCursorW (0, IDC_ARROWW);
1245     wndClass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
1246     wndClass.lpszClassName = STATUSCLASSNAMEW;
1247  
1248     RegisterClassW (&wndClass);
1249 }
1250
1251
1252 /***********************************************************************
1253  * STATUS_Unregister [Internal]
1254  *
1255  * Unregisters the status window class.
1256  */
1257
1258 void
1259 STATUS_Unregister (void)
1260 {
1261     UnregisterClassW (STATUSCLASSNAMEW, (HINSTANCE)NULL);
1262 }
1263