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