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