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
103 wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
104 : wxWidgetCocoaImpl(wxpeer, v)
108 virtual void SetBitmap(const wxBitmap& bitmap)
110 [GetNSButton() setBezelStyle:bitmap.IsOk() ? NSRegularSquareBezelStyle
111 : NSRoundedBezelStyle];
113 wxWidgetCocoaImpl::SetBitmap(bitmap);
117 NSButton *GetNSButton() const
119 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
121 return static_cast<NSButton *>(m_osxView);
125 } // anonymous namespace
127 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
128 wxWindowMac* WXUNUSED(parent),
130 const wxString& WXUNUSED(label),
133 long WXUNUSED(style),
134 long WXUNUSED(extraStyle))
136 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
137 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
139 if ( id == wxID_HELP )
141 [v setBezelStyle:NSHelpButtonBezelStyle];
145 [v setBezelStyle:NSRoundedBezelStyle];
148 [v setButtonType:NSMomentaryPushInButton];
149 return new wxButtonCocoaImpl( wxpeer, v );
152 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
154 if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
155 // NOTE: setKeyEquivalent: nil will trigger an assert
156 // instead do not call in that case.
157 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
160 void wxWidgetCocoaImpl::PerformClick()
162 if ([m_osxView isKindOfClass:[NSControl class]])
163 [(NSControl*)m_osxView performClick:nil];
167 // wxDisclosureButton implementation
170 @interface wxDisclosureNSButton : NSButton
176 - (void) updateImage;
180 + (NSImage *)rotateImage: (NSImage *)image;
184 static const char * disc_triangle_xpm[] = {
201 @implementation wxDisclosureNSButton
205 static BOOL initialized = NO;
209 wxOSXCocoaClassAddWXMethods( self );
213 - (id) initWithFrame:(NSRect) frame
215 self = [super initWithFrame:frame];
216 [self setBezelStyle:NSSmallSquareBezelStyle];
218 [self setImagePosition:NSImageLeft];
225 return isOpen ? 1 : 0;
228 - (void) setIntValue: (int) v
240 wxCFRef<NSImage*> downArray ;
244 static wxBitmap trianglebm(disc_triangle_xpm);
245 if ( downArray.get() == NULL )
247 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
251 [self setImage:(NSImage*)downArray.get()];
253 [self setImage:trianglebm.GetNSImage()];
256 + (NSImage *)rotateImage: (NSImage *)image
258 NSSize imageSize = [image size];
259 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
260 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
262 [newImage lockFocus];
264 NSAffineTransform* tm = [NSAffineTransform transform];
265 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
266 [tm rotateByDegrees:-90];
267 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
271 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
272 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
274 [newImage unlockFocus];
280 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
283 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
284 wxWidgetCocoaImpl(peer, w)
288 ~wxDisclosureTriangleCocoaImpl()
292 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
294 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
296 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
300 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
301 wxWindowMac* WXUNUSED(parent),
302 wxWindowID WXUNUSED(id),
303 const wxString& label,
306 long WXUNUSED(style),
307 long WXUNUSED(extraStyle))
309 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
310 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
311 [v setTitle:wxCFStringRef( label).AsNSString()];
312 wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v );