]>
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 | ||
85 | @end | |
86 | ||
e5d05b90 VZ |
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 | |
f033830e | 116 | |
b727fcd3 VZ |
117 | wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer, |
118 | wxWindowMac* WXUNUSED(parent), | |
119 | wxWindowID id, | |
d8207702 | 120 | const wxString& WXUNUSED(label), |
b727fcd3 | 121 | const wxPoint& pos, |
f033830e | 122 | const wxSize& size, |
b727fcd3 VZ |
123 | long WXUNUSED(style), |
124 | long WXUNUSED(extraStyle)) | |
f033830e | 125 | { |
dbeddfb9 | 126 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
f033830e | 127 | wxNSButton* v = [[wxNSButton alloc] initWithFrame:r]; |
b727fcd3 | 128 | |
f033830e SC |
129 | if ( id == wxID_HELP ) |
130 | { | |
131 | [v setBezelStyle:NSHelpButtonBezelStyle]; | |
132 | } | |
133 | else | |
134 | { | |
135 | [v setBezelStyle:NSRoundedBezelStyle]; | |
136 | } | |
b727fcd3 | 137 | |
f033830e | 138 | [v setButtonType:NSMomentaryPushInButton]; |
e5d05b90 | 139 | return new wxButtonCocoaImpl( wxpeer, v ); |
f033830e SC |
140 | } |
141 | ||
142 | void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault ) | |
b727fcd3 | 143 | { |
5ec8cc4d KO |
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" ]; | |
f033830e SC |
148 | } |
149 | ||
b727fcd3 | 150 | void wxWidgetCocoaImpl::PerformClick() |
f033830e | 151 | { |
def32871 SC |
152 | if ([m_osxView isKindOfClass:[NSControl class]]) |
153 | [(NSControl*)m_osxView performClick:nil]; | |
f033830e SC |
154 | } |
155 | ||
17ad51ed SC |
156 | // |
157 | // wxDisclosureButton implementation | |
158 | // | |
159 | ||
160 | @interface wxDisclosureNSButton : NSButton | |
161 | { | |
162 | ||
163 | BOOL isOpen; | |
164 | } | |
165 | ||
166 | - (void) updateImage; | |
167 | ||
168 | - (void) toggle; | |
169 | ||
170 | + (NSImage *)rotateImage: (NSImage *)image; | |
171 | ||
172 | @end | |
173 | ||
da52d42b SC |
174 | static const char * disc_triangle_xpm[] = { |
175 | "10 9 4 1", | |
176 | " c None", | |
177 | ". c #737373", | |
178 | "+ c #989898", | |
179 | "- c #c6c6c6", | |
180 | " .- ", | |
181 | " ..+- ", | |
182 | " ....+ ", | |
183 | " ......- ", | |
184 | " .......- ", | |
185 | " ......- ", | |
186 | " ....+ ", | |
187 | " ..+- ", | |
188 | " .- ", | |
189 | }; | |
190 | ||
17ad51ed SC |
191 | @implementation wxDisclosureNSButton |
192 | ||
193 | + (void)initialize | |
194 | { | |
195 | static BOOL initialized = NO; | |
b727fcd3 | 196 | if (!initialized) |
17ad51ed SC |
197 | { |
198 | initialized = YES; | |
199 | wxOSXCocoaClassAddWXMethods( self ); | |
200 | } | |
201 | } | |
202 | ||
203 | - (id) initWithFrame:(NSRect) frame | |
204 | { | |
205 | self = [super initWithFrame:frame]; | |
206 | [self setBezelStyle:NSSmallSquareBezelStyle]; | |
207 | isOpen = NO; | |
208 | [self setImagePosition:NSImageLeft]; | |
209 | [self updateImage]; | |
210 | return self; | |
211 | } | |
212 | ||
213 | - (int) intValue | |
214 | { | |
215 | return isOpen ? 1 : 0; | |
216 | } | |
217 | ||
218 | - (void) setIntValue: (int) v | |
219 | { | |
220 | isOpen = ( v != 0 ); | |
221 | [self updateImage]; | |
222 | } | |
223 | ||
224 | - (void) toggle | |
225 | { | |
226 | isOpen = !isOpen; | |
227 | [self updateImage]; | |
228 | } | |
229 | ||
230 | wxCFRef<NSImage*> downArray ; | |
231 | ||
232 | - (void) updateImage | |
233 | { | |
da52d42b | 234 | static wxBitmap trianglebm(disc_triangle_xpm); |
17ad51ed SC |
235 | if ( downArray.get() == NULL ) |
236 | { | |
da52d42b | 237 | downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] ); |
17ad51ed | 238 | } |
b727fcd3 | 239 | |
17ad51ed SC |
240 | if ( isOpen ) |
241 | [self setImage:(NSImage*)downArray.get()]; | |
242 | else | |
da52d42b | 243 | [self setImage:trianglebm.GetNSImage()]; |
17ad51ed SC |
244 | } |
245 | ||
246 | + (NSImage *)rotateImage: (NSImage *)image | |
247 | { | |
248 | NSSize imageSize = [image size]; | |
249 | NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width); | |
250 | NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize]; | |
b727fcd3 | 251 | |
17ad51ed | 252 | [newImage lockFocus]; |
b727fcd3 | 253 | |
17ad51ed SC |
254 | NSAffineTransform* tm = [NSAffineTransform transform]; |
255 | [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2]; | |
256 | [tm rotateByDegrees:-90]; | |
257 | [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2]; | |
258 | [tm concat]; | |
b727fcd3 VZ |
259 | |
260 | ||
17ad51ed SC |
261 | [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height) |
262 | fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0]; | |
b727fcd3 | 263 | |
17ad51ed SC |
264 | [newImage unlockFocus]; |
265 | return newImage; | |
266 | } | |
267 | ||
268 | @end | |
269 | ||
270 | class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl | |
271 | { | |
272 | public : | |
273 | wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) : | |
274 | wxWidgetCocoaImpl(peer, w) | |
275 | { | |
276 | } | |
b727fcd3 | 277 | |
17ad51ed SC |
278 | ~wxDisclosureTriangleCocoaImpl() |
279 | { | |
280 | } | |
281 | ||
282 | virtual void controlAction(WXWidget slf, void* _cmd, void *sender) | |
283 | { | |
284 | wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView; | |
285 | [db toggle]; | |
286 | wxWidgetCocoaImpl::controlAction(slf, _cmd, sender ); | |
287 | } | |
288 | }; | |
289 | ||
b727fcd3 VZ |
290 | wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer, |
291 | wxWindowMac* WXUNUSED(parent), | |
292 | wxWindowID WXUNUSED(id), | |
dbeddfb9 | 293 | const wxString& label, |
b727fcd3 | 294 | const wxPoint& pos, |
dbeddfb9 | 295 | const wxSize& size, |
b727fcd3 VZ |
296 | long WXUNUSED(style), |
297 | long WXUNUSED(extraStyle)) | |
dbeddfb9 | 298 | { |
dbeddfb9 | 299 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
17ad51ed | 300 | wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r]; |
dbeddfb9 | 301 | [v setTitle:wxCFStringRef( label).AsNSString()]; |
17ad51ed | 302 | wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v ); |
dbeddfb9 SC |
303 | return c; |
304 | } |