]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/button.mm
add parentheses for && inside || to fix g++ 4.3 warning
[wxWidgets.git] / src / osx / cocoa / button.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/button.mm
3 // Purpose: wxButton
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id: button.cpp 54845 2008-07-30 14:52:41Z SC $
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 wxSize wxButton::DoGetBestSize() const
27 {
28 if ( GetId() == wxID_HELP )
29 return wxSize( 20 , 20 ) ;
30
31 wxSize sz = GetDefaultSize() ;
32
33 switch (GetWindowVariant())
34 {
35 case wxWINDOW_VARIANT_NORMAL:
36 case wxWINDOW_VARIANT_LARGE:
37 sz.y = 20 ;
38 break;
39
40 case wxWINDOW_VARIANT_SMALL:
41 sz.y = 17 ;
42 break;
43
44 case wxWINDOW_VARIANT_MINI:
45 sz.y = 15 ;
46 break;
47
48 default:
49 break;
50 }
51
52 #if wxOSX_USE_CARBON
53 Rect bestsize = { 0 , 0 , 0 , 0 } ;
54 m_peer->GetBestRect( &bestsize ) ;
55
56 int wBtn;
57 if ( EmptyRect( &bestsize ) || ( GetWindowStyle() & wxBU_EXACTFIT) )
58 {
59 Point bounds;
60
61 ControlFontStyleRec controlFont;
62 OSStatus err = m_peer->GetData<ControlFontStyleRec>( kControlEntireControl, kControlFontStyleTag, &controlFont );
63 verify_noerr( err );
64
65 wxCFStringRef str( m_label, GetFont().GetEncoding() );
66
67 #if wxOSX_USE_ATSU_TEXT
68 SInt16 baseline;
69 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
70 {
71 err = GetThemeTextDimensions(
72 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
73 m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
74 verify_noerr( err );
75 }
76 else
77 #endif
78 {
79 wxClientDC dc(const_cast<wxButton*>(this));
80 wxCoord width, height ;
81 dc.GetTextExtent( m_label , &width, &height);
82 bounds.h = width;
83 bounds.v = height;
84 }
85
86 wBtn = bounds.h + sz.y;
87 }
88 else
89 {
90 wBtn = bestsize.right - bestsize.left ;
91 // non 'normal' window variants don't return the correct height
92 // sz.y = bestsize.bottom - bestsize.top ;
93 }
94 if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
95 sz.x = wBtn;
96 #endif
97
98 return sz ;
99 }
100
101 wxSize wxButton::GetDefaultSize()
102 {
103 int wBtn = 70 ;
104 int hBtn = 20 ;
105
106 return wxSize(wBtn, hBtn);
107 }
108
109 @implementation wxNSButton
110
111 - (id)initWithFrame:(NSRect)frame
112 {
113 [super initWithFrame:frame];
114 m_impl = NULL;
115 [self setTarget: self];
116 [self setAction: @selector(clickedAction:)];
117 return self;
118 }
119
120 - (void) clickedAction: (id) sender
121 {
122 if ( m_impl )
123 {
124 wxButton* wxpeer = (wxButton*) m_impl->GetWXPeer();
125 if ( wxpeer )
126 wxpeer->HandleClicked(0);
127 }
128 }
129
130 - (void)setImplementation: (wxWidgetImpl *) theImplementation
131 {
132 m_impl = theImplementation;
133 }
134
135 - (wxWidgetImpl*) implementation
136 {
137 return m_impl;
138 }
139
140 - (BOOL) isFlipped
141 {
142 return YES;
143 }
144
145 - (int) intValue
146 {
147 switch ( [self state] )
148 {
149 case NSOnState:
150 return 1;
151 case NSMixedState:
152 return 2;
153 default:
154 return 0;
155 }
156 }
157
158 - (void) setIntValue: (int) v
159 {
160 switch( v )
161 {
162 case 2:
163 [self setState:NSMixedState];
164 break;
165 case 1:
166 [self setState:NSOnState];
167 break;
168 default :
169 [self setState:NSOffState];
170 break;
171 }
172 }
173
174 @end
175
176
177 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
178 wxWindowMac* parent,
179 wxWindowID id,
180 const wxString& label,
181 const wxPoint& pos,
182 const wxSize& size,
183 long style,
184 long extraStyle)
185 {
186 NSView* sv = (wxpeer->GetParent()->GetHandle() );
187
188 NSRect r = wxToNSRect( sv, wxRect( pos, size) );
189 // Rect bounds = wxMacGetBoundsForControl( wxpeer, pos , size ) ;
190 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
191
192 if ( id == wxID_HELP )
193 {
194 [v setBezelStyle:NSHelpButtonBezelStyle];
195 }
196 else
197 {
198 [v setBezelStyle:NSRoundedBezelStyle];
199 }
200
201 [v setButtonType:NSMomentaryPushInButton];
202 [sv addSubview:v];
203 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
204 [v setImplementation:c];
205 return c;
206 /*
207 OSStatus err;
208 Rect bounds = wxMacGetBoundsForControl( wxpeer , pos , size ) ;
209 wxMacControl* peer = new wxMacControl(wxpeer) ;
210 if ( id == wxID_HELP )
211 {
212 ControlButtonContentInfo info ;
213 info.contentType = kControlContentIconRef ;
214 GetIconRef(kOnSystemDisk, kSystemIconsCreator, kHelpIcon, &info.u.iconRef);
215 err = CreateRoundButtonControl(
216 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
217 &bounds, kControlRoundButtonNormalSize,
218 &info, peer->GetControlRefAddr() );
219 }
220 else if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
221 {
222 // Button height is static in Mac, can't be changed, so we need to force it here
223 int maxHeight;
224 switch (wxpeer->GetWindowVariant() )
225 {
226 case wxWINDOW_VARIANT_NORMAL:
227 case wxWINDOW_VARIANT_LARGE:
228 maxHeight = 20 ;
229 break;
230 case wxWINDOW_VARIANT_SMALL:
231 maxHeight = 17;
232 case wxWINDOW_VARIANT_MINI:
233 maxHeight = 15;
234 default:
235 break;
236 }
237 bounds.bottom = bounds.top + maxHeight ;
238 wxpeer->SetMaxSize( wxSize( wxpeer->GetMaxWidth() , maxHeight ));
239 err = CreatePushButtonControl(
240 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
241 &bounds, CFSTR(""), peer->GetControlRefAddr() );
242 }
243 else
244 {
245 ControlButtonContentInfo info ;
246 info.contentType = kControlNoContent ;
247 err = CreateBevelButtonControl(
248 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds, CFSTR(""),
249 kControlBevelButtonLargeBevel, kControlBehaviorPushbutton,
250 &info, 0, 0, 0, peer->GetControlRefAddr() );
251 }
252 verify_noerr( err );
253 return peer;
254 */
255 }
256
257 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
258 {
259 [m_osxView setKeyEquivalent: isDefault ? @"\r" : nil ];
260 // SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) isDefault ) ;
261 }
262
263 void wxWidgetCocoaImpl::PerformClick()
264 {
265 }
266
267 // TODO for the disclosure button : NSDisclosureBezelStyle and the button type to NSOnOffButton.