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