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"
19 #include "wx/toplevel.h"
20 #include "wx/tglbtn.h"
22 #include "wx/osx/private.h"
25 #include "wx/osx/cocoa/private/markuptoattr.h"
26 #endif // wxUSE_MARKUP
29 @implementation wxNSButton
33 static BOOL initialized = NO;
37 wxOSXCocoaClassAddWXMethods( self );
43 switch ( [self state] )
54 - (void) setIntValue: (int) v
59 [self setState:NSMixedState];
62 [self setState:NSOnState];
65 [self setState:NSOffState];
70 - (void) setTrackingTag: (NSTrackingRectTag)tag
75 - (NSTrackingRectTag) trackingTag
82 @interface NSView(PossibleSizeMethods)
83 - (NSControlSize)controlSize;
86 wxButtonCocoaImpl::wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
87 : wxWidgetCocoaImpl(wxpeer, v)
92 void wxButtonCocoaImpl::SetBitmap(const wxBitmap& bitmap)
94 // switch bezel style for plain pushbuttons
97 if ([GetNSButton() bezelStyle] == NSRoundedBezelStyle)
98 [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle];
102 [GetNSButton() setBezelStyle:NSRoundedBezelStyle];
105 wxWidgetCocoaImpl::SetBitmap(bitmap);
109 void wxButtonCocoaImpl::SetLabelMarkup(const wxString& markup)
111 wxMarkupToAttrString toAttr(GetWXPeer(), markup);
112 NSMutableAttributedString *attrString = toAttr.GetNSAttributedString();
114 // Button text is always centered.
115 NSMutableParagraphStyle *
116 paragraphStyle = [[NSMutableParagraphStyle alloc] init];
117 [paragraphStyle setAlignment: NSCenterTextAlignment];
118 [attrString addAttribute:NSParagraphStyleAttributeName
120 range:NSMakeRange(0, [attrString length])];
121 [paragraphStyle release];
123 [GetNSButton() setAttributedTitle:attrString];
125 #endif // wxUSE_MARKUP
127 void wxButtonCocoaImpl::SetPressedBitmap( const wxBitmap& bitmap )
129 NSButton* button = GetNSButton();
130 [button setAlternateImage: bitmap.GetNSImage()];
131 if ( GetWXPeer()->IsKindOf(wxCLASSINFO(wxToggleButton)) )
133 [button setButtonType:NSToggleButton];
137 [button setButtonType:NSMomentaryChangeButton];
141 void wxButtonCocoaImpl::GetLayoutInset(int &left , int &top , int &right, int &bottom) const
143 left = top = right = bottom = 0;
144 NSControlSize size = NSRegularControlSize;
145 if ( [m_osxView respondsToSelector:@selector(controlSize)] )
146 size = [m_osxView controlSize];
147 else if ([m_osxView respondsToSelector:@selector(cell)])
149 id cell = [(id)m_osxView cell];
150 if ([cell respondsToSelector:@selector(controlSize)])
151 size = [cell controlSize];
154 if ( [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
158 case NSRegularControlSize:
163 case NSSmallControlSize:
168 case NSMiniControlSize:
177 void wxButtonCocoaImpl::SetAcceleratorFromLabel(const wxString& label)
179 const int accelPos = wxControl::FindAccelIndex(label);
180 if ( accelPos != wxNOT_FOUND )
182 wxString accelstring(label[accelPos + 1]); // Skip '&' itself
183 accelstring.MakeLower();
184 wxCFStringRef cfText(accelstring);
185 [GetNSButton() setKeyEquivalent:cfText.AsNSString()];
186 [GetNSButton() setKeyEquivalentModifierMask:NSCommandKeyMask];
190 [GetNSButton() setKeyEquivalent:@""];
194 NSButton *wxButtonCocoaImpl::GetNSButton() const
196 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
198 return static_cast<NSButton *>(m_osxView);
201 // Set the keyboard accelerator key from the label (e.g. "Click &Me")
202 void wxButton::OSXSetAcceleratorFromLabel(const wxString& label)
204 // Skip setting the accelerator for the default buttons as this would
205 // overwrite the default "Enter" which should be preserved.
206 wxTopLevelWindow * const
207 tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
210 if ( tlw->GetDefaultItem() == this )
214 wxButtonCocoaImpl *impl = static_cast<wxButtonCocoaImpl*>(GetPeer());
215 impl->SetAcceleratorFromLabel(label);
218 extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
220 // set bezel style depending on the wxBORDER_XXX flags specified by the style
221 void SetBezelStyleFromBorderFlags(NSButton *v, long style)
223 if ( style & wxBORDER_NONE )
225 [v setBezelStyle:NSShadowlessSquareBezelStyle];
228 else // we do have a border
230 // see trac #11128 for a thorough discussion
231 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
232 [v setBezelStyle:NSRegularSquareBezelStyle];
233 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
234 [v setBezelStyle:NSSmallSquareBezelStyle];
235 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
236 [v setBezelStyle:NSShadowlessSquareBezelStyle];
238 [v setBezelStyle:NSRegularSquareBezelStyle];
243 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
244 wxWindowMac* WXUNUSED(parent),
246 const wxString& label,
250 long WXUNUSED(extraStyle))
252 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
253 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
255 // We can't display a custom label inside a button with help bezel style so
256 // we only use it if we are using the default label. wxButton itself checks
257 // if the label is just "Help" in which case it discards it and passes us
259 if ( id == wxID_HELP && label.empty() )
261 [v setBezelStyle:NSHelpButtonBezelStyle];
265 if ( style & wxBORDER_NONE )
267 [v setBezelStyle:NSShadowlessSquareBezelStyle];
272 // the following styles only exist for certain sizes, so avoid them for
274 if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
276 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
277 [v setBezelStyle:NSRoundedBezelStyle];
278 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
279 [v setBezelStyle:NSTexturedRoundedBezelStyle];
280 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
281 [v setBezelStyle:NSShadowlessSquareBezelStyle];
283 [v setBezelStyle:NSRoundedBezelStyle];
287 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
288 [v setBezelStyle:NSRegularSquareBezelStyle];
289 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
290 [v setBezelStyle:NSSmallSquareBezelStyle];
291 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
292 [v setBezelStyle:NSShadowlessSquareBezelStyle];
294 [v setBezelStyle:NSRegularSquareBezelStyle];
300 [v setButtonType:NSMomentaryPushInButton];
301 wxButtonCocoaImpl* const impl = new wxButtonCocoaImpl( wxpeer, v );
302 impl->SetAcceleratorFromLabel(label);
306 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
308 if ( [m_osxView isKindOfClass:[NSButton class]] )
312 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
313 [(NSButton*)m_osxView setKeyEquivalentModifierMask: 0];
316 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
320 void wxWidgetCocoaImpl::PerformClick()
322 if ([m_osxView isKindOfClass:[NSControl class]])
323 [(NSControl*)m_osxView performClick:nil];
328 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
329 wxWindowMac* WXUNUSED(parent),
330 wxWindowID WXUNUSED(id),
331 const wxBitmap& bitmap,
335 long WXUNUSED(extraStyle))
337 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
338 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
340 SetBezelStyleFromBorderFlags(v, style);
343 [v setImage:bitmap.GetNSImage() ];
345 [v setButtonType:NSMomentaryPushInButton];
346 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
350 #endif // wxUSE_BMPBUTTON
353 // wxDisclosureButton implementation
356 @interface wxDisclosureNSButton : NSButton
362 - (void) updateImage;
366 + (NSImage *)rotateImage: (NSImage *)image;
370 static const char * disc_triangle_xpm[] = {
387 @implementation wxDisclosureNSButton
391 static BOOL initialized = NO;
395 wxOSXCocoaClassAddWXMethods( self );
399 - (id) initWithFrame:(NSRect) frame
401 self = [super initWithFrame:frame];
403 [self setImagePosition:NSImageLeft];
410 return isOpen ? 1 : 0;
413 - (void) setIntValue: (int) v
425 wxCFRef<NSImage*> downArray ;
429 static wxBitmap trianglebm(disc_triangle_xpm);
430 if ( downArray.get() == NULL )
432 downArray.reset( [[wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] retain] );
436 [self setImage:(NSImage*)downArray.get()];
438 [self setImage:trianglebm.GetNSImage()];
441 + (NSImage *)rotateImage: (NSImage *)image
443 NSSize imageSize = [image size];
444 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
445 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
447 [newImage lockFocus];
449 NSAffineTransform* tm = [NSAffineTransform transform];
450 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
451 [tm rotateByDegrees:-90];
452 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
456 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
457 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
459 [newImage unlockFocus];
460 return [newImage autorelease];
465 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
468 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
469 wxWidgetCocoaImpl(peer, w)
473 ~wxDisclosureTriangleCocoaImpl()
477 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
479 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
481 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
485 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
486 wxWindowMac* WXUNUSED(parent),
487 wxWindowID WXUNUSED(winid),
488 const wxString& label,
492 long WXUNUSED(extraStyle))
494 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
495 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
496 if ( !label.empty() )
497 [v setTitle:wxCFStringRef(label).AsNSString()];
499 SetBezelStyleFromBorderFlags(v, style);
501 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );