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