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 extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
135 // set bezel style depending on the wxBORDER_XXX flags specified by the style
136 void SetBezelStyleFromBorderFlags(NSButton *v, long style)
138 if ( style & wxBORDER_NONE )
140 [v setBezelStyle:NSShadowlessSquareBezelStyle];
143 else // we do have a border
145 // see trac #11128 for a thorough discussion
146 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
147 [v setBezelStyle:NSRegularSquareBezelStyle];
148 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
149 [v setBezelStyle:NSSmallSquareBezelStyle];
151 [v setBezelStyle:NSShadowlessSquareBezelStyle];
155 } // anonymous namespace
157 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
158 wxWindowMac* WXUNUSED(parent),
160 const wxString& WXUNUSED(label),
163 long WXUNUSED(style),
164 long WXUNUSED(extraStyle))
166 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
167 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
169 if ( id == wxID_HELP )
171 [v setBezelStyle:NSHelpButtonBezelStyle];
175 [v setBezelStyle:NSRoundedBezelStyle];
178 [v setButtonType:NSMomentaryPushInButton];
179 return new wxButtonCocoaImpl( wxpeer, v );
182 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
184 if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
185 // NOTE: setKeyEquivalent: nil will trigger an assert
186 // instead do not call in that case.
187 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
190 void wxWidgetCocoaImpl::PerformClick()
192 if ([m_osxView isKindOfClass:[NSControl class]])
193 [(NSControl*)m_osxView performClick:nil];
198 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
199 wxWindowMac* WXUNUSED(parent),
200 wxWindowID WXUNUSED(id),
201 const wxBitmap& bitmap,
205 long WXUNUSED(extraStyle))
207 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
208 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
210 SetBezelStyleFromBorderFlags(v, style);
213 [v setImage:bitmap.GetNSImage() ];
215 [v setButtonType:NSMomentaryPushInButton];
216 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
220 #endif // wxUSE_BMPBUTTON
223 // wxDisclosureButton implementation
226 @interface wxDisclosureNSButton : NSButton
232 - (void) updateImage;
236 + (NSImage *)rotateImage: (NSImage *)image;
240 static const char * disc_triangle_xpm[] = {
257 @implementation wxDisclosureNSButton
261 static BOOL initialized = NO;
265 wxOSXCocoaClassAddWXMethods( self );
269 - (id) initWithFrame:(NSRect) frame
271 self = [super initWithFrame:frame];
273 [self setImagePosition:NSImageLeft];
280 return isOpen ? 1 : 0;
283 - (void) setIntValue: (int) v
295 wxCFRef<NSImage*> downArray ;
299 static wxBitmap trianglebm(disc_triangle_xpm);
300 if ( downArray.get() == NULL )
302 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
306 [self setImage:(NSImage*)downArray.get()];
308 [self setImage:trianglebm.GetNSImage()];
311 + (NSImage *)rotateImage: (NSImage *)image
313 NSSize imageSize = [image size];
314 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
315 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
317 [newImage lockFocus];
319 NSAffineTransform* tm = [NSAffineTransform transform];
320 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
321 [tm rotateByDegrees:-90];
322 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
326 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
327 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
329 [newImage unlockFocus];
335 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
338 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
339 wxWidgetCocoaImpl(peer, w)
343 ~wxDisclosureTriangleCocoaImpl()
347 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
349 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
351 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
355 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
356 wxWindowMac* WXUNUSED(parent),
357 wxWindowID WXUNUSED(winid),
358 const wxString& label,
362 long WXUNUSED(extraStyle))
364 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
365 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
366 if ( !label.empty() )
367 [v setTitle:wxCFStringRef(label).AsNSString()];
369 SetBezelStyleFromBorderFlags(v, style);
371 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );