]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/button.mm
On OSX don't propogate the alignment setting from column to renderer if it is a custo...
[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
7f08aa6c
SC
181extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
182
73b1b996
VZ
183// set bezel style depending on the wxBORDER_XXX flags specified by the style
184void SetBezelStyleFromBorderFlags(NSButton *v, long style)
185{
186 if ( style & wxBORDER_NONE )
187 {
188 [v setBezelStyle:NSShadowlessSquareBezelStyle];
189 [v setBordered:NO];
190 }
191 else // we do have a border
192 {
193 // see trac #11128 for a thorough discussion
194 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
195 [v setBezelStyle:NSRegularSquareBezelStyle];
196 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
197 [v setBezelStyle:NSSmallSquareBezelStyle];
198 else
199 [v setBezelStyle:NSShadowlessSquareBezelStyle];
200 }
201}
202
e5d05b90 203} // anonymous namespace
f033830e 204
b727fcd3
VZ
205wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
206 wxWindowMac* WXUNUSED(parent),
207 wxWindowID id,
01495abf 208 const wxString& label,
b727fcd3 209 const wxPoint& pos,
f033830e 210 const wxSize& size,
b727fcd3
VZ
211 long WXUNUSED(style),
212 long WXUNUSED(extraStyle))
f033830e 213{
dbeddfb9 214 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
f033830e 215 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
b727fcd3 216
01495abf
VZ
217 // We can't display a custom label inside a button with help bezel style so
218 // we only use it if we are using the default label. wxButton itself checks
219 // if the label is just "Help" in which case it discards it and passes us
220 // an empty string.
221 if ( id == wxID_HELP && label.empty() )
f033830e
SC
222 {
223 [v setBezelStyle:NSHelpButtonBezelStyle];
224 }
225 else
226 {
227 [v setBezelStyle:NSRoundedBezelStyle];
228 }
b727fcd3 229
f033830e 230 [v setButtonType:NSMomentaryPushInButton];
e5d05b90 231 return new wxButtonCocoaImpl( wxpeer, v );
f033830e
SC
232}
233
234void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
b727fcd3 235{
3f30bd1a
SC
236 if ( [m_osxView isKindOfClass:[NSButton class]] )
237 {
238 if ( isDefault )
239 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
240 else
241 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
242 }
f033830e
SC
243}
244
b727fcd3 245void wxWidgetCocoaImpl::PerformClick()
f033830e 246{
73b1b996
VZ
247 if ([m_osxView isKindOfClass:[NSControl class]])
248 [(NSControl*)m_osxView performClick:nil];
f033830e
SC
249}
250
b38dc31f
SC
251#if wxUSE_BMPBUTTON
252
253wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
254 wxWindowMac* WXUNUSED(parent),
255 wxWindowID WXUNUSED(id),
256 const wxBitmap& bitmap,
257 const wxPoint& pos,
258 const wxSize& size,
259 long style,
260 long WXUNUSED(extraStyle))
261{
262 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
263 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
73b1b996
VZ
264
265 SetBezelStyleFromBorderFlags(v, style);
266
b38dc31f
SC
267 if (bitmap.Ok())
268 [v setImage:bitmap.GetNSImage() ];
73b1b996 269
b38dc31f
SC
270 [v setButtonType:NSMomentaryPushInButton];
271 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
272 return c;
273}
274
73b1b996 275#endif // wxUSE_BMPBUTTON
b38dc31f 276
17ad51ed
SC
277//
278// wxDisclosureButton implementation
279//
280
281@interface wxDisclosureNSButton : NSButton
282{
283
284 BOOL isOpen;
285}
286
287- (void) updateImage;
288
289- (void) toggle;
290
291+ (NSImage *)rotateImage: (NSImage *)image;
292
293@end
294
da52d42b
SC
295static const char * disc_triangle_xpm[] = {
296"10 9 4 1",
297" c None",
298". c #737373",
299"+ c #989898",
300"- c #c6c6c6",
301" .- ",
302" ..+- ",
303" ....+ ",
304" ......- ",
305" .......- ",
306" ......- ",
307" ....+ ",
308" ..+- ",
309" .- ",
310};
311
17ad51ed
SC
312@implementation wxDisclosureNSButton
313
314+ (void)initialize
315{
316 static BOOL initialized = NO;
b727fcd3 317 if (!initialized)
17ad51ed
SC
318 {
319 initialized = YES;
320 wxOSXCocoaClassAddWXMethods( self );
321 }
322}
323
324- (id) initWithFrame:(NSRect) frame
325{
326 self = [super initWithFrame:frame];
17ad51ed
SC
327 isOpen = NO;
328 [self setImagePosition:NSImageLeft];
329 [self updateImage];
330 return self;
331}
332
333- (int) intValue
334{
335 return isOpen ? 1 : 0;
336}
337
338- (void) setIntValue: (int) v
339{
340 isOpen = ( v != 0 );
341 [self updateImage];
342}
343
344- (void) toggle
345{
346 isOpen = !isOpen;
347 [self updateImage];
348}
349
350wxCFRef<NSImage*> downArray ;
351
352- (void) updateImage
353{
da52d42b 354 static wxBitmap trianglebm(disc_triangle_xpm);
17ad51ed
SC
355 if ( downArray.get() == NULL )
356 {
da52d42b 357 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
17ad51ed 358 }
b727fcd3 359
17ad51ed
SC
360 if ( isOpen )
361 [self setImage:(NSImage*)downArray.get()];
362 else
da52d42b 363 [self setImage:trianglebm.GetNSImage()];
17ad51ed
SC
364}
365
366+ (NSImage *)rotateImage: (NSImage *)image
367{
368 NSSize imageSize = [image size];
369 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
370 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
b727fcd3 371
17ad51ed 372 [newImage lockFocus];
b727fcd3 373
17ad51ed
SC
374 NSAffineTransform* tm = [NSAffineTransform transform];
375 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
376 [tm rotateByDegrees:-90];
377 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
378 [tm concat];
b727fcd3
VZ
379
380
17ad51ed
SC
381 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
382 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
b727fcd3 383
17ad51ed
SC
384 [newImage unlockFocus];
385 return newImage;
386}
387
388@end
389
390class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
391{
392public :
393 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
394 wxWidgetCocoaImpl(peer, w)
395 {
396 }
b727fcd3 397
17ad51ed
SC
398 ~wxDisclosureTriangleCocoaImpl()
399 {
400 }
401
402 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
403 {
404 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
405 [db toggle];
406 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
407 }
408};
409
b727fcd3
VZ
410wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
411 wxWindowMac* WXUNUSED(parent),
73b1b996 412 wxWindowID WXUNUSED(winid),
dbeddfb9 413 const wxString& label,
b727fcd3 414 const wxPoint& pos,
dbeddfb9 415 const wxSize& size,
73b1b996 416 long style,
b727fcd3 417 long WXUNUSED(extraStyle))
dbeddfb9 418{
dbeddfb9 419 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
17ad51ed 420 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
73b1b996
VZ
421 if ( !label.empty() )
422 [v setTitle:wxCFStringRef(label).AsNSString()];
423
424 SetBezelStyleFromBorderFlags(v, style);
425
426 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );
dbeddfb9 427}