]> git.saurik.com Git - wxWidgets.git/blame - src/univ/button.cpp
Update for new event handler stuff
[wxWidgets.git] / src / univ / button.cpp
CommitLineData
1e6feb95
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: univ/button.cpp
3// Purpose: wxButton
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 14.08.00
7// RCS-ID: $Id$
442b35b5 8// Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
65571936 9// Licence: wxWindows licence
1e6feb95
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
a3870b2f 21 #pragma implementation "univbutton.h"
1e6feb95
VZ
22#endif
23
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif
29
30#if wxUSE_BUTTON
31
32#ifndef WX_PRECOMP
33 #include "wx/dcclient.h"
34 #include "wx/dcscreen.h"
35 #include "wx/button.h"
36 #include "wx/validate.h"
c15521c6 37 #include "wx/settings.h"
1e6feb95
VZ
38#endif
39
40#include "wx/univ/renderer.h"
41#include "wx/univ/inphand.h"
42#include "wx/univ/theme.h"
193e19cf 43#include "wx/univ/colschem.h"
5f7bcb48 44#include "wx/stockitem.h"
1e6feb95
VZ
45
46// ----------------------------------------------------------------------------
47// constants
48// ----------------------------------------------------------------------------
49
50// default margins around the image
ea1ad04b 51static const wxCoord DEFAULT_BTN_MARGIN_X = 0; // We should give space for the border, at least.
1e6feb95
VZ
52static const wxCoord DEFAULT_BTN_MARGIN_Y = 0;
53
54// ============================================================================
55// implementation
56// ============================================================================
57
58IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
59
60// ----------------------------------------------------------------------------
61// creation
62// ----------------------------------------------------------------------------
63
64void wxButton::Init()
65{
66 m_isPressed =
a290fa5a 67 m_isDefault = false;
1e6feb95
VZ
68}
69
70bool wxButton::Create(wxWindow *parent,
71 wxWindowID id,
72 const wxBitmap& bitmap,
5f7bcb48 73 const wxString &lbl,
1e6feb95
VZ
74 const wxPoint &pos,
75 const wxSize &size,
76 long style,
77 const wxValidator& validator,
78 const wxString &name)
79{
5f7bcb48
VS
80 wxString label(lbl);
81 if (label.empty() && wxIsStockID(id))
82 label = wxGetStockLabel(id);
83
041ae202
WS
84 long ctrl_style = style & !wxBU_ALIGN_MASK;
85
86 wxASSERT_MSG( (ctrl_style & wxALIGN_MASK) == 0,
87 _T("Some style conflicts with align flags") );
88
89 if((style & wxBU_RIGHT) == wxBU_RIGHT)
90 ctrl_style |= wxALIGN_RIGHT;
91 else if((style & wxBU_LEFT) == wxBU_LEFT)
92 ctrl_style |= wxALIGN_LEFT;
93 else
94 ctrl_style |= wxALIGN_CENTRE_HORIZONTAL;
1e6feb95 95
041ae202
WS
96 if((style & wxBU_TOP) == wxBU_TOP)
97 ctrl_style |= wxALIGN_TOP;
98 else if((style & wxBU_BOTTOM) == wxBU_BOTTOM)
99 ctrl_style |= wxALIGN_BOTTOM;
100 else
101 ctrl_style |= wxALIGN_CENTRE_VERTICAL;
102
103 if ( !wxControl::Create(parent, id, pos, size, ctrl_style, validator, name) )
a290fa5a 104 return false;
1e6feb95
VZ
105
106 SetLabel(label);
107 SetImageLabel(bitmap);
108 // SetBestSize(size); -- called by SetImageLabel()
109
110 CreateInputHandler(wxINP_HANDLER_BUTTON);
111
a290fa5a 112 return true;
1e6feb95
VZ
113}
114
115wxButton::~wxButton()
116{
117}
118
119// ----------------------------------------------------------------------------
120// size management
121// ----------------------------------------------------------------------------
122
123/* static */
124wxSize wxButtonBase::GetDefaultSize()
125{
126 static wxSize s_sizeBtn;
127
128 if ( s_sizeBtn.x == 0 )
129 {
130 wxScreenDC dc;
131
132 // this corresponds more or less to wxMSW standard in Win32 theme (see
133 // wxWin32Renderer::AdjustSize())
61a83c1c
JS
134// s_sizeBtn.x = 8*dc.GetCharWidth();
135// s_sizeBtn.y = (11*dc.GetCharHeight())/10 + 2;
136 // Otto Wyss, Patch 664399
137 s_sizeBtn.x = dc.GetCharWidth()*10 + 2;
138 s_sizeBtn.y = dc.GetCharHeight()*11/10 + 2;
1e6feb95
VZ
139 }
140
141 return s_sizeBtn;
142}
143
144wxSize wxButton::DoGetBestClientSize() const
145{
146 wxClientDC dc(wxConstCast(this, wxButton));
147 wxCoord width, height;
148 dc.GetMultiLineTextExtent(GetLabel(), &width, &height);
149
150 if ( m_bitmap.Ok() )
151 {
152 // allocate extra space for the bitmap
153 wxCoord heightBmp = m_bitmap.GetHeight() + 2*m_marginBmpY;
154 if ( height < heightBmp )
155 height = heightBmp;
156
157 width += m_bitmap.GetWidth() + 2*m_marginBmpX;
158 }
159
041ae202 160 // The default size should not be adjusted, so the code is moved into the
61a83c1c
JS
161 // renderer. This is conceptual wrong but currently the only solution.
162 // (Otto Wyss, Patch 664399)
163
164/*
1e6feb95 165 // for compatibility with other ports, the buttons default size is never
c15521c6 166 // less than the standard one, but not when display not PDAs.
41fecb44 167 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA)
7ef8bfc4 168 {
c15521c6
RR
169 if ( !(GetWindowStyle() & wxBU_EXACTFIT) )
170 {
171 wxSize szDef = GetDefaultSize();
172 if ( width < szDef.x )
173 width = szDef.x;
174 }
7ef8bfc4 175 }
61a83c1c 176*/
1e6feb95
VZ
177 return wxSize(width, height);
178}
179
180// ----------------------------------------------------------------------------
181// drawing
182// ----------------------------------------------------------------------------
183
184void wxButton::DoDraw(wxControlRenderer *renderer)
185{
e4606ed9
VZ
186 if ( !(GetWindowStyle() & wxBORDER_NONE) )
187 {
188 renderer->DrawButtonBorder();
189 }
190
1e6feb95
VZ
191 renderer->DrawLabel(m_bitmap, m_marginBmpX, m_marginBmpY);
192}
193
193e19cf
RR
194bool wxButton::DoDrawBackground(wxDC& dc)
195{
196 wxRect rect;
197 wxSize size = GetSize();
198 rect.width = size.x;
199 rect.height = size.y;
041ae202 200
193e19cf
RR
201 if ( GetBackgroundBitmap().Ok() )
202 {
203 // get the bitmap and the flags
204 int alignment;
205 wxStretch stretch;
206 wxBitmap bmp = GetBackgroundBitmap(&alignment, &stretch);
207 wxControlRenderer::DrawBitmap(dc, bmp, rect, alignment, stretch);
208 }
209 else
210 {
211 m_renderer->DrawButtonSurface(dc, wxTHEME_BG_COLOUR(this),
212 rect, GetStateFlags());
213 }
214
a290fa5a 215 return true;
193e19cf
RR
216}
217
1e6feb95
VZ
218// ----------------------------------------------------------------------------
219// input processing
220// ----------------------------------------------------------------------------
221
222void wxButton::Press()
223{
224 if ( !m_isPressed )
225 {
a290fa5a 226 m_isPressed = true;
1e6feb95
VZ
227
228 Refresh();
229 }
230}
231
232void wxButton::Release()
233{
234 if ( m_isPressed )
235 {
a290fa5a 236 m_isPressed = false;
1e6feb95
VZ
237
238 Refresh();
239 }
240}
241
242void wxButton::Toggle()
243{
244 if ( m_isPressed )
245 Release();
246 else
247 Press();
248
249 if ( !m_isPressed )
250 {
251 // releasing button after it had been pressed generates a click event
252 Click();
253 }
254}
255
256void wxButton::Click()
257{
258 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
259 InitCommandEvent(event);
260 Command(event);
261}
262
263bool wxButton::PerformAction(const wxControlAction& action,
264 long numArg,
265 const wxString& strArg)
266{
267 if ( action == wxACTION_BUTTON_TOGGLE )
268 Toggle();
269 else if ( action == wxACTION_BUTTON_CLICK )
270 Click();
271 else if ( action == wxACTION_BUTTON_PRESS )
272 Press();
273 else if ( action == wxACTION_BUTTON_RELEASE )
274 Release();
275 else
276 return wxControl::PerformAction(action, numArg, strArg);
277
a290fa5a 278 return true;
1e6feb95
VZ
279}
280
281// ----------------------------------------------------------------------------
282// misc
283// ----------------------------------------------------------------------------
284
285void wxButton::SetImageLabel(const wxBitmap& bitmap)
286{
287 m_bitmap = bitmap;
288
289 SetImageMargins(DEFAULT_BTN_MARGIN_X, DEFAULT_BTN_MARGIN_Y);
290}
291
292void wxButton::SetImageMargins(wxCoord x, wxCoord y)
293{
34d26f42
RR
294 m_marginBmpX = x + 2;
295 m_marginBmpY = y + 2;
041ae202 296
1e6feb95
VZ
297 SetBestSize(wxDefaultSize);
298}
299
300void wxButton::SetDefault()
301{
a290fa5a 302 m_isDefault = true;
1e6feb95
VZ
303}
304
305// ============================================================================
306// wxStdButtonInputHandler
307// ============================================================================
308
309wxStdButtonInputHandler::wxStdButtonInputHandler(wxInputHandler *handler)
310 : wxStdInputHandler(handler)
311{
312 m_winCapture = NULL;
a290fa5a 313 m_winHasMouse = false;
1e6feb95
VZ
314}
315
23645bfa 316bool wxStdButtonInputHandler::HandleKey(wxInputConsumer *consumer,
1e6feb95
VZ
317 const wxKeyEvent& event,
318 bool pressed)
319{
320 int keycode = event.GetKeyCode();
321 if ( keycode == WXK_SPACE || keycode == WXK_RETURN )
322 {
23645bfa 323 consumer->PerformAction(wxACTION_BUTTON_TOGGLE);
1e6feb95 324
a290fa5a 325 return true;
1e6feb95
VZ
326 }
327
23645bfa 328 return wxStdInputHandler::HandleKey(consumer, event, pressed);
1e6feb95
VZ
329}
330
23645bfa 331bool wxStdButtonInputHandler::HandleMouse(wxInputConsumer *consumer,
1e6feb95
VZ
332 const wxMouseEvent& event)
333{
334 // the button has 2 states: pressed and normal with the following
335 // transitions between them:
336 //
337 // normal -> left down -> capture mouse and go to pressed state
338 // pressed -> left up inside -> generate click -> go to normal
339 // outside ------------------>
340 //
341 // the other mouse buttons are ignored
342 if ( event.Button(1) )
343 {
96e3c3d4 344 if ( event.LeftDown() || event.LeftDClick() )
1e6feb95 345 {
23645bfa 346 m_winCapture = consumer->GetInputWindow();
1e6feb95 347 m_winCapture->CaptureMouse();
a290fa5a 348 m_winHasMouse = true;
1e6feb95 349
23645bfa 350 consumer->PerformAction(wxACTION_BUTTON_PRESS);
1e6feb95 351
a290fa5a 352 return true;
1e6feb95 353 }
96e3c3d4 354 else if ( event.LeftUp() )
1e6feb95
VZ
355 {
356 if ( m_winCapture )
357 {
358 m_winCapture->ReleaseMouse();
359 m_winCapture = NULL;
360 }
361
362 if ( m_winHasMouse )
363 {
364 // this will generate a click event
23645bfa 365 consumer->PerformAction(wxACTION_BUTTON_TOGGLE);
1e6feb95 366
a290fa5a 367 return true;
1e6feb95
VZ
368 }
369 //else: the mouse was released outside the window, this doesn't
370 // count as a click
371 }
96e3c3d4 372 //else: don't do anything special about the double click
1e6feb95
VZ
373 }
374
23645bfa 375 return wxStdInputHandler::HandleMouse(consumer, event);
1e6feb95
VZ
376}
377
23645bfa 378bool wxStdButtonInputHandler::HandleMouseMove(wxInputConsumer *consumer,
1e6feb95
VZ
379 const wxMouseEvent& event)
380{
381 // we only have to do something when the mouse leaves/enters the pressed
382 // button and don't care about the other ones
383 if ( event.GetEventObject() == m_winCapture )
384 {
385 // leaving the button should remove its pressed state
386 if ( event.Leaving() )
387 {
388 // remember that the mouse is now outside
a290fa5a 389 m_winHasMouse = false;
1e6feb95
VZ
390
391 // we do have a pressed button, so release it
a290fa5a 392 consumer->GetInputWindow()->SetCurrent(false);
23645bfa 393 consumer->PerformAction(wxACTION_BUTTON_RELEASE);
1e6feb95 394
a290fa5a 395 return true;
1e6feb95
VZ
396 }
397 // and entering it back should make it pressed again if it had been
398 // pressed
399 else if ( event.Entering() )
400 {
401 // the mouse is (back) inside the button
a290fa5a 402 m_winHasMouse = true;
1e6feb95
VZ
403
404 // we did have a pressed button which we released when leaving the
405 // window, press it again
a290fa5a 406 consumer->GetInputWindow()->SetCurrent(true);
23645bfa 407 consumer->PerformAction(wxACTION_BUTTON_PRESS);
1e6feb95 408
a290fa5a 409 return true;
1e6feb95
VZ
410 }
411 }
412
23645bfa 413 return wxStdInputHandler::HandleMouseMove(consumer, event);
1e6feb95
VZ
414}
415
61fef19b
VZ
416bool wxStdButtonInputHandler::HandleFocus(wxInputConsumer * WXUNUSED(consumer),
417 const wxFocusEvent& WXUNUSED(event))
1e6feb95 418{
a290fa5a 419 // buttons change appearance when they get/lose focus, so return true to
1e6feb95 420 // refresh
a290fa5a 421 return true;
1e6feb95
VZ
422}
423
23645bfa 424bool wxStdButtonInputHandler::HandleActivation(wxInputConsumer *consumer,
61fef19b 425 bool WXUNUSED(activated))
1e6feb95
VZ
426{
427 // the default button changes appearance when the app is [de]activated, so
a290fa5a 428 // return true to refresh
23645bfa 429 return wxStaticCast(consumer->GetInputWindow(), wxButton)->IsDefault();
1e6feb95
VZ
430}
431
432#endif // wxUSE_BUTTON
433