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