]>
Commit | Line | Data |
---|---|---|
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 | // 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() ) | |
27 | return wxSize( 23 , 23 ) ; | |
28 | ||
29 | wxRect r ; | |
30 | m_peer->GetBestRect(&r); | |
31 | ||
32 | wxSize sz = r.GetSize(); | |
33 | ||
34 | const int wBtnStd = GetDefaultSize().x; | |
35 | ||
36 | if ( (sz.x < wBtnStd) && !HasFlag(wxBU_EXACTFIT) ) | |
37 | sz.x = wBtnStd; | |
38 | ||
39 | return sz ; | |
40 | } | |
41 | ||
42 | wxSize wxButton::GetDefaultSize() | |
43 | { | |
44 | return wxSize(84, 23); | |
45 | } | |
46 | ||
47 | @implementation wxNSButton | |
48 | ||
49 | + (void)initialize | |
50 | { | |
51 | static BOOL initialized = NO; | |
52 | if (!initialized) | |
53 | { | |
54 | initialized = YES; | |
55 | wxOSXCocoaClassAddWXMethods( self ); | |
56 | } | |
57 | } | |
58 | ||
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 | ||
88 | - (void) setTrackingTag: (NSTrackingRectTag)tag | |
89 | { | |
90 | rectTag = tag; | |
91 | } | |
92 | ||
93 | - (NSTrackingRectTag) trackingTag | |
94 | { | |
95 | return rectTag; | |
96 | } | |
97 | ||
98 | @end | |
99 | ||
100 | namespace | |
101 | { | |
102 | ||
103 | class wxButtonCocoaImpl : public wxWidgetCocoaImpl, public wxButtonImpl | |
104 | { | |
105 | public: | |
106 | wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v) | |
107 | : wxWidgetCocoaImpl(wxpeer, v) | |
108 | { | |
109 | } | |
110 | ||
111 | virtual void SetBitmap(const wxBitmap& bitmap) | |
112 | { | |
113 | // switch bezel style for plain pushbuttons | |
114 | if ( bitmap.IsOk() && [GetNSButton() bezelStyle] == NSRoundedBezelStyle ) | |
115 | [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle ]; | |
116 | ||
117 | wxWidgetCocoaImpl::SetBitmap(bitmap); | |
118 | } | |
119 | ||
120 | void SetPressedBitmap( const wxBitmap& bitmap ) | |
121 | { | |
122 | NSButton* button = GetNSButton(); | |
123 | [button setAlternateImage: bitmap.GetNSImage()]; | |
124 | [button setButtonType:NSMomentaryChangeButton]; | |
125 | } | |
126 | ||
127 | private: | |
128 | NSButton *GetNSButton() const | |
129 | { | |
130 | wxASSERT( [m_osxView isKindOfClass:[NSButton class]] ); | |
131 | ||
132 | return static_cast<NSButton *>(m_osxView); | |
133 | } | |
134 | }; | |
135 | ||
136 | extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style); | |
137 | ||
138 | // set bezel style depending on the wxBORDER_XXX flags specified by the style | |
139 | void 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 | ||
158 | } // anonymous namespace | |
159 | ||
160 | wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer, | |
161 | wxWindowMac* WXUNUSED(parent), | |
162 | wxWindowID id, | |
163 | const wxString& label, | |
164 | const wxPoint& pos, | |
165 | const wxSize& size, | |
166 | long WXUNUSED(style), | |
167 | long WXUNUSED(extraStyle)) | |
168 | { | |
169 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; | |
170 | wxNSButton* v = [[wxNSButton alloc] initWithFrame:r]; | |
171 | ||
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() ) | |
177 | { | |
178 | [v setBezelStyle:NSHelpButtonBezelStyle]; | |
179 | } | |
180 | else | |
181 | { | |
182 | [v setBezelStyle:NSRoundedBezelStyle]; | |
183 | } | |
184 | ||
185 | [v setButtonType:NSMomentaryPushInButton]; | |
186 | return new wxButtonCocoaImpl( wxpeer, v ); | |
187 | } | |
188 | ||
189 | void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault ) | |
190 | { | |
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 | } | |
198 | } | |
199 | ||
200 | void wxWidgetCocoaImpl::PerformClick() | |
201 | { | |
202 | if ([m_osxView isKindOfClass:[NSControl class]]) | |
203 | [(NSControl*)m_osxView performClick:nil]; | |
204 | } | |
205 | ||
206 | #if wxUSE_BMPBUTTON | |
207 | ||
208 | wxWidgetImplType* 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]; | |
219 | ||
220 | SetBezelStyleFromBorderFlags(v, style); | |
221 | ||
222 | if (bitmap.Ok()) | |
223 | [v setImage:bitmap.GetNSImage() ]; | |
224 | ||
225 | [v setButtonType:NSMomentaryPushInButton]; | |
226 | wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v ); | |
227 | return c; | |
228 | } | |
229 | ||
230 | #endif // wxUSE_BMPBUTTON | |
231 | ||
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 | ||
250 | static 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 | ||
267 | @implementation wxDisclosureNSButton | |
268 | ||
269 | + (void)initialize | |
270 | { | |
271 | static BOOL initialized = NO; | |
272 | if (!initialized) | |
273 | { | |
274 | initialized = YES; | |
275 | wxOSXCocoaClassAddWXMethods( self ); | |
276 | } | |
277 | } | |
278 | ||
279 | - (id) initWithFrame:(NSRect) frame | |
280 | { | |
281 | self = [super initWithFrame:frame]; | |
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 | ||
305 | wxCFRef<NSImage*> downArray ; | |
306 | ||
307 | - (void) updateImage | |
308 | { | |
309 | static wxBitmap trianglebm(disc_triangle_xpm); | |
310 | if ( downArray.get() == NULL ) | |
311 | { | |
312 | downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] ); | |
313 | } | |
314 | ||
315 | if ( isOpen ) | |
316 | [self setImage:(NSImage*)downArray.get()]; | |
317 | else | |
318 | [self setImage:trianglebm.GetNSImage()]; | |
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]; | |
326 | ||
327 | [newImage lockFocus]; | |
328 | ||
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]; | |
334 | ||
335 | ||
336 | [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height) | |
337 | fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0]; | |
338 | ||
339 | [newImage unlockFocus]; | |
340 | return newImage; | |
341 | } | |
342 | ||
343 | @end | |
344 | ||
345 | class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl | |
346 | { | |
347 | public : | |
348 | wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) : | |
349 | wxWidgetCocoaImpl(peer, w) | |
350 | { | |
351 | } | |
352 | ||
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 | ||
365 | wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer, | |
366 | wxWindowMac* WXUNUSED(parent), | |
367 | wxWindowID WXUNUSED(winid), | |
368 | const wxString& label, | |
369 | const wxPoint& pos, | |
370 | const wxSize& size, | |
371 | long style, | |
372 | long WXUNUSED(extraStyle)) | |
373 | { | |
374 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; | |
375 | wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r]; | |
376 | if ( !label.empty() ) | |
377 | [v setTitle:wxCFStringRef(label).AsNSString()]; | |
378 | ||
379 | SetBezelStyleFromBorderFlags(v, style); | |
380 | ||
381 | return new wxDisclosureTriangleCocoaImpl( wxpeer, v ); | |
382 | } |