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