]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/button.cpp
cleanup
[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 #if TARGET_API_MAC_OSX
65 // Button height is static in Mac, can't be changed, so we need to force it here
66 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL || GetWindowVariant() == wxWINDOW_VARIANT_LARGE )
67 {
68 bounds.bottom = bounds.top + 20 ;
69 m_maxHeight = 20 ;
70 }
71 else if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL )
72 {
73 bounds.bottom = bounds.top + 17 ;
74 m_maxHeight = 17 ;
75 }
76 else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI )
77 {
78 bounds.bottom = bounds.top + 15 ;
79 m_maxHeight = 15 ;
80 }
81 #endif
82
83 err = CreatePushButtonControl(
84 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
85 &bounds, CFSTR(""), m_peer->GetControlRefAddr() );
86 }
87 else
88 {
89 ControlButtonContentInfo info ;
90 info.contentType = kControlNoContent ;
91 err = CreateBevelButtonControl(
92 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds, CFSTR(""),
93 kControlBevelButtonLargeBevel, kControlBehaviorPushbutton,
94 &info, 0, 0, 0, m_peer->GetControlRefAddr() );
95 }
96
97 verify_noerr( err );
98 wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid Mac control") ) ;
99
100 MacPostControlCreate( pos, size );
101
102 return true;
103 }
104
105 wxWindow *wxButton::SetDefault()
106 {
107 wxWindow *btnOldDefault = wxButtonBase::SetDefault();
108
109 if ( btnOldDefault )
110 {
111 // cast needed to access the protected member
112 btnOldDefault->GetPeer()->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 0 ) ;
113 }
114
115 m_peer->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 1 ) ;
116
117 return btnOldDefault;
118 }
119
120 wxSize wxButton::DoGetBestSize() const
121 {
122 if ( GetId() == wxID_HELP )
123 return wxSize( 20 , 20 ) ;
124
125 wxSize sz = GetDefaultSize() ;
126
127 switch (GetWindowVariant())
128 {
129 case wxWINDOW_VARIANT_NORMAL:
130 case wxWINDOW_VARIANT_LARGE:
131 sz.y = 20 ;
132 break;
133
134 case wxWINDOW_VARIANT_SMALL:
135 sz.y = 17 ;
136 break;
137
138 case wxWINDOW_VARIANT_MINI:
139 sz.y = 15 ;
140 break;
141
142 default:
143 break;
144 }
145
146 Rect bestsize = { 0 , 0 , 0 , 0 } ;
147 m_peer->GetBestRect( &bestsize ) ;
148
149 int wBtn;
150 if ( EmptyRect( &bestsize ) || ( GetWindowStyle() & wxBU_EXACTFIT) )
151 {
152 Point bounds;
153
154 ControlFontStyleRec controlFont;
155 OSStatus err = m_peer->GetData<ControlFontStyleRec>( kControlEntireControl, kControlFontStyleTag, &controlFont );
156 verify_noerr( err );
157
158 SInt16 baseline;
159 wxMacCFStringHolder str( m_label, m_font.GetEncoding() );
160
161 #ifndef __LP64__
162 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
163 {
164 err = GetThemeTextDimensions(
165 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
166 m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
167 verify_noerr( err );
168 }
169 else
170 #endif
171 {
172 #if wxMAC_USE_CORE_GRAPHICS
173 wxClientDC dc(const_cast<wxButton*>(this));
174 wxCoord width, height ;
175 dc.GetTextExtent( m_label , &width, &height);
176 bounds.h = width;
177 bounds.v = height;
178 #else
179 wxMacWindowStateSaver sv( this );
180 ::TextFont( m_font.MacGetFontNum() );
181 ::TextSize( (short)(m_font.MacGetFontSize()) );
182 ::TextFace( m_font.MacGetFontStyle() );
183
184 err = GetThemeTextDimensions(
185 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
186 kThemeCurrentPortFont, kThemeStateActive, false, &bounds, &baseline );
187 verify_noerr( err );
188 #endif
189 }
190
191 wBtn = bounds.h + sz.y;
192 }
193 else
194 {
195 wBtn = bestsize.right - bestsize.left ;
196 // non 'normal' window variants don't return the correct height
197 // sz.y = bestsize.bottom - bestsize.top ;
198 }
199
200 if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
201 sz.x = wBtn;
202
203 return sz ;
204 }
205
206 wxSize wxButton::GetDefaultSize()
207 {
208 int wBtn = 70 ;
209 int hBtn = 20 ;
210
211 return wxSize(wBtn, hBtn);
212 }
213
214 void wxButton::Command (wxCommandEvent & event)
215 {
216 m_peer->Flash(kControlButtonPart) ;
217 ProcessCommand(event);
218 }
219
220 wxInt32 wxButton::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
221 {
222 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId);
223 event.SetEventObject(this);
224 ProcessCommand(event);
225
226 return noErr;
227 }