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