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