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"
36 #include "wx/msw/private.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 #if !USE_SHARED_LIBRARY
43 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
46 // this macro tries to adjust the default button height to a reasonable value
47 // using the char height as the base
48 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
50 // ============================================================================
52 // ============================================================================
54 // ----------------------------------------------------------------------------
55 // creation/destruction
56 // ----------------------------------------------------------------------------
58 bool wxButton::Create(wxWindow
*parent
,
60 const wxString
& label
,
64 const wxValidator
& validator
,
67 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
70 SetValidator(validator
);
72 parent
->AddChild((wxButton
*)this);
74 m_backgroundColour
= parent
->GetBackgroundColour() ;
75 m_foregroundColour
= parent
->GetForegroundColour() ;
77 m_hWnd
= (WXHWND
)CreateWindowEx
79 MakeExtendedStyle(m_windowStyle
),
82 WS_VISIBLE
| WS_TABSTOP
| WS_CHILD
,
90 // Subclass again for purposes of dialog editing mode
93 SetFont(parent
->GetFont());
95 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
100 wxButton::~wxButton()
102 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
105 if ( panel
->GetDefaultItem() == this )
107 // don't leave the panel with invalid default item
108 panel
->SetDefaultItem(NULL
);
113 // ----------------------------------------------------------------------------
114 // size management including autosizing
115 // ----------------------------------------------------------------------------
117 wxSize
wxButton::DoGetBestSize()
119 wxString label
= wxGetWindowText(GetHWND());
121 GetTextExtent(label
, &wBtn
, NULL
);
124 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
126 // add a margin - the button is wider than just its label
129 // the button height is proportional to the height of the font used
130 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
132 return wxSize(wBtn
, hBtn
);
136 wxSize
wxButton::GetDefaultSize()
138 // the base unit is the height of the system GUI font
140 wxGetCharSize(0, &wChar
, &hChar
, NULL
);
142 // the button height is proportional to the height of the font used
143 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
145 // and the width/height ration is 75/23
146 return wxSize((75 * hBtn
) / 23, hBtn
);
149 // ----------------------------------------------------------------------------
150 // set this button as the default one in its panel
151 // ----------------------------------------------------------------------------
153 void wxButton::SetDefault()
155 wxWindow
*parent
= GetParent();
156 wxButton
*btnOldDefault
= NULL
;
157 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
160 btnOldDefault
= panel
->GetDefaultItem();
161 panel
->SetDefaultItem(this);
166 SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
169 // this doesn't work with bitmap buttons because it also removes the
170 // "ownerdrawn" style...
171 if ( btnOldDefault
&& !wxDynamicCast(btnOldDefault
, wxBitmapButton
) )
173 // remove the BS_DEFPUSHBUTTON style from the other button
174 long style
= GetWindowLong(GetHwndOf(btnOldDefault
), GWL_STYLE
);
175 style
&= ~BS_DEFPUSHBUTTON
;
176 SendMessage(GetHwndOf(btnOldDefault
), BM_SETSTYLE
, style
, 1L);
179 // set this button as the default
180 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
181 style
|= BS_DEFPUSHBUTTON
;
182 SendMessage(GetHwnd(), BM_SETSTYLE
, style
, 1L);
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 bool wxButton::SendClickEvent()
191 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
192 event
.SetEventObject(this);
194 return ProcessCommand(event
);
197 void wxButton::Command(wxCommandEvent
& event
)
199 ProcessCommand(event
);
202 // ----------------------------------------------------------------------------
203 // event/message handlers
204 // ----------------------------------------------------------------------------
206 bool wxButton::MSWCommand(WXUINT param
, WXWORD id
)
208 bool processed
= FALSE
;
211 case 1: // 1 for accelerator
213 processed
= SendClickEvent();
220 WXHBRUSH
wxButton::OnCtlColor(WXHDC pDC
,
227 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
229 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();