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