Added LGPL standard comment, and copyright notices where necessary.
[wine/multimedia.git] / controls / static.c
blobd573e118ff9c8e6de7ab6c5c6f3788d40583e860
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 "windef.h"
22 #include "wingdi.h"
23 #include "wine/winuser16.h"
24 #include "cursoricon.h"
25 #include "controls.h"
26 #include "user.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(static);
31 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style );
32 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style );
33 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style );
34 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style );
35 static void STATIC_PaintBitmapfn( HWND hwnd, HDC hdc, DWORD style );
36 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style );
37 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
38 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
40 static COLORREF color_windowframe, color_background, color_window;
42 /* offsets for GetWindowLong for static private information */
43 #define HFONT_GWL_OFFSET 0
44 #define HICON_GWL_OFFSET (sizeof(HFONT))
45 #define STATIC_EXTRA_BYTES (HICON_GWL_OFFSET + sizeof(HICON))
47 typedef void (*pfPaint)( HWND hwnd, HDC hdc, DWORD style );
49 static pfPaint staticPaintFunc[SS_TYPEMASK+1] =
51 STATIC_PaintTextfn, /* SS_LEFT */
52 STATIC_PaintTextfn, /* SS_CENTER */
53 STATIC_PaintTextfn, /* SS_RIGHT */
54 STATIC_PaintIconfn, /* SS_ICON */
55 STATIC_PaintRectfn, /* SS_BLACKRECT */
56 STATIC_PaintRectfn, /* SS_GRAYRECT */
57 STATIC_PaintRectfn, /* SS_WHITERECT */
58 STATIC_PaintRectfn, /* SS_BLACKFRAME */
59 STATIC_PaintRectfn, /* SS_GRAYFRAME */
60 STATIC_PaintRectfn, /* SS_WHITEFRAME */
61 NULL, /* Not defined */
62 STATIC_PaintTextfn, /* SS_SIMPLE */
63 STATIC_PaintTextfn, /* SS_LEFTNOWORDWRAP */
64 STATIC_PaintOwnerDrawfn, /* SS_OWNERDRAW */
65 STATIC_PaintBitmapfn, /* SS_BITMAP */
66 NULL, /* SS_ENHMETAFILE */
67 STATIC_PaintEtchedfn, /* SS_ETCHEDHORIZ */
68 STATIC_PaintEtchedfn, /* SS_ETCHEDVERT */
69 STATIC_PaintEtchedfn, /* SS_ETCHEDFRAME */
73 /*********************************************************************
74 * static class descriptor
76 const struct builtin_class_descr STATIC_builtin_class =
78 "Static", /* name */
79 CS_GLOBALCLASS | CS_DBLCLKS | CS_PARENTDC, /* style */
80 StaticWndProcA, /* procA */
81 StaticWndProcW, /* procW */
82 STATIC_EXTRA_BYTES, /* extra */
83 IDC_ARROWA, /* cursor */
84 0 /* brush */
88 /***********************************************************************
89 * STATIC_SetIcon
91 * Set the icon for an SS_ICON control.
93 static HICON STATIC_SetIcon( HWND hwnd, HICON hicon, DWORD style )
95 HICON prevIcon;
96 CURSORICONINFO *info = hicon?(CURSORICONINFO *) GlobalLock16( hicon ):NULL;
98 if ((style & SS_TYPEMASK) != SS_ICON) return 0;
99 if (hicon && !info) {
100 ERR("huh? hicon!=0, but info=0???\n");
101 return 0;
103 prevIcon = SetWindowLongA( hwnd, HICON_GWL_OFFSET, hicon );
104 if (hicon)
106 SetWindowPos( hwnd, 0, 0, 0, info->nWidth, info->nHeight,
107 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
108 GlobalUnlock16( hicon );
110 return prevIcon;
113 /***********************************************************************
114 * STATIC_SetBitmap
116 * Set the bitmap for an SS_BITMAP control.
118 static HBITMAP STATIC_SetBitmap( HWND hwnd, HBITMAP hBitmap, DWORD style )
120 HBITMAP hOldBitmap;
122 if ((style & SS_TYPEMASK) != SS_BITMAP) return 0;
123 if (hBitmap && GetObjectType(hBitmap) != OBJ_BITMAP) {
124 ERR("huh? hBitmap!=0, but not bitmap\n");
125 return 0;
127 hOldBitmap = SetWindowLongA( hwnd, HICON_GWL_OFFSET, hBitmap );
128 if (hBitmap)
130 BITMAP bm;
131 GetObjectW(hBitmap, sizeof(bm), &bm);
132 SetWindowPos( hwnd, 0, 0, 0, bm.bmWidth, bm.bmHeight,
133 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
135 return hOldBitmap;
138 /***********************************************************************
139 * STATIC_LoadIconA
141 * Load the icon for an SS_ICON control.
143 static HICON STATIC_LoadIconA( HWND hwnd, LPCSTR name )
145 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
146 HICON hicon = LoadIconA( hInstance, name );
147 if (!hicon) hicon = LoadIconA( 0, name );
148 return hicon;
151 /***********************************************************************
152 * STATIC_LoadIconW
154 * Load the icon for an SS_ICON control.
156 static HICON STATIC_LoadIconW( HWND hwnd, LPCWSTR name )
158 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
159 HICON hicon = LoadIconW( hInstance, name );
160 if (!hicon) hicon = LoadIconW( 0, name );
161 return hicon;
164 /***********************************************************************
165 * STATIC_LoadBitmapA
167 * Load the bitmap for an SS_BITMAP control.
169 static HBITMAP STATIC_LoadBitmapA( HWND hwnd, LPCSTR name )
171 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
172 HBITMAP hbitmap = LoadBitmapA( hInstance, name );
173 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
174 hbitmap = LoadBitmapA( 0, name );
175 return hbitmap;
178 /***********************************************************************
179 * STATIC_LoadBitmapW
181 * Load the bitmap for an SS_BITMAP control.
183 static HBITMAP STATIC_LoadBitmapW( HWND hwnd, LPCWSTR name )
185 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
186 HBITMAP hbitmap = LoadBitmapW( hInstance, name );
187 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
188 hbitmap = LoadBitmapW( 0, name );
189 return hbitmap;
192 /***********************************************************************
193 * STATIC_TryPaintFcn
195 * Try to immediately paint the control.
197 static VOID STATIC_TryPaintFcn(HWND hwnd, LONG full_style)
199 LONG style = full_style & SS_TYPEMASK;
200 RECT rc;
202 GetClientRect( hwnd, &rc );
203 if (!IsRectEmpty(&rc) && IsWindowVisible(hwnd) && staticPaintFunc[style])
205 HDC hdc;
206 hdc = GetDC( hwnd );
207 (staticPaintFunc[style])( hwnd, hdc, full_style );
208 ReleaseDC( hwnd, hdc );
212 /***********************************************************************
213 * StaticWndProc_common
215 static LRESULT StaticWndProc_common( HWND hwnd, UINT uMsg, WPARAM wParam,
216 LPARAM lParam, BOOL unicode )
218 LRESULT lResult = 0;
219 LONG full_style = GetWindowLongA( hwnd, GWL_STYLE );
220 LONG style = full_style & SS_TYPEMASK;
222 switch (uMsg)
224 case WM_CREATE:
225 if (style < 0L || style > SS_TYPEMASK)
227 ERR("Unknown style 0x%02lx\n", style );
228 return -1;
230 /* initialise colours */
231 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
232 color_background = GetSysColor(COLOR_BACKGROUND);
233 color_window = GetSysColor(COLOR_WINDOW);
234 break;
236 case WM_NCDESTROY:
237 if (style == SS_ICON) {
239 * FIXME
240 * DestroyIcon32( STATIC_SetIcon( wndPtr, 0 ) );
242 * We don't want to do this yet because DestroyIcon32 is broken. If the icon
243 * had already been loaded by the application the last thing we want to do is
244 * GlobalFree16 the handle.
246 break;
248 else return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
249 DefWindowProcA(hwnd, uMsg, wParam, lParam);
251 case WM_PAINT:
253 PAINTSTRUCT ps;
254 BeginPaint(hwnd, &ps);
255 if (staticPaintFunc[style])
256 (staticPaintFunc[style])( hwnd, ps.hdc, full_style );
257 EndPaint(hwnd, &ps);
259 break;
261 case WM_ENABLE:
262 InvalidateRect(hwnd, NULL, FALSE);
263 break;
265 case WM_SYSCOLORCHANGE:
266 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
267 color_background = GetSysColor(COLOR_BACKGROUND);
268 color_window = GetSysColor(COLOR_WINDOW);
269 InvalidateRect(hwnd, NULL, TRUE);
270 break;
272 case WM_NCCREATE:
273 if ((TWEAK_WineLook > WIN31_LOOK) && (full_style & SS_SUNKEN))
274 SetWindowLongA( hwnd, GWL_EXSTYLE,
275 GetWindowLongA( hwnd, GWL_EXSTYLE ) | WS_EX_STATICEDGE );
277 if(unicode)
278 lParam = (LPARAM)(((LPCREATESTRUCTW)lParam)->lpszName);
279 else
280 lParam = (LPARAM)(((LPCREATESTRUCTA)lParam)->lpszName);
281 /* fall through */
282 case WM_SETTEXT:
283 switch (style) {
284 case SS_ICON:
286 HICON hIcon;
287 if(unicode)
288 hIcon = STATIC_LoadIconW(hwnd, (LPCWSTR)lParam);
289 else
290 hIcon = STATIC_LoadIconA(hwnd, (LPCSTR)lParam);
291 /* FIXME : should we also return the previous hIcon here ??? */
292 STATIC_SetIcon(hwnd, hIcon, style);
293 break;
295 case SS_BITMAP:
297 HBITMAP hBitmap;
298 if(unicode)
299 hBitmap = STATIC_LoadBitmapW(hwnd, (LPCWSTR)lParam);
300 else
301 hBitmap = STATIC_LoadBitmapA(hwnd, (LPCSTR)lParam);
302 STATIC_SetBitmap(hwnd, hBitmap, style);
303 break;
305 case SS_LEFT:
306 case SS_CENTER:
307 case SS_RIGHT:
308 case SS_SIMPLE:
309 case SS_LEFTNOWORDWRAP:
311 if (HIWORD(lParam))
313 if(unicode)
314 lResult = DefWindowProcW( hwnd, WM_SETTEXT, wParam, lParam );
315 else
316 lResult = DefWindowProcA( hwnd, WM_SETTEXT, wParam, lParam );
318 if (uMsg == WM_SETTEXT)
319 STATIC_TryPaintFcn( hwnd, full_style );
320 break;
322 default:
323 if (HIWORD(lParam))
325 if(unicode)
326 lResult = DefWindowProcW( hwnd, WM_SETTEXT, wParam, lParam );
327 else
328 lResult = DefWindowProcA( hwnd, WM_SETTEXT, wParam, lParam );
330 if(uMsg == WM_SETTEXT)
331 InvalidateRect(hwnd, NULL, FALSE);
333 return 1; /* success. FIXME: check text length */
335 case WM_SETFONT:
336 if ((style == SS_ICON) || (style == SS_BITMAP)) return 0;
337 SetWindowLongA( hwnd, HFONT_GWL_OFFSET, wParam );
338 if (!LOWORD(lParam)) break; /* don't refresh */
339 switch (style) {
340 case SS_LEFT:
341 case SS_CENTER:
342 case SS_RIGHT:
343 case SS_SIMPLE:
344 case SS_LEFTNOWORDWRAP:
345 STATIC_TryPaintFcn( hwnd, full_style );
346 break;
347 default:
348 InvalidateRect( hwnd, NULL, FALSE );
349 break;
351 break;
353 case WM_GETFONT:
354 return GetWindowLongA( hwnd, HFONT_GWL_OFFSET );
356 case WM_NCHITTEST:
357 if (full_style & SS_NOTIFY)
358 return HTCLIENT;
359 else
360 return HTTRANSPARENT;
362 case WM_GETDLGCODE:
363 return DLGC_STATIC;
365 case STM_GETIMAGE:
366 case STM_GETICON16:
367 case STM_GETICON:
368 return GetWindowLongA( hwnd, HICON_GWL_OFFSET );
370 case STM_SETIMAGE:
371 switch(wParam) {
372 case IMAGE_BITMAP:
373 lResult = STATIC_SetBitmap( hwnd, (HBITMAP)lParam, style );
374 break;
375 case IMAGE_ICON:
376 lResult = STATIC_SetIcon( hwnd, (HICON)lParam, style );
377 break;
378 default:
379 FIXME("STM_SETIMAGE: Unhandled type %x\n", wParam);
380 break;
382 InvalidateRect( hwnd, NULL, FALSE );
383 break;
385 case STM_SETICON16:
386 case STM_SETICON:
387 lResult = STATIC_SetIcon( hwnd, (HICON)wParam, style );
388 InvalidateRect( hwnd, NULL, FALSE );
389 break;
391 default:
392 return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
393 DefWindowProcA(hwnd, uMsg, wParam, lParam);
395 return lResult;
398 /***********************************************************************
399 * StaticWndProcA
401 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
403 if (!IsWindow( hWnd )) return 0;
404 return StaticWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
407 /***********************************************************************
408 * StaticWndProcW
410 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
412 if (!IsWindow( hWnd )) return 0;
413 return StaticWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
416 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style )
418 DRAWITEMSTRUCT dis;
419 LONG id = GetWindowLongA( hwnd, GWL_ID );
421 dis.CtlType = ODT_STATIC;
422 dis.CtlID = id;
423 dis.itemID = 0;
424 dis.itemAction = ODA_DRAWENTIRE;
425 dis.itemState = 0;
426 dis.hwndItem = hwnd;
427 dis.hDC = hdc;
428 dis.itemData = 0;
429 GetClientRect( hwnd, &dis.rcItem );
431 SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
432 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
435 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style )
437 RECT rc;
438 HBRUSH hBrush;
439 HFONT hFont;
440 WORD wFormat;
441 INT len;
442 WCHAR *text;
444 GetClientRect( hwnd, &rc);
446 switch (style & SS_TYPEMASK)
448 case SS_LEFT:
449 wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
450 break;
452 case SS_CENTER:
453 wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
454 break;
456 case SS_RIGHT:
457 wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
458 break;
460 case SS_SIMPLE:
461 wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOCLIP;
462 break;
464 case SS_LEFTNOWORDWRAP:
465 wFormat = DT_LEFT | DT_EXPANDTABS | DT_VCENTER;
466 break;
468 default:
469 return;
472 if (style & SS_NOPREFIX)
473 wFormat |= DT_NOPREFIX;
475 if ((hFont = GetWindowLongA( hwnd, HFONT_GWL_OFFSET ))) SelectObject( hdc, hFont );
477 if ((style & SS_NOPREFIX) || ((style & SS_TYPEMASK) != SS_SIMPLE))
479 hBrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
480 if (!hBrush) /* did the app forget to call defwindowproc ? */
481 hBrush = DefWindowProcW(GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd);
482 FillRect( hdc, &rc, hBrush );
484 if (!IsWindowEnabled(hwnd)) SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
486 if (!(len = SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 ))) return;
487 if (!(text = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return;
488 SendMessageW( hwnd, WM_GETTEXT, len + 1, (LPARAM)text );
489 DrawTextW( hdc, text, -1, &rc, wFormat );
490 HeapFree( GetProcessHeap(), 0, text );
493 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style )
495 RECT rc;
496 HBRUSH hBrush;
498 GetClientRect( hwnd, &rc);
500 switch (style & SS_TYPEMASK)
502 case SS_BLACKRECT:
503 hBrush = CreateSolidBrush(color_windowframe);
504 FillRect( hdc, &rc, hBrush );
505 break;
506 case SS_GRAYRECT:
507 hBrush = CreateSolidBrush(color_background);
508 FillRect( hdc, &rc, hBrush );
509 break;
510 case SS_WHITERECT:
511 hBrush = CreateSolidBrush(color_window);
512 FillRect( hdc, &rc, hBrush );
513 break;
514 case SS_BLACKFRAME:
515 hBrush = CreateSolidBrush(color_windowframe);
516 FrameRect( hdc, &rc, hBrush );
517 break;
518 case SS_GRAYFRAME:
519 hBrush = CreateSolidBrush(color_background);
520 FrameRect( hdc, &rc, hBrush );
521 break;
522 case SS_WHITEFRAME:
523 hBrush = CreateSolidBrush(color_window);
524 FrameRect( hdc, &rc, hBrush );
525 break;
526 default:
527 return;
529 DeleteObject( hBrush );
533 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style )
535 RECT rc;
536 HBRUSH hbrush;
537 HICON hIcon;
539 GetClientRect( hwnd, &rc );
540 hbrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
541 FillRect( hdc, &rc, hbrush );
542 if ((hIcon = GetWindowLongA( hwnd, HICON_GWL_OFFSET )))
543 DrawIcon( hdc, rc.left, rc.top, hIcon );
546 static void STATIC_PaintBitmapfn(HWND hwnd, HDC hdc, DWORD style )
548 RECT rc;
549 HBRUSH hbrush;
550 HICON hIcon;
551 HDC hMemDC;
552 HBITMAP oldbitmap;
554 GetClientRect( hwnd, &rc );
555 hbrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
556 FillRect( hdc, &rc, hbrush );
558 if ((hIcon = GetWindowLongA( hwnd, HICON_GWL_OFFSET )))
560 BITMAP bm;
561 SIZE sz;
563 if(GetObjectType(hIcon) != OBJ_BITMAP) return;
564 if (!(hMemDC = CreateCompatibleDC( hdc ))) return;
565 GetObjectW(hIcon, sizeof(bm), &bm);
566 GetBitmapDimensionEx(hIcon, &sz);
567 oldbitmap = SelectObject(hMemDC, hIcon);
568 BitBlt(hdc, sz.cx, sz.cy, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0,
569 SRCCOPY);
570 SelectObject(hMemDC, oldbitmap);
571 DeleteDC(hMemDC);
576 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style )
578 RECT rc;
580 if (TWEAK_WineLook == WIN31_LOOK)
581 return;
583 GetClientRect( hwnd, &rc );
584 switch (style & SS_TYPEMASK)
586 case SS_ETCHEDHORZ:
587 DrawEdge(hdc,&rc,EDGE_ETCHED,BF_TOP|BF_BOTTOM);
588 break;
589 case SS_ETCHEDVERT:
590 DrawEdge(hdc,&rc,EDGE_ETCHED,BF_LEFT|BF_RIGHT);
591 break;
592 case SS_ETCHEDFRAME:
593 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_RECT);
594 break;