4 * Copyright David W. Metcalfe, 1993
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.
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.
36 * - SS_REALSIZECONTROL
46 * - STM_SETIMAGE: IMAGE_CURSOR, IMAGE_ENHMETAFILE
54 #include "wine/winuser16.h"
56 #include "user_private.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(static);
61 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style );
62 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style );
63 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style );
64 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style );
65 static void STATIC_PaintBitmapfn( HWND hwnd, HDC hdc, DWORD style );
66 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style );
67 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
68 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
70 static COLORREF color_3dshadow, color_3ddkshadow, color_3dhighlight;
72 /* offsets for GetWindowLong for static private information */
73 #define HFONT_GWL_OFFSET 0
74 #define HICON_GWL_OFFSET (sizeof(HFONT))
75 #define STATIC_EXTRA_BYTES (HICON_GWL_OFFSET + sizeof(HICON))
77 typedef void (*pfPaint)( HWND hwnd, HDC hdc, DWORD style );
79 static pfPaint staticPaintFunc[SS_TYPEMASK+1] =
81 STATIC_PaintTextfn, /* SS_LEFT */
82 STATIC_PaintTextfn, /* SS_CENTER */
83 STATIC_PaintTextfn, /* SS_RIGHT */
84 STATIC_PaintIconfn, /* SS_ICON */
85 STATIC_PaintRectfn, /* SS_BLACKRECT */
86 STATIC_PaintRectfn, /* SS_GRAYRECT */
87 STATIC_PaintRectfn, /* SS_WHITERECT */
88 STATIC_PaintRectfn, /* SS_BLACKFRAME */
89 STATIC_PaintRectfn, /* SS_GRAYFRAME */
90 STATIC_PaintRectfn, /* SS_WHITEFRAME */
91 NULL, /* SS_USERITEM */
92 STATIC_PaintTextfn, /* SS_SIMPLE */
93 STATIC_PaintTextfn, /* SS_LEFTNOWORDWRAP */
94 STATIC_PaintOwnerDrawfn, /* SS_OWNERDRAW */
95 STATIC_PaintBitmapfn, /* SS_BITMAP */
96 NULL, /* SS_ENHMETAFILE */
97 STATIC_PaintEtchedfn, /* SS_ETCHEDHORZ */
98 STATIC_PaintEtchedfn, /* SS_ETCHEDVERT */
99 STATIC_PaintEtchedfn, /* SS_ETCHEDFRAME */
103 /*********************************************************************
104 * static class descriptor
106 const struct builtin_class_descr STATIC_builtin_class =
109 CS_DBLCLKS | CS_PARENTDC, /* style */
110 StaticWndProcA, /* procA */
111 StaticWndProcW, /* procW */
112 STATIC_EXTRA_BYTES, /* extra */
113 IDC_ARROW, /* cursor */
118 /***********************************************************************
121 * Set the icon for an SS_ICON control.
123 static HICON STATIC_SetIcon( HWND hwnd, HICON hicon, DWORD style )
126 CURSORICONINFO *info = hicon?(CURSORICONINFO *) GlobalLock16(HICON_16(hicon)):NULL;
128 if ((style & SS_TYPEMASK) != SS_ICON) return 0;
129 if (hicon && !info) {
130 ERR("huh? hicon!=0, but info=0???\n");
133 prevIcon = (HICON)SetWindowLongPtrW( hwnd, HICON_GWL_OFFSET, (LONG_PTR)hicon );
134 if (hicon && !(style & SS_CENTERIMAGE))
136 SetWindowPos( hwnd, 0, 0, 0, info->nWidth, info->nHeight,
137 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
138 GlobalUnlock16(HICON_16(hicon));
143 /***********************************************************************
146 * Set the bitmap for an SS_BITMAP control.
148 static HBITMAP STATIC_SetBitmap( HWND hwnd, HBITMAP hBitmap, DWORD style )
152 if ((style & SS_TYPEMASK) != SS_BITMAP) return 0;
153 if (hBitmap && GetObjectType(hBitmap) != OBJ_BITMAP) {
154 ERR("huh? hBitmap!=0, but not bitmap\n");
157 hOldBitmap = (HBITMAP)SetWindowLongPtrW( hwnd, HICON_GWL_OFFSET, (LONG_PTR)hBitmap );
158 if (hBitmap && !(style & SS_CENTERIMAGE))
161 GetObjectW(hBitmap, sizeof(bm), &bm);
162 SetWindowPos( hwnd, 0, 0, 0, bm.bmWidth, bm.bmHeight,
163 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
168 /***********************************************************************
171 * Load the icon for an SS_ICON control.
173 static HICON STATIC_LoadIconA( HWND hwnd, LPCSTR name )
175 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
176 HICON hicon = LoadIconA( hInstance, name );
177 if (!hicon) hicon = LoadIconA( 0, name );
181 /***********************************************************************
184 * Load the icon for an SS_ICON control.
186 static HICON STATIC_LoadIconW( HWND hwnd, LPCWSTR name )
188 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
189 HICON hicon = LoadIconW( hInstance, name );
190 if (!hicon) hicon = LoadIconW( 0, name );
194 /***********************************************************************
197 * Load the bitmap for an SS_BITMAP control.
199 static HBITMAP STATIC_LoadBitmapA( HWND hwnd, LPCSTR name )
201 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
202 HBITMAP hbitmap = LoadBitmapA( hInstance, name );
203 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
204 hbitmap = LoadBitmapA( 0, name );
208 /***********************************************************************
211 * Load the bitmap for an SS_BITMAP control.
213 static HBITMAP STATIC_LoadBitmapW( HWND hwnd, LPCWSTR name )
215 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
216 HBITMAP hbitmap = LoadBitmapW( hInstance, name );
217 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
218 hbitmap = LoadBitmapW( 0, name );
222 /***********************************************************************
225 * Try to immediately paint the control.
227 static VOID STATIC_TryPaintFcn(HWND hwnd, LONG full_style)
229 LONG style = full_style & SS_TYPEMASK;
232 GetClientRect( hwnd, &rc );
233 if (!IsRectEmpty(&rc) && IsWindowVisible(hwnd) && staticPaintFunc[style])
237 (staticPaintFunc[style])( hwnd, hdc, full_style );
238 ReleaseDC( hwnd, hdc );
242 static VOID STATIC_InitColours(void)
244 color_3ddkshadow = GetSysColor(COLOR_3DDKSHADOW);
245 color_3dshadow = GetSysColor(COLOR_3DSHADOW);
246 color_3dhighlight = GetSysColor(COLOR_3DHIGHLIGHT);
249 /***********************************************************************
250 * StaticWndProc_common
252 static LRESULT StaticWndProc_common( HWND hwnd, UINT uMsg, WPARAM wParam,
253 LPARAM lParam, BOOL unicode )
256 LONG full_style = GetWindowLongW( hwnd, GWL_STYLE );
257 LONG style = full_style & SS_TYPEMASK;
262 if (style < 0L || style > SS_TYPEMASK)
264 ERR("Unknown style 0x%02lx\n", style );
267 STATIC_InitColours();
271 if (style == SS_ICON) {
274 * DestroyIcon32( STATIC_SetIcon( wndPtr, 0 ) );
276 * We don't want to do this yet because DestroyIcon32 is broken. If the icon
277 * had already been loaded by the application the last thing we want to do is
278 * GlobalFree16 the handle.
282 else return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
283 DefWindowProcA(hwnd, uMsg, wParam, lParam);
288 HDC hdc = wParam ? (HDC)wParam : BeginPaint(hwnd, &ps);
289 if (staticPaintFunc[style])
290 (staticPaintFunc[style])( hwnd, hdc, full_style );
291 if (!wParam) EndPaint(hwnd, &ps);
296 InvalidateRect(hwnd, NULL, TRUE);
299 case WM_SYSCOLORCHANGE:
300 STATIC_InitColours();
301 InvalidateRect(hwnd, NULL, TRUE);
305 if (full_style & SS_SUNKEN)
306 SetWindowLongW( hwnd, GWL_EXSTYLE,
307 GetWindowLongW( hwnd, GWL_EXSTYLE ) | WS_EX_STATICEDGE );
310 lParam = (LPARAM)(((LPCREATESTRUCTW)lParam)->lpszName);
312 lParam = (LPARAM)(((LPCREATESTRUCTA)lParam)->lpszName);
320 hIcon = STATIC_LoadIconW(hwnd, (LPCWSTR)lParam);
322 hIcon = STATIC_LoadIconA(hwnd, (LPCSTR)lParam);
323 /* FIXME : should we also return the previous hIcon here ??? */
324 STATIC_SetIcon(hwnd, hIcon, full_style);
331 hBitmap = STATIC_LoadBitmapW(hwnd, (LPCWSTR)lParam);
333 hBitmap = STATIC_LoadBitmapA(hwnd, (LPCSTR)lParam);
334 STATIC_SetBitmap(hwnd, hBitmap, full_style);
341 case SS_LEFTNOWORDWRAP:
346 lResult = DefWindowProcW( hwnd, WM_SETTEXT, wParam, lParam );
348 lResult = DefWindowProcA( hwnd, WM_SETTEXT, wParam, lParam );
350 if (uMsg == WM_SETTEXT)
351 STATIC_TryPaintFcn( hwnd, full_style );
358 lResult = DefWindowProcW( hwnd, WM_SETTEXT, wParam, lParam );
360 lResult = DefWindowProcA( hwnd, WM_SETTEXT, wParam, lParam );
362 if(uMsg == WM_SETTEXT)
363 InvalidateRect(hwnd, NULL, TRUE);
365 return 1; /* success. FIXME: check text length */
368 if ((style == SS_ICON) || (style == SS_BITMAP)) return 0;
369 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, wParam );
371 InvalidateRect( hwnd, NULL, TRUE );
375 return GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
378 if (full_style & SS_NOTIFY)
381 return HTTRANSPARENT;
387 case WM_NCLBUTTONDOWN:
388 if (full_style & SS_NOTIFY)
389 SendMessageW( GetParent(hwnd), WM_COMMAND,
390 MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_CLICKED ), (LPARAM)hwnd);
393 case WM_LBUTTONDBLCLK:
394 case WM_NCLBUTTONDBLCLK:
395 if (full_style & SS_NOTIFY)
396 SendMessageW( GetParent(hwnd), WM_COMMAND,
397 MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_DBLCLK ), (LPARAM)hwnd);
403 return GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET );
408 lResult = (LRESULT)STATIC_SetBitmap( hwnd, (HBITMAP)lParam, full_style );
411 FIXME("STM_SETIMAGE: Unhandled type IMAGE_CURSOR\n");
413 case IMAGE_ENHMETAFILE:
414 FIXME("STM_SETIMAGE: Unhandled type IMAGE_ENHMETAFILE\n");
417 lResult = (LRESULT)STATIC_SetIcon( hwnd, (HICON)lParam, full_style );
420 FIXME("STM_SETIMAGE: Unhandled type %x\n", wParam);
423 InvalidateRect( hwnd, NULL, TRUE );
428 lResult = (LRESULT)STATIC_SetIcon( hwnd, (HICON)wParam, full_style );
429 InvalidateRect( hwnd, NULL, TRUE );
433 return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
434 DefWindowProcA(hwnd, uMsg, wParam, lParam);
439 /***********************************************************************
442 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
444 if (!IsWindow( hWnd )) return 0;
445 return StaticWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
448 /***********************************************************************
451 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
453 if (!IsWindow( hWnd )) return 0;
454 return StaticWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
457 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style )
460 UINT id = (UINT)GetWindowLongPtrW( hwnd, GWLP_ID );
462 dis.CtlType = ODT_STATIC;
465 dis.itemAction = ODA_DRAWENTIRE;
470 GetClientRect( hwnd, &dis.rcItem );
472 SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, (WPARAM)hdc, (LPARAM)hwnd );
473 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
476 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style )
485 GetClientRect( hwnd, &rc);
487 switch (style & SS_TYPEMASK)
490 wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK;
494 wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK;
498 wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK;
502 wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
505 case SS_LEFTNOWORDWRAP:
506 wFormat = DT_LEFT | DT_EXPANDTABS | DT_VCENTER;
513 if (style & SS_NOPREFIX)
514 wFormat |= DT_NOPREFIX;
515 if (style & SS_CENTERIMAGE)
516 wFormat |= DT_VCENTER;
518 if ((hFont = (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET ))) SelectObject( hdc, hFont );
520 if ((style & SS_NOPREFIX) || ((style & SS_TYPEMASK) != SS_SIMPLE))
522 hBrush = (HBRUSH)SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC,
523 (WPARAM)hdc, (LPARAM)hwnd );
524 if (!hBrush) /* did the app forget to call defwindowproc ? */
525 hBrush = (HBRUSH)DefWindowProcW(GetParent(hwnd), WM_CTLCOLORSTATIC,
526 (WPARAM)hdc, (LPARAM)hwnd);
527 FillRect( hdc, &rc, hBrush );
529 if (!IsWindowEnabled(hwnd)) SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
531 if (!(len = SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 ))) return;
532 if (!(text = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return;
533 SendMessageW( hwnd, WM_GETTEXT, len + 1, (LPARAM)text );
534 DrawTextW( hdc, text, -1, &rc, wFormat );
535 HeapFree( GetProcessHeap(), 0, text );
538 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style )
543 GetClientRect( hwnd, &rc);
545 switch (style & SS_TYPEMASK)
548 hBrush = CreateSolidBrush(color_3ddkshadow);
549 FillRect( hdc, &rc, hBrush );
552 hBrush = CreateSolidBrush(color_3dshadow);
553 FillRect( hdc, &rc, hBrush );
556 hBrush = CreateSolidBrush(color_3dhighlight);
557 FillRect( hdc, &rc, hBrush );
560 hBrush = CreateSolidBrush(color_3ddkshadow);
561 FrameRect( hdc, &rc, hBrush );
564 hBrush = CreateSolidBrush(color_3dshadow);
565 FrameRect( hdc, &rc, hBrush );
568 hBrush = CreateSolidBrush(color_3dhighlight);
569 FrameRect( hdc, &rc, hBrush );
574 DeleteObject( hBrush );
578 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style )
585 GetClientRect( hwnd, &rc );
586 hbrush = (HBRUSH)SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC,
587 (WPARAM)hdc, (LPARAM)hwnd );
588 FillRect( hdc, &rc, hbrush );
589 hIcon = (HICON)GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET );
590 if (style & SS_CENTERIMAGE)
592 CURSORICONINFO *info = hIcon ? (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)) : NULL;
593 x = (rc.right - rc.left)/2 - (info ? info->nWidth/2 : 0);
594 y = (rc.bottom - rc.top)/2 - (info ? info->nHeight/2 : 0);
602 DrawIcon( hdc, x, y, hIcon );
605 static void STATIC_PaintBitmapfn(HWND hwnd, HDC hdc, DWORD style )
608 HBITMAP hBitmap, oldbitmap;
610 /* message is still sent, even if the returned brush is not used */
611 SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC,
612 (WPARAM)hdc, (LPARAM)hwnd );
614 if ((hBitmap = (HBITMAP)GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET )))
619 if(GetObjectType(hBitmap) != OBJ_BITMAP) return;
620 if (!(hMemDC = CreateCompatibleDC( hdc ))) return;
621 GetObjectW(hBitmap, sizeof(bm), &bm);
622 oldbitmap = SelectObject(hMemDC, hBitmap);
623 if (style & SS_CENTERIMAGE)
626 GetClientRect(hwnd, &rcClient);
627 x = (rcClient.right - rcClient.left)/2 - bm.bmWidth/2;
628 y = (rcClient.bottom - rcClient.top)/2 - bm.bmHeight/2;
635 BitBlt(hdc, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0,
637 SelectObject(hMemDC, oldbitmap);
643 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style )
647 GetClientRect( hwnd, &rc );
648 switch (style & SS_TYPEMASK)
651 DrawEdge(hdc,&rc,EDGE_ETCHED,BF_TOP|BF_BOTTOM);
654 DrawEdge(hdc,&rc,EDGE_ETCHED,BF_LEFT|BF_RIGHT);
657 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_RECT);