1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/button.mm
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: button.cpp 54845 2008-07-30 14:52:41Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/button.h"
18 #include "wx/toplevel.h"
19 #include "wx/dcclient.h"
22 #include "wx/stockitem.h"
24 #include "wx/osx/private.h"
26 wxSize wxButton::DoGetBestSize() const
28 if ( GetId() == wxID_HELP )
29 return wxSize( 23 , 23 ) ;
31 wxSize sz = GetDefaultSize() ;
33 switch (GetWindowVariant())
35 case wxWINDOW_VARIANT_NORMAL:
36 case wxWINDOW_VARIANT_LARGE:
40 case wxWINDOW_VARIANT_SMALL:
44 case wxWINDOW_VARIANT_MINI:
54 m_peer->GetBestRect(&r);
56 if ( r.GetWidth() == 0 && r.GetHeight() == 0 )
64 if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
68 Rect bestsize = { 0 , 0 , 0 , 0 } ;
69 m_peer->GetBestRect( &bestsize ) ;
72 if ( EmptyRect( &bestsize ) || ( GetWindowStyle() & wxBU_EXACTFIT) )
76 ControlFontStyleRec controlFont;
77 OSStatus err = m_peer->GetData<ControlFontStyleRec>( kControlEntireControl, kControlFontStyleTag, &controlFont );
80 wxCFStringRef str( m_label, GetFont().GetEncoding() );
82 #if wxOSX_USE_ATSU_TEXT
84 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
86 err = GetThemeTextDimensions(
87 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
88 m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
94 wxClientDC dc(const_cast<wxButton*>(this));
95 wxCoord width, height ;
96 dc.GetTextExtent( m_label , &width, &height);
101 wBtn = bounds.h + sz.y;
105 wBtn = bestsize.right - bestsize.left ;
106 // non 'normal' window variants don't return the correct height
107 // sz.y = bestsize.bottom - bestsize.top ;
109 if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
116 wxSize wxButton::GetDefaultSize()
121 return wxSize(wBtn, hBtn);
124 @implementation wxNSButton
128 static BOOL initialized = NO;
132 wxOSXCocoaClassAddWXMethods( self );
138 switch ( [self state] )
149 - (void) setIntValue: (int) v
154 [self setState:NSMixedState];
157 [self setState:NSOnState];
160 [self setState:NSOffState];
168 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
169 wxWindowMac* WXUNUSED(parent),
171 const wxString& WXUNUSED(label),
174 long WXUNUSED(style),
175 long WXUNUSED(extraStyle))
177 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
178 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
180 if ( id == wxID_HELP )
182 [v setBezelStyle:NSHelpButtonBezelStyle];
186 [v setBezelStyle:NSRoundedBezelStyle];
189 [v setButtonType:NSMomentaryPushInButton];
190 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
194 Rect bounds = wxMacGetBoundsForControl( wxpeer , pos , size ) ;
195 wxMacControl* peer = new wxMacControl(wxpeer) ;
196 if ( id == wxID_HELP )
198 ControlButtonContentInfo info ;
199 info.contentType = kControlContentIconRef ;
200 GetIconRef(kOnSystemDisk, kSystemIconsCreator, kHelpIcon, &info.u.iconRef);
201 err = CreateRoundButtonControl(
202 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
203 &bounds, kControlRoundButtonNormalSize,
204 &info, peer->GetControlRefAddr() );
206 else if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
208 // Button height is static in Mac, can't be changed, so we need to force it here
210 switch (wxpeer->GetWindowVariant() )
212 case wxWINDOW_VARIANT_NORMAL:
213 case wxWINDOW_VARIANT_LARGE:
216 case wxWINDOW_VARIANT_SMALL:
218 case wxWINDOW_VARIANT_MINI:
223 bounds.bottom = bounds.top + maxHeight ;
224 wxpeer->SetMaxSize( wxSize( wxpeer->GetMaxWidth() , maxHeight ));
225 err = CreatePushButtonControl(
226 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
227 &bounds, CFSTR(""), peer->GetControlRefAddr() );
231 ControlButtonContentInfo info ;
232 info.contentType = kControlNoContent ;
233 err = CreateBevelButtonControl(
234 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds, CFSTR(""),
235 kControlBevelButtonLargeBevel, kControlBehaviorPushbutton,
236 &info, 0, 0, 0, peer->GetControlRefAddr() );
243 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
245 if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
246 // NOTE: setKeyEquivalent: nil will trigger an assert
247 // instead do not call in that case.
248 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
251 void wxWidgetCocoaImpl::PerformClick()
256 // wxDisclosureButton implementation
259 @interface wxDisclosureNSButton : NSButton
265 - (void) updateImage;
269 + (NSImage *)rotateImage: (NSImage *)image;
273 @implementation wxDisclosureNSButton
277 static BOOL initialized = NO;
281 wxOSXCocoaClassAddWXMethods( self );
285 - (id) initWithFrame:(NSRect) frame
287 self = [super initWithFrame:frame];
288 [self setBezelStyle:NSSmallSquareBezelStyle];
290 [self setImagePosition:NSImageLeft];
297 return isOpen ? 1 : 0;
300 - (void) setIntValue: (int) v
312 wxCFRef<NSImage*> downArray ;
316 if ( downArray.get() == NULL )
318 downArray.reset( [wxDisclosureNSButton rotateImage:[NSImage imageNamed:NSImageNameRightFacingTriangleTemplate]] );
322 [self setImage:(NSImage*)downArray.get()];
324 [self setImage:[NSImage imageNamed:NSImageNameRightFacingTriangleTemplate]];
327 + (NSImage *)rotateImage: (NSImage *)image
329 NSSize imageSize = [image size];
330 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
331 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
333 [newImage lockFocus];
335 NSAffineTransform* tm = [NSAffineTransform transform];
336 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
337 [tm rotateByDegrees:-90];
338 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
342 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
343 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
345 [newImage unlockFocus];
351 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
354 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
355 wxWidgetCocoaImpl(peer, w)
359 ~wxDisclosureTriangleCocoaImpl()
363 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
365 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
367 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
371 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
372 wxWindowMac* WXUNUSED(parent),
373 wxWindowID WXUNUSED(id),
374 const wxString& label,
377 long WXUNUSED(style),
378 long WXUNUSED(extraStyle))
380 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
381 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
382 [v setTitle:wxCFStringRef( label).AsNSString()];
383 wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v );