]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/osx/cocoa/button.mm
Big wxDataViewCtrl renderer classes refactoring.
[wxWidgets.git] / src / osx / cocoa / button.mm
... / ...
CommitLineData
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
21wxSize 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
39wxSize 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
97namespace
98{
99
100class wxButtonCocoaImpl : public wxWidgetCocoaImpl, public wxButtonImpl
101{
102public:
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
124private:
125 NSButton *GetNSButton() const
126 {
127 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
128
129 return static_cast<NSButton *>(m_osxView);
130 }
131};
132
133// set bezel style depending on the wxBORDER_XXX flags specified by the style
134void SetBezelStyleFromBorderFlags(NSButton *v, long style)
135{
136 if ( style & wxBORDER_NONE )
137 {
138 [v setBezelStyle:NSShadowlessSquareBezelStyle];
139 [v setBordered:NO];
140 }
141 else // we do have a border
142 {
143 // see trac #11128 for a thorough discussion
144 if ( (style & wxBORDER_MASK) == wxBORDER_RAISED )
145 [v setBezelStyle:NSRegularSquareBezelStyle];
146 else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN )
147 [v setBezelStyle:NSSmallSquareBezelStyle];
148 else
149 [v setBezelStyle:NSShadowlessSquareBezelStyle];
150 }
151}
152
153} // anonymous namespace
154
155wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
156 wxWindowMac* WXUNUSED(parent),
157 wxWindowID id,
158 const wxString& WXUNUSED(label),
159 const wxPoint& pos,
160 const wxSize& size,
161 long WXUNUSED(style),
162 long WXUNUSED(extraStyle))
163{
164 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
165 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
166
167 if ( id == wxID_HELP )
168 {
169 [v setBezelStyle:NSHelpButtonBezelStyle];
170 }
171 else
172 {
173 [v setBezelStyle:NSRoundedBezelStyle];
174 }
175
176 [v setButtonType:NSMomentaryPushInButton];
177 return new wxButtonCocoaImpl( wxpeer, v );
178}
179
180void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
181{
182 if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
183 // NOTE: setKeyEquivalent: nil will trigger an assert
184 // instead do not call in that case.
185 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
186}
187
188void wxWidgetCocoaImpl::PerformClick()
189{
190 if ([m_osxView isKindOfClass:[NSControl class]])
191 [(NSControl*)m_osxView performClick:nil];
192}
193
194#if wxUSE_BMPBUTTON
195
196wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
197 wxWindowMac* WXUNUSED(parent),
198 wxWindowID WXUNUSED(id),
199 const wxBitmap& bitmap,
200 const wxPoint& pos,
201 const wxSize& size,
202 long style,
203 long WXUNUSED(extraStyle))
204{
205 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
206 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
207
208 SetBezelStyleFromBorderFlags(v, style);
209
210 if (bitmap.Ok())
211 [v setImage:bitmap.GetNSImage() ];
212
213 [v setButtonType:NSMomentaryPushInButton];
214 wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v );
215 return c;
216}
217
218#endif // wxUSE_BMPBUTTON
219
220//
221// wxDisclosureButton implementation
222//
223
224@interface wxDisclosureNSButton : NSButton
225{
226
227 BOOL isOpen;
228}
229
230- (void) updateImage;
231
232- (void) toggle;
233
234+ (NSImage *)rotateImage: (NSImage *)image;
235
236@end
237
238static const char * disc_triangle_xpm[] = {
239"10 9 4 1",
240" c None",
241". c #737373",
242"+ c #989898",
243"- c #c6c6c6",
244" .- ",
245" ..+- ",
246" ....+ ",
247" ......- ",
248" .......- ",
249" ......- ",
250" ....+ ",
251" ..+- ",
252" .- ",
253};
254
255@implementation wxDisclosureNSButton
256
257+ (void)initialize
258{
259 static BOOL initialized = NO;
260 if (!initialized)
261 {
262 initialized = YES;
263 wxOSXCocoaClassAddWXMethods( self );
264 }
265}
266
267- (id) initWithFrame:(NSRect) frame
268{
269 self = [super initWithFrame:frame];
270 isOpen = NO;
271 [self setImagePosition:NSImageLeft];
272 [self updateImage];
273 return self;
274}
275
276- (int) intValue
277{
278 return isOpen ? 1 : 0;
279}
280
281- (void) setIntValue: (int) v
282{
283 isOpen = ( v != 0 );
284 [self updateImage];
285}
286
287- (void) toggle
288{
289 isOpen = !isOpen;
290 [self updateImage];
291}
292
293wxCFRef<NSImage*> downArray ;
294
295- (void) updateImage
296{
297 static wxBitmap trianglebm(disc_triangle_xpm);
298 if ( downArray.get() == NULL )
299 {
300 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
301 }
302
303 if ( isOpen )
304 [self setImage:(NSImage*)downArray.get()];
305 else
306 [self setImage:trianglebm.GetNSImage()];
307}
308
309+ (NSImage *)rotateImage: (NSImage *)image
310{
311 NSSize imageSize = [image size];
312 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
313 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
314
315 [newImage lockFocus];
316
317 NSAffineTransform* tm = [NSAffineTransform transform];
318 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
319 [tm rotateByDegrees:-90];
320 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
321 [tm concat];
322
323
324 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
325 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
326
327 [newImage unlockFocus];
328 return newImage;
329}
330
331@end
332
333class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
334{
335public :
336 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
337 wxWidgetCocoaImpl(peer, w)
338 {
339 }
340
341 ~wxDisclosureTriangleCocoaImpl()
342 {
343 }
344
345 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
346 {
347 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
348 [db toggle];
349 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
350 }
351};
352
353wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
354 wxWindowMac* WXUNUSED(parent),
355 wxWindowID WXUNUSED(winid),
356 const wxString& label,
357 const wxPoint& pos,
358 const wxSize& size,
359 long style,
360 long WXUNUSED(extraStyle))
361{
362 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
363 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
364 if ( !label.empty() )
365 [v setTitle:wxCFStringRef(label).AsNSString()];
366
367 SetBezelStyleFromBorderFlags(v, style);
368
369 return new wxDisclosureTriangleCocoaImpl( wxpeer, v );
370}