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