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"
15 #include "wx/object.h"
18 #include "wx/button.h"
20 #include "wx/osx/private.h"
23 #include "wx/osx/cocoa/private/markuptoattr.h"
24 #endif // wxUSE_MARKUP
27 @implementation wxNSButton
31 static BOOL initialized = NO;
35 wxOSXCocoaClassAddWXMethods( self );
41 switch ( [self state] )
52 - (void) setIntValue: (int) v
57 [self setState:NSMixedState];
60 [self setState:NSOnState];
63 [self setState:NSOffState];
68 - (void) setTrackingTag: (NSTrackingRectTag)tag
73 - (NSTrackingRectTag) trackingTag
80 @interface NSView(PossibleSizeMethods)
81 - (NSControlSize)controlSize;
87 class wxButtonCocoaImpl : public wxWidgetCocoaImpl, public wxButtonImpl
90 wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
91 : wxWidgetCocoaImpl(wxpeer, v)
96 virtual void SetBitmap(const wxBitmap& bitmap)
98 // switch bezel style for plain pushbuttons
101 if ([GetNSButton() bezelStyle] == NSRoundedBezelStyle)
102 [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle];
106 [GetNSButton() setBezelStyle:NSRoundedBezelStyle];
109 wxWidgetCocoaImpl::SetBitmap(bitmap);
113 virtual void SetLabelMarkup(const wxString& markup)
115 wxMarkupToAttrString toAttr(GetWXPeer(), markup);
116 NSMutableAttributedString *attrString = toAttr.GetNSAttributedString();
118 // Button text is always centered.
119 NSMutableParagraphStyle *
120 paragraphStyle = [[NSMutableParagraphStyle alloc] init];
121 [paragraphStyle setAlignment: NSCenterTextAlignment];
122 [attrString addAttribute:NSParagraphStyleAttributeName
124 range:NSMakeRange(0, [attrString length])];
125 [paragraphStyle release];
127 [GetNSButton() setAttributedTitle:attrString];
129 #endif // wxUSE_MARKUP
131 void SetPressedBitmap( const wxBitmap& bitmap )
133 NSButton* button = GetNSButton();
134 [button setAlternateImage: bitmap.GetNSImage()];
135 [button setButtonType:NSMomentaryChangeButton];
138 void GetLayoutInset(int &left , int &top , int &right, int &bottom) const
140 left = top = right = bottom = 0;
141 NSControlSize size = NSRegularControlSize;
142 if ( [m_osxView respondsToSelector:@selector(controlSize)] )
143 size = [m_osxView controlSize];
144 else if ([m_osxView respondsToSelector:@selector(cell)])
146 id cell = [(id)m_osxView cell];
147 if ([cell respondsToSelector:@selector(controlSize)])
148 size = [cell controlSize];
151 if ( [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
155 case NSRegularControlSize:
160 case NSSmallControlSize:
165 case NSMiniControlSize:
176 NSButton *GetNSButton() const
178 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
180 return static_cast<NSButton *>(m_osxView);
184 } // anonymous namespace
186 extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
188 // set bezel style depending on the wxBORDER_XXX flags specified by the style
189 void SetBezelStyleFromBorderFlags(NSButton *v, long style)
191 if ( style & wxBORDER_NONE )
193 [v setBezelStyle:NSShadowlessSquareBezelStyle];
196 else // we do have a border
198 // see trac #11128 for a thorough discussion
199 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
200 [v setBezelStyle:NSRegularSquareBezelStyle];
201 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
202 [v setBezelStyle:NSSmallSquareBezelStyle];
203 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
204 [v setBezelStyle:NSShadowlessSquareBezelStyle];
206 [v setBezelStyle:NSRegularSquareBezelStyle];
211 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
212 wxWindowMac* WXUNUSED(parent),
214 const wxString& label,
218 long WXUNUSED(extraStyle))
220 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
221 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
223 // We can't display a custom label inside a button with help bezel style so
224 // we only use it if we are using the default label. wxButton itself checks
225 // if the label is just "Help" in which case it discards it and passes us
227 if ( id == wxID_HELP && label.empty() )
229 [v setBezelStyle:NSHelpButtonBezelStyle];
233 if ( style & wxBORDER_NONE )
235 [v setBezelStyle:NSShadowlessSquareBezelStyle];
240 // the following styles only exist for certain sizes, so avoid them for
242 if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
244 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
245 [v setBezelStyle:NSRoundedBezelStyle];
246 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
247 [v setBezelStyle:NSTexturedRoundedBezelStyle];
248 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
249 [v setBezelStyle:NSShadowlessSquareBezelStyle];
251 [v setBezelStyle:NSRoundedBezelStyle];
255 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
256 [v setBezelStyle:NSRegularSquareBezelStyle];
257 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
258 [v setBezelStyle:NSSmallSquareBezelStyle];
259 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
260 [v setBezelStyle:NSShadowlessSquareBezelStyle];
262 [v setBezelStyle:NSRegularSquareBezelStyle];
268 [v setButtonType:NSMomentaryPushInButton];
269 return new wxButtonCocoaImpl( wxpeer, v );
272 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
274 if ( [m_osxView isKindOfClass:[NSButton class]] )
277 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
279 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
283 void wxWidgetCocoaImpl::PerformClick()
285 if ([m_osxView isKindOfClass:[NSControl class]])
286 [(NSControl*)m_osxView performClick:nil];
291 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
292 wxWindowMac* WXUNUSED(parent),
293 wxWindowID WXUNUSED(id),
294 const wxBitmap& bitmap,
298 long WXUNUSED(extraStyle))
300 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
301 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
303 SetBezelStyleFromBorderFlags(v, style);
306 [v setImage:bitmap.GetNSImage() ];
308 [v setButtonType:NSMomentaryPushInButton];
309 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
313 #endif // wxUSE_BMPBUTTON
316 // wxDisclosureButton implementation
319 @interface wxDisclosureNSButton : NSButton
325 - (void) updateImage;
329 + (NSImage *)rotateImage: (NSImage *)image;
333 static const char * disc_triangle_xpm[] = {
350 @implementation wxDisclosureNSButton
354 static BOOL initialized = NO;
358 wxOSXCocoaClassAddWXMethods( self );
362 - (id) initWithFrame:(NSRect) frame
364 self = [super initWithFrame:frame];
366 [self setImagePosition:NSImageLeft];
373 return isOpen ? 1 : 0;
376 - (void) setIntValue: (int) v
388 wxCFRef<NSImage*> downArray ;
392 static wxBitmap trianglebm(disc_triangle_xpm);
393 if ( downArray.get() == NULL )
395 downArray.reset( [[wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] retain] );
399 [self setImage:(NSImage*)downArray.get()];
401 [self setImage:trianglebm.GetNSImage()];
404 + (NSImage *)rotateImage: (NSImage *)image
406 NSSize imageSize = [image size];
407 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
408 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
410 [newImage lockFocus];
412 NSAffineTransform* tm = [NSAffineTransform transform];
413 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
414 [tm rotateByDegrees:-90];
415 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
419 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
420 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
422 [newImage unlockFocus];
423 return [newImage autorelease];
428 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
431 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
432 wxWidgetCocoaImpl(peer, w)
436 ~wxDisclosureTriangleCocoaImpl()
440 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
442 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
444 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
448 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
449 wxWindowMac* WXUNUSED(parent),
450 wxWindowID WXUNUSED(winid),
451 const wxString& label,
455 long WXUNUSED(extraStyle))
457 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
458 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
459 if ( !label.empty() )
460 [v setTitle:wxCFStringRef(label).AsNSString()];
462 SetBezelStyleFromBorderFlags(v, style);
464 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );