]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/button.mm
fixing class
[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 the keyboard accelerator key from the label (e.g. "Click &Me")
202 void wxButton::OSXSetAcceleratorFromLabel(const wxString& label)
203 {
204 // Skip setting the accelerator for the default buttons as this would
205 // overwrite the default "Enter" which should be preserved.
206 wxTopLevelWindow * const
207 tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
208 if ( tlw )
209 {
210 if ( tlw->GetDefaultItem() == this )
211 return;
212 }
213
214 wxButtonCocoaImpl *impl = static_cast<wxButtonCocoaImpl*>(GetPeer());
215 impl->SetAcceleratorFromLabel(label);
216 }
217
218 extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
219
220 // set bezel style depending on the wxBORDER_XXX flags specified by the style
221 void SetBezelStyleFromBorderFlags(NSButton *v, long style)
222 {
223 if ( style & wxBORDER_NONE )
224 {
225 [v setBezelStyle:NSShadowlessSquareBezelStyle];
226 [v setBordered:NO];
227 }
228 else // we do have a border
229 {
230 // see trac #11128 for a thorough discussion
231 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
232 [v setBezelStyle:NSRegularSquareBezelStyle];
233 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
234 [v setBezelStyle:NSSmallSquareBezelStyle];
235 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
236 [v setBezelStyle:NSShadowlessSquareBezelStyle];
237 else
238 [v setBezelStyle:NSRegularSquareBezelStyle];
239 }
240 }
241
242
243 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
244 wxWindowMac* WXUNUSED(parent),
245 wxWindowID id,
246 const wxString& label,
247 const wxPoint& pos,
248 const wxSize& size,
249 long style,
250 long WXUNUSED(extraStyle))
251 {
252 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
253 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
254
255 // We can't display a custom label inside a button with help bezel style so
256 // we only use it if we are using the default label. wxButton itself checks
257 // if the label is just "Help" in which case it discards it and passes us
258 // an empty string.
259 if ( id == wxID_HELP && label.empty() )
260 {
261 [v setBezelStyle:NSHelpButtonBezelStyle];
262 }
263 else
264 {
265 if ( style & wxBORDER_NONE )
266 {
267 [v setBezelStyle:NSShadowlessSquareBezelStyle];
268 [v setBordered:NO];
269 }
270 else
271 {
272 // the following styles only exist for certain sizes, so avoid them for
273 // multi-line
274 if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
275 {
276 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
277 [v setBezelStyle:NSRoundedBezelStyle];
278 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
279 [v setBezelStyle:NSTexturedRoundedBezelStyle];
280 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
281 [v setBezelStyle:NSShadowlessSquareBezelStyle];
282 else
283 [v setBezelStyle:NSRoundedBezelStyle];
284 }
285 else
286 {
287 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
288 [v setBezelStyle:NSRegularSquareBezelStyle];
289 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
290 [v setBezelStyle:NSSmallSquareBezelStyle];
291 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
292 [v setBezelStyle:NSShadowlessSquareBezelStyle];
293 else
294 [v setBezelStyle:NSRegularSquareBezelStyle];
295 }
296
297 }
298 }
299
300 [v setButtonType:NSMomentaryPushInButton];
301 wxButtonCocoaImpl* const impl = new wxButtonCocoaImpl( wxpeer, v );
302 impl->SetAcceleratorFromLabel(label);
303 return impl;
304 }
305
306 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
307 {
308 if ( [m_osxView isKindOfClass:[NSButton class]] )
309 {
310 if ( isDefault )
311 {
312 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
313 [(NSButton*)m_osxView setKeyEquivalentModifierMask: 0];
314 }
315 else
316 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
317 }
318 }
319
320 void wxWidgetCocoaImpl::PerformClick()
321 {
322 if ([m_osxView isKindOfClass:[NSControl class]])
323 [(NSControl*)m_osxView performClick:nil];
324 }
325
326 #if wxUSE_BMPBUTTON
327
328 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
329 wxWindowMac* WXUNUSED(parent),
330 wxWindowID WXUNUSED(id),
331 const wxBitmap& bitmap,
332 const wxPoint& pos,
333 const wxSize& size,
334 long style,
335 long WXUNUSED(extraStyle))
336 {
337 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
338 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
339
340 SetBezelStyleFromBorderFlags(v, style);
341
342 if (bitmap.IsOk())
343 [v setImage:bitmap.GetNSImage() ];
344
345 [v setButtonType:NSMomentaryPushInButton];
346 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
347 return c;
348 }
349
350 #endif // wxUSE_BMPBUTTON
351
352 //
353 // wxDisclosureButton implementation
354 //
355
356 @interface wxDisclosureNSButton : NSButton
357 {
358
359 BOOL isOpen;
360 }
361
362 - (void) updateImage;
363
364 - (void) toggle;
365
366 + (NSImage *)rotateImage: (NSImage *)image;
367
368 @end
369
370 static const char * disc_triangle_xpm[] = {
371 "10 9 4 1",
372 " c None",
373 ". c #737373",
374 "+ c #989898",
375 "- c #c6c6c6",
376 " .- ",
377 " ..+- ",
378 " ....+ ",
379 " ......- ",
380 " .......- ",
381 " ......- ",
382 " ....+ ",
383 " ..+- ",
384 " .- ",
385 };
386
387 @implementation wxDisclosureNSButton
388
389 + (void)initialize
390 {
391 static BOOL initialized = NO;
392 if (!initialized)
393 {
394 initialized = YES;
395 wxOSXCocoaClassAddWXMethods( self );
396 }
397 }
398
399 - (id) initWithFrame:(NSRect) frame
400 {
401 self = [super initWithFrame:frame];
402 isOpen = NO;
403 [self setImagePosition:NSImageLeft];
404 [self updateImage];
405 return self;
406 }
407
408 - (int) intValue
409 {
410 return isOpen ? 1 : 0;
411 }
412
413 - (void) setIntValue: (int) v
414 {
415 isOpen = ( v != 0 );
416 [self updateImage];
417 }
418
419 - (void) toggle
420 {
421 isOpen = !isOpen;
422 [self updateImage];
423 }
424
425 wxCFRef<NSImage*> downArray ;
426
427 - (void) updateImage
428 {
429 static wxBitmap trianglebm(disc_triangle_xpm);
430 if ( downArray.get() == NULL )
431 {
432 downArray.reset( [[wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] retain] );
433 }
434
435 if ( isOpen )
436 [self setImage:(NSImage*)downArray.get()];
437 else
438 [self setImage:trianglebm.GetNSImage()];
439 }
440
441 + (NSImage *)rotateImage: (NSImage *)image
442 {
443 NSSize imageSize = [image size];
444 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
445 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
446
447 [newImage lockFocus];
448
449 NSAffineTransform* tm = [NSAffineTransform transform];
450 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
451 [tm rotateByDegrees:-90];
452 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
453 [tm concat];
454
455
456 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
457 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
458
459 [newImage unlockFocus];
460 return [newImage autorelease];
461 }
462
463 @end
464
465 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
466 {
467 public :
468 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
469 wxWidgetCocoaImpl(peer, w)
470 {
471 }
472
473 ~wxDisclosureTriangleCocoaImpl()
474 {
475 }
476
477 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
478 {
479 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
480 [db toggle];
481 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
482 }
483 };
484
485 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
486 wxWindowMac* WXUNUSED(parent),
487 wxWindowID WXUNUSED(winid),
488 const wxString& label,
489 const wxPoint& pos,
490 const wxSize& size,
491 long style,
492 long WXUNUSED(extraStyle))
493 {
494 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
495 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
496 if ( !label.empty() )
497 [v setTitle:wxCFStringRef(label).AsNSString()];
498
499 SetBezelStyleFromBorderFlags(v, style);
500
501 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );
502 }