]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/button.mm
implement support for per-state bitmaps in wxMSW wxButton
[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
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
26wxSize wxButton::DoGetBestSize() const
27{
28 if ( GetId() == wxID_HELP )
423939b2 29 return wxSize( 23 , 23 ) ;
f033830e
SC
30
31 wxSize sz = GetDefaultSize() ;
32
33 switch (GetWindowVariant())
34 {
35 case wxWINDOW_VARIANT_NORMAL:
36 case wxWINDOW_VARIANT_LARGE:
dbeddfb9 37 sz.y = 23 ;
f033830e
SC
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
dbeddfb9
SC
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
f033830e
SC
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
116wxSize wxButton::GetDefaultSize()
117{
118 int wBtn = 70 ;
119 int hBtn = 20 ;
120
121 return wxSize(wBtn, hBtn);
122}
123
124@implementation wxNSButton
125
4dd9fdf8 126+ (void)initialize
f033830e 127{
4dd9fdf8
SC
128 static BOOL initialized = NO;
129 if (!initialized)
f033830e 130 {
4dd9fdf8
SC
131 initialized = YES;
132 wxOSXCocoaClassAddWXMethods( self );
f033830e
SC
133 }
134}
135
f033830e
SC
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
168wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
d8207702 169 wxWindowMac* WXUNUSED(parent),
f033830e 170 wxWindowID id,
d8207702 171 const wxString& WXUNUSED(label),
f033830e
SC
172 const wxPoint& pos,
173 const wxSize& size,
d8207702
SC
174 long WXUNUSED(style),
175 long WXUNUSED(extraStyle))
f033830e 176{
dbeddfb9 177 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
f033830e
SC
178 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
179
180 if ( id == wxID_HELP )
181 {
182 [v setBezelStyle:NSHelpButtonBezelStyle];
183 }
184 else
185 {
186 [v setBezelStyle:NSRoundedBezelStyle];
187 }
188
189 [v setButtonType:NSMomentaryPushInButton];
f033830e 190 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
f033830e
SC
191 return c;
192/*
193 OSStatus err;
194 Rect bounds = wxMacGetBoundsForControl( wxpeer , pos , size ) ;
195 wxMacControl* peer = new wxMacControl(wxpeer) ;
196 if ( id == wxID_HELP )
197 {
198 ControlButtonContentInfo info ;
199 info.contentType = kControlContentIconRef ;
200 GetIconRef(kOnSystemDisk, kSystemIconsCreator, kHelpIcon, &info.u.iconRef);
201 err = CreateRoundButtonControl(
202 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
203 &bounds, kControlRoundButtonNormalSize,
204 &info, peer->GetControlRefAddr() );
205 }
206 else if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
207 {
208 // Button height is static in Mac, can't be changed, so we need to force it here
209 int maxHeight;
210 switch (wxpeer->GetWindowVariant() )
211 {
212 case wxWINDOW_VARIANT_NORMAL:
213 case wxWINDOW_VARIANT_LARGE:
214 maxHeight = 20 ;
215 break;
216 case wxWINDOW_VARIANT_SMALL:
217 maxHeight = 17;
218 case wxWINDOW_VARIANT_MINI:
219 maxHeight = 15;
220 default:
221 break;
222 }
223 bounds.bottom = bounds.top + maxHeight ;
224 wxpeer->SetMaxSize( wxSize( wxpeer->GetMaxWidth() , maxHeight ));
225 err = CreatePushButtonControl(
226 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
227 &bounds, CFSTR(""), peer->GetControlRefAddr() );
228 }
229 else
230 {
231 ControlButtonContentInfo info ;
232 info.contentType = kControlNoContent ;
233 err = CreateBevelButtonControl(
234 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds, CFSTR(""),
235 kControlBevelButtonLargeBevel, kControlBehaviorPushbutton,
236 &info, 0, 0, 0, peer->GetControlRefAddr() );
237 }
238 verify_noerr( err );
239 return peer;
240 */
241}
242
243void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
244{
5ec8cc4d
KO
245 if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
246 // NOTE: setKeyEquivalent: nil will trigger an assert
247 // instead do not call in that case.
248 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
f033830e
SC
249}
250
251void wxWidgetCocoaImpl::PerformClick()
252{
253}
254
17ad51ed
SC
255//
256// wxDisclosureButton implementation
257//
258
259@interface wxDisclosureNSButton : NSButton
260{
261
262 BOOL isOpen;
263}
264
265- (void) updateImage;
266
267- (void) toggle;
268
269+ (NSImage *)rotateImage: (NSImage *)image;
270
271@end
272
273@implementation wxDisclosureNSButton
274
275+ (void)initialize
276{
277 static BOOL initialized = NO;
278 if (!initialized)
279 {
280 initialized = YES;
281 wxOSXCocoaClassAddWXMethods( self );
282 }
283}
284
285- (id) initWithFrame:(NSRect) frame
286{
287 self = [super initWithFrame:frame];
288 [self setBezelStyle:NSSmallSquareBezelStyle];
289 isOpen = NO;
290 [self setImagePosition:NSImageLeft];
291 [self updateImage];
292 return self;
293}
294
295- (int) intValue
296{
297 return isOpen ? 1 : 0;
298}
299
300- (void) setIntValue: (int) v
301{
302 isOpen = ( v != 0 );
303 [self updateImage];
304}
305
306- (void) toggle
307{
308 isOpen = !isOpen;
309 [self updateImage];
310}
311
312wxCFRef<NSImage*> downArray ;
313
314- (void) updateImage
315{
316 if ( downArray.get() == NULL )
317 {
318 downArray.reset( [wxDisclosureNSButton rotateImage:[NSImage imageNamed:NSImageNameRightFacingTriangleTemplate]] );
319 }
320
321 if ( isOpen )
322 [self setImage:(NSImage*)downArray.get()];
323 else
324 [self setImage:[NSImage imageNamed:NSImageNameRightFacingTriangleTemplate]];
325}
326
327+ (NSImage *)rotateImage: (NSImage *)image
328{
329 NSSize imageSize = [image size];
330 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
331 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
332
333 [newImage lockFocus];
334
335 NSAffineTransform* tm = [NSAffineTransform transform];
336 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
337 [tm rotateByDegrees:-90];
338 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
339 [tm concat];
340
341
342 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
343 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
344
345 [newImage unlockFocus];
346 return newImage;
347}
348
349@end
350
351class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
352{
353public :
354 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
355 wxWidgetCocoaImpl(peer, w)
356 {
357 }
358
359 ~wxDisclosureTriangleCocoaImpl()
360 {
361 }
362
363 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
364 {
365 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
366 [db toggle];
367 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
368 }
369};
370
dbeddfb9 371wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
d8207702
SC
372 wxWindowMac* WXUNUSED(parent),
373 wxWindowID WXUNUSED(id),
dbeddfb9
SC
374 const wxString& label,
375 const wxPoint& pos,
376 const wxSize& size,
d8207702
SC
377 long WXUNUSED(style),
378 long WXUNUSED(extraStyle))
dbeddfb9 379{
dbeddfb9 380 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
17ad51ed 381 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
dbeddfb9 382 [v setTitle:wxCFStringRef( label).AsNSString()];
17ad51ed 383 wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v );
dbeddfb9
SC
384 return c;
385}