win32u: Move NtUserTranslateMessage implementation from user32.
[wine.git] / dlls / comctl32 / static.c
blob3151cda655259587b5fcc8e86672a3883ab237ec
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>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wingdi.h"
36 #include "winuser.h"
37 #include "commctrl.h"
38 #include "uxtheme.h"
40 #include "wine/heap.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 = heap_alloc_zero( 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 LRESULT lResult = 0;
441 LONG full_style = GetWindowLongW( hwnd, GWL_STYLE );
442 LONG style = full_style & SS_TYPEMASK;
444 if (!IsWindow( hwnd )) return 0;
446 switch (uMsg)
448 case WM_CREATE:
450 HWND parent;
452 if (style < 0L || style > SS_TYPEMASK)
454 ERR("Unknown style %#lx\n", style );
455 return -1;
458 parent = GetParent( hwnd );
459 if (parent)
460 EnableThemeDialogTexture( parent, ETDT_ENABLE );
461 break;
464 case WM_NCDESTROY:
465 if (style == SS_ICON)
467 struct static_extra_info *extra = get_extra_ptr( hwnd, FALSE );
468 if (extra)
470 if (extra->image_has_alpha)
471 DeleteObject( extra->image.hbitmap );
472 heap_free( extra );
475 * FIXME
476 * DestroyIcon32( STATIC_SetIcon( wndPtr, 0 ) );
478 * We don't want to do this yet because DestroyIcon32 is broken. If the icon
479 * had already been loaded by the application the last thing we want to do is
480 * GlobalFree16 the handle.
482 break;
484 else
485 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
487 case WM_ERASEBKGND:
488 /* do all painting in WM_PAINT like Windows does */
489 return 1;
491 case WM_PRINTCLIENT:
492 case WM_PAINT:
494 PAINTSTRUCT ps;
495 RECT rect;
496 HDC hdc = wParam ? (HDC)wParam : BeginPaint(hwnd, &ps);
497 HRGN hrgn;
498 HBRUSH hbrush;
500 GetClientRect( hwnd, &rect );
501 hrgn = set_control_clipping( hdc, &rect );
502 hbrush = STATIC_SendWmCtlColorStatic( hwnd, hdc );
503 if (staticPaintFunc[style])
504 (staticPaintFunc[style])( hwnd, hdc, hbrush, full_style );
505 SelectClipRgn( hdc, hrgn );
506 if (hrgn) DeleteObject( hrgn );
507 if (!wParam) EndPaint(hwnd, &ps);
509 break;
511 case WM_ENABLE:
512 STATIC_TryPaintFcn( hwnd, full_style );
513 if (full_style & SS_NOTIFY)
515 if (wParam)
516 SendMessageW( GetParent(hwnd), WM_COMMAND,
517 MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_ENABLE ), (LPARAM)hwnd);
518 else
519 SendMessageW( GetParent(hwnd), WM_COMMAND,
520 MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_DISABLE ), (LPARAM)hwnd);
522 break;
524 case WM_SYSCOLORCHANGE:
525 COMCTL32_RefreshSysColors();
526 STATIC_TryPaintFcn( hwnd, full_style );
527 break;
529 case WM_THEMECHANGED:
530 InvalidateRect( hwnd, 0, TRUE );
531 break;
533 case WM_NCCREATE:
535 CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
537 if (full_style & SS_SUNKEN || style == SS_ETCHEDHORZ || style == SS_ETCHEDVERT)
538 SetWindowLongW( hwnd, GWL_EXSTYLE,
539 GetWindowLongW( hwnd, GWL_EXSTYLE ) | WS_EX_STATICEDGE );
541 if (style == SS_ETCHEDHORZ || style == SS_ETCHEDVERT) {
542 RECT rc;
543 GetClientRect(hwnd, &rc);
544 if (style == SS_ETCHEDHORZ)
545 rc.bottom = rc.top;
546 else
547 rc.right = rc.left;
548 AdjustWindowRectEx(&rc, full_style, FALSE, GetWindowLongW(hwnd, GWL_EXSTYLE));
549 SetWindowPos(hwnd, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
552 switch (style)
554 case SS_ICON:
556 HICON hIcon;
558 hIcon = STATIC_LoadIconW(cs->hInstance, cs->lpszName, full_style);
559 STATIC_SetIcon(hwnd, hIcon, full_style);
561 break;
562 case SS_BITMAP:
563 if ((ULONG_PTR)cs->hInstance >> 16)
565 HBITMAP hBitmap;
566 hBitmap = LoadBitmapW(cs->hInstance, cs->lpszName);
567 STATIC_SetBitmap(hwnd, hBitmap, full_style);
569 break;
571 /* SS_ENHMETAFILE: Despite what MSDN says, Windows does not load
572 the enhanced metafile that was specified as the window text. */
574 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
576 case WM_SETTEXT:
577 if (hasTextStyle( full_style ))
579 lResult = DefWindowProcW( hwnd, uMsg, wParam, lParam );
580 STATIC_TryPaintFcn( hwnd, full_style );
582 break;
584 case WM_SETFONT:
585 if (hasTextStyle( full_style ))
587 STATIC_SetFont( hwnd, (HFONT)wParam );
588 if (LOWORD(lParam))
589 RedrawWindow( hwnd, NULL, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_ALLCHILDREN );
591 break;
593 case WM_GETFONT:
594 return (LRESULT)STATIC_GetFont( hwnd );
596 case WM_NCHITTEST:
597 if (full_style & SS_NOTIFY)
598 return HTCLIENT;
599 else
600 return HTTRANSPARENT;
602 case WM_GETDLGCODE:
603 return DLGC_STATIC;
605 case WM_LBUTTONDOWN:
606 case WM_NCLBUTTONDOWN:
607 if (full_style & SS_NOTIFY)
608 SendMessageW( GetParent(hwnd), WM_COMMAND,
609 MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_CLICKED ), (LPARAM)hwnd);
610 return 0;
612 case WM_LBUTTONDBLCLK:
613 case WM_NCLBUTTONDBLCLK:
614 if (full_style & SS_NOTIFY)
615 SendMessageW( GetParent(hwnd), WM_COMMAND,
616 MAKEWPARAM( GetWindowLongPtrW(hwnd,GWLP_ID), STN_DBLCLK ), (LPARAM)hwnd);
617 return 0;
619 case STM_GETIMAGE:
620 return (LRESULT)STATIC_GetImage( hwnd, wParam, full_style );
622 case STM_GETICON:
623 return (LRESULT)STATIC_GetImage( hwnd, IMAGE_ICON, full_style );
625 case STM_SETIMAGE:
626 switch (wParam)
628 case IMAGE_BITMAP:
629 if (style != SS_BITMAP) return 0;
630 lResult = (LRESULT)STATIC_SetBitmap( hwnd, (HBITMAP)lParam, full_style );
631 break;
632 case IMAGE_ENHMETAFILE:
633 if (style != SS_ENHMETAFILE) return 0;
634 lResult = (LRESULT)STATIC_SetEnhMetaFile( hwnd, (HENHMETAFILE)lParam, full_style );
635 break;
636 case IMAGE_ICON:
637 case IMAGE_CURSOR:
638 if (style != SS_ICON) return 0;
639 lResult = (LRESULT)STATIC_SetIcon( hwnd, (HICON)lParam, full_style );
640 break;
641 default:
642 FIXME("STM_SETIMAGE: Unhandled type %Ix\n", wParam);
643 break;
645 STATIC_TryPaintFcn( hwnd, full_style );
646 break;
648 case STM_SETICON:
649 if (style != SS_ICON) return 0;
650 lResult = (LRESULT)STATIC_SetIcon( hwnd, (HICON)wParam, full_style );
651 STATIC_TryPaintFcn( hwnd, full_style );
652 break;
654 default:
655 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
657 return lResult;
660 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style )
662 DRAWITEMSTRUCT dis;
663 HFONT font, oldFont = NULL;
664 UINT id = (UINT)GetWindowLongPtrW( hwnd, GWLP_ID );
666 dis.CtlType = ODT_STATIC;
667 dis.CtlID = id;
668 dis.itemID = 0;
669 dis.itemAction = ODA_DRAWENTIRE;
670 dis.itemState = IsWindowEnabled(hwnd) ? 0 : ODS_DISABLED;
671 dis.hwndItem = hwnd;
672 dis.hDC = hdc;
673 dis.itemData = 0;
674 GetClientRect( hwnd, &dis.rcItem );
676 font = STATIC_GetFont( hwnd );
677 if (font) oldFont = SelectObject( hdc, font );
678 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
679 if (font) SelectObject( hdc, oldFont );
682 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style )
684 RECT rc;
685 HFONT hFont, hOldFont = NULL;
686 UINT format;
687 INT len, buf_size;
688 WCHAR *text;
690 GetClientRect( hwnd, &rc);
692 switch (style & SS_TYPEMASK)
694 case SS_LEFT:
695 format = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK;
696 break;
698 case SS_CENTER:
699 format = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK;
700 break;
702 case SS_RIGHT:
703 format = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK;
704 break;
706 case SS_SIMPLE:
707 format = DT_LEFT | DT_SINGLELINE;
708 break;
710 case SS_LEFTNOWORDWRAP:
711 format = DT_LEFT | DT_EXPANDTABS;
712 break;
714 default:
715 return;
718 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_RIGHT)
719 format = DT_RIGHT | (format & ~(DT_LEFT | DT_CENTER));
721 if (style & SS_NOPREFIX)
722 format |= DT_NOPREFIX;
724 if ((style & SS_TYPEMASK) != SS_SIMPLE)
726 if (style & SS_CENTERIMAGE)
727 format |= DT_SINGLELINE | DT_VCENTER;
728 if (style & SS_EDITCONTROL)
729 format |= DT_EDITCONTROL;
730 if (style & SS_ENDELLIPSIS)
731 format |= DT_SINGLELINE | DT_END_ELLIPSIS;
732 if (style & SS_PATHELLIPSIS)
733 format |= DT_SINGLELINE | DT_PATH_ELLIPSIS;
734 if (style & SS_WORDELLIPSIS)
735 format |= DT_SINGLELINE | DT_WORD_ELLIPSIS;
738 if ((hFont = STATIC_GetFont( hwnd )))
739 hOldFont = SelectObject( hdc, hFont );
741 if ((style & SS_TYPEMASK) != SS_SIMPLE)
743 FillRect( hdc, &rc, hbrush );
744 if (!IsWindowEnabled(hwnd)) SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
747 buf_size = 256;
748 if (!(text = HeapAlloc( GetProcessHeap(), 0, buf_size * sizeof(WCHAR) )))
749 goto no_TextOut;
751 while ((len = InternalGetWindowText( hwnd, text, buf_size )) == buf_size - 1)
753 buf_size *= 2;
754 if (!(text = HeapReAlloc( GetProcessHeap(), 0, text, buf_size * sizeof(WCHAR) )))
755 goto no_TextOut;
758 if (!len) goto no_TextOut;
760 if (((style & SS_TYPEMASK) == SS_SIMPLE) && (style & SS_NOPREFIX))
762 /* Windows uses the faster ExtTextOut() to draw the text and
763 to paint the whole client rectangle with the text background
764 color. Reference: "Static Controls" by Kyle Marsh, 1992 */
765 ExtTextOutW( hdc, rc.left, rc.top, ETO_CLIPPED | ETO_OPAQUE,
766 &rc, text, len, NULL );
768 else
770 DrawTextW( hdc, text, -1, &rc, format );
773 no_TextOut:
774 HeapFree( GetProcessHeap(), 0, text );
776 if (hFont)
777 SelectObject( hdc, hOldFont );
780 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style )
782 RECT rc;
784 GetClientRect( hwnd, &rc);
786 switch (style & SS_TYPEMASK)
788 case SS_BLACKRECT:
789 hbrush = CreateSolidBrush(comctl32_color.clr3dDkShadow);
790 FillRect( hdc, &rc, hbrush );
791 break;
792 case SS_GRAYRECT:
793 hbrush = CreateSolidBrush(comctl32_color.clr3dShadow);
794 FillRect( hdc, &rc, hbrush );
795 break;
796 case SS_WHITERECT:
797 hbrush = CreateSolidBrush(comctl32_color.clr3dHilight);
798 FillRect( hdc, &rc, hbrush );
799 break;
800 case SS_BLACKFRAME:
801 hbrush = CreateSolidBrush(comctl32_color.clr3dDkShadow);
802 FrameRect( hdc, &rc, hbrush );
803 break;
804 case SS_GRAYFRAME:
805 hbrush = CreateSolidBrush(comctl32_color.clr3dShadow);
806 FrameRect( hdc, &rc, hbrush );
807 break;
808 case SS_WHITEFRAME:
809 hbrush = CreateSolidBrush(comctl32_color.clr3dHilight);
810 FrameRect( hdc, &rc, hbrush );
811 break;
812 default:
813 return;
815 DeleteObject( hbrush );
819 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style )
821 RECT rc, iconRect;
822 HICON hIcon;
823 SIZE size;
825 GetClientRect( hwnd, &rc );
826 hIcon = STATIC_GetImage( hwnd, IMAGE_ICON, style );
827 if (!hIcon || !get_icon_size( hIcon, &size ))
829 FillRect(hdc, &rc, hbrush);
831 else
833 if (style & SS_CENTERIMAGE)
835 iconRect.left = (rc.right - rc.left) / 2 - size.cx / 2;
836 iconRect.top = (rc.bottom - rc.top) / 2 - size.cy / 2;
837 iconRect.right = iconRect.left + size.cx;
838 iconRect.bottom = iconRect.top + size.cy;
840 else
841 iconRect = rc;
842 FillRect( hdc, &rc, hbrush );
843 DrawIconEx( hdc, iconRect.left, iconRect.top, hIcon, iconRect.right - iconRect.left,
844 iconRect.bottom - iconRect.top, 0, NULL, DI_NORMAL );
848 static void STATIC_PaintBitmapfn(HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style )
850 HDC hMemDC;
851 HBITMAP hBitmap, oldbitmap;
853 if ((hBitmap = STATIC_GetImage( hwnd, IMAGE_BITMAP, style ))
854 && (GetObjectType(hBitmap) == OBJ_BITMAP)
855 && (hMemDC = CreateCompatibleDC( hdc )))
857 BITMAP bm;
858 RECT rcClient;
859 LOGBRUSH brush;
860 BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
861 struct static_extra_info *extra = get_extra_ptr( hwnd, FALSE );
863 GetObjectW(hBitmap, sizeof(bm), &bm);
864 oldbitmap = SelectObject(hMemDC, hBitmap);
866 /* Set the background color for monochrome bitmaps
867 to the color of the background brush */
868 if (GetObjectW( hbrush, sizeof(brush), &brush ))
870 if (brush.lbStyle == BS_SOLID)
871 SetBkColor(hdc, brush.lbColor);
873 GetClientRect(hwnd, &rcClient);
874 if (style & SS_CENTERIMAGE)
876 FillRect( hdc, &rcClient, hbrush );
877 rcClient.left = (rcClient.right - rcClient.left)/2 - bm.bmWidth/2;
878 rcClient.top = (rcClient.bottom - rcClient.top)/2 - bm.bmHeight/2;
879 rcClient.right = rcClient.left + bm.bmWidth;
880 rcClient.bottom = rcClient.top + bm.bmHeight;
883 if (extra->image_has_alpha)
884 GdiAlphaBlend(hdc, rcClient.left, rcClient.top, rcClient.right - rcClient.left,
885 rcClient.bottom - rcClient.top, hMemDC,
886 0, 0, bm.bmWidth, bm.bmHeight, blend);
887 else
888 StretchBlt(hdc, rcClient.left, rcClient.top, rcClient.right - rcClient.left,
889 rcClient.bottom - rcClient.top, hMemDC,
890 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
891 SelectObject(hMemDC, oldbitmap);
892 DeleteDC(hMemDC);
896 static void STATIC_PaintEnhMetafn(HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style )
898 HENHMETAFILE hEnhMetaFile;
899 RECT rc;
901 GetClientRect(hwnd, &rc);
902 FillRect(hdc, &rc, hbrush);
903 if ((hEnhMetaFile = STATIC_GetImage( hwnd, IMAGE_ENHMETAFILE, style )))
905 /* The control's current font is not selected into the
906 device context! */
907 if (GetObjectType(hEnhMetaFile) == OBJ_ENHMETAFILE)
908 PlayEnhMetaFile(hdc, hEnhMetaFile, &rc);
912 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, HBRUSH hbrush, DWORD style )
914 RECT rc;
916 GetClientRect( hwnd, &rc );
917 DrawEdge(hdc, &rc, EDGE_ETCHED, BF_RECT);
920 void STATIC_Register(void)
922 WNDCLASSW wndClass;
924 memset(&wndClass, 0, sizeof(wndClass));
925 wndClass.style = CS_DBLCLKS | CS_PARENTDC | CS_GLOBALCLASS;
926 wndClass.lpfnWndProc = STATIC_WindowProc;
927 wndClass.cbClsExtra = 0;
928 wndClass.cbWndExtra = sizeof(struct static_extra_info *);
929 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
930 wndClass.hbrBackground = NULL;
931 wndClass.lpszClassName = WC_STATICW;
932 RegisterClassW(&wndClass);