Fixed a couple of bugs in _except_handler3.
[wine/multimedia.git] / controls / static.c
blob8724a8449db34e81e23820e7a4c83ba8413cf690
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 "cursoricon.h"
12 #include "controls.h"
13 #include "user.h"
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(static);
18 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style );
19 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style );
20 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style );
21 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style );
22 static void STATIC_PaintBitmapfn( HWND hwnd, HDC hdc, DWORD style );
23 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style );
24 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
25 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
27 static COLORREF color_windowframe, color_background, color_window;
29 /* offsets for GetWindowLong for static private information */
30 #define HFONT_GWL_OFFSET 0
31 #define HICON_GWL_OFFSET (sizeof(HFONT))
32 #define STATIC_EXTRA_BYTES (HICON_GWL_OFFSET + sizeof(HICON))
34 typedef void (*pfPaint)( HWND hwnd, HDC hdc, DWORD style );
36 static pfPaint staticPaintFunc[SS_TYPEMASK+1] =
38 STATIC_PaintTextfn, /* SS_LEFT */
39 STATIC_PaintTextfn, /* SS_CENTER */
40 STATIC_PaintTextfn, /* SS_RIGHT */
41 STATIC_PaintIconfn, /* SS_ICON */
42 STATIC_PaintRectfn, /* SS_BLACKRECT */
43 STATIC_PaintRectfn, /* SS_GRAYRECT */
44 STATIC_PaintRectfn, /* SS_WHITERECT */
45 STATIC_PaintRectfn, /* SS_BLACKFRAME */
46 STATIC_PaintRectfn, /* SS_GRAYFRAME */
47 STATIC_PaintRectfn, /* SS_WHITEFRAME */
48 NULL, /* Not defined */
49 STATIC_PaintTextfn, /* SS_SIMPLE */
50 STATIC_PaintTextfn, /* SS_LEFTNOWORDWRAP */
51 STATIC_PaintOwnerDrawfn, /* SS_OWNERDRAW */
52 STATIC_PaintBitmapfn, /* SS_BITMAP */
53 NULL, /* SS_ENHMETAFILE */
54 STATIC_PaintEtchedfn, /* SS_ETCHEDHORIZ */
55 STATIC_PaintEtchedfn, /* SS_ETCHEDVERT */
56 STATIC_PaintEtchedfn, /* SS_ETCHEDFRAME */
60 /*********************************************************************
61 * static class descriptor
63 const struct builtin_class_descr STATIC_builtin_class =
65 "Static", /* name */
66 CS_GLOBALCLASS | CS_DBLCLKS | CS_PARENTDC, /* style */
67 StaticWndProcA, /* procA */
68 StaticWndProcW, /* procW */
69 STATIC_EXTRA_BYTES, /* extra */
70 IDC_ARROWA, /* cursor */
71 0 /* brush */
75 /***********************************************************************
76 * STATIC_SetIcon
78 * Set the icon for an SS_ICON control.
80 static HICON STATIC_SetIcon( HWND hwnd, HICON hicon, DWORD style )
82 HICON prevIcon;
83 CURSORICONINFO *info = hicon?(CURSORICONINFO *) GlobalLock16( hicon ):NULL;
85 if ((style & SS_TYPEMASK) != SS_ICON) return 0;
86 if (hicon && !info) {
87 ERR("huh? hicon!=0, but info=0???\n");
88 return 0;
90 prevIcon = SetWindowLongA( hwnd, HICON_GWL_OFFSET, hicon );
91 if (hicon)
93 SetWindowPos( hwnd, 0, 0, 0, info->nWidth, info->nHeight,
94 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
95 GlobalUnlock16( hicon );
97 return prevIcon;
100 /***********************************************************************
101 * STATIC_SetBitmap
103 * Set the bitmap for an SS_BITMAP control.
105 static HBITMAP STATIC_SetBitmap( HWND hwnd, HBITMAP hBitmap, DWORD style )
107 HBITMAP hOldBitmap;
109 if ((style & SS_TYPEMASK) != SS_BITMAP) return 0;
110 if (hBitmap && GetObjectType(hBitmap) != OBJ_BITMAP) {
111 ERR("huh? hBitmap!=0, but not bitmap\n");
112 return 0;
114 hOldBitmap = SetWindowLongA( hwnd, HICON_GWL_OFFSET, hBitmap );
115 if (hBitmap)
117 BITMAP bm;
118 GetObjectW(hBitmap, sizeof(bm), &bm);
119 SetWindowPos( hwnd, 0, 0, 0, bm.bmWidth, bm.bmHeight,
120 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
122 return hOldBitmap;
125 /***********************************************************************
126 * STATIC_LoadIconA
128 * Load the icon for an SS_ICON control.
130 static HICON STATIC_LoadIconA( HWND hwnd, LPCSTR name )
132 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
133 HICON hicon = LoadIconA( hInstance, name );
134 if (!hicon) hicon = LoadIconA( 0, name );
135 return hicon;
138 /***********************************************************************
139 * STATIC_LoadIconW
141 * Load the icon for an SS_ICON control.
143 static HICON STATIC_LoadIconW( HWND hwnd, LPCWSTR name )
145 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
146 HICON hicon = LoadIconW( hInstance, name );
147 if (!hicon) hicon = LoadIconW( 0, name );
148 return hicon;
151 /***********************************************************************
152 * STATIC_LoadBitmapA
154 * Load the bitmap for an SS_BITMAP control.
156 static HBITMAP STATIC_LoadBitmapA( HWND hwnd, LPCSTR name )
158 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
159 HBITMAP hbitmap = LoadBitmapA( hInstance, name );
160 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
161 hbitmap = LoadBitmapA( 0, name );
162 return hbitmap;
165 /***********************************************************************
166 * STATIC_LoadBitmapW
168 * Load the bitmap for an SS_BITMAP control.
170 static HBITMAP STATIC_LoadBitmapW( HWND hwnd, LPCWSTR name )
172 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
173 HBITMAP hbitmap = LoadBitmapW( hInstance, name );
174 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
175 hbitmap = LoadBitmapW( 0, name );
176 return hbitmap;
179 /***********************************************************************
180 * StaticWndProc_common
182 static LRESULT StaticWndProc_common( HWND hwnd, UINT uMsg, WPARAM wParam,
183 LPARAM lParam, BOOL unicode )
185 LRESULT lResult = 0;
186 LONG full_style = GetWindowLongA( hwnd, GWL_STYLE );
187 LONG style = full_style & SS_TYPEMASK;
189 switch (uMsg)
191 case WM_CREATE:
192 if (style < 0L || style > SS_TYPEMASK)
194 ERR("Unknown style 0x%02lx\n", style );
195 return -1;
197 /* initialise colours */
198 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
199 color_background = GetSysColor(COLOR_BACKGROUND);
200 color_window = GetSysColor(COLOR_WINDOW);
201 break;
203 case WM_NCDESTROY:
204 if (style == SS_ICON) {
206 * FIXME
207 * DestroyIcon32( STATIC_SetIcon( wndPtr, 0 ) );
209 * We don't want to do this yet because DestroyIcon32 is broken. If the icon
210 * had already been loaded by the application the last thing we want to do is
211 * GlobalFree16 the handle.
213 break;
215 else return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
216 DefWindowProcA(hwnd, uMsg, wParam, lParam);
218 case WM_PAINT:
220 PAINTSTRUCT ps;
221 BeginPaint(hwnd, &ps);
222 if (staticPaintFunc[style])
223 (staticPaintFunc[style])( hwnd, ps.hdc, full_style );
224 EndPaint(hwnd, &ps);
226 break;
228 case WM_ENABLE:
229 InvalidateRect(hwnd, NULL, FALSE);
230 break;
232 case WM_SYSCOLORCHANGE:
233 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
234 color_background = GetSysColor(COLOR_BACKGROUND);
235 color_window = GetSysColor(COLOR_WINDOW);
236 InvalidateRect(hwnd, NULL, TRUE);
237 break;
239 case WM_NCCREATE:
240 if ((TWEAK_WineLook > WIN31_LOOK) && (full_style & SS_SUNKEN))
241 SetWindowLongA( hwnd, GWL_EXSTYLE,
242 GetWindowLongA( hwnd, GWL_EXSTYLE ) | WS_EX_STATICEDGE );
244 if(unicode)
245 lParam = (LPARAM)(((LPCREATESTRUCTW)lParam)->lpszName);
246 else
247 lParam = (LPARAM)(((LPCREATESTRUCTA)lParam)->lpszName);
248 /* fall through */
249 case WM_SETTEXT:
250 if (style == SS_ICON)
252 HICON hIcon;
253 if(unicode)
254 hIcon = STATIC_LoadIconW(hwnd, (LPCWSTR)lParam);
255 else
256 hIcon = STATIC_LoadIconA(hwnd, (LPCSTR)lParam);
257 /* FIXME : should we also return the previous hIcon here ??? */
258 STATIC_SetIcon(hwnd, hIcon, style);
260 else if (style == SS_BITMAP)
262 HBITMAP hBitmap;
263 if(unicode)
264 hBitmap = STATIC_LoadBitmapW(hwnd, (LPCWSTR)lParam);
265 else
266 hBitmap = STATIC_LoadBitmapA(hwnd, (LPCSTR)lParam);
267 STATIC_SetBitmap(hwnd, hBitmap, style);
269 else if (HIWORD(lParam))
271 if(unicode)
272 lResult = DefWindowProcW( hwnd, WM_SETTEXT, wParam, lParam );
273 else
274 lResult = DefWindowProcA( hwnd, WM_SETTEXT, wParam, lParam );
276 if(uMsg == WM_SETTEXT)
277 InvalidateRect(hwnd, NULL, FALSE);
278 return 1; /* success. FIXME: check text length */
280 case WM_SETFONT:
281 if ((style == SS_ICON) || (style == SS_BITMAP)) return 0;
282 SetWindowLongA( hwnd, HFONT_GWL_OFFSET, wParam );
283 if (LOWORD(lParam))
284 InvalidateRect( hwnd, NULL, FALSE );
285 break;
287 case WM_GETFONT:
288 return GetWindowLongA( hwnd, HFONT_GWL_OFFSET );
290 case WM_NCHITTEST:
291 if (full_style & SS_NOTIFY)
292 return HTCLIENT;
293 else
294 return HTTRANSPARENT;
296 case WM_GETDLGCODE:
297 return DLGC_STATIC;
299 case STM_GETIMAGE:
300 case STM_GETICON16:
301 case STM_GETICON:
302 return GetWindowLongA( hwnd, HICON_GWL_OFFSET );
304 case STM_SETIMAGE:
305 switch(wParam) {
306 case IMAGE_BITMAP:
307 lResult = STATIC_SetBitmap( hwnd, (HBITMAP)lParam, style );
308 break;
309 case IMAGE_ICON:
310 lResult = STATIC_SetIcon( hwnd, (HICON)lParam, style );
311 break;
312 default:
313 FIXME("STM_SETIMAGE: Unhandled type %x\n", wParam);
314 break;
316 InvalidateRect( hwnd, NULL, FALSE );
317 break;
319 case STM_SETICON16:
320 case STM_SETICON:
321 lResult = STATIC_SetIcon( hwnd, (HICON)wParam, style );
322 InvalidateRect( hwnd, NULL, FALSE );
323 break;
325 default:
326 return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
327 DefWindowProcA(hwnd, uMsg, wParam, lParam);
329 return lResult;
332 /***********************************************************************
333 * StaticWndProcA
335 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
337 if (!IsWindow( hWnd )) return 0;
338 return StaticWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
341 /***********************************************************************
342 * StaticWndProcW
344 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
346 if (!IsWindow( hWnd )) return 0;
347 return StaticWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
350 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style )
352 DRAWITEMSTRUCT dis;
353 LONG id = GetWindowLongA( hwnd, GWL_ID );
355 dis.CtlType = ODT_STATIC;
356 dis.CtlID = id;
357 dis.itemID = 0;
358 dis.itemAction = ODA_DRAWENTIRE;
359 dis.itemState = 0;
360 dis.hwndItem = hwnd;
361 dis.hDC = hdc;
362 dis.itemData = 0;
363 GetClientRect( hwnd, &dis.rcItem );
365 SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
366 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
369 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style )
371 RECT rc;
372 HBRUSH hBrush;
373 HFONT hFont;
374 WORD wFormat;
375 INT len;
376 WCHAR *text;
378 GetClientRect( hwnd, &rc);
380 switch (style & SS_TYPEMASK)
382 case SS_LEFT:
383 wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
384 break;
386 case SS_CENTER:
387 wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
388 break;
390 case SS_RIGHT:
391 wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
392 break;
394 case SS_SIMPLE:
395 wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOCLIP;
396 break;
398 case SS_LEFTNOWORDWRAP:
399 wFormat = DT_LEFT | DT_EXPANDTABS | DT_VCENTER;
400 break;
402 default:
403 return;
406 if (style & SS_NOPREFIX)
407 wFormat |= DT_NOPREFIX;
409 if ((hFont = GetWindowLongA( hwnd, HFONT_GWL_OFFSET ))) SelectObject( hdc, hFont );
411 if ((style & SS_NOPREFIX) || ((style & SS_TYPEMASK) != SS_SIMPLE))
413 hBrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
414 if (!hBrush) /* did the app forget to call defwindowproc ? */
415 hBrush = DefWindowProcW(GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd);
416 FillRect( hdc, &rc, hBrush );
418 if (!IsWindowEnabled(hwnd)) SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
420 if (!(len = SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 ))) return;
421 if (!(text = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return;
422 SendMessageW( hwnd, WM_GETTEXT, len + 1, (LPARAM)text );
423 DrawTextW( hdc, text, -1, &rc, wFormat );
424 HeapFree( GetProcessHeap(), 0, text );
427 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style )
429 RECT rc;
430 HBRUSH hBrush;
432 GetClientRect( hwnd, &rc);
434 switch (style & SS_TYPEMASK)
436 case SS_BLACKRECT:
437 hBrush = CreateSolidBrush(color_windowframe);
438 FillRect( hdc, &rc, hBrush );
439 break;
440 case SS_GRAYRECT:
441 hBrush = CreateSolidBrush(color_background);
442 FillRect( hdc, &rc, hBrush );
443 break;
444 case SS_WHITERECT:
445 hBrush = CreateSolidBrush(color_window);
446 FillRect( hdc, &rc, hBrush );
447 break;
448 case SS_BLACKFRAME:
449 hBrush = CreateSolidBrush(color_windowframe);
450 FrameRect( hdc, &rc, hBrush );
451 break;
452 case SS_GRAYFRAME:
453 hBrush = CreateSolidBrush(color_background);
454 FrameRect( hdc, &rc, hBrush );
455 break;
456 case SS_WHITEFRAME:
457 hBrush = CreateSolidBrush(color_window);
458 FrameRect( hdc, &rc, hBrush );
459 break;
460 default:
461 return;
463 DeleteObject( hBrush );
467 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style )
469 RECT rc;
470 HBRUSH hbrush;
471 HICON hIcon;
473 GetClientRect( hwnd, &rc );
474 hbrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
475 FillRect( hdc, &rc, hbrush );
476 if ((hIcon = GetWindowLongA( hwnd, HICON_GWL_OFFSET )))
477 DrawIcon( hdc, rc.left, rc.top, hIcon );
480 static void STATIC_PaintBitmapfn(HWND hwnd, HDC hdc, DWORD style )
482 RECT rc;
483 HBRUSH hbrush;
484 HICON hIcon;
485 HDC hMemDC;
486 HBITMAP oldbitmap;
488 GetClientRect( hwnd, &rc );
489 hbrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
490 FillRect( hdc, &rc, hbrush );
492 if ((hIcon = GetWindowLongA( hwnd, HICON_GWL_OFFSET )))
494 BITMAP bm;
495 SIZE sz;
497 if(GetObjectType(hIcon) != OBJ_BITMAP) return;
498 if (!(hMemDC = CreateCompatibleDC( hdc ))) return;
499 GetObjectW(hIcon, sizeof(bm), &bm);
500 GetBitmapDimensionEx(hIcon, &sz);
501 oldbitmap = SelectObject(hMemDC, hIcon);
502 BitBlt(hdc, sz.cx, sz.cy, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0,
503 SRCCOPY);
504 SelectObject(hMemDC, oldbitmap);
505 DeleteDC(hMemDC);
510 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style )
512 RECT rc;
514 if (TWEAK_WineLook == WIN31_LOOK)
515 return;
517 GetClientRect( hwnd, &rc );
518 switch (style & SS_TYPEMASK)
520 case SS_ETCHEDHORZ:
521 DrawEdge(hdc,&rc,EDGE_ETCHED,BF_TOP|BF_BOTTOM);
522 break;
523 case SS_ETCHEDVERT:
524 DrawEdge(hdc,&rc,EDGE_ETCHED,BF_LEFT|BF_RIGHT);
525 break;
526 case SS_ETCHEDFRAME:
527 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_RECT);
528 break;