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(),
212 const wxBitmap& bitmap = wxBitmap())
214 // We can't display a custom label inside a button with help bezel style so
215 // we only use it if we are using the default label. wxButton itself checks
216 // if the label is just "Help" in which case it discards it and passes us
218 if ( winid == wxID_HELP && label.empty() )
220 [v setBezelStyle:NSHelpButtonBezelStyle];
224 // We can't use rounded bezel styles neither for multiline buttons nor
225 // for buttons containing (big) icons as they are only meant to be used
226 // at certain sizes, so the style used depends on whether the label is
227 // single or multi line.
229 isSimpleText = (label.find_first_of("\n\r") == wxString::npos)
230 && (!bitmap.IsOk() || bitmap.GetHeight() < 20);
233 switch ( style & wxBORDER_MASK )
236 bezel = NSShadowlessSquareBezelStyle;
240 case wxBORDER_SIMPLE:
241 bezel = NSShadowlessSquareBezelStyle;
244 case wxBORDER_SUNKEN:
245 bezel = isSimpleText ? NSTexturedRoundedBezelStyle
246 : NSSmallSquareBezelStyle;
250 wxFAIL_MSG( "Unknown border style" );
254 case wxBORDER_STATIC:
255 case wxBORDER_RAISED:
257 bezel = isSimpleText ? NSRoundedBezelStyle
258 : NSRegularSquareBezelStyle;
262 [v setBezelStyle:bezel];
266 // Set the keyboard accelerator key from the label (e.g. "Click &Me")
267 void wxButton::OSXUpdateAfterLabelChange(const wxString& label)
269 wxButtonCocoaImpl *impl = static_cast<wxButtonCocoaImpl*>(GetPeer());
271 // Update the bezel style as may be necessary if our new label is multi
272 // line while the old one wasn't (or vice versa).
273 SetBezelStyleFromBorderFlags(impl->GetNSButton(),
279 // Skip setting the accelerator for the default buttons as this would
280 // overwrite the default "Enter" which should be preserved.
281 wxTopLevelWindow * const
282 tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
285 if ( tlw->GetDefaultItem() == this )
289 impl->SetAcceleratorFromLabel(label);
293 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
294 wxWindowMac* WXUNUSED(parent),
296 const wxString& label,
300 long WXUNUSED(extraStyle))
302 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
303 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
305 SetBezelStyleFromBorderFlags(v, style, winid, label);
307 [v setButtonType:NSMomentaryPushInButton];
308 wxButtonCocoaImpl* const impl = new wxButtonCocoaImpl( wxpeer, v );
309 impl->SetAcceleratorFromLabel(label);
313 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
315 if ( [m_osxView isKindOfClass:[NSButton class]] )
319 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
320 [(NSButton*)m_osxView setKeyEquivalentModifierMask: 0];
323 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
327 void wxWidgetCocoaImpl::PerformClick()
329 if ([m_osxView isKindOfClass:[NSControl class]])
330 [(NSControl*)m_osxView performClick:nil];
335 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
336 wxWindowMac* WXUNUSED(parent),
338 const wxBitmap& bitmap,
342 long WXUNUSED(extraStyle))
344 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
345 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
347 SetBezelStyleFromBorderFlags(v, style, winid, wxString(), bitmap);
350 [v setImage:bitmap.GetNSImage() ];
352 [v setButtonType:NSMomentaryPushInButton];
353 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
357 #endif // wxUSE_BMPBUTTON
360 // wxDisclosureButton implementation
363 @interface wxDisclosureNSButton : NSButton
369 - (void) updateImage;
373 + (NSImage *)rotateImage: (NSImage *)image;
377 static const char * disc_triangle_xpm[] = {
394 @implementation wxDisclosureNSButton
398 static BOOL initialized = NO;
402 wxOSXCocoaClassAddWXMethods( self );
406 - (id) initWithFrame:(NSRect) frame
408 self = [super initWithFrame:frame];
410 [self setImagePosition:NSImageLeft];
417 return isOpen ? 1 : 0;
420 - (void) setIntValue: (int) v
432 wxCFRef<NSImage*> downArray ;
436 static wxBitmap trianglebm(disc_triangle_xpm);
437 if ( downArray.get() == NULL )
439 downArray.reset( [[wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] retain] );
443 [self setImage:(NSImage*)downArray.get()];
445 [self setImage:trianglebm.GetNSImage()];
448 + (NSImage *)rotateImage: (NSImage *)image
450 NSSize imageSize = [image size];
451 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
452 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
454 [newImage lockFocus];
456 NSAffineTransform* tm = [NSAffineTransform transform];
457 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
458 [tm rotateByDegrees:-90];
459 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
463 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
464 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
466 [newImage unlockFocus];
467 return [newImage autorelease];
472 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
475 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
476 wxWidgetCocoaImpl(peer, w)
480 ~wxDisclosureTriangleCocoaImpl()
484 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
486 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
488 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
492 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
493 wxWindowMac* WXUNUSED(parent),
495 const wxString& label,
499 long WXUNUSED(extraStyle))
501 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
502 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
503 if ( !label.empty() )
504 [v setTitle:wxCFStringRef(label).AsNSString()];
506 SetBezelStyleFromBorderFlags(v, style, winid, label);
508 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );