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