Release 990226.
[wine/gsoc-2012-control.git] / controls / static.c
blobc7c21d0ae282819a406019270195921ccf3a6353
1 /*
2 * Static control
4 * Copyright David W. Metcalfe, 1993
6 */
8 #include "wine/winuser16.h"
9 #include "win.h"
10 #include "bitmap.h"
11 #include "cursoricon.h"
12 #include "static.h"
13 #include "heap.h"
14 #include "debug.h"
15 #include "tweak.h"
17 static void STATIC_PaintTextfn( WND *wndPtr, HDC hdc );
18 static void STATIC_PaintRectfn( WND *wndPtr, HDC hdc );
19 static void STATIC_PaintIconfn( WND *wndPtr, HDC hdc );
20 static void STATIC_PaintBitmapfn( WND *wndPtr, HDC hdc );
21 static void STATIC_PaintEtchedfn( WND *wndPtr, HDC hdc );
23 static COLORREF color_windowframe, color_background, color_window;
26 typedef void (*pfPaint)( WND *, HDC );
28 static pfPaint staticPaintFunc[SS_TYPEMASK+1] =
30 STATIC_PaintTextfn, /* SS_LEFT */
31 STATIC_PaintTextfn, /* SS_CENTER */
32 STATIC_PaintTextfn, /* SS_RIGHT */
33 STATIC_PaintIconfn, /* SS_ICON */
34 STATIC_PaintRectfn, /* SS_BLACKRECT */
35 STATIC_PaintRectfn, /* SS_GRAYRECT */
36 STATIC_PaintRectfn, /* SS_WHITERECT */
37 STATIC_PaintRectfn, /* SS_BLACKFRAME */
38 STATIC_PaintRectfn, /* SS_GRAYFRAME */
39 STATIC_PaintRectfn, /* SS_WHITEFRAME */
40 NULL, /* Not defined */
41 STATIC_PaintTextfn, /* SS_SIMPLE */
42 STATIC_PaintTextfn, /* SS_LEFTNOWORDWRAP */
43 NULL, /* SS_OWNERDRAW */
44 STATIC_PaintBitmapfn, /* SS_BITMAP */
45 NULL, /* SS_ENHMETAFILE */
46 STATIC_PaintEtchedfn, /* SS_ETCHEDHORIZ */
47 STATIC_PaintEtchedfn, /* SS_ETCHEDVERT */
48 STATIC_PaintEtchedfn, /* SS_ETCHEDFRAME */
52 /***********************************************************************
53 * STATIC_SetIcon
55 * Set the icon for an SS_ICON control.
57 static HICON16 STATIC_SetIcon( WND *wndPtr, HICON16 hicon )
59 HICON16 prevIcon;
60 STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
61 CURSORICONINFO *info = hicon?(CURSORICONINFO *) GlobalLock16( hicon ):NULL;
63 if ((wndPtr->dwStyle & SS_TYPEMASK) != SS_ICON) return 0;
64 if (hicon && !info) {
65 ERR(static, "huh? hicon!=0, but info=0???\n");
66 return 0;
68 prevIcon = infoPtr->hIcon;
69 infoPtr->hIcon = hicon;
70 if (hicon)
72 SetWindowPos( wndPtr->hwndSelf, 0, 0, 0, info->nWidth, info->nHeight,
73 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
74 GlobalUnlock16( hicon );
76 return prevIcon;
79 /***********************************************************************
80 * STATIC_SetBitmap
82 * Set the bitmap for an SS_BITMAP control.
84 static HICON16 STATIC_SetBitmap( WND *wndPtr, HICON16 hicon )
86 HICON16 prevIcon;
87 STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
88 BITMAPOBJ *info = (BITMAPOBJ *)GDI_HEAP_LOCK(hicon);
90 if ((wndPtr->dwStyle & SS_TYPEMASK) != SS_BITMAP) return 0;
91 if (hicon && !info) {
92 ERR(static, "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->bitmap.bmWidth, info->bitmap.bmHeight,
100 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
102 GDI_HEAP_UNLOCK( hicon );
103 return prevIcon;
107 /***********************************************************************
108 * STATIC_LoadIcon
110 * Load the icon for an SS_ICON control.
112 static HICON16 STATIC_LoadIcon( WND *wndPtr, LPCSTR name )
114 HICON16 hicon;
116 if (wndPtr->flags & WIN_ISWIN32)
118 if (!HIWORD(wndPtr->hInstance)) {
119 LPSTR segname = SEGPTR_STRDUP(name);
120 hicon = LoadIcon16( wndPtr->hInstance, SEGPTR_GET(segname) );
121 SEGPTR_FREE(segname);
122 } else
123 hicon = LoadIconA( wndPtr->hInstance, name );
124 } else {
125 LPSTR segname = SEGPTR_STRDUP(name);
127 if (HIWORD(wndPtr->hInstance))
128 FIXME(static,"win16 window class, but win32 hinstance??\n");
129 hicon = LoadIcon16( wndPtr->hInstance, SEGPTR_GET(segname) );
130 SEGPTR_FREE(segname);
132 if (!hicon)
133 hicon = LoadIconA( 0, name );
134 return hicon;
137 /***********************************************************************
138 * STATIC_LoadBitmap
140 * Load the bitmap for an SS_BITMAP control.
142 static HBITMAP16 STATIC_LoadBitmap( WND *wndPtr, LPCSTR name )
144 HBITMAP16 hbitmap;
146 if (wndPtr->flags & WIN_ISWIN32)
148 hbitmap = LoadBitmapA( wndPtr->hInstance, name );
149 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
150 hbitmap = LoadBitmapA( 0, name );
152 else
154 LPSTR segname = SEGPTR_STRDUP(name);
155 hbitmap = LoadBitmap16( wndPtr->hInstance, SEGPTR_GET(segname) );
156 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
157 hbitmap = LoadBitmapA( 0, segname );
158 SEGPTR_FREE(segname);
160 return hbitmap;
164 /***********************************************************************
165 * StaticWndProc
167 LRESULT WINAPI StaticWndProc( HWND hWnd, UINT uMsg, WPARAM wParam,
168 LPARAM lParam )
170 LRESULT lResult = 0;
171 WND *wndPtr = WIN_FindWndPtr(hWnd);
172 LONG style = wndPtr->dwStyle & SS_TYPEMASK;
173 STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
175 switch (uMsg)
177 case WM_NCCREATE: {
178 CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
180 if ((TWEAK_WineLook > WIN31_LOOK) && (wndPtr->dwStyle & SS_SUNKEN))
181 wndPtr->dwExStyle |= WS_EX_STATICEDGE;
183 if (style == SS_ICON)
185 if (cs->lpszName) {
186 if (!HIWORD(cs->lpszName) || cs->lpszName[0])
187 STATIC_SetIcon( wndPtr,
188 STATIC_LoadIcon( wndPtr, cs->lpszName ));
190 return 1;
192 if (style == SS_BITMAP)
194 if (cs->lpszName)
195 STATIC_SetBitmap( wndPtr,
196 STATIC_LoadBitmap( wndPtr, cs->lpszName ));
197 WARN(static, "style SS_BITMAP, dwStyle is 0x%08lx\n",
198 wndPtr->dwStyle);
199 return 1;
201 if (!HIWORD(cs->lpszName) && (cs->lpszName)) {
202 FIXME(static,"windowName is 0x%04x, not doing DefWindowProc\n",
203 LOWORD(cs->lpszName)
205 return 1;
207 return DefWindowProcA( hWnd, uMsg, wParam, lParam );
209 case WM_CREATE:
210 if (style < 0L || style > SS_TYPEMASK)
212 ERR(static, "Unknown style 0x%02lx\n", style );
213 lResult = -1L;
214 break;
216 /* initialise colours */
217 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
218 color_background = GetSysColor(COLOR_BACKGROUND);
219 color_window = GetSysColor(COLOR_WINDOW);
220 break;
222 case WM_NCDESTROY:
223 if (style == SS_ICON) {
225 * FIXME
226 * DestroyIcon32( STATIC_SetIcon( wndPtr, 0 ) );
228 * We don't want to do this yet because DestroyIcon32 is broken. If the icon
229 * had already been loaded by the application the last thing we want to do is
230 * GlobalFree16 the handle.
232 } else {
233 lResult = DefWindowProcA( hWnd, uMsg, wParam, lParam );
235 break;
237 case WM_PAINT:
239 PAINTSTRUCT ps;
240 BeginPaint( hWnd, &ps );
241 if (staticPaintFunc[style])
242 (staticPaintFunc[style])( wndPtr, ps.hdc );
243 EndPaint( hWnd, &ps );
245 break;
247 case WM_ENABLE:
248 InvalidateRect( hWnd, NULL, FALSE );
249 break;
251 case WM_SYSCOLORCHANGE:
252 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
253 color_background = GetSysColor(COLOR_BACKGROUND);
254 color_window = GetSysColor(COLOR_WINDOW);
255 InvalidateRect( hWnd, NULL, TRUE );
256 break;
258 case WM_SETTEXT:
259 if (style == SS_ICON)
260 /* FIXME : should we also return the previous hIcon here ??? */
261 STATIC_SetIcon( wndPtr, STATIC_LoadIcon( wndPtr, (LPCSTR)lParam ));
262 else if (style == SS_BITMAP)
263 STATIC_SetBitmap(wndPtr,STATIC_LoadBitmap(wndPtr,(LPCSTR)lParam ));
264 else
265 DEFWND_SetText( wndPtr, (LPCSTR)lParam );
266 InvalidateRect( hWnd, NULL, FALSE );
267 UpdateWindow( hWnd );
268 break;
270 case WM_SETFONT:
271 if (style == SS_ICON) return 0;
272 if (style == SS_BITMAP) return 0;
273 infoPtr->hFont = (HFONT16)wParam;
274 if (LOWORD(lParam))
276 InvalidateRect( hWnd, NULL, FALSE );
277 UpdateWindow( hWnd );
279 break;
281 case WM_GETFONT:
282 return infoPtr->hFont;
284 case WM_NCHITTEST:
285 return HTTRANSPARENT;
287 case WM_GETDLGCODE:
288 return DLGC_STATIC;
290 case STM_GETIMAGE:
291 case STM_GETICON16:
292 case STM_GETICON:
293 return infoPtr->hIcon;
295 case STM_SETIMAGE:
296 /* FIXME: handle wParam */
297 lResult = STATIC_SetBitmap( wndPtr, (HBITMAP)lParam );
298 InvalidateRect( hWnd, NULL, FALSE );
299 UpdateWindow( hWnd );
300 break;
302 case STM_SETICON16:
303 case STM_SETICON:
304 lResult = STATIC_SetIcon( wndPtr, (HICON16)wParam );
305 InvalidateRect( hWnd, NULL, FALSE );
306 UpdateWindow( hWnd );
307 break;
309 default:
310 lResult = DefWindowProcA(hWnd, uMsg, wParam, lParam);
311 break;
314 return lResult;
318 static void STATIC_PaintTextfn( WND *wndPtr, HDC hdc )
320 RECT rc;
321 HBRUSH hBrush;
322 WORD wFormat;
324 LONG style = wndPtr->dwStyle;
325 STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
327 GetClientRect( wndPtr->hwndSelf, &rc);
329 switch (style & SS_TYPEMASK)
331 case SS_LEFT:
332 wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
333 break;
335 case SS_CENTER:
336 wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
337 break;
339 case SS_RIGHT:
340 wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
341 break;
343 case SS_SIMPLE:
344 wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOCLIP;
345 break;
347 case SS_LEFTNOWORDWRAP:
348 wFormat = DT_LEFT | DT_EXPANDTABS | DT_VCENTER;
349 break;
351 default:
352 return;
355 if (style & SS_NOPREFIX)
356 wFormat |= DT_NOPREFIX;
358 if (infoPtr->hFont) SelectObject( hdc, infoPtr->hFont );
359 hBrush = SendMessageA( GetParent(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
360 hdc, wndPtr->hwndSelf );
361 if (!hBrush) hBrush = GetStockObject(WHITE_BRUSH);
362 FillRect( hdc, &rc, hBrush );
363 if (wndPtr->text) DrawTextA( hdc, wndPtr->text, -1, &rc, wFormat );
366 static void STATIC_PaintRectfn( WND *wndPtr, HDC hdc )
368 RECT rc;
369 HBRUSH hBrush;
371 GetClientRect( wndPtr->hwndSelf, &rc);
373 switch (wndPtr->dwStyle & SS_TYPEMASK)
375 case SS_BLACKRECT:
376 hBrush = CreateSolidBrush(color_windowframe);
377 FillRect( hdc, &rc, hBrush );
378 break;
379 case SS_GRAYRECT:
380 hBrush = CreateSolidBrush(color_background);
381 FillRect( hdc, &rc, hBrush );
382 break;
383 case SS_WHITERECT:
384 hBrush = CreateSolidBrush(color_window);
385 FillRect( hdc, &rc, hBrush );
386 break;
387 case SS_BLACKFRAME:
388 hBrush = CreateSolidBrush(color_windowframe);
389 FrameRect( hdc, &rc, hBrush );
390 break;
391 case SS_GRAYFRAME:
392 hBrush = CreateSolidBrush(color_background);
393 FrameRect( hdc, &rc, hBrush );
394 break;
395 case SS_WHITEFRAME:
396 hBrush = CreateSolidBrush(color_window);
397 FrameRect( hdc, &rc, hBrush );
398 break;
399 default:
400 return;
402 DeleteObject( hBrush );
406 static void STATIC_PaintIconfn( WND *wndPtr, HDC hdc )
408 RECT rc;
409 HBRUSH hbrush;
410 STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
412 GetClientRect( wndPtr->hwndSelf, &rc );
413 hbrush = SendMessageA( GetParent(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
414 hdc, wndPtr->hwndSelf );
415 FillRect( hdc, &rc, hbrush );
416 if (infoPtr->hIcon) DrawIcon( hdc, rc.left, rc.top, infoPtr->hIcon );
419 static void STATIC_PaintBitmapfn(WND *wndPtr, HDC hdc )
421 RECT rc;
422 HBRUSH hbrush;
423 STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
424 HDC hMemDC;
425 HBITMAP oldbitmap;
427 GetClientRect( wndPtr->hwndSelf, &rc );
428 hbrush = SendMessageA( GetParent(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
429 hdc, wndPtr->hwndSelf );
430 FillRect( hdc, &rc, hbrush );
431 if (infoPtr->hIcon) {
432 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_HEAP_LOCK( infoPtr->hIcon );
434 if (!bmp) return;
435 if (!(hMemDC = CreateCompatibleDC( hdc ))) return;
437 oldbitmap = SelectObject(hMemDC,infoPtr->hIcon);
438 BitBlt(hdc,bmp->size.cx,bmp->size.cy,bmp->bitmap.bmWidth,bmp->bitmap.bmHeight,hMemDC,0,0,SRCCOPY);
439 DeleteDC(hMemDC);
440 GDI_HEAP_UNLOCK(infoPtr->hIcon);
445 static void STATIC_PaintEtchedfn( WND *wndPtr, HDC hdc )
447 RECT rc;
448 HBRUSH hbrush;
449 HPEN hpen;
451 if (TWEAK_WineLook == WIN31_LOOK)
452 return;
454 GetClientRect( wndPtr->hwndSelf, &rc );
455 hbrush = SendMessageA( GetParent(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
456 hdc, wndPtr->hwndSelf );
457 FillRect( hdc, &rc, hbrush );
459 switch (wndPtr->dwStyle & SS_TYPEMASK)
461 case SS_ETCHEDHORZ:
462 hpen = SelectObject (hdc, GetSysColorPen (COLOR_3DSHADOW));
463 MoveToEx (hdc, rc.left, rc.bottom / 2 - 1, NULL);
464 LineTo (hdc, rc.right - 1, rc.bottom / 2 - 1);
465 SelectObject (hdc, GetSysColorPen (COLOR_3DHIGHLIGHT));
466 MoveToEx (hdc, rc.left, rc.bottom / 2, NULL);
467 LineTo (hdc, rc.right, rc.bottom / 2);
468 LineTo (hdc, rc.right, rc.bottom / 2 - 1);
469 SelectObject (hdc, hpen);
470 break;
472 case SS_ETCHEDVERT:
473 hpen = SelectObject (hdc, GetSysColorPen (COLOR_3DSHADOW));
474 MoveToEx (hdc, rc.right / 2 - 1, rc.top, NULL);
475 LineTo (hdc, rc.right / 2 - 1, rc.bottom - 1);
476 SelectObject (hdc, GetSysColorPen (COLOR_3DHIGHLIGHT));
477 MoveToEx (hdc, rc.right / 2, rc.top, NULL);
478 LineTo (hdc, rc.right / 2, rc.bottom);
479 LineTo (hdc, rc.right / 2 -1 , rc.bottom);
480 SelectObject (hdc, hpen);
481 break;
483 case SS_ETCHEDFRAME:
484 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_RECT);
485 break;