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"
22 #include "wx/osx/cocoa/private/markuptoattr.h"
23 #endif // wxUSE_MARKUP
26 wxSize wxButton::DoGetBestSize() const
28 // We only use help button bezel if we don't have any (non standard) label
29 // to display in the button. Otherwise even wxID_HELP buttons look like
30 // normal push buttons.
31 if ( GetId() == wxID_HELP && GetLabel().empty() )
32 return wxSize( 23 , 23 ) ;
35 m_peer->GetBestRect(&r);
37 wxSize sz = r.GetSize();
38 sz.x = sz.x + MacGetLeftBorderSize() +
39 MacGetRightBorderSize();
40 sz.y = sz.y + MacGetTopBorderSize() +
41 MacGetBottomBorderSize();
43 const int wBtnStd = GetDefaultSize().x;
45 if ( (sz.x < wBtnStd) && !HasFlag(wxBU_EXACTFIT) )
51 wxSize wxButton::GetDefaultSize()
53 return wxSize(84, 20);
56 @implementation wxNSButton
60 static BOOL initialized = NO;
64 wxOSXCocoaClassAddWXMethods( self );
70 switch ( [self state] )
81 - (void) setIntValue: (int) v
86 [self setState:NSMixedState];
89 [self setState:NSOnState];
92 [self setState:NSOffState];
97 - (void) setTrackingTag: (NSTrackingRectTag)tag
102 - (NSTrackingRectTag) trackingTag
109 @interface NSView(PossibleSizeMethods)
110 - (NSControlSize)controlSize;
116 class wxButtonCocoaImpl : public wxWidgetCocoaImpl, public wxButtonImpl
119 wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
120 : wxWidgetCocoaImpl(wxpeer, v)
124 virtual void SetBitmap(const wxBitmap& bitmap)
126 // switch bezel style for plain pushbuttons
127 if ( bitmap.IsOk() && [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
128 [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle ];
130 wxWidgetCocoaImpl::SetBitmap(bitmap);
133 virtual void SetLabelMarkup(const wxString& markup)
135 wxMarkupToAttrString toAttr(GetWXPeer(), markup);
136 NSMutableAttributedString *attrString = toAttr.GetNSAttributedString();
138 // Button text is always centered.
139 NSMutableParagraphStyle *
140 paragraphStyle = [[NSMutableParagraphStyle alloc] init];
141 [paragraphStyle setAlignment: NSCenterTextAlignment];
142 [attrString addAttribute:NSParagraphStyleAttributeName
144 range:NSMakeRange(0, [attrString length])];
145 [paragraphStyle release];
147 [GetNSButton() setAttributedTitle:attrString];
150 void SetPressedBitmap( const wxBitmap& bitmap )
152 NSButton* button = GetNSButton();
153 [button setAlternateImage: bitmap.GetNSImage()];
154 [button setButtonType:NSMomentaryChangeButton];
157 void GetLayoutInset(int &left , int &top , int &right, int &bottom) const
159 left = top = right = bottom = 0;
160 NSControlSize size = NSRegularControlSize;
161 if ( [m_osxView respondsToSelector:@selector(controlSize)] )
162 size = [m_osxView controlSize];
163 else if ([m_osxView respondsToSelector:@selector(cell)])
165 id cell = [(id)m_osxView cell];
166 if ([cell respondsToSelector:@selector(controlSize)])
167 size = [cell controlSize];
170 if ( [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
174 case NSRegularControlSize:
179 case NSSmallControlSize:
184 case NSMiniControlSize:
195 NSButton *GetNSButton() const
197 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
199 return static_cast<NSButton *>(m_osxView);
203 } // anonymous namespace
205 extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
207 // set bezel style depending on the wxBORDER_XXX flags specified by the style
208 void SetBezelStyleFromBorderFlags(NSButton *v, long style)
210 if ( style & wxBORDER_NONE )
212 [v setBezelStyle:NSShadowlessSquareBezelStyle];
215 else // we do have a border
217 // see trac #11128 for a thorough discussion
218 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
219 [v setBezelStyle:NSRegularSquareBezelStyle];
220 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
221 [v setBezelStyle:NSSmallSquareBezelStyle];
223 [v setBezelStyle:NSShadowlessSquareBezelStyle];
228 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
229 wxWindowMac* WXUNUSED(parent),
231 const wxString& label,
234 long WXUNUSED(style),
235 long WXUNUSED(extraStyle))
237 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
238 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
240 // We can't display a custom label inside a button with help bezel style so
241 // we only use it if we are using the default label. wxButton itself checks
242 // if the label is just "Help" in which case it discards it and passes us
244 if ( id == wxID_HELP && label.empty() )
246 [v setBezelStyle:NSHelpButtonBezelStyle];
250 [v setBezelStyle:NSRoundedBezelStyle];
253 [v setButtonType:NSMomentaryPushInButton];
254 return new wxButtonCocoaImpl( wxpeer, v );
257 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
259 if ( [m_osxView isKindOfClass:[NSButton class]] )
262 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
264 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
268 void wxWidgetCocoaImpl::PerformClick()
270 if ([m_osxView isKindOfClass:[NSControl class]])
271 [(NSControl*)m_osxView performClick:nil];
276 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
277 wxWindowMac* WXUNUSED(parent),
278 wxWindowID WXUNUSED(id),
279 const wxBitmap& bitmap,
283 long WXUNUSED(extraStyle))
285 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
286 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
288 SetBezelStyleFromBorderFlags(v, style);
291 [v setImage:bitmap.GetNSImage() ];
293 [v setButtonType:NSMomentaryPushInButton];
294 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
298 #endif // wxUSE_BMPBUTTON
301 // wxDisclosureButton implementation
304 @interface wxDisclosureNSButton : NSButton
310 - (void) updateImage;
314 + (NSImage *)rotateImage: (NSImage *)image;
318 static const char * disc_triangle_xpm[] = {
335 @implementation wxDisclosureNSButton
339 static BOOL initialized = NO;
343 wxOSXCocoaClassAddWXMethods( self );
347 - (id) initWithFrame:(NSRect) frame
349 self = [super initWithFrame:frame];
351 [self setImagePosition:NSImageLeft];
358 return isOpen ? 1 : 0;
361 - (void) setIntValue: (int) v
373 wxCFRef<NSImage*> downArray ;
377 static wxBitmap trianglebm(disc_triangle_xpm);
378 if ( downArray.get() == NULL )
380 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
384 [self setImage:(NSImage*)downArray.get()];
386 [self setImage:trianglebm.GetNSImage()];
389 + (NSImage *)rotateImage: (NSImage *)image
391 NSSize imageSize = [image size];
392 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
393 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
395 [newImage lockFocus];
397 NSAffineTransform* tm = [NSAffineTransform transform];
398 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
399 [tm rotateByDegrees:-90];
400 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
404 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
405 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
407 [newImage unlockFocus];
413 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
416 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
417 wxWidgetCocoaImpl(peer, w)
421 ~wxDisclosureTriangleCocoaImpl()
425 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
427 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
429 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
433 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
434 wxWindowMac* WXUNUSED(parent),
435 wxWindowID WXUNUSED(winid),
436 const wxString& label,
440 long WXUNUSED(extraStyle))
442 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
443 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
444 if ( !label.empty() )
445 [v setTitle:wxCFStringRef(label).AsNSString()];
447 SetBezelStyleFromBorderFlags(v, style);
449 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );