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 SetValidator(validator
);
75 parent
->AddChild((wxButton
*)this);
77 m_backgroundColour
= parent
->GetBackgroundColour() ;
78 m_foregroundColour
= parent
->GetForegroundColour() ;
80 m_hWnd
= (WXHWND
)CreateWindowEx
82 MakeExtendedStyle(m_windowStyle
),
85 WS_VISIBLE
| WS_TABSTOP
| WS_CHILD
,
93 // Subclass again for purposes of dialog editing mode
96 SetFont(parent
->GetFont());
98 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
103 wxButton::~wxButton()
105 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
108 if ( panel
->GetDefaultItem() == this )
110 // don't leave the panel with invalid default item
111 panel
->SetDefaultItem(NULL
);
116 // ----------------------------------------------------------------------------
117 // size management including autosizing
118 // ----------------------------------------------------------------------------
120 wxSize
wxButton::DoGetBestSize()
122 wxString label
= wxGetWindowText(GetHWND());
124 GetTextExtent(label
, &wBtn
, NULL
);
127 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
129 // add a margin - the button is wider than just its label
132 // the button height is proportional to the height of the font used
133 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
135 return wxSize(wBtn
, hBtn
);
139 wxSize
wxButton::GetDefaultSize()
141 static wxSize s_sizeBtn
;
143 if ( s_sizeBtn
.x
== 0 )
146 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
148 // the size of a standard button in the dialog units is 50x14,
149 // translate this to pixels
150 // NB1: the multipliers come from the Windows convention
151 // NB2: the extra +1/+2 were needed to get the size be the same as the
152 // size of the buttons in the standard dialog - I don't know how
153 // this happens, but on my system this size is 75x23 in pixels and
154 // 23*8 isn't even divisible by 14... Would be nice to understand
155 // why these constants are needed though!
156 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
157 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
163 // ----------------------------------------------------------------------------
164 // set this button as the default one in its panel
165 // ----------------------------------------------------------------------------
167 void wxButton::SetDefault()
169 wxWindow
*parent
= GetParent();
170 wxButton
*btnOldDefault
= NULL
;
171 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
174 btnOldDefault
= panel
->GetDefaultItem();
175 panel
->SetDefaultItem(this);
180 SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
183 // this doesn't work with bitmap buttons because it also removes the
184 // "ownerdrawn" style...
185 if ( btnOldDefault
&& !wxDynamicCast(btnOldDefault
, wxBitmapButton
) )
187 // remove the BS_DEFPUSHBUTTON style from the other button
188 long style
= GetWindowLong(GetHwndOf(btnOldDefault
), GWL_STYLE
);
189 style
&= ~BS_DEFPUSHBUTTON
;
190 SendMessage(GetHwndOf(btnOldDefault
), BM_SETSTYLE
, style
, 1L);
193 // set this button as the default
194 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
195 style
|= BS_DEFPUSHBUTTON
;
196 SendMessage(GetHwnd(), BM_SETSTYLE
, style
, 1L);
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
203 bool wxButton::SendClickEvent()
205 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
206 event
.SetEventObject(this);
208 return ProcessCommand(event
);
211 void wxButton::Command(wxCommandEvent
& event
)
213 ProcessCommand(event
);
216 // ----------------------------------------------------------------------------
217 // event/message handlers
218 // ----------------------------------------------------------------------------
220 bool wxButton::MSWCommand(WXUINT param
, WXWORD id
)
222 bool processed
= FALSE
;
225 case 1: // 1 for accelerator
227 processed
= SendClickEvent();
234 WXHBRUSH
wxButton::OnCtlColor(WXHDC pDC
,
241 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
243 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();