]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/button.mm
Create an NSAutoreleasePool in wxMacWakeUp().
[wxWidgets.git] / src / osx / cocoa / button.mm
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
17 #endif
18
19 #include "wx/osx/private.h"
20
21 wxSize wxButton::DoGetBestSize() const
22 {
23 if ( GetId() == wxID_HELP )
24 return wxSize( 23 , 23 ) ;
25
26 wxRect r ;
27 m_peer->GetBestRect(&r);
28
29 wxSize sz = r.GetSize();
30
31 const int wBtnStd = GetDefaultSize().x;
32
33 if ( (sz.x < wBtnStd) && !HasFlag(wxBU_EXACTFIT) )
34 sz.x = wBtnStd;
35
36 return sz ;
37 }
38
39 wxSize wxButton::GetDefaultSize()
40 {
41 return wxSize(84, 23);
42 }
43
44 @implementation wxNSButton
45
46 + (void)initialize
47 {
48 static BOOL initialized = NO;
49 if (!initialized)
50 {
51 initialized = YES;
52 wxOSXCocoaClassAddWXMethods( self );
53 }
54 }
55
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
87 namespace
88 {
89
90 class wxButtonCocoaImpl : public wxWidgetCocoaImpl
91 {
92 public:
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
106 private:
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
116
117 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
118 wxWindowMac* WXUNUSED(parent),
119 wxWindowID id,
120 const wxString& WXUNUSED(label),
121 const wxPoint& pos,
122 const wxSize& size,
123 long WXUNUSED(style),
124 long WXUNUSED(extraStyle))
125 {
126 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
127 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
128
129 if ( id == wxID_HELP )
130 {
131 [v setBezelStyle:NSHelpButtonBezelStyle];
132 }
133 else
134 {
135 [v setBezelStyle:NSRoundedBezelStyle];
136 }
137
138 [v setButtonType:NSMomentaryPushInButton];
139 return new wxButtonCocoaImpl( wxpeer, v );
140 }
141
142 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
143 {
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" ];
148 }
149
150 void wxWidgetCocoaImpl::PerformClick()
151 {
152 if ([m_osxView isKindOfClass:[NSControl class]])
153 [(NSControl*)m_osxView performClick:nil];
154 }
155
156 //
157 // wxDisclosureButton implementation
158 //
159
160 @interface wxDisclosureNSButton : NSButton
161 {
162
163 BOOL isOpen;
164 }
165
166 - (void) updateImage;
167
168 - (void) toggle;
169
170 + (NSImage *)rotateImage: (NSImage *)image;
171
172 @end
173
174 static const char * disc_triangle_xpm[] = {
175 "10 9 4 1",
176 " c None",
177 ". c #737373",
178 "+ c #989898",
179 "- c #c6c6c6",
180 " .- ",
181 " ..+- ",
182 " ....+ ",
183 " ......- ",
184 " .......- ",
185 " ......- ",
186 " ....+ ",
187 " ..+- ",
188 " .- ",
189 };
190
191 @implementation wxDisclosureNSButton
192
193 + (void)initialize
194 {
195 static BOOL initialized = NO;
196 if (!initialized)
197 {
198 initialized = YES;
199 wxOSXCocoaClassAddWXMethods( self );
200 }
201 }
202
203 - (id) initWithFrame:(NSRect) frame
204 {
205 self = [super initWithFrame:frame];
206 [self setBezelStyle:NSSmallSquareBezelStyle];
207 isOpen = NO;
208 [self setImagePosition:NSImageLeft];
209 [self updateImage];
210 return self;
211 }
212
213 - (int) intValue
214 {
215 return isOpen ? 1 : 0;
216 }
217
218 - (void) setIntValue: (int) v
219 {
220 isOpen = ( v != 0 );
221 [self updateImage];
222 }
223
224 - (void) toggle
225 {
226 isOpen = !isOpen;
227 [self updateImage];
228 }
229
230 wxCFRef<NSImage*> downArray ;
231
232 - (void) updateImage
233 {
234 static wxBitmap trianglebm(disc_triangle_xpm);
235 if ( downArray.get() == NULL )
236 {
237 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
238 }
239
240 if ( isOpen )
241 [self setImage:(NSImage*)downArray.get()];
242 else
243 [self setImage:trianglebm.GetNSImage()];
244 }
245
246 + (NSImage *)rotateImage: (NSImage *)image
247 {
248 NSSize imageSize = [image size];
249 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
250 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
251
252 [newImage lockFocus];
253
254 NSAffineTransform* tm = [NSAffineTransform transform];
255 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
256 [tm rotateByDegrees:-90];
257 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
258 [tm concat];
259
260
261 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
262 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
263
264 [newImage unlockFocus];
265 return newImage;
266 }
267
268 @end
269
270 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
271 {
272 public :
273 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
274 wxWidgetCocoaImpl(peer, w)
275 {
276 }
277
278 ~wxDisclosureTriangleCocoaImpl()
279 {
280 }
281
282 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
283 {
284 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
285 [db toggle];
286 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
287 }
288 };
289
290 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
291 wxWindowMac* WXUNUSED(parent),
292 wxWindowID WXUNUSED(id),
293 const wxString& label,
294 const wxPoint& pos,
295 const wxSize& size,
296 long WXUNUSED(style),
297 long WXUNUSED(extraStyle))
298 {
299 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
300 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
301 [v setTitle:wxCFStringRef( label).AsNSString()];
302 wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v );
303 return c;
304 }