On mouse down in thumb, issue SB_THUMBTRACK with current position.
[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, TRUE);
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, TRUE);
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))
339             InvalidateRect( hwnd, NULL, TRUE );
340         break;
341
342     case WM_GETFONT:
343         return GetWindowLongA( hwnd, HFONT_GWL_OFFSET );
344
345     case WM_NCHITTEST:
346         if (full_style & SS_NOTIFY)
347            return HTCLIENT;
348         else
349            return HTTRANSPARENT;
350
351     case WM_GETDLGCODE:
352         return DLGC_STATIC;
353
354     case STM_GETIMAGE:
355     case STM_GETICON16:
356     case STM_GETICON:
357         return GetWindowLongA( hwnd, HICON_GWL_OFFSET );
358
359     case STM_SETIMAGE:
360         switch(wParam) {
361         case IMAGE_BITMAP:
362             lResult = STATIC_SetBitmap( hwnd, (HBITMAP)lParam, style );
363             break;
364         case IMAGE_ICON:
365             lResult = STATIC_SetIcon( hwnd, (HICON)lParam, style );
366             break;
367         default:
368             FIXME("STM_SETIMAGE: Unhandled type %x\n", wParam);
369             break;
370         }
371         InvalidateRect( hwnd, NULL, TRUE );
372         break;
373
374     case STM_SETICON16:
375     case STM_SETICON:
376         lResult = STATIC_SetIcon( hwnd, (HICON)wParam, style );
377         InvalidateRect( hwnd, NULL, TRUE );
378         break;
379
380     default:
381         return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
382                          DefWindowProcA(hwnd, uMsg, wParam, lParam);
383     }
384     return lResult;
385 }
386
387 /***********************************************************************
388  *           StaticWndProcA
389  */
390 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
391 {
392     if (!IsWindow( hWnd )) return 0;
393     return StaticWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
394 }
395
396 /***********************************************************************
397  *           StaticWndProcW
398  */
399 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
400 {
401     if (!IsWindow( hWnd )) return 0;
402     return StaticWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
403 }
404
405 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style )
406 {
407   DRAWITEMSTRUCT dis;
408   LONG id = GetWindowLongA( hwnd, GWL_ID );
409
410   dis.CtlType    = ODT_STATIC;
411   dis.CtlID      = id;
412   dis.itemID     = 0;
413   dis.itemAction = ODA_DRAWENTIRE;
414   dis.itemState  = 0;
415   dis.hwndItem   = hwnd;
416   dis.hDC        = hdc;
417   dis.itemData   = 0;
418   GetClientRect( hwnd, &dis.rcItem );
419
420   SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
421   SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
422 }
423
424 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style )
425 {
426     RECT rc;
427     HBRUSH hBrush;
428     HFONT hFont;
429     WORD wFormat;
430     INT len;
431     WCHAR *text;
432
433     GetClientRect( hwnd, &rc);
434
435     switch (style & SS_TYPEMASK)
436     {
437     case SS_LEFT:
438         wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
439         break;
440
441     case SS_CENTER:
442         wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
443         break;
444
445     case SS_RIGHT:
446         wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
447         break;
448
449     case SS_SIMPLE:
450         wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOCLIP;
451         break;
452
453     case SS_LEFTNOWORDWRAP:
454         wFormat = DT_LEFT | DT_EXPANDTABS | DT_VCENTER;
455         break;
456
457     default:
458         return;
459     }
460
461     if (style & SS_NOPREFIX)
462         wFormat |= DT_NOPREFIX;
463
464     if ((hFont = GetWindowLongA( hwnd, HFONT_GWL_OFFSET ))) SelectObject( hdc, hFont );
465
466     if ((style & SS_NOPREFIX) || ((style & SS_TYPEMASK) != SS_SIMPLE))
467     {
468         hBrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
469         if (!hBrush) /* did the app forget to call defwindowproc ? */
470             hBrush = DefWindowProcW(GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd);
471         FillRect( hdc, &rc, hBrush );
472     }
473     if (!IsWindowEnabled(hwnd)) SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
474
475     if (!(len = SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 ))) return;
476     if (!(text = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return;
477     SendMessageW( hwnd, WM_GETTEXT, len + 1, (LPARAM)text );
478     DrawTextW( hdc, text, -1, &rc, wFormat );
479     HeapFree( GetProcessHeap(), 0, text );
480 }
481
482 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style )
483 {
484     RECT rc;
485     HBRUSH hBrush;
486
487     GetClientRect( hwnd, &rc);
488
489     switch (style & SS_TYPEMASK)
490     {
491     case SS_BLACKRECT:
492         hBrush = CreateSolidBrush(color_windowframe);
493         FillRect( hdc, &rc, hBrush );
494         break;
495     case SS_GRAYRECT:
496         hBrush = CreateSolidBrush(color_background);
497         FillRect( hdc, &rc, hBrush );
498         break;
499     case SS_WHITERECT:
500         hBrush = CreateSolidBrush(color_window);
501         FillRect( hdc, &rc, hBrush );
502         break;
503     case SS_BLACKFRAME:
504         hBrush = CreateSolidBrush(color_windowframe);
505         FrameRect( hdc, &rc, hBrush );
506         break;
507     case SS_GRAYFRAME:
508         hBrush = CreateSolidBrush(color_background);
509         FrameRect( hdc, &rc, hBrush );
510         break;
511     case SS_WHITEFRAME:
512         hBrush = CreateSolidBrush(color_window);
513         FrameRect( hdc, &rc, hBrush );
514         break;
515     default:
516         return;
517     }
518     DeleteObject( hBrush );
519 }
520
521
522 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style )
523 {
524     RECT rc;
525     HBRUSH hbrush;
526     HICON hIcon;
527
528     GetClientRect( hwnd, &rc );
529     hbrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
530     FillRect( hdc, &rc, hbrush );
531     if ((hIcon = GetWindowLongA( hwnd, HICON_GWL_OFFSET )))
532         DrawIcon( hdc, rc.left, rc.top, hIcon );
533 }
534
535 static void STATIC_PaintBitmapfn(HWND hwnd, HDC hdc, DWORD style )
536 {
537     RECT rc;
538     HBRUSH hbrush;
539     HICON hIcon;
540     HDC hMemDC;
541     HBITMAP oldbitmap;
542
543     GetClientRect( hwnd, &rc );
544     hbrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
545     FillRect( hdc, &rc, hbrush );
546
547     if ((hIcon = GetWindowLongA( hwnd, HICON_GWL_OFFSET )))
548     {
549         BITMAP bm;
550         SIZE sz;
551
552         if(GetObjectType(hIcon) != OBJ_BITMAP) return;
553         if (!(hMemDC = CreateCompatibleDC( hdc ))) return;
554         GetObjectW(hIcon, sizeof(bm), &bm);
555         GetBitmapDimensionEx(hIcon, &sz);
556         oldbitmap = SelectObject(hMemDC, hIcon);
557         BitBlt(hdc, sz.cx, sz.cy, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0,
558                SRCCOPY);
559         SelectObject(hMemDC, oldbitmap);
560         DeleteDC(hMemDC);
561     }
562 }
563
564
565 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style )
566 {
567     RECT rc;
568
569     if (TWEAK_WineLook == WIN31_LOOK)
570         return;
571
572     GetClientRect( hwnd, &rc );
573     switch (style & SS_TYPEMASK)
574     {
575         case SS_ETCHEDHORZ:
576             DrawEdge(hdc,&rc,EDGE_ETCHED,BF_TOP|BF_BOTTOM);
577             break;
578         case SS_ETCHEDVERT:
579             DrawEdge(hdc,&rc,EDGE_ETCHED,BF_LEFT|BF_RIGHT);
580             break;
581         case SS_ETCHEDFRAME:
582             DrawEdge (hdc, &rc, EDGE_ETCHED, BF_RECT);
583             break;
584     }
585 }