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.
37 * - SS_REALSIZECONTROL
47 * - STM_SETIMAGE: IMAGE_CURSOR, IMAGE_ENHMETAFILE
55 #include "wine/winuser16.h"
57 #include "user_private.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(static);
62 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style );
63 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style );
64 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style );
65 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style );
66 static void STATIC_PaintBitmapfn( HWND hwnd, HDC hdc, DWORD style );
67 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style );
68 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
69 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
71 static COLORREF color_windowframe, color_background, color_window;
73 /* offsets for GetWindowLong for static private information */
74 #define HFONT_GWL_OFFSET 0
75 #define HICON_GWL_OFFSET (sizeof(HFONT))
76 #define STATIC_EXTRA_BYTES (HICON_GWL_OFFSET + sizeof(HICON))
78 typedef void (*pfPaint)( HWND hwnd, HDC hdc, DWORD style );
80 static pfPaint staticPaintFunc[SS_TYPEMASK+1] =
82 STATIC_PaintTextfn, /* SS_LEFT */
83 STATIC_PaintTextfn, /* SS_CENTER */
84 STATIC_PaintTextfn, /* SS_RIGHT */
85 STATIC_PaintIconfn, /* SS_ICON */
86 STATIC_PaintRectfn, /* SS_BLACKRECT */
87 STATIC_PaintRectfn, /* SS_GRAYRECT */
88 STATIC_PaintRectfn, /* SS_WHITERECT */
89 STATIC_PaintRectfn, /* SS_BLACKFRAME */
90 STATIC_PaintRectfn, /* SS_GRAYFRAME */
91 STATIC_PaintRectfn, /* SS_WHITEFRAME */
92 NULL, /* SS_USERITEM */
93 STATIC_PaintTextfn, /* SS_SIMPLE */
94 STATIC_PaintTextfn, /* SS_LEFTNOWORDWRAP */
95 STATIC_PaintOwnerDrawfn, /* SS_OWNERDRAW */
96 STATIC_PaintBitmapfn, /* SS_BITMAP */
97 NULL, /* SS_ENHMETAFILE */
98 STATIC_PaintEtchedfn, /* SS_ETCHEDHORZ */
99 STATIC_PaintEtchedfn, /* SS_ETCHEDVERT */
100 STATIC_PaintEtchedfn, /* SS_ETCHEDFRAME */
104 /*********************************************************************
105 * static class descriptor
107 const struct builtin_class_descr STATIC_builtin_class =
110 CS_DBLCLKS | CS_PARENTDC, /* style */
111 StaticWndProcA, /* procA */
112 StaticWndProcW, /* procW */
113 STATIC_EXTRA_BYTES, /* extra */
114 IDC_ARROW, /* cursor */
119 /***********************************************************************
122 * Set the icon for an SS_ICON control.
124 static HICON STATIC_SetIcon( HWND hwnd, HICON hicon, DWORD style )
127 CURSORICONINFO *info = hicon?(CURSORICONINFO *) GlobalLock16(HICON_16(hicon)):NULL;
129 if ((style & SS_TYPEMASK) != SS_ICON) return 0;
130 if (hicon && !info) {
131 ERR("huh? hicon!=0, but info=0???\n");
134 prevIcon = (HICON)SetWindowLongPtrW( hwnd, HICON_GWL_OFFSET, (LONG_PTR)hicon );
135 if (hicon && !(style & SS_CENTERIMAGE))
137 SetWindowPos( hwnd, 0, 0, 0, info->nWidth, info->nHeight,
138 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
139 GlobalUnlock16(HICON_16(hicon));
144 /***********************************************************************
147 * Set the bitmap for an SS_BITMAP control.
149 static HBITMAP STATIC_SetBitmap( HWND hwnd, HBITMAP hBitmap, DWORD style )
153 if ((style & SS_TYPEMASK) != SS_BITMAP) return 0;
154 if (hBitmap && GetObjectType(hBitmap) != OBJ_BITMAP) {
155 ERR("huh? hBitmap!=0, but not bitmap\n");
158 hOldBitmap = (HBITMAP)SetWindowLongPtrW( hwnd, HICON_GWL_OFFSET, (LONG_PTR)hBitmap );
159 if (hBitmap && !(style & SS_CENTERIMAGE))
162 GetObjectW(hBitmap, sizeof(bm), &bm);
163 SetWindowPos( hwnd, 0, 0, 0, bm.bmWidth, bm.bmHeight,
164 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
169 /***********************************************************************
172 * Load the icon for an SS_ICON control.
174 static HICON STATIC_LoadIconA( HWND hwnd, LPCSTR name )
176 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
177 HICON hicon = LoadIconA( hInstance, name );
178 if (!hicon) hicon = LoadIconA( 0, name );
182 /***********************************************************************
185 * Load the icon for an SS_ICON control.
187 static HICON STATIC_LoadIconW( HWND hwnd, LPCWSTR name )
189 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
190 HICON hicon = LoadIconW( hInstance, name );
191 if (!hicon) hicon = LoadIconW( 0, name );
195 /***********************************************************************
198 * Load the bitmap for an SS_BITMAP control.
200 static HBITMAP STATIC_LoadBitmapA( HWND hwnd, LPCSTR name )
202 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
203 HBITMAP hbitmap = LoadBitmapA( hInstance, name );
204 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
205 hbitmap = LoadBitmapA( 0, name );
209 /***********************************************************************
212 * Load the bitmap for an SS_BITMAP control.
214 static HBITMAP STATIC_LoadBitmapW( HWND hwnd, LPCWSTR name )
216 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
217 HBITMAP hbitmap = LoadBitmapW( hInstance, name );
218 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
219 hbitmap = LoadBitmapW( 0, name );
223 /***********************************************************************
226 * Try to immediately paint the control.
228 static VOID STATIC_TryPaintFcn(HWND hwnd, LONG full_style)
230 LONG style = full_style & SS_TYPEMASK;
233 GetClientRect( hwnd, &rc );
234 if (!IsRectEmpty(&rc) && IsWindowVisible(hwnd) && staticPaintFunc[style])
238 (staticPaintFunc[style])( hwnd, hdc, full_style );
239 ReleaseDC( hwnd, hdc );
243 /***********************************************************************
244 * StaticWndProc_common
246 static LRESULT StaticWndProc_common( HWND hwnd, UINT uMsg, WPARAM wParam,
247 LPARAM lParam, BOOL unicode )
250 LONG full_style = GetWindowLongW( hwnd, GWL_STYLE );
251 LONG style = full_style & SS_TYPEMASK;
256 if (style < 0L || style > SS_TYPEMASK)
258 ERR("Unknown style 0x%02lx\n", style );
261 /* initialise colours */
262 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
263 color_background = GetSysColor(COLOR_BACKGROUND);
264 color_window = GetSysColor(COLOR_WINDOW);
268 if (style == SS_ICON) {
271 * DestroyIcon32( STATIC_SetIcon( wndPtr, 0 ) );
273 * We don't want to do this yet because DestroyIcon32 is broken. If the icon
274 * had already been loaded by the application the last thing we want to do is
275 * GlobalFree16 the handle.
279 else return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
280 DefWindowProcA(hwnd, uMsg, wParam, lParam);
285 HDC hdc = wParam ? (HDC)wParam : BeginPaint(hwnd, &ps);
286 if (staticPaintFunc[style])
287 (staticPaintFunc[style])( hwnd, hdc, full_style );
288 if (!wParam) EndPaint(hwnd, &ps);
293 InvalidateRect(hwnd, NULL, TRUE);
296 case WM_SYSCOLORCHANGE:
297 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
298 color_background = GetSysColor(COLOR_BACKGROUND);
299 color_window = GetSysColor(COLOR_WINDOW);
300 InvalidateRect(hwnd, NULL, TRUE);
304 if (full_style & SS_SUNKEN)
305 SetWindowLongW( hwnd, GWL_EXSTYLE,
306 GetWindowLongW( hwnd, GWL_EXSTYLE ) | WS_EX_STATICEDGE );
309 lParam = (LPARAM)(((LPCREATESTRUCTW)lParam)->lpszName);
311 lParam = (LPARAM)(((LPCREATESTRUCTA)lParam)->lpszName);
319 hIcon = STATIC_LoadIconW(hwnd, (LPCWSTR)lParam);
321 hIcon = STATIC_LoadIconA(hwnd, (LPCSTR)lParam);
322 /* FIXME : should we also return the previous hIcon here ??? */
323 STATIC_SetIcon(hwnd, hIcon, full_style);
330 hBitmap = STATIC_LoadBitmapW(hwnd, (LPCWSTR)lParam);
332 hBitmap = STATIC_LoadBitmapA(hwnd, (LPCSTR)lParam);
333 STATIC_SetBitmap(hwnd, hBitmap, full_style);
340 case SS_LEFTNOWORDWRAP:
345 lResult = DefWindowProcW( hwnd, WM_SETTEXT, wParam, lParam );
347 lResult = DefWindowProcA( hwnd, WM_SETTEXT, wParam, lParam );
349 if (uMsg == WM_SETTEXT)
350 STATIC_TryPaintFcn( hwnd, full_style );
357 lResult = DefWindowProcW( hwnd, WM_SETTEXT, wParam, lParam );
359 lResult = DefWindowProcA( hwnd, WM_SETTEXT, wParam, lParam );
361 if(uMsg == WM_SETTEXT)
362 InvalidateRect(hwnd, NULL, TRUE);
364 return 1; /* success. FIXME: check text length */
367 if ((style == SS_ICON) || (style == SS_BITMAP)) return 0;
368 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, wParam );
370 InvalidateRect( hwnd, NULL, TRUE );
374 return GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
377 if (full_style & SS_NOTIFY)
380 return HTTRANSPARENT;
386 case WM_NCLBUTTONDOWN:
387 if (full_style & SS_NOTIFY)
388 SendMessageW( GetParent(hwnd), WM_COMMAND,
389 MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_CLICKED ), (LPARAM)hwnd);
392 case WM_LBUTTONDBLCLK:
393 case WM_NCLBUTTONDBLCLK:
394 if (full_style & SS_NOTIFY)
395 SendMessageW( GetParent(hwnd), WM_COMMAND,
396 MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_DBLCLK ), (LPARAM)hwnd);
402 return GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET );
407 lResult = (LRESULT)STATIC_SetBitmap( hwnd, (HBITMAP)lParam, full_style );
410 FIXME("STM_SETIMAGE: Unhandled type IMAGE_CURSOR\n");
412 case IMAGE_ENHMETAFILE:
413 FIXME("STM_SETIMAGE: Unhandled type IMAGE_ENHMETAFILE\n");
416 lResult = (LRESULT)STATIC_SetIcon( hwnd, (HICON)lParam, full_style );
419 FIXME("STM_SETIMAGE: Unhandled type %x\n", wParam);
422 InvalidateRect( hwnd, NULL, TRUE );
427 lResult = (LRESULT)STATIC_SetIcon( hwnd, (HICON)wParam, full_style );
428 InvalidateRect( hwnd, NULL, TRUE );
432 return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
433 DefWindowProcA(hwnd, uMsg, wParam, lParam);
438 /***********************************************************************
441 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
443 if (!IsWindow( hWnd )) return 0;
444 return StaticWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
447 /***********************************************************************
450 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
452 if (!IsWindow( hWnd )) return 0;
453 return StaticWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
456 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style )
459 UINT id = (UINT)GetWindowLongPtrW( hwnd, GWLP_ID );
461 dis.CtlType = ODT_STATIC;
464 dis.itemAction = ODA_DRAWENTIRE;
469 GetClientRect( hwnd, &dis.rcItem );
471 SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, (WPARAM)hdc, (LPARAM)hwnd );
472 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
475 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style )
484 GetClientRect( hwnd, &rc);
486 switch (style & SS_TYPEMASK)
489 wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK;
493 wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK;
497 wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK;
501 wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
504 case SS_LEFTNOWORDWRAP:
505 wFormat = DT_LEFT | DT_EXPANDTABS | DT_VCENTER;
512 if (style & SS_NOPREFIX)
513 wFormat |= DT_NOPREFIX;
514 if (style & SS_CENTERIMAGE)
515 wFormat |= DT_VCENTER;
517 if ((hFont = (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET ))) SelectObject( hdc, hFont );
519 if ((style & SS_NOPREFIX) || ((style & SS_TYPEMASK) != SS_SIMPLE))
521 hBrush = (HBRUSH)SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC,
522 (WPARAM)hdc, (LPARAM)hwnd );
523 if (!hBrush) /* did the app forget to call defwindowproc ? */
524 hBrush = (HBRUSH)DefWindowProcW(GetParent(hwnd), WM_CTLCOLORSTATIC,
525 (WPARAM)hdc, (LPARAM)hwnd);
526 FillRect( hdc, &rc, hBrush );
528 if (!IsWindowEnabled(hwnd)) SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
530 if (!(len = SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 ))) return;
531 if (!(text = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return;
532 SendMessageW( hwnd, WM_GETTEXT, len + 1, (LPARAM)text );
533 DrawTextW( hdc, text, -1, &rc, wFormat );
534 HeapFree( GetProcessHeap(), 0, text );
537 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style )
542 GetClientRect( hwnd, &rc);
544 switch (style & SS_TYPEMASK)
547 hBrush = CreateSolidBrush(color_windowframe);
548 FillRect( hdc, &rc, hBrush );
551 hBrush = CreateSolidBrush(color_background);
552 FillRect( hdc, &rc, hBrush );
555 hBrush = CreateSolidBrush(color_window);
556 FillRect( hdc, &rc, hBrush );
559 hBrush = CreateSolidBrush(color_windowframe);
560 FrameRect( hdc, &rc, hBrush );
563 hBrush = CreateSolidBrush(color_background);
564 FrameRect( hdc, &rc, hBrush );
567 hBrush = CreateSolidBrush(color_window);
568 FrameRect( hdc, &rc, hBrush );
573 DeleteObject( hBrush );
577 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style )
584 GetClientRect( hwnd, &rc );
585 hbrush = (HBRUSH)SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC,
586 (WPARAM)hdc, (LPARAM)hwnd );
587 FillRect( hdc, &rc, hbrush );
588 hIcon = (HICON)GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET );
589 if (style & SS_CENTERIMAGE)
591 CURSORICONINFO *info = hIcon ? (CURSORICONINFO *)GlobalLock16(HICON_16(hIcon)) : NULL;
592 x = (rc.right - rc.left)/2 - (info ? info->nWidth/2 : 0);
593 y = (rc.bottom - rc.top)/2 - (info ? info->nHeight/2 : 0);
601 DrawIcon( hdc, x, y, hIcon );
604 static void STATIC_PaintBitmapfn(HWND hwnd, HDC hdc, DWORD style )
607 HBITMAP hBitmap, oldbitmap;
609 /* message is still sent, even if the returned brush is not used */
610 SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC,
611 (WPARAM)hdc, (LPARAM)hwnd );
613 if ((hBitmap = (HBITMAP)GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET )))
618 if(GetObjectType(hBitmap) != OBJ_BITMAP) return;
619 if (!(hMemDC = CreateCompatibleDC( hdc ))) return;
620 GetObjectW(hBitmap, sizeof(bm), &bm);
621 oldbitmap = SelectObject(hMemDC, hBitmap);
622 if (style & SS_CENTERIMAGE)
625 GetClientRect(hwnd, &rcClient);
626 x = (rcClient.right - rcClient.left)/2 - bm.bmWidth/2;
627 y = (rcClient.bottom - rcClient.top)/2 - bm.bmHeight/2;
634 BitBlt(hdc, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0,
636 SelectObject(hMemDC, oldbitmap);
642 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style )
646 GetClientRect( hwnd, &rc );
647 switch (style & SS_TYPEMASK)
650 DrawEdge(hdc,&rc,EDGE_ETCHED,BF_TOP|BF_BOTTOM);
653 DrawEdge(hdc,&rc,EDGE_ETCHED,BF_LEFT|BF_RIGHT);
656 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_RECT);