WNDPROC handlers now check whether HWND argument is valid.
[wine/dcerpc.git] / controls / static.c
blob21ad0c2da7162da817eefa843861ecd5c236d6bf
1 /*
2 * Static control
4 * Copyright David W. Metcalfe, 1993
6 */
8 #include "windef.h"
9 #include "wingdi.h"
10 #include "wine/winuser16.h"
11 #include "win.h"
12 #include "cursoricon.h"
13 #include "controls.h"
14 #include "heap.h"
15 #include "user.h"
16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(static);
20 static void STATIC_PaintOwnerDrawfn( WND *wndPtr, HDC hdc );
21 static void STATIC_PaintTextfn( WND *wndPtr, HDC hdc );
22 static void STATIC_PaintRectfn( WND *wndPtr, HDC hdc );
23 static void STATIC_PaintIconfn( WND *wndPtr, HDC hdc );
24 static void STATIC_PaintBitmapfn( WND *wndPtr, HDC hdc );
25 static void STATIC_PaintEtchedfn( WND *wndPtr, HDC hdc );
26 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
27 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
29 static COLORREF color_windowframe, color_background, color_window;
31 typedef struct
33 HFONT16 hFont; /* Control font (or 0 for system font) */
34 WORD dummy; /* Don't know what MS-Windows puts in there */
35 HICON16 hIcon; /* Icon handle for SS_ICON controls */
36 } STATICINFO;
38 typedef void (*pfPaint)( WND *, HDC );
40 static pfPaint staticPaintFunc[SS_TYPEMASK+1] =
42 STATIC_PaintTextfn, /* SS_LEFT */
43 STATIC_PaintTextfn, /* SS_CENTER */
44 STATIC_PaintTextfn, /* SS_RIGHT */
45 STATIC_PaintIconfn, /* SS_ICON */
46 STATIC_PaintRectfn, /* SS_BLACKRECT */
47 STATIC_PaintRectfn, /* SS_GRAYRECT */
48 STATIC_PaintRectfn, /* SS_WHITERECT */
49 STATIC_PaintRectfn, /* SS_BLACKFRAME */
50 STATIC_PaintRectfn, /* SS_GRAYFRAME */
51 STATIC_PaintRectfn, /* SS_WHITEFRAME */
52 NULL, /* Not defined */
53 STATIC_PaintTextfn, /* SS_SIMPLE */
54 STATIC_PaintTextfn, /* SS_LEFTNOWORDWRAP */
55 STATIC_PaintOwnerDrawfn, /* SS_OWNERDRAW */
56 STATIC_PaintBitmapfn, /* SS_BITMAP */
57 NULL, /* SS_ENHMETAFILE */
58 STATIC_PaintEtchedfn, /* SS_ETCHEDHORIZ */
59 STATIC_PaintEtchedfn, /* SS_ETCHEDVERT */
60 STATIC_PaintEtchedfn, /* SS_ETCHEDFRAME */
64 /*********************************************************************
65 * static class descriptor
67 const struct builtin_class_descr STATIC_builtin_class =
69 "Static", /* name */
70 CS_GLOBALCLASS | CS_DBLCLKS | CS_PARENTDC, /* style */
71 StaticWndProcA, /* procA */
72 StaticWndProcW, /* procW */
73 sizeof(STATICINFO), /* extra */
74 IDC_ARROWA, /* cursor */
75 0 /* brush */
79 /***********************************************************************
80 * STATIC_SetIcon
82 * Set the icon for an SS_ICON control.
84 static HICON16 STATIC_SetIcon( WND *wndPtr, HICON16 hicon )
86 HICON16 prevIcon;
87 STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
88 CURSORICONINFO *info = hicon?(CURSORICONINFO *) GlobalLock16( hicon ):NULL;
90 if ((wndPtr->dwStyle & SS_TYPEMASK) != SS_ICON) return 0;
91 if (hicon && !info) {
92 ERR("huh? hicon!=0, but info=0???\n");
93 return 0;
95 prevIcon = infoPtr->hIcon;
96 infoPtr->hIcon = hicon;
97 if (hicon)
99 SetWindowPos( wndPtr->hwndSelf, 0, 0, 0, info->nWidth, info->nHeight,
100 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
101 GlobalUnlock16( hicon );
103 return prevIcon;
106 /***********************************************************************
107 * STATIC_SetBitmap
109 * Set the bitmap for an SS_BITMAP control.
111 static HBITMAP16 STATIC_SetBitmap( WND *wndPtr, HBITMAP16 hBitmap )
113 HBITMAP16 hOldBitmap;
114 STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
116 if ((wndPtr->dwStyle & SS_TYPEMASK) != SS_BITMAP) return 0;
117 if (hBitmap && GetObjectType(hBitmap) != OBJ_BITMAP) {
118 ERR("huh? hBitmap!=0, but not bitmap\n");
119 return 0;
121 hOldBitmap = infoPtr->hIcon;
122 infoPtr->hIcon = hBitmap;
123 if (hBitmap)
125 BITMAP bm;
126 GetObjectW(hBitmap, sizeof(bm), &bm);
127 SetWindowPos( wndPtr->hwndSelf, 0, 0, 0, bm.bmWidth, bm.bmHeight,
128 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
130 return hOldBitmap;
133 /***********************************************************************
134 * STATIC_LoadIconA
136 * Load the icon for an SS_ICON control.
138 static HICON STATIC_LoadIconA( WND *wndPtr, LPCSTR name )
140 HICON hicon = LoadIconA( wndPtr->hInstance, name );
141 if (!hicon) hicon = LoadIconA( 0, name );
142 return hicon;
145 /***********************************************************************
146 * STATIC_LoadIconW
148 * Load the icon for an SS_ICON control.
150 static HICON STATIC_LoadIconW( WND *wndPtr, LPCWSTR name )
152 HICON hicon = LoadIconW( wndPtr->hInstance, name );
153 if (!hicon) hicon = LoadIconW( 0, name );
154 return hicon;
157 /***********************************************************************
158 * STATIC_LoadBitmapA
160 * Load the bitmap for an SS_BITMAP control.
162 static HBITMAP STATIC_LoadBitmapA( WND *wndPtr, LPCSTR name )
164 HBITMAP hbitmap = LoadBitmapA( wndPtr->hInstance, name );
165 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
166 hbitmap = LoadBitmapA( 0, name );
167 return hbitmap;
170 /***********************************************************************
171 * STATIC_LoadBitmapW
173 * Load the bitmap for an SS_BITMAP control.
175 static HBITMAP STATIC_LoadBitmapW( WND *wndPtr, LPCWSTR name )
177 HBITMAP hbitmap = LoadBitmapW( wndPtr->hInstance, name );
178 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
179 hbitmap = LoadBitmapW( 0, name );
180 return hbitmap;
183 /***********************************************************************
184 * StaticWndProc_locked
186 static LRESULT StaticWndProc_locked( WND *wndPtr, UINT uMsg, WPARAM wParam,
187 LPARAM lParam, BOOL unicode )
189 LRESULT lResult = 0;
190 LONG style = wndPtr->dwStyle & SS_TYPEMASK;
191 STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
193 switch (uMsg)
195 case WM_CREATE:
196 if (style < 0L || style > SS_TYPEMASK)
198 ERR("Unknown style 0x%02lx\n", style );
199 lResult = -1L;
200 break;
202 /* initialise colours */
203 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
204 color_background = GetSysColor(COLOR_BACKGROUND);
205 color_window = GetSysColor(COLOR_WINDOW);
206 break;
208 case WM_NCDESTROY:
209 if (style == SS_ICON) {
211 * FIXME
212 * DestroyIcon32( STATIC_SetIcon( wndPtr, 0 ) );
214 * We don't want to do this yet because DestroyIcon32 is broken. If the icon
215 * had already been loaded by the application the last thing we want to do is
216 * GlobalFree16 the handle.
218 } else {
219 lResult = unicode ? DefWindowProcW(wndPtr->hwndSelf, uMsg, wParam, lParam) :
220 DefWindowProcA(wndPtr->hwndSelf, uMsg, wParam, lParam);
222 break;
224 case WM_PAINT:
226 PAINTSTRUCT ps;
227 BeginPaint(wndPtr->hwndSelf, &ps);
228 if (staticPaintFunc[style])
229 (staticPaintFunc[style])( wndPtr, ps.hdc );
230 EndPaint(wndPtr->hwndSelf, &ps);
232 break;
234 case WM_ENABLE:
235 InvalidateRect(wndPtr->hwndSelf, NULL, FALSE);
236 break;
238 case WM_SYSCOLORCHANGE:
239 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
240 color_background = GetSysColor(COLOR_BACKGROUND);
241 color_window = GetSysColor(COLOR_WINDOW);
242 InvalidateRect(wndPtr->hwndSelf, NULL, TRUE);
243 break;
245 case WM_NCCREATE:
246 if ((TWEAK_WineLook > WIN31_LOOK) && (wndPtr->dwStyle & SS_SUNKEN))
247 wndPtr->dwExStyle |= WS_EX_STATICEDGE;
249 if(unicode)
250 lParam = (LPARAM)(((LPCREATESTRUCTW)lParam)->lpszName);
251 else
252 lParam = (LPARAM)(((LPCREATESTRUCTA)lParam)->lpszName);
253 /* fall through */
254 case WM_SETTEXT:
255 if (style == SS_ICON)
257 HICON hIcon;
258 if(unicode)
259 hIcon = STATIC_LoadIconW(wndPtr, (LPCWSTR)lParam);
260 else
261 hIcon = STATIC_LoadIconA(wndPtr, (LPCSTR)lParam);
262 /* FIXME : should we also return the previous hIcon here ??? */
263 STATIC_SetIcon(wndPtr, hIcon);
265 else if (style == SS_BITMAP)
267 HBITMAP hBitmap;
268 if(unicode)
269 hBitmap = STATIC_LoadBitmapW(wndPtr, (LPCWSTR)lParam);
270 else
271 hBitmap = STATIC_LoadBitmapA(wndPtr, (LPCSTR)lParam);
272 STATIC_SetBitmap(wndPtr, hBitmap);
274 else if(lParam && HIWORD(lParam))
276 if(unicode)
277 DEFWND_SetTextW(wndPtr, (LPCWSTR)lParam);
278 else
279 DEFWND_SetTextA(wndPtr, (LPCSTR)lParam);
281 if(uMsg == WM_SETTEXT)
282 InvalidateRect(wndPtr->hwndSelf, NULL, FALSE);
283 lResult = 1; /* success. FIXME: check text length */
284 break;
286 case WM_SETFONT:
287 if (style == SS_ICON)
289 lResult = 0;
290 goto END;
292 if (style == SS_BITMAP)
294 lResult = 0;
295 goto END;
297 infoPtr->hFont = (HFONT16)wParam;
298 if (LOWORD(lParam))
299 InvalidateRect( wndPtr->hwndSelf, NULL, FALSE );
300 break;
302 case WM_GETFONT:
303 lResult = infoPtr->hFont;
304 goto END;
306 case WM_NCHITTEST:
307 if (wndPtr->dwStyle & SS_NOTIFY)
308 lResult = HTCLIENT;
309 else
310 lResult = HTTRANSPARENT;
311 goto END;
313 case WM_GETDLGCODE:
314 lResult = DLGC_STATIC;
315 goto END;
317 case STM_GETIMAGE:
318 case STM_GETICON16:
319 case STM_GETICON:
320 lResult = infoPtr->hIcon;
321 goto END;
323 case STM_SETIMAGE:
324 switch(wParam) {
325 case IMAGE_BITMAP:
326 lResult = STATIC_SetBitmap( wndPtr, (HBITMAP)lParam );
327 break;
328 case IMAGE_ICON:
329 lResult = STATIC_SetIcon( wndPtr, (HICON16)lParam );
330 break;
331 default:
332 FIXME("STM_SETIMAGE: Unhandled type %x\n", wParam);
333 break;
335 InvalidateRect( wndPtr->hwndSelf, NULL, FALSE );
336 break;
338 case STM_SETICON16:
339 case STM_SETICON:
340 lResult = STATIC_SetIcon( wndPtr, (HICON16)wParam );
341 InvalidateRect( wndPtr->hwndSelf, NULL, FALSE );
342 break;
344 default:
345 lResult = unicode ? DefWindowProcW(wndPtr->hwndSelf, uMsg, wParam, lParam) :
346 DefWindowProcA(wndPtr->hwndSelf, uMsg, wParam, lParam);
347 break;
350 END:
351 return lResult;
354 /***********************************************************************
355 * StaticWndProcA
357 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
359 LRESULT lResult = 0;
360 WND *wndPtr = WIN_FindWndPtr(hWnd);
362 if (wndPtr)
364 lResult = StaticWndProc_locked(wndPtr, uMsg, wParam, lParam, FALSE);
365 WIN_ReleaseWndPtr(wndPtr);
367 return lResult;
370 /***********************************************************************
371 * StaticWndProcW
373 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
375 LRESULT lResult = 0;
376 WND *wndPtr = WIN_FindWndPtr(hWnd);
378 if (wndPtr)
380 lResult = StaticWndProc_locked(wndPtr, uMsg, wParam, lParam, TRUE);
381 WIN_ReleaseWndPtr(wndPtr);
383 return lResult;
386 static void STATIC_PaintOwnerDrawfn( WND *wndPtr, HDC hdc )
388 DRAWITEMSTRUCT dis;
390 dis.CtlType = ODT_STATIC;
391 dis.CtlID = wndPtr->wIDmenu;
392 dis.itemID = 0;
393 dis.itemAction = ODA_DRAWENTIRE;
394 dis.itemState = 0;
395 dis.hwndItem = wndPtr->hwndSelf;
396 dis.hDC = hdc;
397 dis.itemData = 0;
398 GetClientRect( wndPtr->hwndSelf, &dis.rcItem );
400 SendMessageW( GetParent(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
401 hdc, wndPtr->hwndSelf );
402 SendMessageW( GetParent(wndPtr->hwndSelf), WM_DRAWITEM,
403 wndPtr->wIDmenu, (LPARAM)&dis );
406 static void STATIC_PaintTextfn( WND *wndPtr, HDC hdc )
408 RECT rc;
409 HBRUSH hBrush;
410 WORD wFormat;
412 LONG style = wndPtr->dwStyle;
413 STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
415 GetClientRect( wndPtr->hwndSelf, &rc);
417 switch (style & SS_TYPEMASK)
419 case SS_LEFT:
420 wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
421 break;
423 case SS_CENTER:
424 wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
425 break;
427 case SS_RIGHT:
428 wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
429 break;
431 case SS_SIMPLE:
432 wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOCLIP;
433 break;
435 case SS_LEFTNOWORDWRAP:
436 wFormat = DT_LEFT | DT_EXPANDTABS | DT_VCENTER;
437 break;
439 default:
440 return;
443 if (style & SS_NOPREFIX)
444 wFormat |= DT_NOPREFIX;
446 if (infoPtr->hFont) SelectObject( hdc, infoPtr->hFont );
448 if ((style & SS_NOPREFIX) || ((style & SS_TYPEMASK) != SS_SIMPLE))
450 hBrush = SendMessageW( GetParent(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
451 hdc, wndPtr->hwndSelf );
452 if (!hBrush) /* did the app forget to call defwindowproc ? */
453 hBrush = DefWindowProcW(GetParent(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
454 hdc, wndPtr->hwndSelf);
455 FillRect( hdc, &rc, hBrush );
457 if (!IsWindowEnabled(wndPtr->hwndSelf))
458 SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
460 if (wndPtr->text) DrawTextW( hdc, wndPtr->text, -1, &rc, wFormat );
463 static void STATIC_PaintRectfn( WND *wndPtr, HDC hdc )
465 RECT rc;
466 HBRUSH hBrush;
468 GetClientRect( wndPtr->hwndSelf, &rc);
470 switch (wndPtr->dwStyle & SS_TYPEMASK)
472 case SS_BLACKRECT:
473 hBrush = CreateSolidBrush(color_windowframe);
474 FillRect( hdc, &rc, hBrush );
475 break;
476 case SS_GRAYRECT:
477 hBrush = CreateSolidBrush(color_background);
478 FillRect( hdc, &rc, hBrush );
479 break;
480 case SS_WHITERECT:
481 hBrush = CreateSolidBrush(color_window);
482 FillRect( hdc, &rc, hBrush );
483 break;
484 case SS_BLACKFRAME:
485 hBrush = CreateSolidBrush(color_windowframe);
486 FrameRect( hdc, &rc, hBrush );
487 break;
488 case SS_GRAYFRAME:
489 hBrush = CreateSolidBrush(color_background);
490 FrameRect( hdc, &rc, hBrush );
491 break;
492 case SS_WHITEFRAME:
493 hBrush = CreateSolidBrush(color_window);
494 FrameRect( hdc, &rc, hBrush );
495 break;
496 default:
497 return;
499 DeleteObject( hBrush );
503 static void STATIC_PaintIconfn( WND *wndPtr, HDC hdc )
505 RECT rc;
506 HBRUSH hbrush;
507 STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
509 GetClientRect( wndPtr->hwndSelf, &rc );
510 hbrush = SendMessageW( GetParent(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
511 hdc, wndPtr->hwndSelf );
512 FillRect( hdc, &rc, hbrush );
513 if (infoPtr->hIcon) DrawIcon( hdc, rc.left, rc.top, infoPtr->hIcon );
516 static void STATIC_PaintBitmapfn(WND *wndPtr, HDC hdc )
518 RECT rc;
519 HBRUSH hbrush;
520 STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
521 HDC hMemDC;
522 HBITMAP oldbitmap;
524 GetClientRect( wndPtr->hwndSelf, &rc );
525 hbrush = SendMessageW( GetParent(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
526 hdc, wndPtr->hwndSelf );
527 FillRect( hdc, &rc, hbrush );
529 if (infoPtr->hIcon) {
530 BITMAP bm;
531 SIZE sz;
533 if(GetObjectType(infoPtr->hIcon) != OBJ_BITMAP)
534 return;
535 if (!(hMemDC = CreateCompatibleDC( hdc ))) return;
536 GetObjectW(infoPtr->hIcon, sizeof(bm), &bm);
537 GetBitmapDimensionEx(infoPtr->hIcon, &sz);
538 oldbitmap = SelectObject(hMemDC, infoPtr->hIcon);
539 BitBlt(hdc, sz.cx, sz.cy, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0,
540 SRCCOPY);
541 SelectObject(hMemDC, oldbitmap);
542 DeleteDC(hMemDC);
547 static void STATIC_PaintEtchedfn( WND *wndPtr, HDC hdc )
549 RECT rc;
551 if (TWEAK_WineLook == WIN31_LOOK)
552 return;
554 GetClientRect( wndPtr->hwndSelf, &rc );
555 switch (wndPtr->dwStyle & SS_TYPEMASK)
557 case SS_ETCHEDHORZ:
558 DrawEdge(hdc,&rc,EDGE_ETCHED,BF_TOP|BF_BOTTOM);
559 break;
560 case SS_ETCHEDVERT:
561 DrawEdge(hdc,&rc,EDGE_ETCHED,BF_LEFT|BF_RIGHT);
562 break;
563 case SS_ETCHEDFRAME:
564 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_RECT);
565 break;