user32: combo: CB_SETITEMHEIGHT should make the edit area 2px higher than the item...
[wine] / dlls / user32 / static.c
1 /*
2  * Static control
3  *
4  * Copyright  David W. Metcalfe, 1993
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * NOTES
21  *
22  * This code was audited for completeness against the documented features
23  * of Comctl32.dll version 6.0 on Oct. 4, 2004, by Dimitrie O. Paun.
24  * 
25  * Unless otherwise noted, we believe this code to be complete, as per
26  * the specification mentioned above.
27  * If you discover missing features, or bugs, please note them below.
28  *
29  * Notes:
30  *   - Windows XP introduced new behavior: The background of centered
31  *     icons and bitmaps is painted differently. This is only done if
32  *     a manifest is present.
33  *     Because it has not yet been decided how to implement the two
34  *     different modes in Wine, only the Windows XP mode is implemented.
35  *   - Controls with SS_SIMPLE but without SS_NOPREFIX:
36  *     The text should not be changed. Windows doesn't clear the
37  *     client rectangle, so the new text must be larger than the old one.
38  *   - The SS_RIGHTJUST style is currently not implemented by Windows
39  *     (or it does something different than documented).
40  *
41  * TODO:
42  *   - Animated cursors
43  */
44
45 #include <stdarg.h>
46
47 #include "windef.h"
48 #include "winbase.h"
49 #include "wingdi.h"
50 #include "wine/winuser16.h"
51 #include "controls.h"
52 #include "user_private.h"
53 #include "wine/debug.h"
54
55 WINE_DEFAULT_DEBUG_CHANNEL(static);
56
57 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style );
58 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style );
59 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style );
60 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style );
61 static void STATIC_PaintBitmapfn( HWND hwnd, HDC hdc, DWORD style );
62 static void STATIC_PaintEnhMetafn( HWND hwnd, HDC hdc, DWORD style );
63 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style );
64 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
65 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
66
67 static COLORREF color_3dshadow, color_3ddkshadow, color_3dhighlight;
68
69 /* offsets for GetWindowLong for static private information */
70 #define HFONT_GWL_OFFSET    0
71 #define HICON_GWL_OFFSET    (sizeof(HFONT))
72 #define STATIC_EXTRA_BYTES  (HICON_GWL_OFFSET + sizeof(HICON))
73
74 typedef void (*pfPaint)( HWND hwnd, HDC hdc, DWORD style );
75
76 static const pfPaint staticPaintFunc[SS_TYPEMASK+1] =
77 {
78     STATIC_PaintTextfn,      /* SS_LEFT */
79     STATIC_PaintTextfn,      /* SS_CENTER */
80     STATIC_PaintTextfn,      /* SS_RIGHT */
81     STATIC_PaintIconfn,      /* SS_ICON */
82     STATIC_PaintRectfn,      /* SS_BLACKRECT */
83     STATIC_PaintRectfn,      /* SS_GRAYRECT */
84     STATIC_PaintRectfn,      /* SS_WHITERECT */
85     STATIC_PaintRectfn,      /* SS_BLACKFRAME */
86     STATIC_PaintRectfn,      /* SS_GRAYFRAME */
87     STATIC_PaintRectfn,      /* SS_WHITEFRAME */
88     NULL,                    /* SS_USERITEM */
89     STATIC_PaintTextfn,      /* SS_SIMPLE */
90     STATIC_PaintTextfn,      /* SS_LEFTNOWORDWRAP */
91     STATIC_PaintOwnerDrawfn, /* SS_OWNERDRAW */
92     STATIC_PaintBitmapfn,    /* SS_BITMAP */
93     STATIC_PaintEnhMetafn,   /* SS_ENHMETAFILE */
94     STATIC_PaintEtchedfn,    /* SS_ETCHEDHORZ */
95     STATIC_PaintEtchedfn,    /* SS_ETCHEDVERT */
96     STATIC_PaintEtchedfn,    /* SS_ETCHEDFRAME */
97 };
98
99
100 /*********************************************************************
101  * static class descriptor
102  */
103 const struct builtin_class_descr STATIC_builtin_class =
104 {
105     "Static",            /* name */
106     CS_DBLCLKS | CS_PARENTDC, /* style  */
107     StaticWndProcA,      /* procA */
108     StaticWndProcW,      /* procW */
109     STATIC_EXTRA_BYTES,  /* extra */
110     IDC_ARROW,           /* cursor */
111     0                    /* brush */
112 };
113
114 static void setup_clipping(HWND hwnd, HDC hdc, HRGN *orig)
115 {
116     RECT rc;
117     HRGN hrgn;
118
119     /* Native control has always a clipping region set (this may be because
120      * builtin controls uses CS_PARENTDC) and an application depends on it
121      */
122     hrgn = CreateRectRgn(0, 0, 1, 1);
123     if (GetClipRgn(hdc, hrgn) != 1)
124     {
125         DeleteObject(hrgn);
126         *orig = NULL;
127     } else
128         *orig = hrgn;
129
130     GetClientRect(hwnd, &rc);
131     DPtoLP(hdc, (POINT *)&rc, 2);
132     IntersectClipRect(hdc, rc.left, rc.top, rc.right, rc.bottom);
133 }
134
135 static void restore_clipping(HDC hdc, HRGN hrgn)
136 {
137     SelectClipRgn(hdc, hrgn);
138     if (hrgn != NULL)
139         DeleteObject(hrgn);
140 }
141
142 /***********************************************************************
143  *           STATIC_SetIcon
144  *
145  * Set the icon for an SS_ICON control.
146  */
147 static HICON STATIC_SetIcon( HWND hwnd, HICON hicon, DWORD style )
148 {
149     HICON prevIcon;
150     CURSORICONINFO * info;
151
152     if ((style & SS_TYPEMASK) != SS_ICON) return 0;
153     info = hicon?(CURSORICONINFO *) GlobalLock16(HICON_16(hicon)):NULL;
154     if (hicon && !info) {
155         WARN("hicon != 0, but info == 0\n");
156         return 0;
157     }
158     prevIcon = (HICON)SetWindowLongPtrW( hwnd, HICON_GWL_OFFSET, (LONG_PTR)hicon );
159     if (hicon && !(style & SS_CENTERIMAGE) && !(style & SS_REALSIZECONTROL))
160     {
161         /* Windows currently doesn't implement SS_RIGHTJUST */
162         /*
163         if ((style & SS_RIGHTJUST) != 0)
164         {
165             RECT wr;
166             GetWindowRect(hwnd, &wr);
167             SetWindowPos( hwnd, 0, wr.right - info->nWidth, wr.bottom - info->nHeight,
168                           info->nWidth, info->nHeight, SWP_NOACTIVATE | SWP_NOZORDER );
169         }
170         else */
171         {
172             SetWindowPos( hwnd, 0, 0, 0, info->nWidth, info->nHeight,
173                           SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
174         }
175     }
176     if (info) GlobalUnlock16(HICON_16(hicon));
177     return prevIcon;
178 }
179
180 /***********************************************************************
181  *           STATIC_SetBitmap
182  *
183  * Set the bitmap for an SS_BITMAP control.
184  */
185 static HBITMAP STATIC_SetBitmap( HWND hwnd, HBITMAP hBitmap, DWORD style )
186 {
187     HBITMAP hOldBitmap;
188
189     if ((style & SS_TYPEMASK) != SS_BITMAP) return 0;
190     if (hBitmap && GetObjectType(hBitmap) != OBJ_BITMAP) {
191         WARN("hBitmap != 0, but it's not a bitmap\n");
192         return 0;
193     }
194     hOldBitmap = (HBITMAP)SetWindowLongPtrW( hwnd, HICON_GWL_OFFSET, (LONG_PTR)hBitmap );
195     if (hBitmap && !(style & SS_CENTERIMAGE) && !(style & SS_REALSIZECONTROL))
196     {
197         BITMAP bm;
198         GetObjectW(hBitmap, sizeof(bm), &bm);
199         /* Windows currently doesn't implement SS_RIGHTJUST */
200         /*
201         if ((style & SS_RIGHTJUST) != 0)
202         {
203             RECT wr;
204             GetWindowRect(hwnd, &wr);
205             SetWindowPos( hwnd, 0, wr.right - bm.bmWidth, wr.bottom - bm.bmHeight,
206                           bm.bmWidth, bm.bmHeight, SWP_NOACTIVATE | SWP_NOZORDER );
207         }
208         else */
209         {
210             SetWindowPos( hwnd, 0, 0, 0, bm.bmWidth, bm.bmHeight,
211                           SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
212         }
213         
214     }
215     return hOldBitmap;
216 }
217
218 /***********************************************************************
219  *           STATIC_SetEnhMetaFile
220  *
221  * Set the enhanced metafile for an SS_ENHMETAFILE control.
222  */
223 static HENHMETAFILE STATIC_SetEnhMetaFile( HWND hwnd, HENHMETAFILE hEnhMetaFile, DWORD style )
224 {
225     if ((style & SS_TYPEMASK) != SS_ENHMETAFILE) return 0;
226     if (hEnhMetaFile && GetObjectType(hEnhMetaFile) != OBJ_ENHMETAFILE) {
227         WARN("hEnhMetaFile != 0, but it's not an enhanced metafile\n");
228         return 0;
229     }
230     return (HENHMETAFILE)SetWindowLongPtrW( hwnd, HICON_GWL_OFFSET, (LONG_PTR)hEnhMetaFile );
231 }
232
233 /***********************************************************************
234  *           STATIC_GetImage
235  *
236  * Gets the bitmap for an SS_BITMAP control, the icon/cursor for an
237  * SS_ICON control or the enhanced metafile for an SS_ENHMETAFILE control.
238  */
239 static HANDLE STATIC_GetImage( HWND hwnd, WPARAM wParam, DWORD style )
240 {
241     switch(style & SS_TYPEMASK)
242     {
243         case SS_ICON:
244             if ((wParam != IMAGE_ICON) &&
245                 (wParam != IMAGE_CURSOR)) return NULL;
246             break;
247         case SS_BITMAP:
248             if (wParam != IMAGE_BITMAP) return NULL;
249             break;
250         case SS_ENHMETAFILE:
251             if (wParam != IMAGE_ENHMETAFILE) return NULL;
252             break;
253         default:
254             return NULL;
255     }
256     return (HANDLE)GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET );
257 }
258
259 /***********************************************************************
260  *           STATIC_LoadIconA
261  *
262  * Load the icon for an SS_ICON control.
263  */
264 static HICON STATIC_LoadIconA( HWND hwnd, LPCSTR name, DWORD style )
265 {
266     HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
267     if ((style & SS_REALSIZEIMAGE) != 0)
268     {
269         return LoadImageA(hInstance, name, IMAGE_ICON, 0, 0, LR_SHARED);
270     }
271     else
272     {
273         HICON hicon = LoadIconA( hInstance, name );
274         if (!hicon) hicon = LoadCursorA( hInstance, name );
275         if (!hicon) hicon = LoadIconA( 0, name );
276         /* Windows doesn't try to load a standard cursor,
277            probably because most IDs for standard cursors conflict
278            with the IDs for standard icons anyway */
279         return hicon;
280     }
281 }
282
283 /***********************************************************************
284  *           STATIC_LoadIconW
285  *
286  * Load the icon for an SS_ICON control.
287  */
288 static HICON STATIC_LoadIconW( HWND hwnd, LPCWSTR name, DWORD style )
289 {
290     HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
291     if ((style & SS_REALSIZEIMAGE) != 0)
292     {
293         return LoadImageW(hInstance, name, IMAGE_ICON, 0, 0, LR_SHARED);
294     }
295     else
296     {
297         HICON hicon = LoadIconW( hInstance, name );
298         if (!hicon) hicon = LoadCursorW( hInstance, name );
299         if (!hicon) hicon = LoadIconW( 0, name );
300         /* Windows doesn't try to load a standard cursor,
301            probably because most IDs for standard cursors conflict
302            with the IDs for standard icons anyway */
303         return hicon;
304     }
305 }
306
307 /***********************************************************************
308  *           STATIC_LoadBitmapA
309  *
310  * Load the bitmap for an SS_BITMAP control.
311  */
312 static HBITMAP STATIC_LoadBitmapA( HWND hwnd, LPCSTR name )
313 {
314     HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
315     /* Windows doesn't try to load OEM Bitmaps (hInstance == NULL) */
316     return LoadBitmapA( hInstance, name );
317 }
318
319 /***********************************************************************
320  *           STATIC_LoadBitmapW
321  *
322  * Load the bitmap for an SS_BITMAP control.
323  */
324 static HBITMAP STATIC_LoadBitmapW( HWND hwnd, LPCWSTR name )
325 {
326     HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
327     /* Windows doesn't try to load OEM Bitmaps (hInstance == NULL) */
328     return LoadBitmapW( hInstance, name );
329 }
330
331 /***********************************************************************
332  *           STATIC_TryPaintFcn
333  *
334  * Try to immediately paint the control.
335  */
336 static VOID STATIC_TryPaintFcn(HWND hwnd, LONG full_style)
337 {
338     LONG style = full_style & SS_TYPEMASK;
339     RECT rc;
340
341     GetClientRect( hwnd, &rc );
342     if (!IsRectEmpty(&rc) && IsWindowVisible(hwnd) && staticPaintFunc[style])
343     {
344         HDC hdc;
345         HRGN hOrigClipping;
346
347         hdc = GetDC( hwnd );
348         setup_clipping(hwnd, hdc, &hOrigClipping);
349         (staticPaintFunc[style])( hwnd, hdc, full_style );
350         restore_clipping(hdc, hOrigClipping);
351         ReleaseDC( hwnd, hdc );
352     }
353 }
354
355 static HBRUSH STATIC_SendWmCtlColorStatic(HWND hwnd, HDC hdc)
356 {
357     HBRUSH hBrush;
358     HWND parent = GetParent(hwnd);
359
360     if (!parent) parent = hwnd;
361     hBrush = (HBRUSH) SendMessageW( parent,
362                     WM_CTLCOLORSTATIC, (WPARAM)hdc, (LPARAM)hwnd );
363     if (!hBrush) /* did the app forget to call DefWindowProc ? */
364     {
365         /* FIXME: DefWindowProc should return different colors if a
366                   manifest is present */
367         hBrush = (HBRUSH)DefWindowProcW( parent, WM_CTLCOLORSTATIC,
368                                         (WPARAM)hdc, (LPARAM)hwnd);
369     }
370     return hBrush;
371 }
372
373 static VOID STATIC_InitColours(void)
374 {
375     color_3ddkshadow  = GetSysColor(COLOR_3DDKSHADOW);
376     color_3dshadow    = GetSysColor(COLOR_3DSHADOW);
377     color_3dhighlight = GetSysColor(COLOR_3DHIGHLIGHT);
378 }
379
380 /***********************************************************************
381  *           hasTextStyle
382  *
383  * Tests if the control displays text.
384  */
385 static BOOL hasTextStyle( DWORD style )
386 {
387     switch(style & SS_TYPEMASK)
388     {
389         case SS_SIMPLE:
390         case SS_LEFT:
391         case SS_LEFTNOWORDWRAP:
392         case SS_CENTER:
393         case SS_RIGHT:
394         case SS_OWNERDRAW:
395             return TRUE;
396     }
397     
398     return FALSE;
399 }
400
401 /***********************************************************************
402  *           StaticWndProc_common
403  */
404 static LRESULT StaticWndProc_common( HWND hwnd, UINT uMsg, WPARAM wParam,
405                                      LPARAM lParam, BOOL unicode )
406 {
407     LRESULT lResult = 0;
408     LONG full_style = GetWindowLongW( hwnd, GWL_STYLE );
409     LONG style = full_style & SS_TYPEMASK;
410
411     switch (uMsg)
412     {
413     case WM_CREATE:
414         if (style < 0L || style > SS_TYPEMASK)
415         {
416             ERR("Unknown style 0x%02x\n", style );
417             return -1;
418         }
419         STATIC_InitColours();
420         break;
421
422     case WM_NCDESTROY:
423         if (style == SS_ICON) {
424 /*
425  * FIXME
426  *           DestroyIcon32( STATIC_SetIcon( wndPtr, 0 ) );
427  *
428  * We don't want to do this yet because DestroyIcon32 is broken. If the icon
429  * had already been loaded by the application the last thing we want to do is
430  * GlobalFree16 the handle.
431  */
432             break;
433         }
434         else return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
435                               DefWindowProcA(hwnd, uMsg, wParam, lParam);
436
437     case WM_ERASEBKGND:
438         /* do all painting in WM_PAINT like Windows does */
439         return 1;
440
441     case WM_PRINTCLIENT:
442     case WM_PAINT:
443         {
444             PAINTSTRUCT ps;
445             HDC hdc = wParam ? (HDC)wParam : BeginPaint(hwnd, &ps);
446             if (staticPaintFunc[style])
447             {
448                 HRGN hOrigClipping;
449                 setup_clipping(hwnd, hdc, &hOrigClipping);
450                 (staticPaintFunc[style])( hwnd, hdc, full_style );
451                 restore_clipping(hdc, hOrigClipping);
452             }
453             if (!wParam) EndPaint(hwnd, &ps);
454         }
455         break;
456
457     case WM_ENABLE:
458         STATIC_TryPaintFcn( hwnd, full_style );
459         if (full_style & SS_NOTIFY) {
460             if (wParam) {
461                 SendMessageW( GetParent(hwnd), WM_COMMAND,
462                               MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_ENABLE ), (LPARAM)hwnd);
463             }
464             else {
465                 SendMessageW( GetParent(hwnd), WM_COMMAND,
466                               MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_DISABLE ), (LPARAM)hwnd);
467             }
468         }
469         break;
470
471     case WM_SYSCOLORCHANGE:
472         STATIC_InitColours();
473         STATIC_TryPaintFcn( hwnd, full_style );
474         break;
475
476     case WM_NCCREATE:
477         {
478             LPCSTR textA;
479             LPCWSTR textW;
480     
481             if (full_style & SS_SUNKEN)
482                 SetWindowLongW( hwnd, GWL_EXSTYLE,
483                                 GetWindowLongW( hwnd, GWL_EXSTYLE ) | WS_EX_STATICEDGE );
484
485             if(unicode)
486             {
487                 textA = NULL;
488                 textW = ((LPCREATESTRUCTW)lParam)->lpszName;
489             }
490             else
491             {
492                 textA = ((LPCREATESTRUCTA)lParam)->lpszName;
493                 textW = NULL;
494             }
495
496             switch (style) {
497             case SS_ICON:
498                 {
499                     HICON hIcon;
500                     if(unicode)
501                        hIcon = STATIC_LoadIconW(hwnd, textW, full_style);
502                     else
503                        hIcon = STATIC_LoadIconA(hwnd, textA, full_style);
504                     STATIC_SetIcon(hwnd, hIcon, full_style);
505                 }
506                 break;
507             case SS_BITMAP:
508                 {
509                     HBITMAP hBitmap;
510                     if(unicode)
511                         hBitmap = STATIC_LoadBitmapW(hwnd, textW);
512                     else
513                         hBitmap = STATIC_LoadBitmapA(hwnd, textA);
514                     STATIC_SetBitmap(hwnd, hBitmap, full_style);
515                 }
516                 break;
517             }
518             /* SS_ENHMETAFILE: Despite what MSDN says, Windows does not load
519                the enhanced metafile that was specified as the window text. */
520         }
521         return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
522                          DefWindowProcA(hwnd, uMsg, wParam, lParam);
523
524     case WM_SETTEXT:
525         if (hasTextStyle( full_style ))
526         {
527             if (HIWORD(lParam))
528             {
529                 if(unicode)
530                      lResult = DefWindowProcW( hwnd, uMsg, wParam, lParam );
531                 else
532                     lResult = DefWindowProcA( hwnd, uMsg, wParam, lParam );
533                 STATIC_TryPaintFcn( hwnd, full_style );
534             }
535         }
536         break;
537
538     case WM_SETFONT:
539         if (hasTextStyle( full_style ))
540         {
541             SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, wParam );
542             if (LOWORD(lParam))
543                 RedrawWindow( hwnd, NULL, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_ALLCHILDREN );
544         }
545         break;
546
547     case WM_GETFONT:
548         return GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
549
550     case WM_NCHITTEST:
551         if (full_style & SS_NOTIFY)
552            return HTCLIENT;
553         else
554            return HTTRANSPARENT;
555
556     case WM_GETDLGCODE:
557         return DLGC_STATIC;
558
559     case WM_LBUTTONDOWN:
560     case WM_NCLBUTTONDOWN:
561         if (full_style & SS_NOTIFY)
562             SendMessageW( GetParent(hwnd), WM_COMMAND,
563                           MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_CLICKED ), (LPARAM)hwnd);
564         return 0;
565
566     case WM_LBUTTONDBLCLK:
567     case WM_NCLBUTTONDBLCLK:
568         if (full_style & SS_NOTIFY)
569             SendMessageW( GetParent(hwnd), WM_COMMAND,
570                           MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_DBLCLK ), (LPARAM)hwnd);
571         return 0;
572
573     case STM_GETIMAGE:
574         return (LRESULT)STATIC_GetImage( hwnd, wParam, full_style );
575     
576     case STM_GETICON16:
577     case STM_GETICON:
578         return (LRESULT)STATIC_GetImage( hwnd, IMAGE_ICON, full_style );
579
580     case STM_SETIMAGE:
581         switch(wParam) {
582         case IMAGE_BITMAP:
583             lResult = (LRESULT)STATIC_SetBitmap( hwnd, (HBITMAP)lParam, full_style );
584             break;
585         case IMAGE_ENHMETAFILE:
586             lResult = (LRESULT)STATIC_SetEnhMetaFile( hwnd, (HENHMETAFILE)lParam, full_style );
587             break;
588         case IMAGE_ICON:
589         case IMAGE_CURSOR:
590             lResult = (LRESULT)STATIC_SetIcon( hwnd, (HICON)lParam, full_style );
591             break;
592         default:
593             FIXME("STM_SETIMAGE: Unhandled type %lx\n", wParam);
594             break;
595         }
596         STATIC_TryPaintFcn( hwnd, full_style );
597         break;
598
599     case STM_SETICON16:
600     case STM_SETICON:
601         lResult = (LRESULT)STATIC_SetIcon( hwnd, (HICON)wParam, full_style );
602         STATIC_TryPaintFcn( hwnd, full_style );
603         break;
604
605     default:
606         return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
607                          DefWindowProcA(hwnd, uMsg, wParam, lParam);
608     }
609     return lResult;
610 }
611
612 /***********************************************************************
613  *           StaticWndProcA
614  */
615 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
616 {
617     if (!IsWindow( hWnd )) return 0;
618     return StaticWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
619 }
620
621 /***********************************************************************
622  *           StaticWndProcW
623  */
624 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
625 {
626     if (!IsWindow( hWnd )) return 0;
627     return StaticWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
628 }
629
630 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style )
631 {
632   DRAWITEMSTRUCT dis;
633   HFONT font, oldFont = NULL;
634   UINT id = (UINT)GetWindowLongPtrW( hwnd, GWLP_ID );
635
636   dis.CtlType    = ODT_STATIC;
637   dis.CtlID      = id;
638   dis.itemID     = 0;
639   dis.itemAction = ODA_DRAWENTIRE;
640   dis.itemState  = IsWindowEnabled(hwnd) ? 0 : ODS_DISABLED;
641   dis.hwndItem   = hwnd;
642   dis.hDC        = hdc;
643   dis.itemData   = 0;
644   GetClientRect( hwnd, &dis.rcItem );
645
646   font = (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
647   if (font) oldFont = SelectObject( hdc, font );
648   SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, (WPARAM)hdc, (LPARAM)hwnd );
649   SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
650   if (font) SelectObject( hdc, oldFont );
651 }
652
653 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style )
654 {
655     RECT rc;
656     HBRUSH hBrush;
657     HFONT hFont, hOldFont = NULL;
658     WORD wFormat;
659     INT len, buf_size;
660     WCHAR *text;
661
662     GetClientRect( hwnd, &rc);
663
664     switch (style & SS_TYPEMASK)
665     {
666     case SS_LEFT:
667         wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK;
668         break;
669
670     case SS_CENTER:
671         wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK;
672         break;
673
674     case SS_RIGHT:
675         wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK;
676         break;
677
678     case SS_SIMPLE:
679         wFormat = DT_LEFT | DT_SINGLELINE;
680         break;
681
682     case SS_LEFTNOWORDWRAP:
683         wFormat = DT_LEFT | DT_EXPANDTABS;
684         break;
685
686     default:
687         return;
688     }
689
690     if (style & SS_NOPREFIX)
691         wFormat |= DT_NOPREFIX;
692
693     if ((style & SS_TYPEMASK) != SS_SIMPLE)
694     {
695         if (style & SS_CENTERIMAGE)
696             wFormat |= DT_SINGLELINE | DT_VCENTER;
697         if (style & SS_EDITCONTROL)
698             wFormat |= DT_EDITCONTROL;
699         if (style & SS_ENDELLIPSIS)
700             wFormat |= DT_SINGLELINE | DT_END_ELLIPSIS;
701         if (style & SS_PATHELLIPSIS)
702             wFormat |= DT_SINGLELINE | DT_PATH_ELLIPSIS;
703         if (style & SS_WORDELLIPSIS)
704             wFormat |= DT_SINGLELINE | DT_WORD_ELLIPSIS;
705     }
706
707     if ((hFont = (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET )))
708         hOldFont = (HFONT)SelectObject( hdc, hFont );
709
710     /* SS_SIMPLE controls: WM_CTLCOLORSTATIC is sent, but the returned
711                            brush is not used */
712     hBrush = STATIC_SendWmCtlColorStatic(hwnd, hdc);
713
714     if ((style & SS_TYPEMASK) != SS_SIMPLE)
715     {
716         FillRect( hdc, &rc, hBrush );
717         if (!IsWindowEnabled(hwnd)) SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
718     }
719
720     buf_size = 256;
721     if (!(text = HeapAlloc( GetProcessHeap(), 0, buf_size * sizeof(WCHAR) )))
722         goto no_TextOut;
723
724     while ((len = InternalGetWindowText( hwnd, text, buf_size )) == buf_size - 1)
725     {
726         buf_size *= 2;
727         if (!(text = HeapReAlloc( GetProcessHeap(), 0, text, buf_size * sizeof(WCHAR) )))
728             goto no_TextOut;
729     }
730
731     if (!len) goto no_TextOut;
732
733     if (((style & SS_TYPEMASK) == SS_SIMPLE) && (style & SS_NOPREFIX))
734     {
735         /* Windows uses the faster ExtTextOut() to draw the text and
736            to paint the whole client rectangle with the text background
737            color. Reference: "Static Controls" by Kyle Marsh, 1992 */
738         ExtTextOutW( hdc, rc.left, rc.top, ETO_CLIPPED | ETO_OPAQUE,
739                      &rc, text, len, NULL );
740     }
741     else
742     {
743         DrawTextW( hdc, text, -1, &rc, wFormat );
744     }
745
746 no_TextOut:
747     HeapFree( GetProcessHeap(), 0, text );
748
749     if (hFont)
750         SelectObject( hdc, hOldFont );
751 }
752
753 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style )
754 {
755     RECT rc;
756     HBRUSH hBrush;
757
758     GetClientRect( hwnd, &rc);
759
760     /* FIXME: send WM_CTLCOLORSTATIC */
761     switch (style & SS_TYPEMASK)
762     {
763     case SS_BLACKRECT:
764         hBrush = CreateSolidBrush(color_3ddkshadow);
765         FillRect( hdc, &rc, hBrush );
766         break;
767     case SS_GRAYRECT:
768         hBrush = CreateSolidBrush(color_3dshadow);
769         FillRect( hdc, &rc, hBrush );
770         break;
771     case SS_WHITERECT:
772         hBrush = CreateSolidBrush(color_3dhighlight);
773         FillRect( hdc, &rc, hBrush );
774         break;
775     case SS_BLACKFRAME:
776         hBrush = CreateSolidBrush(color_3ddkshadow);
777         FrameRect( hdc, &rc, hBrush );
778         break;
779     case SS_GRAYFRAME:
780         hBrush = CreateSolidBrush(color_3dshadow);
781         FrameRect( hdc, &rc, hBrush );
782         break;
783     case SS_WHITEFRAME:
784         hBrush = CreateSolidBrush(color_3dhighlight);
785         FrameRect( hdc, &rc, hBrush );
786         break;
787     default:
788         return;
789     }
790     DeleteObject( hBrush );
791 }
792
793
794 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style )
795 {
796     RECT rc, iconRect;
797     HBRUSH hbrush;
798     HICON hIcon;
799     CURSORICONINFO * info;
800
801     GetClientRect( hwnd, &rc );
802     hbrush = STATIC_SendWmCtlColorStatic(hwnd, hdc);
803     hIcon = (HICON)GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET );
804     info = hIcon ? (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)) : NULL;
805     if (!hIcon || !info)
806     {
807         FillRect(hdc, &rc, hbrush);
808     }
809     else
810     {
811         if (style & SS_CENTERIMAGE)
812         {
813             iconRect.left = (rc.right - rc.left) / 2 - info->nWidth / 2;
814             iconRect.top = (rc.bottom - rc.top) / 2 - info->nHeight / 2;
815             iconRect.right = iconRect.left + info->nWidth;
816             iconRect.bottom = iconRect.top + info->nHeight;
817         }
818         else
819             iconRect = rc;
820         FillRect( hdc, &rc, hbrush );
821         DrawIconEx( hdc, iconRect.left, iconRect.top, hIcon, iconRect.right - iconRect.left,
822                     iconRect.bottom - iconRect.top, 0, NULL, DI_NORMAL );
823     }
824     if (info) GlobalUnlock16(HICON_16(hIcon));
825 }
826
827 static void STATIC_PaintBitmapfn(HWND hwnd, HDC hdc, DWORD style )
828 {
829     HDC hMemDC;
830     HBITMAP hBitmap, oldbitmap;
831     HBRUSH hbrush;
832
833     /* message is still sent, even if the returned brush is not used */
834     hbrush = STATIC_SendWmCtlColorStatic(hwnd, hdc);
835
836     if ((hBitmap = (HBITMAP)GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET ))
837          && (GetObjectType(hBitmap) == OBJ_BITMAP)
838          && (hMemDC = CreateCompatibleDC( hdc )))
839     {
840         BITMAP bm;
841         RECT rcClient;
842         LOGBRUSH brush;
843
844         GetObjectW(hBitmap, sizeof(bm), &bm);
845         oldbitmap = SelectObject(hMemDC, hBitmap);
846
847         /* Set the background color for monochrome bitmaps
848            to the color of the background brush */
849         if (GetObjectW( hbrush, sizeof(brush), &brush ))
850         {
851             if (brush.lbStyle == BS_SOLID)
852                 SetBkColor(hdc, brush.lbColor);
853         }
854         GetClientRect(hwnd, &rcClient);
855         if (style & SS_CENTERIMAGE)
856         {
857             INT x, y;
858             x = (rcClient.right - rcClient.left)/2 - bm.bmWidth/2;
859             y = (rcClient.bottom - rcClient.top)/2 - bm.bmHeight/2;
860             FillRect( hdc, &rcClient, hbrush );
861             BitBlt(hdc, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0,
862                    SRCCOPY);
863         }
864         else
865         {
866             StretchBlt(hdc, 0, 0, rcClient.right - rcClient.left,
867                        rcClient.bottom - rcClient.top, hMemDC,
868                        0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
869         }
870         SelectObject(hMemDC, oldbitmap);
871         DeleteDC(hMemDC);
872     }
873     else
874     {
875         RECT rcClient;
876         GetClientRect( hwnd, &rcClient );
877         FillRect( hdc, &rcClient, hbrush );
878     }
879 }
880
881
882 static void STATIC_PaintEnhMetafn(HWND hwnd, HDC hdc, DWORD style )
883 {
884     HENHMETAFILE hEnhMetaFile;
885     RECT rc;
886     HBRUSH hbrush;
887     
888     GetClientRect(hwnd, &rc);
889     hbrush = STATIC_SendWmCtlColorStatic(hwnd, hdc);
890     FillRect(hdc, &rc, hbrush);
891     if ((hEnhMetaFile = (HENHMETAFILE)GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET )))
892     {
893         /* The control's current font is not selected into the
894            device context! */
895         if (GetObjectType(hEnhMetaFile) == OBJ_ENHMETAFILE)
896             PlayEnhMetaFile(hdc, hEnhMetaFile, &rc);
897     }
898 }
899
900
901 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style )
902 {
903     RECT rc;
904
905     /* FIXME: sometimes (not always) sends WM_CTLCOLORSTATIC */
906     GetClientRect( hwnd, &rc );
907     switch (style & SS_TYPEMASK)
908     {
909         case SS_ETCHEDHORZ:
910             DrawEdge(hdc,&rc,EDGE_ETCHED,BF_TOP|BF_BOTTOM);
911             break;
912         case SS_ETCHEDVERT:
913             DrawEdge(hdc,&rc,EDGE_ETCHED,BF_LEFT|BF_RIGHT);
914             break;
915         case SS_ETCHEDFRAME:
916             DrawEdge (hdc, &rc, EDGE_ETCHED, BF_RECT);
917             break;
918     }
919 }