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