]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/tglbtn.cpp
replace m_insertCallback with a virtual function, contrary to the old comments a...
[wxWidgets.git] / src / osx / carbon / tglbtn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/tglbtn.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 // License: wxWindows license
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/uma.h"
27 // Button
28
29 // ----------------------------------------------------------------------------
30 // macros
31 // ----------------------------------------------------------------------------
32
33 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
34 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
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 m_macIsUserPane = FALSE ;
51
52 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
53 return false;
54
55 m_labelOrig = m_label = label ;
56
57 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
58
59 m_peer = new wxMacControl(this) ;
60 verify_noerr ( CreateBevelButtonControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , CFSTR("") ,
61 kControlBevelButtonNormalBevel , kControlBehaviorToggles , NULL , 0 , 0 , 0 , m_peer->GetControlRefAddr() ) );
62
63
64 MacPostControlCreate(pos,size) ;
65
66 return TRUE;
67 }
68
69 wxSize wxToggleButton::DoGetBestSize() const
70 {
71 int wBtn = 70 ;
72 int hBtn = 20 ;
73
74 int lBtn = m_label.Length() * 8 + 12 ;
75 if (lBtn > wBtn)
76 wBtn = lBtn;
77
78 return wxSize ( wBtn , hBtn ) ;
79 }
80
81 void wxToggleButton::SetValue(bool val)
82 {
83 m_peer->SetValue( val ) ;
84 }
85
86 bool wxToggleButton::GetValue() const
87 {
88 return m_peer->GetValue() ;
89 }
90
91 void wxToggleButton::Command(wxCommandEvent & event)
92 {
93 SetValue((event.GetInt() != 0));
94 ProcessCommand(event);
95 }
96
97 wxInt32 wxToggleButton::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
98 {
99 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, m_windowId);
100 event.SetInt(GetValue());
101 event.SetEventObject(this);
102 ProcessCommand(event);
103 return noErr ;
104 }
105
106 // ----------------------------------------------------------------------------
107 // wxBitmapToggleButton
108 // ----------------------------------------------------------------------------
109
110 IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton, wxControl)
111
112 bool wxBitmapToggleButton::Create(wxWindow *parent, wxWindowID id,
113 const wxBitmap& label,
114 const wxPoint& pos,
115 const wxSize& size, long style,
116 const wxValidator& validator,
117 const wxString& name)
118 {
119 m_macIsUserPane = FALSE ;
120
121 m_bitmap = label;
122
123 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
124 return false;
125
126 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
127
128 m_peer = new wxMacControl(this) ;
129
130 ControlButtonContentInfo info;
131 wxMacCreateBitmapButton( &info, m_bitmap );
132 verify_noerr ( CreateBevelButtonControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , CFSTR("") ,
133 kControlBevelButtonNormalBevel , kControlBehaviorOffsetContents | kControlBehaviorToggles , &info , 0 , 0 , 0 , m_peer->GetControlRefAddr() ) );
134
135 MacPostControlCreate(pos,size) ;
136
137 return TRUE;
138 }
139
140 wxSize wxBitmapToggleButton::DoGetBestSize() const
141 {
142 if (!m_bitmap.IsOk())
143 return wxSize(20,20);
144
145 return wxSize ( m_bitmap.GetWidth()+6, m_bitmap.GetHeight()+6 ) ;
146 }
147
148 void wxBitmapToggleButton::SetValue(bool val)
149 {
150 m_peer->SetValue( val ) ;
151 }
152
153 bool wxBitmapToggleButton::GetValue() const
154 {
155 return m_peer->GetValue() ;
156 }
157
158 void wxBitmapToggleButton::Command(wxCommandEvent & event)
159 {
160 SetValue((event.GetInt() != 0));
161 ProcessCommand(event);
162 }
163
164 wxInt32 wxBitmapToggleButton::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
165 {
166 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, m_windowId);
167 event.SetInt(GetValue());
168 event.SetEventObject(this);
169 ProcessCommand(event);
170 return noErr ;
171 }
172
173 #endif // wxUSE_TOGGLEBTN
174