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
104 @interface NSView(PossibleSizeMethods)
105 - (NSControlSize)controlSize;
111 class wxButtonCocoaImpl : public wxWidgetCocoaImpl, public wxButtonImpl
114 wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
115 : wxWidgetCocoaImpl(wxpeer, v)
119 virtual void SetBitmap(const wxBitmap& bitmap)
121 // switch bezel style for plain pushbuttons
122 if ( bitmap.IsOk() && [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
123 [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle ];
125 wxWidgetCocoaImpl::SetBitmap(bitmap);
128 void SetPressedBitmap( const wxBitmap& bitmap )
130 NSButton* button = GetNSButton();
131 [button setAlternateImage: bitmap.GetNSImage()];
132 [button setButtonType:NSMomentaryChangeButton];
135 void GetLayoutInset(int &left , int &top , int &right, int &bottom) const
137 left = top = right = bottom = 0;
138 NSControlSize size = NSRegularControlSize;
139 if ( [m_osxView respondsToSelector:@selector(controlSize)] )
140 size = [m_osxView controlSize];
141 else if ([m_osxView respondsToSelector:@selector(cell)])
143 id cell = [(id)m_osxView cell];
144 if ([cell respondsToSelector:@selector(controlSize)])
145 size = [cell controlSize];
148 if ( [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
152 case NSRegularControlSize:
157 case NSSmallControlSize:
162 case NSMiniControlSize:
173 NSButton *GetNSButton() const
175 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
177 return static_cast<NSButton *>(m_osxView);
181 extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
183 // set bezel style depending on the wxBORDER_XXX flags specified by the style
184 void SetBezelStyleFromBorderFlags(NSButton *v, long style)
186 if ( style & wxBORDER_NONE )
188 [v setBezelStyle:NSShadowlessSquareBezelStyle];
191 else // we do have a border
193 // see trac #11128 for a thorough discussion
194 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
195 [v setBezelStyle:NSRegularSquareBezelStyle];
196 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
197 [v setBezelStyle:NSSmallSquareBezelStyle];
199 [v setBezelStyle:NSShadowlessSquareBezelStyle];
203 } // anonymous namespace
205 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
206 wxWindowMac* WXUNUSED(parent),
208 const wxString& label,
211 long WXUNUSED(style),
212 long WXUNUSED(extraStyle))
214 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
215 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
217 // We can't display a custom label inside a button with help bezel style so
218 // we only use it if we are using the default label. wxButton itself checks
219 // if the label is just "Help" in which case it discards it and passes us
221 if ( id == wxID_HELP && label.empty() )
223 [v setBezelStyle:NSHelpButtonBezelStyle];
227 [v setBezelStyle:NSRoundedBezelStyle];
230 [v setButtonType:NSMomentaryPushInButton];
231 return new wxButtonCocoaImpl( wxpeer, v );
234 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
236 if ( [m_osxView isKindOfClass:[NSButton class]] )
239 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
241 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
245 void wxWidgetCocoaImpl::PerformClick()
247 if ([m_osxView isKindOfClass:[NSControl class]])
248 [(NSControl*)m_osxView performClick:nil];
253 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
254 wxWindowMac* WXUNUSED(parent),
255 wxWindowID WXUNUSED(id),
256 const wxBitmap& bitmap,
260 long WXUNUSED(extraStyle))
262 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
263 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
265 SetBezelStyleFromBorderFlags(v, style);
268 [v setImage:bitmap.GetNSImage() ];
270 [v setButtonType:NSMomentaryPushInButton];
271 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
275 #endif // wxUSE_BMPBUTTON
278 // wxDisclosureButton implementation
281 @interface wxDisclosureNSButton : NSButton
287 - (void) updateImage;
291 + (NSImage *)rotateImage: (NSImage *)image;
295 static const char * disc_triangle_xpm[] = {
312 @implementation wxDisclosureNSButton
316 static BOOL initialized = NO;
320 wxOSXCocoaClassAddWXMethods( self );
324 - (id) initWithFrame:(NSRect) frame
326 self = [super initWithFrame:frame];
328 [self setImagePosition:NSImageLeft];
335 return isOpen ? 1 : 0;
338 - (void) setIntValue: (int) v
350 wxCFRef<NSImage*> downArray ;
354 static wxBitmap trianglebm(disc_triangle_xpm);
355 if ( downArray.get() == NULL )
357 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
361 [self setImage:(NSImage*)downArray.get()];
363 [self setImage:trianglebm.GetNSImage()];
366 + (NSImage *)rotateImage: (NSImage *)image
368 NSSize imageSize = [image size];
369 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
370 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
372 [newImage lockFocus];
374 NSAffineTransform* tm = [NSAffineTransform transform];
375 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
376 [tm rotateByDegrees:-90];
377 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
381 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
382 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
384 [newImage unlockFocus];
390 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
393 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
394 wxWidgetCocoaImpl(peer, w)
398 ~wxDisclosureTriangleCocoaImpl()
402 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
404 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
406 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
410 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
411 wxWindowMac* WXUNUSED(parent),
412 wxWindowID WXUNUSED(winid),
413 const wxString& label,
417 long WXUNUSED(extraStyle))
419 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
420 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
421 if ( !label.empty() )
422 [v setTitle:wxCFStringRef(label).AsNSString()];
424 SetBezelStyleFromBorderFlags(v, style);
426 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );