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