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];
90 class wxButtonCocoaImpl : public wxWidgetCocoaImpl
93 wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
94 : wxWidgetCocoaImpl(wxpeer, v)
98 virtual void SetBitmap(const wxBitmap& bitmap)
100 [GetNSButton() setBezelStyle:bitmap.IsOk() ? NSRegularSquareBezelStyle
101 : NSRoundedBezelStyle];
103 wxWidgetCocoaImpl::SetBitmap(bitmap);
107 NSButton *GetNSButton() const
109 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
111 return static_cast<NSButton *>(m_osxView);
115 } // anonymous namespace
117 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
118 wxWindowMac* WXUNUSED(parent),
120 const wxString& WXUNUSED(label),
123 long WXUNUSED(style),
124 long WXUNUSED(extraStyle))
126 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
127 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
129 if ( id == wxID_HELP )
131 [v setBezelStyle:NSHelpButtonBezelStyle];
135 [v setBezelStyle:NSRoundedBezelStyle];
138 [v setButtonType:NSMomentaryPushInButton];
139 return new wxButtonCocoaImpl( wxpeer, v );
142 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
144 if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
145 // NOTE: setKeyEquivalent: nil will trigger an assert
146 // instead do not call in that case.
147 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
150 void wxWidgetCocoaImpl::PerformClick()
152 if ([m_osxView isKindOfClass:[NSControl class]])
153 [(NSControl*)m_osxView performClick:nil];
157 // wxDisclosureButton implementation
160 @interface wxDisclosureNSButton : NSButton
166 - (void) updateImage;
170 + (NSImage *)rotateImage: (NSImage *)image;
174 static const char * disc_triangle_xpm[] = {
191 @implementation wxDisclosureNSButton
195 static BOOL initialized = NO;
199 wxOSXCocoaClassAddWXMethods( self );
203 - (id) initWithFrame:(NSRect) frame
205 self = [super initWithFrame:frame];
206 [self setBezelStyle:NSSmallSquareBezelStyle];
208 [self setImagePosition:NSImageLeft];
215 return isOpen ? 1 : 0;
218 - (void) setIntValue: (int) v
230 wxCFRef<NSImage*> downArray ;
234 static wxBitmap trianglebm(disc_triangle_xpm);
235 if ( downArray.get() == NULL )
237 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
241 [self setImage:(NSImage*)downArray.get()];
243 [self setImage:trianglebm.GetNSImage()];
246 + (NSImage *)rotateImage: (NSImage *)image
248 NSSize imageSize = [image size];
249 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
250 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
252 [newImage lockFocus];
254 NSAffineTransform* tm = [NSAffineTransform transform];
255 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
256 [tm rotateByDegrees:-90];
257 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
261 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
262 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
264 [newImage unlockFocus];
270 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
273 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
274 wxWidgetCocoaImpl(peer, w)
278 ~wxDisclosureTriangleCocoaImpl()
282 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
284 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
286 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
290 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
291 wxWindowMac* WXUNUSED(parent),
292 wxWindowID WXUNUSED(id),
293 const wxString& label,
296 long WXUNUSED(style),
297 long WXUNUSED(extraStyle))
299 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
300 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
301 [v setTitle:wxCFStringRef( label).AsNSString()];
302 wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v );