Release 20031118.
[wine/multimedia.git] / controls / static.c
blobc020d4c339bd2f4c9753516c1c2768d414a071dc
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "wine/winuser16.h"
27 #include "wownt32.h"
28 #include "cursoricon.h"
29 #include "controls.h"
30 #include "user.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(static);
35 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style );
36 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style );
37 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style );
38 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style );
39 static void STATIC_PaintBitmapfn( HWND hwnd, HDC hdc, DWORD style );
40 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style );
41 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
42 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
44 static COLORREF color_windowframe, color_background, color_window;
46 /* offsets for GetWindowLong for static private information */
47 #define HFONT_GWL_OFFSET 0
48 #define HICON_GWL_OFFSET (sizeof(HFONT))
49 #define STATIC_EXTRA_BYTES (HICON_GWL_OFFSET + sizeof(HICON))
51 typedef void (*pfPaint)( HWND hwnd, HDC hdc, DWORD style );
53 static pfPaint staticPaintFunc[SS_TYPEMASK+1] =
55 STATIC_PaintTextfn, /* SS_LEFT */
56 STATIC_PaintTextfn, /* SS_CENTER */
57 STATIC_PaintTextfn, /* SS_RIGHT */
58 STATIC_PaintIconfn, /* SS_ICON */
59 STATIC_PaintRectfn, /* SS_BLACKRECT */
60 STATIC_PaintRectfn, /* SS_GRAYRECT */
61 STATIC_PaintRectfn, /* SS_WHITERECT */
62 STATIC_PaintRectfn, /* SS_BLACKFRAME */
63 STATIC_PaintRectfn, /* SS_GRAYFRAME */
64 STATIC_PaintRectfn, /* SS_WHITEFRAME */
65 NULL, /* SS_USERITEM */
66 STATIC_PaintTextfn, /* SS_SIMPLE */
67 STATIC_PaintTextfn, /* SS_LEFTNOWORDWRAP */
68 STATIC_PaintOwnerDrawfn, /* SS_OWNERDRAW */
69 STATIC_PaintBitmapfn, /* SS_BITMAP */
70 NULL, /* SS_ENHMETAFILE */
71 STATIC_PaintEtchedfn, /* SS_ETCHEDHORIZ */
72 STATIC_PaintEtchedfn, /* SS_ETCHEDVERT */
73 STATIC_PaintEtchedfn, /* SS_ETCHEDFRAME */
77 /*********************************************************************
78 * static class descriptor
80 const struct builtin_class_descr STATIC_builtin_class =
82 "Static", /* name */
83 CS_GLOBALCLASS | CS_DBLCLKS | CS_PARENTDC, /* style */
84 StaticWndProcA, /* procA */
85 StaticWndProcW, /* procW */
86 STATIC_EXTRA_BYTES, /* extra */
87 IDC_ARROW, /* cursor */
88 0 /* brush */
92 /***********************************************************************
93 * STATIC_SetIcon
95 * Set the icon for an SS_ICON control.
97 static HICON STATIC_SetIcon( HWND hwnd, HICON hicon, DWORD style )
99 HICON prevIcon;
100 CURSORICONINFO *info = hicon?(CURSORICONINFO *) GlobalLock16(HICON_16(hicon)):NULL;
102 if ((style & SS_TYPEMASK) != SS_ICON) return 0;
103 if (hicon && !info) {
104 ERR("huh? hicon!=0, but info=0???\n");
105 return 0;
107 prevIcon = (HICON)SetWindowLongA( hwnd, HICON_GWL_OFFSET, (LONG)hicon );
108 if (hicon)
110 SetWindowPos( hwnd, 0, 0, 0, info->nWidth, info->nHeight,
111 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
112 GlobalUnlock16(HICON_16(hicon));
114 return prevIcon;
117 /***********************************************************************
118 * STATIC_SetBitmap
120 * Set the bitmap for an SS_BITMAP control.
122 static HBITMAP STATIC_SetBitmap( HWND hwnd, HBITMAP hBitmap, DWORD style )
124 HBITMAP hOldBitmap;
126 if ((style & SS_TYPEMASK) != SS_BITMAP) return 0;
127 if (hBitmap && GetObjectType(hBitmap) != OBJ_BITMAP) {
128 ERR("huh? hBitmap!=0, but not bitmap\n");
129 return 0;
131 hOldBitmap = (HBITMAP)SetWindowLongA( hwnd, HICON_GWL_OFFSET, (LONG)hBitmap );
132 if (hBitmap)
134 BITMAP bm;
135 GetObjectW(hBitmap, sizeof(bm), &bm);
136 SetWindowPos( hwnd, 0, 0, 0, bm.bmWidth, bm.bmHeight,
137 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
139 return hOldBitmap;
142 /***********************************************************************
143 * STATIC_LoadIconA
145 * Load the icon for an SS_ICON control.
147 static HICON STATIC_LoadIconA( HWND hwnd, LPCSTR name )
149 HINSTANCE hInstance = (HINSTANCE)GetWindowLongA( hwnd, GWL_HINSTANCE );
150 HICON hicon = LoadIconA( hInstance, name );
151 if (!hicon) hicon = LoadIconA( 0, name );
152 return hicon;
155 /***********************************************************************
156 * STATIC_LoadIconW
158 * Load the icon for an SS_ICON control.
160 static HICON STATIC_LoadIconW( HWND hwnd, LPCWSTR name )
162 HINSTANCE hInstance = (HINSTANCE)GetWindowLongA( hwnd, GWL_HINSTANCE );
163 HICON hicon = LoadIconW( hInstance, name );
164 if (!hicon) hicon = LoadIconW( 0, name );
165 return hicon;
168 /***********************************************************************
169 * STATIC_LoadBitmapA
171 * Load the bitmap for an SS_BITMAP control.
173 static HBITMAP STATIC_LoadBitmapA( HWND hwnd, LPCSTR name )
175 HINSTANCE hInstance = (HINSTANCE)GetWindowLongA( hwnd, GWL_HINSTANCE );
176 HBITMAP hbitmap = LoadBitmapA( hInstance, name );
177 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
178 hbitmap = LoadBitmapA( 0, name );
179 return hbitmap;
182 /***********************************************************************
183 * STATIC_LoadBitmapW
185 * Load the bitmap for an SS_BITMAP control.
187 static HBITMAP STATIC_LoadBitmapW( HWND hwnd, LPCWSTR name )
189 HINSTANCE hInstance = (HINSTANCE)GetWindowLongA( hwnd, GWL_HINSTANCE );
190 HBITMAP hbitmap = LoadBitmapW( hInstance, name );
191 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
192 hbitmap = LoadBitmapW( 0, name );
193 return hbitmap;
196 /***********************************************************************
197 * STATIC_TryPaintFcn
199 * Try to immediately paint the control.
201 static VOID STATIC_TryPaintFcn(HWND hwnd, LONG full_style)
203 LONG style = full_style & SS_TYPEMASK;
204 RECT rc;
206 GetClientRect( hwnd, &rc );
207 if (!IsRectEmpty(&rc) && IsWindowVisible(hwnd) && staticPaintFunc[style])
209 HDC hdc;
210 hdc = GetDC( hwnd );
211 (staticPaintFunc[style])( hwnd, hdc, full_style );
212 ReleaseDC( hwnd, hdc );
216 /***********************************************************************
217 * StaticWndProc_common
219 static LRESULT StaticWndProc_common( HWND hwnd, UINT uMsg, WPARAM wParam,
220 LPARAM lParam, BOOL unicode )
222 LRESULT lResult = 0;
223 LONG full_style = GetWindowLongA( hwnd, GWL_STYLE );
224 LONG style = full_style & SS_TYPEMASK;
226 switch (uMsg)
228 case WM_CREATE:
229 if (style < 0L || style > SS_TYPEMASK)
231 ERR("Unknown style 0x%02lx\n", style );
232 return -1;
234 /* initialise colours */
235 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
236 color_background = GetSysColor(COLOR_BACKGROUND);
237 color_window = GetSysColor(COLOR_WINDOW);
238 break;
240 case WM_NCDESTROY:
241 if (style == SS_ICON) {
243 * FIXME
244 * DestroyIcon32( STATIC_SetIcon( wndPtr, 0 ) );
246 * We don't want to do this yet because DestroyIcon32 is broken. If the icon
247 * had already been loaded by the application the last thing we want to do is
248 * GlobalFree16 the handle.
250 break;
252 else return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
253 DefWindowProcA(hwnd, uMsg, wParam, lParam);
255 case WM_PAINT:
257 PAINTSTRUCT ps;
258 HDC hdc = wParam ? (HDC)wParam : BeginPaint(hwnd, &ps);
259 if (staticPaintFunc[style])
260 (staticPaintFunc[style])( hwnd, hdc, full_style );
261 if (!wParam) EndPaint(hwnd, &ps);
263 break;
265 case WM_ENABLE:
266 InvalidateRect(hwnd, NULL, TRUE);
267 break;
269 case WM_SYSCOLORCHANGE:
270 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
271 color_background = GetSysColor(COLOR_BACKGROUND);
272 color_window = GetSysColor(COLOR_WINDOW);
273 InvalidateRect(hwnd, NULL, TRUE);
274 break;
276 case WM_NCCREATE:
277 if ((TWEAK_WineLook > WIN31_LOOK) && (full_style & SS_SUNKEN))
278 SetWindowLongA( hwnd, GWL_EXSTYLE,
279 GetWindowLongA( hwnd, GWL_EXSTYLE ) | WS_EX_STATICEDGE );
281 if(unicode)
282 lParam = (LPARAM)(((LPCREATESTRUCTW)lParam)->lpszName);
283 else
284 lParam = (LPARAM)(((LPCREATESTRUCTA)lParam)->lpszName);
285 /* fall through */
286 case WM_SETTEXT:
287 switch (style) {
288 case SS_ICON:
290 HICON hIcon;
291 if(unicode)
292 hIcon = STATIC_LoadIconW(hwnd, (LPCWSTR)lParam);
293 else
294 hIcon = STATIC_LoadIconA(hwnd, (LPCSTR)lParam);
295 /* FIXME : should we also return the previous hIcon here ??? */
296 STATIC_SetIcon(hwnd, hIcon, style);
297 break;
299 case SS_BITMAP:
301 HBITMAP hBitmap;
302 if(unicode)
303 hBitmap = STATIC_LoadBitmapW(hwnd, (LPCWSTR)lParam);
304 else
305 hBitmap = STATIC_LoadBitmapA(hwnd, (LPCSTR)lParam);
306 STATIC_SetBitmap(hwnd, hBitmap, style);
307 break;
309 case SS_LEFT:
310 case SS_CENTER:
311 case SS_RIGHT:
312 case SS_SIMPLE:
313 case SS_LEFTNOWORDWRAP:
315 if (HIWORD(lParam))
317 if(unicode)
318 lResult = DefWindowProcW( hwnd, WM_SETTEXT, wParam, lParam );
319 else
320 lResult = DefWindowProcA( hwnd, WM_SETTEXT, wParam, lParam );
322 if (uMsg == WM_SETTEXT)
323 STATIC_TryPaintFcn( hwnd, full_style );
324 break;
326 default:
327 if (HIWORD(lParam))
329 if(unicode)
330 lResult = DefWindowProcW( hwnd, WM_SETTEXT, wParam, lParam );
331 else
332 lResult = DefWindowProcA( hwnd, WM_SETTEXT, wParam, lParam );
334 if(uMsg == WM_SETTEXT)
335 InvalidateRect(hwnd, NULL, TRUE);
337 return 1; /* success. FIXME: check text length */
339 case WM_SETFONT:
340 if ((style == SS_ICON) || (style == SS_BITMAP)) return 0;
341 SetWindowLongA( hwnd, HFONT_GWL_OFFSET, wParam );
342 if (LOWORD(lParam))
343 InvalidateRect( hwnd, NULL, TRUE );
344 break;
346 case WM_GETFONT:
347 return GetWindowLongA( hwnd, HFONT_GWL_OFFSET );
349 case WM_NCHITTEST:
350 if (full_style & SS_NOTIFY)
351 return HTCLIENT;
352 else
353 return HTTRANSPARENT;
355 case WM_GETDLGCODE:
356 return DLGC_STATIC;
358 case WM_LBUTTONDOWN:
359 case WM_NCLBUTTONDOWN:
360 if (full_style & SS_NOTIFY)
361 SendMessageW( GetParent(hwnd), WM_COMMAND,
362 MAKEWPARAM( GetWindowLongW(hwnd,GWL_ID), STN_CLICKED ), (LPARAM)hwnd);
363 return 0;
365 case WM_LBUTTONDBLCLK:
366 case WM_NCLBUTTONDBLCLK:
367 if (full_style & SS_NOTIFY)
368 SendMessageW( GetParent(hwnd), WM_COMMAND,
369 MAKEWPARAM( GetWindowLongW(hwnd,GWL_ID), STN_DBLCLK ), (LPARAM)hwnd);
370 return 0;
372 case STM_GETIMAGE:
373 case STM_GETICON16:
374 case STM_GETICON:
375 return GetWindowLongA( hwnd, HICON_GWL_OFFSET );
377 case STM_SETIMAGE:
378 switch(wParam) {
379 case IMAGE_BITMAP:
380 lResult = (LRESULT)STATIC_SetBitmap( hwnd, (HBITMAP)lParam, style );
381 break;
382 case IMAGE_ICON:
383 lResult = (LRESULT)STATIC_SetIcon( hwnd, (HICON)lParam, style );
384 break;
385 default:
386 FIXME("STM_SETIMAGE: Unhandled type %x\n", wParam);
387 break;
389 InvalidateRect( hwnd, NULL, TRUE );
390 break;
392 case STM_SETICON16:
393 case STM_SETICON:
394 lResult = (LRESULT)STATIC_SetIcon( hwnd, (HICON)wParam, style );
395 InvalidateRect( hwnd, NULL, TRUE );
396 break;
398 default:
399 return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
400 DefWindowProcA(hwnd, uMsg, wParam, lParam);
402 return lResult;
405 /***********************************************************************
406 * StaticWndProcA
408 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
410 if (!IsWindow( hWnd )) return 0;
411 return StaticWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
414 /***********************************************************************
415 * StaticWndProcW
417 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
419 if (!IsWindow( hWnd )) return 0;
420 return StaticWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
423 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style )
425 DRAWITEMSTRUCT dis;
426 LONG id = GetWindowLongA( hwnd, GWL_ID );
428 dis.CtlType = ODT_STATIC;
429 dis.CtlID = id;
430 dis.itemID = 0;
431 dis.itemAction = ODA_DRAWENTIRE;
432 dis.itemState = 0;
433 dis.hwndItem = hwnd;
434 dis.hDC = hdc;
435 dis.itemData = 0;
436 GetClientRect( hwnd, &dis.rcItem );
438 SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, (WPARAM)hdc, (LPARAM)hwnd );
439 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
442 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style )
444 RECT rc;
445 HBRUSH hBrush;
446 HFONT hFont;
447 WORD wFormat;
448 INT len;
449 WCHAR *text;
451 GetClientRect( hwnd, &rc);
453 switch (style & SS_TYPEMASK)
455 case SS_LEFT:
456 wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
457 break;
459 case SS_CENTER:
460 wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
461 break;
463 case SS_RIGHT:
464 wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
465 break;
467 case SS_SIMPLE:
468 wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOCLIP;
469 break;
471 case SS_LEFTNOWORDWRAP:
472 wFormat = DT_LEFT | DT_EXPANDTABS | DT_VCENTER;
473 break;
475 default:
476 return;
479 if (style & SS_NOPREFIX)
480 wFormat |= DT_NOPREFIX;
482 if ((hFont = (HFONT)GetWindowLongA( hwnd, HFONT_GWL_OFFSET ))) SelectObject( hdc, hFont );
484 if ((style & SS_NOPREFIX) || ((style & SS_TYPEMASK) != SS_SIMPLE))
486 hBrush = (HBRUSH)SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC,
487 (WPARAM)hdc, (LPARAM)hwnd );
488 if (!hBrush) /* did the app forget to call defwindowproc ? */
489 hBrush = (HBRUSH)DefWindowProcW(GetParent(hwnd), WM_CTLCOLORSTATIC,
490 (WPARAM)hdc, (LPARAM)hwnd);
491 FillRect( hdc, &rc, hBrush );
493 if (!IsWindowEnabled(hwnd)) SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
495 if (!(len = SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 ))) return;
496 if (!(text = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return;
497 SendMessageW( hwnd, WM_GETTEXT, len + 1, (LPARAM)text );
498 DrawTextW( hdc, text, -1, &rc, wFormat );
499 HeapFree( GetProcessHeap(), 0, text );
502 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style )
504 RECT rc;
505 HBRUSH hBrush;
507 GetClientRect( hwnd, &rc);
509 switch (style & SS_TYPEMASK)
511 case SS_BLACKRECT:
512 hBrush = CreateSolidBrush(color_windowframe);
513 FillRect( hdc, &rc, hBrush );
514 break;
515 case SS_GRAYRECT:
516 hBrush = CreateSolidBrush(color_background);
517 FillRect( hdc, &rc, hBrush );
518 break;
519 case SS_WHITERECT:
520 hBrush = CreateSolidBrush(color_window);
521 FillRect( hdc, &rc, hBrush );
522 break;
523 case SS_BLACKFRAME:
524 hBrush = CreateSolidBrush(color_windowframe);
525 FrameRect( hdc, &rc, hBrush );
526 break;
527 case SS_GRAYFRAME:
528 hBrush = CreateSolidBrush(color_background);
529 FrameRect( hdc, &rc, hBrush );
530 break;
531 case SS_WHITEFRAME:
532 hBrush = CreateSolidBrush(color_window);
533 FrameRect( hdc, &rc, hBrush );
534 break;
535 default:
536 return;
538 DeleteObject( hBrush );
542 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style )
544 RECT rc;
545 HBRUSH hbrush;
546 HICON hIcon;
548 GetClientRect( hwnd, &rc );
549 hbrush = (HBRUSH)SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC,
550 (WPARAM)hdc, (LPARAM)hwnd );
551 FillRect( hdc, &rc, hbrush );
552 if ((hIcon = (HICON)GetWindowLongA( hwnd, HICON_GWL_OFFSET )))
553 DrawIcon( hdc, rc.left, rc.top, hIcon );
556 static void STATIC_PaintBitmapfn(HWND hwnd, HDC hdc, DWORD style )
558 RECT rc;
559 HBRUSH hbrush;
560 HDC hMemDC;
561 HBITMAP hBitmap, oldbitmap;
563 GetClientRect( hwnd, &rc );
564 hbrush = (HBRUSH)SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC,
565 (WPARAM)hdc, (LPARAM)hwnd );
566 FillRect( hdc, &rc, hbrush );
568 if ((hBitmap = (HBITMAP)GetWindowLongA( hwnd, HICON_GWL_OFFSET )))
570 BITMAP bm;
571 SIZE sz;
573 if(GetObjectType(hBitmap) != OBJ_BITMAP) return;
574 if (!(hMemDC = CreateCompatibleDC( hdc ))) return;
575 GetObjectW(hBitmap, sizeof(bm), &bm);
576 GetBitmapDimensionEx(hBitmap, &sz);
577 oldbitmap = SelectObject(hMemDC, hBitmap);
578 BitBlt(hdc, sz.cx, sz.cy, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0,
579 SRCCOPY);
580 SelectObject(hMemDC, oldbitmap);
581 DeleteDC(hMemDC);
586 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style )
588 RECT rc;
590 if (TWEAK_WineLook == WIN31_LOOK)
591 return;
593 GetClientRect( hwnd, &rc );
594 switch (style & SS_TYPEMASK)
596 case SS_ETCHEDHORZ:
597 DrawEdge(hdc,&rc,EDGE_ETCHED,BF_TOP|BF_BOTTOM);
598 break;
599 case SS_ETCHEDVERT:
600 DrawEdge(hdc,&rc,EDGE_ETCHED,BF_LEFT|BF_RIGHT);
601 break;
602 case SS_ETCHEDFRAME:
603 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_RECT);
604 break;