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