1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/button.mm
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
14 #include "wx/object.h"
17 #include "wx/button.h"
18 #include "wx/toplevel.h"
19 #include "wx/tglbtn.h"
21 #include "wx/osx/private.h"
24 #include "wx/osx/cocoa/private/markuptoattr.h"
25 #endif // wxUSE_MARKUP
28 @implementation wxNSButton
32 static BOOL initialized = NO;
36 wxOSXCocoaClassAddWXMethods( self );
42 switch ( [self state] )
53 - (void) setIntValue: (int) v
58 [self setState:NSMixedState];
61 [self setState:NSOnState];
64 [self setState:NSOffState];
69 - (void) setTrackingTag: (NSTrackingRectTag)tag
74 - (NSTrackingRectTag) trackingTag
81 @interface NSView(PossibleSizeMethods)
82 - (NSControlSize)controlSize;
85 wxButtonCocoaImpl::wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
86 : wxWidgetCocoaImpl(wxpeer, v)
91 void wxButtonCocoaImpl::SetBitmap(const wxBitmap& bitmap)
93 // switch bezel style for plain pushbuttons
96 if ([GetNSButton() bezelStyle] == NSRoundedBezelStyle)
97 [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle];
101 [GetNSButton() setBezelStyle:NSRoundedBezelStyle];
104 wxWidgetCocoaImpl::SetBitmap(bitmap);
108 void wxButtonCocoaImpl::SetLabelMarkup(const wxString& markup)
110 wxMarkupToAttrString toAttr(GetWXPeer(), markup);
111 NSMutableAttributedString *attrString = toAttr.GetNSAttributedString();
113 // Button text is always centered.
114 NSMutableParagraphStyle *
115 paragraphStyle = [[NSMutableParagraphStyle alloc] init];
116 [paragraphStyle setAlignment: NSCenterTextAlignment];
117 [attrString addAttribute:NSParagraphStyleAttributeName
119 range:NSMakeRange(0, [attrString length])];
120 [paragraphStyle release];
122 [GetNSButton() setAttributedTitle:attrString];
124 #endif // wxUSE_MARKUP
126 void wxButtonCocoaImpl::SetPressedBitmap( const wxBitmap& bitmap )
128 NSButton* button = GetNSButton();
129 [button setAlternateImage: bitmap.GetNSImage()];
130 if ( GetWXPeer()->IsKindOf(wxCLASSINFO(wxToggleButton)) )
132 [button setButtonType:NSToggleButton];
136 [button setButtonType:NSMomentaryChangeButton];
140 void wxButtonCocoaImpl::GetLayoutInset(int &left , int &top , int &right, int &bottom) const
142 left = top = right = bottom = 0;
143 NSControlSize size = NSRegularControlSize;
144 if ( [m_osxView respondsToSelector:@selector(controlSize)] )
145 size = [m_osxView controlSize];
146 else if ([m_osxView respondsToSelector:@selector(cell)])
148 id cell = [(id)m_osxView cell];
149 if ([cell respondsToSelector:@selector(controlSize)])
150 size = [cell controlSize];
153 if ( [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
157 case NSRegularControlSize:
162 case NSSmallControlSize:
167 case NSMiniControlSize:
176 void wxButtonCocoaImpl::SetAcceleratorFromLabel(const wxString& label)
178 const int accelPos = wxControl::FindAccelIndex(label);
179 if ( accelPos != wxNOT_FOUND )
181 wxString accelstring(label[accelPos + 1]); // Skip '&' itself
182 accelstring.MakeLower();
183 wxCFStringRef cfText(accelstring);
184 [GetNSButton() setKeyEquivalent:cfText.AsNSString()];
185 [GetNSButton() setKeyEquivalentModifierMask:NSCommandKeyMask];
189 [GetNSButton() setKeyEquivalent:@""];
193 NSButton *wxButtonCocoaImpl::GetNSButton() const
195 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
197 return static_cast<NSButton *>(m_osxView);
200 // Set bezel style depending on the wxBORDER_XXX flags specified by the style
201 // and also accounting for the label (bezels are different for multiline
202 // buttons and normal ones) and the ID (special bezel is used for help button).
204 // This is extern because it's also used in src/osx/cocoa/tglbtn.mm.
207 SetBezelStyleFromBorderFlags(NSButton *v,
210 const wxString& label = wxString(),
211 const wxBitmap& bitmap = wxBitmap())
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 neither for multiline buttons nor
224 // for buttons containing (big) icons as they are only meant to be used
225 // at certain sizes, so the style used depends on whether the label is
226 // single or multi line.
228 isSimpleText = (label.find_first_of("\n\r") == wxString::npos)
229 && (!bitmap.IsOk() || bitmap.GetHeight() < 20);
232 switch ( style & wxBORDER_MASK )
235 bezel = NSShadowlessSquareBezelStyle;
239 case wxBORDER_SIMPLE:
240 bezel = NSShadowlessSquareBezelStyle;
243 case wxBORDER_SUNKEN:
244 bezel = isSimpleText ? NSTexturedRoundedBezelStyle
245 : NSSmallSquareBezelStyle;
249 wxFAIL_MSG( "Unknown border style" );
253 case wxBORDER_STATIC:
254 case wxBORDER_RAISED:
256 bezel = isSimpleText ? NSRoundedBezelStyle
257 : NSRegularSquareBezelStyle;
261 [v setBezelStyle:bezel];
265 // Set the keyboard accelerator key from the label (e.g. "Click &Me")
266 void wxButton::OSXUpdateAfterLabelChange(const wxString& label)
268 wxButtonCocoaImpl *impl = static_cast<wxButtonCocoaImpl*>(GetPeer());
270 // Update the bezel style as may be necessary if our new label is multi
271 // line while the old one wasn't (or vice versa).
272 SetBezelStyleFromBorderFlags(impl->GetNSButton(),
278 // Skip setting the accelerator for the default buttons as this would
279 // overwrite the default "Enter" which should be preserved.
280 wxTopLevelWindow * const
281 tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
284 if ( tlw->GetDefaultItem() == this )
288 impl->SetAcceleratorFromLabel(label);
292 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
293 wxWindowMac* WXUNUSED(parent),
295 const wxString& label,
299 long WXUNUSED(extraStyle))
301 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
302 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
304 SetBezelStyleFromBorderFlags(v, style, winid, label);
306 [v setButtonType:NSMomentaryPushInButton];
307 wxButtonCocoaImpl* const impl = new wxButtonCocoaImpl( wxpeer, v );
308 impl->SetAcceleratorFromLabel(label);
312 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
314 if ( [m_osxView isKindOfClass:[NSButton class]] )
318 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
319 [(NSButton*)m_osxView setKeyEquivalentModifierMask: 0];
322 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
326 void wxWidgetCocoaImpl::PerformClick()
328 if ([m_osxView isKindOfClass:[NSControl class]])
329 [(NSControl*)m_osxView performClick:nil];
334 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
335 wxWindowMac* WXUNUSED(parent),
337 const wxBitmap& bitmap,
341 long WXUNUSED(extraStyle))
343 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
344 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
346 SetBezelStyleFromBorderFlags(v, style, winid, wxString(), bitmap);
349 [v setImage:bitmap.GetNSImage() ];
351 [v setButtonType:NSMomentaryPushInButton];
352 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
356 #endif // wxUSE_BMPBUTTON
359 // wxDisclosureButton implementation
362 @interface wxDisclosureNSButton : NSButton
368 - (void) updateImage;
372 + (NSImage *)rotateImage: (NSImage *)image;
376 static const char * disc_triangle_xpm[] = {
393 @implementation wxDisclosureNSButton
397 static BOOL initialized = NO;
401 wxOSXCocoaClassAddWXMethods( self );
405 - (id) initWithFrame:(NSRect) frame
407 self = [super initWithFrame:frame];
409 [self setImagePosition:NSImageLeft];
416 return isOpen ? 1 : 0;
419 - (void) setIntValue: (int) v
431 wxCFRef<NSImage*> downArray ;
435 static wxBitmap trianglebm(disc_triangle_xpm);
436 if ( downArray.get() == NULL )
438 downArray.reset( [[wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] retain] );
442 [self setImage:(NSImage*)downArray.get()];
444 [self setImage:trianglebm.GetNSImage()];
447 + (NSImage *)rotateImage: (NSImage *)image
449 NSSize imageSize = [image size];
450 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
451 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
453 [newImage lockFocus];
455 NSAffineTransform* tm = [NSAffineTransform transform];
456 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
457 [tm rotateByDegrees:-90];
458 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
462 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
463 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
465 [newImage unlockFocus];
466 return [newImage autorelease];
471 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
474 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
475 wxWidgetCocoaImpl(peer, w)
479 ~wxDisclosureTriangleCocoaImpl()
483 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
485 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
487 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
491 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
492 wxWindowMac* WXUNUSED(parent),
494 const wxString& label,
498 long WXUNUSED(extraStyle))
500 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
501 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
502 if ( !label.empty() )
503 [v setTitle:wxCFStringRef(label).AsNSString()];
505 SetBezelStyleFromBorderFlags(v, style, winid, label);
507 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );