]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/button.mm
removing 10.4 code, adding webkit trick for showing dynamic tooltips
[wxWidgets.git] / src / osx / cocoa / button.mm
CommitLineData
f033830e
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/cocoa/button.mm
3// Purpose: wxButton
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
a9a4f229 7// RCS-ID: $Id$
f033830e
SC
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
f033830e 14#ifndef WX_PRECOMP
d6eb3ff8 15#include "wx/object.h"
f033830e
SC
16#endif
17
d6eb3ff8
SC
18#include "wx/button.h"
19
f033830e
SC
20#include "wx/osx/private.h"
21
f672c969
VZ
22#if wxUSE_MARKUP
23 #include "wx/osx/cocoa/private/markuptoattr.h"
24#endif // wxUSE_MARKUP
25
26
f033830e
SC
27@implementation wxNSButton
28
4dd9fdf8 29+ (void)initialize
f033830e 30{
4dd9fdf8 31 static BOOL initialized = NO;
b727fcd3 32 if (!initialized)
f033830e 33 {
4dd9fdf8
SC
34 initialized = YES;
35 wxOSXCocoaClassAddWXMethods( self );
f033830e
SC
36 }
37}
38
f033830e
SC
39- (int) intValue
40{
41 switch ( [self state] )
42 {
43 case NSOnState:
44 return 1;
45 case NSMixedState:
46 return 2;
47 default:
48 return 0;
49 }
50}
51
52- (void) setIntValue: (int) v
53{
54 switch( v )
55 {
56 case 2:
57 [self setState:NSMixedState];
58 break;
59 case 1:
60 [self setState:NSOnState];
61 break;
62 default :
63 [self setState:NSOffState];
64 break;
65 }
66}
67
411a1c35
SC
68- (void) setTrackingTag: (NSTrackingRectTag)tag
69{
70 rectTag = tag;
71}
72
73- (NSTrackingRectTag) trackingTag
74{
75 return rectTag;
76}
77
f033830e
SC
78@end
79
58ce18f2
SC
80@interface NSView(PossibleSizeMethods)
81- (NSControlSize)controlSize;
82@end
83
e5d05b90
VZ
84namespace
85{
86
b38dc31f 87class wxButtonCocoaImpl : public wxWidgetCocoaImpl, public wxButtonImpl
e5d05b90
VZ
88{
89public:
90 wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
91 : wxWidgetCocoaImpl(wxpeer, v)
92 {
24e059c3 93 SetNeedsFrame(false);
e5d05b90
VZ
94 }
95
96 virtual void SetBitmap(const wxBitmap& bitmap)
97 {
b38dc31f 98 // switch bezel style for plain pushbuttons
b8702c67
SC
99 if ( bitmap.IsOk() )
100 {
101 if ([GetNSButton() bezelStyle] == NSRoundedBezelStyle)
102 [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle];
103 }
104 else
105 {
106 [GetNSButton() setBezelStyle:NSRoundedBezelStyle];
107 }
108
e5d05b90
VZ
109 wxWidgetCocoaImpl::SetBitmap(bitmap);
110 }
111
6a219e34 112#if wxUSE_MARKUP
f672c969
VZ
113 virtual void SetLabelMarkup(const wxString& markup)
114 {
115 wxMarkupToAttrString toAttr(GetWXPeer(), markup);
116 NSMutableAttributedString *attrString = toAttr.GetNSAttributedString();
117
118 // Button text is always centered.
119 NSMutableParagraphStyle *
120 paragraphStyle = [[NSMutableParagraphStyle alloc] init];
121 [paragraphStyle setAlignment: NSCenterTextAlignment];
122 [attrString addAttribute:NSParagraphStyleAttributeName
123 value:paragraphStyle
124 range:NSMakeRange(0, [attrString length])];
125 [paragraphStyle release];
126
127 [GetNSButton() setAttributedTitle:attrString];
128 }
6a219e34 129#endif // wxUSE_MARKUP
f672c969 130
b38dc31f
SC
131 void SetPressedBitmap( const wxBitmap& bitmap )
132 {
133 NSButton* button = GetNSButton();
134 [button setAlternateImage: bitmap.GetNSImage()];
135 [button setButtonType:NSMomentaryChangeButton];
136 }
137
54ea2834
SC
138 void GetLayoutInset(int &left , int &top , int &right, int &bottom) const
139 {
140 left = top = right = bottom = 0;
141 NSControlSize size = NSRegularControlSize;
142 if ( [m_osxView respondsToSelector:@selector(controlSize)] )
58ce18f2 143 size = [m_osxView controlSize];
54ea2834
SC
144 else if ([m_osxView respondsToSelector:@selector(cell)])
145 {
146 id cell = [(id)m_osxView cell];
147 if ([cell respondsToSelector:@selector(controlSize)])
148 size = [cell controlSize];
149 }
150
151 if ( [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
152 {
153 switch( size )
154 {
155 case NSRegularControlSize:
156 left = right = 6;
157 top = 4;
158 bottom = 8;
159 break;
160 case NSSmallControlSize:
161 left = right = 5;
162 top = 4;
163 bottom = 7;
164 break;
165 case NSMiniControlSize:
166 left = right = 1;
167 top = 0;
168 bottom = 2;
169 break;
170 }
171 }
172 }
173
174
e5d05b90
VZ
175private:
176 NSButton *GetNSButton() const
177 {
178 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
179
180 return static_cast<NSButton *>(m_osxView);
181 }
182};
183
df04f800
SC
184} // anonymous namespace
185
7f08aa6c
SC
186extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style);
187
73b1b996
VZ
188// set bezel style depending on the wxBORDER_XXX flags specified by the style
189void SetBezelStyleFromBorderFlags(NSButton *v, long style)
190{
191 if ( style & wxBORDER_NONE )
192 {
193 [v setBezelStyle:NSShadowlessSquareBezelStyle];
194 [v setBordered:NO];
195 }
196 else // we do have a border
197 {
198 // see trac #11128 for a thorough discussion
199 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
200 [v setBezelStyle:NSRegularSquareBezelStyle];
201 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
202 [v setBezelStyle:NSSmallSquareBezelStyle];
24e059c3 203 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
73b1b996 204 [v setBezelStyle:NSShadowlessSquareBezelStyle];
24e059c3
SC
205 else
206 [v setBezelStyle:NSRegularSquareBezelStyle];
73b1b996
VZ
207 }
208}
209
f033830e 210
b727fcd3
VZ
211wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
212 wxWindowMac* WXUNUSED(parent),
213 wxWindowID id,
01495abf 214 const wxString& label,
b727fcd3 215 const wxPoint& pos,
f033830e 216 const wxSize& size,
24e059c3 217 long style,
b727fcd3 218 long WXUNUSED(extraStyle))
f033830e 219{
dbeddfb9 220 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
f033830e 221 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
b727fcd3 222
01495abf
VZ
223 // We can't display a custom label inside a button with help bezel style so
224 // we only use it if we are using the default label. wxButton itself checks
225 // if the label is just "Help" in which case it discards it and passes us
226 // an empty string.
227 if ( id == wxID_HELP && label.empty() )
f033830e
SC
228 {
229 [v setBezelStyle:NSHelpButtonBezelStyle];
230 }
231 else
232 {
24e059c3
SC
233 if ( style & wxBORDER_NONE )
234 {
235 [v setBezelStyle:NSShadowlessSquareBezelStyle];
236 [v setBordered:NO];
237 }
238 else
239 {
240 // the following styles only exist for certain sizes, so avoid them for
241 // multi-line
242 if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
243 {
244 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
245 [v setBezelStyle:NSRoundedBezelStyle];
246 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
247 [v setBezelStyle:NSTexturedRoundedBezelStyle];
248 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
249 [v setBezelStyle:NSShadowlessSquareBezelStyle];
250 else
251 [v setBezelStyle:NSRoundedBezelStyle];
252 }
253 else
254 {
255 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
256 [v setBezelStyle:NSRegularSquareBezelStyle];
257 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
258 [v setBezelStyle:NSSmallSquareBezelStyle];
259 else if ( (style & wxBORDER_MASK) == wxBORDER_SIMPLE )
260 [v setBezelStyle:NSShadowlessSquareBezelStyle];
261 else
262 [v setBezelStyle:NSRegularSquareBezelStyle];
263 }
b727fcd3 264
24e059c3
SC
265 }
266 }
267
f033830e 268 [v setButtonType:NSMomentaryPushInButton];
e5d05b90 269 return new wxButtonCocoaImpl( wxpeer, v );
f033830e
SC
270}
271
272void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
b727fcd3 273{
3f30bd1a
SC
274 if ( [m_osxView isKindOfClass:[NSButton class]] )
275 {
276 if ( isDefault )
277 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
278 else
279 [(NSButton*)m_osxView setKeyEquivalent: @"" ];
280 }
f033830e
SC
281}
282
b727fcd3 283void wxWidgetCocoaImpl::PerformClick()
f033830e 284{
73b1b996
VZ
285 if ([m_osxView isKindOfClass:[NSControl class]])
286 [(NSControl*)m_osxView performClick:nil];
f033830e
SC
287}
288
b38dc31f
SC
289#if wxUSE_BMPBUTTON
290
291wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
292 wxWindowMac* WXUNUSED(parent),
293 wxWindowID WXUNUSED(id),
294 const wxBitmap& bitmap,
295 const wxPoint& pos,
296 const wxSize& size,
297 long style,
298 long WXUNUSED(extraStyle))
299{
300 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
301 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
73b1b996
VZ
302
303 SetBezelStyleFromBorderFlags(v, style);
304
a1b806b9 305 if (bitmap.IsOk())
b38dc31f 306 [v setImage:bitmap.GetNSImage() ];
73b1b996 307
b38dc31f
SC
308 [v setButtonType:NSMomentaryPushInButton];
309 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
310 return c;
311}
312
73b1b996 313#endif // wxUSE_BMPBUTTON
b38dc31f 314
17ad51ed
SC
315//
316// wxDisclosureButton implementation
317//
318
319@interface wxDisclosureNSButton : NSButton
320{
321
322 BOOL isOpen;
323}
324
325- (void) updateImage;
326
327- (void) toggle;
328
329+ (NSImage *)rotateImage: (NSImage *)image;
330
331@end
332
da52d42b
SC
333static const char * disc_triangle_xpm[] = {
334"10 9 4 1",
335" c None",
336". c #737373",
337"+ c #989898",
338"- c #c6c6c6",
339" .- ",
340" ..+- ",
341" ....+ ",
342" ......- ",
343" .......- ",
344" ......- ",
345" ....+ ",
346" ..+- ",
347" .- ",
348};
349
17ad51ed
SC
350@implementation wxDisclosureNSButton
351
352+ (void)initialize
353{
354 static BOOL initialized = NO;
b727fcd3 355 if (!initialized)
17ad51ed
SC
356 {
357 initialized = YES;
358 wxOSXCocoaClassAddWXMethods( self );
359 }
360}
361
362- (id) initWithFrame:(NSRect) frame
363{
364 self = [super initWithFrame:frame];
17ad51ed
SC
365 isOpen = NO;
366 [self setImagePosition:NSImageLeft];
367 [self updateImage];
368 return self;
369}
370
371- (int) intValue
372{
373 return isOpen ? 1 : 0;
374}
375
376- (void) setIntValue: (int) v
377{
378 isOpen = ( v != 0 );
379 [self updateImage];
380}
381
382- (void) toggle
383{
384 isOpen = !isOpen;
385 [self updateImage];
386}
387
388wxCFRef<NSImage*> downArray ;
389
390- (void) updateImage
391{
da52d42b 392 static wxBitmap trianglebm(disc_triangle_xpm);
17ad51ed
SC
393 if ( downArray.get() == NULL )
394 {
adf264f2 395 downArray.reset( [[wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] retain] );
17ad51ed 396 }
b727fcd3 397
17ad51ed
SC
398 if ( isOpen )
399 [self setImage:(NSImage*)downArray.get()];
400 else
da52d42b 401 [self setImage:trianglebm.GetNSImage()];
17ad51ed
SC
402}
403
404+ (NSImage *)rotateImage: (NSImage *)image
405{
406 NSSize imageSize = [image size];
407 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
408 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
b727fcd3 409
17ad51ed 410 [newImage lockFocus];
b727fcd3 411
17ad51ed
SC
412 NSAffineTransform* tm = [NSAffineTransform transform];
413 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
414 [tm rotateByDegrees:-90];
415 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
416 [tm concat];
b727fcd3
VZ
417
418
17ad51ed
SC
419 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
420 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
b727fcd3 421
17ad51ed 422 [newImage unlockFocus];
adf264f2 423 return [newImage autorelease];
17ad51ed
SC
424}
425
426@end
427
428class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
429{
430public :
431 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
432 wxWidgetCocoaImpl(peer, w)
433 {
434 }
b727fcd3 435
17ad51ed
SC
436 ~wxDisclosureTriangleCocoaImpl()
437 {
438 }
439
440 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
441 {
442 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
443 [db toggle];
444 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
445 }
446};
447
b727fcd3
VZ
448wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
449 wxWindowMac* WXUNUSED(parent),
73b1b996 450 wxWindowID WXUNUSED(winid),
dbeddfb9 451 const wxString& label,
b727fcd3 452 const wxPoint& pos,
dbeddfb9 453 const wxSize& size,
73b1b996 454 long style,
b727fcd3 455 long WXUNUSED(extraStyle))
dbeddfb9 456{
dbeddfb9 457 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
17ad51ed 458 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
73b1b996
VZ
459 if ( !label.empty() )
460 [v setTitle:wxCFStringRef(label).AsNSString()];
461
462 SetBezelStyleFromBorderFlags(v, style);
463
464 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );
dbeddfb9 465}