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