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