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];
170 class wxButtonCocoaImpl : public wxWidgetCocoaImpl
173 wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
174 : wxWidgetCocoaImpl(wxpeer, v)
178 virtual void SetBitmap(const wxBitmap& bitmap)
180 [GetNSButton() setBezelStyle:bitmap.IsOk() ? NSRegularSquareBezelStyle
181 : NSRoundedBezelStyle];
183 wxWidgetCocoaImpl::SetBitmap(bitmap);
187 NSButton *GetNSButton() const
189 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
191 return static_cast<NSButton *>(m_osxView);
195 } // anonymous namespace
197 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
198 wxWindowMac* WXUNUSED(parent),
200 const wxString& WXUNUSED(label),
203 long WXUNUSED(style),
204 long WXUNUSED(extraStyle))
206 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
207 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
209 if ( id == wxID_HELP )
211 [v setBezelStyle:NSHelpButtonBezelStyle];
215 [v setBezelStyle:NSRoundedBezelStyle];
218 [v setButtonType:NSMomentaryPushInButton];
219 return new wxButtonCocoaImpl( wxpeer, v );
222 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
224 if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
225 // NOTE: setKeyEquivalent: nil will trigger an assert
226 // instead do not call in that case.
227 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
230 void wxWidgetCocoaImpl::PerformClick()
235 // wxDisclosureButton implementation
238 @interface wxDisclosureNSButton : NSButton
244 - (void) updateImage;
248 + (NSImage *)rotateImage: (NSImage *)image;
252 static const char * disc_triangle_xpm[] = {
269 @implementation wxDisclosureNSButton
273 static BOOL initialized = NO;
277 wxOSXCocoaClassAddWXMethods( self );
281 - (id) initWithFrame:(NSRect) frame
283 self = [super initWithFrame:frame];
284 [self setBezelStyle:NSSmallSquareBezelStyle];
286 [self setImagePosition:NSImageLeft];
293 return isOpen ? 1 : 0;
296 - (void) setIntValue: (int) v
308 wxCFRef<NSImage*> downArray ;
312 static wxBitmap trianglebm(disc_triangle_xpm);
313 if ( downArray.get() == NULL )
315 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
319 [self setImage:(NSImage*)downArray.get()];
321 [self setImage:trianglebm.GetNSImage()];
324 + (NSImage *)rotateImage: (NSImage *)image
326 NSSize imageSize = [image size];
327 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
328 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
330 [newImage lockFocus];
332 NSAffineTransform* tm = [NSAffineTransform transform];
333 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
334 [tm rotateByDegrees:-90];
335 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
339 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
340 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
342 [newImage unlockFocus];
348 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
351 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
352 wxWidgetCocoaImpl(peer, w)
356 ~wxDisclosureTriangleCocoaImpl()
360 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
362 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
364 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
368 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
369 wxWindowMac* WXUNUSED(parent),
370 wxWindowID WXUNUSED(id),
371 const wxString& label,
374 long WXUNUSED(style),
375 long WXUNUSED(extraStyle))
377 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
378 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
379 [v setTitle:wxCFStringRef( label).AsNSString()];
380 wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v );