Release 980913
[wine] / controls / static.c
1 /*
2  * Static control
3  *
4  * Copyright  David W. Metcalfe, 1993
5  *
6  */
7
8 #include "windows.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, HDC32 hdc );
18 static void STATIC_PaintRectfn( WND *wndPtr, HDC32 hdc );
19 static void STATIC_PaintIconfn( WND *wndPtr, HDC32 hdc );
20 static void STATIC_PaintBitmapfn( WND *wndPtr, HDC32 hdc );
21 static void STATIC_PaintEtchedfn( WND *wndPtr, HDC32 hdc );
22
23 static COLORREF color_windowframe, color_background, color_window;
24
25
26 typedef void (*pfPaint)( WND *, HDC32 );
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         SetWindowPos32( 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         SetWindowPos32( 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 = LoadIcon32A( 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 = LoadIcon32A( 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 = LoadBitmap32A( wndPtr->hInstance, name );
149         if (!hbitmap)  /* Try OEM icon (FIXME: is this right?) */
150             hbitmap = LoadBitmap32A( 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 = LoadBitmap32A( 0, segname );
158         SEGPTR_FREE(segname);
159     }
160     return hbitmap;
161 }
162
163
164 /***********************************************************************
165  *           StaticWndProc
166  */
167 LRESULT WINAPI StaticWndProc( HWND32 hWnd, UINT32 uMsg, WPARAM32 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         if (TWEAK_Win95Look && (wndPtr->dwStyle & SS_SUNKEN))
179             wndPtr->dwExStyle |= WS_EX_STATICEDGE;
180
181         if (style == SS_ICON)
182         {
183             CREATESTRUCT32A *cs = (CREATESTRUCT32A *)lParam;
184             if (cs->lpszName)
185                 STATIC_SetIcon( wndPtr,
186                                 STATIC_LoadIcon( wndPtr, cs->lpszName ));
187             return 1;
188         }
189         if (style == SS_BITMAP)
190         {
191             CREATESTRUCT32A *cs = (CREATESTRUCT32A *)lParam;
192             if (cs->lpszName)
193                 STATIC_SetBitmap( wndPtr,
194                                 STATIC_LoadBitmap( wndPtr, cs->lpszName ));
195             WARN(static, "style SS_BITMAP, dwStyle is 0x%08lx\n",
196                         wndPtr->dwStyle);
197             return 1;
198         }
199         return DefWindowProc32A( hWnd, uMsg, wParam, lParam );
200
201     case WM_CREATE:
202         if (style < 0L || style > SS_TYPEMASK)
203         {
204             ERR(static, "Unknown style 0x%02lx\n", style );
205             lResult = -1L;
206             break;
207         }
208         /* initialise colours */
209         color_windowframe  = GetSysColor32(COLOR_WINDOWFRAME);
210         color_background   = GetSysColor32(COLOR_BACKGROUND);
211         color_window       = GetSysColor32(COLOR_WINDOW);
212         break;
213
214     case WM_NCDESTROY:
215         if (style == SS_ICON)
216             DestroyIcon32( STATIC_SetIcon( wndPtr, 0 ) );
217         else
218             lResult = DefWindowProc32A( hWnd, uMsg, wParam, lParam );
219         break;
220
221     case WM_PAINT:
222         {
223             PAINTSTRUCT32 ps;
224             BeginPaint32( hWnd, &ps );
225             if (staticPaintFunc[style])
226                 (staticPaintFunc[style])( wndPtr, ps.hdc );
227             EndPaint32( hWnd, &ps );
228         }
229         break;
230
231     case WM_ENABLE:
232         InvalidateRect32( hWnd, NULL, FALSE );
233         break;
234
235     case WM_SYSCOLORCHANGE:
236         color_windowframe  = GetSysColor32(COLOR_WINDOWFRAME);
237         color_background   = GetSysColor32(COLOR_BACKGROUND);
238         color_window       = GetSysColor32(COLOR_WINDOW);
239         InvalidateRect32( hWnd, NULL, TRUE );
240         break;
241
242     case WM_SETTEXT:
243         if (style == SS_ICON)
244             /* FIXME : should we also return the previous hIcon here ??? */
245             STATIC_SetIcon( wndPtr, STATIC_LoadIcon( wndPtr, (LPCSTR)lParam ));
246         else if (style == SS_BITMAP) 
247             STATIC_SetBitmap(wndPtr,STATIC_LoadBitmap(wndPtr,(LPCSTR)lParam ));
248         else
249             DEFWND_SetText( wndPtr, (LPCSTR)lParam );
250         InvalidateRect32( hWnd, NULL, FALSE );
251         UpdateWindow32( hWnd );
252         break;
253
254     case WM_SETFONT:
255         if (style == SS_ICON) return 0;
256         if (style == SS_BITMAP) return 0;
257         infoPtr->hFont = (HFONT16)wParam;
258         if (LOWORD(lParam))
259         {
260             InvalidateRect32( hWnd, NULL, FALSE );
261             UpdateWindow32( hWnd );
262         }
263         break;
264
265     case WM_GETFONT:
266         return infoPtr->hFont;
267
268     case WM_NCHITTEST:
269         return HTTRANSPARENT;
270
271     case WM_GETDLGCODE:
272         return DLGC_STATIC;
273
274     case STM_GETIMAGE:
275     case STM_GETICON16:
276     case STM_GETICON32:
277         return infoPtr->hIcon;
278
279     case STM_SETIMAGE:
280         /* FIXME: handle wParam */
281         lResult = STATIC_SetBitmap( wndPtr, (HBITMAP32)lParam );
282         InvalidateRect32( hWnd, NULL, FALSE );
283         UpdateWindow32( hWnd );
284         break;
285
286     case STM_SETICON16:
287     case STM_SETICON32:
288         lResult = STATIC_SetIcon( wndPtr, (HICON16)wParam );
289         InvalidateRect32( hWnd, NULL, FALSE );
290         UpdateWindow32( hWnd );
291         break;
292
293     default:
294         lResult = DefWindowProc32A(hWnd, uMsg, wParam, lParam);
295         break;
296     }
297     
298     return lResult;
299 }
300
301
302 static void STATIC_PaintTextfn( WND *wndPtr, HDC32 hdc )
303 {
304     RECT32 rc;
305     HBRUSH32 hBrush;
306     WORD wFormat;
307
308     LONG style = wndPtr->dwStyle;
309     STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
310
311     GetClientRect32( wndPtr->hwndSelf, &rc);
312
313     switch (style & SS_TYPEMASK)
314     {
315     case SS_LEFT:
316         wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
317         break;
318
319     case SS_CENTER:
320         wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
321         break;
322
323     case SS_RIGHT:
324         wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
325         break;
326
327     case SS_SIMPLE:
328         wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOCLIP;
329         break;
330
331     case SS_LEFTNOWORDWRAP:
332         wFormat = DT_LEFT | DT_SINGLELINE | DT_EXPANDTABS | DT_VCENTER | DT_NOCLIP;
333         break;
334
335     default:
336         return;
337     }
338
339     if (style & SS_NOPREFIX)
340         wFormat |= DT_NOPREFIX;
341
342     if (infoPtr->hFont) SelectObject32( hdc, infoPtr->hFont );
343     hBrush = SendMessage32A( GetParent32(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
344                              hdc, wndPtr->hwndSelf );
345     if (!hBrush) hBrush = GetStockObject32(WHITE_BRUSH);
346     FillRect32( hdc, &rc, hBrush );
347     if (wndPtr->text) DrawText32A( hdc, wndPtr->text, -1, &rc, wFormat );
348 }
349
350 static void STATIC_PaintRectfn( WND *wndPtr, HDC32 hdc )
351 {
352     RECT32 rc;
353     HBRUSH32 hBrush;
354
355     GetClientRect32( wndPtr->hwndSelf, &rc);
356     
357     switch (wndPtr->dwStyle & SS_TYPEMASK)
358     {
359     case SS_BLACKRECT:
360         hBrush = CreateSolidBrush32(color_windowframe);
361         FillRect32( hdc, &rc, hBrush );
362         break;
363     case SS_GRAYRECT:
364         hBrush = CreateSolidBrush32(color_background);
365         FillRect32( hdc, &rc, hBrush );
366         break;
367     case SS_WHITERECT:
368         hBrush = CreateSolidBrush32(color_window);
369         FillRect32( hdc, &rc, hBrush );
370         break;
371     case SS_BLACKFRAME:
372         hBrush = CreateSolidBrush32(color_windowframe);
373         FrameRect32( hdc, &rc, hBrush );
374         break;
375     case SS_GRAYFRAME:
376         hBrush = CreateSolidBrush32(color_background);
377         FrameRect32( hdc, &rc, hBrush );
378         break;
379     case SS_WHITEFRAME:
380         hBrush = CreateSolidBrush32(color_window);
381         FrameRect32( hdc, &rc, hBrush );
382         break;
383     default:
384         return;
385     }
386     DeleteObject32( hBrush );
387 }
388
389
390 static void STATIC_PaintIconfn( WND *wndPtr, HDC32 hdc )
391 {
392     RECT32 rc;
393     HBRUSH32 hbrush;
394     STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
395
396     GetClientRect32( wndPtr->hwndSelf, &rc );
397     hbrush = SendMessage32A( GetParent32(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
398                              hdc, wndPtr->hwndSelf );
399     FillRect32( hdc, &rc, hbrush );
400     if (infoPtr->hIcon) DrawIcon32( hdc, rc.left, rc.top, infoPtr->hIcon );
401 }
402
403 static void STATIC_PaintBitmapfn(WND *wndPtr, HDC32 hdc )
404 {
405     RECT32 rc;
406     HBRUSH32 hbrush;
407     STATICINFO *infoPtr = (STATICINFO *)wndPtr->wExtra;
408     HDC32 hMemDC;
409     HBITMAP32 oldbitmap;
410
411     GetClientRect32( wndPtr->hwndSelf, &rc );
412     hbrush = SendMessage32A( GetParent32(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
413                              hdc, wndPtr->hwndSelf );
414     FillRect32( hdc, &rc, hbrush );
415     if (infoPtr->hIcon) {
416         BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_HEAP_LOCK( infoPtr->hIcon );
417
418         if (!bmp) return;
419         if (!(hMemDC = CreateCompatibleDC32( hdc ))) return;
420
421         oldbitmap = SelectObject32(hMemDC,infoPtr->hIcon);
422         BitBlt32(hdc,bmp->size.cx,bmp->size.cy,bmp->bitmap.bmWidth,bmp->bitmap.bmHeight,hMemDC,0,0,SRCCOPY);
423         DeleteDC32(hMemDC);
424         GDI_HEAP_UNLOCK(infoPtr->hIcon);
425     }
426 }
427
428
429 static void STATIC_PaintEtchedfn( WND *wndPtr, HDC32 hdc )
430 {
431     RECT32 rc;
432     HBRUSH32 hbrush;
433     HPEN32 hpen;
434
435     if (!TWEAK_Win95Look) return;
436
437     GetClientRect32( wndPtr->hwndSelf, &rc );
438     hbrush = SendMessage32A( GetParent32(wndPtr->hwndSelf), WM_CTLCOLORSTATIC,
439                              hdc, wndPtr->hwndSelf );
440     FillRect32( hdc, &rc, hbrush );
441
442     switch (wndPtr->dwStyle & SS_TYPEMASK)
443     {
444         case SS_ETCHEDHORZ:
445             hpen = SelectObject32 (hdc, GetSysColorPen32 (COLOR_3DSHADOW));
446             MoveToEx32 (hdc, rc.left, rc.bottom / 2 - 1, NULL);
447             LineTo32 (hdc, rc.right - 1, rc.bottom / 2 - 1);
448             SelectObject32 (hdc, GetSysColorPen32 (COLOR_3DHIGHLIGHT));
449             MoveToEx32 (hdc, rc.left, rc.bottom / 2, NULL);
450             LineTo32 (hdc, rc.right, rc.bottom / 2);
451             LineTo32 (hdc, rc.right, rc.bottom / 2 - 1);
452             SelectObject32 (hdc, hpen);
453             break;
454
455         case SS_ETCHEDVERT:
456             hpen = SelectObject32 (hdc, GetSysColorPen32 (COLOR_3DSHADOW));
457             MoveToEx32 (hdc, rc.right / 2 - 1, rc.top, NULL);
458             LineTo32 (hdc, rc.right / 2 - 1, rc.bottom - 1);
459             SelectObject32 (hdc, GetSysColorPen32 (COLOR_3DHIGHLIGHT));
460             MoveToEx32 (hdc, rc.right / 2, rc.top, NULL);
461             LineTo32 (hdc, rc.right / 2, rc.bottom);
462             LineTo32 (hdc, rc.right / 2 -1 , rc.bottom);
463             SelectObject32 (hdc, hpen); 
464             break;
465
466         case SS_ETCHEDFRAME:
467             DrawEdge32 (hdc, &rc, EDGE_ETCHED, BF_RECT);
468             break;
469     }
470 }
471