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