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