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