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 // We only use help button bezel if we don't have any (non standard) label
24 // to display in the button. Otherwise even wxID_HELP buttons look like
25 // normal push buttons.
26 if ( GetId() == wxID_HELP && GetLabel().empty() )
27 return wxSize( 23 , 23 ) ;
30 m_peer->GetBestRect(&r);
32 wxSize sz = r.GetSize();
34 const int wBtnStd = GetDefaultSize().x;
36 if ( (sz.x < wBtnStd) && !HasFlag(wxBU_EXACTFIT) )
42 wxSize wxButton::GetDefaultSize()
44 return wxSize(84, 23);
47 @implementation wxNSButton
51 static BOOL initialized = NO;
55 wxOSXCocoaClassAddWXMethods( self );
61 switch ( [self state] )
72 - (void) setIntValue: (int) v
77 [self setState:NSMixedState];
80 [self setState:NSOnState];
83 [self setState:NSOffState];
88 - (void) setTrackingTag: (NSTrackingRectTag)tag
93 - (NSTrackingRectTag) trackingTag
103 class wxButtonCocoaImpl : public wxWidgetCocoaImpl, public wxButtonImpl
106 wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
107 : wxWidgetCocoaImpl(wxpeer, v)
111 virtual void SetBitmap(const wxBitmap& bitmap)
113 // switch bezel style for plain pushbuttons
114 if ( bitmap.IsOk() && [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
115 [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle ];
117 wxWidgetCocoaImpl::SetBitmap(bitmap);
120 void SetPressedBitmap( const wxBitmap& bitmap )
122 NSButton* button = GetNSButton();
123 [button setAlternateImage: bitmap.GetNSImage()];
124 [button setButtonType:NSMomentaryChangeButton];
128 NSButton *GetNSButton() const
130 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
132 return static_cast<NSButton *>(m_osxView);
136 extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
138 // set bezel style depending on the wxBORDER_XXX flags specified by the style
139 void SetBezelStyleFromBorderFlags(NSButton *v, long style)
141 if ( style & wxBORDER_NONE )
143 [v setBezelStyle:NSShadowlessSquareBezelStyle];
146 else // we do have a border
148 // see trac #11128 for a thorough discussion
149 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
150 [v setBezelStyle:NSRegularSquareBezelStyle];
151 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
152 [v setBezelStyle:NSSmallSquareBezelStyle];
154 [v setBezelStyle:NSShadowlessSquareBezelStyle];
158 } // anonymous namespace
160 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
161 wxWindowMac* WXUNUSED(parent),
163 const wxString& label,
166 long WXUNUSED(style),
167 long WXUNUSED(extraStyle))
169 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
170 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
172 // We can't display a custom label inside a button with help bezel style so
173 // we only use it if we are using the default label. wxButton itself checks
174 // if the label is just "Help" in which case it discards it and passes us
176 if ( id == wxID_HELP && label.empty() )
178 [v setBezelStyle:NSHelpButtonBezelStyle];
182 [v setBezelStyle:NSRoundedBezelStyle];
185 [v setButtonType:NSMomentaryPushInButton];
186 return new wxButtonCocoaImpl( wxpeer, v );
189 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
191 if ( [m_osxView isKindOfClass:[NSButton class]] )
194 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
196 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
200 void wxWidgetCocoaImpl::PerformClick()
202 if ([m_osxView isKindOfClass:[NSControl class]])
203 [(NSControl*)m_osxView performClick:nil];
208 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
209 wxWindowMac* WXUNUSED(parent),
210 wxWindowID WXUNUSED(id),
211 const wxBitmap& bitmap,
215 long WXUNUSED(extraStyle))
217 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
218 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
220 SetBezelStyleFromBorderFlags(v, style);
223 [v setImage:bitmap.GetNSImage() ];
225 [v setButtonType:NSMomentaryPushInButton];
226 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
230 #endif // wxUSE_BMPBUTTON
233 // wxDisclosureButton implementation
236 @interface wxDisclosureNSButton : NSButton
242 - (void) updateImage;
246 + (NSImage *)rotateImage: (NSImage *)image;
250 static const char * disc_triangle_xpm[] = {
267 @implementation wxDisclosureNSButton
271 static BOOL initialized = NO;
275 wxOSXCocoaClassAddWXMethods( self );
279 - (id) initWithFrame:(NSRect) frame
281 self = [super initWithFrame:frame];
283 [self setImagePosition:NSImageLeft];
290 return isOpen ? 1 : 0;
293 - (void) setIntValue: (int) v
305 wxCFRef<NSImage*> downArray ;
309 static wxBitmap trianglebm(disc_triangle_xpm);
310 if ( downArray.get() == NULL )
312 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
316 [self setImage:(NSImage*)downArray.get()];
318 [self setImage:trianglebm.GetNSImage()];
321 + (NSImage *)rotateImage: (NSImage *)image
323 NSSize imageSize = [image size];
324 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
325 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
327 [newImage lockFocus];
329 NSAffineTransform* tm = [NSAffineTransform transform];
330 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
331 [tm rotateByDegrees:-90];
332 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
336 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
337 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
339 [newImage unlockFocus];
345 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
348 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
349 wxWidgetCocoaImpl(peer, w)
353 ~wxDisclosureTriangleCocoaImpl()
357 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
359 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
361 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
365 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
366 wxWindowMac* WXUNUSED(parent),
367 wxWindowID WXUNUSED(winid),
368 const wxString& label,
372 long WXUNUSED(extraStyle))
374 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
375 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
376 if ( !label.empty() )
377 [v setTitle:wxCFStringRef(label).AsNSString()];
379 SetBezelStyleFromBorderFlags(v, style);
381 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );