gdi32: Fix leak in GdiDeleteSpoolFileHandle.
[wine.git] / dlls / comctl32 / static.c
blob29a0498ba09e9c249dea2b9f64c46a258a158942
1 /*
2 * Static control
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Notes:
21 * - Controls with SS_SIMPLE but without SS_NOPREFIX:
22 * The text should not be changed. Windows doesn't clear the
23 * client rectangle, so the new text must be larger than the old one.
24 * - The SS_RIGHTJUST style is currently not implemented by Windows
25 * (or it does something different than documented).
27 * TODO:
28 * - Animated cursors
31 #include <stdarg.h>
32 #include <stdlib.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "commctrl.h"
39 #include "uxtheme.h"
41 #include "wine/debug.h"
43 #include "comctl32.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(static);
47 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style );
48 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style );
49 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style );
50 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style );
51 static void STATIC_PaintBitmapfn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style );
52 static void STATIC_PaintEnhMetafn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style );
53 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style );
55 struct static_extra_info
57 HFONT hfont;
58 union
60 HICON hicon;
61 HBITMAP hbitmap;
62 HENHMETAFILE hemf;
63 } image;
64 BOOL image_has_alpha;
67 typedef void (*pfPaint)( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style );
69 static const pfPaint staticPaintFunc[SS_TYPEMASK+1] =
71 STATIC_PaintTextfn, /* SS_LEFT */
72 STATIC_PaintTextfn, /* SS_CENTER */
73 STATIC_PaintTextfn, /* SS_RIGHT */
74 STATIC_PaintIconfn, /* SS_ICON */
75 STATIC_PaintRectfn, /* SS_BLACKRECT */
76 STATIC_PaintRectfn, /* SS_GRAYRECT */
77 STATIC_PaintRectfn, /* SS_WHITERECT */
78 STATIC_PaintRectfn, /* SS_BLACKFRAME */
79 STATIC_PaintRectfn, /* SS_GRAYFRAME */
80 STATIC_PaintRectfn, /* SS_WHITEFRAME */
81 NULL, /* SS_USERITEM */
82 STATIC_PaintTextfn, /* SS_SIMPLE */
83 STATIC_PaintTextfn, /* SS_LEFTNOWORDWRAP */
84 STATIC_PaintOwnerDrawfn, /* SS_OWNERDRAW */
85 STATIC_PaintBitmapfn, /* SS_BITMAP */
86 STATIC_PaintEnhMetafn, /* SS_ENHMETAFILE */
87 NULL, /* SS_ETCHEDHORZ */
88 NULL, /* SS_ETCHEDVERT */
89 STATIC_PaintEtchedfn, /* SS_ETCHEDFRAME */
92 static struct static_extra_info *get_extra_ptr( HWND hwnd, BOOL force )
94 struct static_extra_info *extra = (struct static_extra_info *)GetWindowLongPtrW( hwnd, 0 );
95 if (!extra && force)
97 extra = Alloc( sizeof(*extra) );
98 if (extra)
99 SetWindowLongPtrW( hwnd, 0, (ULONG_PTR)extra );
101 return extra;
104 static BOOL get_icon_size( HICON handle, SIZE *size )
106 ICONINFO info;
107 BITMAP bmp;
108 int ret;
110 if (!GetIconInfo(handle, &info))
111 return FALSE;
113 ret = GetObjectW(info.hbmColor, sizeof(bmp), &bmp);
114 if (ret)
116 size->cx = bmp.bmWidth;
117 size->cy = bmp.bmHeight;
120 DeleteObject(info.hbmMask);
121 DeleteObject(info.hbmColor);
123 return !!ret;
126 /***********************************************************************
127 * STATIC_SetIcon
129 * Set the icon for an SS_ICON control.
131 static HICON STATIC_SetIcon( HWND hwnd, HICON hicon, DWORD style )
133 HICON prevIcon;
134 SIZE size;
135 struct static_extra_info *extra;
137 if (hicon && !get_icon_size( hicon, &size ))
139 WARN("hicon != 0, but invalid\n");
140 return 0;
143 extra = get_extra_ptr( hwnd, TRUE );
144 if (!extra) return 0;
146 prevIcon = extra->image.hicon;
147 extra->image.hicon = hicon;
148 if (hicon && !(style & SS_CENTERIMAGE) && !(style & SS_REALSIZECONTROL))
150 /* Windows currently doesn't implement SS_RIGHTJUST */
152 if ((style & SS_RIGHTJUST) != 0)
154 RECT wr;
155 GetWindowRect(hwnd, &wr);
156 SetWindowPos( hwnd, 0, wr.right - info->nWidth, wr.bottom - info->nHeight,
157 info->nWidth, info->nHeight, SWP_NOACTIVATE | SWP_NOZORDER );
159 else */
161 SetWindowPos( hwnd, 0, 0, 0, size.cx, size.cy, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
164 return prevIcon;
167 static HBITMAP create_alpha_bitmap( HBITMAP hbitmap )
169 BITMAP bm;
170 HBITMAP alpha;
171 BITMAPINFO info;
172 HDC hdc;
173 void *bits;
174 DWORD i;
175 BYTE *ptr;
176 BOOL has_alpha = FALSE;
178 GetObjectW( hbitmap, sizeof(bm), &bm );
179 if (bm.bmBitsPixel != 32) return 0;
181 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
183 info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
184 info.bmiHeader.biWidth = bm.bmWidth;
185 info.bmiHeader.biHeight = -bm.bmHeight;
186 info.bmiHeader.biPlanes = 1;
187 info.bmiHeader.biBitCount = 32;
188 info.bmiHeader.biCompression = BI_RGB;
189 info.bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
190 info.bmiHeader.biXPelsPerMeter = 0;
191 info.bmiHeader.biYPelsPerMeter = 0;
192 info.bmiHeader.biClrUsed = 0;
193 info.bmiHeader.biClrImportant = 0;
194 if ((alpha = CreateDIBSection( hdc, &info, DIB_RGB_COLORS, &bits, NULL, 0 )))
196 GetDIBits( hdc, hbitmap, 0, bm.bmHeight, bits, &info, DIB_RGB_COLORS );
198 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
199 if ((has_alpha = (ptr[3] != 0))) break;
201 if (!has_alpha)
203 DeleteObject( alpha );
204 alpha = 0;
206 else
208 /* pre-multiply by alpha */
209 for (i = 0, ptr = bits; i < bm.bmWidth * bm.bmHeight; i++, ptr += 4)
211 unsigned int alpha = ptr[3];
212 ptr[0] = (ptr[0] * alpha + 127) / 255;
213 ptr[1] = (ptr[1] * alpha + 127) / 255;
214 ptr[2] = (ptr[2] * alpha + 127) / 255;
219 DeleteDC( hdc );
221 return alpha;
224 /***********************************************************************
225 * STATIC_SetBitmap
227 * Set the bitmap for an SS_BITMAP control.
229 static HBITMAP STATIC_SetBitmap( HWND hwnd, HBITMAP hBitmap, DWORD style )
231 HBITMAP hOldBitmap, alpha;
232 struct static_extra_info *extra;
234 if (hBitmap && GetObjectType(hBitmap) != OBJ_BITMAP)
236 WARN("hBitmap != 0, but it's not a bitmap\n");
237 return 0;
240 extra = get_extra_ptr( hwnd, TRUE );
241 if (!extra) return 0;
243 hOldBitmap = extra->image.hbitmap;
244 extra->image.hbitmap = hBitmap;
245 extra->image_has_alpha = FALSE;
247 if (hBitmap)
249 alpha = create_alpha_bitmap( hBitmap );
250 if (alpha)
252 extra->image.hbitmap = alpha;
253 extra->image_has_alpha = TRUE;
257 if (hBitmap && !(style & SS_CENTERIMAGE) && !(style & SS_REALSIZECONTROL))
259 BITMAP bm;
260 GetObjectW(hBitmap, sizeof(bm), &bm);
262 /* Windows currently doesn't implement SS_RIGHTJUST */
264 if ((style & SS_RIGHTJUST) != 0)
266 RECT wr;
267 GetWindowRect(hwnd, &wr);
268 SetWindowPos( hwnd, 0, wr.right - bm.bmWidth, wr.bottom - bm.bmHeight,
269 bm.bmWidth, bm.bmHeight, SWP_NOACTIVATE | SWP_NOZORDER );
271 else */
273 SetWindowPos( hwnd, 0, 0, 0, bm.bmWidth, bm.bmHeight,
274 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
277 return hOldBitmap;
280 /***********************************************************************
281 * STATIC_SetEnhMetaFile
283 * Set the enhanced metafile for an SS_ENHMETAFILE control.
285 static HENHMETAFILE STATIC_SetEnhMetaFile( HWND hwnd, HENHMETAFILE hEnhMetaFile, DWORD style )
287 HENHMETAFILE old_hemf;
288 struct static_extra_info *extra;
290 if (hEnhMetaFile && GetObjectType(hEnhMetaFile) != OBJ_ENHMETAFILE)
292 WARN("hEnhMetaFile != 0, but it's not an enhanced metafile\n");
293 return 0;
296 extra = get_extra_ptr( hwnd, TRUE );
297 if (!extra) return 0;
299 old_hemf = extra->image.hemf;
300 extra->image.hemf = hEnhMetaFile;
302 return old_hemf;
305 /***********************************************************************
306 * STATIC_GetImage
308 * Gets the bitmap for an SS_BITMAP control, the icon/cursor for an
309 * SS_ICON control or the enhanced metafile for an SS_ENHMETAFILE control.
311 static HANDLE STATIC_GetImage( HWND hwnd, WPARAM wParam, DWORD style )
313 struct static_extra_info *extra;
315 switch (style & SS_TYPEMASK)
317 case SS_ICON:
318 if ((wParam != IMAGE_ICON) &&
319 (wParam != IMAGE_CURSOR)) return NULL;
320 break;
321 case SS_BITMAP:
322 if (wParam != IMAGE_BITMAP) return NULL;
323 break;
324 case SS_ENHMETAFILE:
325 if (wParam != IMAGE_ENHMETAFILE) return NULL;
326 break;
327 default:
328 return NULL;
331 extra = get_extra_ptr( hwnd, FALSE );
332 return extra ? extra->image.hbitmap : 0;
335 static void STATIC_SetFont( HWND hwnd, HFONT hfont )
337 struct static_extra_info *extra = get_extra_ptr( hwnd, TRUE );
338 if (extra)
339 extra->hfont = hfont;
342 static HFONT STATIC_GetFont( HWND hwnd )
344 struct static_extra_info *extra = get_extra_ptr( hwnd, FALSE );
345 return extra ? extra->hfont : 0;
348 /***********************************************************************
349 * STATIC_LoadIconW
351 * Load the icon for an SS_ICON control.
353 static HICON STATIC_LoadIconW( HINSTANCE hInstance, LPCWSTR name, DWORD style )
355 HICON hicon = 0;
357 if (hInstance && ((ULONG_PTR)hInstance >> 16))
359 if ((style & SS_REALSIZEIMAGE) != 0)
360 hicon = LoadImageW(hInstance, name, IMAGE_ICON, 0, 0, LR_SHARED);
361 else
363 hicon = LoadIconW( hInstance, name );
364 if (!hicon) hicon = LoadCursorW( hInstance, name );
367 if (!hicon) hicon = LoadIconW( 0, name );
368 /* Windows doesn't try to load a standard cursor,
369 probably because most IDs for standard cursors conflict
370 with the IDs for standard icons anyway */
371 return hicon;
374 static HBRUSH STATIC_SendWmCtlColorStatic(HWND hwnd, HDC hdc)
376 HBRUSH hBrush;
377 HWND parent = GetParent(hwnd);
379 if (!parent) parent = hwnd;
380 hBrush = (HBRUSH) SendMessageW( parent, WM_CTLCOLORSTATIC, (WPARAM)hdc, (LPARAM)hwnd );
381 if (!hBrush) /* did the app forget to call DefWindowProc ? */
383 /* FIXME: DefWindowProc should return different colors if a
384 manifest is present */
385 hBrush = (HBRUSH)DefWindowProcW( parent, WM_CTLCOLORSTATIC, (WPARAM)hdc, (LPARAM)hwnd);
387 return hBrush;
390 /***********************************************************************
391 * STATIC_TryPaintFcn
393 * Try to immediately paint the control.
395 static VOID STATIC_TryPaintFcn(HWND hwnd, LONG full_style)
397 if (IsWindowVisible(hwnd))
399 RECT rc;
400 HDC hdc;
401 HRGN hrgn;
402 HBRUSH hbrush;
403 LONG style = full_style & SS_TYPEMASK;
405 GetClientRect( hwnd, &rc );
406 hdc = GetDC( hwnd );
407 hrgn = set_control_clipping( hdc, &rc );
408 hbrush = STATIC_SendWmCtlColorStatic( hwnd, hdc );
409 if (staticPaintFunc[style])
410 (staticPaintFunc[style])( hwnd, hdc, hbrush, full_style );
411 SelectClipRgn( hdc, hrgn );
412 if (hrgn) DeleteObject( hrgn );
413 ReleaseDC( hwnd, hdc );
417 /***********************************************************************
418 * hasTextStyle
420 * Tests if the control displays text.
422 static BOOL hasTextStyle( DWORD style )
424 switch (style & SS_TYPEMASK)
426 case SS_SIMPLE:
427 case SS_LEFT:
428 case SS_LEFTNOWORDWRAP:
429 case SS_CENTER:
430 case SS_RIGHT:
431 case SS_OWNERDRAW:
432 return TRUE;
435 return FALSE;
438 static LRESULT CALLBACK STATIC_WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
440 const WCHAR *window_name;
441 LRESULT lResult = 0;
442 LONG full_style = GetWindowLongW( hwnd, GWL_STYLE );
443 LONG style = full_style & SS_TYPEMASK;
445 if (!IsWindow( hwnd )) return 0;
447 switch (uMsg)
449 case WM_CREATE:
451 HWND parent;
453 if (style < 0L || style > SS_TYPEMASK)
455 ERR("Unknown style %#lx\n", style );
456 return -1;
459 parent = GetParent( hwnd );
460 if (parent)
461 EnableThemeDialogTexture( parent, ETDT_ENABLE );
462 break;
465 case WM_NCDESTROY:
466 if (style == SS_ICON)
468 struct static_extra_info *extra = get_extra_ptr( hwnd, FALSE );
469 if (extra)
471 if (extra->image_has_alpha)
472 DeleteObject( extra->image.hbitmap );
473 Free( extra );
476 * FIXME
477 * DestroyIcon32( STATIC_SetIcon( wndPtr, 0 ) );
479 * We don't want to do this yet because DestroyIcon32 is broken. If the icon
480 * had already been loaded by the application the last thing we want to do is
481 * GlobalFree16 the handle.
483 break;
485 else
486 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
488 case WM_ERASEBKGND:
489 /* do all painting in WM_PAINT like Windows does */
490 return 1;
492 case WM_PRINTCLIENT:
493 case WM_PAINT:
495 PAINTSTRUCT ps;
496 RECT rect;
497 HDC hdc = wParam ? (HDC)wParam : BeginPaint(hwnd, &ps);
498 HRGN hrgn;
499 HBRUSH hbrush;
501 GetClientRect( hwnd, &rect );
502 hrgn = set_control_clipping( hdc, &rect );
503 hbrush = STATIC_SendWmCtlColorStatic( hwnd, hdc );
504 if (staticPaintFunc[style])
505 (staticPaintFunc[style])( hwnd, hdc, hbrush, full_style );
506 SelectClipRgn( hdc, hrgn );
507 if (hrgn) DeleteObject( hrgn );
508 if (!wParam) EndPaint(hwnd, &ps);
510 break;
512 case WM_ENABLE:
513 STATIC_TryPaintFcn( hwnd, full_style );
514 if (full_style & SS_NOTIFY)
516 if (wParam)
517 SendMessageW( GetParent(hwnd), WM_COMMAND,
518 MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_ENABLE ), (LPARAM)hwnd);
519 else
520 SendMessageW( GetParent(hwnd), WM_COMMAND,
521 MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_DISABLE ), (LPARAM)hwnd);
523 break;
525 case WM_SYSCOLORCHANGE:
526 COMCTL32_RefreshSysColors();
527 STATIC_TryPaintFcn( hwnd, full_style );
528 break;
530 case WM_THEMECHANGED:
531 InvalidateRect( hwnd, 0, TRUE );
532 break;
534 case WM_NCCREATE:
536 CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
538 if (full_style & SS_SUNKEN || style == SS_ETCHEDHORZ || style == SS_ETCHEDVERT)
539 SetWindowLongW( hwnd, GWL_EXSTYLE,
540 GetWindowLongW( hwnd, GWL_EXSTYLE ) | WS_EX_STATICEDGE );
542 if (style == SS_ETCHEDHORZ || style == SS_ETCHEDVERT) {
543 RECT rc;
544 GetClientRect(hwnd, &rc);
545 if (style == SS_ETCHEDHORZ)
546 rc.bottom = rc.top;
547 else
548 rc.right = rc.left;
549 AdjustWindowRectEx(&rc, full_style, FALSE, GetWindowLongW(hwnd, GWL_EXSTYLE));
550 SetWindowPos(hwnd, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
553 if (cs->lpszName && cs->lpszName[0] == 0xffff)
554 window_name = MAKEINTRESOURCEW(cs->lpszName[1]);
555 else
556 window_name = cs->lpszName;
558 switch (style)
560 case SS_ICON:
562 HICON hIcon;
564 hIcon = STATIC_LoadIconW(cs->hInstance, window_name, full_style);
565 STATIC_SetIcon(hwnd, hIcon, full_style);
567 break;
568 case SS_BITMAP:
569 if ((ULONG_PTR)cs->hInstance >> 16)
571 HBITMAP hBitmap;
572 hBitmap = LoadBitmapW(cs->hInstance, window_name);
573 STATIC_SetBitmap(hwnd, hBitmap, full_style);
575 break;
577 /* SS_ENHMETAFILE: Despite what MSDN says, Windows does not load
578 the enhanced metafile that was specified as the window text. */
580 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
582 case WM_SETTEXT:
583 if (hasTextStyle( full_style ))
585 lResult = DefWindowProcW( hwnd, uMsg, wParam, lParam );
586 STATIC_TryPaintFcn( hwnd, full_style );
588 break;
590 case WM_SETFONT:
591 if (hasTextStyle( full_style ))
593 STATIC_SetFont( hwnd, (HFONT)wParam );
594 if (LOWORD(lParam))
595 RedrawWindow( hwnd, NULL, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_ALLCHILDREN );
597 break;
599 case WM_GETFONT:
600 return (LRESULT)STATIC_GetFont( hwnd );
602 case WM_NCHITTEST:
603 if (full_style & SS_NOTIFY)
604 return HTCLIENT;
605 else
606 return HTTRANSPARENT;
608 case WM_GETDLGCODE:
609 return DLGC_STATIC;
611 case WM_LBUTTONDOWN:
612 case WM_NCLBUTTONDOWN:
613 if (full_style & SS_NOTIFY)
614 SendMessageW( GetParent(hwnd), WM_COMMAND,
615 MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_CLICKED ), (LPARAM)hwnd);
616 return 0;
618 case WM_LBUTTONDBLCLK:
619 case WM_NCLBUTTONDBLCLK:
620 if (full_style & SS_NOTIFY)
621 SendMessageW( GetParent(hwnd), WM_COMMAND,
622 MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_DBLCLK ), (LPARAM)hwnd);
623 return 0;
625 case STM_GETIMAGE:
626 return (LRESULT)STATIC_GetImage( hwnd, wParam, full_style );
628 case STM_GETICON:
629 return (LRESULT)STATIC_GetImage( hwnd, IMAGE_ICON, full_style );
631 case STM_SETIMAGE:
632 switch (wParam)
634 case IMAGE_BITMAP:
635 if (style != SS_BITMAP) return 0;
636 lResult = (LRESULT)STATIC_SetBitmap( hwnd, (HBITMAP)lParam, full_style );
637 break;
638 case IMAGE_ENHMETAFILE:
639 if (style != SS_ENHMETAFILE) return 0;
640 lResult = (LRESULT)STATIC_SetEnhMetaFile( hwnd, (HENHMETAFILE)lParam, full_style );
641 break;
642 case IMAGE_ICON:
643 case IMAGE_CURSOR:
644 if (style != SS_ICON) return 0;
645 lResult = (LRESULT)STATIC_SetIcon( hwnd, (HICON)lParam, full_style );
646 break;
647 default:
648 FIXME("STM_SETIMAGE: Unhandled type %Ix\n", wParam);
649 break;
651 STATIC_TryPaintFcn( hwnd, full_style );
652 break;
654 case STM_SETICON:
655 if (style != SS_ICON) return 0;
656 lResult = (LRESULT)STATIC_SetIcon( hwnd, (HICON)wParam, full_style );
657 STATIC_TryPaintFcn( hwnd, full_style );
658 break;
660 default:
661 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
663 return lResult;
666 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style )
668 DRAWITEMSTRUCT dis;
669 HFONT font, oldFont = NULL;
670 UINT id = (UINT)GetWindowLongPtrW( hwnd, GWLP_ID );
672 dis.CtlType = ODT_STATIC;
673 dis.CtlID = id;
674 dis.itemID = 0;
675 dis.itemAction = ODA_DRAWENTIRE;
676 dis.itemState = IsWindowEnabled(hwnd) ? 0 : ODS_DISABLED;
677 dis.hwndItem = hwnd;
678 dis.hDC = hdc;
679 dis.itemData = 0;
680 GetClientRect( hwnd, &dis.rcItem );
682 font = STATIC_GetFont( hwnd );
683 if (font) oldFont = SelectObject( hdc, font );
684 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
685 if (font) SelectObject( hdc, oldFont );
688 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style )
690 RECT rc;
691 HFONT hFont, hOldFont = NULL;
692 UINT format;
693 INT len, buf_size;
694 WCHAR *text;
696 GetClientRect( hwnd, &rc);
698 switch (style & SS_TYPEMASK)
700 case SS_LEFT:
701 format = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK;
702 break;
704 case SS_CENTER:
705 format = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK;
706 break;
708 case SS_RIGHT:
709 format = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK;
710 break;
712 case SS_SIMPLE:
713 format = DT_LEFT | DT_SINGLELINE;
714 break;
716 case SS_LEFTNOWORDWRAP:
717 format = DT_LEFT | DT_EXPANDTABS;
718 break;
720 default:
721 return;
724 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_RIGHT)
725 format = DT_RIGHT | (format & ~(DT_LEFT | DT_CENTER));
727 if (style & SS_NOPREFIX)
728 format |= DT_NOPREFIX;
730 if ((style & SS_TYPEMASK) != SS_SIMPLE)
732 if (style & SS_CENTERIMAGE)
733 format |= DT_SINGLELINE | DT_VCENTER;
734 if (style & SS_EDITCONTROL)
735 format |= DT_EDITCONTROL;
736 if (style & SS_ENDELLIPSIS)
737 format |= DT_SINGLELINE | DT_END_ELLIPSIS;
738 if (style & SS_PATHELLIPSIS)
739 format |= DT_SINGLELINE | DT_PATH_ELLIPSIS;
740 if (style & SS_WORDELLIPSIS)
741 format |= DT_SINGLELINE | DT_WORD_ELLIPSIS;
744 if ((hFont = STATIC_GetFont( hwnd )))
745 hOldFont = SelectObject( hdc, hFont );
747 if ((style & SS_TYPEMASK) != SS_SIMPLE)
749 FillRect( hdc, &rc, hbrush );
750 if (!IsWindowEnabled(hwnd)) SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
753 buf_size = 256;
754 if (!(text = Alloc( buf_size * sizeof(WCHAR) )))
755 goto no_TextOut;
757 while ((len = InternalGetWindowText( hwnd, text, buf_size )) == buf_size - 1)
759 buf_size *= 2;
760 if (!(text = ReAlloc( text, buf_size * sizeof(WCHAR) )))
761 goto no_TextOut;
764 if (!len) goto no_TextOut;
766 if (((style & SS_TYPEMASK) == SS_SIMPLE) && (style & SS_NOPREFIX))
768 /* Windows uses the faster ExtTextOut() to draw the text and
769 to paint the whole client rectangle with the text background
770 color. Reference: "Static Controls" by Kyle Marsh, 1992 */
771 ExtTextOutW( hdc, rc.left, rc.top, ETO_CLIPPED | ETO_OPAQUE,
772 &rc, text, len, NULL );
774 else
776 DrawTextW( hdc, text, -1, &rc, format );
779 no_TextOut:
780 Free( text );
782 if (hFont)
783 SelectObject( hdc, hOldFont );
786 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style )
788 RECT rc;
790 GetClientRect( hwnd, &rc);
792 switch (style & SS_TYPEMASK)
794 case SS_BLACKRECT:
795 hbrush = CreateSolidBrush(comctl32_color.clr3dDkShadow);
796 FillRect( hdc, &rc, hbrush );
797 break;
798 case SS_GRAYRECT:
799 hbrush = CreateSolidBrush(comctl32_color.clr3dShadow);
800 FillRect( hdc, &rc, hbrush );
801 break;
802 case SS_WHITERECT:
803 hbrush = CreateSolidBrush(comctl32_color.clr3dHilight);
804 FillRect( hdc, &rc, hbrush );
805 break;
806 case SS_BLACKFRAME:
807 hbrush = CreateSolidBrush(comctl32_color.clr3dDkShadow);
808 FrameRect( hdc, &rc, hbrush );
809 break;
810 case SS_GRAYFRAME:
811 hbrush = CreateSolidBrush(comctl32_color.clr3dShadow);
812 FrameRect( hdc, &rc, hbrush );
813 break;
814 case SS_WHITEFRAME:
815 hbrush = CreateSolidBrush(comctl32_color.clr3dHilight);
816 FrameRect( hdc, &rc, hbrush );
817 break;
818 default:
819 return;
821 DeleteObject( hbrush );
825 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style )
827 RECT rc, iconRect;
828 HICON hIcon;
829 SIZE size;
831 GetClientRect( hwnd, &rc );
832 hIcon = STATIC_GetImage( hwnd, IMAGE_ICON, style );
833 if (!hIcon || !get_icon_size( hIcon, &size ))
835 FillRect(hdc, &rc, hbrush);
837 else
839 if (style & SS_CENTERIMAGE)
841 iconRect.left = (rc.right - rc.left) / 2 - size.cx / 2;
842 iconRect.top = (rc.bottom - rc.top) / 2 - size.cy / 2;
843 iconRect.right = iconRect.left + size.cx;
844 iconRect.bottom = iconRect.top + size.cy;
846 else
847 iconRect = rc;
848 FillRect( hdc, &rc, hbrush );
849 DrawIconEx( hdc, iconRect.left, iconRect.top, hIcon, iconRect.right - iconRect.left,
850 iconRect.bottom - iconRect.top, 0, NULL, DI_NORMAL );
854 static void STATIC_PaintBitmapfn(HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style )
856 HDC hMemDC;
857 HBITMAP hBitmap, oldbitmap;
858 RECT rcClient;
860 GetClientRect( hwnd, &rcClient );
861 FillRect( hdc, &rcClient, hbrush );
863 if ((hBitmap = STATIC_GetImage( hwnd, IMAGE_BITMAP, style ))
864 && (GetObjectType(hBitmap) == OBJ_BITMAP)
865 && (hMemDC = CreateCompatibleDC( hdc )))
867 BITMAP bm;
868 LOGBRUSH brush;
869 BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
870 struct static_extra_info *extra = get_extra_ptr( hwnd, FALSE );
872 GetObjectW(hBitmap, sizeof(bm), &bm);
873 oldbitmap = SelectObject(hMemDC, hBitmap);
875 /* Set the background color for monochrome bitmaps
876 to the color of the background brush */
877 if (GetObjectW( hbrush, sizeof(brush), &brush ))
879 if (brush.lbStyle == BS_SOLID)
880 SetBkColor(hdc, brush.lbColor);
882 if (style & SS_CENTERIMAGE)
884 rcClient.left = (rcClient.right - rcClient.left)/2 - bm.bmWidth/2;
885 rcClient.top = (rcClient.bottom - rcClient.top)/2 - bm.bmHeight/2;
886 rcClient.right = rcClient.left + bm.bmWidth;
887 rcClient.bottom = rcClient.top + bm.bmHeight;
890 if (extra->image_has_alpha)
891 GdiAlphaBlend(hdc, rcClient.left, rcClient.top, rcClient.right - rcClient.left,
892 rcClient.bottom - rcClient.top, hMemDC,
893 0, 0, bm.bmWidth, bm.bmHeight, blend);
894 else
895 StretchBlt(hdc, rcClient.left, rcClient.top, rcClient.right - rcClient.left,
896 rcClient.bottom - rcClient.top, hMemDC,
897 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
898 SelectObject(hMemDC, oldbitmap);
899 DeleteDC(hMemDC);
903 static void STATIC_PaintEnhMetafn(HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style )
905 HENHMETAFILE hEnhMetaFile;
906 RECT rc;
908 GetClientRect(hwnd, &rc);
909 FillRect(hdc, &rc, hbrush);
910 if ((hEnhMetaFile = STATIC_GetImage( hwnd, IMAGE_ENHMETAFILE, style )))
912 /* The control's current font is not selected into the
913 device context! */
914 if (GetObjectType(hEnhMetaFile) == OBJ_ENHMETAFILE)
915 PlayEnhMetaFile(hdc, hEnhMetaFile, &rc);
919 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style )
921 RECT rc;
923 GetClientRect( hwnd, &rc );
924 DrawEdge(hdc, &rc, EDGE_ETCHED, BF_RECT);
927 void STATIC_Register(void)
929 WNDCLASSW wndClass;
931 memset(&wndClass, 0, sizeof(wndClass));
932 wndClass.style = CS_DBLCLKS | CS_PARENTDC | CS_GLOBALCLASS;
933 wndClass.lpfnWndProc = STATIC_WindowProc;
934 wndClass.cbClsExtra = 0;
935 wndClass.cbWndExtra = sizeof(struct static_extra_info *);
936 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
937 wndClass.hbrBackground = NULL;
938 wndClass.lpszClassName = WC_STATICW;
939 RegisterClassW(&wndClass);