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();
33 sz.x = sz.x + MacGetLeftBorderSize() +
34 MacGetRightBorderSize();
35 sz.y = sz.y + MacGetTopBorderSize() +
36 MacGetBottomBorderSize();
38 const int wBtnStd = GetDefaultSize().x;
40 if ( (sz.x < wBtnStd) && !HasFlag(wxBU_EXACTFIT) )
46 wxSize wxButton::GetDefaultSize()
48 return wxSize(84, 20);
51 @implementation wxNSButton
55 static BOOL initialized = NO;
59 wxOSXCocoaClassAddWXMethods( self );
65 switch ( [self state] )
76 - (void) setIntValue: (int) v
81 [self setState:NSMixedState];
84 [self setState:NSOnState];
87 [self setState:NSOffState];
92 - (void) setTrackingTag: (NSTrackingRectTag)tag
97 - (NSTrackingRectTag) trackingTag
107 class wxButtonCocoaImpl : public wxWidgetCocoaImpl, public wxButtonImpl
110 wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
111 : wxWidgetCocoaImpl(wxpeer, v)
115 virtual void SetBitmap(const wxBitmap& bitmap)
117 // switch bezel style for plain pushbuttons
118 if ( bitmap.IsOk() && [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
119 [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle ];
121 wxWidgetCocoaImpl::SetBitmap(bitmap);
124 void SetPressedBitmap( const wxBitmap& bitmap )
126 NSButton* button = GetNSButton();
127 [button setAlternateImage: bitmap.GetNSImage()];
128 [button setButtonType:NSMomentaryChangeButton];
131 void GetLayoutInset(int &left , int &top , int &right, int &bottom) const
133 left = top = right = bottom = 0;
134 NSControlSize size = NSRegularControlSize;
135 if ( [m_osxView respondsToSelector:@selector(controlSize)] )
136 size = (NSControlSize) [m_osxView controlSize];
137 else if ([m_osxView respondsToSelector:@selector(cell)])
139 id cell = [(id)m_osxView cell];
140 if ([cell respondsToSelector:@selector(controlSize)])
141 size = [cell controlSize];
144 if ( [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
148 case NSRegularControlSize:
153 case NSSmallControlSize:
158 case NSMiniControlSize:
169 NSButton *GetNSButton() const
171 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
173 return static_cast<NSButton *>(m_osxView);
177 extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
179 // set bezel style depending on the wxBORDER_XXX flags specified by the style
180 void SetBezelStyleFromBorderFlags(NSButton *v, long style)
182 if ( style & wxBORDER_NONE )
184 [v setBezelStyle:NSShadowlessSquareBezelStyle];
187 else // we do have a border
189 // see trac #11128 for a thorough discussion
190 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
191 [v setBezelStyle:NSRegularSquareBezelStyle];
192 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
193 [v setBezelStyle:NSSmallSquareBezelStyle];
195 [v setBezelStyle:NSShadowlessSquareBezelStyle];
199 } // anonymous namespace
201 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
202 wxWindowMac* WXUNUSED(parent),
204 const wxString& label,
207 long WXUNUSED(style),
208 long WXUNUSED(extraStyle))
210 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
211 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
213 // We can't display a custom label inside a button with help bezel style so
214 // we only use it if we are using the default label. wxButton itself checks
215 // if the label is just "Help" in which case it discards it and passes us
217 if ( id == wxID_HELP && label.empty() )
219 [v setBezelStyle:NSHelpButtonBezelStyle];
223 [v setBezelStyle:NSRoundedBezelStyle];
226 [v setButtonType:NSMomentaryPushInButton];
227 return new wxButtonCocoaImpl( wxpeer, v );
230 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
232 if ( [m_osxView isKindOfClass:[NSButton class]] )
235 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
237 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
241 void wxWidgetCocoaImpl::PerformClick()
243 if ([m_osxView isKindOfClass:[NSControl class]])
244 [(NSControl*)m_osxView performClick:nil];
249 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
250 wxWindowMac* WXUNUSED(parent),
251 wxWindowID WXUNUSED(id),
252 const wxBitmap& bitmap,
256 long WXUNUSED(extraStyle))
258 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
259 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
261 SetBezelStyleFromBorderFlags(v, style);
264 [v setImage:bitmap.GetNSImage() ];
266 [v setButtonType:NSMomentaryPushInButton];
267 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
271 #endif // wxUSE_BMPBUTTON
274 // wxDisclosureButton implementation
277 @interface wxDisclosureNSButton : NSButton
283 - (void) updateImage;
287 + (NSImage *)rotateImage: (NSImage *)image;
291 static const char * disc_triangle_xpm[] = {
308 @implementation wxDisclosureNSButton
312 static BOOL initialized = NO;
316 wxOSXCocoaClassAddWXMethods( self );
320 - (id) initWithFrame:(NSRect) frame
322 self = [super initWithFrame:frame];
324 [self setImagePosition:NSImageLeft];
331 return isOpen ? 1 : 0;
334 - (void) setIntValue: (int) v
346 wxCFRef<NSImage*> downArray ;
350 static wxBitmap trianglebm(disc_triangle_xpm);
351 if ( downArray.get() == NULL )
353 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
357 [self setImage:(NSImage*)downArray.get()];
359 [self setImage:trianglebm.GetNSImage()];
362 + (NSImage *)rotateImage: (NSImage *)image
364 NSSize imageSize = [image size];
365 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
366 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
368 [newImage lockFocus];
370 NSAffineTransform* tm = [NSAffineTransform transform];
371 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
372 [tm rotateByDegrees:-90];
373 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
377 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
378 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
380 [newImage unlockFocus];
386 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
389 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
390 wxWidgetCocoaImpl(peer, w)
394 ~wxDisclosureTriangleCocoaImpl()
398 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
400 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
402 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
406 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
407 wxWindowMac* WXUNUSED(parent),
408 wxWindowID WXUNUSED(winid),
409 const wxString& label,
413 long WXUNUSED(extraStyle))
415 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
416 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
417 if ( !label.empty() )
418 [v setTitle:wxCFStringRef(label).AsNSString()];
420 SetBezelStyleFromBorderFlags(v, style);
422 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );