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