]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/button.mm
adapting to init pattern
[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
14#include "wx/button.h"
15
16#ifndef WX_PRECOMP
f033830e
SC
17#endif
18
f033830e
SC
19#include "wx/osx/private.h"
20
f672c969
VZ
21#if wxUSE_MARKUP
22 #include "wx/osx/cocoa/private/markuptoattr.h"
23#endif // wxUSE_MARKUP
24
25
f033830e
SC
26wxSize wxButton::DoGetBestSize() const
27{
01495abf
VZ
28 // We only use help button bezel if we don't have any (non standard) label
29 // to display in the button. Otherwise even wxID_HELP buttons look like
30 // normal push buttons.
31 if ( GetId() == wxID_HELP && GetLabel().empty() )
423939b2 32 return wxSize( 23 , 23 ) ;
f033830e 33
dbeddfb9 34 wxRect r ;
22756322 35 GetPeer()->GetBestRect(&r);
dbeddfb9 36
1fe0e46e 37 wxSize sz = r.GetSize();
54ea2834
SC
38 sz.x = sz.x + MacGetLeftBorderSize() +
39 MacGetRightBorderSize();
40 sz.y = sz.y + MacGetTopBorderSize() +
41 MacGetBottomBorderSize();
42
1fe0e46e 43 const int wBtnStd = GetDefaultSize().x;
f033830e 44
1fe0e46e
VZ
45 if ( (sz.x < wBtnStd) && !HasFlag(wxBU_EXACTFIT) )
46 sz.x = wBtnStd;
f033830e
SC
47
48 return sz ;
49}
50
51wxSize wxButton::GetDefaultSize()
52{
54ea2834 53 return wxSize(84, 20);
f033830e
SC
54}
55
56@implementation wxNSButton
57
4dd9fdf8 58+ (void)initialize
f033830e 59{
4dd9fdf8 60 static BOOL initialized = NO;
b727fcd3 61 if (!initialized)
f033830e 62 {
4dd9fdf8
SC
63 initialized = YES;
64 wxOSXCocoaClassAddWXMethods( self );
f033830e
SC
65 }
66}
67
f033830e
SC
68- (int) intValue
69{
70 switch ( [self state] )
71 {
72 case NSOnState:
73 return 1;
74 case NSMixedState:
75 return 2;
76 default:
77 return 0;
78 }
79}
80
81- (void) setIntValue: (int) v
82{
83 switch( v )
84 {
85 case 2:
86 [self setState:NSMixedState];
87 break;
88 case 1:
89 [self setState:NSOnState];
90 break;
91 default :
92 [self setState:NSOffState];
93 break;
94 }
95}
96
411a1c35
SC
97- (void) setTrackingTag: (NSTrackingRectTag)tag
98{
99 rectTag = tag;
100}
101
102- (NSTrackingRectTag) trackingTag
103{
104 return rectTag;
105}
106
f033830e
SC
107@end
108
58ce18f2
SC
109@interface NSView(PossibleSizeMethods)
110- (NSControlSize)controlSize;
111@end
112
e5d05b90
VZ
113namespace
114{
115
b38dc31f 116class wxButtonCocoaImpl : public wxWidgetCocoaImpl, public wxButtonImpl
e5d05b90
VZ
117{
118public:
119 wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
120 : wxWidgetCocoaImpl(wxpeer, v)
121 {
122 }
123
124 virtual void SetBitmap(const wxBitmap& bitmap)
125 {
b38dc31f
SC
126 // switch bezel style for plain pushbuttons
127 if ( bitmap.IsOk() && [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
128 [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle ];
e5d05b90
VZ
129
130 wxWidgetCocoaImpl::SetBitmap(bitmap);
131 }
132
6a219e34 133#if wxUSE_MARKUP
f672c969
VZ
134 virtual void SetLabelMarkup(const wxString& markup)
135 {
136 wxMarkupToAttrString toAttr(GetWXPeer(), markup);
137 NSMutableAttributedString *attrString = toAttr.GetNSAttributedString();
138
139 // Button text is always centered.
140 NSMutableParagraphStyle *
141 paragraphStyle = [[NSMutableParagraphStyle alloc] init];
142 [paragraphStyle setAlignment: NSCenterTextAlignment];
143 [attrString addAttribute:NSParagraphStyleAttributeName
144 value:paragraphStyle
145 range:NSMakeRange(0, [attrString length])];
146 [paragraphStyle release];
147
148 [GetNSButton() setAttributedTitle:attrString];
149 }
6a219e34 150#endif // wxUSE_MARKUP
f672c969 151
b38dc31f
SC
152 void SetPressedBitmap( const wxBitmap& bitmap )
153 {
154 NSButton* button = GetNSButton();
155 [button setAlternateImage: bitmap.GetNSImage()];
156 [button setButtonType:NSMomentaryChangeButton];
157 }
158
54ea2834
SC
159 void GetLayoutInset(int &left , int &top , int &right, int &bottom) const
160 {
161 left = top = right = bottom = 0;
162 NSControlSize size = NSRegularControlSize;
163 if ( [m_osxView respondsToSelector:@selector(controlSize)] )
58ce18f2 164 size = [m_osxView controlSize];
54ea2834
SC
165 else if ([m_osxView respondsToSelector:@selector(cell)])
166 {
167 id cell = [(id)m_osxView cell];
168 if ([cell respondsToSelector:@selector(controlSize)])
169 size = [cell controlSize];
170 }
171
172 if ( [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
173 {
174 switch( size )
175 {
176 case NSRegularControlSize:
177 left = right = 6;
178 top = 4;
179 bottom = 8;
180 break;
181 case NSSmallControlSize:
182 left = right = 5;
183 top = 4;
184 bottom = 7;
185 break;
186 case NSMiniControlSize:
187 left = right = 1;
188 top = 0;
189 bottom = 2;
190 break;
191 }
192 }
193 }
194
195
e5d05b90
VZ
196private:
197 NSButton *GetNSButton() const
198 {
199 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
200
201 return static_cast<NSButton *>(m_osxView);
202 }
203};
204
df04f800
SC
205} // anonymous namespace
206
7f08aa6c
SC
207extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
208
73b1b996
VZ
209// set bezel style depending on the wxBORDER_XXX flags specified by the style
210void SetBezelStyleFromBorderFlags(NSButton *v, long style)
211{
212 if ( style & wxBORDER_NONE )
213 {
214 [v setBezelStyle:NSShadowlessSquareBezelStyle];
215 [v setBordered:NO];
216 }
217 else // we do have a border
218 {
219 // see trac #11128 for a thorough discussion
220 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
221 [v setBezelStyle:NSRegularSquareBezelStyle];
222 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
223 [v setBezelStyle:NSSmallSquareBezelStyle];
224 else
225 [v setBezelStyle:NSShadowlessSquareBezelStyle];
226 }
227}
228
f033830e 229
b727fcd3
VZ
230wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
231 wxWindowMac* WXUNUSED(parent),
232 wxWindowID id,
01495abf 233 const wxString& label,
b727fcd3 234 const wxPoint& pos,
f033830e 235 const wxSize& size,
b727fcd3
VZ
236 long WXUNUSED(style),
237 long WXUNUSED(extraStyle))
f033830e 238{
dbeddfb9 239 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
f033830e 240 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
b727fcd3 241
01495abf
VZ
242 // We can't display a custom label inside a button with help bezel style so
243 // we only use it if we are using the default label. wxButton itself checks
244 // if the label is just "Help" in which case it discards it and passes us
245 // an empty string.
246 if ( id == wxID_HELP && label.empty() )
f033830e
SC
247 {
248 [v setBezelStyle:NSHelpButtonBezelStyle];
249 }
250 else
251 {
252 [v setBezelStyle:NSRoundedBezelStyle];
253 }
b727fcd3 254
f033830e 255 [v setButtonType:NSMomentaryPushInButton];
e5d05b90 256 return new wxButtonCocoaImpl( wxpeer, v );
f033830e
SC
257}
258
259void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
b727fcd3 260{
3f30bd1a
SC
261 if ( [m_osxView isKindOfClass:[NSButton class]] )
262 {
263 if ( isDefault )
264 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
265 else
266 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
267 }
f033830e
SC
268}
269
b727fcd3 270void wxWidgetCocoaImpl::PerformClick()
f033830e 271{
73b1b996
VZ
272 if ([m_osxView isKindOfClass:[NSControl class]])
273 [(NSControl*)m_osxView performClick:nil];
f033830e
SC
274}
275
b38dc31f
SC
276#if wxUSE_BMPBUTTON
277
278wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
279 wxWindowMac* WXUNUSED(parent),
280 wxWindowID WXUNUSED(id),
281 const wxBitmap& bitmap,
282 const wxPoint& pos,
283 const wxSize& size,
284 long style,
285 long WXUNUSED(extraStyle))
286{
287 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
288 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
73b1b996
VZ
289
290 SetBezelStyleFromBorderFlags(v, style);
291
a1b806b9 292 if (bitmap.IsOk())
b38dc31f 293 [v setImage:bitmap.GetNSImage() ];
73b1b996 294
b38dc31f
SC
295 [v setButtonType:NSMomentaryPushInButton];
296 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
297 return c;
298}
299
73b1b996 300#endif // wxUSE_BMPBUTTON
b38dc31f 301
17ad51ed
SC
302//
303// wxDisclosureButton implementation
304//
305
306@interface wxDisclosureNSButton : NSButton
307{
308
309 BOOL isOpen;
310}
311
312- (void) updateImage;
313
314- (void) toggle;
315
316+ (NSImage *)rotateImage: (NSImage *)image;
317
318@end
319
da52d42b
SC
320static const char * disc_triangle_xpm[] = {
321"10 9 4 1",
322" c None",
323". c #737373",
324"+ c #989898",
325"- c #c6c6c6",
326" .- ",
327" ..+- ",
328" ....+ ",
329" ......- ",
330" .......- ",
331" ......- ",
332" ....+ ",
333" ..+- ",
334" .- ",
335};
336
17ad51ed
SC
337@implementation wxDisclosureNSButton
338
339+ (void)initialize
340{
341 static BOOL initialized = NO;
b727fcd3 342 if (!initialized)
17ad51ed
SC
343 {
344 initialized = YES;
345 wxOSXCocoaClassAddWXMethods( self );
346 }
347}
348
349- (id) initWithFrame:(NSRect) frame
350{
351 self = [super initWithFrame:frame];
17ad51ed
SC
352 isOpen = NO;
353 [self setImagePosition:NSImageLeft];
354 [self updateImage];
355 return self;
356}
357
358- (int) intValue
359{
360 return isOpen ? 1 : 0;
361}
362
363- (void) setIntValue: (int) v
364{
365 isOpen = ( v != 0 );
366 [self updateImage];
367}
368
369- (void) toggle
370{
371 isOpen = !isOpen;
372 [self updateImage];
373}
374
375wxCFRef<NSImage*> downArray ;
376
377- (void) updateImage
378{
da52d42b 379 static wxBitmap trianglebm(disc_triangle_xpm);
17ad51ed
SC
380 if ( downArray.get() == NULL )
381 {
da52d42b 382 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
17ad51ed 383 }
b727fcd3 384
17ad51ed
SC
385 if ( isOpen )
386 [self setImage:(NSImage*)downArray.get()];
387 else
da52d42b 388 [self setImage:trianglebm.GetNSImage()];
17ad51ed
SC
389}
390
391+ (NSImage *)rotateImage: (NSImage *)image
392{
393 NSSize imageSize = [image size];
394 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
395 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
b727fcd3 396
17ad51ed 397 [newImage lockFocus];
b727fcd3 398
17ad51ed
SC
399 NSAffineTransform* tm = [NSAffineTransform transform];
400 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
401 [tm rotateByDegrees:-90];
402 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
403 [tm concat];
b727fcd3
VZ
404
405
17ad51ed
SC
406 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
407 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
b727fcd3 408
17ad51ed
SC
409 [newImage unlockFocus];
410 return newImage;
411}
412
413@end
414
415class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
416{
417public :
418 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
419 wxWidgetCocoaImpl(peer, w)
420 {
421 }
b727fcd3 422
17ad51ed
SC
423 ~wxDisclosureTriangleCocoaImpl()
424 {
425 }
426
427 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
428 {
429 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
430 [db toggle];
431 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
432 }
433};
434
b727fcd3
VZ
435wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
436 wxWindowMac* WXUNUSED(parent),
73b1b996 437 wxWindowID WXUNUSED(winid),
dbeddfb9 438 const wxString& label,
b727fcd3 439 const wxPoint& pos,
dbeddfb9 440 const wxSize& size,
73b1b996 441 long style,
b727fcd3 442 long WXUNUSED(extraStyle))
dbeddfb9 443{
dbeddfb9 444 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
17ad51ed 445 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
73b1b996
VZ
446 if ( !label.empty() )
447 [v setTitle:wxCFStringRef(label).AsNSString()];
448
449 SetBezelStyleFromBorderFlags(v, style);
450
451 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );
dbeddfb9 452}