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