]> git.saurik.com Git - wxWidgets.git/blame - src/msw/button.cpp
properties as collection
[wxWidgets.git] / src / msw / button.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
cd0b1709 2// Name: msw/button.cpp
2bda0e17
KB
3// Purpose: wxButton
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
edccf428
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
cd0b1709 19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
edccf428 21 #pragma implementation "button.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
edccf428 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_BUTTON
32
2bda0e17 33#ifndef WX_PRECOMP
bac409a0 34 #include "wx/app.h"
edccf428
VZ
35 #include "wx/button.h"
36 #include "wx/brush.h"
4e938f5b 37 #include "wx/panel.h"
8a4df159 38 #include "wx/bmpbuttn.h"
fb39c7ec
RR
39 #include "wx/settings.h"
40 #include "wx/dcscreen.h"
2bda0e17
KB
41#endif
42
43#include "wx/msw/private.h"
44
edccf428
VZ
45// ----------------------------------------------------------------------------
46// macros
47// ----------------------------------------------------------------------------
48
51596bcb
SC
49#if wxUSE_EXTENDED_RTTI
50IMPLEMENT_DYNAMIC_CLASS_XTI(wxButton, wxControl,"wx/button.h")
51
52WX_BEGIN_PROPERTIES_TABLE(wxButton)
53 WX_DELEGATE( OnClick , wxEVT_COMMAND_BUTTON_CLICKED , wxCommandEvent )
066f1b7a 54 // default
51596bcb
SC
55WX_END_PROPERTIES_TABLE()
56
57WX_BEGIN_HANDLERS_TABLE(wxButton)
58WX_END_HANDLERS_TABLE()
59
60WX_CONSTRUCTOR_5( wxButton , wxWindow* , Parent , wxWindowID , Id , wxString , Title , wxPoint , Position , wxSize , Size )
61
62
63#else
cd0b1709 64IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
51596bcb 65#endif
2bda0e17 66
edccf428
VZ
67// this macro tries to adjust the default button height to a reasonable value
68// using the char height as the base
1c4a764c 69#define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
2bda0e17 70
edccf428
VZ
71// ============================================================================
72// implementation
73// ============================================================================
74
75// ----------------------------------------------------------------------------
76// creation/destruction
77// ----------------------------------------------------------------------------
78
79bool wxButton::Create(wxWindow *parent,
80 wxWindowID id,
81 const wxString& label,
82 const wxPoint& pos,
83 const wxSize& size,
84 long style,
85 const wxValidator& validator,
86 const wxString& name)
2bda0e17 87{
5b2f31eb 88 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
edccf428
VZ
89 return FALSE;
90
8292017c
VZ
91 WXDWORD exstyle;
92 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
93
94#ifdef __WIN32__
95 // if the label contains several lines we must explicitly tell the button
96 // about it or it wouldn't draw it correctly ("\n"s would just appear as
97 // black boxes)
98 //
99 // NB: we do it here and not in MSWGetStyle() because we need the label
100 // value and m_label is not set yet when MSWGetStyle() is called;
101 // besides changing BS_MULTILINE during run-time is pointless anyhow
102 if ( label.find(_T('\n')) != wxString::npos )
103 {
104 msStyle |= BS_MULTILINE;
105 }
106#endif // __WIN32__
107
108 return MSWCreateControl(_T("BUTTON"), msStyle, pos, size, label, exstyle);
5b2f31eb
VZ
109}
110
111wxButton::~wxButton()
112{
113}
edccf428 114
5b2f31eb
VZ
115// ----------------------------------------------------------------------------
116// flags
117// ----------------------------------------------------------------------------
cd0b1709 118
5b2f31eb
VZ
119WXDWORD wxButton::MSWGetStyle(long style, WXDWORD *exstyle) const
120{
121 // buttons never have an external border, they draw their own one
122 WXDWORD msStyle = wxControl::MSWGetStyle
123 (
124 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
125 );
f6bcfd97 126
5b2f31eb
VZ
127 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
128 // each other in any resizeable dialog which has more than one button in
129 // the bottom
130 msStyle |= WS_CLIPSIBLINGS;
b0766406 131
f6bcfd97 132#ifdef __WIN32__
5b2f31eb
VZ
133 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
134 // and wxBU_RIGHT to get BS_CENTER!
135 if ( style & wxBU_LEFT )
f6bcfd97 136 msStyle |= BS_LEFT;
5b2f31eb 137 if ( style & wxBU_RIGHT )
f6bcfd97 138 msStyle |= BS_RIGHT;
5b2f31eb 139 if ( style & wxBU_TOP )
f6bcfd97 140 msStyle |= BS_TOP;
5b2f31eb 141 if ( style & wxBU_BOTTOM )
f6bcfd97 142 msStyle |= BS_BOTTOM;
5b2f31eb 143#endif // __WIN32__
edccf428 144
5b2f31eb 145 return msStyle;
2bda0e17
KB
146}
147
edccf428
VZ
148// ----------------------------------------------------------------------------
149// size management including autosizing
150// ----------------------------------------------------------------------------
151
f68586e5 152wxSize wxButton::DoGetBestSize() const
2bda0e17 153{
4438caf4 154 int wBtn;
1e023926 155 GetTextExtent(wxGetWindowText(GetHWND()), &wBtn, NULL);
edccf428 156
4438caf4
VZ
157 int wChar, hChar;
158 wxGetCharSize(GetHWND(), &wChar, &hChar, &GetFont());
159
1e023926 160 // add a margin -- the button is wider than just its label
4438caf4
VZ
161 wBtn += 3*wChar;
162
163 // the button height is proportional to the height of the font used
164 int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
edccf428 165
1e023926
VZ
166 // all buttons have at least the standard size unless the user explicitly
167 // wants them to be of smaller size and used wxBU_EXACTFIT style when
168 // creating the button
169 if ( !HasFlag(wxBU_EXACTFIT) )
188b9f2e
RD
170 {
171 wxSize sz = GetDefaultSize();
1e023926
VZ
172 if (wBtn > sz.x)
173 sz.x = wBtn;
174 if (hBtn > sz.y)
175 sz.y = hBtn;
176
188b9f2e
RD
177 return sz;
178 }
a63f2bec 179
1e023926 180 return wxSize(wBtn, hBtn);
2bda0e17
KB
181}
182
e1f36ff8 183/* static */
1e6feb95 184wxSize wxButtonBase::GetDefaultSize()
e1f36ff8 185{
8c3c31d4 186 static wxSize s_sizeBtn;
e1f36ff8 187
8c3c31d4
VZ
188 if ( s_sizeBtn.x == 0 )
189 {
190 wxScreenDC dc;
a756f210 191 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
8c3c31d4
VZ
192
193 // the size of a standard button in the dialog units is 50x14,
194 // translate this to pixels
195 // NB1: the multipliers come from the Windows convention
196 // NB2: the extra +1/+2 were needed to get the size be the same as the
197 // size of the buttons in the standard dialog - I don't know how
198 // this happens, but on my system this size is 75x23 in pixels and
199 // 23*8 isn't even divisible by 14... Would be nice to understand
200 // why these constants are needed though!
201 s_sizeBtn.x = (50 * (dc.GetCharWidth() + 1))/4;
202 s_sizeBtn.y = ((14 * dc.GetCharHeight()) + 2)/8;
203 }
e1f36ff8 204
8c3c31d4 205 return s_sizeBtn;
e1f36ff8
VZ
206}
207
4438caf4 208// ----------------------------------------------------------------------------
036da5e3 209// default button handling
4438caf4
VZ
210// ----------------------------------------------------------------------------
211
d78f09e2
VZ
212/*
213 "Everything you ever wanted to know about the default buttons" or "Why do we
214 have to do all this?"
215
216 In MSW the default button should be activated when the user presses Enter
217 and the current control doesn't process Enter itself somehow. This is
218 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
219 Another aspect of "defaultness" is that the default button has different
220 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
7fb1b2b4
VZ
221 separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should
222 be unset if our parent window is not active so it should be unset whenever
223 we lose activation and set back when we regain it.
d78f09e2
VZ
224
225 Final complication is that when a button is active, it should be the default
226 one, i.e. pressing Enter on a button always activates it and not another
227 one.
228
229 We handle this by maintaining a permanent and a temporary default items in
230 wxControlContainer (both may be NULL). When a button becomes the current
231 control (i.e. gets focus) it sets itself as the temporary default which
232 ensures that it has the right appearance and that Enter will be redirected
233 to it. When the button loses focus, it unsets the temporary default and so
234 the default item will be the permanent default -- that is the default button
235 if any had been set or none otherwise, which is just what we want.
236
7fb1b2b4
VZ
237 NB: all this is quite complicated by now and the worst is that normally
238 it shouldn't be necessary at all as for the normal Windows programs
239 DefWindowProc() and IsDialogMessage() take care of all this
240 automatically -- however in wxWindows programs this doesn't work for
241 nested hierarchies (i.e. a notebook inside a notebook) for unknown
242 reason and so we have to reproduce all this code ourselves. It would be
243 very nice if we could avoid doing it.
d78f09e2
VZ
244 */
245
036da5e3 246// set this button as the (permanently) default one in its panel
edccf428 247void wxButton::SetDefault()
2bda0e17 248{
edccf428 249 wxWindow *parent = GetParent();
2bda0e17 250
036da5e3
VZ
251 wxCHECK_RET( parent, _T("button without parent?") );
252
7fb1b2b4 253 // set this one as the default button both for wxWindows ...
036da5e3 254 wxWindow *winOldDefault = parent->SetDefaultItem(this);
036da5e3 255
7fb1b2b4
VZ
256 // ... and Windows
257 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), FALSE);
258 SetDefaultStyle(this, TRUE);
036da5e3
VZ
259}
260
7fb1b2b4 261// set this button as being currently default
036da5e3
VZ
262void wxButton::SetTmpDefault()
263{
264 wxWindow *parent = GetParent();
265
266 wxCHECK_RET( parent, _T("button without parent?") );
267
268 wxWindow *winOldDefault = parent->GetDefaultItem();
269 parent->SetTmpDefaultItem(this);
7fb1b2b4
VZ
270
271 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), FALSE);
272 SetDefaultStyle(this, TRUE);
036da5e3
VZ
273}
274
7fb1b2b4 275// unset this button as currently default, it may still stay permanent default
036da5e3
VZ
276void wxButton::UnsetTmpDefault()
277{
278 wxWindow *parent = GetParent();
279
280 wxCHECK_RET( parent, _T("button without parent?") );
281
282 parent->SetTmpDefaultItem(NULL);
283
284 wxWindow *winOldDefault = parent->GetDefaultItem();
7fb1b2b4
VZ
285
286 SetDefaultStyle(this, FALSE);
287 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), TRUE);
036da5e3 288}
8ed57d93 289
036da5e3
VZ
290/* static */
291void
7fb1b2b4 292wxButton::SetDefaultStyle(wxButton *btn, bool on)
036da5e3 293{
7fb1b2b4
VZ
294 // we may be called with NULL pointer -- simpler to do the check here than
295 // in the caller which does wxDynamicCast()
296 if ( !btn )
297 return;
298
299 // first, let DefDlgProc() know about the new default button
300 if ( on )
5d1d2d46 301 {
7fb1b2b4
VZ
302 // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have
303 // focus at all any more
304 if ( !wxTheApp->IsActive() )
305 return;
cd0b1709 306
7fb1b2b4
VZ
307 // look for a panel-like window
308 wxWindow *win = btn->GetParent();
309 while ( win && !win->HasFlag(wxTAB_TRAVERSAL) )
310 win = win->GetParent();
311
312 if ( win )
be4017f8 313 {
7fb1b2b4
VZ
314 ::SendMessage(GetHwndOf(win), DM_SETDEFID, btn->GetId(), 0L);
315
316 // sending DM_SETDEFID also changes the button style to
317 // BS_DEFPUSHBUTTON so there is nothing more to do
be4017f8 318 }
5d1d2d46
VZ
319 }
320
7fb1b2b4
VZ
321 // then also change the style as needed
322 long style = ::GetWindowLong(GetHwndOf(btn), GWL_STYLE);
323 if ( !(style & BS_DEFPUSHBUTTON) == on )
be4017f8 324 {
7fb1b2b4
VZ
325 // don't do it with the owner drawn buttons because it will
326 // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW &
327 // BS_DEFPUSHBUTTON != 0)!
036da5e3
VZ
328 if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
329 {
7fb1b2b4
VZ
330 ::SendMessage(GetHwndOf(btn), BM_SETSTYLE,
331 on ? style | BS_DEFPUSHBUTTON
332 : style & ~BS_DEFPUSHBUTTON,
333 1L /* redraw */);
036da5e3 334 }
7fb1b2b4 335 else // owner drawn
036da5e3 336 {
7fb1b2b4
VZ
337 // redraw the button - it will notice itself that it's
338 // [not] the default one [any longer]
339 btn->Refresh();
036da5e3 340 }
be4017f8 341 }
7fb1b2b4 342 //else: already has correct style
2bda0e17
KB
343}
344
edccf428
VZ
345// ----------------------------------------------------------------------------
346// helpers
347// ----------------------------------------------------------------------------
348
349bool wxButton::SendClickEvent()
2bda0e17 350{
edccf428
VZ
351 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
352 event.SetEventObject(this);
353
354 return ProcessCommand(event);
2bda0e17
KB
355}
356
edccf428 357void wxButton::Command(wxCommandEvent & event)
2bda0e17 358{
edccf428 359 ProcessCommand(event);
2bda0e17
KB
360}
361
edccf428
VZ
362// ----------------------------------------------------------------------------
363// event/message handlers
364// ----------------------------------------------------------------------------
2bda0e17 365
33ac7e6f 366bool wxButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
edccf428
VZ
367{
368 bool processed = FALSE;
57c0af52 369 switch ( param )
edccf428 370 {
5aab763c
RD
371 // NOTE: Apparently older versions (NT 4?) of the common controls send
372 // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn
373 // buttons, so in order to send two EVET_BUTTON events we should
374 // catch both types. Currently (Feb 2003) up-to-date versions of
375 // win98, win2k and winXP all send two BN_CLICKED messages for
376 // all button types, so we don't catch BN_DOUBLECLICKED anymore
377 // in order to not get 3 EVT_BUTTON events. If this is a problem
378 // then we need to figure out which version of the comctl32 changed
379 // this behaviour and test for it.
380
a95e38c0
VZ
381 case 1: // message came from an accelerator
382 case BN_CLICKED: // normal buttons send this
57c0af52
VZ
383 processed = SendClickEvent();
384 break;
edccf428 385 }
2bda0e17 386
edccf428 387 return processed;
2bda0e17
KB
388}
389
678cd6de
VZ
390long wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
391{
036da5e3
VZ
392 // when we receive focus, we want to temporary become the default button in
393 // our parent panel so that pressing "Enter" would activate us -- and when
394 // losing it we should restore the previous default button as well
be4017f8 395 if ( nMsg == WM_SETFOCUS )
678cd6de 396 {
036da5e3 397 SetTmpDefault();
be4017f8 398
036da5e3
VZ
399 // let the default processing take place too
400 }
401 else if ( nMsg == WM_KILLFOCUS )
402 {
403 UnsetTmpDefault();
678cd6de 404 }
a95e38c0
VZ
405 else if ( nMsg == WM_LBUTTONDBLCLK )
406 {
f6bcfd97
BP
407 // emulate a click event to force an owner-drawn button to change its
408 // appearance - without this, it won't do it
409 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN, wParam, lParam);
410
036da5e3 411 // and continue with processing the message normally as well
a95e38c0 412 }
678cd6de
VZ
413
414 // let the base class do all real processing
415 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
416}
cd0b1709
VZ
417
418// ----------------------------------------------------------------------------
419// owner-drawn buttons support
420// ----------------------------------------------------------------------------
421
422#ifdef __WIN32__
423
424// drawing helpers
425
426static void DrawButtonText(HDC hdc,
427 RECT *pRect,
428 const wxString& text,
429 COLORREF col)
430{
431 COLORREF colOld = SetTextColor(hdc, col);
432 int modeOld = SetBkMode(hdc, TRANSPARENT);
433
91f2c154
JS
434 // Note: we must have DT_SINGLELINE for DT_VCENTER to work.
435 ::DrawText(hdc, text, text.length(), pRect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
cd0b1709
VZ
436
437 SetBkMode(hdc, modeOld);
438 SetTextColor(hdc, colOld);
439}
440
441static void DrawRect(HDC hdc, const RECT& r)
442{
4676948b
JS
443 wxDrawLine(hdc, r.left, r.top, r.right, r.top);
444 wxDrawLine(hdc, r.right, r.top, r.right, r.bottom);
445 wxDrawLine(hdc, r.right, r.bottom, r.left, r.bottom);
446 wxDrawLine(hdc, r.left, r.bottom, r.left, r.top);
cd0b1709
VZ
447}
448
449void wxButton::MakeOwnerDrawn()
450{
451 long style = GetWindowLong(GetHwnd(), GWL_STYLE);
9750fc42 452 if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
cd0b1709
VZ
453 {
454 // make it so
455 style |= BS_OWNERDRAW;
456 SetWindowLong(GetHwnd(), GWL_STYLE, style);
457 }
458}
459
460bool wxButton::SetBackgroundColour(const wxColour &colour)
461{
462 if ( !wxControl::SetBackgroundColour(colour) )
463 {
464 // nothing to do
465 return FALSE;
466 }
467
468 MakeOwnerDrawn();
469
470 Refresh();
471
472 return TRUE;
473}
474
475bool wxButton::SetForegroundColour(const wxColour &colour)
476{
477 if ( !wxControl::SetForegroundColour(colour) )
478 {
479 // nothing to do
480 return FALSE;
481 }
482
483 MakeOwnerDrawn();
484
485 Refresh();
486
487 return TRUE;
488}
489
490/*
491 The button frame looks like this normally:
492
493 WWWWWWWWWWWWWWWWWWB
16162a64
GRG
494 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
495 WH GB H = light grey (LIGHT)
496 WH GB G = dark grey (SHADOW)
497 WH GB B = black (DKSHADOW)
498 WH GB
cd0b1709
VZ
499 WGGGGGGGGGGGGGGGGGB
500 BBBBBBBBBBBBBBBBBBB
501
502 When the button is selected, the button becomes like this (the total button
503 size doesn't change):
504
505 BBBBBBBBBBBBBBBBBBB
506 BWWWWWWWWWWWWWWWWBB
16162a64
GRG
507 BWHHHHHHHHHHHHHHGBB
508 BWH GBB
509 BWH GBB
cd0b1709
VZ
510 BWGGGGGGGGGGGGGGGBB
511 BBBBBBBBBBBBBBBBBBB
512 BBBBBBBBBBBBBBBBBBB
513
514 When the button is pushed (while selected) it is like:
515
516 BBBBBBBBBBBBBBBBBBB
517 BGGGGGGGGGGGGGGGGGB
518 BG GB
519 BG GB
520 BG GB
16162a64 521 BG GB
cd0b1709
VZ
522 BGGGGGGGGGGGGGGGGGB
523 BBBBBBBBBBBBBBBBBBB
524*/
525
526static void DrawButtonFrame(HDC hdc, const RECT& rectBtn,
527 bool selected, bool pushed)
528{
529 RECT r;
530 CopyRect(&r, &rectBtn);
531
16162a64
GRG
532 HPEN hpenBlack = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DDKSHADOW)),
533 hpenGrey = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW)),
534 hpenLightGr = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DLIGHT)),
535 hpenWhite = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DHILIGHT));
cd0b1709
VZ
536
537 HPEN hpenOld = (HPEN)SelectObject(hdc, hpenBlack);
538
539 r.right--;
540 r.bottom--;
541
542 if ( pushed )
543 {
544 DrawRect(hdc, r);
545
546 (void)SelectObject(hdc, hpenGrey);
547 InflateRect(&r, -1, -1);
548
549 DrawRect(hdc, r);
550 }
551 else // !pushed
552 {
553 if ( selected )
554 {
555 DrawRect(hdc, r);
556
557 InflateRect(&r, -1, -1);
558 }
559
4676948b
JS
560 wxDrawLine(hdc, r.left, r.bottom, r.right, r.bottom);
561 wxDrawLine(hdc, r.right, r.bottom, r.right, r.top - 1);
cd0b1709
VZ
562
563 (void)SelectObject(hdc, hpenWhite);
4676948b
JS
564 wxDrawLine(hdc, r.left, r.bottom - 1, r.left, r.top);
565 wxDrawLine(hdc, r.left, r.top, r.right, r.top);
cd0b1709 566
16162a64 567 (void)SelectObject(hdc, hpenLightGr);
4676948b
JS
568 wxDrawLine(hdc, r.left + 1, r.bottom - 2, r.left + 1, r.top + 1);
569 wxDrawLine(hdc, r.left + 1, r.top + 1, r.right - 1, r.top + 1);
16162a64 570
cd0b1709 571 (void)SelectObject(hdc, hpenGrey);
4676948b
JS
572 wxDrawLine(hdc, r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1);
573 wxDrawLine(hdc, r.right - 1, r.bottom - 1, r.right - 1, r.top);
cd0b1709
VZ
574 }
575
576 (void)SelectObject(hdc, hpenOld);
577 DeleteObject(hpenWhite);
16162a64 578 DeleteObject(hpenLightGr);
cd0b1709
VZ
579 DeleteObject(hpenGrey);
580 DeleteObject(hpenBlack);
581}
582
583bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT *wxdis)
584{
585 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)wxdis;
586
587 RECT rectBtn;
588 CopyRect(&rectBtn, &lpDIS->rcItem);
589
590 COLORREF colBg = wxColourToRGB(GetBackgroundColour()),
591 colFg = wxColourToRGB(GetForegroundColour());
592
593 HDC hdc = lpDIS->hDC;
594 UINT state = lpDIS->itemState;
595
596 // first, draw the background
597 HBRUSH hbrushBackground = ::CreateSolidBrush(colBg);
598
599 FillRect(hdc, &rectBtn, hbrushBackground);
600
601 // draw the border for the current state
602 bool selected = (state & ODS_SELECTED) != 0;
603 if ( !selected )
604 {
605 wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
606 if ( panel )
607 {
608 selected = panel->GetDefaultItem() == this;
609 }
610 }
611 bool pushed = (SendMessage(GetHwnd(), BM_GETSTATE, 0, 0) & BST_PUSHED) != 0;
612
613 DrawButtonFrame(hdc, rectBtn, selected, pushed);
614
615 // draw the focus rect if needed
616 if ( state & ODS_FOCUS )
617 {
618 RECT rectFocus;
619 CopyRect(&rectFocus, &rectBtn);
620
621 // I don't know where does this constant come from, but this is how
622 // Windows draws them
623 InflateRect(&rectFocus, -4, -4);
624
625 DrawFocusRect(hdc, &rectFocus);
626 }
627
9750fc42
VZ
628 if ( pushed )
629 {
630 // the label is shifted by 1 pixel to create "pushed" effect
631 OffsetRect(&rectBtn, 1, 1);
632 }
633
cd0b1709
VZ
634 DrawButtonText(hdc, &rectBtn, GetLabel(),
635 state & ODS_DISABLED ? GetSysColor(COLOR_GRAYTEXT)
636 : colFg);
637
638 ::DeleteObject(hbrushBackground);
639
cd0b1709
VZ
640 return TRUE;
641}
642
643#endif // __WIN32__
1e6feb95
VZ
644
645#endif // wxUSE_BUTTON
646