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