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)
94 virtual void SetBitmap(const wxBitmap& bitmap)
96 // switch bezel style for plain pushbuttons
97 if ( bitmap.IsOk() && [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
98 [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle ];
100 wxWidgetCocoaImpl::SetBitmap(bitmap);
104 virtual void SetLabelMarkup(const wxString& markup)
106 wxMarkupToAttrString toAttr(GetWXPeer(), markup);
107 NSMutableAttributedString *attrString = toAttr.GetNSAttributedString();
109 // Button text is always centered.
110 NSMutableParagraphStyle *
111 paragraphStyle = [[NSMutableParagraphStyle alloc] init];
112 [paragraphStyle setAlignment: NSCenterTextAlignment];
113 [attrString addAttribute:NSParagraphStyleAttributeName
115 range:NSMakeRange(0, [attrString length])];
116 [paragraphStyle release];
118 [GetNSButton() setAttributedTitle:attrString];
120 #endif // wxUSE_MARKUP
122 void SetPressedBitmap( const wxBitmap& bitmap )
124 NSButton* button = GetNSButton();
125 [button setAlternateImage: bitmap.GetNSImage()];
126 [button setButtonType:NSMomentaryChangeButton];
129 void GetLayoutInset(int &left , int &top , int &right, int &bottom) const
131 left = top = right = bottom = 0;
132 NSControlSize size = NSRegularControlSize;
133 if ( [m_osxView respondsToSelector:@selector(controlSize)] )
134 size = [m_osxView controlSize];
135 else if ([m_osxView respondsToSelector:@selector(cell)])
137 id cell = [(id)m_osxView cell];
138 if ([cell respondsToSelector:@selector(controlSize)])
139 size = [cell controlSize];
142 if ( [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
146 case NSRegularControlSize:
151 case NSSmallControlSize:
156 case NSMiniControlSize:
167 NSButton *GetNSButton() const
169 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
171 return static_cast<NSButton *>(m_osxView);
175 } // anonymous namespace
177 extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
179 // set bezel style depending on the wxBORDER_XXX flags specified by the style
180 void SetBezelStyleFromBorderFlags(NSButton *v, long style)
182 if ( style & wxBORDER_NONE )
184 [v setBezelStyle:NSShadowlessSquareBezelStyle];
187 else // we do have a border
189 // see trac #11128 for a thorough discussion
190 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
191 [v setBezelStyle:NSRegularSquareBezelStyle];
192 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
193 [v setBezelStyle:NSSmallSquareBezelStyle];
195 [v setBezelStyle:NSShadowlessSquareBezelStyle];
200 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
201 wxWindowMac* WXUNUSED(parent),
203 const wxString& label,
206 long WXUNUSED(style),
207 long WXUNUSED(extraStyle))
209 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
210 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
212 // We can't display a custom label inside a button with help bezel style so
213 // we only use it if we are using the default label. wxButton itself checks
214 // if the label is just "Help" in which case it discards it and passes us
216 if ( id == wxID_HELP && label.empty() )
218 [v setBezelStyle:NSHelpButtonBezelStyle];
222 [v setBezelStyle:NSRoundedBezelStyle];
225 [v setButtonType:NSMomentaryPushInButton];
226 return new wxButtonCocoaImpl( wxpeer, v );
229 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
231 if ( [m_osxView isKindOfClass:[NSButton class]] )
234 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
236 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
240 void wxWidgetCocoaImpl::PerformClick()
242 if ([m_osxView isKindOfClass:[NSControl class]])
243 [(NSControl*)m_osxView performClick:nil];
248 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
249 wxWindowMac* WXUNUSED(parent),
250 wxWindowID WXUNUSED(id),
251 const wxBitmap& bitmap,
255 long WXUNUSED(extraStyle))
257 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
258 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
260 SetBezelStyleFromBorderFlags(v, style);
263 [v setImage:bitmap.GetNSImage() ];
265 [v setButtonType:NSMomentaryPushInButton];
266 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
270 #endif // wxUSE_BMPBUTTON
273 // wxDisclosureButton implementation
276 @interface wxDisclosureNSButton : NSButton
282 - (void) updateImage;
286 + (NSImage *)rotateImage: (NSImage *)image;
290 static const char * disc_triangle_xpm[] = {
307 @implementation wxDisclosureNSButton
311 static BOOL initialized = NO;
315 wxOSXCocoaClassAddWXMethods( self );
319 - (id) initWithFrame:(NSRect) frame
321 self = [super initWithFrame:frame];
323 [self setImagePosition:NSImageLeft];
330 return isOpen ? 1 : 0;
333 - (void) setIntValue: (int) v
345 wxCFRef<NSImage*> downArray ;
349 static wxBitmap trianglebm(disc_triangle_xpm);
350 if ( downArray.get() == NULL )
352 downArray.reset( [[wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] retain] );
356 [self setImage:(NSImage*)downArray.get()];
358 [self setImage:trianglebm.GetNSImage()];
361 + (NSImage *)rotateImage: (NSImage *)image
363 NSSize imageSize = [image size];
364 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
365 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
367 [newImage lockFocus];
369 NSAffineTransform* tm = [NSAffineTransform transform];
370 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
371 [tm rotateByDegrees:-90];
372 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
376 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
377 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
379 [newImage unlockFocus];
380 return [newImage autorelease];
385 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
388 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
389 wxWidgetCocoaImpl(peer, w)
393 ~wxDisclosureTriangleCocoaImpl()
397 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
399 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
401 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
405 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
406 wxWindowMac* WXUNUSED(parent),
407 wxWindowID WXUNUSED(winid),
408 const wxString& label,
412 long WXUNUSED(extraStyle))
414 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
415 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
416 if ( !label.empty() )
417 [v setTitle:wxCFStringRef(label).AsNSString()];
419 SetBezelStyleFromBorderFlags(v, style);
421 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );