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 @implementation wxNSButton
30 static BOOL initialized = NO;
34 wxOSXCocoaClassAddWXMethods( self );
40 switch ( [self state] )
51 - (void) setIntValue: (int) v
56 [self setState:NSMixedState];
59 [self setState:NSOnState];
62 [self setState:NSOffState];
67 - (void) setTrackingTag: (NSTrackingRectTag)tag
72 - (NSTrackingRectTag) trackingTag
79 @interface NSView(PossibleSizeMethods)
80 - (NSControlSize)controlSize;
86 class wxButtonCocoaImpl : public wxWidgetCocoaImpl, public wxButtonImpl
89 wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
90 : wxWidgetCocoaImpl(wxpeer, v)
95 virtual void SetBitmap(const wxBitmap& bitmap)
97 // switch bezel style for plain pushbuttons
100 if ([GetNSButton() bezelStyle] == NSRoundedBezelStyle)
101 [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle];
105 [GetNSButton() setBezelStyle:NSRoundedBezelStyle];
108 wxWidgetCocoaImpl::SetBitmap(bitmap);
112 virtual void SetLabelMarkup(const wxString& markup)
114 wxMarkupToAttrString toAttr(GetWXPeer(), markup);
115 NSMutableAttributedString *attrString = toAttr.GetNSAttributedString();
117 // Button text is always centered.
118 NSMutableParagraphStyle *
119 paragraphStyle = [[NSMutableParagraphStyle alloc] init];
120 [paragraphStyle setAlignment: NSCenterTextAlignment];
121 [attrString addAttribute:NSParagraphStyleAttributeName
123 range:NSMakeRange(0, [attrString length])];
124 [paragraphStyle release];
126 [GetNSButton() setAttributedTitle:attrString];
128 #endif // wxUSE_MARKUP
130 void SetPressedBitmap( const wxBitmap& bitmap )
132 NSButton* button = GetNSButton();
133 [button setAlternateImage: bitmap.GetNSImage()];
134 [button setButtonType:NSMomentaryChangeButton];
137 void GetLayoutInset(int &left , int &top , int &right, int &bottom) const
139 left = top = right = bottom = 0;
140 NSControlSize size = NSRegularControlSize;
141 if ( [m_osxView respondsToSelector:@selector(controlSize)] )
142 size = [m_osxView controlSize];
143 else if ([m_osxView respondsToSelector:@selector(cell)])
145 id cell = [(id)m_osxView cell];
146 if ([cell respondsToSelector:@selector(controlSize)])
147 size = [cell controlSize];
150 if ( [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
154 case NSRegularControlSize:
159 case NSSmallControlSize:
164 case NSMiniControlSize:
175 NSButton *GetNSButton() const
177 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
179 return static_cast<NSButton *>(m_osxView);
183 } // anonymous namespace
185 extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
187 // set bezel style depending on the wxBORDER_XXX flags specified by the style
188 void SetBezelStyleFromBorderFlags(NSButton *v, long style)
190 if ( style & wxBORDER_NONE )
192 [v setBezelStyle:NSShadowlessSquareBezelStyle];
195 else // we do have a border
197 // see trac #11128 for a thorough discussion
198 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
199 [v setBezelStyle:NSRegularSquareBezelStyle];
200 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
201 [v setBezelStyle:NSSmallSquareBezelStyle];
202 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
203 [v setBezelStyle:NSShadowlessSquareBezelStyle];
205 [v setBezelStyle:NSRegularSquareBezelStyle];
210 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
211 wxWindowMac* WXUNUSED(parent),
213 const wxString& label,
217 long WXUNUSED(extraStyle))
219 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
220 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
222 // We can't display a custom label inside a button with help bezel style so
223 // we only use it if we are using the default label. wxButton itself checks
224 // if the label is just "Help" in which case it discards it and passes us
226 if ( id == wxID_HELP && label.empty() )
228 [v setBezelStyle:NSHelpButtonBezelStyle];
232 if ( style & wxBORDER_NONE )
234 [v setBezelStyle:NSShadowlessSquareBezelStyle];
239 // the following styles only exist for certain sizes, so avoid them for
241 if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
243 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
244 [v setBezelStyle:NSRoundedBezelStyle];
245 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
246 [v setBezelStyle:NSTexturedRoundedBezelStyle];
247 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
248 [v setBezelStyle:NSShadowlessSquareBezelStyle];
250 [v setBezelStyle:NSRoundedBezelStyle];
254 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
255 [v setBezelStyle:NSRegularSquareBezelStyle];
256 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
257 [v setBezelStyle:NSSmallSquareBezelStyle];
258 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
259 [v setBezelStyle:NSShadowlessSquareBezelStyle];
261 [v setBezelStyle:NSRegularSquareBezelStyle];
267 [v setButtonType:NSMomentaryPushInButton];
268 return new wxButtonCocoaImpl( wxpeer, v );
271 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
273 if ( [m_osxView isKindOfClass:[NSButton class]] )
276 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
278 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
282 void wxWidgetCocoaImpl::PerformClick()
284 if ([m_osxView isKindOfClass:[NSControl class]])
285 [(NSControl*)m_osxView performClick:nil];
290 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
291 wxWindowMac* WXUNUSED(parent),
292 wxWindowID WXUNUSED(id),
293 const wxBitmap& bitmap,
297 long WXUNUSED(extraStyle))
299 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
300 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
302 SetBezelStyleFromBorderFlags(v, style);
305 [v setImage:bitmap.GetNSImage() ];
307 [v setButtonType:NSMomentaryPushInButton];
308 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
312 #endif // wxUSE_BMPBUTTON
315 // wxDisclosureButton implementation
318 @interface wxDisclosureNSButton : NSButton
324 - (void) updateImage;
328 + (NSImage *)rotateImage: (NSImage *)image;
332 static const char * disc_triangle_xpm[] = {
349 @implementation wxDisclosureNSButton
353 static BOOL initialized = NO;
357 wxOSXCocoaClassAddWXMethods( self );
361 - (id) initWithFrame:(NSRect) frame
363 self = [super initWithFrame:frame];
365 [self setImagePosition:NSImageLeft];
372 return isOpen ? 1 : 0;
375 - (void) setIntValue: (int) v
387 wxCFRef<NSImage*> downArray ;
391 static wxBitmap trianglebm(disc_triangle_xpm);
392 if ( downArray.get() == NULL )
394 downArray.reset( [[wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] retain] );
398 [self setImage:(NSImage*)downArray.get()];
400 [self setImage:trianglebm.GetNSImage()];
403 + (NSImage *)rotateImage: (NSImage *)image
405 NSSize imageSize = [image size];
406 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
407 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
409 [newImage lockFocus];
411 NSAffineTransform* tm = [NSAffineTransform transform];
412 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
413 [tm rotateByDegrees:-90];
414 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
418 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
419 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
421 [newImage unlockFocus];
422 return [newImage autorelease];
427 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
430 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
431 wxWidgetCocoaImpl(peer, w)
435 ~wxDisclosureTriangleCocoaImpl()
439 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
441 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
443 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
447 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
448 wxWindowMac* WXUNUSED(parent),
449 wxWindowID WXUNUSED(winid),
450 const wxString& label,
454 long WXUNUSED(extraStyle))
456 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
457 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
458 if ( !label.empty() )
459 [v setTitle:wxCFStringRef(label).AsNSString()];
461 SetBezelStyleFromBorderFlags(v, style);
463 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );