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"
19 #include "wx/osx/private.h"
21 wxSize wxButton::DoGetBestSize() const
23 if ( GetId() == wxID_HELP )
24 return wxSize( 23 , 23 ) ;
27 m_peer->GetBestRect(&r);
29 wxSize sz = r.GetSize();
31 const int wBtnStd = GetDefaultSize().x;
33 if ( (sz.x < wBtnStd) && !HasFlag(wxBU_EXACTFIT) )
39 wxSize wxButton::GetDefaultSize()
41 return wxSize(84, 23);
44 @implementation wxNSButton
48 static BOOL initialized = NO;
52 wxOSXCocoaClassAddWXMethods( self );
58 switch ( [self state] )
69 - (void) setIntValue: (int) v
74 [self setState:NSMixedState];
77 [self setState:NSOnState];
80 [self setState:NSOffState];
85 - (void) setTrackingTag: (NSTrackingRectTag)tag
90 - (NSTrackingRectTag) trackingTag
100 class wxButtonCocoaImpl : public wxWidgetCocoaImpl, public wxButtonImpl
103 wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
104 : wxWidgetCocoaImpl(wxpeer, v)
108 virtual void SetBitmap(const wxBitmap& bitmap)
110 // switch bezel style for plain pushbuttons
111 if ( bitmap.IsOk() && [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
112 [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle ];
114 wxWidgetCocoaImpl::SetBitmap(bitmap);
117 void SetPressedBitmap( const wxBitmap& bitmap )
119 NSButton* button = GetNSButton();
120 [button setAlternateImage: bitmap.GetNSImage()];
121 [button setButtonType:NSMomentaryChangeButton];
125 NSButton *GetNSButton() const
127 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
129 return static_cast<NSButton *>(m_osxView);
133 } // anonymous namespace
135 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
136 wxWindowMac* WXUNUSED(parent),
138 const wxString& WXUNUSED(label),
141 long WXUNUSED(style),
142 long WXUNUSED(extraStyle))
144 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
145 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
147 if ( id == wxID_HELP )
149 [v setBezelStyle:NSHelpButtonBezelStyle];
153 [v setBezelStyle:NSRoundedBezelStyle];
156 [v setButtonType:NSMomentaryPushInButton];
157 return new wxButtonCocoaImpl( wxpeer, v );
160 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
162 if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
163 // NOTE: setKeyEquivalent: nil will trigger an assert
164 // instead do not call in that case.
165 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
168 void wxWidgetCocoaImpl::PerformClick()
170 if ([m_osxView isKindOfClass:[NSControl class]])
171 [(NSControl*)m_osxView performClick:nil];
176 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
177 wxWindowMac* WXUNUSED(parent),
178 wxWindowID WXUNUSED(id),
179 const wxBitmap& bitmap,
183 long WXUNUSED(extraStyle))
185 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
186 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
188 // trying to get as close as possible to flags
189 if ( style & wxBORDER_NONE )
191 [v setBezelStyle:NSShadowlessSquareBezelStyle];
196 // see trac #11128 for a thorough discussion
197 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
198 [v setBezelStyle:NSRegularSquareBezelStyle];
199 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
200 [v setBezelStyle:NSSmallSquareBezelStyle];
202 [v setBezelStyle:NSShadowlessSquareBezelStyle];
206 [v setImage:bitmap.GetNSImage() ];
208 [v setButtonType:NSMomentaryPushInButton];
209 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
216 // wxDisclosureButton implementation
219 @interface wxDisclosureNSButton : NSButton
225 - (void) updateImage;
229 + (NSImage *)rotateImage: (NSImage *)image;
233 static const char * disc_triangle_xpm[] = {
250 @implementation wxDisclosureNSButton
254 static BOOL initialized = NO;
258 wxOSXCocoaClassAddWXMethods( self );
262 - (id) initWithFrame:(NSRect) frame
264 self = [super initWithFrame:frame];
265 [self setBezelStyle:NSSmallSquareBezelStyle];
267 [self setImagePosition:NSImageLeft];
274 return isOpen ? 1 : 0;
277 - (void) setIntValue: (int) v
289 wxCFRef<NSImage*> downArray ;
293 static wxBitmap trianglebm(disc_triangle_xpm);
294 if ( downArray.get() == NULL )
296 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
300 [self setImage:(NSImage*)downArray.get()];
302 [self setImage:trianglebm.GetNSImage()];
305 + (NSImage *)rotateImage: (NSImage *)image
307 NSSize imageSize = [image size];
308 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
309 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
311 [newImage lockFocus];
313 NSAffineTransform* tm = [NSAffineTransform transform];
314 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
315 [tm rotateByDegrees:-90];
316 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
320 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
321 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
323 [newImage unlockFocus];
329 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
332 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
333 wxWidgetCocoaImpl(peer, w)
337 ~wxDisclosureTriangleCocoaImpl()
341 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
343 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
345 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
349 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
350 wxWindowMac* WXUNUSED(parent),
351 wxWindowID WXUNUSED(id),
352 const wxString& label,
355 long WXUNUSED(style),
356 long WXUNUSED(extraStyle))
358 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
359 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
360 [v setTitle:wxCFStringRef( label).AsNSString()];
361 wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v );