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