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