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 bezel style depending on the wxBORDER_XXX flags specified by the style
202 // and also accounting for the label (bezels are different for multiline
203 // buttons and normal ones) and the ID (special bezel is used for help button).
205 // This is extern because it's also used in src/osx/cocoa/tglbtn.mm.
208 SetBezelStyleFromBorderFlags(NSButton *v,
211 const wxString& label = wxString())
213 // We can't display a custom label inside a button with help bezel style so
214 // we only use it if we are using the default label. wxButton itself checks
215 // if the label is just "Help" in which case it discards it and passes us
217 if ( winid == wxID_HELP && label.empty() )
219 [v setBezelStyle:NSHelpButtonBezelStyle];
223 // We can't use rounded bezel styles for multiline buttons as they are
224 // only meant to be used at certain sizes, so the style used depends on
225 // whether the label is single or multi line.
226 const bool isSingleLine = label.find_first_of("\n\r") == wxString::npos;
229 switch ( style & wxBORDER_MASK )
232 bezel = NSShadowlessSquareBezelStyle;
236 case wxBORDER_SIMPLE:
237 bezel = NSShadowlessSquareBezelStyle;
240 case wxBORDER_SUNKEN:
241 bezel = isSingleLine ? NSTexturedRoundedBezelStyle
242 : NSSmallSquareBezelStyle;
246 wxFAIL_MSG( "Unknown border style" );
250 case wxBORDER_STATIC:
251 case wxBORDER_RAISED:
253 bezel = isSingleLine ? NSRoundedBezelStyle
254 : NSRegularSquareBezelStyle;
258 [v setBezelStyle:bezel];
262 // Set the keyboard accelerator key from the label (e.g. "Click &Me")
263 void wxButton::OSXUpdateAfterLabelChange(const wxString& label)
265 wxButtonCocoaImpl *impl = static_cast<wxButtonCocoaImpl*>(GetPeer());
267 // Update the bezel style as may be necessary if our new label is multi
268 // line while the old one wasn't (or vice versa).
269 SetBezelStyleFromBorderFlags(impl->GetNSButton(),
275 // Skip setting the accelerator for the default buttons as this would
276 // overwrite the default "Enter" which should be preserved.
277 wxTopLevelWindow * const
278 tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
281 if ( tlw->GetDefaultItem() == this )
285 impl->SetAcceleratorFromLabel(label);
289 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
290 wxWindowMac* WXUNUSED(parent),
292 const wxString& label,
296 long WXUNUSED(extraStyle))
298 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
299 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
301 SetBezelStyleFromBorderFlags(v, style, winid, label);
303 [v setButtonType:NSMomentaryPushInButton];
304 wxButtonCocoaImpl* const impl = new wxButtonCocoaImpl( wxpeer, v );
305 impl->SetAcceleratorFromLabel(label);
309 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
311 if ( [m_osxView isKindOfClass:[NSButton class]] )
315 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
316 [(NSButton*)m_osxView setKeyEquivalentModifierMask: 0];
319 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
323 void wxWidgetCocoaImpl::PerformClick()
325 if ([m_osxView isKindOfClass:[NSControl class]])
326 [(NSControl*)m_osxView performClick:nil];
331 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
332 wxWindowMac* WXUNUSED(parent),
334 const wxBitmap& bitmap,
338 long WXUNUSED(extraStyle))
340 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
341 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
343 SetBezelStyleFromBorderFlags(v, style, winid);
346 [v setImage:bitmap.GetNSImage() ];
348 [v setButtonType:NSMomentaryPushInButton];
349 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
353 #endif // wxUSE_BMPBUTTON
356 // wxDisclosureButton implementation
359 @interface wxDisclosureNSButton : NSButton
365 - (void) updateImage;
369 + (NSImage *)rotateImage: (NSImage *)image;
373 static const char * disc_triangle_xpm[] = {
390 @implementation wxDisclosureNSButton
394 static BOOL initialized = NO;
398 wxOSXCocoaClassAddWXMethods( self );
402 - (id) initWithFrame:(NSRect) frame
404 self = [super initWithFrame:frame];
406 [self setImagePosition:NSImageLeft];
413 return isOpen ? 1 : 0;
416 - (void) setIntValue: (int) v
428 wxCFRef<NSImage*> downArray ;
432 static wxBitmap trianglebm(disc_triangle_xpm);
433 if ( downArray.get() == NULL )
435 downArray.reset( [[wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] retain] );
439 [self setImage:(NSImage*)downArray.get()];
441 [self setImage:trianglebm.GetNSImage()];
444 + (NSImage *)rotateImage: (NSImage *)image
446 NSSize imageSize = [image size];
447 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
448 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
450 [newImage lockFocus];
452 NSAffineTransform* tm = [NSAffineTransform transform];
453 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
454 [tm rotateByDegrees:-90];
455 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
459 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
460 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
462 [newImage unlockFocus];
463 return [newImage autorelease];
468 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
471 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
472 wxWidgetCocoaImpl(peer, w)
476 ~wxDisclosureTriangleCocoaImpl()
480 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
482 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
484 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
488 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
489 wxWindowMac* WXUNUSED(parent),
491 const wxString& label,
495 long WXUNUSED(extraStyle))
497 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
498 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
499 if ( !label.empty() )
500 [v setTitle:wxCFStringRef(label).AsNSString()];
502 SetBezelStyleFromBorderFlags(v, style, winid, label);
504 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );