]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/button.cpp
Added automatic dialog scrolling ability
[wxWidgets.git] / src / mac / carbon / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/button.cpp
3 // Purpose: wxButton
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/button.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/panel.h"
18 #include "wx/toplevel.h"
19 #include "wx/dcclient.h"
20 #endif
21
22 #include "wx/stockitem.h"
23
24 #include "wx/mac/uma.h"
25
26 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
27
28
29 bool wxButton::Create(wxWindow *parent,
30 wxWindowID id,
31 const wxString& lbl,
32 const wxPoint& pos,
33 const wxSize& size,
34 long style,
35 const wxValidator& validator,
36 const wxString& name)
37 {
38 wxString label(lbl);
39 if (label.empty() && wxIsStockID(id))
40 label = wxGetStockLabel(id);
41
42 m_macIsUserPane = false ;
43
44 if ( !wxButtonBase::Create(parent, id, pos, size, style, validator, name) )
45 return false;
46
47 m_labelOrig = m_label = label ;
48
49 OSStatus err;
50 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
51 m_peer = new wxMacControl(this) ;
52 if ( id == wxID_HELP )
53 {
54 ControlButtonContentInfo info ;
55 info.contentType = kControlContentIconRef ;
56 GetIconRef(kOnSystemDisk, kSystemIconsCreator, kHelpIcon, &info.u.iconRef);
57 err = CreateRoundButtonControl(
58 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
59 &bounds, kControlRoundButtonNormalSize,
60 &info, m_peer->GetControlRefAddr() );
61 }
62 else if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
63 {
64 // Button height is static in Mac, can't be changed, so we need to force it here
65 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL || GetWindowVariant() == wxWINDOW_VARIANT_LARGE )
66 {
67 bounds.bottom = bounds.top + 20 ;
68 m_maxHeight = 20 ;
69 }
70 else if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL )
71 {
72 bounds.bottom = bounds.top + 17 ;
73 m_maxHeight = 17 ;
74 }
75 else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI )
76 {
77 bounds.bottom = bounds.top + 15 ;
78 m_maxHeight = 15 ;
79 }
80 err = CreatePushButtonControl(
81 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
82 &bounds, CFSTR(""), m_peer->GetControlRefAddr() );
83 }
84 else
85 {
86 ControlButtonContentInfo info ;
87 info.contentType = kControlNoContent ;
88 err = CreateBevelButtonControl(
89 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds, CFSTR(""),
90 kControlBevelButtonLargeBevel, kControlBehaviorPushbutton,
91 &info, 0, 0, 0, m_peer->GetControlRefAddr() );
92 }
93
94 verify_noerr( err );
95 wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid Mac control") ) ;
96
97 MacPostControlCreate( pos, size );
98
99 return true;
100 }
101
102 wxWindow *wxButton::SetDefault()
103 {
104 wxWindow *btnOldDefault = wxButtonBase::SetDefault();
105
106 if ( btnOldDefault )
107 {
108 // cast needed to access the protected member
109 btnOldDefault->GetPeer()->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 0 ) ;
110 }
111
112 m_peer->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 1 ) ;
113
114 return btnOldDefault;
115 }
116
117 wxSize wxButton::DoGetBestSize() const
118 {
119 if ( GetId() == wxID_HELP )
120 return wxSize( 20 , 20 ) ;
121
122 wxSize sz = GetDefaultSize() ;
123
124 switch (GetWindowVariant())
125 {
126 case wxWINDOW_VARIANT_NORMAL:
127 case wxWINDOW_VARIANT_LARGE:
128 sz.y = 20 ;
129 break;
130
131 case wxWINDOW_VARIANT_SMALL:
132 sz.y = 17 ;
133 break;
134
135 case wxWINDOW_VARIANT_MINI:
136 sz.y = 15 ;
137 break;
138
139 default:
140 break;
141 }
142
143 Rect bestsize = { 0 , 0 , 0 , 0 } ;
144 m_peer->GetBestRect( &bestsize ) ;
145
146 int wBtn;
147 if ( EmptyRect( &bestsize ) || ( GetWindowStyle() & wxBU_EXACTFIT) )
148 {
149 Point bounds;
150
151 ControlFontStyleRec controlFont;
152 OSStatus err = m_peer->GetData<ControlFontStyleRec>( kControlEntireControl, kControlFontStyleTag, &controlFont );
153 verify_noerr( err );
154
155 wxCFStringRef str( m_label, m_font.GetEncoding() );
156
157 #if wxMAC_USE_ATSU_TEXT
158 SInt16 baseline;
159 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
160 {
161 err = GetThemeTextDimensions(
162 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
163 m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
164 verify_noerr( err );
165 }
166 else
167 #endif
168 {
169 wxClientDC dc(const_cast<wxButton*>(this));
170 wxCoord width, height ;
171 dc.GetTextExtent( m_label , &width, &height);
172 bounds.h = width;
173 bounds.v = height;
174 }
175
176 wBtn = bounds.h + sz.y;
177 }
178 else
179 {
180 wBtn = bestsize.right - bestsize.left ;
181 // non 'normal' window variants don't return the correct height
182 // sz.y = bestsize.bottom - bestsize.top ;
183 }
184
185 if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
186 sz.x = wBtn;
187
188 return sz ;
189 }
190
191 wxSize wxButton::GetDefaultSize()
192 {
193 int wBtn = 70 ;
194 int hBtn = 20 ;
195
196 return wxSize(wBtn, hBtn);
197 }
198
199 void wxButton::Command (wxCommandEvent & event)
200 {
201 m_peer->Flash(kControlButtonPart) ;
202 ProcessCommand(event);
203 }
204
205 wxInt32 wxButton::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
206 {
207 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId);
208 event.SetEventObject(this);
209 ProcessCommand(event);
210
211 return noErr;
212 }
213
214 //-------------------------------------------------------
215 // wxDisclosureTriangle
216 //-------------------------------------------------------
217
218 bool wxDisclosureTriangle::Create(wxWindow *parent, wxWindowID id, const wxString& label,
219 const wxPoint& pos, const wxSize& size, long style,const wxValidator& validator, const wxString& name )
220 {
221 m_macIsUserPane = false ;
222
223 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
224 return false;
225
226 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
227 m_peer = new wxMacControl(this) ;
228
229 OSStatus err = CreateDisclosureTriangleControl(
230 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds,
231 kControlDisclosureTrianglePointDefault,
232 wxCFStringRef( label ),
233 0, // closed
234 TRUE, // draw title
235 TRUE, // auto toggle back and forth
236 m_peer->GetControlRefAddr() );
237
238 verify_noerr( err );
239 wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid Mac control") ) ;
240
241 MacPostControlCreate( pos, size );
242 // passing the text in the param doesn't seem to work, so lets do if again
243 SetLabel( label );
244
245 return true;
246 }
247
248 void wxDisclosureTriangle::SetOpen( bool open )
249 {
250 m_peer->SetValue( open ? 1 : 0 );
251 }
252
253 bool wxDisclosureTriangle::IsOpen() const
254 {
255 return m_peer->GetValue() == 1;
256 }
257
258 wxInt32 wxDisclosureTriangle::MacControlHit( WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
259 {
260 // Just emit button event for now
261 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId);
262 event.SetEventObject(this);
263 ProcessCommand(event);
264
265 return noErr;
266 }
267
268 wxSize wxDisclosureTriangle::DoGetBestSize() const
269 {
270 return wxSize(16,16);
271 }
272
273