]> git.saurik.com Git - wxWidgets.git/blob - src/osx/tglbtn_osx.cpp
8a5e348bdcecb4581fe121fc7a49d730e411d044
[wxWidgets.git] / src / osx / tglbtn_osx.cpp
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
6 // Modified by:
7 // Created: 08.02.01
8 // RCS-ID: $Id$
9 // Copyright: (c) Stefan Csomor
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 // ============================================================================
14 // declatations
15 // ============================================================================
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 #include "wx/wxprec.h"
22
23 #if wxUSE_TOGGLEBTN
24
25 #include "wx/tglbtn.h"
26 #include "wx/osx/private.h"
27 #include "wx/bmpbuttn.h" // for wxDEFAULT_BUTTON_MARGIN
28
29 // ----------------------------------------------------------------------------
30 // macros
31 // ----------------------------------------------------------------------------
32
33 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
34 wxDEFINE_EVENT( wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, wxCommandEvent );
35
36 // ============================================================================
37 // implementation
38 // ============================================================================
39 // ----------------------------------------------------------------------------
40 // wxToggleButton
41 // ----------------------------------------------------------------------------
42
43 bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
44 const wxString& label,
45 const wxPoint& pos,
46 const wxSize& size, long style,
47 const wxValidator& validator,
48 const wxString& name)
49 {
50 DontCreatePeer();
51
52 m_marginX =
53 m_marginY = 0;
54
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
60 // this check
61 if ( style & wxBU_NOTEXT )
62 {
63 return wxControl::Create(parent, id, pos, size, style,
64 validator, name);
65 }
66
67 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
68 return false;
69
70 m_labelOrig = m_label = label ;
71
72 SetPeer(wxWidgetImpl::CreateToggleButton( this, parent, id, label, pos, size, style, GetExtraStyle() )) ;
73
74 MacPostControlCreate(pos,size) ;
75
76 return TRUE;
77 }
78
79 wxSize wxToggleButton::DoGetBestSize() const
80 {
81 int wBtn = 70 ;
82 int hBtn = 20 ;
83
84 int lBtn = m_label.Length() * 8 + 12 ;
85 if (lBtn > wBtn)
86 wBtn = lBtn;
87
88 return wxSize ( wBtn , hBtn ) ;
89 }
90
91 void wxToggleButton::SetValue(bool val)
92 {
93 GetPeer()->SetValue( val ) ;
94 }
95
96 bool wxToggleButton::GetValue() const
97 {
98 return GetPeer()->GetValue() ;
99 }
100
101 void wxToggleButton::Command(wxCommandEvent & event)
102 {
103 SetValue((event.GetInt() != 0));
104 ProcessCommand(event);
105 }
106
107 bool wxToggleButton::OSXHandleClicked( double WXUNUSED(timestampsec) )
108 {
109 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, m_windowId);
110 event.SetInt(GetValue());
111 event.SetEventObject(this);
112 ProcessCommand(event);
113 return true ;
114 }
115
116 // ----------------------------------------------------------------------------
117 // wxBitmapToggleButton
118 // ----------------------------------------------------------------------------
119
120 IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton, wxToggleButton)
121
122 bool wxBitmapToggleButton::Create(wxWindow *parent, wxWindowID id,
123 const wxBitmap& label,
124 const wxPoint& pos,
125 const wxSize& size, long style,
126 const wxValidator& validator,
127 const wxString& name)
128 {
129 DontCreatePeer();
130
131 if ( !wxToggleButton::Create(parent, id, wxEmptyString, pos, size, style | wxBU_NOTEXT | wxBU_EXACTFIT, validator, name) )
132 return false;
133
134 m_marginX =
135 m_marginY = wxDEFAULT_BUTTON_MARGIN;
136
137 m_bitmaps[State_Normal] = label;
138
139 SetPeer(wxWidgetImpl::CreateBitmapToggleButton( this, parent, id, label, pos, size, style, GetExtraStyle() ));
140
141 MacPostControlCreate(pos,size) ;
142
143 return TRUE;
144 }
145
146 wxSize wxBitmapToggleButton::DoGetBestSize() const
147 {
148 if (!GetBitmap().IsOk())
149 return wxSize(20,20);
150
151 wxSize best;
152 best.x = GetBitmap().GetWidth() + 2 * m_marginX;
153 best.y = GetBitmap().GetHeight() + 2 * m_marginY;
154
155 return best;
156 }
157
158 #endif // wxUSE_TOGGLEBTN
159