]> git.saurik.com Git - wxWidgets.git/blob - src/msw/button.cpp
wxButton::GetDefaultSize() fix
[wxWidgets.git] / src / msw / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: button.cpp
3 // Purpose: wxButton
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19 #ifdef __GNUG__
20 #pragma implementation "button.h"
21 #endif
22
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 #ifndef WX_PRECOMP
31 #include "wx/button.h"
32 #include "wx/brush.h"
33 #include "wx/panel.h"
34 #include "wx/bmpbuttn.h"
35 #endif
36
37 #include "wx/msw/private.h"
38
39 // ----------------------------------------------------------------------------
40 // macros
41 // ----------------------------------------------------------------------------
42
43 #if !USE_SHARED_LIBRARY
44 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
45 #endif
46
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)
50
51 // ============================================================================
52 // implementation
53 // ============================================================================
54
55 // ----------------------------------------------------------------------------
56 // creation/destruction
57 // ----------------------------------------------------------------------------
58
59 bool wxButton::Create(wxWindow *parent,
60 wxWindowID id,
61 const wxString& label,
62 const wxPoint& pos,
63 const wxSize& size,
64 long style,
65 const wxValidator& validator,
66 const wxString& name)
67 {
68 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
69 return FALSE;
70
71 SetValidator(validator);
72
73 parent->AddChild((wxButton *)this);
74
75 m_backgroundColour = parent->GetBackgroundColour() ;
76 m_foregroundColour = parent->GetForegroundColour() ;
77
78 m_hWnd = (WXHWND)CreateWindowEx
79 (
80 MakeExtendedStyle(m_windowStyle),
81 _T("BUTTON"),
82 label,
83 WS_VISIBLE | WS_TABSTOP | WS_CHILD,
84 0, 0, 0, 0,
85 GetWinHwnd(parent),
86 (HMENU)m_windowId,
87 wxGetInstance(),
88 NULL
89 );
90
91 // Subclass again for purposes of dialog editing mode
92 SubclassWin(m_hWnd);
93
94 SetFont(parent->GetFont());
95
96 SetSize(pos.x, pos.y, size.x, size.y);
97
98 return TRUE;
99 }
100
101 wxButton::~wxButton()
102 {
103 wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
104 if ( panel )
105 {
106 if ( panel->GetDefaultItem() == this )
107 {
108 // don't leave the panel with invalid default item
109 panel->SetDefaultItem(NULL);
110 }
111 }
112 }
113
114 // ----------------------------------------------------------------------------
115 // size management including autosizing
116 // ----------------------------------------------------------------------------
117
118 wxSize wxButton::DoGetBestSize()
119 {
120 wxString label = wxGetWindowText(GetHWND());
121 int wBtn;
122 GetTextExtent(label, &wBtn, NULL);
123
124 int wChar, hChar;
125 wxGetCharSize(GetHWND(), &wChar, &hChar, &GetFont());
126
127 // add a margin - the button is wider than just its label
128 wBtn += 3*wChar;
129
130 // the button height is proportional to the height of the font used
131 int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
132
133 return wxSize(wBtn, hBtn);
134 }
135
136 /* static */
137 wxSize wxButton::GetDefaultSize()
138 {
139 static wxSize s_sizeBtn;
140
141 if ( s_sizeBtn.x == 0 )
142 {
143 wxScreenDC dc;
144 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
145
146 // the size of a standard button in the dialog units is 50x14,
147 // translate this to pixels
148 // NB1: the multipliers come from the Windows convention
149 // NB2: the extra +1/+2 were needed to get the size be the same as the
150 // size of the buttons in the standard dialog - I don't know how
151 // this happens, but on my system this size is 75x23 in pixels and
152 // 23*8 isn't even divisible by 14... Would be nice to understand
153 // why these constants are needed though!
154 s_sizeBtn.x = (50 * (dc.GetCharWidth() + 1))/4;
155 s_sizeBtn.y = ((14 * dc.GetCharHeight()) + 2)/8;
156 }
157
158 return s_sizeBtn;
159 }
160
161 // ----------------------------------------------------------------------------
162 // set this button as the default one in its panel
163 // ----------------------------------------------------------------------------
164
165 void wxButton::SetDefault()
166 {
167 wxWindow *parent = GetParent();
168 wxButton *btnOldDefault = NULL;
169 wxPanel *panel = wxDynamicCast(parent, wxPanel);
170 if ( panel )
171 {
172 btnOldDefault = panel->GetDefaultItem();
173 panel->SetDefaultItem(this);
174 }
175
176 if ( parent )
177 {
178 SendMessage(GetWinHwnd(parent), DM_SETDEFID, m_windowId, 0L);
179 }
180
181 // this doesn't work with bitmap buttons because it also removes the
182 // "ownerdrawn" style...
183 if ( btnOldDefault && !wxDynamicCast(btnOldDefault, wxBitmapButton) )
184 {
185 // remove the BS_DEFPUSHBUTTON style from the other button
186 long style = GetWindowLong(GetHwndOf(btnOldDefault), GWL_STYLE);
187 style &= ~BS_DEFPUSHBUTTON;
188 SendMessage(GetHwndOf(btnOldDefault), BM_SETSTYLE, style, 1L);
189 }
190
191 // set this button as the default
192 long style = GetWindowLong(GetHwnd(), GWL_STYLE);
193 style |= BS_DEFPUSHBUTTON;
194 SendMessage(GetHwnd(), BM_SETSTYLE, style, 1L);
195 }
196
197 // ----------------------------------------------------------------------------
198 // helpers
199 // ----------------------------------------------------------------------------
200
201 bool wxButton::SendClickEvent()
202 {
203 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
204 event.SetEventObject(this);
205
206 return ProcessCommand(event);
207 }
208
209 void wxButton::Command(wxCommandEvent & event)
210 {
211 ProcessCommand(event);
212 }
213
214 // ----------------------------------------------------------------------------
215 // event/message handlers
216 // ----------------------------------------------------------------------------
217
218 bool wxButton::MSWCommand(WXUINT param, WXWORD id)
219 {
220 bool processed = FALSE;
221 switch ( param )
222 {
223 case 1: // 1 for accelerator
224 case BN_CLICKED:
225 processed = SendClickEvent();
226 break;
227 }
228
229 return processed;
230 }
231
232 WXHBRUSH wxButton::OnCtlColor(WXHDC pDC,
233 WXHWND pWnd,
234 WXUINT nCtlColor,
235 WXUINT message,
236 WXWPARAM wParam,
237 WXLPARAM lParam)
238 {
239 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
240
241 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
242 }
243