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"
35 #include "wx/settings.h"
36 #include "wx/dcscreen.h"
39 #include "wx/msw/private.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 #if !USE_SHARED_LIBRARY
46 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
49 // this macro tries to adjust the default button height to a reasonable value
50 // using the char height as the base
51 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
53 // ============================================================================
55 // ============================================================================
57 // ----------------------------------------------------------------------------
58 // creation/destruction
59 // ----------------------------------------------------------------------------
61 bool wxButton::Create(wxWindow
*parent
,
63 const wxString
& label
,
67 const wxValidator
& validator
,
70 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
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
);
98 // bad hack added by Robert to make buttons at least
99 // 80 pixels wide. There are probably better ways...
101 wxSize
nsize( GetSize() );
102 if ((nsize
.x
< 80) || (nsize
.y
< 23))
104 if ((size
.x
== -1) && (nsize
.x
< 80))
106 if ((size
.y
== -1) && (nsize
.y
< 23))
114 wxButton::~wxButton()
116 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
119 if ( panel
->GetDefaultItem() == this )
121 // don't leave the panel with invalid default item
122 panel
->SetDefaultItem(NULL
);
127 // ----------------------------------------------------------------------------
128 // size management including autosizing
129 // ----------------------------------------------------------------------------
131 wxSize
wxButton::DoGetBestSize() const
133 wxString label
= wxGetWindowText(GetHWND());
135 GetTextExtent(label
, &wBtn
, NULL
);
138 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
140 // add a margin - the button is wider than just its label
143 // the button height is proportional to the height of the font used
144 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
146 return wxSize(wBtn
, hBtn
);
150 wxSize
wxButton::GetDefaultSize()
152 static wxSize s_sizeBtn
;
154 if ( s_sizeBtn
.x
== 0 )
157 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
159 // the size of a standard button in the dialog units is 50x14,
160 // translate this to pixels
161 // NB1: the multipliers come from the Windows convention
162 // NB2: the extra +1/+2 were needed to get the size be the same as the
163 // size of the buttons in the standard dialog - I don't know how
164 // this happens, but on my system this size is 75x23 in pixels and
165 // 23*8 isn't even divisible by 14... Would be nice to understand
166 // why these constants are needed though!
167 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
168 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
174 // ----------------------------------------------------------------------------
175 // set this button as the default one in its panel
176 // ----------------------------------------------------------------------------
178 void wxButton::SetDefault()
180 wxWindow
*parent
= GetParent();
181 wxButton
*btnOldDefault
= NULL
;
182 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
185 btnOldDefault
= panel
->GetDefaultItem();
186 panel
->SetDefaultItem(this);
191 SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
194 // this doesn't work with bitmap buttons because it also removes the
195 // "ownerdrawn" style...
196 if ( btnOldDefault
&& !wxDynamicCast(btnOldDefault
, wxBitmapButton
) )
198 // remove the BS_DEFPUSHBUTTON style from the other button
199 long style
= GetWindowLong(GetHwndOf(btnOldDefault
), GWL_STYLE
);
200 style
&= ~BS_DEFPUSHBUTTON
;
201 SendMessage(GetHwndOf(btnOldDefault
), BM_SETSTYLE
, style
, 1L);
204 // set this button as the default
205 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
206 style
|= BS_DEFPUSHBUTTON
;
207 SendMessage(GetHwnd(), BM_SETSTYLE
, style
, 1L);
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
214 bool wxButton::SendClickEvent()
216 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
217 event
.SetEventObject(this);
219 return ProcessCommand(event
);
222 void wxButton::Command(wxCommandEvent
& event
)
224 ProcessCommand(event
);
227 // ----------------------------------------------------------------------------
228 // event/message handlers
229 // ----------------------------------------------------------------------------
231 bool wxButton::MSWCommand(WXUINT param
, WXWORD id
)
233 bool processed
= FALSE
;
236 case 1: // means that the message came from an accelerator
238 processed
= SendClickEvent();
245 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
247 // make sure that we won't have BS_DEFPUSHBUTTON style any more if the
248 // focus is being transfered to another button with the same parent -
249 // otherwise, we could finish with 2 default buttons inside one panel
250 if ( (nMsg
== WM_KILLFOCUS
) &&
251 (GetWindowLong(GetHwnd(), GWL_STYLE
) & BS_DEFPUSHBUTTON
) )
253 wxWindow
*parent
= GetParent();
254 wxWindow
*win
= wxFindWinFromHandle((WXHWND
)wParam
);
255 if ( win
&& win
->GetParent() == parent
)
257 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
260 panel
->SetDefaultItem(this);
262 // else: I don't know what to do - we'll still have the problem
263 // with multiple default buttons in a dialog...
267 // let the base class do all real processing
268 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);