1 /////////////////////////////////////////////////////////////////////////////
2 // Name: univ/tglbtn.cpp
3 // Purpose: wxToggleButton
4 // Author: Vadim Zeitlin
5 // Modified by: David Bjorkevik
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
29 #include "wx/dcclient.h"
30 #include "wx/dcscreen.h"
31 #include "wx/button.h"
32 #include "wx/validate.h"
33 #include "wx/settings.h"
36 #include "wx/univ/renderer.h"
37 #include "wx/univ/inphand.h"
38 #include "wx/univ/theme.h"
39 #include "wx/univ/colschem.h"
40 #include "wx/stockitem.h"
41 #include "wx/tglbtn.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
48 // default margins around the image
49 static const wxCoord DEFAULT_BTN_MARGIN_X
= 0; // We should give space for the border, at least.
50 static const wxCoord DEFAULT_BTN_MARGIN_Y
= 0;
52 // ============================================================================
54 // ============================================================================
56 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 void wxToggleButton::Init()
68 bool wxToggleButton::Create(wxWindow
*parent
,
70 const wxBitmap
& bitmap
,
75 const wxValidator
& validator
,
79 if (label
.empty() && wxIsStockID(id
))
80 label
= wxGetStockLabel(id
);
82 long ctrl_style
= style
& ~wxBU_ALIGN_MASK
;
84 wxASSERT_MSG( (ctrl_style
& wxALIGN_MASK
) == 0,
85 _T("Some style conflicts with align flags") );
87 if((style
& wxBU_RIGHT
) == wxBU_RIGHT
)
88 ctrl_style
|= wxALIGN_RIGHT
;
89 else if((style
& wxBU_LEFT
) == wxBU_LEFT
)
90 ctrl_style
|= wxALIGN_LEFT
;
92 ctrl_style
|= wxALIGN_CENTRE_HORIZONTAL
;
94 if((style
& wxBU_TOP
) == wxBU_TOP
)
95 ctrl_style
|= wxALIGN_TOP
;
96 else if((style
& wxBU_BOTTOM
) == wxBU_BOTTOM
)
97 ctrl_style
|= wxALIGN_BOTTOM
;
99 ctrl_style
|= wxALIGN_CENTRE_VERTICAL
;
101 if ( !wxControl::Create(parent
, id
, pos
, size
, ctrl_style
, validator
, name
) )
105 SetImageLabel(bitmap
);
106 // SetBestSize(size); -- called by SetImageLabel()
108 CreateInputHandler(wxINP_HANDLER_BUTTON
);
113 wxToggleButton::~wxToggleButton()
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
122 wxSize
wxToggleButton::GetDefaultSize()
124 static wxSize s_sizeBtn
;
126 if ( s_sizeBtn
.x
== 0 )
130 // this corresponds more or less to wxMSW standard in Win32 theme (see
131 // wxWin32Renderer::AdjustSize())
132 // s_sizeBtn.x = 8*dc.GetCharWidth();
133 // s_sizeBtn.y = (11*dc.GetCharHeight())/10 + 2;
134 // Otto Wyss, Patch 664399
135 s_sizeBtn
.x
= dc
.GetCharWidth()*10 + 2;
136 s_sizeBtn
.y
= dc
.GetCharHeight()*11/10 + 2;
142 wxSize
wxToggleButton::DoGetBestClientSize() const
144 wxClientDC
dc(wxConstCast(this, wxToggleButton
));
145 wxCoord width
, height
;
146 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
150 // allocate extra space for the bitmap
151 wxCoord heightBmp
= m_bitmap
.GetHeight() + 2*m_marginBmpY
;
152 if ( height
< heightBmp
)
155 width
+= m_bitmap
.GetWidth() + 2*m_marginBmpX
;
158 // The default size should not be adjusted, so the code is moved into the
159 // renderer. This is conceptual wrong but currently the only solution.
160 // (Otto Wyss, Patch 664399)
163 // for compatibility with other ports, the buttons default size is never
164 // less than the standard one, but not when display not PDAs.
165 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA)
167 if ( !(GetWindowStyle() & wxBU_EXACTFIT) )
169 wxSize szDef = GetDefaultSize();
170 if ( width < szDef.x )
175 return wxSize(width
, height
);
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 void wxToggleButton::DoDraw(wxControlRenderer
*renderer
)
184 if ( !(GetWindowStyle() & wxBORDER_NONE
) )
186 renderer
->DrawButtonBorder();
189 renderer
->DrawLabel(m_bitmap
, m_marginBmpX
, m_marginBmpY
);
192 bool wxToggleButton::DoDrawBackground(wxDC
& dc
)
195 wxSize size
= GetSize();
197 rect
.height
= size
.y
;
199 if ( GetBackgroundBitmap().Ok() )
201 // get the bitmap and the flags
204 wxBitmap bmp
= GetBackgroundBitmap(&alignment
, &stretch
);
205 wxControlRenderer::DrawBitmap(dc
, bmp
, rect
, alignment
, stretch
);
209 m_renderer
->DrawButtonSurface(dc
, wxTHEME_BG_COLOUR(this),
210 rect
, GetStateFlags());
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
220 void wxToggleButton::Press()
230 void wxToggleButton::Release()
240 void wxToggleButton::Toggle()
249 // releasing button after it had been pressed generates a click event
256 void wxToggleButton::Click()
258 wxCommandEvent
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, GetId());
259 InitCommandEvent(event
);
260 event
.SetInt(GetValue());
264 bool wxToggleButton::PerformAction(const wxControlAction
& action
,
266 const wxString
& strArg
)
268 if ( action
== wxACTION_BUTTON_TOGGLE
)
270 else if ( action
== wxACTION_BUTTON_CLICK
)
272 else if ( action
== wxACTION_BUTTON_PRESS
)
274 else if ( action
== wxACTION_BUTTON_RELEASE
)
277 return wxControl::PerformAction(action
, numArg
, strArg
);
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
286 void wxToggleButton::SetImageLabel(const wxBitmap
& bitmap
)
290 SetImageMargins(DEFAULT_BTN_MARGIN_X
, DEFAULT_BTN_MARGIN_Y
);
293 void wxToggleButton::SetImageMargins(wxCoord x
, wxCoord y
)
295 m_marginBmpX
= x
+ 2;
296 m_marginBmpY
= y
+ 2;
298 SetBestSize(wxDefaultSize
);
301 // void SetValue(bool state)
302 // Set the value of the toggle button.
303 void wxToggleButton::SetValue(bool state
)
309 // ============================================================================
310 // wxStdButtonInputHandler
311 // ============================================================================
313 wxStdToggleButtonInputHandler::wxStdToggleButtonInputHandler(wxInputHandler
*handler
)
314 : wxStdInputHandler(handler
)
317 m_winHasMouse
= false;
320 bool wxStdToggleButtonInputHandler::HandleKey(wxInputConsumer
*consumer
,
321 const wxKeyEvent
& event
,
324 int keycode
= event
.GetKeyCode();
325 if ( keycode
== WXK_SPACE
|| keycode
== WXK_RETURN
)
327 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
332 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
335 bool wxStdToggleButtonInputHandler::HandleMouse(wxInputConsumer
*consumer
,
336 const wxMouseEvent
& event
)
338 // the button has 2 states: pressed and normal with the following
339 // transitions between them:
341 // normal -> left down -> capture mouse and go to pressed state
342 // pressed -> left up inside -> generate click -> go to normal
343 // outside ------------------>
345 // the other mouse buttons are ignored
346 if ( event
.Button(1) )
348 if ( event
.LeftDown() || event
.LeftDClick() )
350 m_winCapture
= consumer
->GetInputWindow();
351 m_winCapture
->CaptureMouse();
352 m_winHasMouse
= true;
354 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
358 else if ( event
.LeftUp() )
362 m_winCapture
->ReleaseMouse();
368 // this will generate a click event
369 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
373 //else: the mouse was released outside the window, this doesn't
376 //else: don't do anything special about the double click
379 return wxStdInputHandler::HandleMouse(consumer
, event
);
382 bool wxStdToggleButtonInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
383 const wxMouseEvent
& event
)
385 // we only have to do something when the mouse leaves/enters the pressed
386 // button and don't care about the other ones
387 if ( event
.GetEventObject() == m_winCapture
)
389 // leaving the button should remove its pressed state
390 if ( event
.Leaving() )
392 // remember that the mouse is now outside
393 m_winHasMouse
= false;
395 // we do have a pressed button, so release it
396 consumer
->GetInputWindow()->SetCurrent(false);
397 consumer
->PerformAction(wxACTION_BUTTON_RELEASE
);
401 // and entering it back should make it pressed again if it had been
403 else if ( event
.Entering() )
405 // the mouse is (back) inside the button
406 m_winHasMouse
= true;
408 // we did have a pressed button which we released when leaving the
409 // window, press it again
410 consumer
->GetInputWindow()->SetCurrent(true);
411 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
417 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
420 bool wxStdToggleButtonInputHandler::HandleFocus(wxInputConsumer
* WXUNUSED(consumer
),
421 const wxFocusEvent
& WXUNUSED(event
))
423 // buttons change appearance when they get/lose focus, so return true to
428 bool wxStdToggleButtonInputHandler::HandleActivation(wxInputConsumer
*consumer
,
429 bool WXUNUSED(activated
))
431 // the default button changes appearance when the app is [de]activated, so
432 // return true to refresh
433 return wxStaticCast(consumer
->GetInputWindow(), wxToggleButton
)->IsDefault();
436 #endif // wxUSE_TOGGLEBTN