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