1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/tglbtn_osx.cpp
3 // Purpose: Definition of the wxToggleButton class, which implements a
4 // toggle button under wxMac.
5 // Author: Stefan Csomor
9 // Copyright: (c) Stefan Csomor
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 #include "wx/wxprec.h"
25 #include "wx/tglbtn.h"
26 #include "wx/osx/private.h"
27 #include "wx/bmpbuttn.h" // for wxDEFAULT_BUTTON_MARGIN
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
34 wxDEFINE_EVENT( wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, wxCommandEvent
);
36 // ============================================================================
38 // ============================================================================
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
44 const wxString
& label
,
46 const wxSize
& size
, long style
,
47 const wxValidator
& validator
,
55 // FIXME: this hack is needed because we're called from
56 // wxBitmapToggleButton::Create() with this style and we currently use a
57 // different wxWidgetImpl method (CreateBitmapToggleButton() rather than
58 // CreateToggleButton()) for creating bitmap buttons, but we really ought
59 // to unify the creation of buttons of all kinds and then remove
61 if ( style
& wxBU_NOTEXT
)
63 return wxControl::Create(parent
, id
, pos
, size
, style
,
67 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
70 m_labelOrig
= m_label
= label
;
72 SetPeer(wxWidgetImpl::CreateToggleButton( this, parent
, id
, label
, pos
, size
, style
, GetExtraStyle() )) ;
74 MacPostControlCreate(pos
,size
) ;
79 wxSize
wxToggleButton::DoGetBestSize() const
84 int lBtn
= m_label
.Length() * 8 + 12 ;
88 return wxSize ( wBtn
, hBtn
) ;
91 void wxToggleButton::SetValue(bool val
)
93 GetPeer()->SetValue( val
) ;
96 bool wxToggleButton::GetValue() const
98 return GetPeer()->GetValue() ;
101 void wxToggleButton::Command(wxCommandEvent
& event
)
103 SetValue((event
.GetInt() != 0));
104 ProcessCommand(event
);
107 bool wxToggleButton::OSXHandleClicked( double WXUNUSED(timestampsec
) )
109 wxCommandEvent
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, m_windowId
);
110 event
.SetInt(GetValue());
111 event
.SetEventObject(this);
112 ProcessCommand(event
);
116 // ----------------------------------------------------------------------------
117 // wxBitmapToggleButton
118 // ----------------------------------------------------------------------------
120 IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton
, wxToggleButton
)
122 bool wxBitmapToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
123 const wxBitmap
& label
,
125 const wxSize
& size
, long style
,
126 const wxValidator
& validator
,
127 const wxString
& name
)
131 if ( !wxToggleButton::Create(parent
, id
, wxEmptyString
, pos
, size
, style
| wxBU_NOTEXT
| wxBU_EXACTFIT
, validator
, name
) )
135 m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
137 m_bitmaps
[State_Normal
] = label
;
139 SetPeer(wxWidgetImpl::CreateBitmapToggleButton( this, parent
, id
, label
, pos
, size
, style
, GetExtraStyle() ));
141 MacPostControlCreate(pos
,size
) ;
146 wxSize
wxBitmapToggleButton::DoGetBestSize() const
148 if (!GetBitmap().IsOk())
149 return wxSize(20,20);
152 best
.x
= GetBitmap().GetWidth() + 2 * m_marginX
;
153 best
.y
= GetBitmap().GetHeight() + 2 * m_marginY
;
158 #endif // wxUSE_TOGGLEBTN