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