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 // Validator was set in CreateBase
74 //SetValidator(validator);
76 parent->AddChild((wxButton *)this);
78 m_backgroundColour = parent->GetBackgroundColour() ;
79 m_foregroundColour = parent->GetForegroundColour() ;
81 m_hWnd = (WXHWND)CreateWindowEx
83 MakeExtendedStyle(m_windowStyle),
86 WS_VISIBLE | WS_TABSTOP | WS_CHILD,
94 // Subclass again for purposes of dialog editing mode
97 SetFont(parent->GetFont());
99 SetSize(pos.x, pos.y, size.x, size.y);
101 // bad hack added by Robert to make buttons at least
102 // 80 pixels wide. There are probably better ways...
104 wxSize nsize( GetSize() );
105 if ((nsize.x < 80) || (nsize.y < 23))
107 if ((size.x == -1) && (nsize.x < 80)) nsize.x = 80;
108 if ((size.y == -1) && (nsize.y < 23)) nsize.y = 23;
115 wxButton::~wxButton()
117 wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
120 if ( panel->GetDefaultItem() == this )
122 // don't leave the panel with invalid default item
123 panel->SetDefaultItem(NULL);
128 // ----------------------------------------------------------------------------
129 // size management including autosizing
130 // ----------------------------------------------------------------------------
132 wxSize wxButton::DoGetBestSize()
134 wxString label = wxGetWindowText(GetHWND());
136 GetTextExtent(label, &wBtn, NULL);
139 wxGetCharSize(GetHWND(), &wChar, &hChar, &GetFont());
141 // add a margin - the button is wider than just its label
144 // the button height is proportional to the height of the font used
145 int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
147 return wxSize(wBtn, hBtn);
151 wxSize wxButton::GetDefaultSize()
153 static wxSize s_sizeBtn;
155 if ( s_sizeBtn.x == 0 )
158 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
160 // the size of a standard button in the dialog units is 50x14,
161 // translate this to pixels
162 // NB1: the multipliers come from the Windows convention
163 // NB2: the extra +1/+2 were needed to get the size be the same as the
164 // size of the buttons in the standard dialog - I don't know how
165 // this happens, but on my system this size is 75x23 in pixels and
166 // 23*8 isn't even divisible by 14... Would be nice to understand
167 // why these constants are needed though!
168 s_sizeBtn.x = (50 * (dc.GetCharWidth() + 1))/4;
169 s_sizeBtn.y = ((14 * dc.GetCharHeight()) + 2)/8;
175 // ----------------------------------------------------------------------------
176 // set this button as the default one in its panel
177 // ----------------------------------------------------------------------------
179 void wxButton::SetDefault()
181 wxWindow *parent = GetParent();
182 wxButton *btnOldDefault = NULL;
183 wxPanel *panel = wxDynamicCast(parent, wxPanel);
186 btnOldDefault = panel->GetDefaultItem();
187 panel->SetDefaultItem(this);
192 SendMessage(GetWinHwnd(parent), DM_SETDEFID, m_windowId, 0L);
195 // this doesn't work with bitmap buttons because it also removes the
196 // "ownerdrawn" style...
197 if ( btnOldDefault && !wxDynamicCast(btnOldDefault, wxBitmapButton) )
199 // remove the BS_DEFPUSHBUTTON style from the other button
200 long style = GetWindowLong(GetHwndOf(btnOldDefault), GWL_STYLE);
201 style &= ~BS_DEFPUSHBUTTON;
202 SendMessage(GetHwndOf(btnOldDefault), BM_SETSTYLE, style, 1L);
205 // set this button as the default
206 long style = GetWindowLong(GetHwnd(), GWL_STYLE);
207 style |= BS_DEFPUSHBUTTON;
208 SendMessage(GetHwnd(), BM_SETSTYLE, style, 1L);
211 // ----------------------------------------------------------------------------
213 // ----------------------------------------------------------------------------
215 bool wxButton::SendClickEvent()
217 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
218 event.SetEventObject(this);
220 return ProcessCommand(event);
223 void wxButton::Command(wxCommandEvent & event)
225 ProcessCommand(event);
228 // ----------------------------------------------------------------------------
229 // event/message handlers
230 // ----------------------------------------------------------------------------
232 bool wxButton::MSWCommand(WXUINT param, WXWORD id)
234 bool processed = FALSE;
237 case 1: // 1 for accelerator
239 processed = SendClickEvent();
246 WXHBRUSH wxButton::OnCtlColor(WXHDC pDC,
253 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
255 return (WXHBRUSH) backgroundBrush->GetResourceHandle();