]> git.saurik.com Git - wxWidgets.git/blame - src/msw/button.cpp
*** empty log message ***
[wxWidgets.git] / src / msw / button.cpp
CommitLineData
2bda0e17
KB
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
edccf428 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
edccf428
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
2bda0e17 19#ifdef __GNUG__
edccf428 20 #pragma implementation "button.h"
2bda0e17
KB
21#endif
22
23// For compilers that support precompilation, includes "wx.h".
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
edccf428 27 #pragma hdrstop
2bda0e17
KB
28#endif
29
30#ifndef WX_PRECOMP
edccf428
VZ
31 #include "wx/button.h"
32 #include "wx/brush.h"
4e938f5b 33 #include "wx/panel.h"
8a4df159 34 #include "wx/bmpbuttn.h"
fb39c7ec
RR
35 #include "wx/settings.h"
36 #include "wx/dcscreen.h"
2bda0e17
KB
37#endif
38
39#include "wx/msw/private.h"
40
edccf428
VZ
41// ----------------------------------------------------------------------------
42// macros
43// ----------------------------------------------------------------------------
44
edccf428 45 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
2bda0e17 46
edccf428
VZ
47// this macro tries to adjust the default button height to a reasonable value
48// using the char height as the base
1c4a764c 49#define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
2bda0e17 50
edccf428
VZ
51// ============================================================================
52// implementation
53// ============================================================================
54
55// ----------------------------------------------------------------------------
56// creation/destruction
57// ----------------------------------------------------------------------------
58
59bool 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)
2bda0e17 67{
8d99be5f 68 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
edccf428
VZ
69 return FALSE;
70
edccf428
VZ
71 parent->AddChild((wxButton *)this);
72
73 m_backgroundColour = parent->GetBackgroundColour() ;
74 m_foregroundColour = parent->GetForegroundColour() ;
75
76 m_hWnd = (WXHWND)CreateWindowEx
77 (
78 MakeExtendedStyle(m_windowStyle),
223d09f6 79 wxT("BUTTON"),
edccf428
VZ
80 label,
81 WS_VISIBLE | WS_TABSTOP | WS_CHILD,
a77aa9d6 82 0, 0, 0, 0,
edccf428
VZ
83 GetWinHwnd(parent),
84 (HMENU)m_windowId,
85 wxGetInstance(),
86 NULL
87 );
88
89 // Subclass again for purposes of dialog editing mode
90 SubclassWin(m_hWnd);
91
92 SetFont(parent->GetFont());
93
94 SetSize(pos.x, pos.y, size.x, size.y);
95
3f3cec48
RR
96 // bad hack added by Robert to make buttons at least
97 // 80 pixels wide. There are probably better ways...
98 // TODO. FIXME.
99 wxSize nsize( GetSize() );
100 if ((nsize.x < 80) || (nsize.y < 23))
101 {
678cd6de
VZ
102 if ((size.x == -1) && (nsize.x < 80))
103 nsize.x = 80;
104 if ((size.y == -1) && (nsize.y < 23))
105 nsize.y = 23;
3f3cec48
RR
106 SetSize( nsize );
107 }
108
2bda0e17 109 return TRUE;
2bda0e17
KB
110}
111
edccf428 112wxButton::~wxButton()
2bda0e17 113{
edccf428
VZ
114 wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
115 if ( panel )
116 {
117 if ( panel->GetDefaultItem() == this )
118 {
119 // don't leave the panel with invalid default item
120 panel->SetDefaultItem(NULL);
121 }
122 }
2bda0e17
KB
123}
124
edccf428
VZ
125// ----------------------------------------------------------------------------
126// size management including autosizing
127// ----------------------------------------------------------------------------
128
f68586e5 129wxSize wxButton::DoGetBestSize() const
2bda0e17 130{
4438caf4
VZ
131 wxString label = wxGetWindowText(GetHWND());
132 int wBtn;
133 GetTextExtent(label, &wBtn, NULL);
edccf428 134
4438caf4
VZ
135 int wChar, hChar;
136 wxGetCharSize(GetHWND(), &wChar, &hChar, &GetFont());
137
138 // add a margin - the button is wider than just its label
139 wBtn += 3*wChar;
140
141 // the button height is proportional to the height of the font used
142 int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
edccf428 143
4438caf4 144 return wxSize(wBtn, hBtn);
2bda0e17
KB
145}
146
e1f36ff8
VZ
147/* static */
148wxSize wxButton::GetDefaultSize()
149{
8c3c31d4 150 static wxSize s_sizeBtn;
e1f36ff8 151
8c3c31d4
VZ
152 if ( s_sizeBtn.x == 0 )
153 {
154 wxScreenDC dc;
155 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
156
157 // the size of a standard button in the dialog units is 50x14,
158 // translate this to pixels
159 // NB1: the multipliers come from the Windows convention
160 // NB2: the extra +1/+2 were needed to get the size be the same as the
161 // size of the buttons in the standard dialog - I don't know how
162 // this happens, but on my system this size is 75x23 in pixels and
163 // 23*8 isn't even divisible by 14... Would be nice to understand
164 // why these constants are needed though!
165 s_sizeBtn.x = (50 * (dc.GetCharWidth() + 1))/4;
166 s_sizeBtn.y = ((14 * dc.GetCharHeight()) + 2)/8;
167 }
e1f36ff8 168
8c3c31d4 169 return s_sizeBtn;
e1f36ff8
VZ
170}
171
4438caf4
VZ
172// ----------------------------------------------------------------------------
173// set this button as the default one in its panel
174// ----------------------------------------------------------------------------
175
edccf428 176void wxButton::SetDefault()
2bda0e17 177{
edccf428 178 wxWindow *parent = GetParent();
5d1d2d46 179 wxButton *btnOldDefault = NULL;
edccf428
VZ
180 wxPanel *panel = wxDynamicCast(parent, wxPanel);
181 if ( panel )
5d1d2d46
VZ
182 {
183 btnOldDefault = panel->GetDefaultItem();
edccf428 184 panel->SetDefaultItem(this);
5d1d2d46 185 }
2bda0e17 186
edccf428
VZ
187 if ( parent )
188 {
189 SendMessage(GetWinHwnd(parent), DM_SETDEFID, m_windowId, 0L);
190 }
8ed57d93 191
0655ad29
VZ
192 // this doesn't work with bitmap buttons because it also removes the
193 // "ownerdrawn" style...
194 if ( btnOldDefault && !wxDynamicCast(btnOldDefault, wxBitmapButton) )
5d1d2d46
VZ
195 {
196 // remove the BS_DEFPUSHBUTTON style from the other button
197 long style = GetWindowLong(GetHwndOf(btnOldDefault), GWL_STYLE);
198 style &= ~BS_DEFPUSHBUTTON;
199 SendMessage(GetHwndOf(btnOldDefault), BM_SETSTYLE, style, 1L);
200 }
201
202 // set this button as the default
203 long style = GetWindowLong(GetHwnd(), GWL_STYLE);
204 style |= BS_DEFPUSHBUTTON;
205 SendMessage(GetHwnd(), BM_SETSTYLE, style, 1L);
2bda0e17
KB
206}
207
edccf428
VZ
208// ----------------------------------------------------------------------------
209// helpers
210// ----------------------------------------------------------------------------
211
212bool wxButton::SendClickEvent()
2bda0e17 213{
edccf428
VZ
214 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
215 event.SetEventObject(this);
216
217 return ProcessCommand(event);
2bda0e17
KB
218}
219
edccf428 220void wxButton::Command(wxCommandEvent & event)
2bda0e17 221{
edccf428 222 ProcessCommand(event);
2bda0e17
KB
223}
224
edccf428
VZ
225// ----------------------------------------------------------------------------
226// event/message handlers
227// ----------------------------------------------------------------------------
2bda0e17 228
edccf428
VZ
229bool wxButton::MSWCommand(WXUINT param, WXWORD id)
230{
231 bool processed = FALSE;
57c0af52 232 switch ( param )
edccf428 233 {
678cd6de 234 case 1: // means that the message came from an accelerator
57c0af52
VZ
235 case BN_CLICKED:
236 processed = SendClickEvent();
237 break;
edccf428 238 }
2bda0e17 239
edccf428 240 return processed;
2bda0e17
KB
241}
242
678cd6de
VZ
243long wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
244{
245 // make sure that we won't have BS_DEFPUSHBUTTON style any more if the
246 // focus is being transfered to another button with the same parent -
247 // otherwise, we could finish with 2 default buttons inside one panel
248 if ( (nMsg == WM_KILLFOCUS) &&
249 (GetWindowLong(GetHwnd(), GWL_STYLE) & BS_DEFPUSHBUTTON) )
250 {
251 wxWindow *parent = GetParent();
252 wxWindow *win = wxFindWinFromHandle((WXHWND)wParam);
253 if ( win && win->GetParent() == parent )
254 {
255 wxPanel *panel = wxDynamicCast(parent, wxPanel);
256 if ( panel )
257 {
258 panel->SetDefaultItem(this);
259 }
260 // else: I don't know what to do - we'll still have the problem
261 // with multiple default buttons in a dialog...
262 }
263 }
264
265 // let the base class do all real processing
266 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
267}