]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/button.mm
No changes, just use wxRecursionGuard instead of manual boolean flag.
[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 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #ifndef WX_PRECOMP
14 #include "wx/object.h"
15 #endif
16
17 #include "wx/button.h"
18 #include "wx/toplevel.h"
19 #include "wx/tglbtn.h"
20
21 #include "wx/osx/private.h"
22
23 #if wxUSE_MARKUP
24 #include "wx/osx/cocoa/private/markuptoattr.h"
25 #endif // wxUSE_MARKUP
26
27
28 @implementation wxNSButton
29
30 + (void)initialize
31 {
32 static BOOL initialized = NO;
33 if (!initialized)
34 {
35 initialized = YES;
36 wxOSXCocoaClassAddWXMethods( self );
37 }
38 }
39
40 - (int) intValue
41 {
42 switch ( [self state] )
43 {
44 case NSOnState:
45 return 1;
46 case NSMixedState:
47 return 2;
48 default:
49 return 0;
50 }
51 }
52
53 - (void) setIntValue: (int) v
54 {
55 switch( v )
56 {
57 case 2:
58 [self setState:NSMixedState];
59 break;
60 case 1:
61 [self setState:NSOnState];
62 break;
63 default :
64 [self setState:NSOffState];
65 break;
66 }
67 }
68
69 - (void) setTrackingTag: (NSTrackingRectTag)tag
70 {
71 rectTag = tag;
72 }
73
74 - (NSTrackingRectTag) trackingTag
75 {
76 return rectTag;
77 }
78
79 @end
80
81 @interface NSView(PossibleSizeMethods)
82 - (NSControlSize)controlSize;
83 @end
84
85 wxButtonCocoaImpl::wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
86 : wxWidgetCocoaImpl(wxpeer, v)
87 {
88 SetNeedsFrame(false);
89 }
90
91 void wxButtonCocoaImpl::SetBitmap(const wxBitmap& bitmap)
92 {
93 // switch bezel style for plain pushbuttons
94 if ( bitmap.IsOk() )
95 {
96 if ([GetNSButton() bezelStyle] == NSRoundedBezelStyle)
97 [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle];
98 }
99 else
100 {
101 [GetNSButton() setBezelStyle:NSRoundedBezelStyle];
102 }
103
104 wxWidgetCocoaImpl::SetBitmap(bitmap);
105 }
106
107 #if wxUSE_MARKUP
108 void wxButtonCocoaImpl::SetLabelMarkup(const wxString& markup)
109 {
110 wxMarkupToAttrString toAttr(GetWXPeer(), markup);
111 NSMutableAttributedString *attrString = toAttr.GetNSAttributedString();
112
113 // Button text is always centered.
114 NSMutableParagraphStyle *
115 paragraphStyle = [[NSMutableParagraphStyle alloc] init];
116 [paragraphStyle setAlignment: NSCenterTextAlignment];
117 [attrString addAttribute:NSParagraphStyleAttributeName
118 value:paragraphStyle
119 range:NSMakeRange(0, [attrString length])];
120 [paragraphStyle release];
121
122 [GetNSButton() setAttributedTitle:attrString];
123 }
124 #endif // wxUSE_MARKUP
125
126 void wxButtonCocoaImpl::SetPressedBitmap( const wxBitmap& bitmap )
127 {
128 NSButton* button = GetNSButton();
129 [button setAlternateImage: bitmap.GetNSImage()];
130 if ( GetWXPeer()->IsKindOf(wxCLASSINFO(wxToggleButton)) )
131 {
132 [button setButtonType:NSToggleButton];
133 }
134 else
135 {
136 [button setButtonType:NSMomentaryChangeButton];
137 }
138 }
139
140 void wxButtonCocoaImpl::GetLayoutInset(int &left , int &top , int &right, int &bottom) const
141 {
142 left = top = right = bottom = 0;
143 NSControlSize size = NSRegularControlSize;
144 if ( [m_osxView respondsToSelector:@selector(controlSize)] )
145 size = [m_osxView controlSize];
146 else if ([m_osxView respondsToSelector:@selector(cell)])
147 {
148 id cell = [(id)m_osxView cell];
149 if ([cell respondsToSelector:@selector(controlSize)])
150 size = [cell controlSize];
151 }
152
153 if ( [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
154 {
155 switch( size )
156 {
157 case NSRegularControlSize:
158 left = right = 6;
159 top = 4;
160 bottom = 8;
161 break;
162 case NSSmallControlSize:
163 left = right = 5;
164 top = 4;
165 bottom = 7;
166 break;
167 case NSMiniControlSize:
168 left = right = 1;
169 top = 0;
170 bottom = 2;
171 break;
172 }
173 }
174 }
175
176 void wxButtonCocoaImpl::SetAcceleratorFromLabel(const wxString& label)
177 {
178 const int accelPos = wxControl::FindAccelIndex(label);
179 if ( accelPos != wxNOT_FOUND )
180 {
181 wxString accelstring(label[accelPos + 1]); // Skip '&' itself
182 accelstring.MakeLower();
183 wxCFStringRef cfText(accelstring);
184 [GetNSButton() setKeyEquivalent:cfText.AsNSString()];
185 [GetNSButton() setKeyEquivalentModifierMask:NSCommandKeyMask];
186 }
187 else
188 {
189 [GetNSButton() setKeyEquivalent:@""];
190 }
191 }
192
193 NSButton *wxButtonCocoaImpl::GetNSButton() const
194 {
195 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
196
197 return static_cast<NSButton *>(m_osxView);
198 }
199
200 // Set bezel style depending on the wxBORDER_XXX flags specified by the style
201 // and also accounting for the label (bezels are different for multiline
202 // buttons and normal ones) and the ID (special bezel is used for help button).
203 //
204 // This is extern because it's also used in src/osx/cocoa/tglbtn.mm.
205 extern "C"
206 void
207 SetBezelStyleFromBorderFlags(NSButton *v,
208 long style,
209 wxWindowID winid,
210 const wxString& label = wxString(),
211 const wxBitmap& bitmap = wxBitmap())
212 {
213 // We can't display a custom label inside a button with help bezel style so
214 // we only use it if we are using the default label. wxButton itself checks
215 // if the label is just "Help" in which case it discards it and passes us
216 // an empty string.
217 if ( winid == wxID_HELP && label.empty() )
218 {
219 [v setBezelStyle:NSHelpButtonBezelStyle];
220 }
221 else
222 {
223 // We can't use rounded bezel styles neither for multiline buttons nor
224 // for buttons containing (big) icons as they are only meant to be used
225 // at certain sizes, so the style used depends on whether the label is
226 // single or multi line.
227 const bool
228 isSimpleText = (label.find_first_of("\n\r") == wxString::npos)
229 && (!bitmap.IsOk() || bitmap.GetHeight() < 20);
230
231 NSBezelStyle bezel;
232 switch ( style & wxBORDER_MASK )
233 {
234 case wxBORDER_NONE:
235 bezel = NSShadowlessSquareBezelStyle;
236 [v setBordered:NO];
237 break;
238
239 case wxBORDER_SIMPLE:
240 bezel = NSShadowlessSquareBezelStyle;
241 break;
242
243 case wxBORDER_SUNKEN:
244 bezel = isSimpleText ? NSTexturedRoundedBezelStyle
245 : NSSmallSquareBezelStyle;
246 break;
247
248 default:
249 wxFAIL_MSG( "Unknown border style" );
250 // fall through
251
252 case 0:
253 case wxBORDER_STATIC:
254 case wxBORDER_RAISED:
255 case wxBORDER_THEME:
256 bezel = isSimpleText ? NSRoundedBezelStyle
257 : NSRegularSquareBezelStyle;
258 break;
259 }
260
261 [v setBezelStyle:bezel];
262 }
263 }
264
265 // Set the keyboard accelerator key from the label (e.g. "Click &Me")
266 void wxButton::OSXUpdateAfterLabelChange(const wxString& label)
267 {
268 wxButtonCocoaImpl *impl = static_cast<wxButtonCocoaImpl*>(GetPeer());
269
270 // Update the bezel style as may be necessary if our new label is multi
271 // line while the old one wasn't (or vice versa).
272 SetBezelStyleFromBorderFlags(impl->GetNSButton(),
273 GetWindowStyle(),
274 GetId(),
275 label);
276
277
278 // Skip setting the accelerator for the default buttons as this would
279 // overwrite the default "Enter" which should be preserved.
280 wxTopLevelWindow * const
281 tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
282 if ( tlw )
283 {
284 if ( tlw->GetDefaultItem() == this )
285 return;
286 }
287
288 impl->SetAcceleratorFromLabel(label);
289 }
290
291
292 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
293 wxWindowMac* WXUNUSED(parent),
294 wxWindowID winid,
295 const wxString& label,
296 const wxPoint& pos,
297 const wxSize& size,
298 long style,
299 long WXUNUSED(extraStyle))
300 {
301 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
302 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
303
304 SetBezelStyleFromBorderFlags(v, style, winid, label);
305
306 [v setButtonType:NSMomentaryPushInButton];
307 wxButtonCocoaImpl* const impl = new wxButtonCocoaImpl( wxpeer, v );
308 impl->SetAcceleratorFromLabel(label);
309 return impl;
310 }
311
312 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
313 {
314 if ( [m_osxView isKindOfClass:[NSButton class]] )
315 {
316 if ( isDefault )
317 {
318 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
319 [(NSButton*)m_osxView setKeyEquivalentModifierMask: 0];
320 }
321 else
322 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
323 }
324 }
325
326 void wxWidgetCocoaImpl::PerformClick()
327 {
328 if ([m_osxView isKindOfClass:[NSControl class]])
329 [(NSControl*)m_osxView performClick:nil];
330 }
331
332 #if wxUSE_BMPBUTTON
333
334 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
335 wxWindowMac* WXUNUSED(parent),
336 wxWindowID winid,
337 const wxBitmap& bitmap,
338 const wxPoint& pos,
339 const wxSize& size,
340 long style,
341 long WXUNUSED(extraStyle))
342 {
343 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
344 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
345
346 SetBezelStyleFromBorderFlags(v, style, winid, wxString(), bitmap);
347
348 if (bitmap.IsOk())
349 [v setImage:bitmap.GetNSImage() ];
350
351 [v setButtonType:NSMomentaryPushInButton];
352 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
353 return c;
354 }
355
356 #endif // wxUSE_BMPBUTTON
357
358 //
359 // wxDisclosureButton implementation
360 //
361
362 @interface wxDisclosureNSButton : NSButton
363 {
364
365 BOOL isOpen;
366 }
367
368 - (void) updateImage;
369
370 - (void) toggle;
371
372 + (NSImage *)rotateImage: (NSImage *)image;
373
374 @end
375
376 static const char * disc_triangle_xpm[] = {
377 "10 9 4 1",
378 " c None",
379 ". c #737373",
380 "+ c #989898",
381 "- c #c6c6c6",
382 " .- ",
383 " ..+- ",
384 " ....+ ",
385 " ......- ",
386 " .......- ",
387 " ......- ",
388 " ....+ ",
389 " ..+- ",
390 " .- ",
391 };
392
393 @implementation wxDisclosureNSButton
394
395 + (void)initialize
396 {
397 static BOOL initialized = NO;
398 if (!initialized)
399 {
400 initialized = YES;
401 wxOSXCocoaClassAddWXMethods( self );
402 }
403 }
404
405 - (id) initWithFrame:(NSRect) frame
406 {
407 self = [super initWithFrame:frame];
408 isOpen = NO;
409 [self setImagePosition:NSImageLeft];
410 [self updateImage];
411 return self;
412 }
413
414 - (int) intValue
415 {
416 return isOpen ? 1 : 0;
417 }
418
419 - (void) setIntValue: (int) v
420 {
421 isOpen = ( v != 0 );
422 [self updateImage];
423 }
424
425 - (void) toggle
426 {
427 isOpen = !isOpen;
428 [self updateImage];
429 }
430
431 wxCFRef<NSImage*> downArray ;
432
433 - (void) updateImage
434 {
435 static wxBitmap trianglebm(disc_triangle_xpm);
436 if ( downArray.get() == NULL )
437 {
438 downArray.reset( [[wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] retain] );
439 }
440
441 if ( isOpen )
442 [self setImage:(NSImage*)downArray.get()];
443 else
444 [self setImage:trianglebm.GetNSImage()];
445 }
446
447 + (NSImage *)rotateImage: (NSImage *)image
448 {
449 NSSize imageSize = [image size];
450 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
451 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
452
453 [newImage lockFocus];
454
455 NSAffineTransform* tm = [NSAffineTransform transform];
456 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
457 [tm rotateByDegrees:-90];
458 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
459 [tm concat];
460
461
462 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
463 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
464
465 [newImage unlockFocus];
466 return [newImage autorelease];
467 }
468
469 @end
470
471 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
472 {
473 public :
474 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
475 wxWidgetCocoaImpl(peer, w)
476 {
477 }
478
479 ~wxDisclosureTriangleCocoaImpl()
480 {
481 }
482
483 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
484 {
485 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
486 [db toggle];
487 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
488 }
489 };
490
491 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
492 wxWindowMac* WXUNUSED(parent),
493 wxWindowID winid,
494 const wxString& label,
495 const wxPoint& pos,
496 const wxSize& size,
497 long style,
498 long WXUNUSED(extraStyle))
499 {
500 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
501 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
502 if ( !label.empty() )
503 [v setTitle:wxCFStringRef(label).AsNSString()];
504
505 SetBezelStyleFromBorderFlags(v, style, winid, label);
506
507 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );
508 }