adding support for layout coordinates via insets from framecoordinates
[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: button.cpp 54845 2008-07-30 14:52:41Z SC $
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 wxSize wxButton::DoGetBestSize() const
22 {
23     // We only use help button bezel if we don't have any (non standard) label
24     // to display in the button. Otherwise even wxID_HELP buttons look like
25     // normal push buttons.
26     if ( GetId() == wxID_HELP && GetLabel().empty() )
27         return wxSize( 23 , 23 ) ;
28
29     wxRect r ;
30     m_peer->GetBestRect(&r);
31
32     wxSize sz = r.GetSize();
33     sz.x  = sz.x  + MacGetLeftBorderSize() +
34     MacGetRightBorderSize();
35     sz.y = sz.y + MacGetTopBorderSize() +
36     MacGetBottomBorderSize();
37     
38     const int wBtnStd = GetDefaultSize().x;
39
40     if ( (sz.x < wBtnStd) && !HasFlag(wxBU_EXACTFIT) )
41         sz.x = wBtnStd;
42
43     return sz ;
44 }
45
46 wxSize wxButton::GetDefaultSize()
47 {
48     return wxSize(84, 20);
49 }
50
51 @implementation wxNSButton
52
53 + (void)initialize
54 {
55     static BOOL initialized = NO;
56     if (!initialized)
57     {
58         initialized = YES;
59         wxOSXCocoaClassAddWXMethods( self );
60     }
61 }
62
63 - (int) intValue
64 {
65     switch ( [self state] )
66     {
67         case NSOnState:
68             return 1;
69         case NSMixedState:
70             return 2;
71         default:
72             return 0;
73     }
74 }
75
76 - (void) setIntValue: (int) v
77 {
78     switch( v )
79     {
80         case 2:
81             [self setState:NSMixedState];
82             break;
83         case 1:
84             [self setState:NSOnState];
85             break;
86         default :
87             [self setState:NSOffState];
88             break;
89     }
90 }
91
92 - (void) setTrackingTag: (NSTrackingRectTag)tag
93 {
94     rectTag = tag;
95 }
96
97 - (NSTrackingRectTag) trackingTag
98 {
99     return rectTag;
100 }
101
102 @end
103
104 namespace
105 {
106
107 class wxButtonCocoaImpl : public wxWidgetCocoaImpl, public wxButtonImpl
108 {
109 public:
110     wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
111         : wxWidgetCocoaImpl(wxpeer, v)
112     {
113     }
114
115     virtual void SetBitmap(const wxBitmap& bitmap)
116     {
117         // switch bezel style for plain pushbuttons
118         if ( bitmap.IsOk() && [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
119             [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle ];
120
121         wxWidgetCocoaImpl::SetBitmap(bitmap);
122     }
123
124     void SetPressedBitmap( const wxBitmap& bitmap )
125     {
126         NSButton* button = GetNSButton();
127         [button setAlternateImage: bitmap.GetNSImage()];
128         [button setButtonType:NSMomentaryChangeButton];
129     }
130
131     void GetLayoutInset(int &left , int &top , int &right, int &bottom) const
132     {
133         left = top = right = bottom = 0;
134         NSControlSize size = NSRegularControlSize;
135         if ( [m_osxView respondsToSelector:@selector(controlSize)] )
136             size = (NSControlSize) [m_osxView controlSize];
137         else if ([m_osxView respondsToSelector:@selector(cell)])
138         {
139             id cell = [(id)m_osxView cell];
140             if ([cell respondsToSelector:@selector(controlSize)])
141                 size = [cell controlSize];
142         }
143         
144         if ( [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
145         {
146             switch( size )
147             {
148                 case NSRegularControlSize:
149                     left = right = 6;
150                     top = 4;
151                     bottom = 8;
152                     break;
153                 case NSSmallControlSize:
154                     left = right = 5;
155                     top = 4;
156                     bottom = 7;
157                     break;
158                 case NSMiniControlSize:
159                     left = right = 1;
160                     top = 0;
161                     bottom = 2;
162                     break;
163             }
164         }
165     }
166     
167     
168 private:
169     NSButton *GetNSButton() const
170     {
171         wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
172
173         return static_cast<NSButton *>(m_osxView);
174     }
175 };
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 } // anonymous namespace
200
201 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
202                                     wxWindowMac* WXUNUSED(parent),
203                                     wxWindowID id,
204                                     const wxString& label,
205                                     const wxPoint& pos,
206                                     const wxSize& size,
207                                     long WXUNUSED(style),
208                                     long WXUNUSED(extraStyle))
209 {
210     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
211     wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
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 ( id == wxID_HELP && label.empty() )
218     {
219         [v setBezelStyle:NSHelpButtonBezelStyle];
220     }
221     else
222     {
223         [v setBezelStyle:NSRoundedBezelStyle];
224     }
225
226     [v setButtonType:NSMomentaryPushInButton];
227     return new wxButtonCocoaImpl( wxpeer, v );
228 }
229
230 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
231 {
232     if ( [m_osxView isKindOfClass:[NSButton class]] )
233     {
234         if ( isDefault )
235             [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
236         else
237             [(NSButton*)m_osxView setKeyEquivalent: @"" ];
238     }
239 }
240
241 void wxWidgetCocoaImpl::PerformClick()
242 {
243     if ([m_osxView isKindOfClass:[NSControl class]])
244         [(NSControl*)m_osxView performClick:nil];
245 }
246
247 #if wxUSE_BMPBUTTON
248
249 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
250                                                    wxWindowMac* WXUNUSED(parent),
251                                                    wxWindowID WXUNUSED(id),
252                                                    const wxBitmap& bitmap,
253                                                    const wxPoint& pos,
254                                                    const wxSize& size,
255                                                    long style,
256                                                    long WXUNUSED(extraStyle))
257 {
258     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
259     wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
260
261     SetBezelStyleFromBorderFlags(v, style);
262
263     if (bitmap.Ok())
264         [v setImage:bitmap.GetNSImage() ];
265
266     [v setButtonType:NSMomentaryPushInButton];
267     wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
268     return c;
269 }
270
271 #endif // wxUSE_BMPBUTTON
272
273 //
274 // wxDisclosureButton implementation
275 //
276
277 @interface wxDisclosureNSButton : NSButton
278 {
279
280     BOOL isOpen;
281 }
282
283 - (void) updateImage;
284
285 - (void) toggle;
286
287 + (NSImage *)rotateImage: (NSImage *)image;
288
289 @end
290
291 static const char * disc_triangle_xpm[] = {
292 "10 9 4 1",
293 "   c None",
294 ".  c #737373",
295 "+  c #989898",
296 "-  c #c6c6c6",
297 " .-       ",
298 " ..+-     ",
299 " ....+    ",
300 " ......-  ",
301 " .......- ",
302 " ......-  ",
303 " ....+    ",
304 " ..+-     ",
305 " .-       ",
306 };
307
308 @implementation wxDisclosureNSButton
309
310 + (void)initialize
311 {
312     static BOOL initialized = NO;
313     if (!initialized)
314     {
315         initialized = YES;
316         wxOSXCocoaClassAddWXMethods( self );
317     }
318 }
319
320 - (id) initWithFrame:(NSRect) frame
321 {
322     self = [super initWithFrame:frame];
323     isOpen = NO;
324     [self setImagePosition:NSImageLeft];
325     [self updateImage];
326     return self;
327 }
328
329 - (int) intValue
330 {
331     return isOpen ? 1 : 0;
332 }
333
334 - (void) setIntValue: (int) v
335 {
336     isOpen = ( v != 0 );
337     [self updateImage];
338 }
339
340 - (void) toggle
341 {
342     isOpen = !isOpen;
343     [self updateImage];
344 }
345
346 wxCFRef<NSImage*> downArray ;
347
348 - (void) updateImage
349 {
350     static wxBitmap trianglebm(disc_triangle_xpm);
351     if ( downArray.get() == NULL )
352     {
353         downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
354     }
355
356     if ( isOpen )
357         [self setImage:(NSImage*)downArray.get()];
358     else
359         [self setImage:trianglebm.GetNSImage()];
360 }
361
362 + (NSImage *)rotateImage: (NSImage *)image
363 {
364     NSSize imageSize = [image size];
365     NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
366     NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
367
368     [newImage lockFocus];
369
370     NSAffineTransform* tm = [NSAffineTransform transform];
371     [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
372     [tm rotateByDegrees:-90];
373     [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
374     [tm concat];
375
376
377     [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
378         fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
379
380     [newImage unlockFocus];
381     return newImage;
382 }
383
384 @end
385
386 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
387 {
388 public :
389     wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
390         wxWidgetCocoaImpl(peer, w)
391     {
392     }
393
394     ~wxDisclosureTriangleCocoaImpl()
395     {
396     }
397
398     virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
399     {
400         wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
401         [db toggle];
402         wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
403     }
404 };
405
406 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
407                                     wxWindowMac* WXUNUSED(parent),
408                                     wxWindowID WXUNUSED(winid),
409                                     const wxString& label,
410                                     const wxPoint& pos,
411                                     const wxSize& size,
412                                     long style,
413                                     long WXUNUSED(extraStyle))
414 {
415     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
416     wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
417     if ( !label.empty() )
418         [v setTitle:wxCFStringRef(label).AsNSString()];
419
420     SetBezelStyleFromBorderFlags(v, style);
421
422     return new wxDisclosureTriangleCocoaImpl( wxpeer, v );
423 }