]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/button.mm
Use the app name, not display name, as debug report name,
[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 if ( GetId() == wxID_HELP )
24 return wxSize( 23 , 23 ) ;
25
26 wxRect r ;
27 m_peer->GetBestRect(&r);
28
29 wxSize sz = r.GetSize();
30
31 const int wBtnStd = GetDefaultSize().x;
32
33 if ( (sz.x < wBtnStd) && !HasFlag(wxBU_EXACTFIT) )
34 sz.x = wBtnStd;
35
36 return sz ;
37 }
38
39 wxSize wxButton::GetDefaultSize()
40 {
41 return wxSize(84, 23);
42 }
43
44 @implementation wxNSButton
45
46 + (void)initialize
47 {
48 static BOOL initialized = NO;
49 if (!initialized)
50 {
51 initialized = YES;
52 wxOSXCocoaClassAddWXMethods( self );
53 }
54 }
55
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
85 @end
86
87 namespace
88 {
89
90 class wxButtonCocoaImpl : public wxWidgetCocoaImpl
91 {
92 public:
93 wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v)
94 : wxWidgetCocoaImpl(wxpeer, v)
95 {
96 }
97
98 virtual void SetBitmap(const wxBitmap& bitmap)
99 {
100 [GetNSButton() setBezelStyle:bitmap.IsOk() ? NSRegularSquareBezelStyle
101 : NSRoundedBezelStyle];
102
103 wxWidgetCocoaImpl::SetBitmap(bitmap);
104 }
105
106 private:
107 NSButton *GetNSButton() const
108 {
109 wxASSERT( [m_osxView isKindOfClass:[NSButton class]] );
110
111 return static_cast<NSButton *>(m_osxView);
112 }
113 };
114
115 } // anonymous namespace
116
117 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
118 wxWindowMac* WXUNUSED(parent),
119 wxWindowID id,
120 const wxString& WXUNUSED(label),
121 const wxPoint& pos,
122 const wxSize& size,
123 long WXUNUSED(style),
124 long WXUNUSED(extraStyle))
125 {
126 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
127 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
128
129 if ( id == wxID_HELP )
130 {
131 [v setBezelStyle:NSHelpButtonBezelStyle];
132 }
133 else
134 {
135 [v setBezelStyle:NSRoundedBezelStyle];
136 }
137
138 [v setButtonType:NSMomentaryPushInButton];
139 return new wxButtonCocoaImpl( wxpeer, v );
140 }
141
142 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
143 {
144 if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
145 // NOTE: setKeyEquivalent: nil will trigger an assert
146 // instead do not call in that case.
147 [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
148 }
149
150 void wxWidgetCocoaImpl::PerformClick()
151 {
152 }
153
154 //
155 // wxDisclosureButton implementation
156 //
157
158 @interface wxDisclosureNSButton : NSButton
159 {
160
161 BOOL isOpen;
162 }
163
164 - (void) updateImage;
165
166 - (void) toggle;
167
168 + (NSImage *)rotateImage: (NSImage *)image;
169
170 @end
171
172 static const char * disc_triangle_xpm[] = {
173 "10 9 4 1",
174 " c None",
175 ". c #737373",
176 "+ c #989898",
177 "- c #c6c6c6",
178 " .- ",
179 " ..+- ",
180 " ....+ ",
181 " ......- ",
182 " .......- ",
183 " ......- ",
184 " ....+ ",
185 " ..+- ",
186 " .- ",
187 };
188
189 @implementation wxDisclosureNSButton
190
191 + (void)initialize
192 {
193 static BOOL initialized = NO;
194 if (!initialized)
195 {
196 initialized = YES;
197 wxOSXCocoaClassAddWXMethods( self );
198 }
199 }
200
201 - (id) initWithFrame:(NSRect) frame
202 {
203 self = [super initWithFrame:frame];
204 [self setBezelStyle:NSSmallSquareBezelStyle];
205 isOpen = NO;
206 [self setImagePosition:NSImageLeft];
207 [self updateImage];
208 return self;
209 }
210
211 - (int) intValue
212 {
213 return isOpen ? 1 : 0;
214 }
215
216 - (void) setIntValue: (int) v
217 {
218 isOpen = ( v != 0 );
219 [self updateImage];
220 }
221
222 - (void) toggle
223 {
224 isOpen = !isOpen;
225 [self updateImage];
226 }
227
228 wxCFRef<NSImage*> downArray ;
229
230 - (void) updateImage
231 {
232 static wxBitmap trianglebm(disc_triangle_xpm);
233 if ( downArray.get() == NULL )
234 {
235 downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
236 }
237
238 if ( isOpen )
239 [self setImage:(NSImage*)downArray.get()];
240 else
241 [self setImage:trianglebm.GetNSImage()];
242 }
243
244 + (NSImage *)rotateImage: (NSImage *)image
245 {
246 NSSize imageSize = [image size];
247 NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
248 NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
249
250 [newImage lockFocus];
251
252 NSAffineTransform* tm = [NSAffineTransform transform];
253 [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
254 [tm rotateByDegrees:-90];
255 [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
256 [tm concat];
257
258
259 [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
260 fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
261
262 [newImage unlockFocus];
263 return newImage;
264 }
265
266 @end
267
268 class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
269 {
270 public :
271 wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
272 wxWidgetCocoaImpl(peer, w)
273 {
274 }
275
276 ~wxDisclosureTriangleCocoaImpl()
277 {
278 }
279
280 virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
281 {
282 wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
283 [db toggle];
284 wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
285 }
286 };
287
288 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
289 wxWindowMac* WXUNUSED(parent),
290 wxWindowID WXUNUSED(id),
291 const wxString& label,
292 const wxPoint& pos,
293 const wxSize& size,
294 long WXUNUSED(style),
295 long WXUNUSED(extraStyle))
296 {
297 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
298 wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
299 [v setTitle:wxCFStringRef( label).AsNSString()];
300 wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v );
301 return c;
302 }