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