1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/button.mm
4 // Author: Stefan Csomor
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 GetPeer()->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);
134 virtual void SetLabelMarkup(const wxString& markup)
136 wxMarkupToAttrString toAttr(GetWXPeer(), markup);
137 NSMutableAttributedString *attrString = toAttr.GetNSAttributedString();
139 // Button text is always centered.
140 NSMutableParagraphStyle *
141 paragraphStyle = [[NSMutableParagraphStyle alloc] init];
142 [paragraphStyle setAlignment: NSCenterTextAlignment];
143 [attrString addAttribute:NSParagraphStyleAttributeName
145 range:NSMakeRange(0, [attrString length])];
146 [paragraphStyle release];
148 [GetNSButton() setAttributedTitle:attrString];
150 #endif // wxUSE_MARKUP
152 void SetPressedBitmap( const wxBitmap& bitmap )
154 NSButton* button = GetNSButton();
155 [button setAlternateImage: bitmap.GetNSImage()];
156 [button setButtonType:NSMomentaryChangeButton];
159 void GetLayoutInset(int &left , int &top , int &right, int &bottom) const
161 left = top = right = bottom = 0;
162 NSControlSize size = NSRegularControlSize;
163 if ( [m_osxView respondsToSelector:@selector(controlSize)] )
164 size = [m_osxView controlSize];
165 else if ([m_osxView respondsToSelector:@selector(cell)])
167 id cell = [(id)m_osxView cell];
168 if ([cell respondsToSelector:@selector(controlSize)])
169 size = [cell controlSize];
172 if ( [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
176 case NSRegularControlSize:
181 case NSSmallControlSize:
186 case NSMiniControlSize:
197 NSButton *GetNSButton() const
199 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
201 return static_cast<NSButton *>(m_osxView);
205 } // anonymous namespace
207 extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
209 // set bezel style depending on the wxBORDER_XXX flags specified by the style
210 void SetBezelStyleFromBorderFlags(NSButton *v, long style)
212 if ( style & wxBORDER_NONE )
214 [v setBezelStyle:NSShadowlessSquareBezelStyle];
217 else // we do have a border
219 // see trac #11128 for a thorough discussion
220 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
221 [v setBezelStyle:NSRegularSquareBezelStyle];
222 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
223 [v setBezelStyle:NSSmallSquareBezelStyle];
225 [v setBezelStyle:NSShadowlessSquareBezelStyle];
230 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
231 wxWindowMac* WXUNUSED(parent),
233 const wxString& label,
236 long WXUNUSED(style),
237 long WXUNUSED(extraStyle))
239 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
240 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
242 // We can't display a custom label inside a button with help bezel style so
243 // we only use it if we are using the default label. wxButton itself checks
244 // if the label is just "Help" in which case it discards it and passes us
246 if ( id == wxID_HELP && label.empty() )
248 [v setBezelStyle:NSHelpButtonBezelStyle];
252 [v setBezelStyle:NSRoundedBezelStyle];
255 [v setButtonType:NSMomentaryPushInButton];
256 return new wxButtonCocoaImpl( wxpeer, v );
259 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
261 if ( [m_osxView isKindOfClass:[NSButton class]] )
264 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
266 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
270 void wxWidgetCocoaImpl::PerformClick()
272 if ([m_osxView isKindOfClass:[NSControl class]])
273 [(NSControl*)m_osxView performClick:nil];
278 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
279 wxWindowMac* WXUNUSED(parent),
280 wxWindowID WXUNUSED(id),
281 const wxBitmap& bitmap,
285 long WXUNUSED(extraStyle))
287 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
288 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
290 SetBezelStyleFromBorderFlags(v, style);
293 [v setImage:bitmap.GetNSImage() ];
295 [v setButtonType:NSMomentaryPushInButton];
296 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
300 #endif // wxUSE_BMPBUTTON
303 // wxDisclosureButton implementation
306 @interface wxDisclosureNSButton : NSButton
312 - (void) updateImage;
316 + (NSImage *)rotateImage: (NSImage *)image;
320 static const char * disc_triangle_xpm[] = {
337 @implementation wxDisclosureNSButton
341 static BOOL initialized = NO;
345 wxOSXCocoaClassAddWXMethods( self );
349 - (id) initWithFrame:(NSRect) frame
351 self = [super initWithFrame:frame];
353 [self setImagePosition:NSImageLeft];
360 return isOpen ? 1 : 0;
363 - (void) setIntValue: (int) v
375 wxCFRef<NSImage*> downArray ;
379 static wxBitmap trianglebm(disc_triangle_xpm);
380 if ( downArray.get() == NULL )
382 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
386 [self setImage:(NSImage*)downArray.get()];
388 [self setImage:trianglebm.GetNSImage()];
391 + (NSImage *)rotateImage: (NSImage *)image
393 NSSize imageSize = [image size];
394 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
395 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
397 [newImage lockFocus];
399 NSAffineTransform* tm = [NSAffineTransform transform];
400 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
401 [tm rotateByDegrees:-90];
402 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
406 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
407 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
409 [newImage unlockFocus];
415 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
418 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
419 wxWidgetCocoaImpl(peer, w)
423 ~wxDisclosureTriangleCocoaImpl()
427 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
429 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
431 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
435 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
436 wxWindowMac* WXUNUSED(parent),
437 wxWindowID WXUNUSED(winid),
438 const wxString& label,
442 long WXUNUSED(extraStyle))
444 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
445 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
446 if ( !label.empty() )
447 [v setTitle:wxCFStringRef(label).AsNSString()];
449 SetBezelStyleFromBorderFlags(v, style);
451 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );