]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/button.mm
correcting #ifdef to #if, fixes #11062
[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{
23 if ( GetId() == wxID_HELP )
423939b2 24 return wxSize( 23 , 23 ) ;
f033830e 25
dbeddfb9 26 wxRect r ;
dbeddfb9
SC
27 m_peer->GetBestRect(&r);
28
1fe0e46e 29 wxSize sz = r.GetSize();
dbeddfb9 30
1fe0e46e 31 const int wBtnStd = GetDefaultSize().x;
f033830e 32
1fe0e46e
VZ
33 if ( (sz.x < wBtnStd) && !HasFlag(wxBU_EXACTFIT) )
34 sz.x = wBtnStd;
f033830e
SC
35
36 return sz ;
37}
38
39wxSize wxButton::GetDefaultSize()
40{
1fe0e46e 41 return wxSize(84, 23);
f033830e
SC
42}
43
44@implementation wxNSButton
45
4dd9fdf8 46+ (void)initialize
f033830e 47{
4dd9fdf8 48 static BOOL initialized = NO;
b727fcd3 49 if (!initialized)
f033830e 50 {
4dd9fdf8
SC
51 initialized = YES;
52 wxOSXCocoaClassAddWXMethods( self );
f033830e
SC
53 }
54}
55
f033830e
SC
56- (int) intValue
57{
58 switch ( [self state] )
59 {
60 case NSOnState:
61 return 1;
62 case NSMixedState:
63 return 2;
64 default:
65 return 0;
66 }
67}
68
69- (void) setIntValue: (int) v
70{
71 switch( v )
72 {
73 case 2:
74 [self setState:NSMixedState];
75 break;
76 case 1:
77 [self setState:NSOnState];
78 break;
79 default :
80 [self setState:NSOffState];
81 break;
82 }
83}
84
85@end
86
e5d05b90
VZ
87namespace
88{
89
90class wxButtonCocoaImpl : public wxWidgetCocoaImpl
91{
92public:
93 wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
94 : wxWidgetCocoaImpl(wxpeer, v)
95 {
96 }
97
98 virtual void SetBitmap(const wxBitmap& bitmap)
99 {
100 [GetNSButton() setBezelStyle:bitmap.IsOk() ? NSRegularSquareBezelStyle
101 : NSRoundedBezelStyle];
102
103 wxWidgetCocoaImpl::SetBitmap(bitmap);
104 }
105
106private:
107 NSButton *GetNSButton() const
108 {
109 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
110
111 return static_cast<NSButton *>(m_osxView);
112 }
113};
114
115} // anonymous namespace
f033830e 116
b727fcd3
VZ
117wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
118 wxWindowMac* WXUNUSED(parent),
119 wxWindowID id,
d8207702 120 const wxString& WXUNUSED(label),
b727fcd3 121 const wxPoint& pos,
f033830e 122 const wxSize& size,
b727fcd3
VZ
123 long WXUNUSED(style),
124 long WXUNUSED(extraStyle))
f033830e 125{
dbeddfb9 126 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
f033830e 127 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
b727fcd3 128
f033830e
SC
129 if ( id == wxID_HELP )
130 {
131 [v setBezelStyle:NSHelpButtonBezelStyle];
132 }
133 else
134 {
135 [v setBezelStyle:NSRoundedBezelStyle];
136 }
b727fcd3 137
f033830e 138 [v setButtonType:NSMomentaryPushInButton];
e5d05b90 139 return new wxButtonCocoaImpl( wxpeer, v );
f033830e
SC
140}
141
142void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
b727fcd3 143{
5ec8cc4d
KO
144 if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
145 // NOTE: setKeyEquivalent: nil will trigger an assert
146 // instead do not call in that case.
147 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
f033830e
SC
148}
149
b727fcd3 150void wxWidgetCocoaImpl::PerformClick()
f033830e
SC
151{
152}
153
17ad51ed
SC
154//
155// wxDisclosureButton implementation
156//
157
158@interface wxDisclosureNSButton : NSButton
159{
160
161 BOOL isOpen;
162}
163
164- (void) updateImage;
165
166- (void) toggle;
167
168+ (NSImage *)rotateImage: (NSImage *)image;
169
170@end
171
da52d42b
SC
172static const char * disc_triangle_xpm[] = {
173"10 9 4 1",
174" c None",
175". c #737373",
176"+ c #989898",
177"- c #c6c6c6",
178" .- ",
179" ..+- ",
180" ....+ ",
181" ......- ",
182" .......- ",
183" ......- ",
184" ....+ ",
185" ..+- ",
186" .- ",
187};
188
17ad51ed
SC
189@implementation wxDisclosureNSButton
190
191+ (void)initialize
192{
193 static BOOL initialized = NO;
b727fcd3 194 if (!initialized)
17ad51ed
SC
195 {
196 initialized = YES;
197 wxOSXCocoaClassAddWXMethods( self );
198 }
199}
200
201- (id) initWithFrame:(NSRect) frame
202{
203 self = [super initWithFrame:frame];
204 [self setBezelStyle:NSSmallSquareBezelStyle];
205 isOpen = NO;
206 [self setImagePosition:NSImageLeft];
207 [self updateImage];
208 return self;
209}
210
211- (int) intValue
212{
213 return isOpen ? 1 : 0;
214}
215
216- (void) setIntValue: (int) v
217{
218 isOpen = ( v != 0 );
219 [self updateImage];
220}
221
222- (void) toggle
223{
224 isOpen = !isOpen;
225 [self updateImage];
226}
227
228wxCFRef<NSImage*> downArray ;
229
230- (void) updateImage
231{
da52d42b 232 static wxBitmap trianglebm(disc_triangle_xpm);
17ad51ed
SC
233 if ( downArray.get() == NULL )
234 {
da52d42b 235 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
17ad51ed 236 }
b727fcd3 237
17ad51ed
SC
238 if ( isOpen )
239 [self setImage:(NSImage*)downArray.get()];
240 else
da52d42b 241 [self setImage:trianglebm.GetNSImage()];
17ad51ed
SC
242}
243
244+ (NSImage *)rotateImage: (NSImage *)image
245{
246 NSSize imageSize = [image size];
247 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
248 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
b727fcd3 249
17ad51ed 250 [newImage lockFocus];
b727fcd3 251
17ad51ed
SC
252 NSAffineTransform* tm = [NSAffineTransform transform];
253 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
254 [tm rotateByDegrees:-90];
255 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
256 [tm concat];
b727fcd3
VZ
257
258
17ad51ed
SC
259 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
260 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
b727fcd3 261
17ad51ed
SC
262 [newImage unlockFocus];
263 return newImage;
264}
265
266@end
267
268class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
269{
270public :
271 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
272 wxWidgetCocoaImpl(peer, w)
273 {
274 }
b727fcd3 275
17ad51ed
SC
276 ~wxDisclosureTriangleCocoaImpl()
277 {
278 }
279
280 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
281 {
282 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
283 [db toggle];
284 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
285 }
286};
287
b727fcd3
VZ
288wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
289 wxWindowMac* WXUNUSED(parent),
290 wxWindowID WXUNUSED(id),
dbeddfb9 291 const wxString& label,
b727fcd3 292 const wxPoint& pos,
dbeddfb9 293 const wxSize& size,
b727fcd3
VZ
294 long WXUNUSED(style),
295 long WXUNUSED(extraStyle))
dbeddfb9 296{
dbeddfb9 297 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
17ad51ed 298 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
dbeddfb9 299 [v setTitle:wxCFStringRef( label).AsNSString()];
17ad51ed 300 wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v );
dbeddfb9
SC
301 return c;
302}