]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/button.mm
Spell contributor name correctly.
[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 - (void) setTrackingTag: (NSTrackingRectTag)tag
86 {
87 rectTag = tag;
88 }
89
90 - (NSTrackingRectTag) trackingTag
91 {
92 return rectTag;
93 }
94
95 @end
96
97 namespace
98 {
99
100 class wxButtonCocoaImpl : public wxWidgetCocoaImpl, public wxButtonImpl
101 {
102 public:
103 wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
104 : wxWidgetCocoaImpl(wxpeer, v)
105 {
106 }
107
108 virtual void SetBitmap(const wxBitmap& bitmap)
109 {
110 // switch bezel style for plain pushbuttons
111 if ( bitmap.IsOk() && [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
112 [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle ];
113
114 wxWidgetCocoaImpl::SetBitmap(bitmap);
115 }
116
117 void SetPressedBitmap( const wxBitmap& bitmap )
118 {
119 NSButton* button = GetNSButton();
120 [button setAlternateImage: bitmap.GetNSImage()];
121 [button setButtonType:NSMomentaryChangeButton];
122 }
123
124 private:
125 NSButton *GetNSButton() const
126 {
127 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
128
129 return static_cast<NSButton *>(m_osxView);
130 }
131 };
132
133 extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
134
135 // set bezel style depending on the wxBORDER_XXX flags specified by the style
136 void SetBezelStyleFromBorderFlags(NSButton *v, long style)
137 {
138 if ( style & wxBORDER_NONE )
139 {
140 [v setBezelStyle:NSShadowlessSquareBezelStyle];
141 [v setBordered:NO];
142 }
143 else // we do have a border
144 {
145 // see trac #11128 for a thorough discussion
146 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
147 [v setBezelStyle:NSRegularSquareBezelStyle];
148 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
149 [v setBezelStyle:NSSmallSquareBezelStyle];
150 else
151 [v setBezelStyle:NSShadowlessSquareBezelStyle];
152 }
153 }
154
155 } // anonymous namespace
156
157 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
158 wxWindowMac* WXUNUSED(parent),
159 wxWindowID id,
160 const wxString& WXUNUSED(label),
161 const wxPoint& pos,
162 const wxSize& size,
163 long WXUNUSED(style),
164 long WXUNUSED(extraStyle))
165 {
166 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
167 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
168
169 if ( id == wxID_HELP )
170 {
171 [v setBezelStyle:NSHelpButtonBezelStyle];
172 }
173 else
174 {
175 [v setBezelStyle:NSRoundedBezelStyle];
176 }
177
178 [v setButtonType:NSMomentaryPushInButton];
179 return new wxButtonCocoaImpl( wxpeer, v );
180 }
181
182 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
183 {
184 if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
185 // NOTE: setKeyEquivalent: nil will trigger an assert
186 // instead do not call in that case.
187 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
188 }
189
190 void wxWidgetCocoaImpl::PerformClick()
191 {
192 if ([m_osxView isKindOfClass:[NSControl class]])
193 [(NSControl*)m_osxView performClick:nil];
194 }
195
196 #if wxUSE_BMPBUTTON
197
198 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
199 wxWindowMac* WXUNUSED(parent),
200 wxWindowID WXUNUSED(id),
201 const wxBitmap& bitmap,
202 const wxPoint& pos,
203 const wxSize& size,
204 long style,
205 long WXUNUSED(extraStyle))
206 {
207 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
208 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
209
210 SetBezelStyleFromBorderFlags(v, style);
211
212 if (bitmap.Ok())
213 [v setImage:bitmap.GetNSImage() ];
214
215 [v setButtonType:NSMomentaryPushInButton];
216 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
217 return c;
218 }
219
220 #endif // wxUSE_BMPBUTTON
221
222 //
223 // wxDisclosureButton implementation
224 //
225
226 @interface wxDisclosureNSButton : NSButton
227 {
228
229 BOOL isOpen;
230 }
231
232 - (void) updateImage;
233
234 - (void) toggle;
235
236 + (NSImage *)rotateImage: (NSImage *)image;
237
238 @end
239
240 static const char * disc_triangle_xpm[] = {
241 "10 9 4 1",
242 " c None",
243 ". c #737373",
244 "+ c #989898",
245 "- c #c6c6c6",
246 " .- ",
247 " ..+- ",
248 " ....+ ",
249 " ......- ",
250 " .......- ",
251 " ......- ",
252 " ....+ ",
253 " ..+- ",
254 " .- ",
255 };
256
257 @implementation wxDisclosureNSButton
258
259 + (void)initialize
260 {
261 static BOOL initialized = NO;
262 if (!initialized)
263 {
264 initialized = YES;
265 wxOSXCocoaClassAddWXMethods( self );
266 }
267 }
268
269 - (id) initWithFrame:(NSRect) frame
270 {
271 self = [super initWithFrame:frame];
272 isOpen = NO;
273 [self setImagePosition:NSImageLeft];
274 [self updateImage];
275 return self;
276 }
277
278 - (int) intValue
279 {
280 return isOpen ? 1 : 0;
281 }
282
283 - (void) setIntValue: (int) v
284 {
285 isOpen = ( v != 0 );
286 [self updateImage];
287 }
288
289 - (void) toggle
290 {
291 isOpen = !isOpen;
292 [self updateImage];
293 }
294
295 wxCFRef<NSImage*> downArray ;
296
297 - (void) updateImage
298 {
299 static wxBitmap trianglebm(disc_triangle_xpm);
300 if ( downArray.get() == NULL )
301 {
302 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
303 }
304
305 if ( isOpen )
306 [self setImage:(NSImage*)downArray.get()];
307 else
308 [self setImage:trianglebm.GetNSImage()];
309 }
310
311 + (NSImage *)rotateImage: (NSImage *)image
312 {
313 NSSize imageSize = [image size];
314 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
315 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
316
317 [newImage lockFocus];
318
319 NSAffineTransform* tm = [NSAffineTransform transform];
320 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
321 [tm rotateByDegrees:-90];
322 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
323 [tm concat];
324
325
326 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
327 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
328
329 [newImage unlockFocus];
330 return newImage;
331 }
332
333 @end
334
335 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
336 {
337 public :
338 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
339 wxWidgetCocoaImpl(peer, w)
340 {
341 }
342
343 ~wxDisclosureTriangleCocoaImpl()
344 {
345 }
346
347 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
348 {
349 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
350 [db toggle];
351 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
352 }
353 };
354
355 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
356 wxWindowMac* WXUNUSED(parent),
357 wxWindowID WXUNUSED(winid),
358 const wxString& label,
359 const wxPoint& pos,
360 const wxSize& size,
361 long style,
362 long WXUNUSED(extraStyle))
363 {
364 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
365 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
366 if ( !label.empty() )
367 [v setTitle:wxCFStringRef(label).AsNSString()];
368
369 SetBezelStyleFromBorderFlags(v, style);
370
371 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );
372 }