]>
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 | ||
7f08aa6c SC |
133 | extern "C" void SetBezelStyleFromBorderFlags(NSButton *v, long style); |
134 | ||
73b1b996 VZ |
135 | // set bezel style depending on the wxBORDER_XXX flags specified by the style |
136 | void SetBezelStyleFromBorderFlags(NSButton *v, long style) | |
137 | { | |
138 | if ( style & wxBORDER_NONE ) | |
139 | { | |
140 | [v setBezelStyle:NSShadowlessSquareBezelStyle]; | |
141 | [v setBordered:NO]; | |
142 | } | |
143 | else // we do have a border | |
144 | { | |
145 | // see trac #11128 for a thorough discussion | |
146 | if ( (style & wxBORDER_MASK) == wxBORDER_RAISED ) | |
147 | [v setBezelStyle:NSRegularSquareBezelStyle]; | |
148 | else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN ) | |
149 | [v setBezelStyle:NSSmallSquareBezelStyle]; | |
150 | else | |
151 | [v setBezelStyle:NSShadowlessSquareBezelStyle]; | |
152 | } | |
153 | } | |
154 | ||
e5d05b90 | 155 | } // anonymous namespace |
f033830e | 156 | |
b727fcd3 VZ |
157 | wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer, |
158 | wxWindowMac* WXUNUSED(parent), | |
159 | wxWindowID id, | |
d8207702 | 160 | const wxString& WXUNUSED(label), |
b727fcd3 | 161 | const wxPoint& pos, |
f033830e | 162 | const wxSize& size, |
b727fcd3 VZ |
163 | long WXUNUSED(style), |
164 | long WXUNUSED(extraStyle)) | |
f033830e | 165 | { |
dbeddfb9 | 166 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
f033830e | 167 | wxNSButton* v = [[wxNSButton alloc] initWithFrame:r]; |
b727fcd3 | 168 | |
f033830e SC |
169 | if ( id == wxID_HELP ) |
170 | { | |
171 | [v setBezelStyle:NSHelpButtonBezelStyle]; | |
172 | } | |
173 | else | |
174 | { | |
175 | [v setBezelStyle:NSRoundedBezelStyle]; | |
176 | } | |
b727fcd3 | 177 | |
f033830e | 178 | [v setButtonType:NSMomentaryPushInButton]; |
e5d05b90 | 179 | return new wxButtonCocoaImpl( wxpeer, v ); |
f033830e SC |
180 | } |
181 | ||
182 | void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault ) | |
b727fcd3 | 183 | { |
5ec8cc4d KO |
184 | if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] ) |
185 | // NOTE: setKeyEquivalent: nil will trigger an assert | |
186 | // instead do not call in that case. | |
187 | [(NSButton*)m_osxView setKeyEquivalent: @"\r" ]; | |
f033830e SC |
188 | } |
189 | ||
b727fcd3 | 190 | void wxWidgetCocoaImpl::PerformClick() |
f033830e | 191 | { |
73b1b996 VZ |
192 | if ([m_osxView isKindOfClass:[NSControl class]]) |
193 | [(NSControl*)m_osxView performClick:nil]; | |
f033830e SC |
194 | } |
195 | ||
b38dc31f SC |
196 | #if wxUSE_BMPBUTTON |
197 | ||
198 | wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer, | |
199 | wxWindowMac* WXUNUSED(parent), | |
200 | wxWindowID WXUNUSED(id), | |
201 | const wxBitmap& bitmap, | |
202 | const wxPoint& pos, | |
203 | const wxSize& size, | |
204 | long style, | |
205 | long WXUNUSED(extraStyle)) | |
206 | { | |
207 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; | |
208 | wxNSButton* v = [[wxNSButton alloc] initWithFrame:r]; | |
73b1b996 VZ |
209 | |
210 | SetBezelStyleFromBorderFlags(v, style); | |
211 | ||
b38dc31f SC |
212 | if (bitmap.Ok()) |
213 | [v setImage:bitmap.GetNSImage() ]; | |
73b1b996 | 214 | |
b38dc31f SC |
215 | [v setButtonType:NSMomentaryPushInButton]; |
216 | wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v ); | |
217 | return c; | |
218 | } | |
219 | ||
73b1b996 | 220 | #endif // wxUSE_BMPBUTTON |
b38dc31f | 221 | |
17ad51ed SC |
222 | // |
223 | // wxDisclosureButton implementation | |
224 | // | |
225 | ||
226 | @interface wxDisclosureNSButton : NSButton | |
227 | { | |
228 | ||
229 | BOOL isOpen; | |
230 | } | |
231 | ||
232 | - (void) updateImage; | |
233 | ||
234 | - (void) toggle; | |
235 | ||
236 | + (NSImage *)rotateImage: (NSImage *)image; | |
237 | ||
238 | @end | |
239 | ||
da52d42b SC |
240 | static const char * disc_triangle_xpm[] = { |
241 | "10 9 4 1", | |
242 | " c None", | |
243 | ". c #737373", | |
244 | "+ c #989898", | |
245 | "- c #c6c6c6", | |
246 | " .- ", | |
247 | " ..+- ", | |
248 | " ....+ ", | |
249 | " ......- ", | |
250 | " .......- ", | |
251 | " ......- ", | |
252 | " ....+ ", | |
253 | " ..+- ", | |
254 | " .- ", | |
255 | }; | |
256 | ||
17ad51ed SC |
257 | @implementation wxDisclosureNSButton |
258 | ||
259 | + (void)initialize | |
260 | { | |
261 | static BOOL initialized = NO; | |
b727fcd3 | 262 | if (!initialized) |
17ad51ed SC |
263 | { |
264 | initialized = YES; | |
265 | wxOSXCocoaClassAddWXMethods( self ); | |
266 | } | |
267 | } | |
268 | ||
269 | - (id) initWithFrame:(NSRect) frame | |
270 | { | |
271 | self = [super initWithFrame:frame]; | |
17ad51ed SC |
272 | isOpen = NO; |
273 | [self setImagePosition:NSImageLeft]; | |
274 | [self updateImage]; | |
275 | return self; | |
276 | } | |
277 | ||
278 | - (int) intValue | |
279 | { | |
280 | return isOpen ? 1 : 0; | |
281 | } | |
282 | ||
283 | - (void) setIntValue: (int) v | |
284 | { | |
285 | isOpen = ( v != 0 ); | |
286 | [self updateImage]; | |
287 | } | |
288 | ||
289 | - (void) toggle | |
290 | { | |
291 | isOpen = !isOpen; | |
292 | [self updateImage]; | |
293 | } | |
294 | ||
295 | wxCFRef<NSImage*> downArray ; | |
296 | ||
297 | - (void) updateImage | |
298 | { | |
da52d42b | 299 | static wxBitmap trianglebm(disc_triangle_xpm); |
17ad51ed SC |
300 | if ( downArray.get() == NULL ) |
301 | { | |
da52d42b | 302 | downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] ); |
17ad51ed | 303 | } |
b727fcd3 | 304 | |
17ad51ed SC |
305 | if ( isOpen ) |
306 | [self setImage:(NSImage*)downArray.get()]; | |
307 | else | |
da52d42b | 308 | [self setImage:trianglebm.GetNSImage()]; |
17ad51ed SC |
309 | } |
310 | ||
311 | + (NSImage *)rotateImage: (NSImage *)image | |
312 | { | |
313 | NSSize imageSize = [image size]; | |
314 | NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width); | |
315 | NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize]; | |
b727fcd3 | 316 | |
17ad51ed | 317 | [newImage lockFocus]; |
b727fcd3 | 318 | |
17ad51ed SC |
319 | NSAffineTransform* tm = [NSAffineTransform transform]; |
320 | [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2]; | |
321 | [tm rotateByDegrees:-90]; | |
322 | [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2]; | |
323 | [tm concat]; | |
b727fcd3 VZ |
324 | |
325 | ||
17ad51ed SC |
326 | [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height) |
327 | fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0]; | |
b727fcd3 | 328 | |
17ad51ed SC |
329 | [newImage unlockFocus]; |
330 | return newImage; | |
331 | } | |
332 | ||
333 | @end | |
334 | ||
335 | class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl | |
336 | { | |
337 | public : | |
338 | wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) : | |
339 | wxWidgetCocoaImpl(peer, w) | |
340 | { | |
341 | } | |
b727fcd3 | 342 | |
17ad51ed SC |
343 | ~wxDisclosureTriangleCocoaImpl() |
344 | { | |
345 | } | |
346 | ||
347 | virtual void controlAction(WXWidget slf, void* _cmd, void *sender) | |
348 | { | |
349 | wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView; | |
350 | [db toggle]; | |
351 | wxWidgetCocoaImpl::controlAction(slf, _cmd, sender ); | |
352 | } | |
353 | }; | |
354 | ||
b727fcd3 VZ |
355 | wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer, |
356 | wxWindowMac* WXUNUSED(parent), | |
73b1b996 | 357 | wxWindowID WXUNUSED(winid), |
dbeddfb9 | 358 | const wxString& label, |
b727fcd3 | 359 | const wxPoint& pos, |
dbeddfb9 | 360 | const wxSize& size, |
73b1b996 | 361 | long style, |
b727fcd3 | 362 | long WXUNUSED(extraStyle)) |
dbeddfb9 | 363 | { |
dbeddfb9 | 364 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
17ad51ed | 365 | wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r]; |
73b1b996 VZ |
366 | if ( !label.empty() ) |
367 | [v setTitle:wxCFStringRef(label).AsNSString()]; | |
368 | ||
369 | SetBezelStyleFromBorderFlags(v, style); | |
370 | ||
371 | return new wxDisclosureTriangleCocoaImpl( wxpeer, v ); | |
dbeddfb9 | 372 | } |