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 // set bezel style depending on the wxBORDER_XXX flags specified by the style
134 void SetBezelStyleFromBorderFlags(NSButton *v, long style)
136 if ( style & wxBORDER_NONE )
138 [v setBezelStyle:NSShadowlessSquareBezelStyle];
141 else // we do have a border
143 // see trac #11128 for a thorough discussion
144 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
145 [v setBezelStyle:NSRegularSquareBezelStyle];
146 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
147 [v setBezelStyle:NSSmallSquareBezelStyle];
149 [v setBezelStyle:NSShadowlessSquareBezelStyle];
153 } // anonymous namespace
155 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
156 wxWindowMac* WXUNUSED(parent),
158 const wxString& WXUNUSED(label),
161 long WXUNUSED(style),
162 long WXUNUSED(extraStyle))
164 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
165 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
167 if ( id == wxID_HELP )
169 [v setBezelStyle:NSHelpButtonBezelStyle];
173 [v setBezelStyle:NSRoundedBezelStyle];
176 [v setButtonType:NSMomentaryPushInButton];
177 return new wxButtonCocoaImpl( wxpeer, v );
180 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
182 if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
183 // NOTE: setKeyEquivalent: nil will trigger an assert
184 // instead do not call in that case.
185 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
188 void wxWidgetCocoaImpl::PerformClick()
190 if ([m_osxView isKindOfClass:[NSControl class]])
191 [(NSControl*)m_osxView performClick:nil];
196 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
197 wxWindowMac* WXUNUSED(parent),
198 wxWindowID WXUNUSED(id),
199 const wxBitmap& bitmap,
203 long WXUNUSED(extraStyle))
205 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
206 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
208 SetBezelStyleFromBorderFlags(v, style);
211 [v setImage:bitmap.GetNSImage() ];
213 [v setButtonType:NSMomentaryPushInButton];
214 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
218 #endif // wxUSE_BMPBUTTON
221 // wxDisclosureButton implementation
224 @interface wxDisclosureNSButton : NSButton
230 - (void) updateImage;
234 + (NSImage *)rotateImage: (NSImage *)image;
238 static const char * disc_triangle_xpm[] = {
255 @implementation wxDisclosureNSButton
259 static BOOL initialized = NO;
263 wxOSXCocoaClassAddWXMethods( self );
267 - (id) initWithFrame:(NSRect) frame
269 self = [super initWithFrame:frame];
271 [self setImagePosition:NSImageLeft];
278 return isOpen ? 1 : 0;
281 - (void) setIntValue: (int) v
293 wxCFRef<NSImage*> downArray ;
297 static wxBitmap trianglebm(disc_triangle_xpm);
298 if ( downArray.get() == NULL )
300 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
304 [self setImage:(NSImage*)downArray.get()];
306 [self setImage:trianglebm.GetNSImage()];
309 + (NSImage *)rotateImage: (NSImage *)image
311 NSSize imageSize = [image size];
312 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
313 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
315 [newImage lockFocus];
317 NSAffineTransform* tm = [NSAffineTransform transform];
318 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
319 [tm rotateByDegrees:-90];
320 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
324 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
325 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
327 [newImage unlockFocus];
333 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
336 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
337 wxWidgetCocoaImpl(peer, w)
341 ~wxDisclosureTriangleCocoaImpl()
345 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
347 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
349 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
353 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
354 wxWindowMac* WXUNUSED(parent),
355 wxWindowID WXUNUSED(winid),
356 const wxString& label,
360 long WXUNUSED(extraStyle))
362 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
363 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
364 if ( !label.empty() )
365 [v setTitle:wxCFStringRef(label).AsNSString()];
367 SetBezelStyleFromBorderFlags(v, style);
369 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );