[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 if (btnPaintFunc[btn_type])
360 {
361 PAINTSTRUCT ps;
362 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
363 int nOldMode = SetBkMode( hdc, OPAQUE );
364 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
365 SetBkMode(hdc, nOldMode); /* reset painting mode */
366 if( !wParam ) EndPaint( hWnd, &ps );
367 }
368 break;
369
370 case WM_KEYDOWN:
371 if (wParam == VK_SPACE)
372 {
373 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
374 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
375 SetCapture( hWnd );
376 }
377 break;
378
379 case WM_LBUTTONDBLCLK:
380 if(style & BS_NOTIFY ||
381 btn_type == BS_RADIOBUTTON ||
382 btn_type == BS_USERBUTTON ||
383 btn_type == BS_OWNERDRAW)
384 {
385 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
386 break;
387 }
388 /* fall through */
389 case WM_LBUTTONDOWN:
390 SetCapture( hWnd );
391 SetFocus( hWnd );
392 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
393 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
394 break;
395
396 case WM_KEYUP:
397 if (wParam != VK_SPACE)
398 break;
399 /* fall through */
400 case WM_LBUTTONUP:
401 {
402 BOOL TellParent = FALSE; //// ReactOS see note below.
403 state = get_button_state( hWnd );
404 if (!(state & BUTTON_BTNPRESSED)) break;
405 state &= BUTTON_NSTATES;
406 set_button_state( hWnd, state );
407 if (!(state & BST_PUSHED))
408 {
409 ReleaseCapture();
410 break;
411 }
412 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
413 GetClientRect( hWnd, &rect );
414 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
415 {
416 state = get_button_state( hWnd );
417 switch(btn_type)
418 {
419 case BS_AUTOCHECKBOX:
420 SendMessageW( hWnd, BM_SETCHECK, !(state & BST_CHECKED), 0 );
421 break;
422 case BS_AUTORADIOBUTTON:
423 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
424 break;
425 case BS_AUTO3STATE:
426 SendMessageW( hWnd, BM_SETCHECK,
427 (state & BST_INDETERMINATE) ? 0 : ((state & 3) + 1), 0 );
428 break;
429 }
430 TellParent = TRUE; // <---- Fix CORE-10194, Notify parent after capture is released.
431 }
432 ReleaseCapture();
433 if (TellParent) BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
434 }
435 break;
436
437 case WM_CAPTURECHANGED:
438 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
439 state = get_button_state( hWnd );
440 if (state & BUTTON_BTNPRESSED)
441 {
442 state &= BUTTON_NSTATES;
443 set_button_state( hWnd, state );
444 if (state & BST_PUSHED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
445 }
446 break;
447
448 case WM_MOUSEMOVE:
449 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
450 {
451 GetClientRect( hWnd, &rect );
452 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
453 }
454 break;
455
456 case WM_SETTEXT:
457 {
458 /* Clear an old text here as Windows does */
459 //
460 // wine Bug: http://bugs.winehq.org/show_bug.cgi?id=25790
461 // Patch: http://source.winehq.org/patches/data/70889
462 // By: Alexander LAW, Replicate Windows behavior of WM_SETTEXT handler regarding WM_CTLCOLOR*
463 //
464 #ifdef __REACTOS__
465 if (style & WS_VISIBLE)
466 {
467 #endif
468 HDC hdc = GetDC(hWnd);
469 HBRUSH hbrush;
470 RECT client, rc;
471 HWND parent = GetParent(hWnd);
472 #ifdef __REACTOS__
473 UINT ctlMessage = (btn_type == BS_PUSHBUTTON ||
474 btn_type == BS_DEFPUSHBUTTON ||
475 btn_type == BS_PUSHLIKE ||
476 btn_type == BS_USERBUTTON ||
477 btn_type == BS_OWNERDRAW) ?
478 WM_CTLCOLORBTN : WM_CTLCOLORSTATIC;
479 #endif
480
481 if (!parent) parent = hWnd;
482 #ifdef __REACTOS__
483 hbrush = GetControlColor(parent, hWnd, hdc, ctlMessage);
484 #else
485 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
486 (WPARAM)hdc, (LPARAM)hWnd);
487 if (!hbrush) /* did the app forget to call DefWindowProc ? */
488 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
489 (WPARAM)hdc, (LPARAM)hWnd);
490 #endif
491
492 GetClientRect(hWnd, &client);
493 rc = client;
494 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
495 /* Clip by client rect bounds */
496 if (rc.right > client.right) rc.right = client.right;
497 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
498 FillRect(hdc, &rc, hbrush);
499 ReleaseDC(hWnd, hdc);
500 #ifdef __REACTOS__
501 }
502 #endif
503
504 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
505 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
506 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
507 InvalidateRect( hWnd, NULL, TRUE );
508 else
509 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
510 return 1; /* success. FIXME: check text length */
511 }
512
513 case WM_SETFONT:
514 set_button_font( hWnd, (HFONT)wParam );
515 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
516 break;
517
518 case WM_GETFONT:
519 return (LRESULT)get_button_font( hWnd );
520
521 case WM_SETFOCUS:
522 TRACE("WM_SETFOCUS %p\n",hWnd);
523 set_button_state( hWnd, get_button_state(hWnd) | BST_FOCUS );
524 paint_button( hWnd, btn_type, ODA_FOCUS );
525 if (style & BS_NOTIFY)
526 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
527 break;
528
529 case WM_KILLFOCUS:
530 TRACE("WM_KILLFOCUS %p\n",hWnd);
531 state = get_button_state( hWnd );
532 set_button_state( hWnd, state & ~BST_FOCUS );
533 paint_button( hWnd, btn_type, ODA_FOCUS );
534
535 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
536 ReleaseCapture();
537 if (style & BS_NOTIFY)
538 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
539
540 InvalidateRect( hWnd, NULL, FALSE );
541 break;
542
543 case WM_SYSCOLORCHANGE:
544 InvalidateRect( hWnd, NULL, FALSE );
545 break;
546
547 case BM_SETSTYLE:
548 if ((wParam & BS_TYPEMASK) >= MAX_BTN_TYPE) break;
549 btn_type = wParam & BS_TYPEMASK;
550 style = (style & ~BS_TYPEMASK) | btn_type;
551 #ifdef __REACTOS__
552 NtUserAlterWindowStyle(hWnd, GWL_STYLE, style);
553 #else
554 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
555 #endif
556
557 /* Only redraw if lParam flag is set.*/
558 if (lParam)
559 InvalidateRect( hWnd, NULL, TRUE );
560
561 break;
562
563 case BM_CLICK:
564 #ifdef __REACTOS__
565 state = get_button_state(hWnd);
566 if (state & BUTTON_BMCLICK)
567 break;
568 set_button_state(hWnd, state | BUTTON_BMCLICK); // Tracked in STATE_GWL_OFFSET.
569 #endif
570 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
571 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
572 #ifdef __REACTOS__
573 state = get_button_state(hWnd);
574 if (!(state & BUTTON_BMCLICK)) break;
575 state &= ~BUTTON_BMCLICK;
576 set_button_state(hWnd, state);
577 #endif
578 break;
579
580 case BM_SETIMAGE:
581 /* Check that image format matches button style */
582 switch (style & (BS_BITMAP|BS_ICON))
583 {
584 case BS_BITMAP:
585 if (wParam != IMAGE_BITMAP) return 0;
586 break;
587 case BS_ICON:
588 if (wParam != IMAGE_ICON) return 0;
589 break;
590 default:
591 return 0;
592 }
593 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
594 InvalidateRect( hWnd, NULL, FALSE );
595 return (LRESULT)oldHbitmap;
596
597 case BM_GETIMAGE:
598 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
599
600 case BM_GETCHECK:
601 return get_button_state( hWnd ) & 3;
602
603 case BM_SETCHECK:
604 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
605 state = get_button_state( hWnd );
606 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
607 {
608 #ifdef __REACTOS__
609 if (wParam) style |= WS_TABSTOP;
610 else style &= ~WS_TABSTOP;
611 NtUserAlterWindowStyle(hWnd, GWL_STYLE, style);
612 #else
613 if (wParam) WIN_SetStyle( hWnd, WS_TABSTOP, 0 );
614 else WIN_SetStyle( hWnd, 0, WS_TABSTOP );
615 #endif
616 }
617 if ((state & 3) != wParam)
618 {
619 set_button_state( hWnd, (state & ~3) | wParam );
620 paint_button( hWnd, btn_type, ODA_SELECT );
621 }
622 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BST_CHECKED) && (style & WS_CHILD))
623 BUTTON_CheckAutoRadioButton( hWnd );
624 break;
625
626 case BM_GETSTATE:
627 return get_button_state( hWnd );
628
629 case BM_SETSTATE:
630 state = get_button_state( hWnd );
631 if (wParam)
632 set_button_state( hWnd, state | BST_PUSHED );
633 else
634 set_button_state( hWnd, state & ~BST_PUSHED );
635
636 paint_button( hWnd, btn_type, ODA_SELECT );
637 break;
638
639 #ifdef __REACTOS__
640 case WM_UPDATEUISTATE:
641 if (unicode)
642 DefWindowProcW(hWnd, uMsg, wParam, lParam);
643 else
644 DefWindowProcA(hWnd, uMsg, wParam, lParam);
645
646 if (button_update_uistate(hWnd, unicode))
647 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
648 break;
649 #endif
650
651 case WM_NCHITTEST:
652 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
653 /* fall through */
654 default:
655 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
656 DefWindowProcA(hWnd, uMsg, wParam, lParam);
657 }
658 return 0;
659 }
660
661 #ifdef __REACTOS__
662
663 /***********************************************************************
664 * ButtonWndProcW
665 * The button window procedure. This is just a wrapper which locks
666 * the passed HWND and calls the real window procedure (with a WND*
667 * pointer pointing to the locked windowstructure).
668 */
669 LRESULT WINAPI ButtonWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
670 {
671 if (!IsWindow(hWnd)) return 0;
672 return ButtonWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
673 }
674
675 /***********************************************************************
676 * ButtonWndProcA
677 */
678 LRESULT WINAPI ButtonWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
679 {
680 if (!IsWindow(hWnd)) return 0;
681 return ButtonWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
682 }
683
684 #endif /* __REACTOS__ */
685
686 /**********************************************************************
687 * Convert button styles to flags used by DrawText.
688 */
689 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
690 {
691 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
692
693 /* "Convert" pushlike buttons to pushbuttons */
694 if (style & BS_PUSHLIKE)
695 style &= ~BS_TYPEMASK;
696
697 if (!(style & BS_MULTILINE))
698 dtStyle |= DT_SINGLELINE;
699 else
700 dtStyle |= DT_WORDBREAK;
701
702 switch (style & BS_CENTER)
703 {
704 case BS_LEFT: /* DT_LEFT is 0 */ break;
705 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
706 case BS_CENTER: dtStyle |= DT_CENTER; break;
707 default:
708 /* Pushbutton's text is centered by default */
709 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
710 /* all other flavours have left aligned text */
711 }
712
713 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
714
715 /* DrawText ignores vertical alignment for multiline text,
716 * but we use these flags to align label manually.
717 */
718 if (get_button_type(style) != BS_GROUPBOX)
719 {
720 switch (style & BS_VCENTER)
721 {
722 case BS_TOP: /* DT_TOP is 0 */ break;
723 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
724 case BS_VCENTER: /* fall through */
725 default: dtStyle |= DT_VCENTER; break;
726 }
727 }
728 else
729 /* GroupBox's text is always single line and is top aligned. */
730 dtStyle |= DT_SINGLELINE;
731
732 return dtStyle;
733 }
734
735 /**********************************************************************
736 * BUTTON_CalcLabelRect
737 *
738 * Calculates label's rectangle depending on button style.
739 *
740 * Returns flags to be passed to DrawText.
741 * Calculated rectangle doesn't take into account button state
742 * (pushed, etc.). If there is nothing to draw (no text/image) output
743 * rectangle is empty, and return value is (UINT)-1.
744 */
745 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
746 {
747 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
748 LONG ex_style = GetWindowLongPtrW( hwnd, GWL_EXSTYLE );
749 WCHAR *text;
750 ICONINFO iconInfo;
751 BITMAP bm;
752 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
753 RECT r = *rc;
754 INT n;
755
756 /* Calculate label rectangle according to label type */
757 switch (style & (BS_ICON|BS_BITMAP))
758 {
759 case BS_TEXT:
760 if (!(text = get_button_text( hwnd ))) goto empty_rect;
761 if (!text[0])
762 {
763 HeapFree( GetProcessHeap(), 0, text );
764 goto empty_rect;
765 }
766 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
767 HeapFree( GetProcessHeap(), 0, text );
768 #ifdef __REACTOS__
769 if (get_ui_state(hwnd) & UISF_HIDEACCEL)
770 dtStyle |= DT_HIDEPREFIX;
771 #endif
772 break;
773
774 case BS_ICON:
775 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
776 goto empty_rect;
777
778 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
779
780 r.right = r.left + bm.bmWidth;
781 r.bottom = r.top + bm.bmHeight;
782
783 DeleteObject(iconInfo.hbmColor);
784 DeleteObject(iconInfo.hbmMask);
785 break;
786
787 case BS_BITMAP:
788 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
789 goto empty_rect;
790
791 r.right = r.left + bm.bmWidth;
792 r.bottom = r.top + bm.bmHeight;
793 break;
794
795 default:
796 empty_rect:
797 rc->right = r.left;
798 rc->bottom = r.top;
799 return (UINT)-1;
800 }
801
802 /* Position label inside bounding rectangle according to
803 * alignment flags. (calculated rect is always left-top aligned).
804 * If label is aligned to any side - shift label in opposite
805 * direction to leave extra space for focus rectangle.
806 */
807 switch (dtStyle & (DT_CENTER|DT_RIGHT))
808 {
809 case DT_LEFT: r.left++; r.right++; break;
810 case DT_CENTER: n = r.right - r.left;
811 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
812 r.right = r.left + n; break;
813 case DT_RIGHT: n = r.right - r.left;
814 r.right = rc->right - 1;
815 r.left = r.right - n;
816 break;
817 }
818
819 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
820 {
821 case DT_TOP: r.top++; r.bottom++; break;
822 case DT_VCENTER: n = r.bottom - r.top;
823 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
824 r.bottom = r.top + n; break;
825 case DT_BOTTOM: n = r.bottom - r.top;
826 r.bottom = rc->bottom - 1;
827 r.top = r.bottom - n;
828 break;
829 }
830
831 *rc = r;
832 return dtStyle;
833 }
834
835
836 /**********************************************************************
837 * BUTTON_DrawTextCallback
838 *
839 * Callback function used by DrawStateW function.
840 */
841 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
842 {
843 RECT rc;
844 rc.left = 0;
845 rc.top = 0;
846 rc.right = cx;
847 rc.bottom = cy;
848
849 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
850 return TRUE;
851 }
852
853
854 /**********************************************************************
855 * BUTTON_DrawLabel
856 *
857 * Common function for drawing button label.
858 */
859 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
860 {
861 DRAWSTATEPROC lpOutputProc = NULL;
862 LPARAM lp;
863 WPARAM wp = 0;
864 HBRUSH hbr = 0;
865 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
866 LONG state = get_button_state( hwnd );
867 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
868 WCHAR *text = NULL;
869
870 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
871 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
872 * I don't have Win31 on hand to verify that, so I leave it as is.
873 */
874
875 if ((style & BS_PUSHLIKE) && (state & BST_INDETERMINATE))
876 {
877 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
878 flags |= DSS_MONO;
879 }
880
881 switch (style & (BS_ICON|BS_BITMAP))
882 {
883 case BS_TEXT:
884 /* DST_COMPLEX -- is 0 */
885 lpOutputProc = BUTTON_DrawTextCallback;
886 if (!(text = get_button_text( hwnd ))) return;
887 lp = (LPARAM)text;
888 wp = (WPARAM)dtFlags;
889
890 #ifdef __REACTOS__
891 if (dtFlags & DT_HIDEPREFIX)
892 flags |= DSS_HIDEPREFIX;
893 #endif
894 break;
895
896 case BS_ICON:
897 flags |= DST_ICON;
898 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
899 break;
900
901 case BS_BITMAP:
902 flags |= DST_BITMAP;
903 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
904 break;
905
906 default:
907 return;
908 }
909
910 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
911 rc->right - rc->left, rc->bottom - rc->top, flags);
912 HeapFree( GetProcessHeap(), 0, text );
913 }
914
915 /**********************************************************************
916 * Push Button Functions
917 */
918 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
919 {
920 RECT rc, r;
921 UINT dtFlags, uState;
922 HPEN hOldPen;
923 HBRUSH hOldBrush;
924 INT oldBkMode;
925 COLORREF oldTxtColor;
926 HFONT hFont;
927 LONG state = get_button_state( hwnd );
928 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
929 BOOL pushedState = (state & BST_PUSHED);
930 HWND parent;
931 HRGN hrgn;
932
933 GetClientRect( hwnd, &rc );
934
935 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
936 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
937 parent = GetParent(hwnd);
938 if (!parent) parent = hwnd;
939 #ifdef __REACTOS__
940 GetControlColor( parent, hwnd, hDC, WM_CTLCOLORBTN);
941 #else
942 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
943 #endif
944
945 hrgn = set_control_clipping( hDC, &rc );
946 #ifdef __REACTOS__
947 hOldPen = SelectObject(hDC, GetStockObject(DC_PEN));
948 SetDCPenColor(hDC, GetSysColor(COLOR_WINDOWFRAME));
949 #else
950 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
951 #endif
952 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
953 oldBkMode = SetBkMode(hDC, TRANSPARENT);
954
955 if (get_button_type(style) == BS_DEFPUSHBUTTON)
956 {
957 if (action != ODA_FOCUS)
958 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
959 InflateRect( &rc, -1, -1 );
960 }
961
962 /* completely skip the drawing if only focus has changed */
963 if (action == ODA_FOCUS) goto draw_focus;
964
965 uState = DFCS_BUTTONPUSH;
966
967 if (style & BS_FLAT)
968 uState |= DFCS_MONO;
969 else if (pushedState)
970 {
971 if (get_button_type(style) == BS_DEFPUSHBUTTON )
972 uState |= DFCS_FLAT;
973 else
974 uState |= DFCS_PUSHED;
975 }
976
977 if (state & (BST_CHECKED | BST_INDETERMINATE))
978 uState |= DFCS_CHECKED;
979
980 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
981
982 /* draw button label */
983 r = rc;
984 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
985
986 if (dtFlags == (UINT)-1L)
987 goto cleanup;
988
989 if (pushedState)
990 OffsetRect(&r, 1, 1);
991
992 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
993
994 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
995
996 SetTextColor( hDC, oldTxtColor );
997
998 draw_focus:
999 if (action == ODA_FOCUS || (state & BST_FOCUS))
1000 {
1001 #ifdef __REACTOS__
1002 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
1003 {
1004 #endif
1005 InflateRect( &rc, -2, -2 );
1006 DrawFocusRect( hDC, &rc );
1007 #ifdef __REACTOS__
1008 }
1009 #endif
1010 }
1011
1012 cleanup:
1013 SelectObject( hDC, hOldPen );
1014 SelectObject( hDC, hOldBrush );
1015 SetBkMode(hDC, oldBkMode);
1016 SelectClipRgn( hDC, hrgn );
1017 if (hrgn) DeleteObject( hrgn );
1018 }
1019
1020 /**********************************************************************
1021 * Check Box & Radio Button Functions
1022 */
1023
1024 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
1025 {
1026 RECT rbox, rtext, client;
1027 HBRUSH hBrush;
1028 int delta;
1029 UINT dtFlags;
1030 HFONT hFont;
1031 LONG state = get_button_state( hwnd );
1032 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
1033 HWND parent;
1034 HRGN hrgn;
1035
1036 if (style & BS_PUSHLIKE)
1037 {
1038 PB_Paint( hwnd, hDC, action );
1039 return;
1040 }
1041
1042 GetClientRect(hwnd, &client);
1043 rbox = rtext = client;
1044
1045 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1046
1047 parent = GetParent(hwnd);
1048 if (!parent) parent = hwnd;
1049 #ifdef __REACTOS__
1050 hBrush = GetControlColor(parent, hwnd, hDC, WM_CTLCOLORSTATIC);
1051 #else
1052 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
1053 (WPARAM)hDC, (LPARAM)hwnd);
1054 if (!hBrush) /* did the app forget to call defwindowproc ? */
1055 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1056 (WPARAM)hDC, (LPARAM)hwnd );
1057 #endif
1058 hrgn = set_control_clipping( hDC, &client );
1059
1060 if (style & BS_LEFTTEXT)
1061 {
1062 /* magic +4 is what CTL3D expects */
1063
1064 rtext.right -= checkBoxWidth + 4;
1065 rbox.left = rbox.right - checkBoxWidth;
1066 }
1067 else
1068 {
1069 rtext.left += checkBoxWidth + 4;
1070 rbox.right = checkBoxWidth;
1071 }
1072
1073 /* Since WM_ERASEBKGND does nothing, first prepare background */
1074 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
1075 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
1076
1077 /* Draw label */
1078 client = rtext;
1079 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
1080
1081 /* Only adjust rbox when rtext is valid */
1082 if (dtFlags != (UINT)-1L)
1083 {
1084 rbox.top = rtext.top;
1085 rbox.bottom = rtext.bottom;
1086 }
1087
1088 /* Draw the check-box bitmap */
1089 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
1090 {
1091 UINT flags;
1092
1093 if ((get_button_type(style) == BS_RADIOBUTTON) ||
1094 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
1095 else if (state & BST_INDETERMINATE) flags = DFCS_BUTTON3STATE;
1096 else flags = DFCS_BUTTONCHECK;
1097
1098 if (state & (BST_CHECKED | BST_INDETERMINATE)) flags |= DFCS_CHECKED;
1099 if (state & BST_PUSHED) flags |= DFCS_PUSHED;
1100
1101 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
1102
1103 /* rbox must have the correct height */
1104 delta = rbox.bottom - rbox.top - checkBoxHeight;
1105
1106 if (style & BS_TOP) {
1107 if (delta > 0) {
1108 rbox.bottom = rbox.top + checkBoxHeight;
1109 } else {
1110 rbox.top -= -delta/2 + 1;
1111 rbox.bottom = rbox.top + checkBoxHeight;
1112 }
1113 } else if (style & BS_BOTTOM) {
1114 if (delta > 0) {
1115 rbox.top = rbox.bottom - checkBoxHeight;
1116 } else {
1117 rbox.bottom += -delta/2 + 1;
1118 rbox.top = rbox.bottom - checkBoxHeight;
1119 }
1120 } else { /* Default */
1121 if (delta > 0) {
1122 int ofs = (delta / 2);
1123 rbox.bottom -= ofs + 1;
1124 rbox.top = rbox.bottom - checkBoxHeight;
1125 } else if (delta < 0) {
1126 int ofs = (-delta / 2);
1127 rbox.top -= ofs + 1;
1128 rbox.bottom = rbox.top + checkBoxHeight;
1129 }
1130 }
1131
1132 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
1133 }
1134
1135 if (dtFlags == (UINT)-1L) /* Noting to draw */
1136 return;
1137
1138 if (action == ODA_DRAWENTIRE)
1139 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
1140
1141 /* ... and focus */
1142 if (action == ODA_FOCUS || (state & BST_FOCUS))
1143 {
1144 #ifdef __REACTOS__
1145 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
1146 {
1147 #endif
1148 rtext.left--;
1149 rtext.right++;
1150 IntersectRect(&rtext, &rtext, &client);
1151 DrawFocusRect( hDC, &rtext );
1152 #ifdef __REACTOS__
1153 }
1154 #endif
1155 }
1156 SelectClipRgn( hDC, hrgn );
1157 if (hrgn) DeleteObject( hrgn );
1158 }
1159
1160
1161 /**********************************************************************
1162 * BUTTON_CheckAutoRadioButton
1163 *
1164 * hwnd is checked, uncheck every other auto radio button in group
1165 */
1166 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
1167 {
1168 HWND parent, sibling, start;
1169
1170 parent = GetParent(hwnd);
1171 /* make sure that starting control is not disabled or invisible */
1172 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
1173 do
1174 {
1175 if (!sibling) break;
1176 if ((hwnd != sibling) &&
1177 ((GetWindowLongPtrW( sibling, GWL_STYLE) & BS_TYPEMASK) == BS_AUTORADIOBUTTON))
1178 SendMessageW( sibling, BM_SETCHECK, BST_UNCHECKED, 0 );
1179 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1180 } while (sibling != start);
1181 }
1182
1183
1184 /**********************************************************************
1185 * Group Box Functions
1186 */
1187
1188 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1189 {
1190 RECT rc, rcFrame;
1191 HBRUSH hbr;
1192 HFONT hFont;
1193 UINT dtFlags;
1194 TEXTMETRICW tm;
1195 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
1196 HWND parent;
1197 HRGN hrgn;
1198
1199 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1200 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1201 parent = GetParent(hwnd);
1202 if (!parent) parent = hwnd;
1203 #ifdef __REACTOS__
1204 hbr = GetControlColor(parent, hwnd, hDC, WM_CTLCOLORSTATIC);
1205 #else
1206 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1207 if (!hbr) /* did the app forget to call defwindowproc ? */
1208 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1209 (WPARAM)hDC, (LPARAM)hwnd);
1210 #endif
1211 GetClientRect( hwnd, &rc);
1212 rcFrame = rc;
1213 hrgn = set_control_clipping( hDC, &rc );
1214
1215 GetTextMetricsW (hDC, &tm);
1216 rcFrame.top += (tm.tmHeight / 2) - 1;
1217 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1218
1219 InflateRect(&rc, -7, 1);
1220 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1221
1222 if (dtFlags != (UINT)-1L)
1223 {
1224 /* Because buttons have CS_PARENTDC class style, there is a chance
1225 * that label will be drawn out of client rect.
1226 * But Windows doesn't clip label's rect, so do I.
1227 */
1228
1229 /* There is 1-pixel margin at the left, right, and bottom */
1230 rc.left--; rc.right++; rc.bottom++;
1231 FillRect(hDC, &rc, hbr);
1232 rc.left++; rc.right--; rc.bottom--;
1233
1234 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1235 }
1236 SelectClipRgn( hDC, hrgn );
1237 if (hrgn) DeleteObject( hrgn );
1238 }
1239
1240
1241 /**********************************************************************
1242 * User Button Functions
1243 */
1244
1245 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1246 {
1247 RECT rc;
1248 HBRUSH hBrush;
1249 HFONT hFont;
1250 LONG state = get_button_state( hwnd );
1251 HWND parent;
1252
1253 GetClientRect( hwnd, &rc);
1254
1255 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1256
1257 parent = GetParent(hwnd);
1258 if (!parent) parent = hwnd;
1259 #ifdef __REACTOS__
1260 hBrush = GetControlColor( parent, hwnd, hDC, WM_CTLCOLORBTN);
1261 #else
1262 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1263 if (!hBrush) /* did the app forget to call defwindowproc ? */
1264 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1265 (WPARAM)hDC, (LPARAM)hwnd);
1266 #endif
1267
1268 FillRect( hDC, &rc, hBrush );
1269 if (action == ODA_FOCUS || (state & BST_FOCUS))
1270 #ifdef __REACTOS__
1271 {
1272 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
1273 #endif
1274 DrawFocusRect( hDC, &rc );
1275 #ifdef __REACTOS__
1276 }
1277 #endif
1278
1279 switch (action)
1280 {
1281 case ODA_FOCUS:
1282 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_FOCUS) ? BN_SETFOCUS : BN_KILLFOCUS );
1283 break;
1284
1285 case ODA_SELECT:
1286 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_PUSHED) ? BN_HILITE : BN_UNHILITE );
1287 break;
1288
1289 default:
1290 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1291 break;
1292 }
1293 }
1294
1295
1296 /**********************************************************************
1297 * Ownerdrawn Button Functions
1298 */
1299
1300 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1301 {
1302 LONG state = get_button_state( hwnd );
1303 DRAWITEMSTRUCT dis;
1304 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1305 HWND parent;
1306 HFONT hFont, hPrevFont = 0;
1307 HRGN hrgn;
1308
1309 dis.CtlType = ODT_BUTTON;
1310 dis.CtlID = id;
1311 dis.itemID = 0;
1312 dis.itemAction = action;
1313 dis.itemState = ((state & BST_FOCUS) ? ODS_FOCUS : 0) |
1314 ((state & BST_PUSHED) ? ODS_SELECTED : 0) |
1315 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1316 dis.hwndItem = hwnd;
1317 dis.hDC = hDC;
1318 dis.itemData = 0;
1319 GetClientRect( hwnd, &dis.rcItem );
1320
1321 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1322 parent = GetParent(hwnd);
1323 if (!parent) parent = hwnd;
1324 #ifdef __REACTOS__
1325 GetControlColor( parent, hwnd, hDC, WM_CTLCOLORBTN);
1326 #else
1327 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1328 #endif
1329
1330 hrgn = set_control_clipping( hDC, &dis.rcItem );
1331
1332 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1333 if (hPrevFont) SelectObject(hDC, hPrevFont);
1334 SelectClipRgn( hDC, hrgn );
1335 if (hrgn) DeleteObject( hrgn );
1336 }