Get rid of the SysColorPen stuff in user32, it's a wine concept. Use DC pen instead.
[reactos.git] / reactos / dll / win32 / user32 / controls / button.c
1 /* File: button.c -- Button type widgets
2 *
3 * Copyright (C) 1993 Johannes Ruscheinski
4 * Copyright (C) 1993 David Metcalfe
5 * Copyright (C) 1994 Alexandre Julliard
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 *
21 * NOTES
22 *
23 * This code was audited for completeness against the documented features
24 * of Comctl32.dll version 6.0 on Oct. 3, 2004, by Dimitrie O. Paun.
25 *
26 * Unless otherwise noted, we believe this code to be complete, as per
27 * the specification mentioned above.
28 * If you discover missing features, or bugs, please note them below.
29 *
30 * TODO
31 * Styles
32 * - BS_NOTIFY: is it complete?
33 * - BS_RIGHTBUTTON: same as BS_LEFTTEXT
34 * - BS_TYPEMASK
35 *
36 * Messages
37 * - WM_CHAR: Checks a (manual or automatic) check box on '+' or '=', clears it on '-' key.
38 * - WM_SETFOCUS: For (manual or automatic) radio buttons, send the parent window BN_CLICKED
39 * - WM_NCCREATE: Turns any BS_OWNERDRAW button into a BS_PUSHBUTTON button.
40 * - WM_SYSKEYUP
41 * - BCM_GETIDEALSIZE
42 * - BCM_GETIMAGELIST
43 * - BCM_GETTEXTMARGIN
44 * - BCM_SETIMAGELIST
45 * - BCM_SETTEXTMARGIN
46 *
47 * Notifications
48 * - BCN_HOTITEMCHANGE
49 * - BN_DISABLE
50 * - BN_PUSHED/BN_HILITE
51 * + BN_KILLFOCUS: is it OK?
52 * - BN_PAINT
53 * + BN_SETFOCUS: is it OK?
54 * - BN_UNPUSHED/BN_UNHILITE
55 * - NM_CUSTOMDRAW
56 *
57 * Structures/Macros/Definitions
58 * - BUTTON_IMAGELIST
59 * - NMBCHOTITEM
60 * - Button_GetIdealSize
61 * - Button_GetImageList
62 * - Button_GetTextMargin
63 * - Button_SetImageList
64 * - Button_SetTextMargin
65 */
66 #include <user32.h>
67
68 #include <wine/debug.h>
69 WINE_DEFAULT_DEBUG_CHANNEL(button);
70
71 /* GetWindowLong offsets for window extra information */
72 #define STATE_GWL_OFFSET 0
73 #define HFONT_GWL_OFFSET (sizeof(LONG))
74 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
75 #define UISTATE_GWL_OFFSET (HIMAGE_GWL_OFFSET+sizeof(HFONT))
76 #define NB_EXTRA_BYTES (UISTATE_GWL_OFFSET+sizeof(LONG))
77
78 /* Button state values */
79 #define BUTTON_UNCHECKED 0x00
80 #define BUTTON_CHECKED 0x01
81 #define BUTTON_3STATE 0x02
82 #define BUTTON_HIGHLIGHTED 0x04
83 #define BUTTON_HASFOCUS 0x08
84 #define BUTTON_NSTATES 0x0F
85 /* undocumented flags */
86 #define BUTTON_BTNPRESSED 0x40
87 #define BUTTON_UNKNOWN2 0x20
88 #define BUTTON_UNKNOWN3 0x10
89
90 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
91 do { /* Notify parent which has created this button control */ \
92 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
93 SendMessageW(GetParent(hWnd), WM_COMMAND, \
94 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
95 (LPARAM)(hWnd)); \
96 } while(0)
97
98 static UINT BUTTON_CalcLabelRect( HWND hwnd, HDC hdc, RECT *rc );
99 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
100 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
101 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
102 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
103 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
104 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
105 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
106 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
107
108 #define MAX_BTN_TYPE 12
109
110 static const WORD maxCheckState[MAX_BTN_TYPE] =
111 {
112 BUTTON_UNCHECKED, /* BS_PUSHBUTTON */
113 BUTTON_UNCHECKED, /* BS_DEFPUSHBUTTON */
114 BUTTON_CHECKED, /* BS_CHECKBOX */
115 BUTTON_CHECKED, /* BS_AUTOCHECKBOX */
116 BUTTON_CHECKED, /* BS_RADIOBUTTON */
117 BUTTON_3STATE, /* BS_3STATE */
118 BUTTON_3STATE, /* BS_AUTO3STATE */
119 BUTTON_UNCHECKED, /* BS_GROUPBOX */
120 BUTTON_UNCHECKED, /* BS_USERBUTTON */
121 BUTTON_CHECKED, /* BS_AUTORADIOBUTTON */
122 BUTTON_UNCHECKED, /* Not defined */
123 BUTTON_UNCHECKED /* BS_OWNERDRAW */
124 };
125
126 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
127
128 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
129 {
130 PB_Paint, /* BS_PUSHBUTTON */
131 PB_Paint, /* BS_DEFPUSHBUTTON */
132 CB_Paint, /* BS_CHECKBOX */
133 CB_Paint, /* BS_AUTOCHECKBOX */
134 CB_Paint, /* BS_RADIOBUTTON */
135 CB_Paint, /* BS_3STATE */
136 CB_Paint, /* BS_AUTO3STATE */
137 GB_Paint, /* BS_GROUPBOX */
138 UB_Paint, /* BS_USERBUTTON */
139 CB_Paint, /* BS_AUTORADIOBUTTON */
140 NULL, /* Not defined */
141 OB_Paint /* BS_OWNERDRAW */
142 };
143
144 static HBITMAP hbitmapCheckBoxes = 0;
145 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
146
147
148 /*********************************************************************
149 * button class descriptor
150 */
151 static const WCHAR buttonW[] = {'B','u','t','t','o','n',0};
152 const struct builtin_class_descr BUTTON_builtin_class =
153 {
154 buttonW, /* name */
155 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
156 ButtonWndProcA, /* procA */
157 ButtonWndProcW, /* procW */
158 NB_EXTRA_BYTES, /* extra */
159 IDC_ARROW, /* cursor */
160 0 /* brush */
161 };
162
163
164 static inline LONG get_button_state( HWND hwnd )
165 {
166 return GetWindowLongPtrW( hwnd, STATE_GWL_OFFSET );
167 }
168
169 static inline void set_button_state( HWND hwnd, LONG state )
170 {
171 SetWindowLongPtrW( hwnd, STATE_GWL_OFFSET, state );
172 }
173
174 static __inline void set_ui_state( HWND hwnd, LONG flags )
175 {
176 SetWindowLongPtrW( hwnd, UISTATE_GWL_OFFSET, flags );
177 }
178
179 static __inline LONG get_ui_state( HWND hwnd )
180 {
181 return GetWindowLongPtrW( hwnd, UISTATE_GWL_OFFSET );
182 }
183
184 __inline static HFONT get_button_font( HWND hwnd )
185 {
186 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
187 }
188
189 static inline void set_button_font( HWND hwnd, HFONT font )
190 {
191 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
192 }
193
194 static inline UINT get_button_type( LONG window_style )
195 {
196 return (window_style & 0x0f);
197 }
198
199 /* paint a button of any type */
200 static inline void paint_button( HWND hwnd, LONG style, UINT action )
201 {
202 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
203 {
204 HDC hdc = GetDC( hwnd );
205 btnPaintFunc[style]( hwnd, hdc, action );
206 ReleaseDC( hwnd, hdc );
207 }
208 }
209
210 /* retrieve the button text; returned buffer must be freed by caller */
211 static inline WCHAR *get_button_text( HWND hwnd )
212 {
213 INT len = 512;
214 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
215 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
216 return buffer;
217 }
218
219 static void setup_clipping( HWND hwnd, HDC hdc )
220 {
221 RECT rc;
222
223 GetClientRect( hwnd, &rc );
224 DPtoLP( hdc, (POINT *)&rc, 2 );
225 IntersectClipRect( hdc, rc.left, rc.top, rc.right, rc.bottom );
226 }
227
228 /* Retrieve the UI state for the control */
229 static BOOL button_update_uistate(HWND hwnd, BOOL unicode)
230 {
231 LONG flags, prevflags;
232
233 if (unicode)
234 flags = DefWindowProcW(hwnd, WM_QUERYUISTATE, 0, 0);
235 else
236 flags = DefWindowProcA(hwnd, WM_QUERYUISTATE, 0, 0);
237
238 prevflags = get_ui_state(hwnd);
239
240 if (prevflags != flags)
241 {
242 set_ui_state(hwnd, flags);
243 return TRUE;
244 }
245
246 return FALSE;
247 }
248
249 /***********************************************************************
250 * ButtonWndProc_common
251 */
252 static LRESULT ButtonWndProc_common(HWND hWnd, UINT uMsg,
253 WPARAM wParam, LPARAM lParam, BOOL unicode )
254 {
255 RECT rect;
256 POINT pt;
257 LONG style = GetWindowLongPtrW( hWnd, GWL_STYLE );
258 UINT btn_type = get_button_type( style );
259 LONG state;
260 HANDLE oldHbitmap;
261
262 pt.x = (short)LOWORD(lParam);
263 pt.y = (short)HIWORD(lParam);
264
265 switch (uMsg)
266 {
267 case WM_GETDLGCODE:
268 switch(btn_type)
269 {
270 case BS_USERBUTTON:
271 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
272 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
273 case BS_RADIOBUTTON:
274 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
275 case BS_GROUPBOX: return DLGC_STATIC;
276 default: return DLGC_BUTTON;
277 }
278
279 case WM_ENABLE:
280 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
281 break;
282
283 case WM_CREATE:
284 if (!hbitmapCheckBoxes)
285 {
286 BITMAP bmp;
287 hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
288 GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
289 checkBoxWidth = bmp.bmWidth / 4;
290 checkBoxHeight = bmp.bmHeight / 3;
291 }
292 if (btn_type >= MAX_BTN_TYPE)
293 return -1; /* abort */
294 set_button_state( hWnd, BUTTON_UNCHECKED );
295 button_update_uistate( hWnd, unicode );
296 return 0;
297
298 case WM_ERASEBKGND:
299 if (btn_type == BS_OWNERDRAW)
300 {
301 HDC hdc = (HDC)wParam;
302 RECT rc;
303 HBRUSH hBrush;
304 HWND parent = GetParent(hWnd);
305 if (!parent) parent = hWnd;
306 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
307 if (!hBrush) /* did the app forget to call defwindowproc ? */
308 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
309 (WPARAM)hdc, (LPARAM)hWnd);
310 GetClientRect(hWnd, &rc);
311 FillRect(hdc, &rc, hBrush);
312 }
313 return 1;
314
315 case WM_PRINTCLIENT:
316 case WM_PAINT:
317 if (btnPaintFunc[btn_type])
318 {
319 PAINTSTRUCT ps;
320 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
321 int nOldMode = SetBkMode( hdc, OPAQUE );
322 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
323 SetBkMode(hdc, nOldMode); /* reset painting mode */
324 if( !wParam ) EndPaint( hWnd, &ps );
325 }
326 break;
327
328 case WM_KEYDOWN:
329 if (wParam == VK_SPACE)
330 {
331 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
332 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
333 SetCapture( hWnd );
334 }
335 break;
336
337 case WM_LBUTTONDBLCLK:
338 if(style & BS_NOTIFY ||
339 btn_type == BS_RADIOBUTTON ||
340 btn_type == BS_USERBUTTON ||
341 btn_type == BS_OWNERDRAW)
342 {
343 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
344 break;
345 }
346 /* fall through */
347 case WM_LBUTTONDOWN:
348 SetCapture( hWnd );
349 SetFocus( hWnd );
350 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
351 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
352 break;
353
354 case WM_KEYUP:
355 if (wParam != VK_SPACE)
356 break;
357 /* fall through */
358 case WM_LBUTTONUP:
359 state = get_button_state( hWnd );
360 if (!(state & BUTTON_BTNPRESSED)) break;
361 state &= BUTTON_NSTATES;
362 set_button_state( hWnd, state );
363 if (!(state & BUTTON_HIGHLIGHTED))
364 {
365 ReleaseCapture();
366 break;
367 }
368 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
369 ReleaseCapture();
370 GetClientRect( hWnd, &rect );
371 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
372 {
373 state = get_button_state( hWnd );
374 switch(btn_type)
375 {
376 case BS_AUTOCHECKBOX:
377 SendMessageW( hWnd, BM_SETCHECK, !(state & BUTTON_CHECKED), 0 );
378 break;
379 case BS_AUTORADIOBUTTON:
380 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
381 break;
382 case BS_AUTO3STATE:
383 SendMessageW( hWnd, BM_SETCHECK,
384 (state & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
385 break;
386 }
387 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
388 }
389 break;
390
391 case WM_CAPTURECHANGED:
392 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
393 state = get_button_state( hWnd );
394 if (state & BUTTON_BTNPRESSED)
395 {
396 state &= BUTTON_NSTATES;
397 set_button_state( hWnd, state );
398 if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
399 }
400 break;
401
402 case WM_MOUSEMOVE:
403 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
404 {
405 GetClientRect( hWnd, &rect );
406 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
407 }
408 break;
409
410 case WM_SETTEXT:
411 {
412 /* Clear an old text here as Windows does */
413 HDC hdc = GetDC(hWnd);
414 HBRUSH hbrush;
415 RECT client, rc;
416 HWND parent = GetParent(hWnd);
417
418 if (!parent) parent = hWnd;
419 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
420 (WPARAM)hdc, (LPARAM)hWnd);
421 if (!hbrush) /* did the app forget to call DefWindowProc ? */
422 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
423 (WPARAM)hdc, (LPARAM)hWnd);
424
425 GetClientRect(hWnd, &client);
426 rc = client;
427 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
428 /* Clip by client rect bounds */
429 if (rc.right > client.right) rc.right = client.right;
430 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
431 FillRect(hdc, &rc, hbrush);
432 ReleaseDC(hWnd, hdc);
433
434 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
435 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
436 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
437 InvalidateRect( hWnd, NULL, TRUE );
438 else
439 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
440 return 1; /* success. FIXME: check text length */
441 }
442
443 case WM_SETFONT:
444 set_button_font( hWnd, (HFONT)wParam );
445 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
446 break;
447
448 case WM_GETFONT:
449 return (LRESULT)get_button_font( hWnd );
450
451 case WM_SETFOCUS:
452 TRACE("WM_SETFOCUS %p\n",hWnd);
453 set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
454 paint_button( hWnd, btn_type, ODA_FOCUS );
455 if (style & BS_NOTIFY)
456 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
457 break;
458
459 case WM_KILLFOCUS:
460 TRACE("WM_KILLFOCUS %p\n",hWnd);
461 state = get_button_state( hWnd );
462 set_button_state( hWnd, state & ~BUTTON_HASFOCUS );
463 paint_button( hWnd, btn_type, ODA_FOCUS );
464
465 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
466 ReleaseCapture();
467 if (style & BS_NOTIFY)
468 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
469
470 InvalidateRect( hWnd, NULL, FALSE );
471 break;
472
473 case WM_SYSCOLORCHANGE:
474 InvalidateRect( hWnd, NULL, FALSE );
475 break;
476
477 #ifndef __REACTOS__
478 case BM_SETSTYLE16:
479 #endif
480 case BM_SETSTYLE:
481 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
482 btn_type = wParam & 0x0f;
483 style = (style & ~0x0f) | btn_type;
484 SetWindowLongPtrW( hWnd, GWL_STYLE, style );
485
486 /* Only redraw if lParam flag is set.*/
487 if (lParam)
488 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
489
490 break;
491
492 case BM_CLICK:
493 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
494 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
495 break;
496
497 case BM_SETIMAGE:
498 /* Check that image format matches button style */
499 switch (style & (BS_BITMAP|BS_ICON))
500 {
501 case BS_BITMAP:
502 if (wParam != IMAGE_BITMAP) return 0;
503 break;
504 case BS_ICON:
505 if (wParam != IMAGE_ICON) return 0;
506 break;
507 default:
508 return 0;
509 }
510 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
511 InvalidateRect( hWnd, NULL, FALSE );
512 return (LRESULT)oldHbitmap;
513
514 case BM_GETIMAGE:
515 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
516
517 #ifndef __REACTOS__
518 case BM_GETCHECK16:
519 #endif
520 case BM_GETCHECK:
521 return get_button_state( hWnd ) & 3;
522
523 #ifndef __REACTOS__
524 case BM_SETCHECK16:
525 #endif
526 case BM_SETCHECK:
527 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
528 state = get_button_state( hWnd );
529 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
530 {
531 if (wParam) style |= WS_TABSTOP;
532 else style &= ~WS_TABSTOP;
533 SetWindowLongPtrW( hWnd, GWL_STYLE, style );
534 }
535 if ((state & 3) != wParam)
536 {
537 set_button_state( hWnd, (state & ~3) | wParam );
538 paint_button( hWnd, btn_type, ODA_SELECT );
539 }
540 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
541 BUTTON_CheckAutoRadioButton( hWnd );
542 break;
543
544 #ifndef __REACTOS__
545 case BM_GETSTATE16:
546 #endif
547 case BM_GETSTATE:
548 return get_button_state( hWnd );
549
550 #ifndef __REACTOS__
551 case BM_SETSTATE16:
552 #endif
553 case BM_SETSTATE:
554 state = get_button_state( hWnd );
555 if (wParam)
556 {
557 if (state & BUTTON_HIGHLIGHTED) break;
558 set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
559 }
560 else
561 {
562 if (!(state & BUTTON_HIGHLIGHTED)) break;
563 set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
564 }
565 paint_button( hWnd, btn_type, ODA_SELECT );
566 break;
567
568 case WM_UPDATEUISTATE:
569 if (unicode)
570 DefWindowProcW(hWnd, uMsg, wParam, lParam);
571 else
572 DefWindowProcA(hWnd, uMsg, wParam, lParam);
573
574 if (button_update_uistate(hWnd, unicode))
575 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
576 break;
577
578 case WM_NCHITTEST:
579 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
580 /* fall through */
581 default:
582 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
583 DefWindowProcA(hWnd, uMsg, wParam, lParam);
584 }
585 return 0;
586 }
587
588 /***********************************************************************
589 * ButtonWndProcW
590 * The button window procedure. This is just a wrapper which locks
591 * the passed HWND and calls the real window procedure (with a WND*
592 * pointer pointing to the locked windowstructure).
593 */
594 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
595 {
596 if (!IsWindow( hWnd )) return 0;
597 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, TRUE );
598 }
599
600
601 /***********************************************************************
602 * ButtonWndProcA
603 */
604 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
605 {
606 if (!IsWindow( hWnd )) return 0;
607 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, FALSE );
608 }
609
610
611 /**********************************************************************
612 * Convert button styles to flags used by DrawText.
613 */
614 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
615 {
616 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
617
618 /* "Convert" pushlike buttons to pushbuttons */
619 if (style & BS_PUSHLIKE)
620 style &= ~0x0F;
621
622 if (!(style & BS_MULTILINE))
623 dtStyle |= DT_SINGLELINE;
624 else
625 dtStyle |= DT_WORDBREAK;
626
627 switch (style & BS_CENTER)
628 {
629 case BS_LEFT: /* DT_LEFT is 0 */ break;
630 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
631 case BS_CENTER: dtStyle |= DT_CENTER; break;
632 default:
633 /* Pushbutton's text is centered by default */
634 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
635 /* all other flavours have left aligned text */
636 }
637
638 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
639
640 /* DrawText ignores vertical alignment for multiline text,
641 * but we use these flags to align label manually.
642 */
643 if (get_button_type(style) != BS_GROUPBOX)
644 {
645 switch (style & BS_VCENTER)
646 {
647 case BS_TOP: /* DT_TOP is 0 */ break;
648 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
649 case BS_VCENTER: /* fall through */
650 default: dtStyle |= DT_VCENTER; break;
651 }
652 }
653 else
654 /* GroupBox's text is always single line and is top aligned. */
655 dtStyle |= DT_SINGLELINE;
656
657 return dtStyle;
658 }
659
660 /**********************************************************************
661 * BUTTON_CalcLabelRect
662 *
663 * Calculates label's rectangle depending on button style.
664 *
665 * Returns flags to be passed to DrawText.
666 * Calculated rectangle doesn't take into account button state
667 * (pushed, etc.). If there is nothing to draw (no text/image) output
668 * rectangle is empty, and return value is (UINT)-1.
669 */
670 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
671 {
672 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
673 LONG ex_style = GetWindowLongPtrW( hwnd, GWL_EXSTYLE );
674 WCHAR *text;
675 ICONINFO iconInfo;
676 BITMAP bm;
677 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
678 RECT r = *rc;
679 INT n;
680
681 /* Calculate label rectangle according to label type */
682 switch (style & (BS_ICON|BS_BITMAP))
683 {
684 case BS_TEXT:
685 if (!(text = get_button_text( hwnd ))) goto empty_rect;
686 if (!text[0])
687 {
688 HeapFree( GetProcessHeap(), 0, text );
689 goto empty_rect;
690 }
691 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
692 HeapFree( GetProcessHeap(), 0, text );
693
694 if (get_ui_state( hwnd ) & UISF_HIDEACCEL)
695 dtStyle |= DT_HIDEPREFIX;
696 break;
697
698 case BS_ICON:
699 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
700 goto empty_rect;
701
702 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
703
704 r.right = r.left + bm.bmWidth;
705 r.bottom = r.top + bm.bmHeight;
706
707 DeleteObject(iconInfo.hbmColor);
708 DeleteObject(iconInfo.hbmMask);
709 break;
710
711 case BS_BITMAP:
712 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
713 goto empty_rect;
714
715 r.right = r.left + bm.bmWidth;
716 r.bottom = r.top + bm.bmHeight;
717 break;
718
719 default:
720 empty_rect:
721 rc->right = r.left;
722 rc->bottom = r.top;
723 return (UINT)-1;
724 }
725
726 /* Position label inside bounding rectangle according to
727 * alignment flags. (calculated rect is always left-top aligned).
728 * If label is aligned to any side - shift label in opposite
729 * direction to leave extra space for focus rectangle.
730 */
731 switch (dtStyle & (DT_CENTER|DT_RIGHT))
732 {
733 case DT_LEFT: r.left++; r.right++; break;
734 case DT_CENTER: n = r.right - r.left;
735 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
736 r.right = r.left + n; break;
737 case DT_RIGHT: n = r.right - r.left;
738 r.right = rc->right - 1;
739 r.left = r.right - n;
740 break;
741 }
742
743 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
744 {
745 case DT_TOP: r.top++; r.bottom++; break;
746 case DT_VCENTER: n = r.bottom - r.top;
747 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
748 r.bottom = r.top + n; break;
749 case DT_BOTTOM: n = r.bottom - r.top;
750 r.bottom = rc->bottom - 1;
751 r.top = r.bottom - n;
752 break;
753 }
754
755 *rc = r;
756 return dtStyle;
757 }
758
759
760 /**********************************************************************
761 * BUTTON_DrawTextCallback
762 *
763 * Callback function used by DrawStateW function.
764 */
765 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
766 {
767 RECT rc;
768 rc.left = 0;
769 rc.top = 0;
770 rc.right = cx;
771 rc.bottom = cy;
772
773 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
774 return TRUE;
775 }
776
777
778 /**********************************************************************
779 * BUTTON_DrawLabel
780 *
781 * Common function for drawing button label.
782 */
783 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
784 {
785 DRAWSTATEPROC lpOutputProc = NULL;
786 LPARAM lp;
787 WPARAM wp = 0;
788 HBRUSH hbr = 0;
789 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
790 LONG state = get_button_state( hwnd );
791 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
792 WCHAR *text = NULL;
793
794 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
795 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
796 * I don't have Win31 on hand to verify that, so I leave it as is.
797 */
798
799 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
800 {
801 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
802 flags |= DSS_MONO;
803 }
804
805 switch (style & (BS_ICON|BS_BITMAP))
806 {
807 case BS_TEXT:
808 /* DST_COMPLEX -- is 0 */
809 lpOutputProc = BUTTON_DrawTextCallback;
810 if (!(text = get_button_text( hwnd ))) return;
811 lp = (LPARAM)text;
812 wp = (WPARAM)dtFlags;
813
814 if (dtFlags & DT_HIDEPREFIX)
815 flags |= DSS_HIDEPREFIX;
816 break;
817
818 case BS_ICON:
819 flags |= DST_ICON;
820 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
821 break;
822
823 case BS_BITMAP:
824 flags |= DST_BITMAP;
825 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
826 break;
827
828 default:
829 return;
830 }
831
832 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
833 rc->right - rc->left, rc->bottom - rc->top, flags);
834 HeapFree( GetProcessHeap(), 0, text );
835 }
836
837 /**********************************************************************
838 * Push Button Functions
839 */
840 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
841 {
842 RECT rc, focus_rect, r;
843 UINT dtFlags, uState;
844 HPEN hOldPen;
845 HBRUSH hOldBrush;
846 INT oldBkMode;
847 COLORREF oldTxtColor;
848 HFONT hFont;
849 LONG state = get_button_state( hwnd );
850 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
851 BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
852 HWND parent;
853
854 GetClientRect( hwnd, &rc );
855
856 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
857 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
858 parent = GetParent(hwnd);
859 if (!parent) parent = hwnd;
860 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
861
862 setup_clipping( hwnd, hDC );
863 #ifdef __REACTOS__
864 hOldPen = SelectObject(hDC, GetStockObject(DC_PEN));
865 SetDCPenColor(hDC, GetSysColor(COLOR_WINDOWFRAME));
866 #else
867 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
868 #endif
869 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
870 oldBkMode = SetBkMode(hDC, TRANSPARENT);
871
872 if (get_button_type(style) == BS_DEFPUSHBUTTON)
873 {
874 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
875 InflateRect( &rc, -1, -1 );
876 }
877
878 uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
879
880 if (style & BS_FLAT)
881 uState |= DFCS_MONO;
882 else if (pushedState)
883 {
884 if (get_button_type(style) == BS_DEFPUSHBUTTON )
885 uState |= DFCS_FLAT;
886 else
887 uState |= DFCS_PUSHED;
888 }
889
890 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
891 uState |= DFCS_CHECKED;
892
893 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
894
895 focus_rect = rc;
896
897 /* draw button label */
898 r = rc;
899 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
900
901 if (dtFlags == (UINT)-1L)
902 goto cleanup;
903
904 if (pushedState)
905 OffsetRect(&r, 1, 1);
906
907 IntersectClipRect(hDC, rc.left, rc.top, rc.right, rc.bottom);
908
909 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
910
911 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
912
913 SetTextColor( hDC, oldTxtColor );
914
915 if (state & BUTTON_HASFOCUS)
916 {
917 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
918 {
919 InflateRect( &focus_rect, -1, -1 );
920 IntersectRect(&focus_rect, &focus_rect, &rc);
921 DrawFocusRect( hDC, &focus_rect );
922 }
923 }
924
925 cleanup:
926 SelectObject( hDC, hOldPen );
927 SelectObject( hDC, hOldBrush );
928 SetBkMode(hDC, oldBkMode);
929 }
930
931 /**********************************************************************
932 * Check Box & Radio Button Functions
933 */
934
935 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
936 {
937 RECT rbox, rtext, client;
938 HBRUSH hBrush;
939 int delta;
940 UINT dtFlags;
941 HFONT hFont;
942 LONG state = get_button_state( hwnd );
943 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
944 HWND parent;
945
946 if (style & BS_PUSHLIKE)
947 {
948 PB_Paint( hwnd, hDC, action );
949 return;
950 }
951
952 GetClientRect(hwnd, &client);
953 rbox = rtext = client;
954
955 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
956
957 parent = GetParent(hwnd);
958 if (!parent) parent = hwnd;
959 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
960 (WPARAM)hDC, (LPARAM)hwnd);
961 if (!hBrush) /* did the app forget to call defwindowproc ? */
962 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
963 (WPARAM)hDC, (LPARAM)hwnd );
964 setup_clipping( hwnd, hDC );
965
966 if (style & BS_LEFTTEXT)
967 {
968 /* magic +4 is what CTL3D expects */
969
970 rtext.right -= checkBoxWidth + 4;
971 rbox.left = rbox.right - checkBoxWidth;
972 }
973 else
974 {
975 rtext.left += checkBoxWidth + 4;
976 rbox.right = checkBoxWidth;
977 }
978
979 /* Since WM_ERASEBKGND does nothing, first prepare background */
980 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
981 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
982
983 /* Draw label */
984 client = rtext;
985 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
986
987 /* Only adjust rbox when rtext is valid */
988 if (dtFlags != (UINT)-1L)
989 {
990 rbox.top = rtext.top;
991 rbox.bottom = rtext.bottom;
992 }
993
994 /* Draw the check-box bitmap */
995 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
996 {
997 UINT flags;
998
999 if ((get_button_type(style) == BS_RADIOBUTTON) ||
1000 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
1001 else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
1002 else flags = DFCS_BUTTONCHECK;
1003
1004 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
1005 if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
1006
1007 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
1008
1009 /* rbox must have the correct height */
1010 delta = rbox.bottom - rbox.top - checkBoxHeight;
1011
1012 if (style & BS_TOP) {
1013 if (delta > 0) {
1014 rbox.bottom = rbox.top + checkBoxHeight;
1015 } else {
1016 rbox.top -= -delta/2 + 1;
1017 rbox.bottom = rbox.top + checkBoxHeight;
1018 }
1019 } else if (style & BS_BOTTOM) {
1020 if (delta > 0) {
1021 rbox.top = rbox.bottom - checkBoxHeight;
1022 } else {
1023 rbox.bottom += -delta/2 + 1;
1024 rbox.top = rbox.bottom - checkBoxHeight;
1025 }
1026 } else { /* Default */
1027 if (delta > 0) {
1028 int ofs = (delta / 2);
1029 rbox.bottom -= ofs + 1;
1030 rbox.top = rbox.bottom - checkBoxHeight;
1031 } else if (delta < 0) {
1032 int ofs = (-delta / 2);
1033 rbox.top -= ofs + 1;
1034 rbox.bottom = rbox.top + checkBoxHeight;
1035 }
1036 }
1037
1038 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
1039 }
1040
1041 if (dtFlags == (UINT)-1L) /* Noting to draw */
1042 return;
1043
1044 if (action == ODA_DRAWENTIRE)
1045 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
1046
1047 /* ... and focus */
1048 if ((action == ODA_FOCUS) ||
1049 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1050 {
1051 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
1052 {
1053 rtext.left--;
1054 rtext.right++;
1055 IntersectRect(&rtext, &rtext, &client);
1056 DrawFocusRect( hDC, &rtext );
1057 }
1058 }
1059 }
1060
1061
1062 /**********************************************************************
1063 * BUTTON_CheckAutoRadioButton
1064 *
1065 * hwnd is checked, uncheck every other auto radio button in group
1066 */
1067 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
1068 {
1069 HWND parent, sibling, start;
1070
1071 parent = GetParent(hwnd);
1072 /* make sure that starting control is not disabled or invisible */
1073 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
1074 do
1075 {
1076 if (!sibling) break;
1077 if ((hwnd != sibling) &&
1078 ((GetWindowLongPtrW( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
1079 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
1080 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1081 } while (sibling != start);
1082 }
1083
1084
1085 /**********************************************************************
1086 * Group Box Functions
1087 */
1088
1089 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1090 {
1091 RECT rc, rcFrame;
1092 HBRUSH hbr;
1093 HFONT hFont;
1094 UINT dtFlags;
1095 TEXTMETRICW tm;
1096 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
1097 HWND parent;
1098
1099 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1100 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1101 parent = GetParent(hwnd);
1102 if (!parent) parent = hwnd;
1103 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1104 if (!hbr) /* did the app forget to call defwindowproc ? */
1105 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1106 (WPARAM)hDC, (LPARAM)hwnd);
1107 setup_clipping( hwnd, hDC );
1108
1109 GetClientRect( hwnd, &rc);
1110 rcFrame = rc;
1111
1112 GetTextMetricsW (hDC, &tm);
1113 rcFrame.top += (tm.tmHeight / 2) - 1;
1114 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1115
1116 InflateRect(&rc, -7, 1);
1117 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1118
1119 if (dtFlags == (UINT)-1L)
1120 return;
1121
1122 /* Because buttons have CS_PARENTDC class style, there is a chance
1123 * that label will be drawn out of client rect.
1124 * But Windows doesn't clip label's rect, so do I.
1125 */
1126
1127 /* There is 1-pixel margin at the left, right, and bottom */
1128 rc.left--; rc.right++; rc.bottom++;
1129 FillRect(hDC, &rc, hbr);
1130 rc.left++; rc.right--; rc.bottom--;
1131
1132 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1133 }
1134
1135
1136 /**********************************************************************
1137 * User Button Functions
1138 */
1139
1140 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1141 {
1142 RECT rc;
1143 HBRUSH hBrush;
1144 HFONT hFont;
1145 LONG state = get_button_state( hwnd );
1146 HWND parent;
1147
1148 if (action == ODA_SELECT) return;
1149
1150 GetClientRect( hwnd, &rc);
1151
1152 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1153
1154 parent = GetParent(hwnd);
1155 if (!parent) parent = hwnd;
1156 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1157 if (!hBrush) /* did the app forget to call defwindowproc ? */
1158 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1159 (WPARAM)hDC, (LPARAM)hwnd);
1160
1161 FillRect( hDC, &rc, hBrush );
1162 if ((action == ODA_FOCUS) ||
1163 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1164 {
1165 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
1166 DrawFocusRect( hDC, &rc );
1167 }
1168 }
1169
1170
1171 /**********************************************************************
1172 * Ownerdrawn Button Functions
1173 */
1174
1175 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1176 {
1177 LONG state = get_button_state( hwnd );
1178 DRAWITEMSTRUCT dis;
1179 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1180 HWND parent;
1181 HFONT hFont, hPrevFont = 0;
1182
1183 dis.CtlType = ODT_BUTTON;
1184 dis.CtlID = id;
1185 dis.itemID = 0;
1186 dis.itemAction = action;
1187 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1188 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1189 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1190 dis.hwndItem = hwnd;
1191 dis.hDC = hDC;
1192 dis.itemData = 0;
1193 GetClientRect( hwnd, &dis.rcItem );
1194
1195 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1196 parent = GetParent(hwnd);
1197 if (!parent) parent = hwnd;
1198 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1199
1200 setup_clipping( hwnd, hDC );
1201
1202 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1203 if (hPrevFont) SelectObject(hDC, hPrevFont);
1204 }