]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/button.mm
implementing rollover and pressed image for bitmapbutton on osx_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 #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
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 [GetNSButton() setBezelStyle:bitmap.IsOk() ? NSRegularSquareBezelStyle
111 : NSRoundedBezelStyle];
112
113 wxWidgetCocoaImpl::SetBitmap(bitmap);
114 }
115
116 private:
117 NSButton *GetNSButton() const
118 {
119 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
120
121 return static_cast<NSButton *>(m_osxView);
122 }
123 };
124
125 } // anonymous namespace
126
127 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
128 wxWindowMac* WXUNUSED(parent),
129 wxWindowID id,
130 const wxString& WXUNUSED(label),
131 const wxPoint& pos,
132 const wxSize& size,
133 long WXUNUSED(style),
134 long WXUNUSED(extraStyle))
135 {
136 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
137 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
138
139 if ( id == wxID_HELP )
140 {
141 [v setBezelStyle:NSHelpButtonBezelStyle];
142 }
143 else
144 {
145 [v setBezelStyle:NSRoundedBezelStyle];
146 }
147
148 [v setButtonType:NSMomentaryPushInButton];
149 return new wxButtonCocoaImpl( wxpeer, v );
150 }
151
152 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
153 {
154 if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
155 // NOTE: setKeyEquivalent: nil will trigger an assert
156 // instead do not call in that case.
157 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
158 }
159
160 void wxWidgetCocoaImpl::PerformClick()
161 {
162 if ([m_osxView isKindOfClass:[NSControl class]])
163 [(NSControl*)m_osxView performClick:nil];
164 }
165
166 //
167 // wxDisclosureButton implementation
168 //
169
170 @interface wxDisclosureNSButton : NSButton
171 {
172
173 BOOL isOpen;
174 }
175
176 - (void) updateImage;
177
178 - (void) toggle;
179
180 + (NSImage *)rotateImage: (NSImage *)image;
181
182 @end
183
184 static const char * disc_triangle_xpm[] = {
185 "10 9 4 1",
186 " c None",
187 ". c #737373",
188 "+ c #989898",
189 "- c #c6c6c6",
190 " .- ",
191 " ..+- ",
192 " ....+ ",
193 " ......- ",
194 " .......- ",
195 " ......- ",
196 " ....+ ",
197 " ..+- ",
198 " .- ",
199 };
200
201 @implementation wxDisclosureNSButton
202
203 + (void)initialize
204 {
205 static BOOL initialized = NO;
206 if (!initialized)
207 {
208 initialized = YES;
209 wxOSXCocoaClassAddWXMethods( self );
210 }
211 }
212
213 - (id) initWithFrame:(NSRect) frame
214 {
215 self = [super initWithFrame:frame];
216 [self setBezelStyle:NSSmallSquareBezelStyle];
217 isOpen = NO;
218 [self setImagePosition:NSImageLeft];
219 [self updateImage];
220 return self;
221 }
222
223 - (int) intValue
224 {
225 return isOpen ? 1 : 0;
226 }
227
228 - (void) setIntValue: (int) v
229 {
230 isOpen = ( v != 0 );
231 [self updateImage];
232 }
233
234 - (void) toggle
235 {
236 isOpen = !isOpen;
237 [self updateImage];
238 }
239
240 wxCFRef<NSImage*> downArray ;
241
242 - (void) updateImage
243 {
244 static wxBitmap trianglebm(disc_triangle_xpm);
245 if ( downArray.get() == NULL )
246 {
247 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
248 }
249
250 if ( isOpen )
251 [self setImage:(NSImage*)downArray.get()];
252 else
253 [self setImage:trianglebm.GetNSImage()];
254 }
255
256 + (NSImage *)rotateImage: (NSImage *)image
257 {
258 NSSize imageSize = [image size];
259 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
260 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
261
262 [newImage lockFocus];
263
264 NSAffineTransform* tm = [NSAffineTransform transform];
265 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
266 [tm rotateByDegrees:-90];
267 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
268 [tm concat];
269
270
271 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
272 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
273
274 [newImage unlockFocus];
275 return newImage;
276 }
277
278 @end
279
280 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
281 {
282 public :
283 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
284 wxWidgetCocoaImpl(peer, w)
285 {
286 }
287
288 ~wxDisclosureTriangleCocoaImpl()
289 {
290 }
291
292 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
293 {
294 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
295 [db toggle];
296 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
297 }
298 };
299
300 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
301 wxWindowMac* WXUNUSED(parent),
302 wxWindowID WXUNUSED(id),
303 const wxString& label,
304 const wxPoint& pos,
305 const wxSize& size,
306 long WXUNUSED(style),
307 long WXUNUSED(extraStyle))
308 {
309 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
310 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
311 [v setTitle:wxCFStringRef( label).AsNSString()];
312 wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v );
313 return c;
314 }