Use GDI APIs to access bitmap info.
[wine] / controls / static.c
1 /*
2  * Static control
3  *
4  * Copyright  David W. Metcalfe, 1993
5  *
6  */
7
8 #include "wine/winuser16.h"
9 #include "win.h"
10 #include "cursoricon.h"
11 #include "static.h"
12 #include "heap.h"
13 #include "debugtools.h"
14 #include "tweak.h"
15
16 DEFAULT_DEBUG_CHANNEL(static)
17
18 static void STATIC_PaintTextfn( WND *wndPtr, HDC hdc );
19 static void STATIC_PaintRectfn( WND *wndPtr, HDC hdc );
20 static void STATIC_PaintIconfn( WND *wndPtr, HDC hdc );
21 static void STATIC_PaintBitmapfn( WND *wndPtr, HDC hdc );
22 static void STATIC_PaintEtchedfn( WND *wndPtr, HDC hdc );
23
24 static COLORREF color_windowframe, color_background, color_window;
25
26
27 typedef void (*pfPaint)( WND *, HDC );
28
29 static pfPaint staticPaintFunc[SS_TYPEMASK+1] =
30 {
31     STATIC_PaintTextfn,      /* SS_LEFT */
32     STATIC_PaintTextfn,      /* SS_CENTER */
33     STATIC_PaintTextfn,      /* SS_RIGHT */
34     STATIC_PaintIconfn,      /* SS_ICON */
35     STATIC_PaintRectfn,      /* SS_BLACKRECT */
36     STATIC_PaintRectfn,      /* SS_GRAYRECT */
37     STATIC_PaintRectfn,      /* SS_WHITERECT */
38     STATIC_PaintRectfn,      /* SS_BLACKFRAME */
39     STATIC_PaintRectfn,      /* SS_GRAYFRAME */
40     STATIC_PaintRectfn,      /* SS_WHITEFRAME */
41     NULL,                    /* Not defined */
42     STATIC_PaintTextfn,      /* SS_SIMPLE */
43     STATIC_PaintTextfn,      /* SS_LEFTNOWORDWRAP */
44     NULL,                    /* SS_OWNERDRAW */
45     STATIC_PaintBitmapfn,    /* SS_BITMAP */
46     NULL,                    /* SS_ENHMETAFILE */
47     STATIC_PaintEtchedfn,    /* SS_ETCHEDHORIZ */
48     STATIC_PaintEtchedfn,    /* SS_ETCHEDVERT */
49     STATIC_PaintEtchedfn,    /* SS_ETCHEDFRAME */
50 };
51
52
53 /***********************************************************************
54  *           STATIC_SetIcon
55  *
56  * Set the icon for an SS_ICON control.
57  */
58 static HICON16 STATIC_SetIcon( WND *wndPtr, HICON16 hicon )
59 {
60     HICON16 prevIcon;
61     STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
62     CURSORICONINFO *info = hicon?(CURSORICONINFO *) GlobalLock16( hicon ):NULL;
63
64     if ((wndPtr->dwStyle & SS_TYPEMASK) != SS_ICON) return 0;
65     if (hicon && !info) {
66         ERR("huh? hicon!=0, but info=0???\n");
67         return 0;
68     }
69     prevIcon = infoPtr->hIcon;
70     infoPtr->hIcon = hicon;
71     if (hicon)
72     {
73         SetWindowPos( wndPtr->hwndSelf, 0, 0, 0, info->nWidth, info->nHeight,
74                         SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
75         GlobalUnlock16( hicon );
76     }
77     return prevIcon;
78 }
79
80 /***********************************************************************
81  *           STATIC_SetBitmap
82  *
83  * Set the bitmap for an SS_BITMAP control.
84  */
85 static HBITMAP16 STATIC_SetBitmap( WND *wndPtr, HBITMAP16 hBitmap )
86 {
87     HBITMAP16 hOldBitmap;
88     STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
89
90     if ((wndPtr->dwStyle & SS_TYPEMASK) != SS_BITMAP) return 0;
91     if (hBitmap && GetObjectType(hBitmap) != OBJ_BITMAP) {
92         ERR("huh? hBitmap!=0, but not bitmap\n");
93         return 0;
94     }
95     hOldBitmap = infoPtr->hIcon;
96     infoPtr->hIcon = hBitmap;
97     if (hBitmap)
98     {
99         BITMAP bm;
100         GetObjectA(hBitmap, sizeof(bm), &bm);
101         SetWindowPos( wndPtr->hwndSelf, 0, 0, 0, bm.bmWidth, bm.bmHeight,
102                       SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
103     }
104     return hOldBitmap;
105 }
106
107
108 /***********************************************************************
109  *           STATIC_LoadIcon
110  *
111  * Load the icon for an SS_ICON control.
112  */
113 static HICON16 STATIC_LoadIcon( WND *wndPtr, LPCSTR name )
114 {
115     HICON16 hicon;
116
117     if (wndPtr->flags & WIN_ISWIN32)
118     {
119         if (!HIWORD(wndPtr->hInstance)) {
120             LPSTR segname = SEGPTR_STRDUP(name);
121             hicon = LoadIcon16( wndPtr->hInstance, SEGPTR_GET(segname) );
122             SEGPTR_FREE(segname);
123         } else
124             hicon = LoadIconA( wndPtr->hInstance, name );
125     } else {
126         LPSTR segname = SEGPTR_STRDUP(name);
127
128         if (HIWORD(wndPtr->hInstance))
129                 FIXME("win16 window class, but win32 hinstance??\n");
130         hicon = LoadIcon16( wndPtr->hInstance, SEGPTR_GET(segname) );
131         SEGPTR_FREE(segname);
132     }
133     if (!hicon)
134         hicon = LoadIconA( 0, name );
135     return hicon;
136 }
137
138 /***********************************************************************
139  *           STATIC_LoadBitmap
140  *
141  * Load the bitmap for an SS_BITMAP control.
142  */
143 static HBITMAP16 STATIC_LoadBitmap( WND *wndPtr, LPCSTR name )
144 {
145     HBITMAP16 hbitmap;
146
147     if (wndPtr->flags & WIN_ISWIN32)
148     {
149         hbitmap = LoadBitmapA( wndPtr->hInstance, name );
150         if (!hbitmap)  /* Try OEM icon (FIXME: is this right?) */
151             hbitmap = LoadBitmapA( 0, name );
152     }
153     else
154     {
155         LPSTR segname = SEGPTR_STRDUP(name);
156         hbitmap = LoadBitmap16( wndPtr->hInstance, SEGPTR_GET(segname) );
157         if (!hbitmap)  /* Try OEM icon (FIXME: is this right?) */
158             hbitmap = LoadBitmapA( 0, segname );
159         SEGPTR_FREE(segname);
160     }
161     return hbitmap;
162 }
163
164
165 /***********************************************************************
166  *           StaticWndProc
167  */
168 LRESULT WINAPI StaticWndProc( HWND hWnd, UINT uMsg, WPARAM wParam,
169                               LPARAM lParam )
170 {
171     LRESULT lResult = 0;
172     WND *wndPtr = WIN_FindWndPtr(hWnd);
173     LONG style = wndPtr->dwStyle & SS_TYPEMASK;
174     STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
175
176     switch (uMsg)
177     {
178     case WM_NCCREATE: {
179         CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
180
181         if ((TWEAK_WineLook > WIN31_LOOK) && (wndPtr->dwStyle & SS_SUNKEN))
182             wndPtr->dwExStyle |= WS_EX_STATICEDGE;
183
184         if (style == SS_ICON)
185         {
186             if (cs->lpszName)
187             {
188                 if (!HIWORD(cs->lpszName) || cs->lpszName[0])
189                     STATIC_SetIcon( wndPtr,
190                                 STATIC_LoadIcon( wndPtr, cs->lpszName ));
191             }
192             lResult = 1;
193             goto END;
194         }
195         if (style == SS_BITMAP)
196         {
197             if (cs->lpszName)
198                 STATIC_SetBitmap( wndPtr,
199                                 STATIC_LoadBitmap( wndPtr, cs->lpszName ));
200             WARN("style SS_BITMAP, dwStyle is 0x%08lx\n",
201                         wndPtr->dwStyle);
202             lResult = 1;
203             goto END;
204         }
205         if (!HIWORD(cs->lpszName) && (cs->lpszName))
206         {
207                 FIXME("windowName is 0x%04x, not doing DefWindowProc\n",
208                     LOWORD(cs->lpszName));
209                 lResult = 1;
210                 goto END;
211         }
212         lResult = DefWindowProcA( hWnd, uMsg, wParam, lParam );
213         goto END;
214     }
215     case WM_CREATE:
216         if (style < 0L || style > SS_TYPEMASK)
217         {
218             ERR("Unknown style 0x%02lx\n", style );
219             lResult = -1L;
220             break;
221         }
222         /* initialise colours */
223         color_windowframe  = GetSysColor(COLOR_WINDOWFRAME);
224         color_background   = GetSysColor(COLOR_BACKGROUND);
225         color_window       = GetSysColor(COLOR_WINDOW);
226         break;
227
228     case WM_NCDESTROY:
229         if (style == SS_ICON) {
230 /*
231  * FIXME
232  *           DestroyIcon32( STATIC_SetIcon( wndPtr, 0 ) );
233  * 
234  * We don't want to do this yet because DestroyIcon32 is broken. If the icon
235  * had already been loaded by the application the last thing we want to do is
236  * GlobalFree16 the handle.
237  */
238         } else {
239             lResult = DefWindowProcA( hWnd, uMsg, wParam, lParam );
240         }
241         break;
242
243     case WM_PAINT:
244         {
245             PAINTSTRUCT ps;
246             BeginPaint( hWnd, &ps );
247             if (staticPaintFunc[style])
248                 (staticPaintFunc[style])( wndPtr, ps.hdc );
249             EndPaint( hWnd, &ps );
250         }
251         break;
252
253     case WM_ENABLE:
254         InvalidateRect( hWnd, NULL, FALSE );
255         break;
256
257     case WM_SYSCOLORCHANGE:
258         color_windowframe  = GetSysColor(COLOR_WINDOWFRAME);
259         color_background   = GetSysColor(COLOR_BACKGROUND);
260         color_window       = GetSysColor(COLOR_WINDOW);
261         InvalidateRect( hWnd, NULL, TRUE );
262         break;
263
264     case WM_SETTEXT:
265         if (style == SS_ICON)
266             /* FIXME : should we also return the previous hIcon here ??? */
267             STATIC_SetIcon( wndPtr, STATIC_LoadIcon( wndPtr, (LPCSTR)lParam ));
268         else if (style == SS_BITMAP) 
269             STATIC_SetBitmap(wndPtr,STATIC_LoadBitmap(wndPtr,(LPCSTR)lParam ));
270         else
271             DEFWND_SetText( wndPtr, (LPCSTR)lParam );
272         InvalidateRect( hWnd, NULL, FALSE );
273         UpdateWindow( hWnd );
274         break;
275
276     case WM_SETFONT:
277         if (style == SS_ICON)
278         {
279             lResult = 0;
280             goto END;
281         }
282         if (style == SS_BITMAP)
283         {
284             lResult = 0;
285             goto END;
286         }
287         infoPtr->hFont = (HFONT16)wParam;
288         if (LOWORD(lParam))
289         {
290             InvalidateRect( hWnd, NULL, FALSE );
291             UpdateWindow( hWnd );
292         }
293         break;
294
295     case WM_GETFONT:
296         lResult = infoPtr->hFont;
297         goto END;
298
299     case WM_NCHITTEST:
300         lResult = HTTRANSPARENT;
301         goto END;
302
303     case WM_GETDLGCODE:
304         lResult = DLGC_STATIC;
305         goto END;
306
307     case STM_GETIMAGE:
308     case STM_GETICON16:
309     case STM_GETICON:
310         lResult = infoPtr->hIcon;
311         goto END;
312
313     case STM_SETIMAGE:
314         /* FIXME: handle wParam */
315         lResult = STATIC_SetBitmap( wndPtr, (HBITMAP)lParam );
316         InvalidateRect( hWnd, NULL, FALSE );
317         UpdateWindow( hWnd );
318         break;
319
320     case STM_SETICON16:
321     case STM_SETICON:
322         lResult = STATIC_SetIcon( wndPtr, (HICON16)wParam );
323         InvalidateRect( hWnd, NULL, FALSE );
324         UpdateWindow( hWnd );
325         break;
326
327     default:
328         lResult = DefWindowProcA(hWnd, uMsg, wParam, lParam);
329         break;
330     }
331     
332 END:
333     WIN_ReleaseWndPtr(wndPtr);
334     return lResult;
335 }
336
337
338 static void STATIC_PaintTextfn( WND *wndPtr, HDC hdc )
339 {
340     RECT rc;
341     HBRUSH hBrush;
342     WORD wFormat;
343
344     LONG style = wndPtr->dwStyle;
345     STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
346
347     GetClientRect( wndPtr->hwndSelf, &rc);
348
349     switch (style & SS_TYPEMASK)
350     {
351     case SS_LEFT:
352         wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
353         break;
354
355     case SS_CENTER:
356         wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
357         break;
358
359     case SS_RIGHT:
360         wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
361         break;
362
363     case SS_SIMPLE:
364         wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOCLIP;
365         break;
366
367     case SS_LEFTNOWORDWRAP:
368         wFormat = DT_LEFT | DT_EXPANDTABS | DT_VCENTER;
369         break;
370
371     default:
372         return;
373     }
374
375     if (style & SS_NOPREFIX)
376         wFormat |= DT_NOPREFIX;
377
378     if (infoPtr->hFont) SelectObject( hdc, infoPtr->hFont );
379     hBrush = SendMessageA( GetParent(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
380                              hdc, wndPtr->hwndSelf );
381     if (!hBrush) hBrush = GetStockObject(WHITE_BRUSH);
382     FillRect( hdc, &rc, hBrush );
383     if (wndPtr->text) DrawTextA( hdc, wndPtr->text, -1, &rc, wFormat );
384 }
385
386 static void STATIC_PaintRectfn( WND *wndPtr, HDC hdc )
387 {
388     RECT rc;
389     HBRUSH hBrush;
390
391     GetClientRect( wndPtr->hwndSelf, &rc);
392     
393     switch (wndPtr->dwStyle & SS_TYPEMASK)
394     {
395     case SS_BLACKRECT:
396         hBrush = CreateSolidBrush(color_windowframe);
397         FillRect( hdc, &rc, hBrush );
398         break;
399     case SS_GRAYRECT:
400         hBrush = CreateSolidBrush(color_background);
401         FillRect( hdc, &rc, hBrush );
402         break;
403     case SS_WHITERECT:
404         hBrush = CreateSolidBrush(color_window);
405         FillRect( hdc, &rc, hBrush );
406         break;
407     case SS_BLACKFRAME:
408         hBrush = CreateSolidBrush(color_windowframe);
409         FrameRect( hdc, &rc, hBrush );
410         break;
411     case SS_GRAYFRAME:
412         hBrush = CreateSolidBrush(color_background);
413         FrameRect( hdc, &rc, hBrush );
414         break;
415     case SS_WHITEFRAME:
416         hBrush = CreateSolidBrush(color_window);
417         FrameRect( hdc, &rc, hBrush );
418         break;
419     default:
420         return;
421     }
422     DeleteObject( hBrush );
423 }
424
425
426 static void STATIC_PaintIconfn( WND *wndPtr, HDC hdc )
427 {
428     RECT rc;
429     HBRUSH hbrush;
430     STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
431
432     GetClientRect( wndPtr->hwndSelf, &rc );
433     hbrush = SendMessageA( GetParent(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
434                              hdc, wndPtr->hwndSelf );
435     FillRect( hdc, &rc, hbrush );
436     if (infoPtr->hIcon) DrawIcon( hdc, rc.left, rc.top, infoPtr->hIcon );
437 }
438
439 static void STATIC_PaintBitmapfn(WND *wndPtr, HDC hdc )
440 {
441     RECT rc;
442     HBRUSH hbrush;
443     STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
444     HDC hMemDC;
445     HBITMAP oldbitmap;
446
447     GetClientRect( wndPtr->hwndSelf, &rc );
448     hbrush = SendMessageA( GetParent(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
449                              hdc, wndPtr->hwndSelf );
450     FillRect( hdc, &rc, hbrush );
451
452     if (infoPtr->hIcon) {
453         BITMAP bm;
454         SIZE sz;
455
456         if(GetObjectType(infoPtr->hIcon) != OBJ_BITMAP)
457             return;
458         if (!(hMemDC = CreateCompatibleDC( hdc ))) return;
459         GetObjectA(infoPtr->hIcon, sizeof(bm), &bm);
460         GetBitmapDimensionEx(infoPtr->hIcon, &sz);
461         oldbitmap = SelectObject(hMemDC, infoPtr->hIcon);
462         BitBlt(hdc, sz.cx, sz.cy, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0,
463                SRCCOPY);
464         SelectObject(hMemDC, oldbitmap);
465         DeleteDC(hMemDC);
466     }
467 }
468
469
470 static void STATIC_PaintEtchedfn( WND *wndPtr, HDC hdc )
471 {
472     RECT rc;
473     HBRUSH hbrush;
474     HPEN hpen;
475
476     if (TWEAK_WineLook == WIN31_LOOK)
477         return;
478
479     GetClientRect( wndPtr->hwndSelf, &rc );
480     hbrush = SendMessageA( GetParent(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
481                              hdc, wndPtr->hwndSelf );
482     FillRect( hdc, &rc, hbrush );
483
484     switch (wndPtr->dwStyle & SS_TYPEMASK)
485     {
486         case SS_ETCHEDHORZ:
487             hpen = SelectObject (hdc, GetSysColorPen (COLOR_3DSHADOW));
488             MoveToEx (hdc, rc.left, rc.bottom / 2 - 1, NULL);
489             LineTo (hdc, rc.right - 1, rc.bottom / 2 - 1);
490             SelectObject (hdc, GetSysColorPen (COLOR_3DHIGHLIGHT));
491             MoveToEx (hdc, rc.left, rc.bottom / 2, NULL);
492             LineTo (hdc, rc.right, rc.bottom / 2);
493             LineTo (hdc, rc.right, rc.bottom / 2 - 1);
494             SelectObject (hdc, hpen);
495             break;
496
497         case SS_ETCHEDVERT:
498             hpen = SelectObject (hdc, GetSysColorPen (COLOR_3DSHADOW));
499             MoveToEx (hdc, rc.right / 2 - 1, rc.top, NULL);
500             LineTo (hdc, rc.right / 2 - 1, rc.bottom - 1);
501             SelectObject (hdc, GetSysColorPen (COLOR_3DHIGHLIGHT));
502             MoveToEx (hdc, rc.right / 2, rc.top, NULL);
503             LineTo (hdc, rc.right / 2, rc.bottom);
504             LineTo (hdc, rc.right / 2 -1 , rc.bottom);
505             SelectObject (hdc, hpen); 
506             break;
507
508         case SS_ETCHEDFRAME:
509             DrawEdge (hdc, &rc, EDGE_ETCHED, BF_RECT);
510             break;
511     }
512 }
513