added support for bitmaps in wxButton to wxOSX/Cocoa
[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     #include "wx/panel.h"
18     #include "wx/toplevel.h"
19     #include "wx/dcclient.h"
20 #endif
21
22 #include "wx/stockitem.h"
23
24 #include "wx/osx/private.h"
25
26 wxSize wxButton::DoGetBestSize() const
27 {
28     if ( GetId() == wxID_HELP )
29         return wxSize( 23 , 23 ) ;
30
31     wxSize sz = GetDefaultSize() ;
32
33     switch (GetWindowVariant())
34     {
35         case wxWINDOW_VARIANT_NORMAL:
36         case wxWINDOW_VARIANT_LARGE:
37             sz.y = 23 ;
38             break;
39
40         case wxWINDOW_VARIANT_SMALL:
41             sz.y = 17 ;
42             break;
43
44         case wxWINDOW_VARIANT_MINI:
45             sz.y = 15 ;
46             break;
47
48         default:
49             break;
50     }
51
52     wxRect r ;
53
54     m_peer->GetBestRect(&r);
55
56     if ( r.GetWidth() == 0 && r.GetHeight() == 0 )
57     {
58     }
59     sz.x = r.GetWidth();
60     sz.y = r.GetHeight();
61
62     int wBtn = 96;
63
64     if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
65         sz.x = wBtn;
66
67 #if wxOSX_USE_CARBON
68     Rect    bestsize = { 0 , 0 , 0 , 0 } ;
69     m_peer->GetBestRect( &bestsize ) ;
70
71     int wBtn;
72     if ( EmptyRect( &bestsize ) || ( GetWindowStyle() & wxBU_EXACTFIT) )
73     {
74         Point bounds;
75
76         ControlFontStyleRec controlFont;
77         OSStatus err = m_peer->GetData<ControlFontStyleRec>( kControlEntireControl, kControlFontStyleTag, &controlFont );
78         verify_noerr( err );
79
80         wxCFStringRef str( m_label,  GetFont().GetEncoding() );
81
82 #if wxOSX_USE_ATSU_TEXT
83         SInt16 baseline;
84         if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
85         {
86             err = GetThemeTextDimensions(
87                 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
88                 m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
89             verify_noerr( err );
90         }
91         else
92 #endif
93         {
94             wxClientDC dc(const_cast<wxButton*>(this));
95             wxCoord width, height ;
96             dc.GetTextExtent( m_label , &width, &height);
97             bounds.h = width;
98             bounds.v = height;
99         }
100
101         wBtn = bounds.h + sz.y;
102     }
103     else
104     {
105         wBtn = bestsize.right - bestsize.left ;
106         // non 'normal' window variants don't return the correct height
107         // sz.y = bestsize.bottom - bestsize.top ;
108     }
109     if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
110         sz.x = wBtn;
111 #endif
112
113     return sz ;
114 }
115
116 wxSize wxButton::GetDefaultSize()
117 {
118     int wBtn = 70 ;
119     int hBtn = 20 ;
120
121     return wxSize(wBtn, hBtn);
122 }
123
124 @implementation wxNSButton
125
126 + (void)initialize
127 {
128     static BOOL initialized = NO;
129     if (!initialized)
130     {
131         initialized = YES;
132         wxOSXCocoaClassAddWXMethods( self );
133     }
134 }
135
136 - (int) intValue
137 {
138     switch ( [self state] )
139     {
140         case NSOnState:
141             return 1;
142         case NSMixedState:
143             return 2;
144         default:
145             return 0;
146     }
147 }
148
149 - (void) setIntValue: (int) v
150 {
151     switch( v )
152     {
153         case 2:
154             [self setState:NSMixedState];
155             break;
156         case 1:
157             [self setState:NSOnState];
158             break;
159         default :
160             [self setState:NSOffState];
161             break;
162     }
163 }
164
165 @end
166
167 namespace
168 {
169
170 class wxButtonCocoaImpl : public wxWidgetCocoaImpl
171 {
172 public:
173     wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
174         : wxWidgetCocoaImpl(wxpeer, v)
175     {
176     }
177
178     virtual void SetBitmap(const wxBitmap& bitmap)
179     {
180         [GetNSButton() setBezelStyle:bitmap.IsOk() ? NSRegularSquareBezelStyle
181                                                    : NSRoundedBezelStyle];
182
183         wxWidgetCocoaImpl::SetBitmap(bitmap);
184     }
185
186 private:
187     NSButton *GetNSButton() const
188     {
189         wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
190
191         return static_cast<NSButton *>(m_osxView);
192     }
193 };
194
195 } // anonymous namespace
196
197 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
198                                     wxWindowMac* WXUNUSED(parent),
199                                     wxWindowID id,
200                                     const wxString& WXUNUSED(label),
201                                     const wxPoint& pos,
202                                     const wxSize& size,
203                                     long WXUNUSED(style),
204                                     long WXUNUSED(extraStyle))
205 {
206     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
207     wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
208
209     if ( id == wxID_HELP )
210     {
211         [v setBezelStyle:NSHelpButtonBezelStyle];
212     }
213     else
214     {
215         [v setBezelStyle:NSRoundedBezelStyle];
216     }
217
218     [v setButtonType:NSMomentaryPushInButton];
219     return new wxButtonCocoaImpl( wxpeer, v );
220 }
221
222 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
223 {
224     if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
225         // NOTE: setKeyEquivalent: nil will trigger an assert
226         // instead do not call in that case.
227         [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
228 }
229
230 void wxWidgetCocoaImpl::PerformClick()
231 {
232 }
233
234 //
235 // wxDisclosureButton implementation
236 //
237
238 @interface wxDisclosureNSButton : NSButton
239 {
240
241     BOOL isOpen;
242 }
243
244 - (void) updateImage;
245
246 - (void) toggle;
247
248 + (NSImage *)rotateImage: (NSImage *)image;
249
250 @end
251
252 static const char * disc_triangle_xpm[] = {
253 "10 9 4 1",
254 "   c None",
255 ".  c #737373",
256 "+  c #989898",
257 "-  c #c6c6c6",
258 " .-       ",
259 " ..+-     ",
260 " ....+    ",
261 " ......-  ",
262 " .......- ",
263 " ......-  ",
264 " ....+    ",
265 " ..+-     ",
266 " .-       ",
267 };
268
269 @implementation wxDisclosureNSButton
270
271 + (void)initialize
272 {
273     static BOOL initialized = NO;
274     if (!initialized)
275     {
276         initialized = YES;
277         wxOSXCocoaClassAddWXMethods( self );
278     }
279 }
280
281 - (id) initWithFrame:(NSRect) frame
282 {
283     self = [super initWithFrame:frame];
284     [self setBezelStyle:NSSmallSquareBezelStyle];
285     isOpen = NO;
286     [self setImagePosition:NSImageLeft];
287     [self updateImage];
288     return self;
289 }
290
291 - (int) intValue
292 {
293     return isOpen ? 1 : 0;
294 }
295
296 - (void) setIntValue: (int) v
297 {
298     isOpen = ( v != 0 );
299     [self updateImage];
300 }
301
302 - (void) toggle
303 {
304     isOpen = !isOpen;
305     [self updateImage];
306 }
307
308 wxCFRef<NSImage*> downArray ;
309
310 - (void) updateImage
311 {
312     static wxBitmap trianglebm(disc_triangle_xpm);
313     if ( downArray.get() == NULL )
314     {
315         downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
316     }
317
318     if ( isOpen )
319         [self setImage:(NSImage*)downArray.get()];
320     else
321         [self setImage:trianglebm.GetNSImage()];
322 }
323
324 + (NSImage *)rotateImage: (NSImage *)image
325 {
326     NSSize imageSize = [image size];
327     NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
328     NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
329
330     [newImage lockFocus];
331
332     NSAffineTransform* tm = [NSAffineTransform transform];
333     [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
334     [tm rotateByDegrees:-90];
335     [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
336     [tm concat];
337
338
339     [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
340         fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
341
342     [newImage unlockFocus];
343     return newImage;
344 }
345
346 @end
347
348 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
349 {
350 public :
351     wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
352         wxWidgetCocoaImpl(peer, w)
353     {
354     }
355
356     ~wxDisclosureTriangleCocoaImpl()
357     {
358     }
359
360     virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
361     {
362         wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
363         [db toggle];
364         wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
365     }
366 };
367
368 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
369                                     wxWindowMac* WXUNUSED(parent),
370                                     wxWindowID WXUNUSED(id),
371                                     const wxString& label,
372                                     const wxPoint& pos,
373                                     const wxSize& size,
374                                     long WXUNUSED(style),
375                                     long WXUNUSED(extraStyle))
376 {
377     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
378     wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
379     [v setTitle:wxCFStringRef( label).AsNSString()];
380     wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v );
381     return c;
382 }