Fixed WM_SETFONT handling (no redraw made in some cases).
[wine] / controls / static.c
1 /*
2  * Static control
3  *
4  * Copyright  David W. Metcalfe, 1993
5  *
6  */
7
8 #include "windef.h"
9 #include "wingdi.h"
10 #include "wine/winuser16.h"
11 #include "cursoricon.h"
12 #include "controls.h"
13 #include "user.h"
14 #include "debugtools.h"
15
16 DEFAULT_DEBUG_CHANNEL(static);
17
18 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style );
19 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style );
20 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style );
21 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style );
22 static void STATIC_PaintBitmapfn( HWND hwnd, HDC hdc, DWORD style );
23 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style );
24 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
25 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
26
27 static COLORREF color_windowframe, color_background, color_window;
28
29 /* offsets for GetWindowLong for static private information */
30 #define HFONT_GWL_OFFSET    0
31 #define HICON_GWL_OFFSET    (sizeof(HFONT))
32 #define STATIC_EXTRA_BYTES  (HICON_GWL_OFFSET + sizeof(HICON))
33
34 typedef void (*pfPaint)( HWND hwnd, HDC hdc, DWORD style );
35
36 static pfPaint staticPaintFunc[SS_TYPEMASK+1] =
37 {
38     STATIC_PaintTextfn,      /* SS_LEFT */
39     STATIC_PaintTextfn,      /* SS_CENTER */
40     STATIC_PaintTextfn,      /* SS_RIGHT */
41     STATIC_PaintIconfn,      /* SS_ICON */
42     STATIC_PaintRectfn,      /* SS_BLACKRECT */
43     STATIC_PaintRectfn,      /* SS_GRAYRECT */
44     STATIC_PaintRectfn,      /* SS_WHITERECT */
45     STATIC_PaintRectfn,      /* SS_BLACKFRAME */
46     STATIC_PaintRectfn,      /* SS_GRAYFRAME */
47     STATIC_PaintRectfn,      /* SS_WHITEFRAME */
48     NULL,                    /* Not defined */
49     STATIC_PaintTextfn,      /* SS_SIMPLE */
50     STATIC_PaintTextfn,      /* SS_LEFTNOWORDWRAP */
51     STATIC_PaintOwnerDrawfn, /* SS_OWNERDRAW */
52     STATIC_PaintBitmapfn,    /* SS_BITMAP */
53     NULL,                    /* SS_ENHMETAFILE */
54     STATIC_PaintEtchedfn,    /* SS_ETCHEDHORIZ */
55     STATIC_PaintEtchedfn,    /* SS_ETCHEDVERT */
56     STATIC_PaintEtchedfn,    /* SS_ETCHEDFRAME */
57 };
58
59
60 /*********************************************************************
61  * static class descriptor
62  */
63 const struct builtin_class_descr STATIC_builtin_class =
64 {
65     "Static",            /* name */
66     CS_GLOBALCLASS | CS_DBLCLKS | CS_PARENTDC, /* style  */
67     StaticWndProcA,      /* procA */
68     StaticWndProcW,      /* procW */
69     STATIC_EXTRA_BYTES,  /* extra */
70     IDC_ARROWA,          /* cursor */
71     0                    /* brush */
72 };
73
74
75 /***********************************************************************
76  *           STATIC_SetIcon
77  *
78  * Set the icon for an SS_ICON control.
79  */
80 static HICON STATIC_SetIcon( HWND hwnd, HICON hicon, DWORD style )
81 {
82     HICON prevIcon;
83     CURSORICONINFO *info = hicon?(CURSORICONINFO *) GlobalLock16( hicon ):NULL;
84
85     if ((style & SS_TYPEMASK) != SS_ICON) return 0;
86     if (hicon && !info) {
87         ERR("huh? hicon!=0, but info=0???\n");
88         return 0;
89     }
90     prevIcon = SetWindowLongA( hwnd, HICON_GWL_OFFSET, hicon );
91     if (hicon)
92     {
93         SetWindowPos( hwnd, 0, 0, 0, info->nWidth, info->nHeight,
94                         SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
95         GlobalUnlock16( hicon );
96     }
97     return prevIcon;
98 }
99
100 /***********************************************************************
101  *           STATIC_SetBitmap
102  *
103  * Set the bitmap for an SS_BITMAP control.
104  */
105 static HBITMAP STATIC_SetBitmap( HWND hwnd, HBITMAP hBitmap, DWORD style )
106 {
107     HBITMAP hOldBitmap;
108
109     if ((style & SS_TYPEMASK) != SS_BITMAP) return 0;
110     if (hBitmap && GetObjectType(hBitmap) != OBJ_BITMAP) {
111         ERR("huh? hBitmap!=0, but not bitmap\n");
112         return 0;
113     }
114     hOldBitmap = SetWindowLongA( hwnd, HICON_GWL_OFFSET, hBitmap );
115     if (hBitmap)
116     {
117         BITMAP bm;
118         GetObjectW(hBitmap, sizeof(bm), &bm);
119         SetWindowPos( hwnd, 0, 0, 0, bm.bmWidth, bm.bmHeight,
120                       SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
121     }
122     return hOldBitmap;
123 }
124
125 /***********************************************************************
126  *           STATIC_LoadIconA
127  *
128  * Load the icon for an SS_ICON control.
129  */
130 static HICON STATIC_LoadIconA( HWND hwnd, LPCSTR name )
131 {
132     HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
133     HICON hicon = LoadIconA( hInstance, name );
134     if (!hicon) hicon = LoadIconA( 0, name );
135     return hicon;
136 }
137
138 /***********************************************************************
139  *           STATIC_LoadIconW
140  *
141  * Load the icon for an SS_ICON control.
142  */
143 static HICON STATIC_LoadIconW( HWND hwnd, LPCWSTR name )
144 {
145     HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
146     HICON hicon = LoadIconW( hInstance, name );
147     if (!hicon) hicon = LoadIconW( 0, name );
148     return hicon;
149 }
150
151 /***********************************************************************
152  *           STATIC_LoadBitmapA
153  *
154  * Load the bitmap for an SS_BITMAP control.
155  */
156 static HBITMAP STATIC_LoadBitmapA( HWND hwnd, LPCSTR name )
157 {
158     HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
159     HBITMAP hbitmap = LoadBitmapA( hInstance, name );
160     if (!hbitmap)  /* Try OEM icon (FIXME: is this right?) */
161         hbitmap = LoadBitmapA( 0, name );
162     return hbitmap;
163 }
164
165 /***********************************************************************
166  *           STATIC_LoadBitmapW
167  *
168  * Load the bitmap for an SS_BITMAP control.
169  */
170 static HBITMAP STATIC_LoadBitmapW( HWND hwnd, LPCWSTR name )
171 {
172     HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
173     HBITMAP hbitmap = LoadBitmapW( hInstance, name );
174     if (!hbitmap)  /* Try OEM icon (FIXME: is this right?) */
175         hbitmap = LoadBitmapW( 0, name );
176     return hbitmap;
177 }
178
179 /***********************************************************************
180  *           STATIC_TryPaintFcn
181  *
182  * Try to immediately paint the control.
183  */
184 static VOID STATIC_TryPaintFcn(HWND hwnd, LONG full_style)
185 {
186     LONG style = full_style & SS_TYPEMASK;
187     RECT rc;
188
189     GetClientRect( hwnd, &rc );
190     if (!IsRectEmpty(&rc) && IsWindowVisible(hwnd) && staticPaintFunc[style])
191     {
192         HDC hdc;
193         hdc = GetDC( hwnd );
194         (staticPaintFunc[style])( hwnd, hdc, full_style );
195         ReleaseDC( hwnd, hdc );
196     }
197 }
198
199 /***********************************************************************
200  *           StaticWndProc_common
201  */
202 static LRESULT StaticWndProc_common( HWND hwnd, UINT uMsg, WPARAM wParam,
203                                      LPARAM lParam, BOOL unicode )
204 {
205     LRESULT lResult = 0;
206     LONG full_style = GetWindowLongA( hwnd, GWL_STYLE );
207     LONG style = full_style & SS_TYPEMASK;
208
209     switch (uMsg)
210     {
211     case WM_CREATE:
212         if (style < 0L || style > SS_TYPEMASK)
213         {
214             ERR("Unknown style 0x%02lx\n", style );
215             return -1;
216         }
217         /* initialise colours */
218         color_windowframe  = GetSysColor(COLOR_WINDOWFRAME);
219         color_background   = GetSysColor(COLOR_BACKGROUND);
220         color_window       = GetSysColor(COLOR_WINDOW);
221         break;
222
223     case WM_NCDESTROY:
224         if (style == SS_ICON) {
225 /*
226  * FIXME
227  *           DestroyIcon32( STATIC_SetIcon( wndPtr, 0 ) );
228  * 
229  * We don't want to do this yet because DestroyIcon32 is broken. If the icon
230  * had already been loaded by the application the last thing we want to do is
231  * GlobalFree16 the handle.
232  */
233             break;
234         }
235         else return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
236                               DefWindowProcA(hwnd, uMsg, wParam, lParam);
237
238     case WM_PAINT:
239         {
240             PAINTSTRUCT ps;
241             BeginPaint(hwnd, &ps);
242             if (staticPaintFunc[style])
243                 (staticPaintFunc[style])( hwnd, ps.hdc, full_style );
244             EndPaint(hwnd, &ps);
245         }
246         break;
247
248     case WM_ENABLE:
249         InvalidateRect(hwnd, NULL, FALSE);
250         break;
251
252     case WM_SYSCOLORCHANGE:
253         color_windowframe  = GetSysColor(COLOR_WINDOWFRAME);
254         color_background   = GetSysColor(COLOR_BACKGROUND);
255         color_window       = GetSysColor(COLOR_WINDOW);
256         InvalidateRect(hwnd, NULL, TRUE);
257         break;
258
259     case WM_NCCREATE:
260         if ((TWEAK_WineLook > WIN31_LOOK) && (full_style & SS_SUNKEN))
261             SetWindowLongA( hwnd, GWL_EXSTYLE,
262                             GetWindowLongA( hwnd, GWL_EXSTYLE ) | WS_EX_STATICEDGE );
263
264         if(unicode)
265             lParam = (LPARAM)(((LPCREATESTRUCTW)lParam)->lpszName);
266         else
267             lParam = (LPARAM)(((LPCREATESTRUCTA)lParam)->lpszName);
268         /* fall through */
269     case WM_SETTEXT:
270         switch (style) {
271         case SS_ICON:
272         {
273             HICON hIcon;
274             if(unicode)
275                 hIcon = STATIC_LoadIconW(hwnd, (LPCWSTR)lParam);
276             else
277                 hIcon = STATIC_LoadIconA(hwnd, (LPCSTR)lParam);
278             /* FIXME : should we also return the previous hIcon here ??? */
279             STATIC_SetIcon(hwnd, hIcon, style);
280             break;
281         }
282         case SS_BITMAP: 
283         {
284             HBITMAP hBitmap;
285             if(unicode)
286                 hBitmap = STATIC_LoadBitmapW(hwnd, (LPCWSTR)lParam);
287             else
288                 hBitmap = STATIC_LoadBitmapA(hwnd, (LPCSTR)lParam);
289             STATIC_SetBitmap(hwnd, hBitmap, style);
290             break;
291         }
292         case SS_LEFT:
293         case SS_CENTER:
294         case SS_RIGHT:
295         case SS_SIMPLE:
296         case SS_LEFTNOWORDWRAP:
297         {
298             if (HIWORD(lParam))
299             {
300                 if(unicode)
301                     lResult = DefWindowProcW( hwnd, WM_SETTEXT, wParam, lParam );
302                 else
303                     lResult = DefWindowProcA( hwnd, WM_SETTEXT, wParam, lParam );
304             }
305             if (uMsg == WM_SETTEXT)
306                 STATIC_TryPaintFcn( hwnd, full_style );
307             break;
308         }
309         default:
310             if (HIWORD(lParam))
311             {
312                 if(unicode)
313                     lResult = DefWindowProcW( hwnd, WM_SETTEXT, wParam, lParam );
314                 else
315                     lResult = DefWindowProcA( hwnd, WM_SETTEXT, wParam, lParam );
316             }
317             if(uMsg == WM_SETTEXT)
318                 InvalidateRect(hwnd, NULL, FALSE);
319         }
320         return 1; /* success. FIXME: check text length */
321
322     case WM_SETFONT:
323         if ((style == SS_ICON) || (style == SS_BITMAP)) return 0;
324         SetWindowLongA( hwnd, HFONT_GWL_OFFSET, wParam );
325         if (!LOWORD(lParam)) break;  /* don't refresh */
326         switch (style) {
327         case SS_LEFT:
328         case SS_CENTER:
329         case SS_RIGHT:
330         case SS_SIMPLE:
331         case SS_LEFTNOWORDWRAP:
332             STATIC_TryPaintFcn( hwnd, full_style );
333             break;
334         default:
335             InvalidateRect( hwnd, NULL, FALSE );
336             break;
337         }
338         break;
339
340     case WM_GETFONT:
341         return GetWindowLongA( hwnd, HFONT_GWL_OFFSET );
342
343     case WM_NCHITTEST:
344         if (full_style & SS_NOTIFY)
345            return HTCLIENT;
346         else
347            return HTTRANSPARENT;
348
349     case WM_GETDLGCODE:
350         return DLGC_STATIC;
351
352     case STM_GETIMAGE:
353     case STM_GETICON16:
354     case STM_GETICON:
355         return GetWindowLongA( hwnd, HICON_GWL_OFFSET );
356
357     case STM_SETIMAGE:
358         switch(wParam) {
359         case IMAGE_BITMAP:
360             lResult = STATIC_SetBitmap( hwnd, (HBITMAP)lParam, style );
361             break;
362         case IMAGE_ICON:
363             lResult = STATIC_SetIcon( hwnd, (HICON)lParam, style );
364             break;
365         default:
366             FIXME("STM_SETIMAGE: Unhandled type %x\n", wParam);
367             break;
368         }
369         InvalidateRect( hwnd, NULL, FALSE );
370         break;
371
372     case STM_SETICON16:
373     case STM_SETICON:
374         lResult = STATIC_SetIcon( hwnd, (HICON)wParam, style );
375         InvalidateRect( hwnd, NULL, FALSE );
376         break;
377
378     default:
379         return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
380                          DefWindowProcA(hwnd, uMsg, wParam, lParam);
381     }
382     return lResult;
383 }
384
385 /***********************************************************************
386  *           StaticWndProcA
387  */
388 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
389 {
390     if (!IsWindow( hWnd )) return 0;
391     return StaticWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
392 }
393
394 /***********************************************************************
395  *           StaticWndProcW
396  */
397 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
398 {
399     if (!IsWindow( hWnd )) return 0;
400     return StaticWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
401 }
402
403 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style )
404 {
405   DRAWITEMSTRUCT dis;
406   LONG id = GetWindowLongA( hwnd, GWL_ID );
407
408   dis.CtlType    = ODT_STATIC;
409   dis.CtlID      = id;
410   dis.itemID     = 0;
411   dis.itemAction = ODA_DRAWENTIRE;
412   dis.itemState  = 0;
413   dis.hwndItem   = hwnd;
414   dis.hDC        = hdc;
415   dis.itemData   = 0;
416   GetClientRect( hwnd, &dis.rcItem );
417
418   SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
419   SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
420 }
421
422 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style )
423 {
424     RECT rc;
425     HBRUSH hBrush;
426     HFONT hFont;
427     WORD wFormat;
428     INT len;
429     WCHAR *text;
430
431     GetClientRect( hwnd, &rc);
432
433     switch (style & SS_TYPEMASK)
434     {
435     case SS_LEFT:
436         wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
437         break;
438
439     case SS_CENTER:
440         wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
441         break;
442
443     case SS_RIGHT:
444         wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
445         break;
446
447     case SS_SIMPLE:
448         wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOCLIP;
449         break;
450
451     case SS_LEFTNOWORDWRAP:
452         wFormat = DT_LEFT | DT_EXPANDTABS | DT_VCENTER;
453         break;
454
455     default:
456         return;
457     }
458
459     if (style & SS_NOPREFIX)
460         wFormat |= DT_NOPREFIX;
461
462     if ((hFont = GetWindowLongA( hwnd, HFONT_GWL_OFFSET ))) SelectObject( hdc, hFont );
463
464     if ((style & SS_NOPREFIX) || ((style & SS_TYPEMASK) != SS_SIMPLE))
465     {
466         hBrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
467         if (!hBrush) /* did the app forget to call defwindowproc ? */
468             hBrush = DefWindowProcW(GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd);
469         FillRect( hdc, &rc, hBrush );
470     }
471     if (!IsWindowEnabled(hwnd)) SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
472
473     if (!(len = SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 ))) return;
474     if (!(text = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return;
475     SendMessageW( hwnd, WM_GETTEXT, len + 1, (LPARAM)text );
476     DrawTextW( hdc, text, -1, &rc, wFormat );
477     HeapFree( GetProcessHeap(), 0, text );
478 }
479
480 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style )
481 {
482     RECT rc;
483     HBRUSH hBrush;
484
485     GetClientRect( hwnd, &rc);
486     
487     switch (style & SS_TYPEMASK)
488     {
489     case SS_BLACKRECT:
490         hBrush = CreateSolidBrush(color_windowframe);
491         FillRect( hdc, &rc, hBrush );
492         break;
493     case SS_GRAYRECT:
494         hBrush = CreateSolidBrush(color_background);
495         FillRect( hdc, &rc, hBrush );
496         break;
497     case SS_WHITERECT:
498         hBrush = CreateSolidBrush(color_window);
499         FillRect( hdc, &rc, hBrush );
500         break;
501     case SS_BLACKFRAME:
502         hBrush = CreateSolidBrush(color_windowframe);
503         FrameRect( hdc, &rc, hBrush );
504         break;
505     case SS_GRAYFRAME:
506         hBrush = CreateSolidBrush(color_background);
507         FrameRect( hdc, &rc, hBrush );
508         break;
509     case SS_WHITEFRAME:
510         hBrush = CreateSolidBrush(color_window);
511         FrameRect( hdc, &rc, hBrush );
512         break;
513     default:
514         return;
515     }
516     DeleteObject( hBrush );
517 }
518
519
520 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style )
521 {
522     RECT rc;
523     HBRUSH hbrush;
524     HICON hIcon;
525
526     GetClientRect( hwnd, &rc );
527     hbrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
528     FillRect( hdc, &rc, hbrush );
529     if ((hIcon = GetWindowLongA( hwnd, HICON_GWL_OFFSET )))
530         DrawIcon( hdc, rc.left, rc.top, hIcon );
531 }
532
533 static void STATIC_PaintBitmapfn(HWND hwnd, HDC hdc, DWORD style )
534 {
535     RECT rc;
536     HBRUSH hbrush;
537     HICON hIcon;
538     HDC hMemDC;
539     HBITMAP oldbitmap;
540
541     GetClientRect( hwnd, &rc );
542     hbrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
543     FillRect( hdc, &rc, hbrush );
544
545     if ((hIcon = GetWindowLongA( hwnd, HICON_GWL_OFFSET )))
546     {
547         BITMAP bm;
548         SIZE sz;
549
550         if(GetObjectType(hIcon) != OBJ_BITMAP) return;
551         if (!(hMemDC = CreateCompatibleDC( hdc ))) return;
552         GetObjectW(hIcon, sizeof(bm), &bm);
553         GetBitmapDimensionEx(hIcon, &sz);
554         oldbitmap = SelectObject(hMemDC, hIcon);
555         BitBlt(hdc, sz.cx, sz.cy, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0,
556                SRCCOPY);
557         SelectObject(hMemDC, oldbitmap);
558         DeleteDC(hMemDC);
559     }
560 }
561
562
563 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style )
564 {
565     RECT rc;
566
567     if (TWEAK_WineLook == WIN31_LOOK)
568         return;
569
570     GetClientRect( hwnd, &rc );
571     switch (style & SS_TYPEMASK)
572     {
573         case SS_ETCHEDHORZ:
574             DrawEdge(hdc,&rc,EDGE_ETCHED,BF_TOP|BF_BOTTOM);
575             break;
576         case SS_ETCHEDVERT:
577             DrawEdge(hdc,&rc,EDGE_ETCHED,BF_LEFT|BF_RIGHT);
578             break;
579         case SS_ETCHEDFRAME:
580             DrawEdge (hdc, &rc, EDGE_ETCHED, BF_RECT);
581             break;
582     }
583 }