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