]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/button.cpp
speeding up rectangle drawing by using specific methods, needs 40% less time
[wxWidgets.git] / src / osx / carbon / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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/osx/private.h"
25
26 //
27 //
28 //
29
30 wxSize wxButton::DoGetBestSize() const
31 {
32 if ( GetId() == wxID_HELP )
33 return wxSize( 20 , 20 ) ;
34
35 wxSize sz = GetDefaultSize() ;
36
37 switch (GetWindowVariant())
38 {
39 case wxWINDOW_VARIANT_NORMAL:
40 case wxWINDOW_VARIANT_LARGE:
41 sz.y = 20 ;
42 break;
43
44 case wxWINDOW_VARIANT_SMALL:
45 sz.y = 17 ;
46 break;
47
48 case wxWINDOW_VARIANT_MINI:
49 sz.y = 15 ;
50 break;
51
52 default:
53 break;
54 }
55
56 #if wxOSX_USE_CARBON
57 Rect bestsize = { 0 , 0 , 0 , 0 } ;
58 GetPeer()->GetBestRect( &bestsize ) ;
59
60 int wBtn;
61 if ( EmptyRect( &bestsize ) || ( GetWindowStyle() & wxBU_EXACTFIT) )
62 {
63 Point bounds;
64
65 ControlFontStyleRec controlFont;
66 OSStatus err = GetPeer()->GetData<ControlFontStyleRec>( kControlEntireControl, kControlFontStyleTag, &controlFont );
67 verify_noerr( err );
68
69 // GetThemeTextDimensions will cache strings and the documentation
70 // says not to use the NoCopy string creation calls.
71 // This also means that we can't use CFSTR without
72 // -fno-constant-cfstrings if the library might be unloaded,
73 // as GetThemeTextDimensions may cache a pointer to our
74 // unloaded segment.
75 wxCFStringRef str( !m_label.empty() ? m_label : wxString(" "),
76 GetFont().GetEncoding() );
77
78 #if wxOSX_USE_ATSU_TEXT
79 SInt16 baseline;
80 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
81 {
82 err = GetThemeTextDimensions(
83 (CFStringRef)str,
84 m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
85 verify_noerr( err );
86 }
87 else
88 #endif
89 {
90 wxClientDC dc(const_cast<wxButton*>(this));
91 wxCoord width, height ;
92 dc.GetTextExtent( m_label , &width, &height);
93 bounds.h = width;
94 bounds.v = height;
95 }
96
97 wBtn = bounds.h + sz.y;
98 }
99 else
100 {
101 wBtn = bestsize.right - bestsize.left ;
102 // non 'normal' window variants don't return the correct height
103 // sz.y = bestsize.bottom - bestsize.top ;
104 }
105 if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
106 sz.x = wBtn;
107 #endif
108
109 return sz ;
110 }
111
112 wxSize wxButton::GetDefaultSize()
113 {
114 int wBtn = 70 ;
115 int hBtn = 20 ;
116
117 return wxSize(wBtn, hBtn);
118 }
119
120 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
121 wxWindowMac* parent,
122 wxWindowID id,
123 const wxString& label,
124 const wxPoint& pos,
125 const wxSize& size,
126 long WXUNUSED(style),
127 long WXUNUSED(extraStyle))
128 {
129 OSStatus err;
130 Rect bounds = wxMacGetBoundsForControl( wxpeer , pos , size ) ;
131 wxMacControl* peer = new wxMacControl(wxpeer) ;
132 if ( id == wxID_HELP )
133 {
134 ControlButtonContentInfo info ;
135 info.contentType = kControlContentIconRef ;
136 GetIconRef(kOnSystemDisk, kSystemIconsCreator, kHelpIcon, &info.u.iconRef);
137 err = CreateRoundButtonControl(
138 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
139 &bounds, kControlRoundButtonNormalSize,
140 &info, peer->GetControlRefAddr() );
141 }
142 else if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
143 {
144 // Button height is static in Mac, can't be changed, so we need to force it here
145 int maxHeight;
146 switch (wxpeer->GetWindowVariant() )
147 {
148 default:
149 wxFAIL_MSG( "unknown window variant" );
150 // fall through
151
152 case wxWINDOW_VARIANT_NORMAL:
153 case wxWINDOW_VARIANT_LARGE:
154 maxHeight = 20 ;
155 break;
156 case wxWINDOW_VARIANT_SMALL:
157 maxHeight = 17;
158 break;
159 case wxWINDOW_VARIANT_MINI:
160 maxHeight = 15;
161 }
162 bounds.bottom = bounds.top + maxHeight ;
163 wxpeer->SetMaxSize( wxSize( wxpeer->GetMaxWidth() , maxHeight ));
164 err = CreatePushButtonControl(
165 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
166 &bounds, CFSTR(""), peer->GetControlRefAddr() );
167 }
168 else
169 {
170 ControlButtonContentInfo info ;
171 info.contentType = kControlNoContent ;
172 err = CreateBevelButtonControl(
173 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds, CFSTR(""),
174 kControlBevelButtonLargeBevel, kControlBehaviorPushbutton,
175 &info, 0, 0, 0, peer->GetControlRefAddr() );
176 }
177 verify_noerr( err );
178 return peer;
179 }
180
181 void wxMacControl::SetDefaultButton( bool isDefault )
182 {
183 SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) isDefault ) ;
184 }
185
186 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
187 wxWindowMac* parent,
188 wxWindowID WXUNUSED(id),
189 const wxString& label,
190 const wxPoint& pos,
191 const wxSize& size,
192 long WXUNUSED(style),
193 long WXUNUSED(extraStyle))
194 {
195 Rect bounds = wxMacGetBoundsForControl( wxpeer , pos , size ) ;
196 wxMacControl* peer = new wxMacControl(wxpeer) ;
197
198 OSStatus err = CreateDisclosureTriangleControl(
199 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds,
200 kControlDisclosureTrianglePointDefault,
201 wxCFStringRef( label ),
202 0, // closed
203 TRUE, // draw title
204 TRUE, // auto toggle back and forth
205 peer->GetControlRefAddr() );
206
207 verify_noerr( err );
208 return peer;
209 }
210
211