]>
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 | |
f033830e SC |
7 | // Copyright: (c) Stefan Csomor |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #include "wx/wxprec.h" | |
12 | ||
f033830e | 13 | #ifndef WX_PRECOMP |
d6eb3ff8 | 14 | #include "wx/object.h" |
f033830e SC |
15 | #endif |
16 | ||
d6eb3ff8 | 17 | #include "wx/button.h" |
0afa3752 | 18 | #include "wx/toplevel.h" |
b108581c | 19 | #include "wx/tglbtn.h" |
d6eb3ff8 | 20 | |
f033830e SC |
21 | #include "wx/osx/private.h" |
22 | ||
f672c969 VZ |
23 | #if wxUSE_MARKUP |
24 | #include "wx/osx/cocoa/private/markuptoattr.h" | |
25 | #endif // wxUSE_MARKUP | |
26 | ||
27 | ||
f033830e SC |
28 | @implementation wxNSButton |
29 | ||
4dd9fdf8 | 30 | + (void)initialize |
f033830e | 31 | { |
4dd9fdf8 | 32 | static BOOL initialized = NO; |
b727fcd3 | 33 | if (!initialized) |
f033830e | 34 | { |
4dd9fdf8 SC |
35 | initialized = YES; |
36 | wxOSXCocoaClassAddWXMethods( self ); | |
f033830e SC |
37 | } |
38 | } | |
39 | ||
f033830e SC |
40 | - (int) intValue |
41 | { | |
42 | switch ( [self state] ) | |
43 | { | |
44 | case NSOnState: | |
45 | return 1; | |
46 | case NSMixedState: | |
47 | return 2; | |
48 | default: | |
49 | return 0; | |
50 | } | |
51 | } | |
52 | ||
53 | - (void) setIntValue: (int) v | |
54 | { | |
55 | switch( v ) | |
56 | { | |
57 | case 2: | |
58 | [self setState:NSMixedState]; | |
59 | break; | |
60 | case 1: | |
61 | [self setState:NSOnState]; | |
62 | break; | |
63 | default : | |
64 | [self setState:NSOffState]; | |
65 | break; | |
66 | } | |
67 | } | |
68 | ||
411a1c35 SC |
69 | - (void) setTrackingTag: (NSTrackingRectTag)tag |
70 | { | |
71 | rectTag = tag; | |
72 | } | |
73 | ||
74 | - (NSTrackingRectTag) trackingTag | |
75 | { | |
76 | return rectTag; | |
77 | } | |
78 | ||
f033830e SC |
79 | @end |
80 | ||
58ce18f2 SC |
81 | @interface NSView(PossibleSizeMethods) |
82 | - (NSControlSize)controlSize; | |
83 | @end | |
84 | ||
b108581c SC |
85 | wxButtonCocoaImpl::wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v) |
86 | : wxWidgetCocoaImpl(wxpeer, v) | |
e5d05b90 | 87 | { |
b108581c SC |
88 | SetNeedsFrame(false); |
89 | } | |
e5d05b90 | 90 | |
b108581c | 91 | void wxButtonCocoaImpl::SetBitmap(const wxBitmap& bitmap) |
e5d05b90 | 92 | { |
b108581c SC |
93 | // switch bezel style for plain pushbuttons |
94 | if ( bitmap.IsOk() ) | |
e5d05b90 | 95 | { |
b108581c SC |
96 | if ([GetNSButton() bezelStyle] == NSRoundedBezelStyle) |
97 | [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle]; | |
e5d05b90 | 98 | } |
b108581c | 99 | else |
e5d05b90 | 100 | { |
b108581c | 101 | [GetNSButton() setBezelStyle:NSRoundedBezelStyle]; |
e5d05b90 | 102 | } |
b108581c SC |
103 | |
104 | wxWidgetCocoaImpl::SetBitmap(bitmap); | |
105 | } | |
e5d05b90 | 106 | |
6a219e34 | 107 | #if wxUSE_MARKUP |
b108581c SC |
108 | void wxButtonCocoaImpl::SetLabelMarkup(const wxString& markup) |
109 | { | |
110 | wxMarkupToAttrString toAttr(GetWXPeer(), markup); | |
111 | NSMutableAttributedString *attrString = toAttr.GetNSAttributedString(); | |
112 | ||
113 | // Button text is always centered. | |
114 | NSMutableParagraphStyle * | |
115 | paragraphStyle = [[NSMutableParagraphStyle alloc] init]; | |
116 | [paragraphStyle setAlignment: NSCenterTextAlignment]; | |
117 | [attrString addAttribute:NSParagraphStyleAttributeName | |
118 | value:paragraphStyle | |
119 | range:NSMakeRange(0, [attrString length])]; | |
120 | [paragraphStyle release]; | |
121 | ||
122 | [GetNSButton() setAttributedTitle:attrString]; | |
123 | } | |
6a219e34 | 124 | #endif // wxUSE_MARKUP |
f672c969 | 125 | |
b108581c SC |
126 | void wxButtonCocoaImpl::SetPressedBitmap( const wxBitmap& bitmap ) |
127 | { | |
128 | NSButton* button = GetNSButton(); | |
129 | [button setAlternateImage: bitmap.GetNSImage()]; | |
130 | if ( GetWXPeer()->IsKindOf(wxCLASSINFO(wxToggleButton)) ) | |
131 | { | |
132 | [button setButtonType:NSToggleButton]; | |
133 | } | |
134 | else | |
b38dc31f | 135 | { |
b38dc31f SC |
136 | [button setButtonType:NSMomentaryChangeButton]; |
137 | } | |
b108581c | 138 | } |
b38dc31f | 139 | |
b108581c SC |
140 | void wxButtonCocoaImpl::GetLayoutInset(int &left , int &top , int &right, int &bottom) const |
141 | { | |
142 | left = top = right = bottom = 0; | |
143 | NSControlSize size = NSRegularControlSize; | |
144 | if ( [m_osxView respondsToSelector:@selector(controlSize)] ) | |
145 | size = [m_osxView controlSize]; | |
146 | else if ([m_osxView respondsToSelector:@selector(cell)]) | |
54ea2834 | 147 | { |
b108581c SC |
148 | id cell = [(id)m_osxView cell]; |
149 | if ([cell respondsToSelector:@selector(controlSize)]) | |
150 | size = [cell controlSize]; | |
151 | } | |
152 | ||
153 | if ( [GetNSButton() bezelStyle] == NSRoundedBezelStyle ) | |
154 | { | |
155 | switch( size ) | |
54ea2834 | 156 | { |
b108581c SC |
157 | case NSRegularControlSize: |
158 | left = right = 6; | |
159 | top = 4; | |
160 | bottom = 8; | |
161 | break; | |
162 | case NSSmallControlSize: | |
163 | left = right = 5; | |
164 | top = 4; | |
165 | bottom = 7; | |
166 | break; | |
167 | case NSMiniControlSize: | |
168 | left = right = 1; | |
169 | top = 0; | |
170 | bottom = 2; | |
171 | break; | |
54ea2834 SC |
172 | } |
173 | } | |
b108581c | 174 | } |
0afa3752 | 175 | |
b108581c SC |
176 | void wxButtonCocoaImpl::SetAcceleratorFromLabel(const wxString& label) |
177 | { | |
178 | const int accelPos = wxControl::FindAccelIndex(label); | |
179 | if ( accelPos != wxNOT_FOUND ) | |
0afa3752 | 180 | { |
b108581c SC |
181 | wxString accelstring(label[accelPos + 1]); // Skip '&' itself |
182 | accelstring.MakeLower(); | |
183 | wxCFStringRef cfText(accelstring); | |
184 | [GetNSButton() setKeyEquivalent:cfText.AsNSString()]; | |
185 | [GetNSButton() setKeyEquivalentModifierMask:NSCommandKeyMask]; | |
0afa3752 | 186 | } |
b108581c | 187 | else |
e5d05b90 | 188 | { |
b108581c | 189 | [GetNSButton() setKeyEquivalent:@""]; |
e5d05b90 | 190 | } |
b108581c | 191 | } |
e5d05b90 | 192 | |
b108581c SC |
193 | NSButton *wxButtonCocoaImpl::GetNSButton() const |
194 | { | |
195 | wxASSERT( [m_osxView isKindOfClass:[NSButton class]] ); | |
196 | ||
197 | return static_cast<NSButton *>(m_osxView); | |
198 | } | |
df04f800 | 199 | |
04a6d8ef VZ |
200 | // Set bezel style depending on the wxBORDER_XXX flags specified by the style |
201 | // and also accounting for the label (bezels are different for multiline | |
202 | // buttons and normal ones) and the ID (special bezel is used for help button). | |
203 | // | |
204 | // This is extern because it's also used in src/osx/cocoa/tglbtn.mm. | |
205 | extern "C" | |
206 | void | |
207 | SetBezelStyleFromBorderFlags(NSButton *v, | |
208 | long style, | |
209 | wxWindowID winid, | |
0c2e6e8b VZ |
210 | const wxString& label = wxString(), |
211 | const wxBitmap& bitmap = wxBitmap()) | |
73b1b996 | 212 | { |
04a6d8ef 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 ( winid == wxID_HELP && label.empty() ) | |
73b1b996 | 218 | { |
04a6d8ef | 219 | [v setBezelStyle:NSHelpButtonBezelStyle]; |
73b1b996 | 220 | } |
04a6d8ef | 221 | else |
73b1b996 | 222 | { |
0c2e6e8b VZ |
223 | // We can't use rounded bezel styles neither for multiline buttons nor |
224 | // for buttons containing (big) icons as they are only meant to be used | |
225 | // at certain sizes, so the style used depends on whether the label is | |
226 | // single or multi line. | |
227 | const bool | |
228 | isSimpleText = (label.find_first_of("\n\r") == wxString::npos) | |
229 | && (!bitmap.IsOk() || bitmap.GetHeight() < 20); | |
04a6d8ef VZ |
230 | |
231 | NSBezelStyle bezel; | |
232 | switch ( style & wxBORDER_MASK ) | |
233 | { | |
234 | case wxBORDER_NONE: | |
235 | bezel = NSShadowlessSquareBezelStyle; | |
236 | [v setBordered:NO]; | |
237 | break; | |
238 | ||
239 | case wxBORDER_SIMPLE: | |
240 | bezel = NSShadowlessSquareBezelStyle; | |
241 | break; | |
242 | ||
243 | case wxBORDER_SUNKEN: | |
0c2e6e8b | 244 | bezel = isSimpleText ? NSTexturedRoundedBezelStyle |
04a6d8ef VZ |
245 | : NSSmallSquareBezelStyle; |
246 | break; | |
247 | ||
248 | default: | |
249 | wxFAIL_MSG( "Unknown border style" ); | |
250 | // fall through | |
251 | ||
252 | case 0: | |
253 | case wxBORDER_STATIC: | |
254 | case wxBORDER_RAISED: | |
255 | case wxBORDER_THEME: | |
0c2e6e8b | 256 | bezel = isSimpleText ? NSRoundedBezelStyle |
04a6d8ef VZ |
257 | : NSRegularSquareBezelStyle; |
258 | break; | |
259 | } | |
260 | ||
261 | [v setBezelStyle:bezel]; | |
73b1b996 VZ |
262 | } |
263 | } | |
264 | ||
cabc71de VZ |
265 | // Set the keyboard accelerator key from the label (e.g. "Click &Me") |
266 | void wxButton::OSXUpdateAfterLabelChange(const wxString& label) | |
267 | { | |
268 | wxButtonCocoaImpl *impl = static_cast<wxButtonCocoaImpl*>(GetPeer()); | |
269 | ||
270 | // Update the bezel style as may be necessary if our new label is multi | |
271 | // line while the old one wasn't (or vice versa). | |
272 | SetBezelStyleFromBorderFlags(impl->GetNSButton(), | |
273 | GetWindowStyle(), | |
274 | GetId(), | |
275 | label); | |
276 | ||
277 | ||
278 | // Skip setting the accelerator for the default buttons as this would | |
279 | // overwrite the default "Enter" which should be preserved. | |
280 | wxTopLevelWindow * const | |
281 | tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); | |
282 | if ( tlw ) | |
283 | { | |
284 | if ( tlw->GetDefaultItem() == this ) | |
285 | return; | |
286 | } | |
287 | ||
288 | impl->SetAcceleratorFromLabel(label); | |
289 | } | |
290 | ||
f033830e | 291 | |
b727fcd3 VZ |
292 | wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer, |
293 | wxWindowMac* WXUNUSED(parent), | |
04a6d8ef | 294 | wxWindowID winid, |
01495abf | 295 | const wxString& label, |
b727fcd3 | 296 | const wxPoint& pos, |
f033830e | 297 | const wxSize& size, |
24e059c3 | 298 | long style, |
b727fcd3 | 299 | long WXUNUSED(extraStyle)) |
f033830e | 300 | { |
dbeddfb9 | 301 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
f033830e | 302 | wxNSButton* v = [[wxNSButton alloc] initWithFrame:r]; |
b727fcd3 | 303 | |
04a6d8ef | 304 | SetBezelStyleFromBorderFlags(v, style, winid, label); |
0afa3752 | 305 | |
f033830e | 306 | [v setButtonType:NSMomentaryPushInButton]; |
0afa3752 VZ |
307 | wxButtonCocoaImpl* const impl = new wxButtonCocoaImpl( wxpeer, v ); |
308 | impl->SetAcceleratorFromLabel(label); | |
309 | return impl; | |
f033830e SC |
310 | } |
311 | ||
312 | void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault ) | |
b727fcd3 | 313 | { |
3f30bd1a SC |
314 | if ( [m_osxView isKindOfClass:[NSButton class]] ) |
315 | { | |
316 | if ( isDefault ) | |
0afa3752 | 317 | { |
3f30bd1a | 318 | [(NSButton*)m_osxView setKeyEquivalent: @"\r" ]; |
0afa3752 VZ |
319 | [(NSButton*)m_osxView setKeyEquivalentModifierMask: 0]; |
320 | } | |
3f30bd1a SC |
321 | else |
322 | [(NSButton*)m_osxView setKeyEquivalent: @"" ]; | |
323 | } | |
f033830e SC |
324 | } |
325 | ||
b727fcd3 | 326 | void wxWidgetCocoaImpl::PerformClick() |
f033830e | 327 | { |
73b1b996 VZ |
328 | if ([m_osxView isKindOfClass:[NSControl class]]) |
329 | [(NSControl*)m_osxView performClick:nil]; | |
f033830e SC |
330 | } |
331 | ||
b38dc31f SC |
332 | #if wxUSE_BMPBUTTON |
333 | ||
334 | wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer, | |
335 | wxWindowMac* WXUNUSED(parent), | |
04a6d8ef | 336 | wxWindowID winid, |
b38dc31f SC |
337 | const wxBitmap& bitmap, |
338 | const wxPoint& pos, | |
339 | const wxSize& size, | |
340 | long style, | |
341 | long WXUNUSED(extraStyle)) | |
342 | { | |
343 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; | |
344 | wxNSButton* v = [[wxNSButton alloc] initWithFrame:r]; | |
73b1b996 | 345 | |
0c2e6e8b | 346 | SetBezelStyleFromBorderFlags(v, style, winid, wxString(), bitmap); |
73b1b996 | 347 | |
a1b806b9 | 348 | if (bitmap.IsOk()) |
b38dc31f | 349 | [v setImage:bitmap.GetNSImage() ]; |
73b1b996 | 350 | |
b38dc31f SC |
351 | [v setButtonType:NSMomentaryPushInButton]; |
352 | wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v ); | |
353 | return c; | |
354 | } | |
355 | ||
73b1b996 | 356 | #endif // wxUSE_BMPBUTTON |
b38dc31f | 357 | |
17ad51ed SC |
358 | // |
359 | // wxDisclosureButton implementation | |
360 | // | |
361 | ||
362 | @interface wxDisclosureNSButton : NSButton | |
363 | { | |
364 | ||
365 | BOOL isOpen; | |
366 | } | |
367 | ||
368 | - (void) updateImage; | |
369 | ||
370 | - (void) toggle; | |
371 | ||
372 | + (NSImage *)rotateImage: (NSImage *)image; | |
373 | ||
374 | @end | |
375 | ||
da52d42b SC |
376 | static const char * disc_triangle_xpm[] = { |
377 | "10 9 4 1", | |
378 | " c None", | |
379 | ". c #737373", | |
380 | "+ c #989898", | |
381 | "- c #c6c6c6", | |
382 | " .- ", | |
383 | " ..+- ", | |
384 | " ....+ ", | |
385 | " ......- ", | |
386 | " .......- ", | |
387 | " ......- ", | |
388 | " ....+ ", | |
389 | " ..+- ", | |
390 | " .- ", | |
391 | }; | |
392 | ||
17ad51ed SC |
393 | @implementation wxDisclosureNSButton |
394 | ||
395 | + (void)initialize | |
396 | { | |
397 | static BOOL initialized = NO; | |
b727fcd3 | 398 | if (!initialized) |
17ad51ed SC |
399 | { |
400 | initialized = YES; | |
401 | wxOSXCocoaClassAddWXMethods( self ); | |
402 | } | |
403 | } | |
404 | ||
405 | - (id) initWithFrame:(NSRect) frame | |
406 | { | |
407 | self = [super initWithFrame:frame]; | |
17ad51ed SC |
408 | isOpen = NO; |
409 | [self setImagePosition:NSImageLeft]; | |
410 | [self updateImage]; | |
411 | return self; | |
412 | } | |
413 | ||
414 | - (int) intValue | |
415 | { | |
416 | return isOpen ? 1 : 0; | |
417 | } | |
418 | ||
419 | - (void) setIntValue: (int) v | |
420 | { | |
421 | isOpen = ( v != 0 ); | |
422 | [self updateImage]; | |
423 | } | |
424 | ||
425 | - (void) toggle | |
426 | { | |
427 | isOpen = !isOpen; | |
428 | [self updateImage]; | |
429 | } | |
430 | ||
431 | wxCFRef<NSImage*> downArray ; | |
432 | ||
433 | - (void) updateImage | |
434 | { | |
da52d42b | 435 | static wxBitmap trianglebm(disc_triangle_xpm); |
17ad51ed SC |
436 | if ( downArray.get() == NULL ) |
437 | { | |
adf264f2 | 438 | downArray.reset( [[wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] retain] ); |
17ad51ed | 439 | } |
b727fcd3 | 440 | |
17ad51ed SC |
441 | if ( isOpen ) |
442 | [self setImage:(NSImage*)downArray.get()]; | |
443 | else | |
da52d42b | 444 | [self setImage:trianglebm.GetNSImage()]; |
17ad51ed SC |
445 | } |
446 | ||
447 | + (NSImage *)rotateImage: (NSImage *)image | |
448 | { | |
449 | NSSize imageSize = [image size]; | |
450 | NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width); | |
451 | NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize]; | |
b727fcd3 | 452 | |
17ad51ed | 453 | [newImage lockFocus]; |
b727fcd3 | 454 | |
17ad51ed SC |
455 | NSAffineTransform* tm = [NSAffineTransform transform]; |
456 | [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2]; | |
457 | [tm rotateByDegrees:-90]; | |
458 | [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2]; | |
459 | [tm concat]; | |
b727fcd3 VZ |
460 | |
461 | ||
17ad51ed SC |
462 | [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height) |
463 | fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0]; | |
b727fcd3 | 464 | |
17ad51ed | 465 | [newImage unlockFocus]; |
adf264f2 | 466 | return [newImage autorelease]; |
17ad51ed SC |
467 | } |
468 | ||
469 | @end | |
470 | ||
471 | class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl | |
472 | { | |
473 | public : | |
474 | wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) : | |
475 | wxWidgetCocoaImpl(peer, w) | |
476 | { | |
477 | } | |
b727fcd3 | 478 | |
17ad51ed SC |
479 | ~wxDisclosureTriangleCocoaImpl() |
480 | { | |
481 | } | |
482 | ||
483 | virtual void controlAction(WXWidget slf, void* _cmd, void *sender) | |
484 | { | |
485 | wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView; | |
486 | [db toggle]; | |
487 | wxWidgetCocoaImpl::controlAction(slf, _cmd, sender ); | |
488 | } | |
489 | }; | |
490 | ||
b727fcd3 VZ |
491 | wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer, |
492 | wxWindowMac* WXUNUSED(parent), | |
04a6d8ef | 493 | wxWindowID winid, |
dbeddfb9 | 494 | const wxString& label, |
b727fcd3 | 495 | const wxPoint& pos, |
dbeddfb9 | 496 | const wxSize& size, |
73b1b996 | 497 | long style, |
b727fcd3 | 498 | long WXUNUSED(extraStyle)) |
dbeddfb9 | 499 | { |
dbeddfb9 | 500 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
17ad51ed | 501 | wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r]; |
73b1b996 VZ |
502 | if ( !label.empty() ) |
503 | [v setTitle:wxCFStringRef(label).AsNSString()]; | |
504 | ||
04a6d8ef | 505 | SetBezelStyleFromBorderFlags(v, style, winid, label); |
73b1b996 VZ |
506 | |
507 | return new wxDisclosureTriangleCocoaImpl( wxpeer, v ); | |
dbeddfb9 | 508 | } |