shlwapi: Beginning implementation of IUnknown_QueryServiceForWebBrowserApp.
[wine] / dlls / user32 / static.c
1 /*
2  * Static control
3  *
4  * Copyright  David W. Metcalfe, 1993
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * NOTES
21  *
22  * This code was audited for completeness against the documented features
23  * of Comctl32.dll version 6.0 on Oct. 4, 2004, by Dimitrie O. Paun.
24  * 
25  * Unless otherwise noted, we believe this code to be complete, as per
26  * the specification mentioned above.
27  * If you discover missing features, or bugs, please note them below.
28  *
29  * Notes:
30  *   - Windows XP introduced new behavior: The background of centered
31  *     icons and bitmaps is painted differently. This is only done if
32  *     a manifest is present.
33  *     Because it has not yet been decided how to implement the two
34  *     different modes in Wine, only the Windows XP mode is implemented.
35  *   - Controls with SS_SIMPLE but without SS_NOPREFIX:
36  *     The text should not be changed. Windows doesn't clear the
37  *     client rectangle, so the new text must be larger than the old one.
38  *   - The SS_RIGHTJUST style is currently not implemented by Windows
39  *     (or it does something different than documented).
40  *
41  * TODO:
42  *   - Animated cursors
43  */
44
45 #include <stdarg.h>
46
47 #include "windef.h"
48 #include "winbase.h"
49 #include "wingdi.h"
50 #include "wine/winuser16.h"
51 #include "controls.h"
52 #include "user_private.h"
53 #include "wine/debug.h"
54
55 WINE_DEFAULT_DEBUG_CHANNEL(static);
56
57 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style );
58 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style );
59 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style );
60 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style );
61 static void STATIC_PaintBitmapfn( HWND hwnd, HDC hdc, DWORD style );
62 static void STATIC_PaintEnhMetafn( HWND hwnd, HDC hdc, DWORD style );
63 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style );
64 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
65 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
66
67 static COLORREF color_3dshadow, color_3ddkshadow, color_3dhighlight;
68
69 /* offsets for GetWindowLong for static private information */
70 #define HFONT_GWL_OFFSET    0
71 #define HICON_GWL_OFFSET    (sizeof(HFONT))
72 #define STATIC_EXTRA_BYTES  (HICON_GWL_OFFSET + sizeof(HICON))
73
74 typedef void (*pfPaint)( HWND hwnd, HDC hdc, DWORD style );
75
76 static const pfPaint staticPaintFunc[SS_TYPEMASK+1] =
77 {
78     STATIC_PaintTextfn,      /* SS_LEFT */
79     STATIC_PaintTextfn,      /* SS_CENTER */
80     STATIC_PaintTextfn,      /* SS_RIGHT */
81     STATIC_PaintIconfn,      /* SS_ICON */
82     STATIC_PaintRectfn,      /* SS_BLACKRECT */
83     STATIC_PaintRectfn,      /* SS_GRAYRECT */
84     STATIC_PaintRectfn,      /* SS_WHITERECT */
85     STATIC_PaintRectfn,      /* SS_BLACKFRAME */
86     STATIC_PaintRectfn,      /* SS_GRAYFRAME */
87     STATIC_PaintRectfn,      /* SS_WHITEFRAME */
88     NULL,                    /* SS_USERITEM */
89     STATIC_PaintTextfn,      /* SS_SIMPLE */
90     STATIC_PaintTextfn,      /* SS_LEFTNOWORDWRAP */
91     STATIC_PaintOwnerDrawfn, /* SS_OWNERDRAW */
92     STATIC_PaintBitmapfn,    /* SS_BITMAP */
93     STATIC_PaintEnhMetafn,   /* SS_ENHMETAFILE */
94     STATIC_PaintEtchedfn,    /* SS_ETCHEDHORZ */
95     STATIC_PaintEtchedfn,    /* SS_ETCHEDVERT */
96     STATIC_PaintEtchedfn,    /* SS_ETCHEDFRAME */
97 };
98
99
100 /*********************************************************************
101  * static class descriptor
102  */
103 static const WCHAR staticW[] = {'S','t','a','t','i','c',0};
104 const struct builtin_class_descr STATIC_builtin_class =
105 {
106     staticW,             /* name */
107     CS_DBLCLKS | CS_PARENTDC, /* style  */
108     StaticWndProcA,      /* procA */
109     StaticWndProcW,      /* procW */
110     STATIC_EXTRA_BYTES,  /* extra */
111     IDC_ARROW,           /* cursor */
112     0                    /* brush */
113 };
114
115 static void setup_clipping(HWND hwnd, HDC hdc, HRGN *orig)
116 {
117     RECT rc;
118     HRGN hrgn;
119
120     /* Native control has always a clipping region set (this may be because
121      * builtin controls uses CS_PARENTDC) and an application depends on it
122      */
123     hrgn = CreateRectRgn(0, 0, 1, 1);
124     if (GetClipRgn(hdc, hrgn) != 1)
125     {
126         DeleteObject(hrgn);
127         *orig = NULL;
128     } else
129         *orig = hrgn;
130
131     GetClientRect(hwnd, &rc);
132     DPtoLP(hdc, (POINT *)&rc, 2);
133     IntersectClipRect(hdc, rc.left, rc.top, rc.right, rc.bottom);
134 }
135
136 static void restore_clipping(HDC hdc, HRGN hrgn)
137 {
138     SelectClipRgn(hdc, hrgn);
139     if (hrgn != NULL)
140         DeleteObject(hrgn);
141 }
142
143 /***********************************************************************
144  *           STATIC_SetIcon
145  *
146  * Set the icon for an SS_ICON control.
147  */
148 static HICON STATIC_SetIcon( HWND hwnd, HICON hicon, DWORD style )
149 {
150     HICON prevIcon;
151     SIZE size;
152
153     if ((style & SS_TYPEMASK) != SS_ICON) return 0;
154     if (hicon && !get_icon_size( hicon, &size ))
155     {
156         WARN("hicon != 0, but invalid\n");
157         return 0;
158     }
159     prevIcon = (HICON)SetWindowLongPtrW( hwnd, HICON_GWL_OFFSET, (LONG_PTR)hicon );
160     if (hicon && !(style & SS_CENTERIMAGE) && !(style & SS_REALSIZECONTROL))
161     {
162         /* Windows currently doesn't implement SS_RIGHTJUST */
163         /*
164         if ((style & SS_RIGHTJUST) != 0)
165         {
166             RECT wr;
167             GetWindowRect(hwnd, &wr);
168             SetWindowPos( hwnd, 0, wr.right - info->nWidth, wr.bottom - info->nHeight,
169                           info->nWidth, info->nHeight, SWP_NOACTIVATE | SWP_NOZORDER );
170         }
171         else */
172         {
173             SetWindowPos( hwnd, 0, 0, 0, size.cx, size.cy, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
174         }
175     }
176     return prevIcon;
177 }
178
179 /***********************************************************************
180  *           STATIC_SetBitmap
181  *
182  * Set the bitmap for an SS_BITMAP control.
183  */
184 static HBITMAP STATIC_SetBitmap( HWND hwnd, HBITMAP hBitmap, DWORD style )
185 {
186     HBITMAP hOldBitmap;
187
188     if ((style & SS_TYPEMASK) != SS_BITMAP) return 0;
189     if (hBitmap && GetObjectType(hBitmap) != OBJ_BITMAP) {
190         WARN("hBitmap != 0, but it's not a bitmap\n");
191         return 0;
192     }
193     hOldBitmap = (HBITMAP)SetWindowLongPtrW( hwnd, HICON_GWL_OFFSET, (LONG_PTR)hBitmap );
194     if (hBitmap && !(style & SS_CENTERIMAGE) && !(style & SS_REALSIZECONTROL))
195     {
196         BITMAP bm;
197         GetObjectW(hBitmap, sizeof(bm), &bm);
198         /* Windows currently doesn't implement SS_RIGHTJUST */
199         /*
200         if ((style & SS_RIGHTJUST) != 0)
201         {
202             RECT wr;
203             GetWindowRect(hwnd, &wr);
204             SetWindowPos( hwnd, 0, wr.right - bm.bmWidth, wr.bottom - bm.bmHeight,
205                           bm.bmWidth, bm.bmHeight, SWP_NOACTIVATE | SWP_NOZORDER );
206         }
207         else */
208         {
209             SetWindowPos( hwnd, 0, 0, 0, bm.bmWidth, bm.bmHeight,
210                           SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
211         }
212         
213     }
214     return hOldBitmap;
215 }
216
217 /***********************************************************************
218  *           STATIC_SetEnhMetaFile
219  *
220  * Set the enhanced metafile for an SS_ENHMETAFILE control.
221  */
222 static HENHMETAFILE STATIC_SetEnhMetaFile( HWND hwnd, HENHMETAFILE hEnhMetaFile, DWORD style )
223 {
224     if ((style & SS_TYPEMASK) != SS_ENHMETAFILE) return 0;
225     if (hEnhMetaFile && GetObjectType(hEnhMetaFile) != OBJ_ENHMETAFILE) {
226         WARN("hEnhMetaFile != 0, but it's not an enhanced metafile\n");
227         return 0;
228     }
229     return (HENHMETAFILE)SetWindowLongPtrW( hwnd, HICON_GWL_OFFSET, (LONG_PTR)hEnhMetaFile );
230 }
231
232 /***********************************************************************
233  *           STATIC_GetImage
234  *
235  * Gets the bitmap for an SS_BITMAP control, the icon/cursor for an
236  * SS_ICON control or the enhanced metafile for an SS_ENHMETAFILE control.
237  */
238 static HANDLE STATIC_GetImage( HWND hwnd, WPARAM wParam, DWORD style )
239 {
240     switch(style & SS_TYPEMASK)
241     {
242         case SS_ICON:
243             if ((wParam != IMAGE_ICON) &&
244                 (wParam != IMAGE_CURSOR)) return NULL;
245             break;
246         case SS_BITMAP:
247             if (wParam != IMAGE_BITMAP) return NULL;
248             break;
249         case SS_ENHMETAFILE:
250             if (wParam != IMAGE_ENHMETAFILE) return NULL;
251             break;
252         default:
253             return NULL;
254     }
255     return (HANDLE)GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET );
256 }
257
258 /***********************************************************************
259  *           STATIC_LoadIconA
260  *
261  * Load the icon for an SS_ICON control.
262  */
263 static HICON STATIC_LoadIconA( HWND hwnd, LPCSTR name, DWORD style )
264 {
265     HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
266     if ((style & SS_REALSIZEIMAGE) != 0)
267     {
268         return LoadImageA(hInstance, name, IMAGE_ICON, 0, 0, LR_SHARED);
269     }
270     else
271     {
272         HICON hicon = LoadIconA( hInstance, name );
273         if (!hicon) hicon = LoadCursorA( hInstance, name );
274         if (!hicon) hicon = LoadIconA( 0, name );
275         /* Windows doesn't try to load a standard cursor,
276            probably because most IDs for standard cursors conflict
277            with the IDs for standard icons anyway */
278         return hicon;
279     }
280 }
281
282 /***********************************************************************
283  *           STATIC_LoadIconW
284  *
285  * Load the icon for an SS_ICON control.
286  */
287 static HICON STATIC_LoadIconW( HWND hwnd, LPCWSTR name, DWORD style )
288 {
289     HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
290     if ((style & SS_REALSIZEIMAGE) != 0)
291     {
292         return LoadImageW(hInstance, name, IMAGE_ICON, 0, 0, LR_SHARED);
293     }
294     else
295     {
296         HICON hicon = LoadIconW( hInstance, name );
297         if (!hicon) hicon = LoadCursorW( hInstance, name );
298         if (!hicon) hicon = LoadIconW( 0, name );
299         /* Windows doesn't try to load a standard cursor,
300            probably because most IDs for standard cursors conflict
301            with the IDs for standard icons anyway */
302         return hicon;
303     }
304 }
305
306 /***********************************************************************
307  *           STATIC_LoadBitmapA
308  *
309  * Load the bitmap for an SS_BITMAP control.
310  */
311 static HBITMAP STATIC_LoadBitmapA( HWND hwnd, LPCSTR name )
312 {
313     HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
314     /* Windows doesn't try to load OEM Bitmaps (hInstance == NULL) */
315     return LoadBitmapA( hInstance, name );
316 }
317
318 /***********************************************************************
319  *           STATIC_LoadBitmapW
320  *
321  * Load the bitmap for an SS_BITMAP control.
322  */
323 static HBITMAP STATIC_LoadBitmapW( HWND hwnd, LPCWSTR name )
324 {
325     HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
326     /* Windows doesn't try to load OEM Bitmaps (hInstance == NULL) */
327     return LoadBitmapW( hInstance, name );
328 }
329
330 /***********************************************************************
331  *           STATIC_TryPaintFcn
332  *
333  * Try to immediately paint the control.
334  */
335 static VOID STATIC_TryPaintFcn(HWND hwnd, LONG full_style)
336 {
337     LONG style = full_style & SS_TYPEMASK;
338     RECT rc;
339
340     GetClientRect( hwnd, &rc );
341     if (!IsRectEmpty(&rc) && IsWindowVisible(hwnd) && staticPaintFunc[style])
342     {
343         HDC hdc;
344         HRGN hOrigClipping;
345
346         hdc = GetDC( hwnd );
347         setup_clipping(hwnd, hdc, &hOrigClipping);
348         (staticPaintFunc[style])( hwnd, hdc, full_style );
349         restore_clipping(hdc, hOrigClipping);
350         ReleaseDC( hwnd, hdc );
351     }
352 }
353
354 static HBRUSH STATIC_SendWmCtlColorStatic(HWND hwnd, HDC hdc)
355 {
356     HBRUSH hBrush;
357     HWND parent = GetParent(hwnd);
358
359     if (!parent) parent = hwnd;
360     hBrush = (HBRUSH) SendMessageW( parent,
361                     WM_CTLCOLORSTATIC, (WPARAM)hdc, (LPARAM)hwnd );
362     if (!hBrush) /* did the app forget to call DefWindowProc ? */
363     {
364         /* FIXME: DefWindowProc should return different colors if a
365                   manifest is present */
366         hBrush = (HBRUSH)DefWindowProcW( parent, WM_CTLCOLORSTATIC,
367                                         (WPARAM)hdc, (LPARAM)hwnd);
368     }
369     return hBrush;
370 }
371
372 static VOID STATIC_InitColours(void)
373 {
374     color_3ddkshadow  = GetSysColor(COLOR_3DDKSHADOW);
375     color_3dshadow    = GetSysColor(COLOR_3DSHADOW);
376     color_3dhighlight = GetSysColor(COLOR_3DHIGHLIGHT);
377 }
378
379 /***********************************************************************
380  *           hasTextStyle
381  *
382  * Tests if the control displays text.
383  */
384 static BOOL hasTextStyle( DWORD style )
385 {
386     switch(style & SS_TYPEMASK)
387     {
388         case SS_SIMPLE:
389         case SS_LEFT:
390         case SS_LEFTNOWORDWRAP:
391         case SS_CENTER:
392         case SS_RIGHT:
393         case SS_OWNERDRAW:
394             return TRUE;
395     }
396     
397     return FALSE;
398 }
399
400 /***********************************************************************
401  *           StaticWndProc_common
402  */
403 static LRESULT StaticWndProc_common( HWND hwnd, UINT uMsg, WPARAM wParam,
404                                      LPARAM lParam, BOOL unicode )
405 {
406     LRESULT lResult = 0;
407     LONG full_style = GetWindowLongW( hwnd, GWL_STYLE );
408     LONG style = full_style & SS_TYPEMASK;
409
410     switch (uMsg)
411     {
412     case WM_CREATE:
413         if (style < 0L || style > SS_TYPEMASK)
414         {
415             ERR("Unknown style 0x%02x\n", style );
416             return -1;
417         }
418         STATIC_InitColours();
419         break;
420
421     case WM_NCDESTROY:
422         if (style == SS_ICON) {
423 /*
424  * FIXME
425  *           DestroyIcon32( STATIC_SetIcon( wndPtr, 0 ) );
426  *
427  * We don't want to do this yet because DestroyIcon32 is broken. If the icon
428  * had already been loaded by the application the last thing we want to do is
429  * GlobalFree16 the handle.
430  */
431             break;
432         }
433         else return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
434                               DefWindowProcA(hwnd, uMsg, wParam, lParam);
435
436     case WM_ERASEBKGND:
437         /* do all painting in WM_PAINT like Windows does */
438         return 1;
439
440     case WM_PRINTCLIENT:
441     case WM_PAINT:
442         {
443             PAINTSTRUCT ps;
444             HDC hdc = wParam ? (HDC)wParam : BeginPaint(hwnd, &ps);
445             if (staticPaintFunc[style])
446             {
447                 HRGN hOrigClipping;
448                 setup_clipping(hwnd, hdc, &hOrigClipping);
449                 (staticPaintFunc[style])( hwnd, hdc, full_style );
450                 restore_clipping(hdc, hOrigClipping);
451             }
452             if (!wParam) EndPaint(hwnd, &ps);
453         }
454         break;
455
456     case WM_ENABLE:
457         STATIC_TryPaintFcn( hwnd, full_style );
458         if (full_style & SS_NOTIFY) {
459             if (wParam) {
460                 SendMessageW( GetParent(hwnd), WM_COMMAND,
461                               MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_ENABLE ), (LPARAM)hwnd);
462             }
463             else {
464                 SendMessageW( GetParent(hwnd), WM_COMMAND,
465                               MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_DISABLE ), (LPARAM)hwnd);
466             }
467         }
468         break;
469
470     case WM_SYSCOLORCHANGE:
471         STATIC_InitColours();
472         STATIC_TryPaintFcn( hwnd, full_style );
473         break;
474
475     case WM_NCCREATE:
476         {
477             LPCSTR textA;
478             LPCWSTR textW;
479     
480             if (full_style & SS_SUNKEN)
481                 SetWindowLongW( hwnd, GWL_EXSTYLE,
482                                 GetWindowLongW( hwnd, GWL_EXSTYLE ) | WS_EX_STATICEDGE );
483
484             if(unicode)
485             {
486                 textA = NULL;
487                 textW = ((LPCREATESTRUCTW)lParam)->lpszName;
488             }
489             else
490             {
491                 textA = ((LPCREATESTRUCTA)lParam)->lpszName;
492                 textW = NULL;
493             }
494
495             switch (style) {
496             case SS_ICON:
497                 {
498                     HICON hIcon;
499                     if(unicode)
500                        hIcon = STATIC_LoadIconW(hwnd, textW, full_style);
501                     else
502                        hIcon = STATIC_LoadIconA(hwnd, textA, full_style);
503                     STATIC_SetIcon(hwnd, hIcon, full_style);
504                 }
505                 break;
506             case SS_BITMAP:
507                 {
508                     HBITMAP hBitmap;
509                     if(unicode)
510                         hBitmap = STATIC_LoadBitmapW(hwnd, textW);
511                     else
512                         hBitmap = STATIC_LoadBitmapA(hwnd, textA);
513                     STATIC_SetBitmap(hwnd, hBitmap, full_style);
514                 }
515                 break;
516             }
517             /* SS_ENHMETAFILE: Despite what MSDN says, Windows does not load
518                the enhanced metafile that was specified as the window text. */
519         }
520         return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
521                          DefWindowProcA(hwnd, uMsg, wParam, lParam);
522
523     case WM_SETTEXT:
524         if (hasTextStyle( full_style ))
525         {
526             if (HIWORD(lParam))
527             {
528                 if(unicode)
529                      lResult = DefWindowProcW( hwnd, uMsg, wParam, lParam );
530                 else
531                     lResult = DefWindowProcA( hwnd, uMsg, wParam, lParam );
532                 STATIC_TryPaintFcn( hwnd, full_style );
533             }
534         }
535         break;
536
537     case WM_SETFONT:
538         if (hasTextStyle( full_style ))
539         {
540             SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, wParam );
541             if (LOWORD(lParam))
542                 RedrawWindow( hwnd, NULL, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_ALLCHILDREN );
543         }
544         break;
545
546     case WM_GETFONT:
547         return GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
548
549     case WM_NCHITTEST:
550         if (full_style & SS_NOTIFY)
551            return HTCLIENT;
552         else
553            return HTTRANSPARENT;
554
555     case WM_GETDLGCODE:
556         return DLGC_STATIC;
557
558     case WM_LBUTTONDOWN:
559     case WM_NCLBUTTONDOWN:
560         if (full_style & SS_NOTIFY)
561             SendMessageW( GetParent(hwnd), WM_COMMAND,
562                           MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_CLICKED ), (LPARAM)hwnd);
563         return 0;
564
565     case WM_LBUTTONDBLCLK:
566     case WM_NCLBUTTONDBLCLK:
567         if (full_style & SS_NOTIFY)
568             SendMessageW( GetParent(hwnd), WM_COMMAND,
569                           MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_DBLCLK ), (LPARAM)hwnd);
570         return 0;
571
572     case STM_GETIMAGE:
573         return (LRESULT)STATIC_GetImage( hwnd, wParam, full_style );
574
575     case STM_GETICON:
576         return (LRESULT)STATIC_GetImage( hwnd, IMAGE_ICON, full_style );
577
578     case STM_SETIMAGE:
579         switch(wParam) {
580         case IMAGE_BITMAP:
581             lResult = (LRESULT)STATIC_SetBitmap( hwnd, (HBITMAP)lParam, full_style );
582             break;
583         case IMAGE_ENHMETAFILE:
584             lResult = (LRESULT)STATIC_SetEnhMetaFile( hwnd, (HENHMETAFILE)lParam, full_style );
585             break;
586         case IMAGE_ICON:
587         case IMAGE_CURSOR:
588             lResult = (LRESULT)STATIC_SetIcon( hwnd, (HICON)lParam, full_style );
589             break;
590         default:
591             FIXME("STM_SETIMAGE: Unhandled type %lx\n", wParam);
592             break;
593         }
594         STATIC_TryPaintFcn( hwnd, full_style );
595         break;
596
597     case STM_SETICON:
598         lResult = (LRESULT)STATIC_SetIcon( hwnd, (HICON)wParam, full_style );
599         STATIC_TryPaintFcn( hwnd, full_style );
600         break;
601
602     default:
603         return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
604                          DefWindowProcA(hwnd, uMsg, wParam, lParam);
605     }
606     return lResult;
607 }
608
609 /***********************************************************************
610  *           StaticWndProc_wrapper16
611  */
612 static LRESULT StaticWndProc_wrapper16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
613 {
614     switch (msg)
615     {
616     case STM_SETICON16:
617         wParam = (WPARAM)HICON_32( (HICON16)wParam );
618         return StaticWndProc_common( hwnd, STM_SETICON, wParam, lParam, FALSE );
619     case STM_GETICON16:
620         return HICON_16( StaticWndProc_common( hwnd, STM_GETICON, wParam, lParam, FALSE ));
621     default:
622         return StaticWndProc_common( hwnd, msg, wParam, lParam, unicode );
623     }
624 }
625
626 /***********************************************************************
627  *           StaticWndProcA
628  */
629 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
630 {
631     if (!IsWindow( hWnd )) return 0;
632     return StaticWndProc_wrapper16(hWnd, uMsg, wParam, lParam, FALSE);
633 }
634
635 /***********************************************************************
636  *           StaticWndProcW
637  */
638 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
639 {
640     if (!IsWindow( hWnd )) return 0;
641     return StaticWndProc_wrapper16(hWnd, uMsg, wParam, lParam, TRUE);
642 }
643
644 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style )
645 {
646   DRAWITEMSTRUCT dis;
647   HFONT font, oldFont = NULL;
648   UINT id = (UINT)GetWindowLongPtrW( hwnd, GWLP_ID );
649
650   dis.CtlType    = ODT_STATIC;
651   dis.CtlID      = id;
652   dis.itemID     = 0;
653   dis.itemAction = ODA_DRAWENTIRE;
654   dis.itemState  = IsWindowEnabled(hwnd) ? 0 : ODS_DISABLED;
655   dis.hwndItem   = hwnd;
656   dis.hDC        = hdc;
657   dis.itemData   = 0;
658   GetClientRect( hwnd, &dis.rcItem );
659
660   font = (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
661   if (font) oldFont = SelectObject( hdc, font );
662   SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, (WPARAM)hdc, (LPARAM)hwnd );
663   SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
664   if (font) SelectObject( hdc, oldFont );
665 }
666
667 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style )
668 {
669     RECT rc;
670     HBRUSH hBrush;
671     HFONT hFont, hOldFont = NULL;
672     WORD wFormat;
673     INT len, buf_size;
674     WCHAR *text;
675
676     GetClientRect( hwnd, &rc);
677
678     switch (style & SS_TYPEMASK)
679     {
680     case SS_LEFT:
681         wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK;
682         break;
683
684     case SS_CENTER:
685         wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK;
686         break;
687
688     case SS_RIGHT:
689         wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK;
690         break;
691
692     case SS_SIMPLE:
693         wFormat = DT_LEFT | DT_SINGLELINE;
694         break;
695
696     case SS_LEFTNOWORDWRAP:
697         wFormat = DT_LEFT | DT_EXPANDTABS;
698         break;
699
700     default:
701         return;
702     }
703
704     if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_RIGHT)
705         wFormat = DT_RIGHT | (wFormat & ~(DT_LEFT | DT_CENTER));
706
707     if (style & SS_NOPREFIX)
708         wFormat |= DT_NOPREFIX;
709
710     if ((style & SS_TYPEMASK) != SS_SIMPLE)
711     {
712         if (style & SS_CENTERIMAGE)
713             wFormat |= DT_SINGLELINE | DT_VCENTER;
714         if (style & SS_EDITCONTROL)
715             wFormat |= DT_EDITCONTROL;
716         if (style & SS_ENDELLIPSIS)
717             wFormat |= DT_SINGLELINE | DT_END_ELLIPSIS;
718         if (style & SS_PATHELLIPSIS)
719             wFormat |= DT_SINGLELINE | DT_PATH_ELLIPSIS;
720         if (style & SS_WORDELLIPSIS)
721             wFormat |= DT_SINGLELINE | DT_WORD_ELLIPSIS;
722     }
723
724     if ((hFont = (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET )))
725         hOldFont = SelectObject( hdc, hFont );
726
727     /* SS_SIMPLE controls: WM_CTLCOLORSTATIC is sent, but the returned
728                            brush is not used */
729     hBrush = STATIC_SendWmCtlColorStatic(hwnd, hdc);
730
731     if ((style & SS_TYPEMASK) != SS_SIMPLE)
732     {
733         FillRect( hdc, &rc, hBrush );
734         if (!IsWindowEnabled(hwnd)) SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
735     }
736
737     buf_size = 256;
738     if (!(text = HeapAlloc( GetProcessHeap(), 0, buf_size * sizeof(WCHAR) )))
739         goto no_TextOut;
740
741     while ((len = InternalGetWindowText( hwnd, text, buf_size )) == buf_size - 1)
742     {
743         buf_size *= 2;
744         if (!(text = HeapReAlloc( GetProcessHeap(), 0, text, buf_size * sizeof(WCHAR) )))
745             goto no_TextOut;
746     }
747
748     if (!len) goto no_TextOut;
749
750     if (((style & SS_TYPEMASK) == SS_SIMPLE) && (style & SS_NOPREFIX))
751     {
752         /* Windows uses the faster ExtTextOut() to draw the text and
753            to paint the whole client rectangle with the text background
754            color. Reference: "Static Controls" by Kyle Marsh, 1992 */
755         ExtTextOutW( hdc, rc.left, rc.top, ETO_CLIPPED | ETO_OPAQUE,
756                      &rc, text, len, NULL );
757     }
758     else
759     {
760         DrawTextW( hdc, text, -1, &rc, wFormat );
761     }
762
763 no_TextOut:
764     HeapFree( GetProcessHeap(), 0, text );
765
766     if (hFont)
767         SelectObject( hdc, hOldFont );
768 }
769
770 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style )
771 {
772     RECT rc;
773     HBRUSH hBrush;
774
775     GetClientRect( hwnd, &rc);
776
777     /* FIXME: send WM_CTLCOLORSTATIC */
778     switch (style & SS_TYPEMASK)
779     {
780     case SS_BLACKRECT:
781         hBrush = CreateSolidBrush(color_3ddkshadow);
782         FillRect( hdc, &rc, hBrush );
783         break;
784     case SS_GRAYRECT:
785         hBrush = CreateSolidBrush(color_3dshadow);
786         FillRect( hdc, &rc, hBrush );
787         break;
788     case SS_WHITERECT:
789         hBrush = CreateSolidBrush(color_3dhighlight);
790         FillRect( hdc, &rc, hBrush );
791         break;
792     case SS_BLACKFRAME:
793         hBrush = CreateSolidBrush(color_3ddkshadow);
794         FrameRect( hdc, &rc, hBrush );
795         break;
796     case SS_GRAYFRAME:
797         hBrush = CreateSolidBrush(color_3dshadow);
798         FrameRect( hdc, &rc, hBrush );
799         break;
800     case SS_WHITEFRAME:
801         hBrush = CreateSolidBrush(color_3dhighlight);
802         FrameRect( hdc, &rc, hBrush );
803         break;
804     default:
805         return;
806     }
807     DeleteObject( hBrush );
808 }
809
810
811 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style )
812 {
813     RECT rc, iconRect;
814     HBRUSH hbrush;
815     HICON hIcon;
816     SIZE size;
817
818     GetClientRect( hwnd, &rc );
819     hbrush = STATIC_SendWmCtlColorStatic(hwnd, hdc);
820     hIcon = (HICON)GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET );
821     if (!hIcon || !get_icon_size( hIcon, &size ))
822     {
823         FillRect(hdc, &rc, hbrush);
824     }
825     else
826     {
827         if (style & SS_CENTERIMAGE)
828         {
829             iconRect.left = (rc.right - rc.left) / 2 - size.cx / 2;
830             iconRect.top = (rc.bottom - rc.top) / 2 - size.cy / 2;
831             iconRect.right = iconRect.left + size.cx;
832             iconRect.bottom = iconRect.top + size.cy;
833         }
834         else
835             iconRect = rc;
836         FillRect( hdc, &rc, hbrush );
837         DrawIconEx( hdc, iconRect.left, iconRect.top, hIcon, iconRect.right - iconRect.left,
838                     iconRect.bottom - iconRect.top, 0, NULL, DI_NORMAL );
839     }
840 }
841
842 static void STATIC_PaintBitmapfn(HWND hwnd, HDC hdc, DWORD style )
843 {
844     HDC hMemDC;
845     HBITMAP hBitmap, oldbitmap;
846     HBRUSH hbrush;
847
848     /* message is still sent, even if the returned brush is not used */
849     hbrush = STATIC_SendWmCtlColorStatic(hwnd, hdc);
850
851     if ((hBitmap = (HBITMAP)GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET ))
852          && (GetObjectType(hBitmap) == OBJ_BITMAP)
853          && (hMemDC = CreateCompatibleDC( hdc )))
854     {
855         BITMAP bm;
856         RECT rcClient;
857         LOGBRUSH brush;
858
859         GetObjectW(hBitmap, sizeof(bm), &bm);
860         oldbitmap = SelectObject(hMemDC, hBitmap);
861
862         /* Set the background color for monochrome bitmaps
863            to the color of the background brush */
864         if (GetObjectW( hbrush, sizeof(brush), &brush ))
865         {
866             if (brush.lbStyle == BS_SOLID)
867                 SetBkColor(hdc, brush.lbColor);
868         }
869         GetClientRect(hwnd, &rcClient);
870         if (style & SS_CENTERIMAGE)
871         {
872             INT x, y;
873             x = (rcClient.right - rcClient.left)/2 - bm.bmWidth/2;
874             y = (rcClient.bottom - rcClient.top)/2 - bm.bmHeight/2;
875             FillRect( hdc, &rcClient, hbrush );
876             BitBlt(hdc, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0,
877                    SRCCOPY);
878         }
879         else
880         {
881             StretchBlt(hdc, 0, 0, rcClient.right - rcClient.left,
882                        rcClient.bottom - rcClient.top, hMemDC,
883                        0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
884         }
885         SelectObject(hMemDC, oldbitmap);
886         DeleteDC(hMemDC);
887     }
888 }
889
890
891 static void STATIC_PaintEnhMetafn(HWND hwnd, HDC hdc, DWORD style )
892 {
893     HENHMETAFILE hEnhMetaFile;
894     RECT rc;
895     HBRUSH hbrush;
896     
897     GetClientRect(hwnd, &rc);
898     hbrush = STATIC_SendWmCtlColorStatic(hwnd, hdc);
899     FillRect(hdc, &rc, hbrush);
900     if ((hEnhMetaFile = (HENHMETAFILE)GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET )))
901     {
902         /* The control's current font is not selected into the
903            device context! */
904         if (GetObjectType(hEnhMetaFile) == OBJ_ENHMETAFILE)
905             PlayEnhMetaFile(hdc, hEnhMetaFile, &rc);
906     }
907 }
908
909
910 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style )
911 {
912     RECT rc;
913
914     /* FIXME: sometimes (not always) sends WM_CTLCOLORSTATIC */
915     GetClientRect( hwnd, &rc );
916     switch (style & SS_TYPEMASK)
917     {
918         case SS_ETCHEDHORZ:
919             DrawEdge(hdc,&rc,EDGE_ETCHED,BF_TOP|BF_BOTTOM);
920             break;
921         case SS_ETCHEDVERT:
922             DrawEdge(hdc,&rc,EDGE_ETCHED,BF_LEFT|BF_RIGHT);
923             break;
924         case SS_ETCHEDFRAME:
925             DrawEdge (hdc, &rc, EDGE_ETCHED, BF_RECT);
926             break;
927     }
928 }