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