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 ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
192 // NOTE: setKeyEquivalent: nil will trigger an assert
193 // instead do not call in that case.
194 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
197 void wxWidgetCocoaImpl::PerformClick()
199 if ([m_osxView isKindOfClass:[NSControl class]])
200 [(NSControl*)m_osxView performClick:nil];
205 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
206 wxWindowMac* WXUNUSED(parent),
207 wxWindowID WXUNUSED(id),
208 const wxBitmap& bitmap,
212 long WXUNUSED(extraStyle))
214 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
215 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
217 SetBezelStyleFromBorderFlags(v, style);
220 [v setImage:bitmap.GetNSImage() ];
222 [v setButtonType:NSMomentaryPushInButton];
223 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
227 #endif // wxUSE_BMPBUTTON
230 // wxDisclosureButton implementation
233 @interface wxDisclosureNSButton : NSButton
239 - (void) updateImage;
243 + (NSImage *)rotateImage: (NSImage *)image;
247 static const char * disc_triangle_xpm[] = {
264 @implementation wxDisclosureNSButton
268 static BOOL initialized = NO;
272 wxOSXCocoaClassAddWXMethods( self );
276 - (id) initWithFrame:(NSRect) frame
278 self = [super initWithFrame:frame];
280 [self setImagePosition:NSImageLeft];
287 return isOpen ? 1 : 0;
290 - (void) setIntValue: (int) v
302 wxCFRef<NSImage*> downArray ;
306 static wxBitmap trianglebm(disc_triangle_xpm);
307 if ( downArray.get() == NULL )
309 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
313 [self setImage:(NSImage*)downArray.get()];
315 [self setImage:trianglebm.GetNSImage()];
318 + (NSImage *)rotateImage: (NSImage *)image
320 NSSize imageSize = [image size];
321 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
322 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
324 [newImage lockFocus];
326 NSAffineTransform* tm = [NSAffineTransform transform];
327 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
328 [tm rotateByDegrees:-90];
329 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
333 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
334 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
336 [newImage unlockFocus];
342 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
345 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
346 wxWidgetCocoaImpl(peer, w)
350 ~wxDisclosureTriangleCocoaImpl()
354 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
356 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
358 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
362 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
363 wxWindowMac* WXUNUSED(parent),
364 wxWindowID WXUNUSED(winid),
365 const wxString& label,
369 long WXUNUSED(extraStyle))
371 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
372 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
373 if ( !label.empty() )
374 [v setTitle:wxCFStringRef(label).AsNSString()];
376 SetBezelStyleFromBorderFlags(v, style);
378 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );