]>
Commit | Line | Data |
---|---|---|
33e90275 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/osx/cocoa/window.mm | |
3 | // Purpose: widgets (non tlw) for cocoa | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 2008-06-20 | |
a9a4f229 | 7 | // RCS-ID: $Id$ |
33e90275 SC |
8 | // Copyright: (c) Stefan Csomor |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
05cf95ed | 14 | #ifndef WX_PRECOMP |
f487501a | 15 | #include "wx/dcclient.h" |
ca9047f7 | 16 | #include "wx/frame.h" |
c583fa5a | 17 | #include "wx/log.h" |
75a2c6a1 | 18 | #include "wx/textctrl.h" |
4275201b | 19 | #include "wx/combobox.h" |
05cf95ed | 20 | #endif |
33e90275 SC |
21 | |
22 | #ifdef __WXMAC__ | |
4dd9fdf8 | 23 | #include "wx/osx/private.h" |
33e90275 SC |
24 | #endif |
25 | ||
f2be0678 | 26 | #include "wx/evtloop.h" |
ab9a0b84 | 27 | |
0c530e5a SC |
28 | #if wxUSE_CARET |
29 | #include "wx/caret.h" | |
30 | #endif | |
31 | ||
4dd9fdf8 SC |
32 | #if wxUSE_DRAG_AND_DROP |
33 | #include "wx/dnd.h" | |
34 | #endif | |
35 | ||
a7b9865d KO |
36 | #if wxUSE_TOOLTIPS |
37 | #include "wx/tooltip.h" | |
38 | #endif | |
39 | ||
4dd9fdf8 SC |
40 | #include <objc/objc-runtime.h> |
41 | ||
f06e0fea SC |
42 | // Get the window with the focus |
43 | ||
7cb2a241 | 44 | NSView* GetViewFromResponder( NSResponder* responder ) |
f06e0fea | 45 | { |
7cb2a241 SC |
46 | NSView* view = nil; |
47 | if ( [responder isKindOfClass:[NSTextView class]] ) | |
f06e0fea | 48 | { |
c8fdb345 | 49 | NSView* delegate = (NSView*) [(NSTextView*)responder delegate]; |
7cb2a241 SC |
50 | if ( [delegate isKindOfClass:[NSTextField class] ] ) |
51 | view = delegate; | |
f06e0fea | 52 | else |
7cb2a241 | 53 | view = (NSView*) responder; |
f06e0fea | 54 | } |
7cb2a241 SC |
55 | else |
56 | { | |
57 | if ( [responder isKindOfClass:[NSView class]] ) | |
58 | view = (NSView*) responder; | |
59 | } | |
60 | return view; | |
61 | } | |
62 | ||
63 | NSView* GetFocusedViewInWindow( NSWindow* keyWindow ) | |
64 | { | |
65 | NSView* focusedView = nil; | |
66 | if ( keyWindow != nil ) | |
67 | focusedView = GetViewFromResponder([keyWindow firstResponder]); | |
68 | ||
f06e0fea SC |
69 | return focusedView; |
70 | } | |
71 | ||
7cb2a241 SC |
72 | WXWidget wxWidgetImpl::FindFocus() |
73 | { | |
cffdfa8f | 74 | return GetFocusedViewInWindow( [NSApp keyWindow] ); |
7cb2a241 SC |
75 | } |
76 | ||
dbeddfb9 SC |
77 | NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin ) |
78 | { | |
79 | int x, y, w, h ; | |
80 | ||
81 | window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ; | |
82 | wxRect bounds(x,y,w,h); | |
83 | NSView* sv = (window->GetParent()->GetHandle() ); | |
84 | ||
85 | return wxToNSRect( sv, bounds ); | |
86 | } | |
33e90275 SC |
87 | |
88 | @interface wxNSView : NSView | |
89 | { | |
fc099495 | 90 | NSTrackingRectTag rectTag; |
849754a4 KO |
91 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 |
92 | NSTrackingArea* _trackingArea; | |
93 | #endif | |
33e90275 SC |
94 | } |
95 | ||
03647350 | 96 | // the tracking tag is needed to track mouse enter / exit events |
fc099495 KO |
97 | - (void) setTrackingTag: (NSTrackingRectTag)tag; |
98 | - (NSTrackingRectTag) trackingTag; | |
849754a4 KO |
99 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 |
100 | // under 10.5 we can also track mouse moved events on non-focused windows if | |
101 | // we use the new NSTrackingArea APIs. | |
102 | - (void) updateTrackingArea; | |
103 | - (NSTrackingArea*) trackingArea; | |
104 | #endif | |
33e90275 SC |
105 | @end // wxNSView |
106 | ||
03647350 | 107 | @interface NSView(PossibleMethods) |
ffad7b0d SC |
108 | - (void)setTitle:(NSString *)aString; |
109 | - (void)setStringValue:(NSString *)aString; | |
110 | - (void)setIntValue:(int)anInt; | |
111 | - (void)setFloatValue:(float)aFloat; | |
112 | - (void)setDoubleValue:(double)aDouble; | |
113 | ||
19c7ac3d SC |
114 | - (double)minValue; |
115 | - (double)maxValue; | |
ffad7b0d SC |
116 | - (void)setMinValue:(double)aDouble; |
117 | - (void)setMaxValue:(double)aDouble; | |
118 | ||
119 | - (void)sizeToFit; | |
120 | ||
121 | - (BOOL)isEnabled; | |
122 | - (void)setEnabled:(BOOL)flag; | |
123 | ||
124 | - (void)setImage:(NSImage *)image; | |
125 | - (void)setControlSize:(NSControlSize)size; | |
0c530e5a | 126 | |
6ac636dd SC |
127 | - (void)setFont:(NSFont *)fontObject; |
128 | ||
0c530e5a | 129 | - (id)contentView; |
4dd9fdf8 SC |
130 | |
131 | - (void)setTarget:(id)anObject; | |
132 | - (void)setAction:(SEL)aSelector; | |
133 | - (void)setDoubleAction:(SEL)aSelector; | |
3b2527c7 | 134 | - (void)setBackgroundColor:(NSColor*)aColor; |
bc5c09a3 | 135 | - (void)setOpaque:(BOOL)opaque; |
1a289c62 | 136 | - (void)setTextColor:(NSColor *)color; |
3b2527c7 | 137 | - (void)setImagePosition:(NSCellImagePosition)aPosition; |
03647350 | 138 | @end |
ffad7b0d | 139 | |
a8fc3508 | 140 | long wxOSXTranslateCocoaKey( NSEvent* event, int eventType ) |
524c47aa | 141 | { |
63a6419c KO |
142 | long retval = 0; |
143 | ||
144 | if ([event type] != NSFlagsChanged) | |
03647350 | 145 | { |
63a6419c KO |
146 | NSString* s = [event charactersIgnoringModifiers]; |
147 | // backspace char reports as delete w/modifiers for some reason | |
148 | if ([s length] == 1) | |
149 | { | |
a8fc3508 | 150 | if ( eventType == wxEVT_CHAR && ([event modifierFlags] & NSControlKeyMask) && ( [s characterAtIndex:0] >= 'a' && [s characterAtIndex:0] <= 'z' ) ) |
63a6419c | 151 | { |
a8fc3508 SC |
152 | retval = WXK_CONTROL_A + ([s characterAtIndex:0] - 'a'); |
153 | } | |
154 | else | |
155 | { | |
156 | switch ( [s characterAtIndex:0] ) | |
157 | { | |
158 | // backspace key | |
159 | case 0x7F : | |
160 | case 8 : | |
161 | retval = WXK_BACK; | |
162 | break; | |
163 | case NSUpArrowFunctionKey : | |
164 | retval = WXK_UP; | |
165 | break; | |
166 | case NSDownArrowFunctionKey : | |
167 | retval = WXK_DOWN; | |
168 | break; | |
169 | case NSLeftArrowFunctionKey : | |
170 | retval = WXK_LEFT; | |
171 | break; | |
172 | case NSRightArrowFunctionKey : | |
173 | retval = WXK_RIGHT; | |
174 | break; | |
175 | case NSInsertFunctionKey : | |
176 | retval = WXK_INSERT; | |
177 | break; | |
178 | case NSDeleteFunctionKey : | |
179 | retval = WXK_DELETE; | |
180 | break; | |
181 | case NSHomeFunctionKey : | |
182 | retval = WXK_HOME; | |
183 | break; | |
184 | // case NSBeginFunctionKey : | |
185 | // retval = WXK_BEGIN; | |
186 | // break; | |
187 | case NSEndFunctionKey : | |
188 | retval = WXK_END; | |
189 | break; | |
190 | case NSPageUpFunctionKey : | |
191 | retval = WXK_PAGEUP; | |
192 | break; | |
193 | case NSPageDownFunctionKey : | |
194 | retval = WXK_PAGEDOWN; | |
195 | break; | |
196 | case NSHelpFunctionKey : | |
197 | retval = WXK_HELP; | |
198 | break; | |
199 | default: | |
200 | int intchar = [s characterAtIndex: 0]; | |
201 | if ( intchar >= NSF1FunctionKey && intchar <= NSF24FunctionKey ) | |
202 | retval = WXK_F1 + (intchar - NSF1FunctionKey ); | |
203 | break; | |
204 | } | |
63a6419c KO |
205 | } |
206 | } | |
207 | } | |
03647350 | 208 | |
63a6419c KO |
209 | // Some keys don't seem to have constants. The code mimics the approach |
210 | // taken by WebKit. See: | |
211 | // http://trac.webkit.org/browser/trunk/WebCore/platform/mac/KeyEventMac.mm | |
212 | switch( [event keyCode] ) | |
524c47aa | 213 | { |
63a6419c KO |
214 | // command key |
215 | case 54: | |
216 | case 55: | |
68065b91 | 217 | retval = WXK_CONTROL; |
524c47aa | 218 | break; |
63a6419c KO |
219 | // caps locks key |
220 | case 57: // Capslock | |
221 | retval = WXK_CAPITAL; | |
524c47aa | 222 | break; |
63a6419c KO |
223 | // shift key |
224 | case 56: // Left Shift | |
225 | case 60: // Right Shift | |
226 | retval = WXK_SHIFT; | |
524c47aa | 227 | break; |
63a6419c KO |
228 | // alt key |
229 | case 58: // Left Alt | |
230 | case 61: // Right Alt | |
231 | retval = WXK_ALT; | |
524c47aa | 232 | break; |
63a6419c KO |
233 | // ctrl key |
234 | case 59: // Left Ctrl | |
235 | case 62: // Right Ctrl | |
68065b91 | 236 | retval = WXK_RAW_CONTROL; |
524c47aa | 237 | break; |
63a6419c | 238 | // clear key |
03647350 | 239 | case 71: |
63a6419c | 240 | retval = WXK_CLEAR; |
524c47aa | 241 | break; |
63a6419c | 242 | // tab key |
03647350 | 243 | case 48: |
63a6419c | 244 | retval = WXK_TAB; |
524c47aa SC |
245 | break; |
246 | ||
4d61ae5f SC |
247 | case 75: // / |
248 | retval = WXK_NUMPAD_DIVIDE; | |
03647350 | 249 | break; |
4d61ae5f SC |
250 | case 67: // * |
251 | retval = WXK_NUMPAD_MULTIPLY; | |
252 | break; | |
253 | case 78: // - | |
254 | retval = WXK_NUMPAD_SUBTRACT; | |
255 | break; | |
256 | case 69: // + | |
257 | retval = WXK_NUMPAD_ADD; | |
258 | break; | |
259 | case 76: // Enter | |
260 | retval = WXK_NUMPAD_ENTER; | |
261 | break; | |
262 | case 65: // . | |
263 | retval = WXK_NUMPAD_DECIMAL; | |
264 | break; | |
265 | case 82: // 0 | |
266 | retval = WXK_NUMPAD0; | |
267 | break; | |
268 | case 83: // 1 | |
269 | retval = WXK_NUMPAD1; | |
270 | break; | |
271 | case 84: // 2 | |
272 | retval = WXK_NUMPAD2; | |
273 | break; | |
274 | case 85: // 3 | |
275 | retval = WXK_NUMPAD3; | |
276 | break; | |
277 | case 86: // 4 | |
278 | retval = WXK_NUMPAD4; | |
279 | break; | |
280 | case 87: // 5 | |
281 | retval = WXK_NUMPAD5; | |
282 | break; | |
283 | case 88: // 6 | |
284 | retval = WXK_NUMPAD6; | |
285 | break; | |
286 | case 89: // 7 | |
287 | retval = WXK_NUMPAD7; | |
288 | break; | |
289 | case 91: // 8 | |
290 | retval = WXK_NUMPAD8; | |
291 | break; | |
292 | case 92: // 9 | |
293 | retval = WXK_NUMPAD9; | |
294 | break; | |
295 | default: | |
296 | //retval = [event keyCode]; | |
524c47aa SC |
297 | break; |
298 | } | |
299 | return retval; | |
300 | } | |
301 | ||
ddbc8ac9 | 302 | void wxWidgetCocoaImpl::SetupKeyEvent(wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charString) |
524c47aa SC |
303 | { |
304 | UInt32 modifiers = [nsEvent modifierFlags] ; | |
fc39cf72 | 305 | int eventType = [nsEvent type]; |
524c47aa SC |
306 | |
307 | wxevent.m_shiftDown = modifiers & NSShiftKeyMask; | |
dd9ec596 | 308 | wxevent.m_rawControlDown = modifiers & NSControlKeyMask; |
524c47aa | 309 | wxevent.m_altDown = modifiers & NSAlternateKeyMask; |
dd9ec596 | 310 | wxevent.m_controlDown = modifiers & NSCommandKeyMask; |
03647350 | 311 | |
19c7ac3d | 312 | wxevent.m_rawCode = [nsEvent keyCode]; |
524c47aa | 313 | wxevent.m_rawFlags = modifiers; |
03647350 | 314 | |
e490b0d2 | 315 | wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ; |
19c7ac3d SC |
316 | |
317 | wxString chars; | |
318 | if ( eventType != NSFlagsChanged ) | |
319 | { | |
a8fc3508 | 320 | NSString* nschars = [[nsEvent charactersIgnoringModifiers] uppercaseString]; |
f0e0116e | 321 | if ( charString ) |
63a6419c KO |
322 | { |
323 | // if charString is set, it did not come from key up / key down | |
324 | wxevent.SetEventType( wxEVT_CHAR ); | |
f66ecdc4 | 325 | chars = wxCFStringRef::AsString(charString); |
63a6419c KO |
326 | } |
327 | else if ( nschars ) | |
19c7ac3d | 328 | { |
f66ecdc4 | 329 | chars = wxCFStringRef::AsString(nschars); |
19c7ac3d SC |
330 | } |
331 | } | |
03647350 | 332 | |
63a6419c KO |
333 | int aunichar = chars.Length() > 0 ? chars[0] : 0; |
334 | long keyval = 0; | |
03647350 | 335 | |
63a6419c | 336 | if (wxevent.GetEventType() != wxEVT_CHAR) |
4d61ae5f | 337 | { |
a8fc3508 | 338 | keyval = wxOSXTranslateCocoaKey(nsEvent, wxevent.GetEventType()) ; |
4d61ae5f SC |
339 | switch (eventType) |
340 | { | |
341 | case NSKeyDown : | |
342 | wxevent.SetEventType( wxEVT_KEY_DOWN ) ; | |
343 | break; | |
344 | case NSKeyUp : | |
345 | wxevent.SetEventType( wxEVT_KEY_UP ) ; | |
346 | break; | |
347 | case NSFlagsChanged : | |
348 | switch (keyval) | |
349 | { | |
350 | case WXK_CONTROL: | |
351 | wxevent.SetEventType( wxevent.m_controlDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP); | |
352 | break; | |
353 | case WXK_SHIFT: | |
354 | wxevent.SetEventType( wxevent.m_shiftDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP); | |
355 | break; | |
356 | case WXK_ALT: | |
357 | wxevent.SetEventType( wxevent.m_altDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP); | |
358 | break; | |
a8fc3508 SC |
359 | case WXK_RAW_CONTROL: |
360 | wxevent.SetEventType( wxevent.m_rawControlDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP); | |
4d61ae5f SC |
361 | break; |
362 | } | |
363 | break; | |
364 | default : | |
365 | break ; | |
366 | } | |
367 | } | |
19c7ac3d | 368 | |
63a6419c KO |
369 | if ( !keyval ) |
370 | { | |
371 | if ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN ) | |
372 | keyval = wxToupper( aunichar ) ; | |
373 | else | |
374 | keyval = aunichar; | |
375 | } | |
03647350 | 376 | |
19c7ac3d | 377 | #if wxUSE_UNICODE |
e999b903 VZ |
378 | // OS X generates events with key codes in Unicode private use area for |
379 | // unprintable symbols such as cursor arrows (WXK_UP is mapped to U+F700) | |
380 | // and function keys (WXK_F2 is U+F705). We don't want to use them as the | |
381 | // result of wxKeyEvent::GetUnicodeKey() however as it's supposed to return | |
382 | // WXK_NONE for "non characters" so explicitly exclude them. | |
383 | // | |
384 | // We only exclude the private use area inside the Basic Multilingual Plane | |
385 | // as key codes beyond it don't seem to be currently used. | |
386 | if ( !(aunichar >= 0xe000 && aunichar < 0xf900) ) | |
387 | wxevent.m_uniChar = aunichar; | |
19c7ac3d SC |
388 | #endif |
389 | wxevent.m_keyCode = keyval; | |
ddbc8ac9 SC |
390 | |
391 | wxWindowMac* peer = GetWXPeer(); | |
392 | if ( peer ) | |
393 | { | |
394 | wxevent.SetEventObject(peer); | |
395 | wxevent.SetId(peer->GetId()) ; | |
396 | } | |
524c47aa SC |
397 | } |
398 | ||
54f11060 SC |
399 | UInt32 g_lastButton = 0 ; |
400 | bool g_lastButtonWasFakeRight = false ; | |
401 | ||
83ffdd71 SC |
402 | // better scroll wheel support |
403 | // see http://lists.apple.com/archives/cocoa-dev/2007/Feb/msg00050.html | |
404 | ||
405 | @interface NSEvent (DeviceDelta) | |
b2b61216 VZ |
406 | - (CGFloat)deviceDeltaX; |
407 | - (CGFloat)deviceDeltaY; | |
83ffdd71 SC |
408 | @end |
409 | ||
ddbc8ac9 | 410 | void wxWidgetCocoaImpl::SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent ) |
33e90275 | 411 | { |
4800ca79 | 412 | int eventType = [nsEvent type]; |
33e90275 | 413 | UInt32 modifiers = [nsEvent modifierFlags] ; |
58110007 SC |
414 | |
415 | NSPoint locationInWindow = [nsEvent locationInWindow]; | |
416 | ||
417 | // adjust coordinates for the window of the target view | |
418 | if ( [nsEvent window] != [m_osxView window] ) | |
419 | { | |
420 | if ( [nsEvent window] != nil ) | |
421 | locationInWindow = [[nsEvent window] convertBaseToScreen:locationInWindow]; | |
422 | ||
423 | if ( [m_osxView window] != nil ) | |
424 | locationInWindow = [[m_osxView window] convertScreenToBase:locationInWindow]; | |
425 | } | |
426 | ||
427 | NSPoint locationInView = [m_osxView convertPoint:locationInWindow fromView:nil]; | |
428 | wxPoint locationInViewWX = wxFromNSPoint( m_osxView, locationInView ); | |
33e90275 SC |
429 | |
430 | // these parameters are not given for all events | |
431 | UInt32 button = [nsEvent buttonNumber]; | |
4800ca79 | 432 | UInt32 clickCount = 0; |
33e90275 | 433 | |
58110007 SC |
434 | wxevent.m_x = locationInViewWX.x; |
435 | wxevent.m_y = locationInViewWX.y; | |
33e90275 | 436 | wxevent.m_shiftDown = modifiers & NSShiftKeyMask; |
dd9ec596 | 437 | wxevent.m_rawControlDown = modifiers & NSControlKeyMask; |
33e90275 | 438 | wxevent.m_altDown = modifiers & NSAlternateKeyMask; |
dd9ec596 | 439 | wxevent.m_controlDown = modifiers & NSCommandKeyMask; |
e490b0d2 | 440 | wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ; |
54f11060 | 441 | |
03647350 | 442 | UInt32 mouseChord = 0; |
54f11060 SC |
443 | |
444 | switch (eventType) | |
445 | { | |
446 | case NSLeftMouseDown : | |
447 | case NSLeftMouseDragged : | |
448 | mouseChord = 1U; | |
449 | break; | |
450 | case NSRightMouseDown : | |
451 | case NSRightMouseDragged : | |
452 | mouseChord = 2U; | |
453 | break; | |
454 | case NSOtherMouseDown : | |
455 | case NSOtherMouseDragged : | |
456 | mouseChord = 4U; | |
457 | break; | |
458 | } | |
459 | ||
33e90275 SC |
460 | // a control click is interpreted as a right click |
461 | bool thisButtonIsFakeRight = false ; | |
54f11060 | 462 | if ( button == 0 && (modifiers & NSControlKeyMask) ) |
33e90275 | 463 | { |
54f11060 | 464 | button = 1 ; |
33e90275 SC |
465 | thisButtonIsFakeRight = true ; |
466 | } | |
467 | ||
468 | // otherwise we report double clicks by connecting a left click with a ctrl-left click | |
469 | if ( clickCount > 1 && button != g_lastButton ) | |
470 | clickCount = 1 ; | |
54f11060 | 471 | |
33e90275 SC |
472 | // we must make sure that our synthetic 'right' button corresponds in |
473 | // mouse down, moved and mouse up, and does not deliver a right down and left up | |
54f11060 | 474 | switch (eventType) |
33e90275 | 475 | { |
54f11060 SC |
476 | case NSLeftMouseDown : |
477 | case NSRightMouseDown : | |
478 | case NSOtherMouseDown : | |
479 | g_lastButton = button ; | |
480 | g_lastButtonWasFakeRight = thisButtonIsFakeRight ; | |
481 | break; | |
482 | } | |
33e90275 SC |
483 | |
484 | if ( button == 0 ) | |
485 | { | |
486 | g_lastButton = 0 ; | |
487 | g_lastButtonWasFakeRight = false ; | |
488 | } | |
54f11060 | 489 | else if ( g_lastButton == 1 && g_lastButtonWasFakeRight ) |
33e90275 SC |
490 | button = g_lastButton ; |
491 | ||
492 | // Adjust the chord mask to remove the primary button and add the | |
493 | // secondary button. It is possible that the secondary button is | |
494 | // already pressed, e.g. on a mouse connected to a laptop, but this | |
495 | // possibility is ignored here: | |
496 | if( thisButtonIsFakeRight && ( mouseChord & 1U ) ) | |
497 | mouseChord = ((mouseChord & ~1U) | 2U); | |
498 | ||
499 | if(mouseChord & 1U) | |
500 | wxevent.m_leftDown = true ; | |
501 | if(mouseChord & 2U) | |
502 | wxevent.m_rightDown = true ; | |
503 | if(mouseChord & 4U) | |
504 | wxevent.m_middleDown = true ; | |
505 | ||
33e90275 | 506 | // translate into wx types |
33e90275 SC |
507 | switch (eventType) |
508 | { | |
509 | case NSLeftMouseDown : | |
510 | case NSRightMouseDown : | |
511 | case NSOtherMouseDown : | |
08c1b134 | 512 | clickCount = [nsEvent clickCount]; |
33e90275 SC |
513 | switch ( button ) |
514 | { | |
515 | case 0 : | |
516 | wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ; | |
517 | break ; | |
518 | ||
519 | case 1 : | |
520 | wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ; | |
521 | break ; | |
522 | ||
523 | case 2 : | |
524 | wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ; | |
525 | break ; | |
526 | ||
527 | default: | |
528 | break ; | |
529 | } | |
530 | break ; | |
531 | ||
532 | case NSLeftMouseUp : | |
533 | case NSRightMouseUp : | |
534 | case NSOtherMouseUp : | |
08c1b134 | 535 | clickCount = [nsEvent clickCount]; |
33e90275 SC |
536 | switch ( button ) |
537 | { | |
538 | case 0 : | |
539 | wxevent.SetEventType( wxEVT_LEFT_UP ) ; | |
540 | break ; | |
541 | ||
542 | case 1 : | |
543 | wxevent.SetEventType( wxEVT_RIGHT_UP ) ; | |
544 | break ; | |
545 | ||
546 | case 2 : | |
547 | wxevent.SetEventType( wxEVT_MIDDLE_UP ) ; | |
548 | break ; | |
549 | ||
550 | default: | |
551 | break ; | |
552 | } | |
553 | break ; | |
554 | ||
555 | case NSScrollWheel : | |
556 | { | |
a533f050 SC |
557 | float deltaX = 0.0; |
558 | float deltaY = 0.0; | |
559 | ||
33e90275 | 560 | wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ; |
a533f050 SC |
561 | |
562 | // see http://developer.apple.com/qa/qa2005/qa1453.html | |
563 | // for more details on why we have to look for the exact type | |
564 | ||
565 | const EventRef cEvent = (EventRef) [nsEvent eventRef]; | |
566 | bool isMouseScrollEvent = false; | |
567 | if ( cEvent ) | |
568 | isMouseScrollEvent = ::GetEventKind(cEvent) == kEventMouseScroll; | |
569 | ||
570 | if ( isMouseScrollEvent ) | |
571 | { | |
572 | deltaX = [nsEvent deviceDeltaX]; | |
573 | deltaY = [nsEvent deviceDeltaY]; | |
574 | } | |
575 | else | |
576 | { | |
577 | deltaX = ([nsEvent deltaX] * 10); | |
578 | deltaY = ([nsEvent deltaY] * 10); | |
579 | } | |
580 | ||
e32090ba | 581 | wxevent.m_wheelDelta = 10; |
33e90275 | 582 | wxevent.m_linesPerAction = 1; |
a533f050 SC |
583 | |
584 | if ( fabs(deltaX) > fabs(deltaY) ) | |
4800ca79 | 585 | { |
33e90275 | 586 | wxevent.m_wheelAxis = 1; |
a533f050 | 587 | wxevent.m_wheelRotation = (int)deltaX; |
4800ca79 SC |
588 | } |
589 | else | |
590 | { | |
a533f050 | 591 | wxevent.m_wheelRotation = (int)deltaY; |
4800ca79 | 592 | } |
a533f050 | 593 | |
33e90275 SC |
594 | } |
595 | break ; | |
596 | ||
597 | case NSMouseEntered : | |
f1f15003 KO |
598 | wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ; |
599 | break; | |
33e90275 | 600 | case NSMouseExited : |
f1f15003 KO |
601 | wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ; |
602 | break; | |
33e90275 SC |
603 | case NSLeftMouseDragged : |
604 | case NSRightMouseDragged : | |
605 | case NSOtherMouseDragged : | |
606 | case NSMouseMoved : | |
607 | wxevent.SetEventType( wxEVT_MOTION ) ; | |
608 | break; | |
609 | default : | |
610 | break ; | |
611 | } | |
03647350 | 612 | |
08c1b134 | 613 | wxevent.m_clickCount = clickCount; |
ddbc8ac9 SC |
614 | wxWindowMac* peer = GetWXPeer(); |
615 | if ( peer ) | |
616 | { | |
617 | wxevent.SetEventObject(peer); | |
618 | wxevent.SetId(peer->GetId()) ; | |
619 | } | |
33e90275 SC |
620 | } |
621 | ||
622 | @implementation wxNSView | |
623 | ||
4dd9fdf8 SC |
624 | + (void)initialize |
625 | { | |
626 | static BOOL initialized = NO; | |
03647350 | 627 | if (!initialized) |
4dd9fdf8 SC |
628 | { |
629 | initialized = YES; | |
630 | wxOSXCocoaClassAddWXMethods( self ); | |
631 | } | |
632 | } | |
633 | ||
fc099495 KO |
634 | - (void) setTrackingTag: (NSTrackingRectTag)tag |
635 | { | |
636 | rectTag = tag; | |
637 | } | |
638 | ||
639 | - (NSTrackingRectTag) trackingTag | |
640 | { | |
641 | return rectTag; | |
642 | } | |
643 | ||
849754a4 KO |
644 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 |
645 | - (void) updateTrackingArea | |
646 | { | |
647 | if (_trackingArea) | |
648 | { | |
649 | [self removeTrackingArea: _trackingArea]; | |
650 | [_trackingArea release]; | |
651 | } | |
652 | ||
653 | NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited|NSTrackingMouseMoved|NSTrackingActiveAlways; | |
654 | ||
655 | NSTrackingArea* area = [[NSTrackingArea alloc] initWithRect: [self bounds] options: options owner: self userInfo: nil]; | |
656 | [self addTrackingArea: area]; | |
657 | ||
658 | _trackingArea = area; | |
659 | } | |
660 | ||
661 | - (NSTrackingArea*) trackingArea | |
662 | { | |
663 | return _trackingArea; | |
664 | } | |
665 | #endif | |
4dd9fdf8 SC |
666 | @end // wxNSView |
667 | ||
668 | // | |
669 | // event handlers | |
670 | // | |
671 | ||
672 | #if wxUSE_DRAG_AND_DROP | |
673 | ||
674 | // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html | |
675 | // for details on the NSPasteboard -> PasteboardRef conversion | |
676 | ||
677 | NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender ) | |
678 | { | |
679 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
680 | if (impl == NULL) | |
681 | return NSDragOperationNone; | |
03647350 | 682 | |
4dd9fdf8 SC |
683 | return impl->draggingEntered(sender, self, _cmd); |
684 | } | |
685 | ||
686 | void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender ) | |
687 | { | |
688 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
689 | if (impl == NULL) | |
690 | return ; | |
03647350 | 691 | |
4dd9fdf8 SC |
692 | return impl->draggingExited(sender, self, _cmd); |
693 | } | |
694 | ||
695 | NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender ) | |
696 | { | |
697 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
698 | if (impl == NULL) | |
699 | return NSDragOperationNone; | |
03647350 | 700 | |
4dd9fdf8 SC |
701 | return impl->draggingUpdated(sender, self, _cmd); |
702 | } | |
703 | ||
704 | BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender ) | |
705 | { | |
706 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
707 | if (impl == NULL) | |
708 | return NSDragOperationNone; | |
03647350 | 709 | |
4dd9fdf8 SC |
710 | return impl->performDragOperation(sender, self, _cmd) ? YES:NO ; |
711 | } | |
712 | ||
7cb2a241 SC |
713 | #endif |
714 | ||
03647350 | 715 | void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event) |
4dd9fdf8 SC |
716 | { |
717 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
718 | if (impl == NULL) | |
719 | return; | |
03647350 | 720 | |
4dd9fdf8 SC |
721 | impl->mouseEvent(event, self, _cmd); |
722 | } | |
723 | ||
849252d5 | 724 | BOOL wxOSX_acceptsFirstMouse(NSView* WXUNUSED(self), SEL WXUNUSED(_cmd), NSEvent *WXUNUSED(event)) |
5a900d50 KO |
725 | { |
726 | // This is needed to support click through, otherwise the first click on a window | |
727 | // will not do anything unless it is the active window already. | |
728 | return YES; | |
729 | } | |
730 | ||
03647350 | 731 | void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event) |
4dd9fdf8 SC |
732 | { |
733 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
734 | if (impl == NULL) | |
735 | return; | |
03647350 | 736 | |
4dd9fdf8 SC |
737 | impl->keyEvent(event, self, _cmd); |
738 | } | |
739 | ||
03647350 | 740 | void wxOSX_insertText(NSView* self, SEL _cmd, NSString* text) |
f0e0116e KO |
741 | { |
742 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
743 | if (impl == NULL) | |
744 | return; | |
03647350 | 745 | |
f0e0116e KO |
746 | impl->insertText(text, self, _cmd); |
747 | } | |
748 | ||
03647350 | 749 | BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event) |
4dd9fdf8 SC |
750 | { |
751 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
752 | if (impl == NULL) | |
753 | return NO; | |
03647350 | 754 | |
4dd9fdf8 SC |
755 | return impl->performKeyEquivalent(event, self, _cmd); |
756 | } | |
757 | ||
09a9eb20 KO |
758 | BOOL wxOSX_acceptsFirstResponder(NSView* self, SEL _cmd) |
759 | { | |
760 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
761 | if (impl == NULL) | |
762 | return NO; | |
03647350 | 763 | |
09a9eb20 KO |
764 | return impl->acceptsFirstResponder(self, _cmd); |
765 | } | |
766 | ||
4dd9fdf8 SC |
767 | BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd) |
768 | { | |
769 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
770 | if (impl == NULL) | |
771 | return NO; | |
03647350 | 772 | |
4dd9fdf8 SC |
773 | return impl->becomeFirstResponder(self, _cmd); |
774 | } | |
775 | ||
776 | BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd) | |
777 | { | |
778 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
779 | if (impl == NULL) | |
780 | return NO; | |
03647350 | 781 | |
4dd9fdf8 SC |
782 | return impl->resignFirstResponder(self, _cmd); |
783 | } | |
784 | ||
785 | void wxOSX_resetCursorRects(NSView* self, SEL _cmd) | |
786 | { | |
787 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
788 | if (impl == NULL) | |
789 | return; | |
03647350 | 790 | |
4dd9fdf8 SC |
791 | impl->resetCursorRects(self, _cmd); |
792 | } | |
793 | ||
794 | BOOL wxOSX_isFlipped(NSView* self, SEL _cmd) | |
795 | { | |
796 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
797 | if (impl == NULL) | |
798 | return NO; | |
03647350 | 799 | |
4dd9fdf8 SC |
800 | return impl->isFlipped(self, _cmd) ? YES:NO; |
801 | } | |
802 | ||
c71096a0 VZ |
803 | typedef void (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect); |
804 | ||
4dd9fdf8 SC |
805 | void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect) |
806 | { | |
807 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
808 | if (impl == NULL) | |
809 | return; | |
03647350 | 810 | |
c71096a0 VZ |
811 | #ifdef wxUSE_THREADS |
812 | // OS X starts a NSUIHeartBeatThread for animating the default button in a | |
813 | // dialog. This causes a drawRect of the active dialog from outside the | |
814 | // main UI thread. This causes an occasional crash since the wx drawing | |
815 | // objects (like wxPen) are not thread safe. | |
816 | // | |
817 | // Notice that NSUIHeartBeatThread seems to be undocumented and doing | |
818 | // [NSWindow setAllowsConcurrentViewDrawing:NO] does not affect it. | |
819 | if ( !wxThread::IsMain() ) | |
820 | { | |
d4a4ef90 SC |
821 | if ( impl->IsUserPane() ) |
822 | { | |
823 | wxWindow* win = impl->GetWXPeer(); | |
824 | if ( win->UseBgCol() ) | |
825 | { | |
826 | ||
827 | CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort]; | |
828 | CGContextSaveGState( context ); | |
829 | ||
830 | CGContextSetFillColorWithColor( context, win->GetBackgroundColour().GetCGColor()); | |
a3c22212 SC |
831 | CGRect r = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height); |
832 | CGContextFillRect( context, r ); | |
d4a4ef90 SC |
833 | |
834 | CGContextRestoreGState( context ); | |
835 | } | |
836 | } | |
837 | else | |
838 | { | |
839 | // just call the superclass handler, we don't need any custom wx drawing | |
840 | // here and it seems to work fine: | |
841 | wxOSX_DrawRectHandlerPtr | |
842 | superimpl = (wxOSX_DrawRectHandlerPtr) | |
843 | [[self superclass] instanceMethodForSelector:_cmd]; | |
844 | superimpl(self, _cmd, rect); | |
845 | } | |
846 | ||
c71096a0 VZ |
847 | return; |
848 | } | |
849 | #endif // wxUSE_THREADS | |
850 | ||
4dd9fdf8 SC |
851 | return impl->drawRect(&rect, self, _cmd); |
852 | } | |
853 | ||
e32090ba | 854 | void wxOSX_controlAction(NSView* self, SEL _cmd, id sender) |
4dd9fdf8 SC |
855 | { |
856 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
857 | if (impl == NULL) | |
858 | return; | |
03647350 | 859 | |
e32090ba | 860 | impl->controlAction(self, _cmd, sender); |
4dd9fdf8 SC |
861 | } |
862 | ||
e32090ba | 863 | void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender) |
4dd9fdf8 SC |
864 | { |
865 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
866 | if (impl == NULL) | |
867 | return; | |
03647350 | 868 | |
e32090ba | 869 | impl->controlDoubleAction(self, _cmd, sender); |
4dd9fdf8 | 870 | } |
dbeddfb9 | 871 | |
d8207702 | 872 | unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd)) |
33e90275 | 873 | { |
4dd9fdf8 SC |
874 | id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s; |
875 | NSPasteboard *pboard = [sender draggingPasteboard]; | |
876 | NSDragOperation sourceDragMask = [sender draggingSourceOperationMask]; | |
03647350 | 877 | |
4dd9fdf8 SC |
878 | wxWindow* wxpeer = GetWXPeer(); |
879 | if ( wxpeer == NULL ) | |
880 | return NSDragOperationNone; | |
03647350 | 881 | |
4dd9fdf8 SC |
882 | wxDropTarget* target = wxpeer->GetDropTarget(); |
883 | if ( target == NULL ) | |
884 | return NSDragOperationNone; | |
885 | ||
886 | wxDragResult result = wxDragNone; | |
e6b3143a SC |
887 | NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil]; |
888 | wxPoint pt = wxFromNSPoint( m_osxView, nspoint ); | |
4dd9fdf8 SC |
889 | |
890 | if ( sourceDragMask & NSDragOperationLink ) | |
891 | result = wxDragLink; | |
892 | else if ( sourceDragMask & NSDragOperationCopy ) | |
893 | result = wxDragCopy; | |
894 | else if ( sourceDragMask & NSDragOperationMove ) | |
895 | result = wxDragMove; | |
896 | ||
03647350 | 897 | PasteboardRef pboardRef; |
4dd9fdf8 SC |
898 | PasteboardCreate((CFStringRef)[pboard name], &pboardRef); |
899 | target->SetCurrentDragPasteboard(pboardRef); | |
900 | result = target->OnEnter(pt.x, pt.y, result); | |
901 | CFRelease(pboardRef); | |
03647350 | 902 | |
4dd9fdf8 SC |
903 | NSDragOperation nsresult = NSDragOperationNone; |
904 | switch (result ) | |
33e90275 | 905 | { |
4dd9fdf8 SC |
906 | case wxDragLink: |
907 | nsresult = NSDragOperationLink; | |
908 | case wxDragMove: | |
909 | nsresult = NSDragOperationMove; | |
910 | case wxDragCopy: | |
911 | nsresult = NSDragOperationCopy; | |
912 | default : | |
913 | break; | |
914 | } | |
915 | return nsresult; | |
916 | } | |
33e90275 | 917 | |
d8207702 | 918 | void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd)) |
4dd9fdf8 SC |
919 | { |
920 | id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s; | |
921 | NSPasteboard *pboard = [sender draggingPasteboard]; | |
03647350 | 922 | |
4dd9fdf8 SC |
923 | wxWindow* wxpeer = GetWXPeer(); |
924 | if ( wxpeer == NULL ) | |
925 | return; | |
03647350 | 926 | |
4dd9fdf8 SC |
927 | wxDropTarget* target = wxpeer->GetDropTarget(); |
928 | if ( target == NULL ) | |
929 | return; | |
03647350 VZ |
930 | |
931 | PasteboardRef pboardRef; | |
4dd9fdf8 SC |
932 | PasteboardCreate((CFStringRef)[pboard name], &pboardRef); |
933 | target->SetCurrentDragPasteboard(pboardRef); | |
934 | target->OnLeave(); | |
935 | CFRelease(pboardRef); | |
936 | } | |
937 | ||
d8207702 | 938 | unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd)) |
4dd9fdf8 SC |
939 | { |
940 | id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s; | |
941 | NSPasteboard *pboard = [sender draggingPasteboard]; | |
942 | NSDragOperation sourceDragMask = [sender draggingSourceOperationMask]; | |
03647350 | 943 | |
4dd9fdf8 SC |
944 | wxWindow* wxpeer = GetWXPeer(); |
945 | if ( wxpeer == NULL ) | |
946 | return NSDragOperationNone; | |
03647350 | 947 | |
4dd9fdf8 SC |
948 | wxDropTarget* target = wxpeer->GetDropTarget(); |
949 | if ( target == NULL ) | |
950 | return NSDragOperationNone; | |
951 | ||
952 | wxDragResult result = wxDragNone; | |
e6b3143a SC |
953 | NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil]; |
954 | wxPoint pt = wxFromNSPoint( m_osxView, nspoint ); | |
4dd9fdf8 SC |
955 | |
956 | if ( sourceDragMask & NSDragOperationLink ) | |
957 | result = wxDragLink; | |
958 | else if ( sourceDragMask & NSDragOperationCopy ) | |
959 | result = wxDragCopy; | |
960 | else if ( sourceDragMask & NSDragOperationMove ) | |
961 | result = wxDragMove; | |
808cbd17 | 962 | |
03647350 | 963 | PasteboardRef pboardRef; |
4dd9fdf8 SC |
964 | PasteboardCreate((CFStringRef)[pboard name], &pboardRef); |
965 | target->SetCurrentDragPasteboard(pboardRef); | |
966 | result = target->OnDragOver(pt.x, pt.y, result); | |
967 | CFRelease(pboardRef); | |
03647350 | 968 | |
4dd9fdf8 SC |
969 | NSDragOperation nsresult = NSDragOperationNone; |
970 | switch (result ) | |
971 | { | |
972 | case wxDragLink: | |
973 | nsresult = NSDragOperationLink; | |
974 | case wxDragMove: | |
975 | nsresult = NSDragOperationMove; | |
976 | case wxDragCopy: | |
977 | nsresult = NSDragOperationCopy; | |
978 | default : | |
979 | break; | |
980 | } | |
981 | return nsresult; | |
982 | } | |
983 | ||
d8207702 | 984 | bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd)) |
4dd9fdf8 SC |
985 | { |
986 | id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s; | |
dbeddfb9 | 987 | |
4dd9fdf8 SC |
988 | NSPasteboard *pboard = [sender draggingPasteboard]; |
989 | NSDragOperation sourceDragMask = [sender draggingSourceOperationMask]; | |
03647350 | 990 | |
4dd9fdf8 SC |
991 | wxWindow* wxpeer = GetWXPeer(); |
992 | wxDropTarget* target = wxpeer->GetDropTarget(); | |
993 | wxDragResult result = wxDragNone; | |
e6b3143a SC |
994 | NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil]; |
995 | wxPoint pt = wxFromNSPoint( m_osxView, nspoint ); | |
4dd9fdf8 SC |
996 | |
997 | if ( sourceDragMask & NSDragOperationLink ) | |
998 | result = wxDragLink; | |
999 | else if ( sourceDragMask & NSDragOperationCopy ) | |
1000 | result = wxDragCopy; | |
1001 | else if ( sourceDragMask & NSDragOperationMove ) | |
1002 | result = wxDragMove; | |
1003 | ||
03647350 | 1004 | PasteboardRef pboardRef; |
4dd9fdf8 SC |
1005 | PasteboardCreate((CFStringRef)[pboard name], &pboardRef); |
1006 | target->SetCurrentDragPasteboard(pboardRef); | |
5646fba6 SC |
1007 | |
1008 | if (target->OnDrop(pt.x, pt.y)) | |
1009 | result = target->OnData(pt.x, pt.y, result); | |
1010 | ||
4dd9fdf8 | 1011 | CFRelease(pboardRef); |
03647350 | 1012 | |
4dd9fdf8 SC |
1013 | return result != wxDragNone; |
1014 | } | |
1015 | ||
63a6419c | 1016 | typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event); |
4dd9fdf8 SC |
1017 | typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event); |
1018 | typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event); | |
1019 | typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd); | |
1020 | typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd); | |
4dd9fdf8 SC |
1021 | |
1022 | void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd) | |
1023 | { | |
1024 | if ( !DoHandleMouseEvent(event) ) | |
1025 | { | |
2e498654 | 1026 | // for plain NSView mouse events would propagate to parents otherwise |
d15694e8 | 1027 | if (!IsUserPane()) |
2e498654 SC |
1028 | { |
1029 | wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; | |
1030 | superimpl(slf, (SEL)_cmd, event); | |
1031 | } | |
4dd9fdf8 SC |
1032 | } |
1033 | } | |
1034 | ||
1035 | void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd) | |
1036 | { | |
715824d5 | 1037 | if ( [event type] == NSKeyDown ) |
8c0e92cd SC |
1038 | { |
1039 | // there are key equivalents that are not command-combos and therefore not handled by cocoa automatically, | |
1040 | // therefore we call the menubar directly here, exit if the menu is handling the shortcut | |
1041 | if ( [[[NSApplication sharedApplication] mainMenu] performKeyEquivalent:event] ) | |
1042 | return; | |
1043 | ||
715824d5 | 1044 | m_lastKeyDownEvent = event; |
8c0e92cd SC |
1045 | } |
1046 | ||
7cb2a241 | 1047 | if ( GetFocusedViewInWindow([slf window]) != slf || m_hasEditor || !DoHandleKeyEvent(event) ) |
4dd9fdf8 SC |
1048 | { |
1049 | wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; | |
1050 | superimpl(slf, (SEL)_cmd, event); | |
1051 | } | |
715824d5 | 1052 | m_lastKeyDownEvent = NULL; |
4dd9fdf8 SC |
1053 | } |
1054 | ||
f0e0116e | 1055 | void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd) |
4dd9fdf8 | 1056 | { |
7cb2a241 | 1057 | if ( m_lastKeyDownEvent==NULL || m_hasEditor || !DoHandleCharEvent(m_lastKeyDownEvent, text) ) |
4dd9fdf8 | 1058 | { |
2e498654 SC |
1059 | wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; |
1060 | superimpl(slf, (SEL)_cmd, text); | |
4dd9fdf8 | 1061 | } |
f0e0116e | 1062 | } |
4dd9fdf8 | 1063 | |
f0e0116e KO |
1064 | |
1065 | bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd) | |
1066 | { | |
8c0e92cd SC |
1067 | bool handled = false; |
1068 | ||
1069 | wxKeyEvent wxevent(wxEVT_KEY_DOWN); | |
1070 | SetupKeyEvent( wxevent, event ); | |
1071 | ||
1072 | // because performKeyEquivalent is going up the entire view hierarchy, we don't have to | |
1073 | // walk up the ancestors ourselves but let cocoa do it | |
1074 | ||
1075 | int command = m_wxPeer->GetAcceleratorTable()->GetCommand( wxevent ); | |
1076 | if (command != -1) | |
1077 | { | |
1078 | wxEvtHandler * const handler = m_wxPeer->GetEventHandler(); | |
1079 | ||
1080 | wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command ); | |
1081 | handled = handler->ProcessEvent( command_event ); | |
1082 | ||
1083 | if ( !handled ) | |
1084 | { | |
1085 | // accelerators can also be used with buttons, try them too | |
1086 | command_event.SetEventType(wxEVT_COMMAND_BUTTON_CLICKED); | |
1087 | handled = handler->ProcessEvent( command_event ); | |
1088 | } | |
1089 | } | |
1090 | ||
1091 | if ( !handled ) | |
671b125b SC |
1092 | { |
1093 | wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; | |
1094 | return superimpl(slf, (SEL)_cmd, event); | |
1095 | } | |
1096 | return YES; | |
4dd9fdf8 SC |
1097 | } |
1098 | ||
09a9eb20 KO |
1099 | bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd) |
1100 | { | |
d15694e8 | 1101 | if ( IsUserPane() ) |
f06e0fea SC |
1102 | return m_wxPeer->AcceptsFocus(); |
1103 | else | |
1104 | { | |
1105 | wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; | |
1106 | return superimpl(slf, (SEL)_cmd); | |
1107 | } | |
09a9eb20 KO |
1108 | } |
1109 | ||
4dd9fdf8 SC |
1110 | bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd) |
1111 | { | |
1112 | wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; | |
b42865ce | 1113 | // get the current focus before running becomeFirstResponder |
03647350 | 1114 | NSView* otherView = FindFocus(); |
7cb2a241 | 1115 | |
b42865ce | 1116 | wxWidgetImpl* otherWindow = FindFromWXWidget(otherView); |
4dd9fdf8 SC |
1117 | BOOL r = superimpl(slf, (SEL)_cmd); |
1118 | if ( r ) | |
f06e0fea | 1119 | { |
b42865ce | 1120 | DoNotifyFocusEvent( true, otherWindow ); |
f06e0fea | 1121 | } |
7cb2a241 | 1122 | |
4dd9fdf8 SC |
1123 | return r; |
1124 | } | |
1125 | ||
1126 | bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd) | |
1127 | { | |
1128 | wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; | |
1129 | BOOL r = superimpl(slf, (SEL)_cmd); | |
b42865ce | 1130 | // get the current focus after running resignFirstResponder |
7cb2a241 SC |
1131 | // note that this value isn't reliable, it might return the same view that |
1132 | // is resigning | |
03647350 | 1133 | NSView* otherView = FindFocus(); |
b42865ce | 1134 | wxWidgetImpl* otherWindow = FindFromWXWidget(otherView); |
45d1c40a VZ |
1135 | |
1136 | // It doesn't make sense to notify about the loss of focus if we're not | |
1137 | // really losing it and the window which has just gained focus is the same | |
1138 | // one as this window itself. Of course, this should never happen in the | |
1139 | // first place but somehow it does in wxGrid code and without this check we | |
1140 | // enter into an infinite recursion, see #12267. | |
1141 | if ( otherWindow == this ) | |
1142 | return r; | |
1143 | ||
f06e0fea | 1144 | // NSTextViews have an editor as true responder, therefore the might get the |
7cb2a241 SC |
1145 | // resign notification if their editor takes over, don't trigger any event then |
1146 | if ( r && !m_hasEditor) | |
f06e0fea | 1147 | { |
b42865ce | 1148 | DoNotifyFocusEvent( false, otherWindow ); |
f06e0fea | 1149 | } |
4dd9fdf8 SC |
1150 | return r; |
1151 | } | |
1152 | ||
1153 | void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd) | |
1154 | { | |
1155 | wxWindow* wxpeer = GetWXPeer(); | |
1156 | if ( wxpeer ) | |
1157 | { | |
1158 | NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR(); | |
1159 | if (cursor == NULL) | |
dbeddfb9 | 1160 | { |
4dd9fdf8 SC |
1161 | wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; |
1162 | superimpl(slf, (SEL)_cmd); | |
dbeddfb9 | 1163 | } |
4dd9fdf8 | 1164 | else |
57c0a8ac | 1165 | { |
4dd9fdf8 SC |
1166 | [slf addCursorRect: [slf bounds] |
1167 | cursor: cursor]; | |
57c0a8ac | 1168 | } |
4dd9fdf8 SC |
1169 | } |
1170 | } | |
03647350 | 1171 | |
d8207702 | 1172 | bool wxWidgetCocoaImpl::isFlipped(WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd)) |
4dd9fdf8 SC |
1173 | { |
1174 | return m_isFlipped; | |
1175 | } | |
33e90275 | 1176 | |
4dd9fdf8 SC |
1177 | |
1178 | #define OSX_DEBUG_DRAWING 0 | |
1179 | ||
d8207702 | 1180 | void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *WXUNUSED(_cmd)) |
4dd9fdf8 | 1181 | { |
396f0e3d SC |
1182 | // preparing the update region |
1183 | ||
1184 | wxRegion updateRgn; | |
1185 | const NSRect *rects; | |
1186 | NSInteger count; | |
1187 | ||
1188 | [slf getRectsBeingDrawn:&rects count:&count]; | |
1189 | for ( int i = 0 ; i < count ; ++i ) | |
1190 | { | |
1191 | updateRgn.Union(wxFromNSRect(slf, rects[i])); | |
1192 | } | |
1193 | ||
1194 | wxWindow* wxpeer = GetWXPeer(); | |
1195 | ||
1196 | if ( wxpeer->MacGetLeftBorderSize() != 0 || wxpeer->MacGetTopBorderSize() != 0 ) | |
1197 | { | |
1198 | // as this update region is in native window locals we must adapt it to wx window local | |
1199 | updateRgn.Offset( wxpeer->MacGetLeftBorderSize() , wxpeer->MacGetTopBorderSize() ); | |
1200 | } | |
1201 | ||
1202 | if ( wxpeer->MacGetTopLevelWindow()->GetWindowStyle() & wxFRAME_SHAPED ) | |
1203 | { | |
1204 | int xoffset = 0, yoffset = 0; | |
1205 | wxRegion rgn = wxpeer->MacGetTopLevelWindow()->GetShape(); | |
1206 | wxpeer->MacRootWindowToWindow( &xoffset, &yoffset ); | |
1207 | rgn.Offset( xoffset, yoffset ); | |
1208 | updateRgn.Intersect(rgn); | |
1209 | } | |
1210 | ||
1211 | wxpeer->GetUpdateRegion() = updateRgn; | |
1212 | ||
1213 | // setting up the drawing context | |
1214 | ||
4dd9fdf8 SC |
1215 | CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort]; |
1216 | CGContextSaveGState( context ); | |
396f0e3d | 1217 | |
4dd9fdf8 SC |
1218 | #if OSX_DEBUG_DRAWING |
1219 | CGContextBeginPath( context ); | |
1220 | CGContextMoveToPoint(context, 0, 0); | |
2a2064f8 | 1221 | NSRect bounds = [slf bounds]; |
4dd9fdf8 SC |
1222 | CGContextAddLineToPoint(context, 10, 0); |
1223 | CGContextMoveToPoint(context, 0, 0); | |
1224 | CGContextAddLineToPoint(context, 0, 10); | |
1225 | CGContextMoveToPoint(context, bounds.size.width, bounds.size.height); | |
1226 | CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10); | |
1227 | CGContextMoveToPoint(context, bounds.size.width, bounds.size.height); | |
1228 | CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height); | |
1229 | CGContextClosePath( context ); | |
1230 | CGContextStrokePath(context); | |
1231 | #endif | |
396f0e3d | 1232 | |
4dd9fdf8 SC |
1233 | if ( !m_isFlipped ) |
1234 | { | |
1235 | CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height ); | |
1236 | CGContextScaleCTM( context, 1, -1 ); | |
1237 | } | |
b61b8371 | 1238 | |
4dd9fdf8 | 1239 | wxpeer->MacSetCGContextRef( context ); |
03647350 | 1240 | |
09489833 | 1241 | bool handled = wxpeer->MacDoRedraw( 0 ); |
4dd9fdf8 | 1242 | CGContextRestoreGState( context ); |
09489833 SC |
1243 | |
1244 | CGContextSaveGState( context ); | |
4dd9fdf8 SC |
1245 | if ( !handled ) |
1246 | { | |
1247 | // call super | |
1248 | SEL _cmd = @selector(drawRect:); | |
1249 | wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd]; | |
1250 | superimpl(slf, _cmd, *(NSRect*)rect); | |
09489833 SC |
1251 | CGContextRestoreGState( context ); |
1252 | CGContextSaveGState( context ); | |
33e90275 | 1253 | } |
7a69cf79 SC |
1254 | // as we called restore above, we have to flip again if necessary |
1255 | if ( !m_isFlipped ) | |
1256 | { | |
1257 | CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height ); | |
1258 | CGContextScaleCTM( context, 1, -1 ); | |
1259 | } | |
09489833 | 1260 | wxpeer->MacPaintChildrenBorders(); |
15fc716c | 1261 | wxpeer->MacSetCGContextRef( NULL ); |
09489833 | 1262 | CGContextRestoreGState( context ); |
33e90275 SC |
1263 | } |
1264 | ||
d8207702 | 1265 | void wxWidgetCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender)) |
4dd9fdf8 SC |
1266 | { |
1267 | wxWindow* wxpeer = (wxWindow*) GetWXPeer(); | |
1268 | if ( wxpeer ) | |
1269 | wxpeer->OSXHandleClicked(0); | |
1270 | } | |
33e90275 | 1271 | |
d8207702 | 1272 | void wxWidgetCocoaImpl::controlDoubleAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender)) |
524c47aa | 1273 | { |
524c47aa | 1274 | } |
33e90275 | 1275 | |
75a2c6a1 KO |
1276 | void wxWidgetCocoaImpl::controlTextDidChange() |
1277 | { | |
1278 | wxWindow* wxpeer = (wxWindow*)GetWXPeer(); | |
1279 | if ( wxpeer ) | |
1280 | { | |
4275201b SC |
1281 | // since native rtti doesn't have to be enabled and wx' rtti is not aware of the mixin wxTextEntry, workaround is needed |
1282 | wxTextCtrl *tc = wxDynamicCast( wxpeer , wxTextCtrl ); | |
1283 | wxComboBox *cb = wxDynamicCast( wxpeer , wxComboBox ); | |
1284 | if ( tc ) | |
1285 | tc->SendTextUpdatedEventIfAllowed(); | |
1286 | else if ( cb ) | |
1287 | cb->SendTextUpdatedEventIfAllowed(); | |
1288 | else | |
1289 | { | |
1290 | wxFAIL_MSG("Unexpected class for controlTextDidChange event"); | |
1291 | } | |
75a2c6a1 KO |
1292 | } |
1293 | } | |
1294 | ||
03647350 | 1295 | // |
4dd9fdf8 SC |
1296 | |
1297 | #if OBJC_API_VERSION >= 2 | |
1298 | ||
1299 | #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \ | |
1300 | class_addMethod(c, s, i, t ); | |
1301 | ||
1302 | #else | |
1303 | ||
1304 | #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \ | |
68ebe5a0 | 1305 | { s, (char*) t, i }, |
4dd9fdf8 SC |
1306 | |
1307 | #endif | |
1308 | ||
1309 | void wxOSXCocoaClassAddWXMethods(Class c) | |
1310 | { | |
1311 | ||
1312 | #if OBJC_API_VERSION < 2 | |
1313 | static objc_method wxmethods[] = | |
1314 | { | |
1315 | #endif | |
1316 | ||
1317 | wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
1318 | wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
1319 | wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
1320 | ||
1321 | wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
1322 | wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
1323 | wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
1324 | ||
1325 | wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
1326 | ||
1327 | wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
1328 | wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
1329 | wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
5a900d50 KO |
1330 | |
1331 | wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstMouse:), (IMP) wxOSX_acceptsFirstMouse, "v@:@" ) | |
4dd9fdf8 SC |
1332 | |
1333 | wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
1334 | wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
1335 | wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" ) | |
03647350 | 1336 | |
4dd9fdf8 SC |
1337 | wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" ) |
1338 | wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" ) | |
1339 | wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" ) | |
03647350 | 1340 | |
f0e0116e | 1341 | wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" ) |
4dd9fdf8 | 1342 | |
7cb2a241 | 1343 | wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "c@:@" ) |
4dd9fdf8 | 1344 | |
09a9eb20 | 1345 | wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstResponder), (IMP) wxOSX_acceptsFirstResponder, "c@:" ) |
4dd9fdf8 SC |
1346 | wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" ) |
1347 | wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" ) | |
1348 | wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" ) | |
1349 | ||
1350 | wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" ) | |
1351 | wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" ) | |
1352 | ||
e32090ba SC |
1353 | wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" ) |
1354 | wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" ) | |
4dd9fdf8 SC |
1355 | |
1356 | #if wxUSE_DRAG_AND_DROP | |
1357 | wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" ) | |
1358 | wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" ) | |
1359 | wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" ) | |
1360 | wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" ) | |
03647350 VZ |
1361 | #endif |
1362 | ||
4dd9fdf8 SC |
1363 | #if OBJC_API_VERSION < 2 |
1364 | } ; | |
1365 | static int method_count = WXSIZEOF( wxmethods ); | |
1366 | static objc_method_list *wxmethodlist = NULL; | |
1367 | if ( wxmethodlist == NULL ) | |
1368 | { | |
1369 | wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) ); | |
1370 | memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) ); | |
1371 | wxmethodlist->method_count = method_count; | |
1372 | wxmethodlist->obsolete = 0; | |
1373 | } | |
1374 | class_addMethods( c, wxmethodlist ); | |
1375 | #endif | |
1376 | } | |
1377 | ||
1378 | // | |
1379 | // C++ implementation class | |
1380 | // | |
33e90275 SC |
1381 | |
1382 | IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl ) | |
1383 | ||
415f4a01 SC |
1384 | wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl, bool isUserPane ) : |
1385 | wxWidgetImpl( peer, isRootControl, isUserPane ) | |
33e90275 | 1386 | { |
4dd9fdf8 SC |
1387 | Init(); |
1388 | m_osxView = w; | |
d94e9b62 VZ |
1389 | |
1390 | // check if the user wants to create the control initially hidden | |
1391 | if ( !peer->IsShown() ) | |
1392 | SetVisibility(false); | |
1393 | ||
9a038ddc SC |
1394 | // gc aware handling |
1395 | if ( m_osxView ) | |
1396 | CFRetain(m_osxView); | |
1397 | [m_osxView release]; | |
33e90275 SC |
1398 | } |
1399 | ||
03647350 | 1400 | wxWidgetCocoaImpl::wxWidgetCocoaImpl() |
33e90275 | 1401 | { |
4dd9fdf8 | 1402 | Init(); |
33e90275 SC |
1403 | } |
1404 | ||
1405 | void wxWidgetCocoaImpl::Init() | |
1406 | { | |
1407 | m_osxView = NULL; | |
4dd9fdf8 | 1408 | m_isFlipped = true; |
f0e0116e | 1409 | m_lastKeyDownEvent = NULL; |
7cb2a241 | 1410 | m_hasEditor = false; |
33e90275 SC |
1411 | } |
1412 | ||
1413 | wxWidgetCocoaImpl::~wxWidgetCocoaImpl() | |
1414 | { | |
4dd9fdf8 SC |
1415 | RemoveAssociations( this ); |
1416 | ||
dbeddfb9 SC |
1417 | if ( !IsRootControl() ) |
1418 | { | |
1419 | NSView *sv = [m_osxView superview]; | |
1420 | if ( sv != nil ) | |
1421 | [m_osxView removeFromSuperview]; | |
1422 | } | |
9a038ddc SC |
1423 | // gc aware handling |
1424 | if ( m_osxView ) | |
1425 | CFRelease(m_osxView); | |
33e90275 | 1426 | } |
03647350 VZ |
1427 | |
1428 | bool wxWidgetCocoaImpl::IsVisible() const | |
33e90275 SC |
1429 | { |
1430 | return [m_osxView isHiddenOrHasHiddenAncestor] == NO; | |
1431 | } | |
1432 | ||
dbeddfb9 SC |
1433 | void wxWidgetCocoaImpl::SetVisibility( bool visible ) |
1434 | { | |
1435 | [m_osxView setHidden:(visible ? NO:YES)]; | |
1436 | } | |
1437 | ||
ab9a0b84 VZ |
1438 | // ---------------------------------------------------------------------------- |
1439 | // window animation stuff | |
1440 | // ---------------------------------------------------------------------------- | |
1441 | ||
f2be0678 | 1442 | // define a delegate used to refresh the window during animation |
030495ec | 1443 | @interface wxNSAnimationDelegate : NSObject wxOSX_10_6_AND_LATER(<NSAnimationDelegate>) |
ab9a0b84 | 1444 | { |
ab9a0b84 | 1445 | wxWindow *m_win; |
f2be0678 | 1446 | bool m_isDone; |
ab9a0b84 VZ |
1447 | } |
1448 | ||
f2be0678 VZ |
1449 | - (id)init:(wxWindow *)win; |
1450 | ||
1451 | - (bool)isDone; | |
ab9a0b84 VZ |
1452 | |
1453 | // NSAnimationDelegate methods | |
f2be0678 | 1454 | - (void)animationDidEnd:(NSAnimation*)animation; |
ab9a0b84 VZ |
1455 | - (void)animation:(NSAnimation*)animation |
1456 | didReachProgressMark:(NSAnimationProgress)progress; | |
ab9a0b84 VZ |
1457 | @end |
1458 | ||
1459 | @implementation wxNSAnimationDelegate | |
1460 | ||
f2be0678 | 1461 | - (id)init:(wxWindow *)win |
ab9a0b84 | 1462 | { |
f599a415 | 1463 | self = [super init]; |
ab9a0b84 VZ |
1464 | |
1465 | m_win = win; | |
f2be0678 VZ |
1466 | m_isDone = false; |
1467 | ||
ab9a0b84 VZ |
1468 | return self; |
1469 | } | |
1470 | ||
f2be0678 VZ |
1471 | - (bool)isDone |
1472 | { | |
1473 | return m_isDone; | |
1474 | } | |
1475 | ||
ab9a0b84 VZ |
1476 | - (void)animation:(NSAnimation*)animation |
1477 | didReachProgressMark:(NSAnimationProgress)progress | |
1478 | { | |
1479 | wxUnusedVar(animation); | |
1480 | wxUnusedVar(progress); | |
1481 | ||
1482 | m_win->SendSizeEvent(); | |
1483 | } | |
1484 | ||
f2be0678 | 1485 | - (void)animationDidEnd:(NSAnimation*)animation |
ab9a0b84 | 1486 | { |
849252d5 | 1487 | wxUnusedVar(animation); |
f2be0678 | 1488 | m_isDone = true; |
ab9a0b84 VZ |
1489 | } |
1490 | ||
1491 | @end | |
1492 | ||
1493 | /* static */ | |
1494 | bool | |
1495 | wxWidgetCocoaImpl::ShowViewOrWindowWithEffect(wxWindow *win, | |
1496 | bool show, | |
1497 | wxShowEffect effect, | |
1498 | unsigned timeout) | |
1499 | { | |
ab9a0b84 VZ |
1500 | // create the dictionary describing the animation to perform on this view |
1501 | NSObject * const | |
1502 | viewOrWin = static_cast<NSObject *>(win->OSXGetViewOrWindow()); | |
1503 | NSMutableDictionary * const | |
1504 | dict = [NSMutableDictionary dictionaryWithCapacity:4]; | |
1505 | [dict setObject:viewOrWin forKey:NSViewAnimationTargetKey]; | |
1506 | ||
1507 | // determine the start and end rectangles assuming we're hiding the window | |
f2be0678 | 1508 | const wxRect rectOrig = win->GetRect(); |
ab9a0b84 VZ |
1509 | wxRect rectStart, |
1510 | rectEnd; | |
1511 | rectStart = | |
f2be0678 | 1512 | rectEnd = rectOrig; |
ab9a0b84 VZ |
1513 | |
1514 | if ( show ) | |
1515 | { | |
1516 | if ( effect == wxSHOW_EFFECT_ROLL_TO_LEFT || | |
1517 | effect == wxSHOW_EFFECT_SLIDE_TO_LEFT ) | |
1518 | effect = wxSHOW_EFFECT_ROLL_TO_RIGHT; | |
1519 | else if ( effect == wxSHOW_EFFECT_ROLL_TO_RIGHT || | |
1520 | effect == wxSHOW_EFFECT_SLIDE_TO_RIGHT ) | |
1521 | effect = wxSHOW_EFFECT_ROLL_TO_LEFT; | |
1522 | else if ( effect == wxSHOW_EFFECT_ROLL_TO_TOP || | |
1523 | effect == wxSHOW_EFFECT_SLIDE_TO_TOP ) | |
1524 | effect = wxSHOW_EFFECT_ROLL_TO_BOTTOM; | |
1525 | else if ( effect == wxSHOW_EFFECT_ROLL_TO_BOTTOM || | |
1526 | effect == wxSHOW_EFFECT_SLIDE_TO_BOTTOM ) | |
1527 | effect = wxSHOW_EFFECT_ROLL_TO_TOP; | |
1528 | } | |
1529 | ||
1530 | switch ( effect ) | |
1531 | { | |
1532 | case wxSHOW_EFFECT_ROLL_TO_LEFT: | |
1533 | case wxSHOW_EFFECT_SLIDE_TO_LEFT: | |
1534 | rectEnd.width = 0; | |
1535 | break; | |
1536 | ||
1537 | case wxSHOW_EFFECT_ROLL_TO_RIGHT: | |
1538 | case wxSHOW_EFFECT_SLIDE_TO_RIGHT: | |
1539 | rectEnd.x = rectStart.GetRight(); | |
1540 | rectEnd.width = 0; | |
1541 | break; | |
1542 | ||
1543 | case wxSHOW_EFFECT_ROLL_TO_TOP: | |
1544 | case wxSHOW_EFFECT_SLIDE_TO_TOP: | |
1545 | rectEnd.height = 0; | |
1546 | break; | |
1547 | ||
1548 | case wxSHOW_EFFECT_ROLL_TO_BOTTOM: | |
1549 | case wxSHOW_EFFECT_SLIDE_TO_BOTTOM: | |
1550 | rectEnd.y = rectStart.GetBottom(); | |
1551 | rectEnd.height = 0; | |
1552 | break; | |
1553 | ||
1554 | case wxSHOW_EFFECT_EXPAND: | |
1555 | rectEnd.x = rectStart.x + rectStart.width / 2; | |
1556 | rectEnd.y = rectStart.y + rectStart.height / 2; | |
1557 | rectEnd.width = | |
1558 | rectEnd.height = 0; | |
1559 | break; | |
1560 | ||
1561 | case wxSHOW_EFFECT_BLEND: | |
1562 | [dict setObject:(show ? NSViewAnimationFadeInEffect | |
1563 | : NSViewAnimationFadeOutEffect) | |
1564 | forKey:NSViewAnimationEffectKey]; | |
1565 | break; | |
1566 | ||
1567 | case wxSHOW_EFFECT_NONE: | |
1568 | case wxSHOW_EFFECT_MAX: | |
1569 | wxFAIL_MSG( "unexpected animation effect" ); | |
1570 | return false; | |
1571 | ||
1572 | default: | |
1573 | wxFAIL_MSG( "unknown animation effect" ); | |
1574 | return false; | |
1575 | }; | |
1576 | ||
1577 | if ( show ) | |
1578 | { | |
1579 | // we need to restore it to the original rectangle instead of making it | |
1580 | // disappear | |
1581 | wxSwap(rectStart, rectEnd); | |
1582 | ||
1583 | // and as the window is currently hidden, we need to show it for the | |
1584 | // animation to be visible at all (but don't restore it at its full | |
1585 | // rectangle as it shouldn't appear immediately) | |
1586 | win->SetSize(rectStart); | |
f2be0678 | 1587 | win->Show(); |
ab9a0b84 VZ |
1588 | } |
1589 | ||
1590 | NSView * const parentView = [viewOrWin isKindOfClass:[NSView class]] | |
1591 | ? [(NSView *)viewOrWin superview] | |
1592 | : nil; | |
1593 | const NSRect rStart = wxToNSRect(parentView, rectStart); | |
1594 | const NSRect rEnd = wxToNSRect(parentView, rectEnd); | |
1595 | ||
1596 | [dict setObject:[NSValue valueWithRect:rStart] | |
1597 | forKey:NSViewAnimationStartFrameKey]; | |
1598 | [dict setObject:[NSValue valueWithRect:rEnd] | |
1599 | forKey:NSViewAnimationEndFrameKey]; | |
1600 | ||
1601 | // create an animation using the values in the above dictionary | |
ab9a0b84 VZ |
1602 | NSViewAnimation * const |
1603 | anim = [[NSViewAnimation alloc] | |
1604 | initWithViewAnimations:[NSArray arrayWithObject:dict]]; | |
ab9a0b84 VZ |
1605 | |
1606 | if ( !timeout ) | |
1607 | { | |
1608 | // what is a good default duration? Windows uses 200ms, Web frameworks | |
1609 | // use anything from 250ms to 1s... choose something in the middle | |
1610 | timeout = 500; | |
1611 | } | |
1612 | ||
1613 | [anim setDuration:timeout/1000.]; // duration is in seconds here | |
1614 | ||
1615 | // if the window being animated changes its layout depending on its size | |
1616 | // (which is almost always the case) we need to redo it during animation | |
1617 | // | |
1618 | // the number of layouts here is arbitrary, but 10 seems like too few (e.g. | |
1619 | // controls in wxInfoBar visibly jump around) | |
1620 | const int NUM_LAYOUTS = 20; | |
1621 | for ( float f = 1./NUM_LAYOUTS; f < 1.; f += 1./NUM_LAYOUTS ) | |
1622 | [anim addProgressMark:f]; | |
1623 | ||
f2be0678 VZ |
1624 | wxNSAnimationDelegate * const |
1625 | animDelegate = [[wxNSAnimationDelegate alloc] init:win]; | |
1626 | [anim setDelegate:animDelegate]; | |
ab9a0b84 VZ |
1627 | [anim startAnimation]; |
1628 | ||
f2be0678 VZ |
1629 | // Cocoa is capable of doing animation asynchronously or even from separate |
1630 | // thread but wx API doesn't provide any way to be notified about the | |
1631 | // animation end and without this we really must ensure that the window has | |
1632 | // the expected (i.e. the same as if a simple Show() had been used) size | |
1633 | // when we return, so block here until the animation finishes | |
1634 | // | |
1635 | // notice that because the default animation mode is NSAnimationBlocking, | |
1636 | // no user input events ought to be processed from here | |
dc72e2ad VZ |
1637 | { |
1638 | wxEventLoopGuarantor ensureEventLoopExistence; | |
1639 | wxEventLoopBase * const loop = wxEventLoopBase::GetActive(); | |
1640 | while ( ![animDelegate isDone] ) | |
1641 | loop->Dispatch(); | |
1642 | } | |
f2be0678 VZ |
1643 | |
1644 | if ( !show ) | |
1645 | { | |
1646 | // NSViewAnimation is smart enough to hide the NSView being animated at | |
1647 | // the end but we also must ensure that it's hidden for wx too | |
1648 | win->Hide(); | |
1649 | ||
1650 | // and we must also restore its size because it isn't expected to | |
1651 | // change just because the window was hidden | |
1652 | win->SetSize(rectOrig); | |
1653 | } | |
1654 | else | |
1655 | { | |
1656 | // refresh it once again after the end to ensure that everything is in | |
1657 | // place | |
1658 | win->SendSizeEvent(); | |
1659 | } | |
1660 | ||
1661 | [anim setDelegate:nil]; | |
1662 | [animDelegate release]; | |
1663 | [anim release]; | |
1664 | ||
ab9a0b84 VZ |
1665 | return true; |
1666 | } | |
1667 | ||
1668 | bool wxWidgetCocoaImpl::ShowWithEffect(bool show, | |
1669 | wxShowEffect effect, | |
1670 | unsigned timeout) | |
1671 | { | |
1672 | return ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout); | |
1673 | } | |
1674 | ||
33e90275 SC |
1675 | void wxWidgetCocoaImpl::Raise() |
1676 | { | |
11fed901 | 1677 | // Not implemented |
33e90275 | 1678 | } |
03647350 | 1679 | |
33e90275 SC |
1680 | void wxWidgetCocoaImpl::Lower() |
1681 | { | |
11fed901 | 1682 | // Not implemented |
33e90275 SC |
1683 | } |
1684 | ||
d8207702 | 1685 | void wxWidgetCocoaImpl::ScrollRect( const wxRect *WXUNUSED(rect), int WXUNUSED(dx), int WXUNUSED(dy) ) |
33e90275 | 1686 | { |
54f11060 SC |
1687 | #if 1 |
1688 | SetNeedsDisplay() ; | |
1689 | #else | |
1690 | // We should do something like this, but it wasn't working in 10.4. | |
1691 | if (GetNeedsDisplay() ) | |
1692 | { | |
1693 | SetNeedsDisplay() ; | |
1694 | } | |
1695 | NSRect r = wxToNSRect( [m_osxView superview], *rect ); | |
1696 | NSSize offset = NSMakeSize((float)dx, (float)dy); | |
1697 | [m_osxView scrollRect:r by:offset]; | |
1698 | #endif | |
33e90275 SC |
1699 | } |
1700 | ||
1701 | void wxWidgetCocoaImpl::Move(int x, int y, int width, int height) | |
1702 | { | |
0c530e5a | 1703 | wxWindowMac* parent = GetWXPeer()->GetParent(); |
03647350 | 1704 | // under Cocoa we might have a contentView in the wxParent to which we have to |
0c530e5a | 1705 | // adjust the coordinates |
ea468399 | 1706 | if (parent && [m_osxView superview] != parent->GetHandle() ) |
0c530e5a | 1707 | { |
57c0a8ac SC |
1708 | int cx = 0,cy = 0,cw = 0,ch = 0; |
1709 | if ( parent->GetPeer() ) | |
1710 | { | |
1711 | parent->GetPeer()->GetContentArea(cx, cy, cw, ch); | |
1712 | x -= cx; | |
1713 | y -= cy; | |
1714 | } | |
0c530e5a | 1715 | } |
95fb3153 | 1716 | [[m_osxView superview] setNeedsDisplayInRect:[m_osxView frame]]; |
33e90275 SC |
1717 | NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) ); |
1718 | [m_osxView setFrame:r]; | |
03647350 VZ |
1719 | [[m_osxView superview] setNeedsDisplayInRect:r]; |
1720 | ||
849754a4 KO |
1721 | wxNSView* wxview = (wxNSView*)m_osxView; |
1722 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 | |
1723 | if ([wxview respondsToSelector:@selector(updateTrackingArea)] ) | |
1724 | [wxview updateTrackingArea]; | |
1725 | #else | |
41af81ea | 1726 | if ([m_osxView respondsToSelector:@selector(trackingTag)] ) |
fc099495 | 1727 | { |
849754a4 KO |
1728 | if ( [wxview trackingTag] ) |
1729 | [wxview removeTrackingRect: [wxview trackingTag]]; | |
03647350 | 1730 | |
849754a4 | 1731 | [wxview setTrackingTag: [wxview addTrackingRect: [m_osxView bounds] owner: wxview userData: nil assumeInside: NO]]; |
fc099495 | 1732 | } |
849754a4 | 1733 | #endif |
33e90275 SC |
1734 | } |
1735 | ||
1736 | void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const | |
1737 | { | |
1738 | wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] ); | |
1739 | x = r.GetLeft(); | |
1740 | y = r.GetTop(); | |
7a69cf79 SC |
1741 | |
1742 | // under Cocoa we might have a contentView in the wxParent to which we have to | |
1743 | // adjust the coordinates | |
1744 | wxWindowMac* parent = GetWXPeer()->GetParent(); | |
1745 | if (parent && [m_osxView superview] != parent->GetHandle() ) | |
1746 | { | |
1747 | int cx = 0,cy = 0,cw = 0,ch = 0; | |
1748 | if ( parent->GetPeer() ) | |
1749 | { | |
1750 | parent->GetPeer()->GetContentArea(cx, cy, cw, ch); | |
1751 | x += cx; | |
1752 | y += cy; | |
1753 | } | |
1754 | } | |
33e90275 SC |
1755 | } |
1756 | ||
1757 | void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const | |
1758 | { | |
1759 | NSRect rect = [m_osxView frame]; | |
e490b0d2 VZ |
1760 | width = (int)rect.size.width; |
1761 | height = (int)rect.size.height; | |
33e90275 SC |
1762 | } |
1763 | ||
dbeddfb9 | 1764 | void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const |
33e90275 | 1765 | { |
0c530e5a SC |
1766 | if ( [m_osxView respondsToSelector:@selector(contentView) ] ) |
1767 | { | |
1768 | NSView* cv = [m_osxView contentView]; | |
03647350 | 1769 | |
0c530e5a SC |
1770 | NSRect bounds = [m_osxView bounds]; |
1771 | NSRect rect = [cv frame]; | |
03647350 | 1772 | |
e490b0d2 VZ |
1773 | int y = (int)rect.origin.y; |
1774 | int x = (int)rect.origin.x; | |
0c530e5a | 1775 | if ( ![ m_osxView isFlipped ] ) |
e490b0d2 | 1776 | y = (int)(bounds.size.height - (rect.origin.y + rect.size.height)); |
0c530e5a SC |
1777 | left = x; |
1778 | top = y; | |
e490b0d2 VZ |
1779 | width = (int)rect.size.width; |
1780 | height = (int)rect.size.height; | |
0c530e5a SC |
1781 | } |
1782 | else | |
1783 | { | |
1784 | left = top = 0; | |
1785 | GetSize( width, height ); | |
1786 | } | |
33e90275 SC |
1787 | } |
1788 | ||
1789 | void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where ) | |
1790 | { | |
1791 | if ( where ) | |
1792 | [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )]; | |
1793 | else | |
1794 | [m_osxView setNeedsDisplay:YES]; | |
1795 | } | |
1796 | ||
1797 | bool wxWidgetCocoaImpl::GetNeedsDisplay() const | |
1798 | { | |
1799 | return [m_osxView needsDisplay]; | |
1800 | } | |
1801 | ||
dbeddfb9 | 1802 | bool wxWidgetCocoaImpl::CanFocus() const |
33e90275 | 1803 | { |
dbeddfb9 | 1804 | return [m_osxView canBecomeKeyView] == YES; |
33e90275 SC |
1805 | } |
1806 | ||
1807 | bool wxWidgetCocoaImpl::HasFocus() const | |
1808 | { | |
f06e0fea | 1809 | return ( FindFocus() == m_osxView ); |
33e90275 SC |
1810 | } |
1811 | ||
03647350 | 1812 | bool wxWidgetCocoaImpl::SetFocus() |
33e90275 | 1813 | { |
78a17075 | 1814 | if ( !CanFocus() ) |
dbeddfb9 | 1815 | return false; |
03647350 | 1816 | |
47e29847 | 1817 | [[m_osxView window] makeKeyAndOrderFront:nil] ; |
c6d90011 | 1818 | [[m_osxView window] makeFirstResponder: m_osxView] ; |
dbeddfb9 | 1819 | return true; |
33e90275 SC |
1820 | } |
1821 | ||
1822 | ||
1823 | void wxWidgetCocoaImpl::RemoveFromParent() | |
1824 | { | |
1825 | [m_osxView removeFromSuperview]; | |
1826 | } | |
1827 | ||
1828 | void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent ) | |
1829 | { | |
1830 | NSView* container = parent->GetWXWidget() ; | |
1831 | wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ; | |
1832 | [container addSubview:m_osxView]; | |
1833 | } | |
1834 | ||
3b2527c7 | 1835 | void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &col ) |
dbeddfb9 | 1836 | { |
3b2527c7 SC |
1837 | NSView* targetView = m_osxView; |
1838 | if ( [m_osxView isKindOfClass:[NSScrollView class] ] ) | |
1839 | targetView = [(NSScrollView*) m_osxView documentView]; | |
1840 | ||
1841 | if ( [targetView respondsToSelector:@selector(setBackgroundColor:) ] ) | |
1842 | { | |
1843 | [targetView setBackgroundColor:[NSColor colorWithCalibratedRed:(CGFloat) (col.Red() / 255.0) | |
1844 | green:(CGFloat) (col.Green() / 255.0) | |
1845 | blue:(CGFloat) (col.Blue() / 255.0) | |
1846 | alpha:(CGFloat) (col.Alpha() / 255.0)]]; | |
1847 | } | |
dbeddfb9 SC |
1848 | } |
1849 | ||
bc5c09a3 SC |
1850 | bool wxWidgetCocoaImpl::SetBackgroundStyle( wxBackgroundStyle style ) |
1851 | { | |
1852 | BOOL opaque = ( style == wxBG_STYLE_PAINT ); | |
1853 | ||
1854 | if ( [m_osxView respondsToSelector:@selector(setOpaque:) ] ) | |
1855 | { | |
1856 | [m_osxView setOpaque: opaque]; | |
1857 | } | |
1858 | ||
1859 | return true ; | |
1860 | } | |
1861 | ||
dbeddfb9 SC |
1862 | void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding ) |
1863 | { | |
1864 | if ( [m_osxView respondsToSelector:@selector(setTitle:) ] ) | |
1865 | { | |
d8207702 | 1866 | wxCFStringRef cf( title , encoding ); |
dbeddfb9 SC |
1867 | [m_osxView setTitle:cf.AsNSString()]; |
1868 | } | |
e32090ba SC |
1869 | else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] ) |
1870 | { | |
d8207702 | 1871 | wxCFStringRef cf( title , encoding ); |
e32090ba SC |
1872 | [m_osxView setStringValue:cf.AsNSString()]; |
1873 | } | |
dbeddfb9 | 1874 | } |
03647350 | 1875 | |
dbeddfb9 SC |
1876 | |
1877 | void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to ) | |
1878 | { | |
1879 | NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt ); | |
03647350 | 1880 | p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ]; |
dbeddfb9 SC |
1881 | *pt = wxFromNSPoint( to->GetWXWidget(), p ); |
1882 | } | |
1883 | ||
03647350 | 1884 | wxInt32 wxWidgetCocoaImpl::GetValue() const |
dbeddfb9 SC |
1885 | { |
1886 | return [(NSControl*)m_osxView intValue]; | |
1887 | } | |
1888 | ||
03647350 | 1889 | void wxWidgetCocoaImpl::SetValue( wxInt32 v ) |
dbeddfb9 SC |
1890 | { |
1891 | if ( [m_osxView respondsToSelector:@selector(setIntValue:)] ) | |
1892 | { | |
1893 | [m_osxView setIntValue:v]; | |
1894 | } | |
1895 | else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] ) | |
1896 | { | |
1897 | [m_osxView setFloatValue:(double)v]; | |
1898 | } | |
1899 | else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] ) | |
1900 | { | |
1901 | [m_osxView setDoubleValue:(double)v]; | |
1902 | } | |
1903 | } | |
1904 | ||
03647350 | 1905 | void wxWidgetCocoaImpl::SetMinimum( wxInt32 v ) |
dbeddfb9 SC |
1906 | { |
1907 | if ( [m_osxView respondsToSelector:@selector(setMinValue:)] ) | |
1908 | { | |
1909 | [m_osxView setMinValue:(double)v]; | |
1910 | } | |
1911 | } | |
1912 | ||
03647350 | 1913 | void wxWidgetCocoaImpl::SetMaximum( wxInt32 v ) |
dbeddfb9 SC |
1914 | { |
1915 | if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] ) | |
1916 | { | |
1917 | [m_osxView setMaxValue:(double)v]; | |
1918 | } | |
1919 | } | |
1920 | ||
03647350 | 1921 | wxInt32 wxWidgetCocoaImpl::GetMinimum() const |
19c7ac3d | 1922 | { |
f0c6b963 | 1923 | if ( [m_osxView respondsToSelector:@selector(minValue)] ) |
19c7ac3d | 1924 | { |
e490b0d2 | 1925 | return (int)[m_osxView minValue]; |
19c7ac3d SC |
1926 | } |
1927 | return 0; | |
1928 | } | |
1929 | ||
03647350 | 1930 | wxInt32 wxWidgetCocoaImpl::GetMaximum() const |
19c7ac3d | 1931 | { |
f0c6b963 | 1932 | if ( [m_osxView respondsToSelector:@selector(maxValue)] ) |
19c7ac3d | 1933 | { |
e490b0d2 | 1934 | return (int)[m_osxView maxValue]; |
19c7ac3d SC |
1935 | } |
1936 | return 0; | |
1937 | } | |
1938 | ||
e5d05b90 VZ |
1939 | wxBitmap wxWidgetCocoaImpl::GetBitmap() const |
1940 | { | |
1941 | wxBitmap bmp; | |
1942 | ||
1943 | // TODO: how to create a wxBitmap from NSImage? | |
1944 | #if 0 | |
1945 | if ( [m_osxView respondsToSelector:@selector(image:)] ) | |
1946 | bmp = [m_osxView image]; | |
1947 | #endif | |
1948 | ||
1949 | return bmp; | |
1950 | } | |
1951 | ||
dbeddfb9 SC |
1952 | void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap ) |
1953 | { | |
1954 | if ( [m_osxView respondsToSelector:@selector(setImage:)] ) | |
1955 | { | |
1956 | [m_osxView setImage:bitmap.GetNSImage()]; | |
411a1c35 | 1957 | [m_osxView setNeedsDisplay:YES]; |
dbeddfb9 SC |
1958 | } |
1959 | } | |
1960 | ||
e5d05b90 VZ |
1961 | void wxWidgetCocoaImpl::SetBitmapPosition( wxDirection dir ) |
1962 | { | |
1963 | if ( [m_osxView respondsToSelector:@selector(setImagePosition:)] ) | |
1964 | { | |
1965 | NSCellImagePosition pos; | |
1966 | switch ( dir ) | |
1967 | { | |
1968 | case wxLEFT: | |
1969 | pos = NSImageLeft; | |
1970 | break; | |
1971 | ||
1972 | case wxRIGHT: | |
1973 | pos = NSImageRight; | |
1974 | break; | |
1975 | ||
1976 | case wxTOP: | |
1977 | pos = NSImageAbove; | |
1978 | break; | |
1979 | ||
1980 | case wxBOTTOM: | |
1981 | pos = NSImageBelow; | |
1982 | break; | |
1983 | ||
1984 | default: | |
1985 | wxFAIL_MSG( "invalid image position" ); | |
1986 | pos = NSNoImage; | |
1987 | } | |
1988 | ||
1989 | [m_osxView setImagePosition:pos]; | |
1990 | } | |
1991 | } | |
1992 | ||
d8207702 | 1993 | void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook)) |
dbeddfb9 SC |
1994 | { |
1995 | // implementation in subclass | |
1996 | } | |
1997 | ||
1998 | void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const | |
1999 | { | |
2000 | r->x = r->y = r->width = r->height = 0; | |
09a9eb20 | 2001 | |
dbeddfb9 SC |
2002 | if ( [m_osxView respondsToSelector:@selector(sizeToFit)] ) |
2003 | { | |
2004 | NSRect former = [m_osxView frame]; | |
2005 | [m_osxView sizeToFit]; | |
2006 | NSRect best = [m_osxView frame]; | |
2007 | [m_osxView setFrame:former]; | |
e490b0d2 VZ |
2008 | r->width = (int)best.size.width; |
2009 | r->height = (int)best.size.height; | |
dbeddfb9 SC |
2010 | } |
2011 | } | |
2012 | ||
2013 | bool wxWidgetCocoaImpl::IsEnabled() const | |
2014 | { | |
6ade9e89 SC |
2015 | NSView* targetView = m_osxView; |
2016 | if ( [m_osxView isKindOfClass:[NSScrollView class] ] ) | |
2017 | targetView = [(NSScrollView*) m_osxView documentView]; | |
2018 | ||
2019 | if ( [targetView respondsToSelector:@selector(isEnabled) ] ) | |
2020 | return [targetView isEnabled]; | |
0f9b48d1 | 2021 | return true; |
dbeddfb9 SC |
2022 | } |
2023 | ||
2024 | void wxWidgetCocoaImpl::Enable( bool enable ) | |
2025 | { | |
6ade9e89 SC |
2026 | NSView* targetView = m_osxView; |
2027 | if ( [m_osxView isKindOfClass:[NSScrollView class] ] ) | |
2028 | targetView = [(NSScrollView*) m_osxView documentView]; | |
2029 | ||
2030 | if ( [targetView respondsToSelector:@selector(setEnabled:) ] ) | |
2031 | [targetView setEnabled:enable]; | |
dbeddfb9 SC |
2032 | } |
2033 | ||
2034 | void wxWidgetCocoaImpl::PulseGauge() | |
2035 | { | |
2036 | } | |
2037 | ||
d8207702 | 2038 | void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 WXUNUSED(val), wxInt32 WXUNUSED(view) ) |
dbeddfb9 SC |
2039 | { |
2040 | } | |
33e90275 | 2041 | |
03647350 | 2042 | void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant ) |
0f9b48d1 SC |
2043 | { |
2044 | NSControlSize size = NSRegularControlSize; | |
03647350 | 2045 | |
0f9b48d1 SC |
2046 | switch ( variant ) |
2047 | { | |
2048 | case wxWINDOW_VARIANT_NORMAL : | |
2049 | size = NSRegularControlSize; | |
2050 | break ; | |
2051 | ||
2052 | case wxWINDOW_VARIANT_SMALL : | |
2053 | size = NSSmallControlSize; | |
2054 | break ; | |
2055 | ||
2056 | case wxWINDOW_VARIANT_MINI : | |
2057 | size = NSMiniControlSize; | |
2058 | break ; | |
2059 | ||
2060 | case wxWINDOW_VARIANT_LARGE : | |
2061 | size = NSRegularControlSize; | |
2062 | break ; | |
2063 | ||
2064 | default: | |
9a83f860 | 2065 | wxFAIL_MSG(wxT("unexpected window variant")); |
0f9b48d1 SC |
2066 | break ; |
2067 | } | |
2068 | if ( [m_osxView respondsToSelector:@selector(setControlSize:)] ) | |
2069 | [m_osxView setControlSize:size]; | |
03647350 VZ |
2070 | else if ([m_osxView respondsToSelector:@selector(cell)]) |
2071 | { | |
2072 | id cell = [(id)m_osxView cell]; | |
2073 | if ([cell respondsToSelector:@selector(setControlSize:)]) | |
2074 | [cell setControlSize:size]; | |
2075 | } | |
0f9b48d1 SC |
2076 | } |
2077 | ||
1a289c62 | 2078 | void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&col, long, bool) |
1e181c7a | 2079 | { |
09a9eb20 | 2080 | if ([m_osxView respondsToSelector:@selector(setFont:)]) |
aa6208d9 | 2081 | [m_osxView setFont: font.OSXGetNSFont()]; |
1a289c62 SC |
2082 | if ([m_osxView respondsToSelector:@selector(setTextColor:)]) |
2083 | [m_osxView setTextColor:[NSColor colorWithCalibratedRed:(CGFloat) (col.Red() / 255.0) | |
2084 | green:(CGFloat) (col.Green() / 255.0) | |
2085 | blue:(CGFloat) (col.Blue() / 255.0) | |
2086 | alpha:(CGFloat) (col.Alpha() / 255.0)]]; | |
1e181c7a SC |
2087 | } |
2088 | ||
a7b9865d KO |
2089 | void wxWidgetCocoaImpl::SetToolTip(wxToolTip* tooltip) |
2090 | { | |
2091 | if (tooltip) | |
2092 | { | |
2093 | wxCFStringRef cf( tooltip->GetTip() , m_wxPeer->GetFont().GetEncoding() ); | |
2094 | [m_osxView setToolTip: cf.AsNSString()]; | |
2095 | } | |
2096 | else | |
2097 | [m_osxView setToolTip: nil]; | |
2098 | ||
2099 | } | |
2100 | ||
c4825ef7 SC |
2101 | void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control ) |
2102 | { | |
4dd9fdf8 SC |
2103 | WXWidget c = control ? control : (WXWidget) m_osxView; |
2104 | wxWidgetImpl::Associate( c, this ) ; | |
2105 | if ([c respondsToSelector:@selector(setAction:)]) | |
2106 | { | |
2107 | [c setTarget: c]; | |
e32090ba | 2108 | [c setAction: @selector(controlAction:)]; |
4dd9fdf8 SC |
2109 | if ([c respondsToSelector:@selector(setDoubleAction:)]) |
2110 | { | |
e32090ba | 2111 | [c setDoubleAction: @selector(controlDoubleAction:)]; |
4dd9fdf8 | 2112 | } |
03647350 | 2113 | |
4dd9fdf8 | 2114 | } |
c4825ef7 SC |
2115 | } |
2116 | ||
f0e0116e KO |
2117 | bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text) |
2118 | { | |
4d61ae5f | 2119 | wxKeyEvent wxevent(wxEVT_CHAR); |
f0e0116e | 2120 | SetupKeyEvent( wxevent, event, text ); |
f0e0116e KO |
2121 | |
2122 | return GetWXPeer()->OSXHandleKeyEvent(wxevent); | |
2123 | } | |
2124 | ||
ffad7b0d SC |
2125 | bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event) |
2126 | { | |
2127 | wxKeyEvent wxevent(wxEVT_KEY_DOWN); | |
2128 | SetupKeyEvent( wxevent, event ); | |
f0e0116e | 2129 | bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent); |
19c7ac3d | 2130 | |
f0e0116e KO |
2131 | // this will fire higher level events, like insertText, to help |
2132 | // us handle EVT_CHAR, etc. | |
715824d5 | 2133 | |
cc710171 | 2134 | if ( !result ) |
f0e0116e | 2135 | { |
cc710171 | 2136 | if ( IsUserPane() && [event type] == NSKeyDown) |
7cb2a241 | 2137 | { |
a8fc3508 SC |
2138 | long keycode = wxOSXTranslateCocoaKey( event, wxEVT_CHAR ); |
2139 | ||
2140 | if ( (keycode > 0 && keycode < WXK_SPACE) || keycode == WXK_DELETE || keycode >= WXK_START ) | |
849252d5 SC |
2141 | { |
2142 | // eventually we could setup a doCommandBySelector catcher and retransform this into the wx key chars | |
2143 | wxKeyEvent wxevent2(wxevent) ; | |
68065b91 | 2144 | wxevent2.SetEventType(wxEVT_CHAR); |
a8fc3508 | 2145 | wxevent2.m_keyCode = keycode; |
b895498c | 2146 | result = GetWXPeer()->OSXHandleKeyEvent(wxevent2); |
849252d5 | 2147 | } |
7cb2a241 | 2148 | else |
849252d5 | 2149 | { |
b895498c SC |
2150 | if ( !wxevent.CmdDown() ) |
2151 | { | |
2152 | if ( [m_osxView isKindOfClass:[NSScrollView class] ] ) | |
2153 | [[(NSScrollView*)m_osxView documentView] interpretKeyEvents:[NSArray arrayWithObject:event]]; | |
2154 | else | |
2155 | [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]]; | |
2156 | result = true; | |
2157 | } | |
849252d5 | 2158 | } |
7cb2a241 | 2159 | } |
f0e0116e | 2160 | } |
715824d5 | 2161 | |
f0e0116e | 2162 | return result; |
ffad7b0d SC |
2163 | } |
2164 | ||
b466e85a | 2165 | bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event) |
4850cc8b | 2166 | { |
4850cc8b | 2167 | wxMouseEvent wxevent(wxEVT_LEFT_DOWN); |
ddbc8ac9 | 2168 | SetupMouseEvent(wxevent , event) ; |
b466e85a SC |
2169 | |
2170 | return GetWXPeer()->HandleWindowEvent(wxevent); | |
4850cc8b SC |
2171 | } |
2172 | ||
b42865ce | 2173 | void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow) |
0c530e5a SC |
2174 | { |
2175 | wxWindow* thisWindow = GetWXPeer(); | |
2176 | if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() ) | |
2177 | { | |
2178 | thisWindow->MacInvalidateBorders(); | |
2179 | } | |
2180 | ||
2181 | if ( receivedFocus ) | |
2182 | { | |
9a83f860 | 2183 | wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow)); |
0c530e5a SC |
2184 | wxChildFocusEvent eventFocus((wxWindow*)thisWindow); |
2185 | thisWindow->HandleWindowEvent(eventFocus); | |
2186 | ||
2187 | #if wxUSE_CARET | |
2188 | if ( thisWindow->GetCaret() ) | |
2189 | thisWindow->GetCaret()->OnSetFocus(); | |
2190 | #endif | |
2191 | ||
2192 | wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId()); | |
2193 | event.SetEventObject(thisWindow); | |
b42865ce KO |
2194 | if (otherWindow) |
2195 | event.SetWindow(otherWindow->GetWXPeer()); | |
0c530e5a SC |
2196 | thisWindow->HandleWindowEvent(event) ; |
2197 | } | |
2198 | else // !receivedFocuss | |
2199 | { | |
2200 | #if wxUSE_CARET | |
2201 | if ( thisWindow->GetCaret() ) | |
2202 | thisWindow->GetCaret()->OnKillFocus(); | |
2203 | #endif | |
2204 | ||
9a83f860 | 2205 | wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow)); |
03647350 | 2206 | |
0c530e5a SC |
2207 | wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId()); |
2208 | event.SetEventObject(thisWindow); | |
b42865ce KO |
2209 | if (otherWindow) |
2210 | event.SetWindow(otherWindow->GetWXPeer()); | |
0c530e5a SC |
2211 | thisWindow->HandleWindowEvent(event) ; |
2212 | } | |
2213 | } | |
2214 | ||
54f11060 SC |
2215 | void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor) |
2216 | { | |
201c6db3 | 2217 | if ( !wxIsBusy() ) |
54f11060 | 2218 | { |
201c6db3 SC |
2219 | NSPoint location = [NSEvent mouseLocation]; |
2220 | location = [[m_osxView window] convertScreenToBase:location]; | |
2221 | NSPoint locationInView = [m_osxView convertPoint:location fromView:nil]; | |
2222 | ||
2223 | if( NSMouseInRect(locationInView, [m_osxView bounds], YES) ) | |
2224 | { | |
2225 | [(NSCursor*)cursor.GetHCURSOR() set]; | |
2226 | } | |
54f11060 SC |
2227 | } |
2228 | [[m_osxView window] invalidateCursorRectsForView:m_osxView]; | |
2229 | } | |
2230 | ||
2231 | void wxWidgetCocoaImpl::CaptureMouse() | |
2232 | { | |
2233 | [[m_osxView window] disableCursorRects]; | |
2234 | } | |
2235 | ||
2236 | void wxWidgetCocoaImpl::ReleaseMouse() | |
2237 | { | |
2238 | [[m_osxView window] enableCursorRects]; | |
2239 | } | |
4850cc8b | 2240 | |
4dd9fdf8 SC |
2241 | void wxWidgetCocoaImpl::SetFlipped(bool flipped) |
2242 | { | |
2243 | m_isFlipped = flipped; | |
2244 | } | |
2245 | ||
33e90275 SC |
2246 | // |
2247 | // Factory methods | |
2248 | // | |
2249 | ||
03647350 | 2250 | wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent), |
d8207702 SC |
2251 | wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size, |
2252 | long WXUNUSED(style), long WXUNUSED(extraStyle)) | |
33e90275 | 2253 | { |
dbeddfb9 | 2254 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
33e90275 | 2255 | wxNSView* v = [[wxNSView alloc] initWithFrame:r]; |
4dd9fdf8 SC |
2256 | |
2257 | // temporary hook for dnd | |
2258 | [v registerForDraggedTypes:[NSArray arrayWithObjects: | |
2259 | NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]]; | |
03647350 | 2260 | |
d15694e8 | 2261 | wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v, false, true ); |
33e90275 SC |
2262 | return c; |
2263 | } | |
2264 | ||
03647350 | 2265 | wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now ) |
33e90275 SC |
2266 | { |
2267 | NSWindow* tlw = now->GetWXWindow(); | |
17e2694c SC |
2268 | |
2269 | wxWidgetCocoaImpl* c = NULL; | |
2270 | if ( now->IsNativeWindowWrapper() ) | |
2271 | { | |
bc5c09a3 SC |
2272 | NSView* cv = [tlw contentView]; |
2273 | c = new wxWidgetCocoaImpl( now, cv, true ); | |
2274 | // increase ref count, because the impl destructor will decrement it again | |
2275 | CFRetain(cv); | |
2276 | if ( !now->IsShown() ) | |
2277 | [cv setHidden:NO]; | |
2278 | ||
17e2694c SC |
2279 | } |
2280 | else | |
2281 | { | |
2282 | wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]]; | |
2283 | c = new wxWidgetCocoaImpl( now, v, true ); | |
2284 | c->InstallEventHandler(); | |
2285 | [tlw setContentView:v]; | |
2286 | } | |
33e90275 SC |
2287 | return c; |
2288 | } |