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