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