Bring back ext2 code from branch
[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 const struct builtin_class_descr BUTTON_builtin_class =
152 {
153 #ifdef __REACTOS__
154 L"Button", /* name */
155 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
156 ButtonWndProcW, /* procW */
157 ButtonWndProcA, /* procA */
158 NB_EXTRA_BYTES, /* extra */
159 (LPWSTR)IDC_ARROW, /* cursor */
160 0 /* brush */
161 #else
162 "Button", /* name */
163 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
164 ButtonWndProcA, /* procA */
165 ButtonWndProcW, /* procW */
166 NB_EXTRA_BYTES, /* extra */
167 IDC_ARROW, /* cursor */
168 0 /* brush */
169 #endif
170 };
171
172
173 __inline static LONG get_button_state( HWND hwnd )
174 {
175 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
176 }
177
178 __inline static void set_button_state( HWND hwnd, LONG state )
179 {
180 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
181 }
182
183 static __inline void set_ui_state( HWND hwnd, LONG flags )
184 {
185 SetWindowLongW( hwnd, UISTATE_GWL_OFFSET, flags );
186 }
187
188 static __inline LONG get_ui_state( HWND hwnd )
189 {
190 return GetWindowLongPtrW( hwnd, UISTATE_GWL_OFFSET );
191 }
192
193 __inline static HFONT get_button_font( HWND hwnd )
194 {
195 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
196 }
197
198 __inline static void set_button_font( HWND hwnd, HFONT font )
199 {
200 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
201 }
202
203 __inline static UINT get_button_type( LONG window_style )
204 {
205 return (window_style & 0x0f);
206 }
207
208 /* paint a button of any type */
209 __inline static void paint_button( HWND hwnd, LONG style, UINT action )
210 {
211 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
212 {
213 HDC hdc = GetDC( hwnd );
214 btnPaintFunc[style]( hwnd, hdc, action );
215 ReleaseDC( hwnd, hdc );
216 }
217 }
218
219 /* retrieve the button text; returned buffer must be freed by caller */
220 __inline static WCHAR *get_button_text( HWND hwnd )
221 {
222 INT len = 512;
223 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
224 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
225 return buffer;
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 WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
253 WPARAM wParam, LPARAM lParam, BOOL unicode )
254 {
255 RECT rect;
256 POINT pt;
257 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
258 UINT btn_type = get_button_type( style );
259 LONG state;
260 HANDLE oldHbitmap;
261
262 pt.x = LOWORD(lParam);
263 pt.y = 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 }
334 break;
335
336 case WM_LBUTTONDBLCLK:
337 if(style & BS_NOTIFY ||
338 btn_type == BS_RADIOBUTTON ||
339 btn_type == BS_USERBUTTON ||
340 btn_type == BS_OWNERDRAW)
341 {
342 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
343 break;
344 }
345 /* fall through */
346 case WM_LBUTTONDOWN:
347 SetCapture( hWnd );
348 SetFocus( hWnd );
349 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
350 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
351 break;
352
353 case WM_KEYUP:
354 if (wParam != VK_SPACE)
355 break;
356 /* fall through */
357 case WM_LBUTTONUP:
358 state = get_button_state( hWnd );
359 if (!(state & BUTTON_BTNPRESSED)) break;
360 state &= BUTTON_NSTATES;
361 set_button_state( hWnd, state );
362 if (!(state & BUTTON_HIGHLIGHTED))
363 {
364 ReleaseCapture();
365 break;
366 }
367 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
368 ReleaseCapture();
369 GetClientRect( hWnd, &rect );
370 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
371 {
372 state = get_button_state( hWnd );
373 switch(btn_type)
374 {
375 case BS_AUTOCHECKBOX:
376 SendMessageW( hWnd, BM_SETCHECK, !(state & BUTTON_CHECKED), 0 );
377 break;
378 case BS_AUTORADIOBUTTON:
379 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
380 break;
381 case BS_AUTO3STATE:
382 SendMessageW( hWnd, BM_SETCHECK,
383 (state & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
384 break;
385 }
386 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
387 }
388 break;
389
390 case WM_CAPTURECHANGED:
391 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
392 state = get_button_state( hWnd );
393 if (state & BUTTON_BTNPRESSED)
394 {
395 state &= BUTTON_NSTATES;
396 set_button_state( hWnd, state );
397 if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
398 }
399 break;
400
401 case WM_MOUSEMOVE:
402 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
403 {
404 GetClientRect( hWnd, &rect );
405 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
406 }
407 break;
408
409 case WM_SETTEXT:
410 {
411 /* Clear an old text here as Windows does */
412 HDC hdc = GetDC(hWnd);
413 HBRUSH hbrush;
414 RECT client, rc;
415 HWND parent = GetParent(hWnd);
416
417 if (!parent) parent = hWnd;
418 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
419 (WPARAM)hdc, (LPARAM)hWnd);
420 if (!hbrush) /* did the app forget to call DefWindowProc ? */
421 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
422 (WPARAM)hdc, (LPARAM)hWnd);
423
424 GetClientRect(hWnd, &client);
425 rc = client;
426 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
427 /* Clip by client rect bounds */
428 if (rc.right > client.right) rc.right = client.right;
429 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
430 FillRect(hdc, &rc, hbrush);
431 ReleaseDC(hWnd, hdc);
432
433 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
434 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
435 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
436 NtUserInvalidateRect( hWnd, NULL, TRUE );
437 else
438 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
439 return 1; /* success. FIXME: check text length */
440 }
441
442 case WM_SETFONT:
443 set_button_font( hWnd, (HFONT)wParam );
444 if (lParam) paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
445 break;
446
447 case WM_GETFONT:
448 return (LRESULT)get_button_font( hWnd );
449
450 case WM_SETFOCUS:
451 TRACE("WM_SETFOCUS %p\n",hWnd);
452 set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
453 paint_button( hWnd, btn_type, ODA_FOCUS );
454 if (style & BS_NOTIFY)
455 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
456 break;
457
458 case WM_KILLFOCUS:
459 TRACE("WM_KILLFOCUS %p\n",hWnd);
460 state = get_button_state( hWnd );
461 set_button_state( hWnd, state & ~BUTTON_HASFOCUS );
462 paint_button( hWnd, btn_type, ODA_FOCUS );
463
464 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
465 ReleaseCapture();
466 if (style & BS_NOTIFY)
467 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
468
469 break;
470
471 case WM_SYSCOLORCHANGE:
472 NtUserInvalidateRect( hWnd, NULL, FALSE );
473 break;
474
475 #ifndef __REACTOS__
476 case BM_SETSTYLE16:
477 #endif
478 case BM_SETSTYLE:
479 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
480 btn_type = wParam & 0x0f;
481 style = (style & ~0x0f) | btn_type;
482 SetWindowLongW( hWnd, GWL_STYLE, style );
483
484 /* Only redraw if lParam flag is set.*/
485 if (lParam)
486 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
487
488 break;
489
490 case BM_CLICK:
491 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
492 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
493 break;
494
495 case BM_SETIMAGE:
496 /* Check that image format matches button style */
497 switch (style & (BS_BITMAP|BS_ICON))
498 {
499 case BS_BITMAP:
500 if (wParam != IMAGE_BITMAP) return 0;
501 break;
502 case BS_ICON:
503 if (wParam != IMAGE_ICON) return 0;
504 break;
505 default:
506 return 0;
507 }
508 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
509 NtUserInvalidateRect( hWnd, NULL, FALSE );
510 return (LRESULT)oldHbitmap;
511
512 case BM_GETIMAGE:
513 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
514
515 #ifndef __REACTOS__
516 case BM_GETCHECK16:
517 #endif
518 case BM_GETCHECK:
519 return get_button_state( hWnd ) & 3;
520
521 #ifndef __REACTOS__
522 case BM_SETCHECK16:
523 #endif
524 case BM_SETCHECK:
525 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
526 state = get_button_state( hWnd );
527 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
528 {
529 if (wParam) style |= WS_TABSTOP;
530 else style &= ~WS_TABSTOP;
531 SetWindowLongW( hWnd, GWL_STYLE, style );
532 }
533 if ((state & 3) != wParam)
534 {
535 set_button_state( hWnd, (state & ~3) | wParam );
536 paint_button( hWnd, btn_type, ODA_SELECT );
537 }
538 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
539 BUTTON_CheckAutoRadioButton( hWnd );
540 break;
541
542 #ifndef __REACTOS__
543 case BM_GETSTATE16:
544 #endif
545 case BM_GETSTATE:
546 return get_button_state( hWnd );
547
548 #ifndef __REACTOS__
549 case BM_SETSTATE16:
550 #endif
551 case BM_SETSTATE:
552 state = get_button_state( hWnd );
553 if (wParam)
554 {
555 if (state & BUTTON_HIGHLIGHTED) break;
556 set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
557 }
558 else
559 {
560 if (!(state & BUTTON_HIGHLIGHTED)) break;
561 set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
562 }
563 paint_button( hWnd, btn_type, ODA_SELECT );
564 break;
565
566 case WM_UPDATEUISTATE:
567 if (unicode)
568 DefWindowProcW(hWnd, uMsg, wParam, lParam);
569 else
570 DefWindowProcA(hWnd, uMsg, wParam, lParam);
571
572 if (button_update_uistate(hWnd, unicode))
573 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
574 break;
575
576 case WM_NCHITTEST:
577 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
578 /* fall through */
579 default:
580 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
581 DefWindowProcA(hWnd, uMsg, wParam, lParam);
582 }
583 return 0;
584 }
585
586
587 /***********************************************************************
588 * ButtonWndProcW
589 * The button window procedure. This is just a wrapper which locks
590 * the passed HWND and calls the real window procedure (with a WND*
591 * pointer pointing to the locked windowstructure).
592 */
593 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
594 {
595 if (!IsWindow( hWnd )) return 0;
596 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, TRUE );
597 }
598
599
600 /***********************************************************************
601 * ButtonWndProcA
602 */
603 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
604 {
605 if (!IsWindow( hWnd )) return 0;
606 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, FALSE );
607 }
608
609
610 /**********************************************************************
611 * Convert button styles to flags used by DrawText.
612 * TODO: handle WS_EX_RIGHT extended style.
613 */
614 static UINT BUTTON_BStoDT(DWORD 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 /* DrawText ignores vertical alignment for multiline text,
639 * but we use these flags to align label manualy.
640 */
641 if (get_button_type(style) != BS_GROUPBOX)
642 {
643 switch (style & BS_VCENTER)
644 {
645 case BS_TOP: /* DT_TOP is 0 */ break;
646 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
647 case BS_VCENTER: /* fall through */
648 default: dtStyle |= DT_VCENTER; break;
649 }
650 }
651 else
652 /* GroupBox's text is always single line and is top aligned. */
653 dtStyle |= DT_SINGLELINE;
654
655 return dtStyle;
656 }
657
658 /**********************************************************************
659 * BUTTON_CalcLabelRect
660 *
661 * Calculates label's rectangle depending on button style.
662 *
663 * Returns flags to be passed to DrawText.
664 * Calculated rectangle doesn't take into account button state
665 * (pushed, etc.). If there is nothing to draw (no text/image) output
666 * rectangle is empty, and return value is (UINT)-1.
667 */
668 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
669 {
670 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
671 WCHAR *text;
672 ICONINFO iconInfo;
673 BITMAP bm;
674 UINT dtStyle = BUTTON_BStoDT(style);
675 RECT r = *rc;
676 INT n;
677
678 /* Calculate label rectangle according to label type */
679 switch (style & (BS_ICON|BS_BITMAP))
680 {
681 case BS_TEXT:
682 if (!(text = get_button_text( hwnd ))) goto empty_rect;
683 if (!text[0])
684 {
685 HeapFree( GetProcessHeap(), 0, text );
686 goto empty_rect;
687 }
688 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
689 HeapFree( GetProcessHeap(), 0, text );
690
691 if (get_ui_state( hwnd ) & UISF_HIDEACCEL)
692 dtStyle |= DT_HIDEPREFIX;
693 break;
694
695 case BS_ICON:
696 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
697 goto empty_rect;
698
699 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
700
701 r.right = r.left + bm.bmWidth;
702 r.bottom = r.top + bm.bmHeight;
703
704 DeleteObject(iconInfo.hbmColor);
705 DeleteObject(iconInfo.hbmMask);
706 break;
707
708 case BS_BITMAP:
709 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
710 goto empty_rect;
711
712 r.right = r.left + bm.bmWidth;
713 r.bottom = r.top + bm.bmHeight;
714 break;
715
716 default:
717 empty_rect:
718 rc->right = r.left;
719 rc->bottom = r.top;
720 return (UINT)(LONG)-1;
721 }
722
723 /* Position label inside bounding rectangle according to
724 * alignment flags. (calculated rect is always left-top aligned).
725 * If label is aligned to any side - shift label in opposite
726 * direction to leave extra space for focus rectangle.
727 */
728 switch (dtStyle & (DT_CENTER|DT_RIGHT))
729 {
730 case DT_LEFT: r.left++; r.right++; break;
731 case DT_CENTER: n = r.right - r.left;
732 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
733 r.right = r.left + n; break;
734 case DT_RIGHT: n = r.right - r.left;
735 r.right = rc->right - 1;
736 r.left = r.right - n;
737 break;
738 }
739
740 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
741 {
742 case DT_TOP: r.top++; r.bottom++; break;
743 case DT_VCENTER: n = r.bottom - r.top;
744 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
745 r.bottom = r.top + n; break;
746 case DT_BOTTOM: n = r.bottom - r.top;
747 r.bottom = rc->bottom - 1;
748 r.top = r.bottom - n;
749 break;
750 }
751
752 *rc = r;
753 return dtStyle;
754 }
755
756
757 /**********************************************************************
758 * BUTTON_DrawTextCallback
759 *
760 * Callback function used by DrawStateW function.
761 */
762 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
763 {
764 RECT rc;
765 rc.left = 0;
766 rc.top = 0;
767 rc.right = cx;
768 rc.bottom = cy;
769
770 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
771 return TRUE;
772 }
773
774
775 /**********************************************************************
776 * BUTTON_DrawLabel
777 *
778 * Common function for drawing button label.
779 */
780 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, RECT *rc)
781 {
782 DRAWSTATEPROC lpOutputProc = NULL;
783 LPARAM lp;
784 WPARAM wp = 0;
785 HBRUSH hbr = 0;
786 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
787 LONG state = get_button_state( hwnd );
788 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
789 WCHAR *text = NULL;
790
791 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
792 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
793 * I don't have Win31 on hand to verify that, so I leave it as is.
794 */
795
796 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
797 {
798 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
799 flags |= DSS_MONO;
800 }
801
802 switch (style & (BS_ICON|BS_BITMAP))
803 {
804 case BS_TEXT:
805 /* DST_COMPLEX -- is 0 */
806 lpOutputProc = BUTTON_DrawTextCallback;
807 if (!(text = get_button_text( hwnd ))) return;
808 lp = (LPARAM)text;
809 wp = (WPARAM)dtFlags;
810
811 if (dtFlags & DT_HIDEPREFIX)
812 flags |= DSS_HIDEPREFIX;
813 break;
814
815 case BS_ICON:
816 flags |= DST_ICON;
817 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
818 break;
819
820 case BS_BITMAP:
821 flags |= DST_BITMAP;
822 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
823 break;
824
825 default:
826 return;
827 }
828
829 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
830 rc->right - rc->left, rc->bottom - rc->top, flags);
831 HeapFree( GetProcessHeap(), 0, text );
832 }
833
834 /**********************************************************************
835 * Push Button Functions
836 */
837 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
838 {
839 RECT rc, focus_rect, r;
840 UINT dtFlags, uState;
841 HPEN hOldPen;
842 HBRUSH hOldBrush;
843 INT oldBkMode;
844 COLORREF oldTxtColor;
845 HFONT hFont;
846 LONG state = get_button_state( hwnd );
847 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
848 BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
849 HWND parent;
850
851 GetClientRect( hwnd, &rc );
852
853 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
854 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
855 parent = GetParent(hwnd);
856 if (!parent) parent = hwnd;
857 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
858 #ifdef __REACTOS__
859 hOldPen = (HPEN)SelectObject(hDC, GetSysColorPen(COLOR_WINDOWFRAME));
860 #else
861 hOldPen = (HPEN)SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
862 #endif
863 hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
864 oldBkMode = SetBkMode(hDC, TRANSPARENT);
865
866 if (get_button_type(style) == BS_DEFPUSHBUTTON)
867 {
868 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
869 InflateRect( &rc, -1, -1 );
870 }
871
872 uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
873
874 if (style & BS_FLAT)
875 uState |= DFCS_MONO;
876 else if (pushedState)
877 {
878 if (get_button_type(style) == BS_DEFPUSHBUTTON )
879 uState |= DFCS_FLAT;
880 else
881 uState |= DFCS_PUSHED;
882 }
883
884 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
885 uState |= DFCS_CHECKED;
886
887 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
888
889 focus_rect = rc;
890
891 /* draw button label */
892 r = rc;
893 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
894
895 if (dtFlags == (UINT)-1L)
896 goto cleanup;
897
898 if (pushedState)
899 OffsetRect(&r, 1, 1);
900
901 IntersectClipRect(hDC, rc.left, rc.top, rc.right, rc.bottom);
902
903 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
904
905 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
906
907 SetTextColor( hDC, oldTxtColor );
908
909 if (state & BUTTON_HASFOCUS)
910 {
911 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
912 {
913 InflateRect( &focus_rect, -1, -1 );
914 IntersectRect(&focus_rect, &focus_rect, &rc);
915 DrawFocusRect( hDC, &focus_rect );
916 }
917 }
918
919 cleanup:
920 SelectObject( hDC, hOldPen );
921 SelectObject( hDC, hOldBrush );
922 SetBkMode(hDC, oldBkMode);
923 }
924
925 /**********************************************************************
926 * Check Box & Radio Button Functions
927 */
928
929 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
930 {
931 RECT rbox, rtext, client;
932 HBRUSH hBrush;
933 int delta;
934 UINT dtFlags;
935 HFONT hFont;
936 LONG state = get_button_state( hwnd );
937 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
938 HWND parent;
939
940 if (style & BS_PUSHLIKE)
941 {
942 PB_Paint( hwnd, hDC, action );
943 return;
944 }
945
946 GetClientRect(hwnd, &client);
947 rbox = rtext = client;
948
949 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
950
951 parent = GetParent(hwnd);
952 if (!parent) parent = hwnd;
953 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
954 (WPARAM)hDC, (LPARAM)hwnd);
955 if (!hBrush) /* did the app forget to call defwindowproc ? */
956 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
957 (WPARAM)hDC, (LPARAM)hwnd );
958
959 if (style & BS_LEFTTEXT)
960 {
961 /* magic +4 is what CTL3D expects */
962
963 rtext.right -= checkBoxWidth + 4;
964 rbox.left = rbox.right - checkBoxWidth;
965 }
966 else
967 {
968 rtext.left += checkBoxWidth + 4;
969 rbox.right = checkBoxWidth;
970 }
971
972 /* Since WM_ERASEBKGND does nothing, first prepare background */
973 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
974 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
975
976 /* Draw label */
977 client = rtext;
978 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
979
980 /* Only adjust rbox when rtext is valid */
981 if (dtFlags != (UINT)-1L)
982 {
983 rbox.top = rtext.top;
984 rbox.bottom = rtext.bottom;
985 }
986
987 /* Draw the check-box bitmap */
988 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
989 {
990 UINT flags;
991
992 if ((get_button_type(style) == BS_RADIOBUTTON) ||
993 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
994 else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
995 else flags = DFCS_BUTTONCHECK;
996
997 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
998 if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
999
1000 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
1001
1002 /* rbox must have the correct height */
1003 delta = rbox.bottom - rbox.top - checkBoxHeight;
1004
1005 if (style & BS_TOP) {
1006 if (delta > 0) {
1007 rbox.bottom = rbox.top + checkBoxHeight;
1008 } else {
1009 rbox.top -= -delta/2 + 1;
1010 rbox.bottom = rbox.top + checkBoxHeight;
1011 }
1012 } else if (style & BS_BOTTOM) {
1013 if (delta > 0) {
1014 rbox.top = rbox.bottom - checkBoxHeight;
1015 } else {
1016 rbox.bottom += -delta/2 + 1;
1017 rbox.top = rbox.bottom - checkBoxHeight;
1018 }
1019 } else { /* Default */
1020 if (delta > 0) {
1021 int ofs = (delta / 2);
1022 rbox.bottom -= ofs + 1;
1023 rbox.top = rbox.bottom - checkBoxHeight;
1024 } else if (delta < 0) {
1025 int ofs = (-delta / 2);
1026 rbox.top -= ofs + 1;
1027 rbox.bottom = rbox.top + checkBoxHeight;
1028 }
1029 }
1030
1031 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
1032 }
1033
1034 if (dtFlags == (UINT)-1L) /* Noting to draw */
1035 return;
1036
1037 IntersectClipRect(hDC, client.left, client.top, client.right, client.bottom);
1038
1039 if (action == ODA_DRAWENTIRE)
1040 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
1041
1042 /* ... and focus */
1043 if ((action == ODA_FOCUS) ||
1044 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1045 {
1046 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
1047 {
1048 rtext.left--;
1049 rtext.right++;
1050 IntersectRect(&rtext, &rtext, &client);
1051 DrawFocusRect( hDC, &rtext );
1052 }
1053 }
1054 }
1055
1056
1057 /**********************************************************************
1058 * BUTTON_CheckAutoRadioButton
1059 *
1060 * hwnd is checked, uncheck every other auto radio button in group
1061 */
1062 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
1063 {
1064 HWND parent, sibling, start;
1065
1066 parent = GetParent(hwnd);
1067 /* make sure that starting control is not disabled or invisible */
1068 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
1069 do
1070 {
1071 if (!sibling) break;
1072 if ((hwnd != sibling) &&
1073 ((GetWindowLongW( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
1074 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
1075 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1076 } while (sibling != start);
1077 }
1078
1079
1080 /**********************************************************************
1081 * Group Box Functions
1082 */
1083
1084 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1085 {
1086 RECT rc, rcFrame;
1087 HBRUSH hbr;
1088 HFONT hFont;
1089 UINT dtFlags;
1090 TEXTMETRICW tm;
1091 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1092 HWND parent;
1093
1094 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1095 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1096 parent = GetParent(hwnd);
1097 if (!parent) parent = hwnd;
1098 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1099 if (!hbr) /* did the app forget to call defwindowproc ? */
1100 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1101 (WPARAM)hDC, (LPARAM)hwnd);
1102
1103 GetClientRect( hwnd, &rc);
1104 rcFrame = rc;
1105
1106 GetTextMetricsW (hDC, &tm);
1107 rcFrame.top += (tm.tmHeight / 2) - 1;
1108 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1109
1110 InflateRect(&rc, -7, 1);
1111 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1112
1113 if (dtFlags == (UINT)-1L)
1114 return;
1115
1116 /* Because buttons have CS_PARENTDC class style, there is a chance
1117 * that label will be drawn out of client rect.
1118 * But Windows doesn't clip label's rect, so do I.
1119 */
1120
1121 /* There is 1-pixel marging at the left, right, and bottom */
1122 rc.left--; rc.right++; rc.bottom++;
1123 FillRect(hDC, &rc, hbr);
1124 rc.left++; rc.right--; rc.bottom--;
1125
1126 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1127 }
1128
1129
1130 /**********************************************************************
1131 * User Button Functions
1132 */
1133
1134 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1135 {
1136 RECT rc;
1137 HBRUSH hBrush;
1138 HFONT hFont;
1139 LONG state = get_button_state( hwnd );
1140 HWND parent;
1141
1142 if (action == ODA_SELECT) return;
1143
1144 GetClientRect( hwnd, &rc);
1145
1146 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1147
1148 parent = GetParent(hwnd);
1149 if (!parent) parent = hwnd;
1150 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1151 if (!hBrush) /* did the app forget to call defwindowproc ? */
1152 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1153 (WPARAM)hDC, (LPARAM)hwnd);
1154
1155 FillRect( hDC, &rc, hBrush );
1156 if ((action == ODA_FOCUS) ||
1157 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1158 {
1159 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
1160 DrawFocusRect( hDC, &rc );
1161 }
1162 }
1163
1164
1165 /**********************************************************************
1166 * Ownerdrawn Button Functions
1167 */
1168
1169 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1170 {
1171 LONG state = get_button_state( hwnd );
1172 DRAWITEMSTRUCT dis;
1173 HRGN clipRegion;
1174 RECT clipRect;
1175 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1176 HWND parent;
1177 HFONT hFont, hPrevFont = 0;
1178
1179 dis.CtlType = ODT_BUTTON;
1180 dis.CtlID = id;
1181 dis.itemID = 0;
1182 dis.itemAction = action;
1183 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1184 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1185 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1186 dis.hwndItem = hwnd;
1187 dis.hDC = hDC;
1188 dis.itemData = 0;
1189 GetClientRect( hwnd, &dis.rcItem );
1190
1191 clipRegion = CreateRectRgnIndirect(&dis.rcItem);
1192 if (GetClipRgn(hDC, clipRegion) != 1)
1193 {
1194 DeleteObject(clipRegion);
1195 clipRegion=NULL;
1196 }
1197 clipRect = dis.rcItem;
1198 DPtoLP(hDC, (LPPOINT) &clipRect, 2);
1199 IntersectClipRect(hDC, clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
1200
1201 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1202 parent = GetParent(hwnd);
1203 if (!parent) parent = hwnd;
1204 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1205 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1206 if (hPrevFont) SelectObject(hDC, hPrevFont);
1207 SelectClipRgn(hDC, clipRegion);
1208 }