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 "cursoricon.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(static);
64 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style );
65 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style );
66 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style );
67 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style );
68 static void STATIC_PaintBitmapfn( HWND hwnd, HDC hdc, DWORD style );
69 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style );
70 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
71 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
73 static COLORREF color_windowframe, color_background, color_window;
75 /* offsets for GetWindowLong for static private information */
76 #define HFONT_GWL_OFFSET 0
77 #define HICON_GWL_OFFSET (sizeof(HFONT))
78 #define STATIC_EXTRA_BYTES (HICON_GWL_OFFSET + sizeof(HICON))
80 typedef void (*pfPaint)( HWND hwnd, HDC hdc, DWORD style );
82 static pfPaint staticPaintFunc[SS_TYPEMASK+1] =
84 STATIC_PaintTextfn, /* SS_LEFT */
85 STATIC_PaintTextfn, /* SS_CENTER */
86 STATIC_PaintTextfn, /* SS_RIGHT */
87 STATIC_PaintIconfn, /* SS_ICON */
88 STATIC_PaintRectfn, /* SS_BLACKRECT */
89 STATIC_PaintRectfn, /* SS_GRAYRECT */
90 STATIC_PaintRectfn, /* SS_WHITERECT */
91 STATIC_PaintRectfn, /* SS_BLACKFRAME */
92 STATIC_PaintRectfn, /* SS_GRAYFRAME */
93 STATIC_PaintRectfn, /* SS_WHITEFRAME */
94 NULL, /* SS_USERITEM */
95 STATIC_PaintTextfn, /* SS_SIMPLE */
96 STATIC_PaintTextfn, /* SS_LEFTNOWORDWRAP */
97 STATIC_PaintOwnerDrawfn, /* SS_OWNERDRAW */
98 STATIC_PaintBitmapfn, /* SS_BITMAP */
99 NULL, /* SS_ENHMETAFILE */
100 STATIC_PaintEtchedfn, /* SS_ETCHEDHORZ */
101 STATIC_PaintEtchedfn, /* SS_ETCHEDVERT */
102 STATIC_PaintEtchedfn, /* SS_ETCHEDFRAME */
106 /*********************************************************************
107 * static class descriptor
109 const struct builtin_class_descr STATIC_builtin_class =
112 CS_DBLCLKS | CS_PARENTDC, /* style */
113 StaticWndProcA, /* procA */
114 StaticWndProcW, /* procW */
115 STATIC_EXTRA_BYTES, /* extra */
116 IDC_ARROW, /* cursor */
121 /***********************************************************************
124 * Set the icon for an SS_ICON control.
126 static HICON STATIC_SetIcon( HWND hwnd, HICON hicon, DWORD style )
129 CURSORICONINFO *info = hicon?(CURSORICONINFO *) GlobalLock16(HICON_16(hicon)):NULL;
131 if ((style & SS_TYPEMASK) != SS_ICON) return 0;
132 if (hicon && !info) {
133 ERR("huh? hicon!=0, but info=0???\n");
136 prevIcon = (HICON)SetWindowLongPtrW( hwnd, HICON_GWL_OFFSET, (LONG_PTR)hicon );
139 SetWindowPos( hwnd, 0, 0, 0, info->nWidth, info->nHeight,
140 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
141 GlobalUnlock16(HICON_16(hicon));
146 /***********************************************************************
149 * Set the bitmap for an SS_BITMAP control.
151 static HBITMAP STATIC_SetBitmap( HWND hwnd, HBITMAP hBitmap, DWORD style )
155 if ((style & SS_TYPEMASK) != SS_BITMAP) return 0;
156 if (hBitmap && GetObjectType(hBitmap) != OBJ_BITMAP) {
157 ERR("huh? hBitmap!=0, but not bitmap\n");
160 hOldBitmap = (HBITMAP)SetWindowLongPtrW( hwnd, HICON_GWL_OFFSET, (LONG_PTR)hBitmap );
164 GetObjectW(hBitmap, sizeof(bm), &bm);
165 SetWindowPos( hwnd, 0, 0, 0, bm.bmWidth, bm.bmHeight,
166 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
171 /***********************************************************************
174 * Load the icon for an SS_ICON control.
176 static HICON STATIC_LoadIconA( HWND hwnd, LPCSTR name )
178 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
179 HICON hicon = LoadIconA( hInstance, name );
180 if (!hicon) hicon = LoadIconA( 0, name );
184 /***********************************************************************
187 * Load the icon for an SS_ICON control.
189 static HICON STATIC_LoadIconW( HWND hwnd, LPCWSTR name )
191 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
192 HICON hicon = LoadIconW( hInstance, name );
193 if (!hicon) hicon = LoadIconW( 0, name );
197 /***********************************************************************
200 * Load the bitmap for an SS_BITMAP control.
202 static HBITMAP STATIC_LoadBitmapA( HWND hwnd, LPCSTR name )
204 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
205 HBITMAP hbitmap = LoadBitmapA( hInstance, name );
206 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
207 hbitmap = LoadBitmapA( 0, name );
211 /***********************************************************************
214 * Load the bitmap for an SS_BITMAP control.
216 static HBITMAP STATIC_LoadBitmapW( HWND hwnd, LPCWSTR name )
218 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
219 HBITMAP hbitmap = LoadBitmapW( hInstance, name );
220 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
221 hbitmap = LoadBitmapW( 0, name );
225 /***********************************************************************
228 * Try to immediately paint the control.
230 static VOID STATIC_TryPaintFcn(HWND hwnd, LONG full_style)
232 LONG style = full_style & SS_TYPEMASK;
235 GetClientRect( hwnd, &rc );
236 if (!IsRectEmpty(&rc) && IsWindowVisible(hwnd) && staticPaintFunc[style])
240 (staticPaintFunc[style])( hwnd, hdc, full_style );
241 ReleaseDC( hwnd, hdc );
245 /***********************************************************************
246 * StaticWndProc_common
248 static LRESULT StaticWndProc_common( HWND hwnd, UINT uMsg, WPARAM wParam,
249 LPARAM lParam, BOOL unicode )
252 LONG full_style = GetWindowLongW( hwnd, GWL_STYLE );
253 LONG style = full_style & SS_TYPEMASK;
258 if (style < 0L || style > SS_TYPEMASK)
260 ERR("Unknown style 0x%02lx\n", style );
263 /* initialise colours */
264 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
265 color_background = GetSysColor(COLOR_BACKGROUND);
266 color_window = GetSysColor(COLOR_WINDOW);
270 if (style == SS_ICON) {
273 * DestroyIcon32( STATIC_SetIcon( wndPtr, 0 ) );
275 * We don't want to do this yet because DestroyIcon32 is broken. If the icon
276 * had already been loaded by the application the last thing we want to do is
277 * GlobalFree16 the handle.
281 else return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
282 DefWindowProcA(hwnd, uMsg, wParam, lParam);
287 HDC hdc = wParam ? (HDC)wParam : BeginPaint(hwnd, &ps);
288 if (staticPaintFunc[style])
289 (staticPaintFunc[style])( hwnd, hdc, full_style );
290 if (!wParam) EndPaint(hwnd, &ps);
295 InvalidateRect(hwnd, NULL, TRUE);
298 case WM_SYSCOLORCHANGE:
299 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
300 color_background = GetSysColor(COLOR_BACKGROUND);
301 color_window = GetSysColor(COLOR_WINDOW);
302 InvalidateRect(hwnd, NULL, TRUE);
306 if (full_style & SS_SUNKEN)
307 SetWindowLongW( hwnd, GWL_EXSTYLE,
308 GetWindowLongW( hwnd, GWL_EXSTYLE ) | WS_EX_STATICEDGE );
311 lParam = (LPARAM)(((LPCREATESTRUCTW)lParam)->lpszName);
313 lParam = (LPARAM)(((LPCREATESTRUCTA)lParam)->lpszName);
321 hIcon = STATIC_LoadIconW(hwnd, (LPCWSTR)lParam);
323 hIcon = STATIC_LoadIconA(hwnd, (LPCSTR)lParam);
324 /* FIXME : should we also return the previous hIcon here ??? */
325 STATIC_SetIcon(hwnd, hIcon, style);
332 hBitmap = STATIC_LoadBitmapW(hwnd, (LPCWSTR)lParam);
334 hBitmap = STATIC_LoadBitmapA(hwnd, (LPCSTR)lParam);
335 STATIC_SetBitmap(hwnd, hBitmap, style);
342 case SS_LEFTNOWORDWRAP:
347 lResult = DefWindowProcW( hwnd, WM_SETTEXT, wParam, lParam );
349 lResult = DefWindowProcA( hwnd, WM_SETTEXT, wParam, lParam );
351 if (uMsg == WM_SETTEXT)
352 STATIC_TryPaintFcn( hwnd, full_style );
359 lResult = DefWindowProcW( hwnd, WM_SETTEXT, wParam, lParam );
361 lResult = DefWindowProcA( hwnd, WM_SETTEXT, wParam, lParam );
363 if(uMsg == WM_SETTEXT)
364 InvalidateRect(hwnd, NULL, TRUE);
366 return 1; /* success. FIXME: check text length */
369 if ((style == SS_ICON) || (style == SS_BITMAP)) return 0;
370 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, wParam );
372 InvalidateRect( hwnd, NULL, TRUE );
376 return GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
379 if (full_style & SS_NOTIFY)
382 return HTTRANSPARENT;
388 case WM_NCLBUTTONDOWN:
389 if (full_style & SS_NOTIFY)
390 SendMessageW( GetParent(hwnd), WM_COMMAND,
391 MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_CLICKED ), (LPARAM)hwnd);
394 case WM_LBUTTONDBLCLK:
395 case WM_NCLBUTTONDBLCLK:
396 if (full_style & SS_NOTIFY)
397 SendMessageW( GetParent(hwnd), WM_COMMAND,
398 MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_DBLCLK ), (LPARAM)hwnd);
404 return GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET );
409 lResult = (LRESULT)STATIC_SetBitmap( hwnd, (HBITMAP)lParam, style );
412 FIXME("STM_SETIMAGE: Unhandled type IMAGE_CURSOR\n");
414 case IMAGE_ENHMETAFILE:
415 FIXME("STM_SETIMAGE: Unhandled type IMAGE_ENHMETAFILE\n");
418 lResult = (LRESULT)STATIC_SetIcon( hwnd, (HICON)lParam, style );
421 FIXME("STM_SETIMAGE: Unhandled type %x\n", wParam);
424 InvalidateRect( hwnd, NULL, TRUE );
429 lResult = (LRESULT)STATIC_SetIcon( hwnd, (HICON)wParam, style );
430 InvalidateRect( hwnd, NULL, TRUE );
434 return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
435 DefWindowProcA(hwnd, uMsg, wParam, lParam);
440 /***********************************************************************
443 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
445 if (!IsWindow( hWnd )) return 0;
446 return StaticWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
449 /***********************************************************************
452 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
454 if (!IsWindow( hWnd )) return 0;
455 return StaticWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
458 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style )
461 UINT id = (UINT)GetWindowLongPtrW( hwnd, GWLP_ID );
463 dis.CtlType = ODT_STATIC;
466 dis.itemAction = ODA_DRAWENTIRE;
471 GetClientRect( hwnd, &dis.rcItem );
473 SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, (WPARAM)hdc, (LPARAM)hwnd );
474 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
477 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style )
486 GetClientRect( hwnd, &rc);
488 switch (style & SS_TYPEMASK)
491 wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK;
495 wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK;
499 wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK;
503 wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
506 case SS_LEFTNOWORDWRAP:
507 wFormat = DT_LEFT | DT_EXPANDTABS | DT_VCENTER;
514 if (style & SS_NOPREFIX)
515 wFormat |= DT_NOPREFIX;
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 )
583 GetClientRect( hwnd, &rc );
584 hbrush = (HBRUSH)SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC,
585 (WPARAM)hdc, (LPARAM)hwnd );
586 FillRect( hdc, &rc, hbrush );
587 if ((hIcon = (HICON)GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET )))
588 DrawIcon( hdc, rc.left, rc.top, hIcon );
591 static void STATIC_PaintBitmapfn(HWND hwnd, HDC hdc, DWORD style )
594 HBITMAP hBitmap, oldbitmap;
596 /* message is still sent, even if the returned brush is not used */
597 SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC,
598 (WPARAM)hdc, (LPARAM)hwnd );
600 if ((hBitmap = (HBITMAP)GetWindowLongPtrW( hwnd, HICON_GWL_OFFSET )))
604 if(GetObjectType(hBitmap) != OBJ_BITMAP) return;
605 if (!(hMemDC = CreateCompatibleDC( hdc ))) return;
606 GetObjectW(hBitmap, sizeof(bm), &bm);
607 oldbitmap = SelectObject(hMemDC, hBitmap);
608 BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0,
610 SelectObject(hMemDC, oldbitmap);
616 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style )
620 GetClientRect( hwnd, &rc );
621 switch (style & SS_TYPEMASK)
624 DrawEdge(hdc,&rc,EDGE_ETCHED,BF_TOP|BF_BOTTOM);
627 DrawEdge(hdc,&rc,EDGE_ETCHED,BF_LEFT|BF_RIGHT);
630 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_RECT);