]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/button.mm
fixing class
[wxWidgets.git] / src / osx / cocoa / button.mm
CommitLineData
f033830e
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/cocoa/button.mm
3// Purpose: wxButton
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
a9a4f229 7// RCS-ID: $Id$
f033830e
SC
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
f033830e 14#ifndef WX_PRECOMP
d6eb3ff8 15#include "wx/object.h"
f033830e
SC
16#endif
17
d6eb3ff8 18#include "wx/button.h"
0afa3752 19#include "wx/toplevel.h"
b108581c 20#include "wx/tglbtn.h"
d6eb3ff8 21
f033830e
SC
22#include "wx/osx/private.h"
23
f672c969
VZ
24#if wxUSE_MARKUP
25 #include "wx/osx/cocoa/private/markuptoattr.h"
26#endif // wxUSE_MARKUP
27
28
f033830e
SC
29@implementation wxNSButton
30
4dd9fdf8 31+ (void)initialize
f033830e 32{
4dd9fdf8 33 static BOOL initialized = NO;
b727fcd3 34 if (!initialized)
f033830e 35 {
4dd9fdf8
SC
36 initialized = YES;
37 wxOSXCocoaClassAddWXMethods( self );
f033830e
SC
38 }
39}
40
f033830e
SC
41- (int) intValue
42{
43 switch ( [self state] )
44 {
45 case NSOnState:
46 return 1;
47 case NSMixedState:
48 return 2;
49 default:
50 return 0;
51 }
52}
53
54- (void) setIntValue: (int) v
55{
56 switch( v )
57 {
58 case 2:
59 [self setState:NSMixedState];
60 break;
61 case 1:
62 [self setState:NSOnState];
63 break;
64 default :
65 [self setState:NSOffState];
66 break;
67 }
68}
69
411a1c35
SC
70- (void) setTrackingTag: (NSTrackingRectTag)tag
71{
72 rectTag = tag;
73}
74
75- (NSTrackingRectTag) trackingTag
76{
77 return rectTag;
78}
79
f033830e
SC
80@end
81
58ce18f2
SC
82@interface NSView(PossibleSizeMethods)
83- (NSControlSize)controlSize;
84@end
85
b108581c
SC
86wxButtonCocoaImpl::wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
87: wxWidgetCocoaImpl(wxpeer, v)
e5d05b90 88{
b108581c
SC
89 SetNeedsFrame(false);
90}
e5d05b90 91
b108581c 92void wxButtonCocoaImpl::SetBitmap(const wxBitmap& bitmap)
e5d05b90 93{
b108581c
SC
94 // switch bezel style for plain pushbuttons
95 if ( bitmap.IsOk() )
e5d05b90 96 {
b108581c
SC
97 if ([GetNSButton() bezelStyle] == NSRoundedBezelStyle)
98 [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle];
e5d05b90 99 }
b108581c 100 else
e5d05b90 101 {
b108581c 102 [GetNSButton() setBezelStyle:NSRoundedBezelStyle];
e5d05b90 103 }
b108581c
SC
104
105 wxWidgetCocoaImpl::SetBitmap(bitmap);
106}
e5d05b90 107
6a219e34 108#if wxUSE_MARKUP
b108581c
SC
109void wxButtonCocoaImpl::SetLabelMarkup(const wxString& markup)
110{
111 wxMarkupToAttrString toAttr(GetWXPeer(), markup);
112 NSMutableAttributedString *attrString = toAttr.GetNSAttributedString();
113
114 // Button text is always centered.
115 NSMutableParagraphStyle *
116 paragraphStyle = [[NSMutableParagraphStyle alloc] init];
117 [paragraphStyle setAlignment: NSCenterTextAlignment];
118 [attrString addAttribute:NSParagraphStyleAttributeName
119 value:paragraphStyle
120 range:NSMakeRange(0, [attrString length])];
121 [paragraphStyle release];
122
123 [GetNSButton() setAttributedTitle:attrString];
124}
6a219e34 125#endif // wxUSE_MARKUP
f672c969 126
b108581c
SC
127void wxButtonCocoaImpl::SetPressedBitmap( const wxBitmap& bitmap )
128{
129 NSButton* button = GetNSButton();
130 [button setAlternateImage: bitmap.GetNSImage()];
131 if ( GetWXPeer()->IsKindOf(wxCLASSINFO(wxToggleButton)) )
132 {
133 [button setButtonType:NSToggleButton];
134 }
135 else
b38dc31f 136 {
b38dc31f
SC
137 [button setButtonType:NSMomentaryChangeButton];
138 }
b108581c 139}
b38dc31f 140
b108581c
SC
141void wxButtonCocoaImpl::GetLayoutInset(int &left , int &top , int &right, int &bottom) const
142{
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)])
54ea2834 148 {
b108581c
SC
149 id cell = [(id)m_osxView cell];
150 if ([cell respondsToSelector:@selector(controlSize)])
151 size = [cell controlSize];
152 }
153
154 if ( [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
155 {
156 switch( size )
54ea2834 157 {
b108581c
SC
158 case NSRegularControlSize:
159 left = right = 6;
160 top = 4;
161 bottom = 8;
162 break;
163 case NSSmallControlSize:
164 left = right = 5;
165 top = 4;
166 bottom = 7;
167 break;
168 case NSMiniControlSize:
169 left = right = 1;
170 top = 0;
171 bottom = 2;
172 break;
54ea2834
SC
173 }
174 }
b108581c 175}
0afa3752 176
b108581c
SC
177void wxButtonCocoaImpl::SetAcceleratorFromLabel(const wxString& label)
178{
179 const int accelPos = wxControl::FindAccelIndex(label);
180 if ( accelPos != wxNOT_FOUND )
0afa3752 181 {
b108581c
SC
182 wxString accelstring(label[accelPos + 1]); // Skip '&' itself
183 accelstring.MakeLower();
184 wxCFStringRef cfText(accelstring);
185 [GetNSButton() setKeyEquivalent:cfText.AsNSString()];
186 [GetNSButton() setKeyEquivalentModifierMask:NSCommandKeyMask];
0afa3752 187 }
b108581c 188 else
e5d05b90 189 {
b108581c 190 [GetNSButton() setKeyEquivalent:@""];
e5d05b90 191 }
b108581c 192}
e5d05b90 193
b108581c
SC
194NSButton *wxButtonCocoaImpl::GetNSButton() const
195{
196 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
197
198 return static_cast<NSButton *>(m_osxView);
199}
df04f800 200
0afa3752
VZ
201// Set the keyboard accelerator key from the label (e.g. "Click &Me")
202void wxButton::OSXSetAcceleratorFromLabel(const wxString& label)
203{
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);
208 if ( tlw )
209 {
210 if ( tlw->GetDefaultItem() == this )
211 return;
212 }
213
214 wxButtonCocoaImpl *impl = static_cast<wxButtonCocoaImpl*>(GetPeer());
215 impl->SetAcceleratorFromLabel(label);
216}
217
7f08aa6c
SC
218extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
219
73b1b996
VZ
220// set bezel style depending on the wxBORDER_XXX flags specified by the style
221void SetBezelStyleFromBorderFlags(NSButton *v, long style)
222{
223 if ( style & wxBORDER_NONE )
224 {
225 [v setBezelStyle:NSShadowlessSquareBezelStyle];
226 [v setBordered:NO];
227 }
228 else // we do have a border
229 {
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];
24e059c3 235 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
73b1b996 236 [v setBezelStyle:NSShadowlessSquareBezelStyle];
24e059c3
SC
237 else
238 [v setBezelStyle:NSRegularSquareBezelStyle];
73b1b996
VZ
239 }
240}
241
f033830e 242
b727fcd3
VZ
243wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
244 wxWindowMac* WXUNUSED(parent),
245 wxWindowID id,
01495abf 246 const wxString& label,
b727fcd3 247 const wxPoint& pos,
f033830e 248 const wxSize& size,
24e059c3 249 long style,
b727fcd3 250 long WXUNUSED(extraStyle))
f033830e 251{
dbeddfb9 252 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
f033830e 253 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
b727fcd3 254
01495abf
VZ
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
258 // an empty string.
259 if ( id == wxID_HELP && label.empty() )
f033830e
SC
260 {
261 [v setBezelStyle:NSHelpButtonBezelStyle];
262 }
263 else
264 {
24e059c3
SC
265 if ( style & wxBORDER_NONE )
266 {
267 [v setBezelStyle:NSShadowlessSquareBezelStyle];
268 [v setBordered:NO];
269 }
270 else
271 {
272 // the following styles only exist for certain sizes, so avoid them for
273 // multi-line
274 if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
275 {
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];
282 else
283 [v setBezelStyle:NSRoundedBezelStyle];
284 }
285 else
286 {
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];
293 else
294 [v setBezelStyle:NSRegularSquareBezelStyle];
295 }
b727fcd3 296
24e059c3
SC
297 }
298 }
0afa3752 299
f033830e 300 [v setButtonType:NSMomentaryPushInButton];
0afa3752
VZ
301 wxButtonCocoaImpl* const impl = new wxButtonCocoaImpl( wxpeer, v );
302 impl->SetAcceleratorFromLabel(label);
303 return impl;
f033830e
SC
304}
305
306void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
b727fcd3 307{
3f30bd1a
SC
308 if ( [m_osxView isKindOfClass:[NSButton class]] )
309 {
310 if ( isDefault )
0afa3752 311 {
3f30bd1a 312 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
0afa3752
VZ
313 [(NSButton*)m_osxView setKeyEquivalentModifierMask: 0];
314 }
3f30bd1a
SC
315 else
316 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
317 }
f033830e
SC
318}
319
b727fcd3 320void wxWidgetCocoaImpl::PerformClick()
f033830e 321{
73b1b996
VZ
322 if ([m_osxView isKindOfClass:[NSControl class]])
323 [(NSControl*)m_osxView performClick:nil];
f033830e
SC
324}
325
b38dc31f
SC
326#if wxUSE_BMPBUTTON
327
328wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
329 wxWindowMac* WXUNUSED(parent),
330 wxWindowID WXUNUSED(id),
331 const wxBitmap& bitmap,
332 const wxPoint& pos,
333 const wxSize& size,
334 long style,
335 long WXUNUSED(extraStyle))
336{
337 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
338 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
73b1b996
VZ
339
340 SetBezelStyleFromBorderFlags(v, style);
341
a1b806b9 342 if (bitmap.IsOk())
b38dc31f 343 [v setImage:bitmap.GetNSImage() ];
73b1b996 344
b38dc31f
SC
345 [v setButtonType:NSMomentaryPushInButton];
346 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
347 return c;
348}
349
73b1b996 350#endif // wxUSE_BMPBUTTON
b38dc31f 351
17ad51ed
SC
352//
353// wxDisclosureButton implementation
354//
355
356@interface wxDisclosureNSButton : NSButton
357{
358
359 BOOL isOpen;
360}
361
362- (void) updateImage;
363
364- (void) toggle;
365
366+ (NSImage *)rotateImage: (NSImage *)image;
367
368@end
369
da52d42b
SC
370static const char * disc_triangle_xpm[] = {
371"10 9 4 1",
372" c None",
373". c #737373",
374"+ c #989898",
375"- c #c6c6c6",
376" .- ",
377" ..+- ",
378" ....+ ",
379" ......- ",
380" .......- ",
381" ......- ",
382" ....+ ",
383" ..+- ",
384" .- ",
385};
386
17ad51ed
SC
387@implementation wxDisclosureNSButton
388
389+ (void)initialize
390{
391 static BOOL initialized = NO;
b727fcd3 392 if (!initialized)
17ad51ed
SC
393 {
394 initialized = YES;
395 wxOSXCocoaClassAddWXMethods( self );
396 }
397}
398
399- (id) initWithFrame:(NSRect) frame
400{
401 self = [super initWithFrame:frame];
17ad51ed
SC
402 isOpen = NO;
403 [self setImagePosition:NSImageLeft];
404 [self updateImage];
405 return self;
406}
407
408- (int) intValue
409{
410 return isOpen ? 1 : 0;
411}
412
413- (void) setIntValue: (int) v
414{
415 isOpen = ( v != 0 );
416 [self updateImage];
417}
418
419- (void) toggle
420{
421 isOpen = !isOpen;
422 [self updateImage];
423}
424
425wxCFRef<NSImage*> downArray ;
426
427- (void) updateImage
428{
da52d42b 429 static wxBitmap trianglebm(disc_triangle_xpm);
17ad51ed
SC
430 if ( downArray.get() == NULL )
431 {
adf264f2 432 downArray.reset( [[wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] retain] );
17ad51ed 433 }
b727fcd3 434
17ad51ed
SC
435 if ( isOpen )
436 [self setImage:(NSImage*)downArray.get()];
437 else
da52d42b 438 [self setImage:trianglebm.GetNSImage()];
17ad51ed
SC
439}
440
441+ (NSImage *)rotateImage: (NSImage *)image
442{
443 NSSize imageSize = [image size];
444 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
445 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
b727fcd3 446
17ad51ed 447 [newImage lockFocus];
b727fcd3 448
17ad51ed
SC
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];
453 [tm concat];
b727fcd3
VZ
454
455
17ad51ed
SC
456 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
457 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
b727fcd3 458
17ad51ed 459 [newImage unlockFocus];
adf264f2 460 return [newImage autorelease];
17ad51ed
SC
461}
462
463@end
464
465class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
466{
467public :
468 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
469 wxWidgetCocoaImpl(peer, w)
470 {
471 }
b727fcd3 472
17ad51ed
SC
473 ~wxDisclosureTriangleCocoaImpl()
474 {
475 }
476
477 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
478 {
479 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
480 [db toggle];
481 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
482 }
483};
484
b727fcd3
VZ
485wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
486 wxWindowMac* WXUNUSED(parent),
73b1b996 487 wxWindowID WXUNUSED(winid),
dbeddfb9 488 const wxString& label,
b727fcd3 489 const wxPoint& pos,
dbeddfb9 490 const wxSize& size,
73b1b996 491 long style,
b727fcd3 492 long WXUNUSED(extraStyle))
dbeddfb9 493{
dbeddfb9 494 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
17ad51ed 495 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
73b1b996
VZ
496 if ( !label.empty() )
497 [v setTitle:wxCFStringRef(label).AsNSString()];
498
499 SetBezelStyleFromBorderFlags(v, style);
500
501 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );
dbeddfb9 502}