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()
155 // wxDisclosureButton implementation
158 @interface wxDisclosureNSButton : NSButton
164 - (void) updateImage;
168 + (NSImage *)rotateImage: (NSImage *)image;
172 static const char * disc_triangle_xpm[] = {
189 @implementation wxDisclosureNSButton
193 static BOOL initialized = NO;
197 wxOSXCocoaClassAddWXMethods( self );
201 - (id) initWithFrame:(NSRect) frame
203 self = [super initWithFrame:frame];
204 [self setBezelStyle:NSSmallSquareBezelStyle];
206 [self setImagePosition:NSImageLeft];
213 return isOpen ? 1 : 0;
216 - (void) setIntValue: (int) v
228 wxCFRef<NSImage*> downArray ;
232 static wxBitmap trianglebm(disc_triangle_xpm);
233 if ( downArray.get() == NULL )
235 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
239 [self setImage:(NSImage*)downArray.get()];
241 [self setImage:trianglebm.GetNSImage()];
244 + (NSImage *)rotateImage: (NSImage *)image
246 NSSize imageSize = [image size];
247 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
248 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
250 [newImage lockFocus];
252 NSAffineTransform* tm = [NSAffineTransform transform];
253 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
254 [tm rotateByDegrees:-90];
255 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
259 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
260 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
262 [newImage unlockFocus];
268 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
271 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
272 wxWidgetCocoaImpl(peer, w)
276 ~wxDisclosureTriangleCocoaImpl()
280 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
282 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
284 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
288 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
289 wxWindowMac* WXUNUSED(parent),
290 wxWindowID WXUNUSED(id),
291 const wxString& label,
294 long WXUNUSED(style),
295 long WXUNUSED(extraStyle))
297 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
298 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
299 [v setTitle:wxCFStringRef( label).AsNSString()];
300 wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v );