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 // the base unit is the height of the system GUI font
141 wxGetCharSize(0, &wChar
, &hChar
, NULL
);
143 // the button height is proportional to the height of the font used
144 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
146 // and the width/height ration is 75/23
147 return wxSize((75 * hBtn
) / 23, hBtn
);
150 // ----------------------------------------------------------------------------
151 // set this button as the default one in its panel
152 // ----------------------------------------------------------------------------
154 void wxButton::SetDefault()
156 wxWindow
*parent
= GetParent();
157 wxButton
*btnOldDefault
= NULL
;
158 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
161 btnOldDefault
= panel
->GetDefaultItem();
162 panel
->SetDefaultItem(this);
167 SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
170 // this doesn't work with bitmap buttons because it also removes the
171 // "ownerdrawn" style...
172 if ( btnOldDefault
&& !wxDynamicCast(btnOldDefault
, wxBitmapButton
) )
174 // remove the BS_DEFPUSHBUTTON style from the other button
175 long style
= GetWindowLong(GetHwndOf(btnOldDefault
), GWL_STYLE
);
176 style
&= ~BS_DEFPUSHBUTTON
;
177 SendMessage(GetHwndOf(btnOldDefault
), BM_SETSTYLE
, style
, 1L);
180 // set this button as the default
181 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
182 style
|= BS_DEFPUSHBUTTON
;
183 SendMessage(GetHwnd(), BM_SETSTYLE
, style
, 1L);
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 bool wxButton::SendClickEvent()
192 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
193 event
.SetEventObject(this);
195 return ProcessCommand(event
);
198 void wxButton::Command(wxCommandEvent
& event
)
200 ProcessCommand(event
);
203 // ----------------------------------------------------------------------------
204 // event/message handlers
205 // ----------------------------------------------------------------------------
207 bool wxButton::MSWCommand(WXUINT param
, WXWORD id
)
209 bool processed
= FALSE
;
212 case 1: // 1 for accelerator
214 processed
= SendClickEvent();
221 WXHBRUSH
wxButton::OnCtlColor(WXHDC pDC
,
228 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
230 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();