]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/button.cpp
speeding up rectangle drawing by using specific methods, needs 40% less time
[wxWidgets.git] / src / osx / carbon / button.cpp
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
524c47aa 2// Name: src/osx/carbon/button.cpp
489468fe
SC
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
524c47aa 24#include "wx/osx/private.h"
489468fe 25
524c47aa
SC
26//
27//
28//
489468fe
SC
29
30wxSize 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
524c47aa 56#if wxOSX_USE_CARBON
489468fe 57 Rect bestsize = { 0 , 0 , 0 , 0 } ;
22756322 58 GetPeer()->GetBestRect( &bestsize ) ;
489468fe
SC
59
60 int wBtn;
61 if ( EmptyRect( &bestsize ) || ( GetWindowStyle() & wxBU_EXACTFIT) )
62 {
63 Point bounds;
64
65 ControlFontStyleRec controlFont;
22756322 66 OSStatus err = GetPeer()->GetData<ControlFontStyleRec>( kControlEntireControl, kControlFontStyleTag, &controlFont );
489468fe
SC
67 verify_noerr( err );
68
03647350
VZ
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(" "),
6fd21e16 76 GetFont().GetEncoding() );
489468fe 77
292e5e1f 78#if wxOSX_USE_ATSU_TEXT
489468fe
SC
79 SInt16 baseline;
80 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
81 {
82 err = GetThemeTextDimensions(
6fd21e16 83 (CFStringRef)str,
489468fe
SC
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 }
489468fe
SC
105 if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
106 sz.x = wBtn;
524c47aa 107#endif
489468fe
SC
108
109 return sz ;
110}
111
112wxSize wxButton::GetDefaultSize()
113{
114 int wBtn = 70 ;
115 int hBtn = 20 ;
116
117 return wxSize(wBtn, hBtn);
118}
119
ca910e1a
VZ
120wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
121 wxWindowMac* parent,
122 wxWindowID id,
524c47aa 123 const wxString& label,
ca910e1a 124 const wxPoint& pos,
524c47aa 125 const wxSize& size,
ca910e1a
VZ
126 long WXUNUSED(style),
127 long WXUNUSED(extraStyle))
489468fe 128{
524c47aa
SC
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;
ca910e1a 146 switch (wxpeer->GetWindowVariant() )
524c47aa 147 {
ca910e1a
VZ
148 default:
149 wxFAIL_MSG( "unknown window variant" );
150 // fall through
151
524c47aa
SC
152 case wxWINDOW_VARIANT_NORMAL:
153 case wxWINDOW_VARIANT_LARGE:
154 maxHeight = 20 ;
155 break;
156 case wxWINDOW_VARIANT_SMALL:
157 maxHeight = 17;
af8dcb5e 158 break;
524c47aa
SC
159 case wxWINDOW_VARIANT_MINI:
160 maxHeight = 15;
524c47aa
SC
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;
489468fe
SC
179}
180
524c47aa 181void wxMacControl::SetDefaultButton( bool isDefault )
ca910e1a 182{
524c47aa 183 SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) isDefault ) ;
489468fe
SC
184}
185
ca910e1a
VZ
186wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
187 wxWindowMac* parent,
188 wxWindowID WXUNUSED(id),
524c47aa 189 const wxString& label,
ca910e1a 190 const wxPoint& pos,
524c47aa 191 const wxSize& size,
ca910e1a
VZ
192 long WXUNUSED(style),
193 long WXUNUSED(extraStyle))
489468fe 194{
524c47aa
SC
195 Rect bounds = wxMacGetBoundsForControl( wxpeer , pos , size ) ;
196 wxMacControl* peer = new wxMacControl(wxpeer) ;
489468fe
SC
197
198 OSStatus err = CreateDisclosureTriangleControl(
ca910e1a 199 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds,
489468fe
SC
200 kControlDisclosureTrianglePointDefault,
201 wxCFStringRef( label ),
202 0, // closed
203 TRUE, // draw title
204 TRUE, // auto toggle back and forth
524c47aa 205 peer->GetControlRefAddr() );
ca910e1a 206
489468fe 207 verify_noerr( err );
524c47aa 208 return peer;
489468fe 209}
ca910e1a 210
489468fe 211