Determine wxButton size correctly in wxMSW.
[wxWidgets.git] / src / msw / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/button.cpp
3 // Purpose: wxButton
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_BUTTON
28
29 #include "wx/button.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/brush.h"
34 #include "wx/panel.h"
35 #include "wx/bmpbuttn.h"
36 #include "wx/settings.h"
37 #include "wx/dcscreen.h"
38 #include "wx/dcclient.h"
39 #include "wx/toplevel.h"
40 #endif
41
42 #include "wx/imaglist.h"
43 #include "wx/stockitem.h"
44 #include "wx/msw/private.h"
45 #include "wx/msw/private/button.h"
46 #include "wx/msw/private/dc.h"
47
48 using namespace wxMSWImpl;
49
50 #if wxUSE_UXTHEME
51 #include "wx/msw/uxtheme.h"
52
53 // no need to include tmschema.h
54 #ifndef BP_PUSHBUTTON
55 #define BP_PUSHBUTTON 1
56
57 #define PBS_NORMAL 1
58 #define PBS_HOT 2
59 #define PBS_PRESSED 3
60 #define PBS_DISABLED 4
61 #define PBS_DEFAULTED 5
62
63 #define TMT_CONTENTMARGINS 3602
64 #endif
65
66 // provide the necessary declarations ourselves if they're missing from
67 // headers
68 #ifndef BCM_SETIMAGELIST
69 #define BCM_SETIMAGELIST 0x1602
70 #define BCM_SETTEXTMARGIN 0x1604
71
72 enum
73 {
74 BUTTON_IMAGELIST_ALIGN_LEFT,
75 BUTTON_IMAGELIST_ALIGN_RIGHT,
76 BUTTON_IMAGELIST_ALIGN_TOP,
77 BUTTON_IMAGELIST_ALIGN_BOTTOM
78 };
79
80 struct BUTTON_IMAGELIST
81 {
82 HIMAGELIST himl;
83 RECT margin;
84 UINT uAlign;
85 };
86 #endif
87 #endif // wxUSE_UXTHEME
88
89 #ifndef WM_THEMECHANGED
90 #define WM_THEMECHANGED 0x031A
91 #endif
92
93 #ifndef ODS_NOACCEL
94 #define ODS_NOACCEL 0x0100
95 #endif
96
97 #ifndef ODS_NOFOCUSRECT
98 #define ODS_NOFOCUSRECT 0x0200
99 #endif
100
101 #ifndef DT_HIDEPREFIX
102 #define DT_HIDEPREFIX 0x00100000
103 #endif
104
105 // ----------------------------------------------------------------------------
106 // button image data
107 // ----------------------------------------------------------------------------
108
109 // we use different data classes for owner drawn buttons and for themed XP ones
110
111 class wxButtonImageData
112 {
113 public:
114 wxButtonImageData() { }
115 virtual ~wxButtonImageData() { }
116
117 virtual wxBitmap GetBitmap(wxButton::State which) const = 0;
118 virtual void SetBitmap(const wxBitmap& bitmap, wxButton::State which) = 0;
119
120 virtual wxSize GetBitmapMargins() const = 0;
121 virtual void SetBitmapMargins(wxCoord x, wxCoord y) = 0;
122
123 virtual wxDirection GetBitmapPosition() const = 0;
124 virtual void SetBitmapPosition(wxDirection dir) = 0;
125
126 private:
127 wxDECLARE_NO_COPY_CLASS(wxButtonImageData);
128 };
129
130 namespace
131 {
132
133 // the gap between button edge and the interior area used by Windows for the
134 // standard buttons
135 const int OD_BUTTON_MARGIN = 4;
136
137 class wxODButtonImageData : public wxButtonImageData
138 {
139 public:
140 wxODButtonImageData(wxButton *btn, const wxBitmap& bitmap)
141 {
142 SetBitmap(bitmap, wxButton::State_Normal);
143
144 m_dir = wxLEFT;
145
146 // we use margins when we have both bitmap and text, but when we have
147 // only the bitmap it should take up the entire button area
148 if ( btn->ShowsLabel() )
149 {
150 m_margin.x = btn->GetCharWidth();
151 m_margin.y = btn->GetCharHeight() / 2;
152 }
153 }
154
155 virtual wxBitmap GetBitmap(wxButton::State which) const
156 {
157 return m_bitmaps[which];
158 }
159
160 virtual void SetBitmap(const wxBitmap& bitmap, wxButton::State which)
161 {
162 m_bitmaps[which] = bitmap;
163 }
164
165 virtual wxSize GetBitmapMargins() const
166 {
167 return m_margin;
168 }
169
170 virtual void SetBitmapMargins(wxCoord x, wxCoord y)
171 {
172 m_margin = wxSize(x, y);
173 }
174
175 virtual wxDirection GetBitmapPosition() const
176 {
177 return m_dir;
178 }
179
180 virtual void SetBitmapPosition(wxDirection dir)
181 {
182 m_dir = dir;
183 }
184
185 private:
186 // just store the values passed to us to be able to retrieve them later
187 // from the drawing code
188 wxBitmap m_bitmaps[wxButton::State_Max];
189 wxSize m_margin;
190 wxDirection m_dir;
191
192 wxDECLARE_NO_COPY_CLASS(wxODButtonImageData);
193 };
194
195 #if wxUSE_UXTHEME
196
197 // somehow the margin is one pixel greater than the value returned by
198 // GetThemeMargins() call
199 const int XP_BUTTON_EXTRA_MARGIN = 1;
200
201 class wxXPButtonImageData : public wxButtonImageData
202 {
203 public:
204 // we must be constructed with the size of our images as we need to create
205 // the image list
206 wxXPButtonImageData(wxButton *btn, const wxBitmap& bitmap)
207 : m_iml(bitmap.GetWidth(), bitmap.GetHeight(), true /* use mask */,
208 wxButton::State_Max),
209 m_hwndBtn(GetHwndOf(btn))
210 {
211 // initialize all bitmaps to normal state
212 for ( int n = 0; n < wxButton::State_Max; n++ )
213 {
214 m_iml.Add(bitmap);
215 }
216
217 m_data.himl = GetHimagelistOf(&m_iml);
218
219 // use default margins
220 m_data.margin.left =
221 m_data.margin.right = btn->GetCharWidth();
222 m_data.margin.top =
223 m_data.margin.bottom = btn->GetCharHeight() / 2;
224
225 // and default alignment
226 m_data.uAlign = BUTTON_IMAGELIST_ALIGN_LEFT;
227
228 UpdateImageInfo();
229 }
230
231 virtual wxBitmap GetBitmap(wxButton::State which) const
232 {
233 return m_iml.GetBitmap(which);
234 }
235
236 virtual void SetBitmap(const wxBitmap& bitmap, wxButton::State which)
237 {
238 m_iml.Replace(which, bitmap);
239
240 UpdateImageInfo();
241 }
242
243 virtual wxSize GetBitmapMargins() const
244 {
245 return wxSize(m_data.margin.left, m_data.margin.top);
246 }
247
248 virtual void SetBitmapMargins(wxCoord x, wxCoord y)
249 {
250 RECT& margin = m_data.margin;
251 margin.left =
252 margin.right = x;
253 margin.top =
254 margin.bottom = y;
255
256 if ( !::SendMessage(m_hwndBtn, BCM_SETTEXTMARGIN, 0, (LPARAM)&margin) )
257 {
258 wxLogDebug("SendMessage(BCM_SETTEXTMARGIN) failed");
259 }
260 }
261
262 virtual wxDirection GetBitmapPosition() const
263 {
264 switch ( m_data.uAlign )
265 {
266 default:
267 wxFAIL_MSG( "invalid image alignment" );
268 // fall through
269
270 case BUTTON_IMAGELIST_ALIGN_LEFT:
271 return wxLEFT;
272
273 case BUTTON_IMAGELIST_ALIGN_RIGHT:
274 return wxRIGHT;
275
276 case BUTTON_IMAGELIST_ALIGN_TOP:
277 return wxTOP;
278
279 case BUTTON_IMAGELIST_ALIGN_BOTTOM:
280 return wxBOTTOM;
281 }
282 }
283
284 virtual void SetBitmapPosition(wxDirection dir)
285 {
286 UINT alignNew;
287 switch ( dir )
288 {
289 default:
290 wxFAIL_MSG( "invalid direction" );
291 // fall through
292
293 case wxLEFT:
294 alignNew = BUTTON_IMAGELIST_ALIGN_LEFT;
295 break;
296
297 case wxRIGHT:
298 alignNew = BUTTON_IMAGELIST_ALIGN_RIGHT;
299 break;
300
301 case wxTOP:
302 alignNew = BUTTON_IMAGELIST_ALIGN_TOP;
303 break;
304
305 case wxBOTTOM:
306 alignNew = BUTTON_IMAGELIST_ALIGN_BOTTOM;
307 break;
308 }
309
310 if ( alignNew != m_data.uAlign )
311 {
312 m_data.uAlign = alignNew;
313 UpdateImageInfo();
314 }
315 }
316
317 private:
318 void UpdateImageInfo()
319 {
320 if ( !::SendMessage(m_hwndBtn, BCM_SETIMAGELIST, 0, (LPARAM)&m_data) )
321 {
322 wxLogDebug("SendMessage(BCM_SETIMAGELIST) failed");
323 }
324 }
325
326 // we store image list separately to be able to use convenient wxImageList
327 // methods instead of working with raw HIMAGELIST
328 wxImageList m_iml;
329
330 // store the rest of the data in BCM_SETIMAGELIST-friendly form
331 BUTTON_IMAGELIST m_data;
332
333 // the button we're associated with
334 const HWND m_hwndBtn;
335
336
337 wxDECLARE_NO_COPY_CLASS(wxXPButtonImageData);
338 };
339
340 #endif // wxUSE_UXTHEME
341
342 } // anonymous namespace
343
344 // ----------------------------------------------------------------------------
345 // macros
346 // ----------------------------------------------------------------------------
347
348 #if wxUSE_EXTENDED_RTTI
349
350 WX_DEFINE_FLAGS( wxButtonStyle )
351
352 wxBEGIN_FLAGS( wxButtonStyle )
353 // new style border flags, we put them first to
354 // use them for streaming out
355 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
356 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
357 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
358 wxFLAGS_MEMBER(wxBORDER_RAISED)
359 wxFLAGS_MEMBER(wxBORDER_STATIC)
360 wxFLAGS_MEMBER(wxBORDER_NONE)
361
362 // old style border flags
363 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
364 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
365 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
366 wxFLAGS_MEMBER(wxRAISED_BORDER)
367 wxFLAGS_MEMBER(wxSTATIC_BORDER)
368 wxFLAGS_MEMBER(wxBORDER)
369
370 // standard window styles
371 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
372 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
373 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
374 wxFLAGS_MEMBER(wxWANTS_CHARS)
375 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
376 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
377 wxFLAGS_MEMBER(wxVSCROLL)
378 wxFLAGS_MEMBER(wxHSCROLL)
379
380 wxFLAGS_MEMBER(wxBU_LEFT)
381 wxFLAGS_MEMBER(wxBU_RIGHT)
382 wxFLAGS_MEMBER(wxBU_TOP)
383 wxFLAGS_MEMBER(wxBU_BOTTOM)
384 wxFLAGS_MEMBER(wxBU_EXACTFIT)
385 wxEND_FLAGS( wxButtonStyle )
386
387 IMPLEMENT_DYNAMIC_CLASS_XTI(wxButton, wxControl,"wx/button.h")
388
389 wxBEGIN_PROPERTIES_TABLE(wxButton)
390 wxEVENT_PROPERTY( Click , wxEVT_COMMAND_BUTTON_CLICKED , wxCommandEvent)
391
392 wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
393 wxPROPERTY( Label, wxString , SetLabel, GetLabel, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
394
395 wxPROPERTY_FLAGS( WindowStyle , wxButtonStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
396
397 wxEND_PROPERTIES_TABLE()
398
399 wxBEGIN_HANDLERS_TABLE(wxButton)
400 wxEND_HANDLERS_TABLE()
401
402 wxCONSTRUCTOR_6( wxButton , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle )
403
404
405 #else
406 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
407 #endif
408
409 // ============================================================================
410 // implementation
411 // ============================================================================
412
413 // ----------------------------------------------------------------------------
414 // helper functions from wx/msw/private/button.h
415 // ----------------------------------------------------------------------------
416
417 void wxMSWButton::UpdateMultilineStyle(HWND hwnd, const wxString& label)
418 {
419 // update BS_MULTILINE style depending on the new label (resetting it
420 // doesn't seem to do anything very useful but it shouldn't hurt and we do
421 // have to set it whenever the label becomes multi line as otherwise it
422 // wouldn't be shown correctly as we don't use BS_MULTILINE when creating
423 // the control unless it already has new lines in its label)
424 long styleOld = ::GetWindowLong(hwnd, GWL_STYLE),
425 styleNew;
426 if ( label.find(wxT('\n')) != wxString::npos )
427 styleNew = styleOld | BS_MULTILINE;
428 else
429 styleNew = styleOld & ~BS_MULTILINE;
430
431 if ( styleNew != styleOld )
432 ::SetWindowLong(hwnd, GWL_STYLE, styleNew);
433 }
434
435 wxSize wxMSWButton::GetFittingSize(wxWindow *win, const wxSize& sizeLabel)
436 {
437 // FIXME: this is pure guesswork, need to retrieve the real button margins
438 wxSize sizeBtn = sizeLabel;
439
440 sizeBtn.x += 3*win->GetCharWidth();
441 sizeBtn.y = 11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(sizeLabel.y)/10;
442
443 return sizeBtn;
444 }
445
446 wxSize wxMSWButton::ComputeBestSize(wxControl *btn)
447 {
448 wxClientDC dc(btn);
449
450 wxSize sizeBtn;
451 dc.GetMultiLineTextExtent(btn->GetLabelText(), &sizeBtn.x, &sizeBtn.y);
452
453 sizeBtn = GetFittingSize(btn, sizeBtn);
454
455 // all buttons have at least the standard size unless the user explicitly
456 // wants them to be of smaller size and used wxBU_EXACTFIT style when
457 // creating the button
458 if ( !btn->HasFlag(wxBU_EXACTFIT) )
459 {
460 // The size of a standard button in the dialog units is 50x14, use it.
461 // Note that we intentionally don't use GetDefaultSize() here, because
462 // it's inexact -- dialog units depend on this dialog's font.
463 wxSize sizeDef = btn->ConvertDialogToPixels(wxSize(50, 14));
464 if ( sizeBtn.x < sizeDef.x )
465 sizeBtn.x = sizeDef.x;
466 if ( sizeBtn.y < sizeDef.y )
467 sizeBtn.y = sizeDef.y;
468 }
469
470 btn->CacheBestSize(sizeBtn);
471
472 return sizeBtn;
473 }
474
475 // ----------------------------------------------------------------------------
476 // creation/destruction
477 // ----------------------------------------------------------------------------
478
479 bool wxButton::Create(wxWindow *parent,
480 wxWindowID id,
481 const wxString& lbl,
482 const wxPoint& pos,
483 const wxSize& size,
484 long style,
485 const wxValidator& validator,
486 const wxString& name)
487 {
488 wxString label(lbl);
489 if (label.empty() && wxIsStockID(id))
490 {
491 // On Windows, some buttons aren't supposed to have mnemonics
492 label = wxGetStockLabel
493 (
494 id,
495 id == wxID_OK || id == wxID_CANCEL || id == wxID_CLOSE
496 ? wxSTOCK_NOFLAGS
497 : wxSTOCK_WITH_MNEMONIC
498 );
499 }
500
501 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
502 return false;
503
504 WXDWORD exstyle;
505 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
506
507 // if the label contains several lines we must explicitly tell the button
508 // about it or it wouldn't draw it correctly ("\n"s would just appear as
509 // black boxes)
510 //
511 // NB: we do it here and not in MSWGetStyle() because we need the label
512 // value and the label is not set yet when MSWGetStyle() is called
513 msStyle |= wxMSWButton::GetMultilineStyle(label);
514
515 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, exstyle);
516 }
517
518 wxButton::~wxButton()
519 {
520 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
521 if ( tlw && tlw->GetTmpDefaultItem() == this )
522 {
523 UnsetTmpDefault();
524 }
525
526 delete m_imageData;
527 }
528
529 // ----------------------------------------------------------------------------
530 // flags
531 // ----------------------------------------------------------------------------
532
533 WXDWORD wxButton::MSWGetStyle(long style, WXDWORD *exstyle) const
534 {
535 // buttons never have an external border, they draw their own one
536 WXDWORD msStyle = wxControl::MSWGetStyle
537 (
538 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
539 );
540
541 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
542 // each other in any resizeable dialog which has more than one button in
543 // the bottom
544 msStyle |= WS_CLIPSIBLINGS;
545
546 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
547 // and wxBU_RIGHT to get BS_CENTER!
548 if ( style & wxBU_LEFT )
549 msStyle |= BS_LEFT;
550 if ( style & wxBU_RIGHT )
551 msStyle |= BS_RIGHT;
552 if ( style & wxBU_TOP )
553 msStyle |= BS_TOP;
554 if ( style & wxBU_BOTTOM )
555 msStyle |= BS_BOTTOM;
556 #ifndef __WXWINCE__
557 // flat 2d buttons
558 if ( style & wxNO_BORDER )
559 msStyle |= BS_FLAT;
560 #endif // __WXWINCE__
561
562 return msStyle;
563 }
564
565 void wxButton::SetLabel(const wxString& label)
566 {
567 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label);
568
569 wxButtonBase::SetLabel(label);
570 }
571
572 // ----------------------------------------------------------------------------
573 // size management including autosizing
574 // ----------------------------------------------------------------------------
575
576 wxSize wxButton::DoGetBestSize() const
577 {
578 wxSize size;
579
580 // account for the text part if we have it or if we don't have any image at
581 // all (buttons initially created with empty label should still have a non
582 // zero size)
583 if ( ShowsLabel() || !m_imageData )
584 {
585 size = wxMSWButton::ComputeBestSize(const_cast<wxButton *>(this));
586 }
587
588 if ( m_imageData )
589 {
590 // account for the bitmap size
591 const wxSize sizeBmp = m_imageData->GetBitmap(State_Normal).GetSize();
592 const wxDirection dirBmp = m_imageData->GetBitmapPosition();
593 if ( dirBmp == wxLEFT || dirBmp == wxRIGHT )
594 {
595 size.x += sizeBmp.x;
596 if ( sizeBmp.y > size.y )
597 size.y = sizeBmp.y;
598 }
599 else // bitmap on top/below the text
600 {
601 size.y += sizeBmp.y;
602 if ( sizeBmp.x > size.x )
603 size.x = sizeBmp.x;
604 }
605
606 // account for the user-specified margins
607 size += 2*m_imageData->GetBitmapMargins();
608
609 // and also for the margins we always add internally (unless we have no
610 // border at all in which case the button has exactly the same size as
611 // bitmap and so no margins should be used)
612 if ( !HasFlag(wxBORDER_NONE) )
613 {
614 int marginH = 0,
615 marginV = 0;
616 #if wxUSE_UXTHEME
617 if ( wxUxThemeEngine::GetIfActive() )
618 {
619 wxUxThemeHandle theme(const_cast<wxButton *>(this), L"BUTTON");
620
621 MARGINS margins;
622 wxUxThemeEngine::Get()->GetThemeMargins(theme, NULL,
623 BP_PUSHBUTTON,
624 PBS_NORMAL,
625 TMT_CONTENTMARGINS,
626 NULL,
627 &margins);
628
629 // XP doesn't draw themed buttons correctly when the client
630 // area is smaller than 8x8 - enforce this minimum size for
631 // small bitmaps
632 size.IncTo(wxSize(8, 8));
633
634 marginH = margins.cxLeftWidth + margins.cxRightWidth
635 + 2*XP_BUTTON_EXTRA_MARGIN;
636 marginV = margins.cyTopHeight + margins.cyBottomHeight
637 + 2*XP_BUTTON_EXTRA_MARGIN;
638 }
639 else
640 #endif // wxUSE_UXTHEME
641 {
642 marginH =
643 marginV = OD_BUTTON_MARGIN;
644 }
645
646 size.IncBy(marginH, marginV);
647 }
648
649 CacheBestSize(size);
650 }
651
652 return size;
653 }
654
655 /* static */
656 wxSize wxButtonBase::GetDefaultSize()
657 {
658 static wxSize s_sizeBtn;
659
660 if ( s_sizeBtn.x == 0 )
661 {
662 wxScreenDC dc;
663 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
664
665 // the size of a standard button in the dialog units is 50x14,
666 // translate this to pixels
667 // NB1: the multipliers come from the Windows convention
668 // NB2: the extra +1/+2 were needed to get the size be the same as the
669 // size of the buttons in the standard dialog - I don't know how
670 // this happens, but on my system this size is 75x23 in pixels and
671 // 23*8 isn't even divisible by 14... Would be nice to understand
672 // why these constants are needed though!
673 s_sizeBtn.x = (50 * (dc.GetCharWidth() + 1))/4;
674 s_sizeBtn.y = ((14 * dc.GetCharHeight()) + 2)/8;
675 }
676
677 return s_sizeBtn;
678 }
679
680 // ----------------------------------------------------------------------------
681 // default button handling
682 // ----------------------------------------------------------------------------
683
684 /*
685 The comment below and all this code is probably due to not using WM_NEXTDLGCTL
686 message when changing focus (but just SetFocus() which is not enough), see
687 http://blogs.msdn.com/oldnewthing/archive/2004/08/02/205624.aspx for the
688 full explanation.
689
690 TODO: Do use WM_NEXTDLGCTL and get rid of all this code.
691
692
693 "Everything you ever wanted to know about the default buttons" or "Why do we
694 have to do all this?"
695
696 In MSW the default button should be activated when the user presses Enter
697 and the current control doesn't process Enter itself somehow. This is
698 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
699 Another aspect of "defaultness" is that the default button has different
700 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
701 separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should
702 be unset if our parent window is not active so it should be unset whenever
703 we lose activation and set back when we regain it.
704
705 Final complication is that when a button is active, it should be the default
706 one, i.e. pressing Enter on a button always activates it and not another
707 one.
708
709 We handle this by maintaining a permanent and a temporary default items in
710 wxControlContainer (both may be NULL). When a button becomes the current
711 control (i.e. gets focus) it sets itself as the temporary default which
712 ensures that it has the right appearance and that Enter will be redirected
713 to it. When the button loses focus, it unsets the temporary default and so
714 the default item will be the permanent default -- that is the default button
715 if any had been set or none otherwise, which is just what we want.
716
717 NB: all this is quite complicated by now and the worst is that normally
718 it shouldn't be necessary at all as for the normal Windows programs
719 DefWindowProc() and IsDialogMessage() take care of all this
720 automatically -- however in wxWidgets programs this doesn't work for
721 nested hierarchies (i.e. a notebook inside a notebook) for unknown
722 reason and so we have to reproduce all this code ourselves. It would be
723 very nice if we could avoid doing it.
724 */
725
726 // set this button as the (permanently) default one in its panel
727 wxWindow *wxButton::SetDefault()
728 {
729 // set this one as the default button both for wxWidgets ...
730 wxWindow *winOldDefault = wxButtonBase::SetDefault();
731
732 // ... and Windows
733 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false);
734 SetDefaultStyle(this, true);
735
736 return winOldDefault;
737 }
738
739 // return the top level parent window if it's not being deleted yet, otherwise
740 // return NULL
741 static wxTopLevelWindow *GetTLWParentIfNotBeingDeleted(wxWindow *win)
742 {
743 for ( ;; )
744 {
745 // IsTopLevel() will return false for a wxTLW being deleted, so we also
746 // need the parent test for this case
747 wxWindow * const parent = win->GetParent();
748 if ( !parent || win->IsTopLevel() )
749 {
750 if ( win->IsBeingDeleted() )
751 return NULL;
752
753 break;
754 }
755
756 win = parent;
757 }
758
759 wxASSERT_MSG( win, wxT("button without top level parent?") );
760
761 wxTopLevelWindow * const tlw = wxDynamicCast(win, wxTopLevelWindow);
762 wxASSERT_MSG( tlw, wxT("logic error in GetTLWParentIfNotBeingDeleted()") );
763
764 return tlw;
765 }
766
767 // set this button as being currently default
768 void wxButton::SetTmpDefault()
769 {
770 wxTopLevelWindow * const tlw = GetTLWParentIfNotBeingDeleted(GetParent());
771 if ( !tlw )
772 return;
773
774 wxWindow *winOldDefault = tlw->GetDefaultItem();
775 tlw->SetTmpDefaultItem(this);
776
777 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false);
778 SetDefaultStyle(this, true);
779 }
780
781 // unset this button as currently default, it may still stay permanent default
782 void wxButton::UnsetTmpDefault()
783 {
784 wxTopLevelWindow * const tlw = GetTLWParentIfNotBeingDeleted(GetParent());
785 if ( !tlw )
786 return;
787
788 tlw->SetTmpDefaultItem(NULL);
789
790 wxWindow *winOldDefault = tlw->GetDefaultItem();
791
792 SetDefaultStyle(this, false);
793 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), true);
794 }
795
796 /* static */
797 void
798 wxButton::SetDefaultStyle(wxButton *btn, bool on)
799 {
800 // we may be called with NULL pointer -- simpler to do the check here than
801 // in the caller which does wxDynamicCast()
802 if ( !btn )
803 return;
804
805 // first, let DefDlgProc() know about the new default button
806 if ( on )
807 {
808 // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have
809 // focus at all any more
810 if ( !wxTheApp->IsActive() )
811 return;
812
813 wxWindow * const tlw = wxGetTopLevelParent(btn);
814 wxCHECK_RET( tlw, wxT("button without top level window?") );
815
816 ::SendMessage(GetHwndOf(tlw), DM_SETDEFID, btn->GetId(), 0L);
817
818 // sending DM_SETDEFID also changes the button style to
819 // BS_DEFPUSHBUTTON so there is nothing more to do
820 }
821
822 // then also change the style as needed
823 long style = ::GetWindowLong(GetHwndOf(btn), GWL_STYLE);
824 if ( !(style & BS_DEFPUSHBUTTON) == on )
825 {
826 // don't do it with the owner drawn buttons because it will
827 // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW &
828 // BS_DEFPUSHBUTTON != 0)!
829 if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
830 {
831 ::SendMessage(GetHwndOf(btn), BM_SETSTYLE,
832 on ? style | BS_DEFPUSHBUTTON
833 : style & ~BS_DEFPUSHBUTTON,
834 1L /* redraw */);
835 }
836 else // owner drawn
837 {
838 // redraw the button - it will notice itself that it's
839 // [not] the default one [any longer]
840 btn->Refresh();
841 }
842 }
843 //else: already has correct style
844 }
845
846 // ----------------------------------------------------------------------------
847 // helpers
848 // ----------------------------------------------------------------------------
849
850 bool wxButton::SendClickEvent()
851 {
852 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
853 event.SetEventObject(this);
854
855 return ProcessCommand(event);
856 }
857
858 void wxButton::Command(wxCommandEvent & event)
859 {
860 ProcessCommand(event);
861 }
862
863 // ----------------------------------------------------------------------------
864 // event/message handlers
865 // ----------------------------------------------------------------------------
866
867 bool wxButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
868 {
869 bool processed = false;
870 switch ( param )
871 {
872 // NOTE: Apparently older versions (NT 4?) of the common controls send
873 // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn
874 // buttons, so in order to send two EVT_BUTTON events we should
875 // catch both types. Currently (Feb 2003) up-to-date versions of
876 // win98, win2k and winXP all send two BN_CLICKED messages for
877 // all button types, so we don't catch BN_DOUBLECLICKED anymore
878 // in order to not get 3 EVT_BUTTON events. If this is a problem
879 // then we need to figure out which version of the comctl32 changed
880 // this behaviour and test for it.
881
882 case 1: // message came from an accelerator
883 case BN_CLICKED: // normal buttons send this
884 processed = SendClickEvent();
885 break;
886 }
887
888 return processed;
889 }
890
891 WXLRESULT wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
892 {
893 // when we receive focus, we want to temporarily become the default button in
894 // our parent panel so that pressing "Enter" would activate us -- and when
895 // losing it we should restore the previous default button as well
896 if ( nMsg == WM_SETFOCUS )
897 {
898 SetTmpDefault();
899
900 // let the default processing take place too
901 }
902 else if ( nMsg == WM_KILLFOCUS )
903 {
904 UnsetTmpDefault();
905 }
906 else if ( nMsg == WM_LBUTTONDBLCLK )
907 {
908 // emulate a click event to force an owner-drawn button to change its
909 // appearance - without this, it won't do it
910 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN, wParam, lParam);
911
912 // and continue with processing the message normally as well
913 }
914 #if wxUSE_UXTHEME
915 else if ( nMsg == WM_THEMECHANGED )
916 {
917 // need to recalculate the best size here
918 // as the theme size might have changed
919 InvalidateBestSize();
920 }
921 #endif // wxUSE_UXTHEME
922 // must use m_mouseInWindow here instead of IsMouseInWindow()
923 // since we need to know the first time the mouse enters the window
924 // and IsMouseInWindow() would return true in this case
925 else if ( (nMsg == WM_MOUSEMOVE && !m_mouseInWindow) ||
926 nMsg == WM_MOUSELEAVE )
927 {
928 if (
929 IsEnabled() &&
930 (
931 #if wxUSE_UXTHEME
932 wxUxThemeEngine::GetIfActive() ||
933 #endif // wxUSE_UXTHEME
934 m_imageData && m_imageData->GetBitmap(State_Current).IsOk()
935 )
936 )
937 {
938 Refresh();
939 }
940 }
941
942 // let the base class do all real processing
943 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
944 }
945
946 // ----------------------------------------------------------------------------
947 // button images
948 // ----------------------------------------------------------------------------
949
950 wxBitmap wxButton::DoGetBitmap(State which) const
951 {
952 return m_imageData ? m_imageData->GetBitmap(which) : wxBitmap();
953 }
954
955 void wxButton::DoSetBitmap(const wxBitmap& bitmap, State which)
956 {
957 // allocate the image data when the first bitmap is set
958 if ( !m_imageData )
959 {
960 #if wxUSE_UXTHEME
961 // using image list doesn't work correctly if we don't have any label
962 // (even if we use BUTTON_IMAGELIST_ALIGN_CENTER alignment and
963 // BS_BITMAP style), at least under Windows 2003 so use owner drawn
964 // strategy for bitmap-only buttons
965 if ( ShowsLabel() && wxUxThemeEngine::GetIfActive() )
966 {
967 m_imageData = new wxXPButtonImageData(this, bitmap);
968 }
969 else
970 #endif // wxUSE_UXTHEME
971 {
972 m_imageData = new wxODButtonImageData(this, bitmap);
973 MakeOwnerDrawn();
974 }
975 }
976 else
977 {
978 m_imageData->SetBitmap(bitmap, which);
979 }
980
981 // it should be enough to only invalidate the best size when the normal
982 // bitmap changes as all bitmaps assigned to the button should be of the
983 // same size anyhow
984 if ( which == State_Normal )
985 InvalidateBestSize();
986
987 Refresh();
988 }
989
990 wxSize wxButton::DoGetBitmapMargins() const
991 {
992 return m_imageData ? m_imageData->GetBitmapMargins() : wxSize(0, 0);
993 }
994
995 void wxButton::DoSetBitmapMargins(wxCoord x, wxCoord y)
996 {
997 wxCHECK_RET( m_imageData, "SetBitmap() must be called first" );
998
999 m_imageData->SetBitmapMargins(x, y);
1000 }
1001
1002 void wxButton::DoSetBitmapPosition(wxDirection dir)
1003 {
1004 wxCHECK_RET( m_imageData, "SetBitmap() must be called first" );
1005
1006 m_imageData->SetBitmapPosition(dir);
1007 }
1008
1009 // ----------------------------------------------------------------------------
1010 // owner-drawn buttons support
1011 // ----------------------------------------------------------------------------
1012
1013 // drawing helpers
1014 namespace
1015 {
1016
1017 // return the button state using both the ODS_XXX flags specified in state
1018 // parameter and the current button state
1019 wxButton::State GetButtonState(wxButton *btn, UINT state)
1020 {
1021 if ( state & ODS_DISABLED )
1022 return wxButton::State_Disabled;
1023
1024 if ( state & ODS_SELECTED )
1025 return wxButton::State_Pressed;
1026
1027 if ( btn->HasCapture() || btn->IsMouseInWindow() )
1028 return wxButton::State_Current;
1029
1030 if ( state & ODS_FOCUS )
1031 return wxButton::State_Focused;
1032
1033 return wxButton::State_Normal;
1034 }
1035
1036 void DrawButtonText(HDC hdc,
1037 RECT *pRect,
1038 const wxString& text,
1039 COLORREF col,
1040 int flags)
1041 {
1042 wxTextColoursChanger changeFg(hdc, col, CLR_INVALID);
1043 wxBkModeChanger changeBkMode(hdc, wxBRUSHSTYLE_TRANSPARENT);
1044
1045 // center text horizontally in any case
1046 flags |= DT_CENTER;
1047
1048 if ( text.find(wxT('\n')) != wxString::npos )
1049 {
1050 // draw multiline label
1051
1052 // first we need to compute its bounding rect
1053 RECT rc;
1054 ::CopyRect(&rc, pRect);
1055 ::DrawText(hdc, text.wx_str(), text.length(), &rc,
1056 DT_CENTER | DT_CALCRECT);
1057
1058 // now center this rect inside the entire button area
1059 const LONG w = rc.right - rc.left;
1060 const LONG h = rc.bottom - rc.top;
1061 rc.left = (pRect->right - pRect->left)/2 - w/2;
1062 rc.right = rc.left+w;
1063 rc.top = (pRect->bottom - pRect->top)/2 - h/2;
1064 rc.bottom = rc.top+h;
1065
1066 ::DrawText(hdc, text.wx_str(), text.length(), &rc, flags);
1067 }
1068 else // single line label
1069 {
1070 // centre text vertically too (notice that we must have DT_SINGLELINE
1071 // for DT_VCENTER to work)
1072 ::DrawText(hdc, text.wx_str(), text.length(), pRect,
1073 flags | DT_SINGLELINE | DT_VCENTER);
1074 }
1075 }
1076
1077 void DrawRect(HDC hdc, const RECT& r)
1078 {
1079 wxDrawLine(hdc, r.left, r.top, r.right, r.top);
1080 wxDrawLine(hdc, r.right, r.top, r.right, r.bottom);
1081 wxDrawLine(hdc, r.right, r.bottom, r.left, r.bottom);
1082 wxDrawLine(hdc, r.left, r.bottom, r.left, r.top);
1083 }
1084
1085 /*
1086 The button frame looks like this normally:
1087
1088 WWWWWWWWWWWWWWWWWWB
1089 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
1090 WH GB H = light grey (LIGHT)
1091 WH GB G = dark grey (SHADOW)
1092 WH GB B = black (DKSHADOW)
1093 WH GB
1094 WGGGGGGGGGGGGGGGGGB
1095 BBBBBBBBBBBBBBBBBBB
1096
1097 When the button is selected, the button becomes like this (the total button
1098 size doesn't change):
1099
1100 BBBBBBBBBBBBBBBBBBB
1101 BWWWWWWWWWWWWWWWWBB
1102 BWHHHHHHHHHHHHHHGBB
1103 BWH GBB
1104 BWH GBB
1105 BWGGGGGGGGGGGGGGGBB
1106 BBBBBBBBBBBBBBBBBBB
1107 BBBBBBBBBBBBBBBBBBB
1108
1109 When the button is pushed (while selected) it is like:
1110
1111 BBBBBBBBBBBBBBBBBBB
1112 BGGGGGGGGGGGGGGGGGB
1113 BG GB
1114 BG GB
1115 BG GB
1116 BG GB
1117 BGGGGGGGGGGGGGGGGGB
1118 BBBBBBBBBBBBBBBBBBB
1119 */
1120 void DrawButtonFrame(HDC hdc, RECT& rectBtn,
1121 bool selected, bool pushed)
1122 {
1123 RECT r;
1124 CopyRect(&r, &rectBtn);
1125
1126 AutoHPEN hpenBlack(GetSysColor(COLOR_3DDKSHADOW)),
1127 hpenGrey(GetSysColor(COLOR_3DSHADOW)),
1128 hpenLightGr(GetSysColor(COLOR_3DLIGHT)),
1129 hpenWhite(GetSysColor(COLOR_3DHILIGHT));
1130
1131 SelectInHDC selectPen(hdc, hpenBlack);
1132
1133 r.right--;
1134 r.bottom--;
1135
1136 if ( pushed )
1137 {
1138 DrawRect(hdc, r);
1139
1140 (void)SelectObject(hdc, hpenGrey);
1141 ::InflateRect(&r, -1, -1);
1142
1143 DrawRect(hdc, r);
1144 }
1145 else // !pushed
1146 {
1147 if ( selected )
1148 {
1149 DrawRect(hdc, r);
1150
1151 ::InflateRect(&r, -1, -1);
1152 }
1153
1154 wxDrawLine(hdc, r.left, r.bottom, r.right, r.bottom);
1155 wxDrawLine(hdc, r.right, r.bottom, r.right, r.top - 1);
1156
1157 (void)SelectObject(hdc, hpenWhite);
1158 wxDrawLine(hdc, r.left, r.bottom - 1, r.left, r.top);
1159 wxDrawLine(hdc, r.left, r.top, r.right, r.top);
1160
1161 (void)SelectObject(hdc, hpenLightGr);
1162 wxDrawLine(hdc, r.left + 1, r.bottom - 2, r.left + 1, r.top + 1);
1163 wxDrawLine(hdc, r.left + 1, r.top + 1, r.right - 1, r.top + 1);
1164
1165 (void)SelectObject(hdc, hpenGrey);
1166 wxDrawLine(hdc, r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1);
1167 wxDrawLine(hdc, r.right - 1, r.bottom - 1, r.right - 1, r.top);
1168 }
1169
1170 InflateRect(&rectBtn, -OD_BUTTON_MARGIN, -OD_BUTTON_MARGIN);
1171 }
1172
1173 #if wxUSE_UXTHEME
1174 void DrawXPBackground(wxButton *button, HDC hdc, RECT& rectBtn, UINT state)
1175 {
1176 wxUxThemeHandle theme(button, L"BUTTON");
1177
1178 // this array is indexed by wxButton::State values and so must be kept in
1179 // sync with it
1180 static const int uxStates[] =
1181 {
1182 PBS_NORMAL, PBS_HOT, PBS_PRESSED, PBS_DISABLED, PBS_DEFAULTED
1183 };
1184
1185 int iState = uxStates[GetButtonState(button, state)];
1186
1187 wxUxThemeEngine * const engine = wxUxThemeEngine::Get();
1188
1189 // draw parent background if needed
1190 if ( engine->IsThemeBackgroundPartiallyTransparent
1191 (
1192 theme,
1193 BP_PUSHBUTTON,
1194 iState
1195 ) )
1196 {
1197 engine->DrawThemeParentBackground(GetHwndOf(button), hdc, &rectBtn);
1198 }
1199
1200 // draw background
1201 engine->DrawThemeBackground(theme, hdc, BP_PUSHBUTTON, iState,
1202 &rectBtn, NULL);
1203
1204 // calculate content area margins
1205 MARGINS margins;
1206 engine->GetThemeMargins(theme, hdc, BP_PUSHBUTTON, iState,
1207 TMT_CONTENTMARGINS, &rectBtn, &margins);
1208 ::InflateRect(&rectBtn, -margins.cxLeftWidth, -margins.cyTopHeight);
1209 ::InflateRect(&rectBtn, -XP_BUTTON_EXTRA_MARGIN, -XP_BUTTON_EXTRA_MARGIN);
1210
1211 if ( button->UseBgCol() )
1212 {
1213 COLORREF colBg = wxColourToRGB(button->GetBackgroundColour());
1214 AutoHBRUSH hbrushBackground(colBg);
1215
1216 // don't overwrite the focus rect
1217 RECT rectClient;
1218 ::CopyRect(&rectClient, &rectBtn);
1219 ::InflateRect(&rectClient, -1, -1);
1220 FillRect(hdc, &rectClient, hbrushBackground);
1221 }
1222 }
1223 #endif // wxUSE_UXTHEME
1224
1225 } // anonymous namespace
1226
1227 // ----------------------------------------------------------------------------
1228 // owner drawn buttons support
1229 // ----------------------------------------------------------------------------
1230
1231 void wxButton::MakeOwnerDrawn()
1232 {
1233 long style = GetWindowLong(GetHwnd(), GWL_STYLE);
1234 if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
1235 {
1236 // make it so
1237 style |= BS_OWNERDRAW;
1238 SetWindowLong(GetHwnd(), GWL_STYLE, style);
1239 }
1240 }
1241
1242 bool wxButton::SetBackgroundColour(const wxColour &colour)
1243 {
1244 if ( !wxControl::SetBackgroundColour(colour) )
1245 {
1246 // nothing to do
1247 return false;
1248 }
1249
1250 MakeOwnerDrawn();
1251
1252 Refresh();
1253
1254 return true;
1255 }
1256
1257 bool wxButton::SetForegroundColour(const wxColour &colour)
1258 {
1259 if ( !wxControl::SetForegroundColour(colour) )
1260 {
1261 // nothing to do
1262 return false;
1263 }
1264
1265 MakeOwnerDrawn();
1266
1267 Refresh();
1268
1269 return true;
1270 }
1271
1272 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT *wxdis)
1273 {
1274 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)wxdis;
1275 HDC hdc = lpDIS->hDC;
1276
1277 UINT state = lpDIS->itemState;
1278 bool pushed = (SendMessage(GetHwnd(), BM_GETSTATE, 0, 0) & BST_PUSHED) != 0;
1279
1280 RECT rectBtn;
1281 CopyRect(&rectBtn, &lpDIS->rcItem);
1282
1283 // draw the button background
1284 if ( !HasFlag(wxBORDER_NONE) )
1285 {
1286 #if wxUSE_UXTHEME
1287 if ( wxUxThemeEngine::GetIfActive() )
1288 {
1289 DrawXPBackground(this, hdc, rectBtn, state);
1290 }
1291 else
1292 #endif // wxUSE_UXTHEME
1293 {
1294 COLORREF colBg = wxColourToRGB(GetBackgroundColour());
1295
1296 // first, draw the background
1297 AutoHBRUSH hbrushBackground(colBg);
1298 FillRect(hdc, &rectBtn, hbrushBackground);
1299
1300 // draw the border for the current state
1301 bool selected = (state & ODS_SELECTED) != 0;
1302 if ( !selected )
1303 {
1304 wxTopLevelWindow *
1305 tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
1306 if ( tlw )
1307 {
1308 selected = tlw->GetDefaultItem() == this;
1309 }
1310 }
1311
1312 DrawButtonFrame(hdc, rectBtn, selected, pushed);
1313 }
1314
1315 // draw the focus rectangle if we need it
1316 if ( (state & ODS_FOCUS) && !(state & ODS_NOFOCUSRECT) )
1317 {
1318 DrawFocusRect(hdc, &rectBtn);
1319
1320 #if wxUSE_UXTHEME
1321 if ( !wxUxThemeEngine::GetIfActive() )
1322 #endif // wxUSE_UXTHEME
1323 {
1324 if ( pushed )
1325 {
1326 // the label is shifted by 1 pixel to create "pushed" effect
1327 OffsetRect(&rectBtn, 1, 1);
1328 }
1329 }
1330 }
1331 }
1332
1333
1334 // draw the image, if any
1335 if ( m_imageData )
1336 {
1337 wxBitmap bmp = m_imageData->GetBitmap(GetButtonState(this, state));
1338 if ( !bmp.IsOk() )
1339 bmp = m_imageData->GetBitmap(State_Normal);
1340
1341 const wxSize sizeBmp = bmp.GetSize();
1342 const wxSize margin = m_imageData->GetBitmapMargins();
1343 const wxSize sizeBmpWithMargins(sizeBmp + 2*margin);
1344 wxRect rectButton(wxRectFromRECT(rectBtn));
1345
1346 // for simplicity, we start with centred rectangle and then move it to
1347 // the appropriate edge
1348 wxRect rectBitmap = wxRect(sizeBmp).CentreIn(rectButton);
1349 switch ( m_imageData->GetBitmapPosition() )
1350 {
1351 default:
1352 wxFAIL_MSG( "invalid direction" );
1353 // fall through
1354
1355 case wxLEFT:
1356 rectBitmap.x = rectButton.x + margin.x;
1357 rectButton.x += sizeBmpWithMargins.x;
1358 rectButton.width -= sizeBmpWithMargins.x;
1359 break;
1360
1361 case wxRIGHT:
1362 rectBitmap.x = rectButton.GetRight() - sizeBmp.x - margin.x;
1363 rectButton.width -= sizeBmpWithMargins.x;
1364 break;
1365
1366 case wxTOP:
1367 rectBitmap.y = rectButton.y + margin.y;
1368 rectButton.y += sizeBmpWithMargins.y;
1369 rectButton.height -= sizeBmpWithMargins.y;
1370 break;
1371
1372 case wxBOTTOM:
1373 rectBitmap.y = rectButton.GetBottom() - sizeBmp.y - margin.y;
1374 rectButton.height -= sizeBmpWithMargins.y;
1375 break;
1376 }
1377
1378 wxDCTemp dst((WXHDC)hdc);
1379 dst.DrawBitmap(bmp, rectBitmap.GetPosition(), true);
1380
1381 wxCopyRectToRECT(rectButton, rectBtn);
1382 }
1383
1384
1385 // finally draw the label
1386 if ( ShowsLabel() )
1387 {
1388 COLORREF colFg = state & ODS_DISABLED
1389 ? ::GetSysColor(COLOR_GRAYTEXT)
1390 : wxColourToRGB(GetForegroundColour());
1391
1392 // notice that DT_HIDEPREFIX doesn't work on old (pre-Windows 2000)
1393 // systems but by happy coincidence ODS_NOACCEL is not used under them
1394 // neither so DT_HIDEPREFIX should never be used there
1395 DrawButtonText(hdc, &rectBtn, GetLabel(), colFg,
1396 state & ODS_NOACCEL ? DT_HIDEPREFIX : 0);
1397 }
1398
1399 return true;
1400 }
1401
1402 #endif // wxUSE_BUTTON
1403