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
98 if ( bitmap.IsOk() && [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
99 [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle ];
101 wxWidgetCocoaImpl::SetBitmap(bitmap);
105 virtual void SetLabelMarkup(const wxString& markup)
107 wxMarkupToAttrString toAttr(GetWXPeer(), markup);
108 NSMutableAttributedString *attrString = toAttr.GetNSAttributedString();
110 // Button text is always centered.
111 NSMutableParagraphStyle *
112 paragraphStyle = [[NSMutableParagraphStyle alloc] init];
113 [paragraphStyle setAlignment: NSCenterTextAlignment];
114 [attrString addAttribute:NSParagraphStyleAttributeName
116 range:NSMakeRange(0, [attrString length])];
117 [paragraphStyle release];
119 [GetNSButton() setAttributedTitle:attrString];
121 #endif // wxUSE_MARKUP
123 void SetPressedBitmap( const wxBitmap& bitmap )
125 NSButton* button = GetNSButton();
126 [button setAlternateImage: bitmap.GetNSImage()];
127 [button setButtonType:NSMomentaryChangeButton];
130 void GetLayoutInset(int &left , int &top , int &right, int &bottom) const
132 left = top = right = bottom = 0;
133 NSControlSize size = NSRegularControlSize;
134 if ( [m_osxView respondsToSelector:@selector(controlSize)] )
135 size = [m_osxView controlSize];
136 else if ([m_osxView respondsToSelector:@selector(cell)])
138 id cell = [(id)m_osxView cell];
139 if ([cell respondsToSelector:@selector(controlSize)])
140 size = [cell controlSize];
143 if ( [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
147 case NSRegularControlSize:
152 case NSSmallControlSize:
157 case NSMiniControlSize:
168 NSButton *GetNSButton() const
170 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
172 return static_cast<NSButton *>(m_osxView);
176 } // anonymous namespace
178 extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
180 // set bezel style depending on the wxBORDER_XXX flags specified by the style
181 void SetBezelStyleFromBorderFlags(NSButton *v, long style)
183 if ( style & wxBORDER_NONE )
185 [v setBezelStyle:NSShadowlessSquareBezelStyle];
188 else // we do have a border
190 // see trac #11128 for a thorough discussion
191 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
192 [v setBezelStyle:NSRegularSquareBezelStyle];
193 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
194 [v setBezelStyle:NSSmallSquareBezelStyle];
195 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
196 [v setBezelStyle:NSShadowlessSquareBezelStyle];
198 [v setBezelStyle:NSRegularSquareBezelStyle];
203 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
204 wxWindowMac* WXUNUSED(parent),
206 const wxString& label,
210 long WXUNUSED(extraStyle))
212 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
213 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
215 // We can't display a custom label inside a button with help bezel style so
216 // we only use it if we are using the default label. wxButton itself checks
217 // if the label is just "Help" in which case it discards it and passes us
219 if ( id == wxID_HELP && label.empty() )
221 [v setBezelStyle:NSHelpButtonBezelStyle];
225 if ( style & wxBORDER_NONE )
227 [v setBezelStyle:NSShadowlessSquareBezelStyle];
232 // the following styles only exist for certain sizes, so avoid them for
234 if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
236 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
237 [v setBezelStyle:NSRoundedBezelStyle];
238 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
239 [v setBezelStyle:NSTexturedRoundedBezelStyle];
240 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
241 [v setBezelStyle:NSShadowlessSquareBezelStyle];
243 [v setBezelStyle:NSRoundedBezelStyle];
247 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
248 [v setBezelStyle:NSRegularSquareBezelStyle];
249 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
250 [v setBezelStyle:NSSmallSquareBezelStyle];
251 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
252 [v setBezelStyle:NSShadowlessSquareBezelStyle];
254 [v setBezelStyle:NSRegularSquareBezelStyle];
260 [v setButtonType:NSMomentaryPushInButton];
261 return new wxButtonCocoaImpl( wxpeer, v );
264 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
266 if ( [m_osxView isKindOfClass:[NSButton class]] )
269 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
271 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
275 void wxWidgetCocoaImpl::PerformClick()
277 if ([m_osxView isKindOfClass:[NSControl class]])
278 [(NSControl*)m_osxView performClick:nil];
283 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
284 wxWindowMac* WXUNUSED(parent),
285 wxWindowID WXUNUSED(id),
286 const wxBitmap& bitmap,
290 long WXUNUSED(extraStyle))
292 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
293 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
295 SetBezelStyleFromBorderFlags(v, style);
298 [v setImage:bitmap.GetNSImage() ];
300 [v setButtonType:NSMomentaryPushInButton];
301 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
305 #endif // wxUSE_BMPBUTTON
308 // wxDisclosureButton implementation
311 @interface wxDisclosureNSButton : NSButton
317 - (void) updateImage;
321 + (NSImage *)rotateImage: (NSImage *)image;
325 static const char * disc_triangle_xpm[] = {
342 @implementation wxDisclosureNSButton
346 static BOOL initialized = NO;
350 wxOSXCocoaClassAddWXMethods( self );
354 - (id) initWithFrame:(NSRect) frame
356 self = [super initWithFrame:frame];
358 [self setImagePosition:NSImageLeft];
365 return isOpen ? 1 : 0;
368 - (void) setIntValue: (int) v
380 wxCFRef<NSImage*> downArray ;
384 static wxBitmap trianglebm(disc_triangle_xpm);
385 if ( downArray.get() == NULL )
387 downArray.reset( [[wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] retain] );
391 [self setImage:(NSImage*)downArray.get()];
393 [self setImage:trianglebm.GetNSImage()];
396 + (NSImage *)rotateImage: (NSImage *)image
398 NSSize imageSize = [image size];
399 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
400 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
402 [newImage lockFocus];
404 NSAffineTransform* tm = [NSAffineTransform transform];
405 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
406 [tm rotateByDegrees:-90];
407 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
411 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
412 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
414 [newImage unlockFocus];
415 return [newImage autorelease];
420 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
423 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
424 wxWidgetCocoaImpl(peer, w)
428 ~wxDisclosureTriangleCocoaImpl()
432 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
434 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
436 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
440 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
441 wxWindowMac* WXUNUSED(parent),
442 wxWindowID WXUNUSED(winid),
443 const wxString& label,
447 long WXUNUSED(extraStyle))
449 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
450 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
451 if ( !label.empty() )
452 [v setTitle:wxCFStringRef(label).AsNSString()];
454 SetBezelStyleFromBorderFlags(v, style);
456 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );