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