1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "button.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
31 #include "wx/button.h"
34 #include "wx/bmpbuttn.h"
37 #include "wx/msw/private.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 #if !USE_SHARED_LIBRARY
44 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
47 // this macro tries to adjust the default button height to a reasonable value
48 // using the char height as the base
49 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
51 // ============================================================================
53 // ============================================================================
55 // ----------------------------------------------------------------------------
56 // creation/destruction
57 // ----------------------------------------------------------------------------
59 bool wxButton::Create(wxWindow
*parent
,
61 const wxString
& label
,
65 const wxValidator
& validator
,
68 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
71 SetValidator(validator
);
73 parent
->AddChild((wxButton
*)this);
75 m_backgroundColour
= parent
->GetBackgroundColour() ;
76 m_foregroundColour
= parent
->GetForegroundColour() ;
78 m_hWnd
= (WXHWND
)CreateWindowEx
80 MakeExtendedStyle(m_windowStyle
),
83 WS_VISIBLE
| WS_TABSTOP
| WS_CHILD
,
91 // Subclass again for purposes of dialog editing mode
94 SetFont(parent
->GetFont());
96 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
101 wxButton::~wxButton()
103 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
106 if ( panel
->GetDefaultItem() == this )
108 // don't leave the panel with invalid default item
109 panel
->SetDefaultItem(NULL
);
114 // ----------------------------------------------------------------------------
115 // size management including autosizing
116 // ----------------------------------------------------------------------------
118 wxSize
wxButton::DoGetBestSize()
120 wxString label
= wxGetWindowText(GetHWND());
122 GetTextExtent(label
, &wBtn
, NULL
);
125 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
127 // add a margin - the button is wider than just its label
130 // the button height is proportional to the height of the font used
131 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
133 return wxSize(wBtn
, hBtn
);
137 wxSize
wxButton::GetDefaultSize()
139 static wxSize s_sizeBtn
;
141 if ( s_sizeBtn
.x
== 0 )
144 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
146 // the size of a standard button in the dialog units is 50x14,
147 // translate this to pixels
148 // NB1: the multipliers come from the Windows convention
149 // NB2: the extra +1/+2 were needed to get the size be the same as the
150 // size of the buttons in the standard dialog - I don't know how
151 // this happens, but on my system this size is 75x23 in pixels and
152 // 23*8 isn't even divisible by 14... Would be nice to understand
153 // why these constants are needed though!
154 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
155 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
161 // ----------------------------------------------------------------------------
162 // set this button as the default one in its panel
163 // ----------------------------------------------------------------------------
165 void wxButton::SetDefault()
167 wxWindow
*parent
= GetParent();
168 wxButton
*btnOldDefault
= NULL
;
169 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
172 btnOldDefault
= panel
->GetDefaultItem();
173 panel
->SetDefaultItem(this);
178 SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
181 // this doesn't work with bitmap buttons because it also removes the
182 // "ownerdrawn" style...
183 if ( btnOldDefault
&& !wxDynamicCast(btnOldDefault
, wxBitmapButton
) )
185 // remove the BS_DEFPUSHBUTTON style from the other button
186 long style
= GetWindowLong(GetHwndOf(btnOldDefault
), GWL_STYLE
);
187 style
&= ~BS_DEFPUSHBUTTON
;
188 SendMessage(GetHwndOf(btnOldDefault
), BM_SETSTYLE
, style
, 1L);
191 // set this button as the default
192 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
193 style
|= BS_DEFPUSHBUTTON
;
194 SendMessage(GetHwnd(), BM_SETSTYLE
, style
, 1L);
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
201 bool wxButton::SendClickEvent()
203 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
204 event
.SetEventObject(this);
206 return ProcessCommand(event
);
209 void wxButton::Command(wxCommandEvent
& event
)
211 ProcessCommand(event
);
214 // ----------------------------------------------------------------------------
215 // event/message handlers
216 // ----------------------------------------------------------------------------
218 bool wxButton::MSWCommand(WXUINT param
, WXWORD id
)
220 bool processed
= FALSE
;
223 case 1: // 1 for accelerator
225 processed
= SendClickEvent();
232 WXHBRUSH
wxButton::OnCtlColor(WXHDC pDC
,
239 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
241 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();