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