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"
28 #include "wx/tglbtn.h"
31 #include "wx/dcclient.h"
32 #include "wx/dcscreen.h"
33 #include "wx/button.h"
34 #include "wx/validate.h"
35 #include "wx/settings.h"
38 #include "wx/univ/renderer.h"
39 #include "wx/univ/inphand.h"
40 #include "wx/univ/theme.h"
41 #include "wx/univ/colschem.h"
42 #include "wx/stockitem.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
49 // default margins around the image
50 static const wxCoord DEFAULT_BTN_MARGIN_X
= 0; // We should give space for the border, at least.
51 static const wxCoord DEFAULT_BTN_MARGIN_Y
= 0;
53 // ============================================================================
55 // ============================================================================
57 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 void wxToggleButton::Init()
69 bool wxToggleButton::Create(wxWindow
*parent
,
71 const wxBitmap
& bitmap
,
76 const wxValidator
& validator
,
80 if (label
.empty() && wxIsStockID(id
))
81 label
= wxGetStockLabel(id
);
83 long ctrl_style
= style
& ~wxBU_ALIGN_MASK
;
85 wxASSERT_MSG( (ctrl_style
& wxALIGN_MASK
) == 0,
86 _T("Some style conflicts with align flags") );
88 if((style
& wxBU_RIGHT
) == wxBU_RIGHT
)
89 ctrl_style
|= wxALIGN_RIGHT
;
90 else if((style
& wxBU_LEFT
) == wxBU_LEFT
)
91 ctrl_style
|= wxALIGN_LEFT
;
93 ctrl_style
|= wxALIGN_CENTRE_HORIZONTAL
;
95 if((style
& wxBU_TOP
) == wxBU_TOP
)
96 ctrl_style
|= wxALIGN_TOP
;
97 else if((style
& wxBU_BOTTOM
) == wxBU_BOTTOM
)
98 ctrl_style
|= wxALIGN_BOTTOM
;
100 ctrl_style
|= wxALIGN_CENTRE_VERTICAL
;
102 if ( !wxControl::Create(parent
, id
, pos
, size
, ctrl_style
, validator
, name
) )
106 SetImageLabel(bitmap
);
107 // SetBestSize(size); -- called by SetImageLabel()
109 CreateInputHandler(wxINP_HANDLER_BUTTON
);
114 wxToggleButton::~wxToggleButton()
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
123 wxSize
wxToggleButton::GetDefaultSize()
125 static wxSize s_sizeBtn
;
127 if ( s_sizeBtn
.x
== 0 )
131 // this corresponds more or less to wxMSW standard in Win32 theme (see
132 // wxWin32Renderer::AdjustSize())
133 // s_sizeBtn.x = 8*dc.GetCharWidth();
134 // s_sizeBtn.y = (11*dc.GetCharHeight())/10 + 2;
135 // Otto Wyss, Patch 664399
136 s_sizeBtn
.x
= dc
.GetCharWidth()*10 + 2;
137 s_sizeBtn
.y
= dc
.GetCharHeight()*11/10 + 2;
143 wxSize
wxToggleButton::DoGetBestClientSize() const
145 wxClientDC
dc(wxConstCast(this, wxToggleButton
));
146 wxCoord width
, height
;
147 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
151 // allocate extra space for the bitmap
152 wxCoord heightBmp
= m_bitmap
.GetHeight() + 2*m_marginBmpY
;
153 if ( height
< heightBmp
)
156 width
+= m_bitmap
.GetWidth() + 2*m_marginBmpX
;
159 // The default size should not be adjusted, so the code is moved into the
160 // renderer. This is conceptual wrong but currently the only solution.
161 // (Otto Wyss, Patch 664399)
164 // for compatibility with other ports, the buttons default size is never
165 // less than the standard one, but not when display not PDAs.
166 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA)
168 if ( !(GetWindowStyle() & wxBU_EXACTFIT) )
170 wxSize szDef = GetDefaultSize();
171 if ( width < szDef.x )
176 return wxSize(width
, height
);
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 void wxToggleButton::DoDraw(wxControlRenderer
*renderer
)
185 if ( !(GetWindowStyle() & wxBORDER_NONE
) )
187 renderer
->DrawButtonBorder();
190 renderer
->DrawLabel(m_bitmap
, m_marginBmpX
, m_marginBmpY
);
193 bool wxToggleButton::DoDrawBackground(wxDC
& dc
)
196 wxSize size
= GetSize();
198 rect
.height
= size
.y
;
200 if ( GetBackgroundBitmap().Ok() )
202 // get the bitmap and the flags
205 wxBitmap bmp
= GetBackgroundBitmap(&alignment
, &stretch
);
206 wxControlRenderer::DrawBitmap(dc
, bmp
, rect
, alignment
, stretch
);
210 m_renderer
->DrawButtonSurface(dc
, wxTHEME_BG_COLOUR(this),
211 rect
, GetStateFlags());
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
221 void wxToggleButton::Press()
231 void wxToggleButton::Release()
241 void wxToggleButton::Toggle()
250 // releasing button after it had been pressed generates a click event
257 void wxToggleButton::Click()
259 wxCommandEvent
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, GetId());
260 InitCommandEvent(event
);
261 event
.SetInt(GetValue());
265 bool wxToggleButton::PerformAction(const wxControlAction
& action
,
267 const wxString
& strArg
)
269 if ( action
== wxACTION_BUTTON_TOGGLE
)
271 else if ( action
== wxACTION_BUTTON_CLICK
)
273 else if ( action
== wxACTION_BUTTON_PRESS
)
275 else if ( action
== wxACTION_BUTTON_RELEASE
)
278 return wxControl::PerformAction(action
, numArg
, strArg
);
283 // ----------------------------------------------------------------------------
285 // ----------------------------------------------------------------------------
287 void wxToggleButton::SetImageLabel(const wxBitmap
& bitmap
)
291 SetImageMargins(DEFAULT_BTN_MARGIN_X
, DEFAULT_BTN_MARGIN_Y
);
294 void wxToggleButton::SetImageMargins(wxCoord x
, wxCoord y
)
296 m_marginBmpX
= x
+ 2;
297 m_marginBmpY
= y
+ 2;
299 SetBestSize(wxDefaultSize
);
302 // void SetValue(bool state)
303 // Set the value of the toggle button.
304 void wxToggleButton::SetValue(bool state
)
310 // ============================================================================
311 // wxStdButtonInputHandler
312 // ============================================================================
314 wxStdToggleButtonInputHandler::wxStdToggleButtonInputHandler(wxInputHandler
*handler
)
315 : wxStdInputHandler(handler
)
318 m_winHasMouse
= false;
321 bool wxStdToggleButtonInputHandler::HandleKey(wxInputConsumer
*consumer
,
322 const wxKeyEvent
& event
,
325 int keycode
= event
.GetKeyCode();
326 if ( keycode
== WXK_SPACE
|| keycode
== WXK_RETURN
)
328 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
333 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
336 bool wxStdToggleButtonInputHandler::HandleMouse(wxInputConsumer
*consumer
,
337 const wxMouseEvent
& event
)
339 // the button has 2 states: pressed and normal with the following
340 // transitions between them:
342 // normal -> left down -> capture mouse and go to pressed state
343 // pressed -> left up inside -> generate click -> go to normal
344 // outside ------------------>
346 // the other mouse buttons are ignored
347 if ( event
.Button(1) )
349 if ( event
.LeftDown() || event
.LeftDClick() )
351 m_winCapture
= consumer
->GetInputWindow();
352 m_winCapture
->CaptureMouse();
353 m_winHasMouse
= true;
355 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
359 else if ( event
.LeftUp() )
363 m_winCapture
->ReleaseMouse();
369 // this will generate a click event
370 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
374 //else: the mouse was released outside the window, this doesn't
377 //else: don't do anything special about the double click
380 return wxStdInputHandler::HandleMouse(consumer
, event
);
383 bool wxStdToggleButtonInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
384 const wxMouseEvent
& event
)
386 // we only have to do something when the mouse leaves/enters the pressed
387 // button and don't care about the other ones
388 if ( event
.GetEventObject() == m_winCapture
)
390 // leaving the button should remove its pressed state
391 if ( event
.Leaving() )
393 // remember that the mouse is now outside
394 m_winHasMouse
= false;
396 // we do have a pressed button, so release it
397 consumer
->GetInputWindow()->SetCurrent(false);
398 consumer
->PerformAction(wxACTION_BUTTON_RELEASE
);
402 // and entering it back should make it pressed again if it had been
404 else if ( event
.Entering() )
406 // the mouse is (back) inside the button
407 m_winHasMouse
= true;
409 // we did have a pressed button which we released when leaving the
410 // window, press it again
411 consumer
->GetInputWindow()->SetCurrent(true);
412 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
418 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
421 bool wxStdToggleButtonInputHandler::HandleFocus(wxInputConsumer
* WXUNUSED(consumer
),
422 const wxFocusEvent
& WXUNUSED(event
))
424 // buttons change appearance when they get/lose focus, so return true to
429 bool wxStdToggleButtonInputHandler::HandleActivation(wxInputConsumer
*consumer
,
430 bool WXUNUSED(activated
))
432 // the default button changes appearance when the app is [de]activated, so
433 // return true to refresh
434 return wxStaticCast(consumer
->GetInputWindow(), wxToggleButton
)->IsDefault();
437 #endif // wxUSE_TOGGLEBTN