moving bitmapbutton functionality up to button for OSX
[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     if ( GetId() == wxID_HELP )
24         return wxSize( 23 , 23 ) ;
25
26     wxRect r ;
27     m_peer->GetBestRect(&r);
28
29     wxSize sz = r.GetSize();
30
31     const int wBtnStd = GetDefaultSize().x;
32
33     if ( (sz.x < wBtnStd) && !HasFlag(wxBU_EXACTFIT) )
34         sz.x = wBtnStd;
35
36     return sz ;
37 }
38
39 wxSize wxButton::GetDefaultSize()
40 {
41     return wxSize(84, 23);
42 }
43
44 @implementation wxNSButton
45
46 + (void)initialize
47 {
48     static BOOL initialized = NO;
49     if (!initialized)
50     {
51         initialized = YES;
52         wxOSXCocoaClassAddWXMethods( self );
53     }
54 }
55
56 - (int) intValue
57 {
58     switch ( [self state] )
59     {
60         case NSOnState:
61             return 1;
62         case NSMixedState:
63             return 2;
64         default:
65             return 0;
66     }
67 }
68
69 - (void) setIntValue: (int) v
70 {
71     switch( v )
72     {
73         case 2:
74             [self setState:NSMixedState];
75             break;
76         case 1:
77             [self setState:NSOnState];
78             break;
79         default :
80             [self setState:NSOffState];
81             break;
82     }
83 }
84
85 - (void) setTrackingTag: (NSTrackingRectTag)tag
86 {
87     rectTag = tag;
88 }
89
90 - (NSTrackingRectTag) trackingTag
91 {
92     return rectTag;
93 }
94
95 @end
96
97 namespace
98 {
99
100 class wxButtonCocoaImpl : public wxWidgetCocoaImpl, public wxButtonImpl
101 {
102 public:
103     wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
104         : wxWidgetCocoaImpl(wxpeer, v)
105     {
106     }
107
108     virtual void SetBitmap(const wxBitmap& bitmap)
109     {
110         // switch bezel style for plain pushbuttons
111         if ( bitmap.IsOk() && [GetNSButton() bezelStyle] == NSRoundedBezelStyle )
112             [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle ];
113
114         wxWidgetCocoaImpl::SetBitmap(bitmap);
115     }
116
117     void SetPressedBitmap( const wxBitmap& bitmap )
118     {
119         NSButton* button = GetNSButton();
120         [button setAlternateImage: bitmap.GetNSImage()];
121         [button setButtonType:NSMomentaryChangeButton];
122     }
123
124 private:
125     NSButton *GetNSButton() const
126     {
127         wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
128
129         return static_cast<NSButton *>(m_osxView);
130     }
131 };
132
133 } // anonymous namespace
134
135 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
136                                     wxWindowMac* WXUNUSED(parent),
137                                     wxWindowID id,
138                                     const wxString& WXUNUSED(label),
139                                     const wxPoint& pos,
140                                     const wxSize& size,
141                                     long WXUNUSED(style),
142                                     long WXUNUSED(extraStyle))
143 {
144     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
145     wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
146
147     if ( id == wxID_HELP )
148     {
149         [v setBezelStyle:NSHelpButtonBezelStyle];
150     }
151     else
152     {
153         [v setBezelStyle:NSRoundedBezelStyle];
154     }
155
156     [v setButtonType:NSMomentaryPushInButton];
157     return new wxButtonCocoaImpl( wxpeer, v );
158 }
159
160 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
161 {
162     if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
163         // NOTE: setKeyEquivalent: nil will trigger an assert
164         // instead do not call in that case.
165         [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
166 }
167
168 void wxWidgetCocoaImpl::PerformClick()
169 {
170     if ([m_osxView isKindOfClass:[NSControl class]]) 
171         [(NSControl*)m_osxView performClick:nil]; 
172 }
173
174 #if wxUSE_BMPBUTTON
175
176 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
177                                                    wxWindowMac* WXUNUSED(parent),
178                                                    wxWindowID WXUNUSED(id),
179                                                    const wxBitmap& bitmap,
180                                                    const wxPoint& pos,
181                                                    const wxSize& size,
182                                                    long style,
183                                                    long WXUNUSED(extraStyle))
184 {
185     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
186     wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
187     
188     // trying to get as close as possible to flags
189     if ( style & wxBORDER_NONE )
190     {
191         [v setBezelStyle:NSShadowlessSquareBezelStyle];
192         [v setBordered:NO]; 
193     }
194     else
195     {
196         // see trac #11128 for a thorough discussion
197         if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
198             [v setBezelStyle:NSRegularSquareBezelStyle];
199         else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
200             [v setBezelStyle:NSSmallSquareBezelStyle];
201         else
202             [v setBezelStyle:NSShadowlessSquareBezelStyle];
203     }
204     
205     if (bitmap.Ok())
206         [v setImage:bitmap.GetNSImage() ];
207     
208     [v setButtonType:NSMomentaryPushInButton];
209     wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
210     return c;
211 }
212
213 #endif
214
215 //
216 // wxDisclosureButton implementation
217 //
218
219 @interface wxDisclosureNSButton : NSButton
220 {
221
222     BOOL isOpen;
223 }
224
225 - (void) updateImage;
226
227 - (void) toggle;
228
229 + (NSImage *)rotateImage: (NSImage *)image;
230
231 @end
232
233 static const char * disc_triangle_xpm[] = {
234 "10 9 4 1",
235 "   c None",
236 ".  c #737373",
237 "+  c #989898",
238 "-  c #c6c6c6",
239 " .-       ",
240 " ..+-     ",
241 " ....+    ",
242 " ......-  ",
243 " .......- ",
244 " ......-  ",
245 " ....+    ",
246 " ..+-     ",
247 " .-       ",
248 };
249
250 @implementation wxDisclosureNSButton
251
252 + (void)initialize
253 {
254     static BOOL initialized = NO;
255     if (!initialized)
256     {
257         initialized = YES;
258         wxOSXCocoaClassAddWXMethods( self );
259     }
260 }
261
262 - (id) initWithFrame:(NSRect) frame
263 {
264     self = [super initWithFrame:frame];
265     [self setBezelStyle:NSSmallSquareBezelStyle];
266     isOpen = NO;
267     [self setImagePosition:NSImageLeft];
268     [self updateImage];
269     return self;
270 }
271
272 - (int) intValue
273 {
274     return isOpen ? 1 : 0;
275 }
276
277 - (void) setIntValue: (int) v
278 {
279     isOpen = ( v != 0 );
280     [self updateImage];
281 }
282
283 - (void) toggle
284 {
285     isOpen = !isOpen;
286     [self updateImage];
287 }
288
289 wxCFRef<NSImage*> downArray ;
290
291 - (void) updateImage
292 {
293     static wxBitmap trianglebm(disc_triangle_xpm);
294     if ( downArray.get() == NULL )
295     {
296         downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
297     }
298
299     if ( isOpen )
300         [self setImage:(NSImage*)downArray.get()];
301     else
302         [self setImage:trianglebm.GetNSImage()];
303 }
304
305 + (NSImage *)rotateImage: (NSImage *)image
306 {
307     NSSize imageSize = [image size];
308     NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
309     NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
310
311     [newImage lockFocus];
312
313     NSAffineTransform* tm = [NSAffineTransform transform];
314     [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
315     [tm rotateByDegrees:-90];
316     [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
317     [tm concat];
318
319
320     [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
321         fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
322
323     [newImage unlockFocus];
324     return newImage;
325 }
326
327 @end
328
329 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
330 {
331 public :
332     wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
333         wxWidgetCocoaImpl(peer, w)
334     {
335     }
336
337     ~wxDisclosureTriangleCocoaImpl()
338     {
339     }
340
341     virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
342     {
343         wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
344         [db toggle];
345         wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
346     }
347 };
348
349 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
350                                     wxWindowMac* WXUNUSED(parent),
351                                     wxWindowID WXUNUSED(id),
352                                     const wxString& label,
353                                     const wxPoint& pos,
354                                     const wxSize& size,
355                                     long WXUNUSED(style),
356                                     long WXUNUSED(extraStyle))
357 {
358     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
359     wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
360     [v setTitle:wxCFStringRef( label).AsNSString()];
361     wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v );
362     return c;
363 }