]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/window.mm
Add support for wxDV_NO_HEADER to OS X/Cocoa wxDataViewCtrl.
[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
ab9a0b84
VZ
1202@interface wxNSAnimationDelegate : NSObject
1203#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1204 <NSAnimationDelegate>
1205#endif
1206{
ab9a0b84 1207 wxWindow *m_win;
f2be0678 1208 bool m_isDone;
ab9a0b84
VZ
1209}
1210
f2be0678
VZ
1211- (id)init:(wxWindow *)win;
1212
1213- (bool)isDone;
ab9a0b84
VZ
1214
1215// NSAnimationDelegate methods
f2be0678 1216- (void)animationDidEnd:(NSAnimation*)animation;
ab9a0b84
VZ
1217- (void)animation:(NSAnimation*)animation
1218 didReachProgressMark:(NSAnimationProgress)progress;
ab9a0b84
VZ
1219@end
1220
1221@implementation wxNSAnimationDelegate
1222
f2be0678 1223- (id)init:(wxWindow *)win
ab9a0b84
VZ
1224{
1225 [super init];
1226
1227 m_win = win;
f2be0678
VZ
1228 m_isDone = false;
1229
ab9a0b84
VZ
1230 return self;
1231}
1232
f2be0678
VZ
1233- (bool)isDone
1234{
1235 return m_isDone;
1236}
1237
ab9a0b84
VZ
1238- (void)animation:(NSAnimation*)animation
1239 didReachProgressMark:(NSAnimationProgress)progress
1240{
1241 wxUnusedVar(animation);
1242 wxUnusedVar(progress);
1243
1244 m_win->SendSizeEvent();
1245}
1246
f2be0678 1247- (void)animationDidEnd:(NSAnimation*)animation
ab9a0b84 1248{
f2be0678 1249 m_isDone = true;
ab9a0b84
VZ
1250}
1251
1252@end
1253
1254/* static */
1255bool
1256wxWidgetCocoaImpl::ShowViewOrWindowWithEffect(wxWindow *win,
1257 bool show,
1258 wxShowEffect effect,
1259 unsigned timeout)
1260{
ab9a0b84
VZ
1261 // create the dictionary describing the animation to perform on this view
1262 NSObject * const
1263 viewOrWin = static_cast<NSObject *>(win->OSXGetViewOrWindow());
1264 NSMutableDictionary * const
1265 dict = [NSMutableDictionary dictionaryWithCapacity:4];
1266 [dict setObject:viewOrWin forKey:NSViewAnimationTargetKey];
1267
1268 // determine the start and end rectangles assuming we're hiding the window
f2be0678 1269 const wxRect rectOrig = win->GetRect();
ab9a0b84
VZ
1270 wxRect rectStart,
1271 rectEnd;
1272 rectStart =
f2be0678 1273 rectEnd = rectOrig;
ab9a0b84
VZ
1274
1275 if ( show )
1276 {
1277 if ( effect == wxSHOW_EFFECT_ROLL_TO_LEFT ||
1278 effect == wxSHOW_EFFECT_SLIDE_TO_LEFT )
1279 effect = wxSHOW_EFFECT_ROLL_TO_RIGHT;
1280 else if ( effect == wxSHOW_EFFECT_ROLL_TO_RIGHT ||
1281 effect == wxSHOW_EFFECT_SLIDE_TO_RIGHT )
1282 effect = wxSHOW_EFFECT_ROLL_TO_LEFT;
1283 else if ( effect == wxSHOW_EFFECT_ROLL_TO_TOP ||
1284 effect == wxSHOW_EFFECT_SLIDE_TO_TOP )
1285 effect = wxSHOW_EFFECT_ROLL_TO_BOTTOM;
1286 else if ( effect == wxSHOW_EFFECT_ROLL_TO_BOTTOM ||
1287 effect == wxSHOW_EFFECT_SLIDE_TO_BOTTOM )
1288 effect = wxSHOW_EFFECT_ROLL_TO_TOP;
1289 }
1290
1291 switch ( effect )
1292 {
1293 case wxSHOW_EFFECT_ROLL_TO_LEFT:
1294 case wxSHOW_EFFECT_SLIDE_TO_LEFT:
1295 rectEnd.width = 0;
1296 break;
1297
1298 case wxSHOW_EFFECT_ROLL_TO_RIGHT:
1299 case wxSHOW_EFFECT_SLIDE_TO_RIGHT:
1300 rectEnd.x = rectStart.GetRight();
1301 rectEnd.width = 0;
1302 break;
1303
1304 case wxSHOW_EFFECT_ROLL_TO_TOP:
1305 case wxSHOW_EFFECT_SLIDE_TO_TOP:
1306 rectEnd.height = 0;
1307 break;
1308
1309 case wxSHOW_EFFECT_ROLL_TO_BOTTOM:
1310 case wxSHOW_EFFECT_SLIDE_TO_BOTTOM:
1311 rectEnd.y = rectStart.GetBottom();
1312 rectEnd.height = 0;
1313 break;
1314
1315 case wxSHOW_EFFECT_EXPAND:
1316 rectEnd.x = rectStart.x + rectStart.width / 2;
1317 rectEnd.y = rectStart.y + rectStart.height / 2;
1318 rectEnd.width =
1319 rectEnd.height = 0;
1320 break;
1321
1322 case wxSHOW_EFFECT_BLEND:
1323 [dict setObject:(show ? NSViewAnimationFadeInEffect
1324 : NSViewAnimationFadeOutEffect)
1325 forKey:NSViewAnimationEffectKey];
1326 break;
1327
1328 case wxSHOW_EFFECT_NONE:
1329 case wxSHOW_EFFECT_MAX:
1330 wxFAIL_MSG( "unexpected animation effect" );
1331 return false;
1332
1333 default:
1334 wxFAIL_MSG( "unknown animation effect" );
1335 return false;
1336 };
1337
1338 if ( show )
1339 {
1340 // we need to restore it to the original rectangle instead of making it
1341 // disappear
1342 wxSwap(rectStart, rectEnd);
1343
1344 // and as the window is currently hidden, we need to show it for the
1345 // animation to be visible at all (but don't restore it at its full
1346 // rectangle as it shouldn't appear immediately)
1347 win->SetSize(rectStart);
f2be0678 1348 win->Show();
ab9a0b84
VZ
1349 }
1350
1351 NSView * const parentView = [viewOrWin isKindOfClass:[NSView class]]
1352 ? [(NSView *)viewOrWin superview]
1353 : nil;
1354 const NSRect rStart = wxToNSRect(parentView, rectStart);
1355 const NSRect rEnd = wxToNSRect(parentView, rectEnd);
1356
1357 [dict setObject:[NSValue valueWithRect:rStart]
1358 forKey:NSViewAnimationStartFrameKey];
1359 [dict setObject:[NSValue valueWithRect:rEnd]
1360 forKey:NSViewAnimationEndFrameKey];
1361
1362 // create an animation using the values in the above dictionary
ab9a0b84
VZ
1363 NSViewAnimation * const
1364 anim = [[NSViewAnimation alloc]
1365 initWithViewAnimations:[NSArray arrayWithObject:dict]];
ab9a0b84
VZ
1366
1367 if ( !timeout )
1368 {
1369 // what is a good default duration? Windows uses 200ms, Web frameworks
1370 // use anything from 250ms to 1s... choose something in the middle
1371 timeout = 500;
1372 }
1373
1374 [anim setDuration:timeout/1000.]; // duration is in seconds here
1375
1376 // if the window being animated changes its layout depending on its size
1377 // (which is almost always the case) we need to redo it during animation
1378 //
1379 // the number of layouts here is arbitrary, but 10 seems like too few (e.g.
1380 // controls in wxInfoBar visibly jump around)
1381 const int NUM_LAYOUTS = 20;
1382 for ( float f = 1./NUM_LAYOUTS; f < 1.; f += 1./NUM_LAYOUTS )
1383 [anim addProgressMark:f];
1384
f2be0678
VZ
1385 wxNSAnimationDelegate * const
1386 animDelegate = [[wxNSAnimationDelegate alloc] init:win];
1387 [anim setDelegate:animDelegate];
ab9a0b84
VZ
1388 [anim startAnimation];
1389
f2be0678
VZ
1390 // Cocoa is capable of doing animation asynchronously or even from separate
1391 // thread but wx API doesn't provide any way to be notified about the
1392 // animation end and without this we really must ensure that the window has
1393 // the expected (i.e. the same as if a simple Show() had been used) size
1394 // when we return, so block here until the animation finishes
1395 //
1396 // notice that because the default animation mode is NSAnimationBlocking,
1397 // no user input events ought to be processed from here
dc72e2ad
VZ
1398 {
1399 wxEventLoopGuarantor ensureEventLoopExistence;
1400 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
1401 while ( ![animDelegate isDone] )
1402 loop->Dispatch();
1403 }
f2be0678
VZ
1404
1405 if ( !show )
1406 {
1407 // NSViewAnimation is smart enough to hide the NSView being animated at
1408 // the end but we also must ensure that it's hidden for wx too
1409 win->Hide();
1410
1411 // and we must also restore its size because it isn't expected to
1412 // change just because the window was hidden
1413 win->SetSize(rectOrig);
1414 }
1415 else
1416 {
1417 // refresh it once again after the end to ensure that everything is in
1418 // place
1419 win->SendSizeEvent();
1420 }
1421
1422 [anim setDelegate:nil];
1423 [animDelegate release];
1424 [anim release];
1425
ab9a0b84
VZ
1426 return true;
1427}
1428
1429bool wxWidgetCocoaImpl::ShowWithEffect(bool show,
1430 wxShowEffect effect,
1431 unsigned timeout)
1432{
1433 return ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
1434}
1435
33e90275
SC
1436void wxWidgetCocoaImpl::Raise()
1437{
11fed901 1438 // Not implemented
33e90275 1439}
03647350 1440
33e90275
SC
1441void wxWidgetCocoaImpl::Lower()
1442{
11fed901 1443 // Not implemented
33e90275
SC
1444}
1445
d8207702 1446void wxWidgetCocoaImpl::ScrollRect( const wxRect *WXUNUSED(rect), int WXUNUSED(dx), int WXUNUSED(dy) )
33e90275 1447{
54f11060
SC
1448#if 1
1449 SetNeedsDisplay() ;
1450#else
1451 // We should do something like this, but it wasn't working in 10.4.
1452 if (GetNeedsDisplay() )
1453 {
1454 SetNeedsDisplay() ;
1455 }
1456 NSRect r = wxToNSRect( [m_osxView superview], *rect );
1457 NSSize offset = NSMakeSize((float)dx, (float)dy);
1458 [m_osxView scrollRect:r by:offset];
1459#endif
33e90275
SC
1460}
1461
1462void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
1463{
0c530e5a 1464 wxWindowMac* parent = GetWXPeer()->GetParent();
03647350 1465 // under Cocoa we might have a contentView in the wxParent to which we have to
0c530e5a 1466 // adjust the coordinates
ea468399 1467 if (parent && [m_osxView superview] != parent->GetHandle() )
0c530e5a 1468 {
57c0a8ac
SC
1469 int cx = 0,cy = 0,cw = 0,ch = 0;
1470 if ( parent->GetPeer() )
1471 {
1472 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
1473 x -= cx;
1474 y -= cy;
1475 }
0c530e5a 1476 }
95fb3153 1477 [[m_osxView superview] setNeedsDisplayInRect:[m_osxView frame]];
33e90275
SC
1478 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
1479 [m_osxView setFrame:r];
03647350
VZ
1480 [[m_osxView superview] setNeedsDisplayInRect:r];
1481
41af81ea 1482 if ([m_osxView respondsToSelector:@selector(trackingTag)] )
fc099495
KO
1483 {
1484 if ( [(wxNSView*)m_osxView trackingTag] )
1485 [m_osxView removeTrackingRect: [(wxNSView*)m_osxView trackingTag]];
03647350 1486
fc099495
KO
1487 [(wxNSView*)m_osxView setTrackingTag: [m_osxView addTrackingRect: [m_osxView bounds] owner: m_osxView userData: nil assumeInside: NO]];
1488 }
33e90275
SC
1489}
1490
1491void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1492{
1493 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1494 x = r.GetLeft();
1495 y = r.GetTop();
1496}
1497
1498void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
1499{
1500 NSRect rect = [m_osxView frame];
e490b0d2
VZ
1501 width = (int)rect.size.width;
1502 height = (int)rect.size.height;
33e90275
SC
1503}
1504
dbeddfb9 1505void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
33e90275 1506{
0c530e5a
SC
1507 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1508 {
1509 NSView* cv = [m_osxView contentView];
03647350 1510
0c530e5a
SC
1511 NSRect bounds = [m_osxView bounds];
1512 NSRect rect = [cv frame];
03647350 1513
e490b0d2
VZ
1514 int y = (int)rect.origin.y;
1515 int x = (int)rect.origin.x;
0c530e5a 1516 if ( ![ m_osxView isFlipped ] )
e490b0d2 1517 y = (int)(bounds.size.height - (rect.origin.y + rect.size.height));
0c530e5a
SC
1518 left = x;
1519 top = y;
e490b0d2
VZ
1520 width = (int)rect.size.width;
1521 height = (int)rect.size.height;
0c530e5a
SC
1522 }
1523 else
1524 {
1525 left = top = 0;
1526 GetSize( width, height );
1527 }
33e90275
SC
1528}
1529
1530void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1531{
1532 if ( where )
1533 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1534 else
1535 [m_osxView setNeedsDisplay:YES];
1536}
1537
1538bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1539{
1540 return [m_osxView needsDisplay];
1541}
1542
dbeddfb9 1543bool wxWidgetCocoaImpl::CanFocus() const
33e90275 1544{
dbeddfb9 1545 return [m_osxView canBecomeKeyView] == YES;
33e90275
SC
1546}
1547
1548bool wxWidgetCocoaImpl::HasFocus() const
1549{
f06e0fea 1550 return ( FindFocus() == m_osxView );
33e90275
SC
1551}
1552
03647350 1553bool wxWidgetCocoaImpl::SetFocus()
33e90275 1554{
dbeddfb9
SC
1555 if ( [m_osxView canBecomeKeyView] == NO )
1556 return false;
03647350 1557
dbeddfb9 1558 [[m_osxView window] makeFirstResponder: m_osxView] ;
47e29847 1559 [[m_osxView window] makeKeyAndOrderFront:nil] ;
dbeddfb9 1560 return true;
33e90275
SC
1561}
1562
1563
1564void wxWidgetCocoaImpl::RemoveFromParent()
1565{
1566 [m_osxView removeFromSuperview];
1567}
1568
1569void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1570{
1571 NSView* container = parent->GetWXWidget() ;
1572 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1573 [container addSubview:m_osxView];
1574}
1575
3b2527c7 1576void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &col )
dbeddfb9 1577{
3b2527c7
SC
1578 NSView* targetView = m_osxView;
1579 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1580 targetView = [(NSScrollView*) m_osxView documentView];
1581
1582 if ( [targetView respondsToSelector:@selector(setBackgroundColor:) ] )
1583 {
1584 [targetView setBackgroundColor:[NSColor colorWithCalibratedRed:(CGFloat) (col.Red() / 255.0)
1585 green:(CGFloat) (col.Green() / 255.0)
1586 blue:(CGFloat) (col.Blue() / 255.0)
1587 alpha:(CGFloat) (col.Alpha() / 255.0)]];
1588 }
dbeddfb9
SC
1589}
1590
1591void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1592{
1593 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1594 {
d8207702 1595 wxCFStringRef cf( title , encoding );
dbeddfb9
SC
1596 [m_osxView setTitle:cf.AsNSString()];
1597 }
e32090ba
SC
1598 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1599 {
d8207702 1600 wxCFStringRef cf( title , encoding );
e32090ba
SC
1601 [m_osxView setStringValue:cf.AsNSString()];
1602 }
dbeddfb9 1603}
03647350 1604
dbeddfb9
SC
1605
1606void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1607{
1608 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
03647350 1609 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
dbeddfb9
SC
1610 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1611}
1612
03647350 1613wxInt32 wxWidgetCocoaImpl::GetValue() const
dbeddfb9
SC
1614{
1615 return [(NSControl*)m_osxView intValue];
1616}
1617
03647350 1618void wxWidgetCocoaImpl::SetValue( wxInt32 v )
dbeddfb9
SC
1619{
1620 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1621 {
1622 [m_osxView setIntValue:v];
1623 }
1624 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1625 {
1626 [m_osxView setFloatValue:(double)v];
1627 }
1628 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1629 {
1630 [m_osxView setDoubleValue:(double)v];
1631 }
1632}
1633
03647350 1634void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
dbeddfb9
SC
1635{
1636 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1637 {
1638 [m_osxView setMinValue:(double)v];
1639 }
1640}
1641
03647350 1642void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
dbeddfb9
SC
1643{
1644 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1645 {
1646 [m_osxView setMaxValue:(double)v];
1647 }
1648}
1649
03647350 1650wxInt32 wxWidgetCocoaImpl::GetMinimum() const
19c7ac3d
SC
1651{
1652 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1653 {
e490b0d2 1654 return (int)[m_osxView minValue];
19c7ac3d
SC
1655 }
1656 return 0;
1657}
1658
03647350 1659wxInt32 wxWidgetCocoaImpl::GetMaximum() const
19c7ac3d
SC
1660{
1661 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1662 {
e490b0d2 1663 return (int)[m_osxView maxValue];
19c7ac3d
SC
1664 }
1665 return 0;
1666}
1667
e5d05b90
VZ
1668wxBitmap wxWidgetCocoaImpl::GetBitmap() const
1669{
1670 wxBitmap bmp;
1671
1672 // TODO: how to create a wxBitmap from NSImage?
1673#if 0
1674 if ( [m_osxView respondsToSelector:@selector(image:)] )
1675 bmp = [m_osxView image];
1676#endif
1677
1678 return bmp;
1679}
1680
dbeddfb9
SC
1681void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1682{
1683 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1684 {
1685 [m_osxView setImage:bitmap.GetNSImage()];
411a1c35 1686 [m_osxView setNeedsDisplay:YES];
dbeddfb9
SC
1687 }
1688}
1689
e5d05b90
VZ
1690void wxWidgetCocoaImpl::SetBitmapPosition( wxDirection dir )
1691{
1692 if ( [m_osxView respondsToSelector:@selector(setImagePosition:)] )
1693 {
1694 NSCellImagePosition pos;
1695 switch ( dir )
1696 {
1697 case wxLEFT:
1698 pos = NSImageLeft;
1699 break;
1700
1701 case wxRIGHT:
1702 pos = NSImageRight;
1703 break;
1704
1705 case wxTOP:
1706 pos = NSImageAbove;
1707 break;
1708
1709 case wxBOTTOM:
1710 pos = NSImageBelow;
1711 break;
1712
1713 default:
1714 wxFAIL_MSG( "invalid image position" );
1715 pos = NSNoImage;
1716 }
1717
1718 [m_osxView setImagePosition:pos];
1719 }
1720}
1721
d8207702 1722void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook))
dbeddfb9
SC
1723{
1724 // implementation in subclass
1725}
1726
1727void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1728{
1729 r->x = r->y = r->width = r->height = 0;
09a9eb20 1730
dbeddfb9
SC
1731 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1732 {
1733 NSRect former = [m_osxView frame];
1734 [m_osxView sizeToFit];
1735 NSRect best = [m_osxView frame];
1736 [m_osxView setFrame:former];
e490b0d2
VZ
1737 r->width = (int)best.size.width;
1738 r->height = (int)best.size.height;
dbeddfb9
SC
1739 }
1740}
1741
1742bool wxWidgetCocoaImpl::IsEnabled() const
1743{
0f9b48d1
SC
1744 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1745 return [m_osxView isEnabled];
1746 return true;
dbeddfb9
SC
1747}
1748
1749void wxWidgetCocoaImpl::Enable( bool enable )
1750{
0f9b48d1
SC
1751 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1752 [m_osxView setEnabled:enable];
dbeddfb9
SC
1753}
1754
1755void wxWidgetCocoaImpl::PulseGauge()
1756{
1757}
1758
d8207702 1759void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 WXUNUSED(val), wxInt32 WXUNUSED(view) )
dbeddfb9
SC
1760{
1761}
33e90275 1762
03647350 1763void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
0f9b48d1
SC
1764{
1765 NSControlSize size = NSRegularControlSize;
03647350 1766
0f9b48d1
SC
1767 switch ( variant )
1768 {
1769 case wxWINDOW_VARIANT_NORMAL :
1770 size = NSRegularControlSize;
1771 break ;
1772
1773 case wxWINDOW_VARIANT_SMALL :
1774 size = NSSmallControlSize;
1775 break ;
1776
1777 case wxWINDOW_VARIANT_MINI :
1778 size = NSMiniControlSize;
1779 break ;
1780
1781 case wxWINDOW_VARIANT_LARGE :
1782 size = NSRegularControlSize;
1783 break ;
1784
1785 default:
9a83f860 1786 wxFAIL_MSG(wxT("unexpected window variant"));
0f9b48d1
SC
1787 break ;
1788 }
1789 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1790 [m_osxView setControlSize:size];
03647350
VZ
1791 else if ([m_osxView respondsToSelector:@selector(cell)])
1792 {
1793 id cell = [(id)m_osxView cell];
1794 if ([cell respondsToSelector:@selector(setControlSize:)])
1795 [cell setControlSize:size];
1796 }
0f9b48d1
SC
1797}
1798
09a9eb20 1799void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&, long, bool)
1e181c7a 1800{
09a9eb20 1801 if ([m_osxView respondsToSelector:@selector(setFont:)])
aa6208d9 1802 [m_osxView setFont: font.OSXGetNSFont()];
1e181c7a
SC
1803}
1804
c4825ef7
SC
1805void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1806{
4dd9fdf8
SC
1807 WXWidget c = control ? control : (WXWidget) m_osxView;
1808 wxWidgetImpl::Associate( c, this ) ;
1809 if ([c respondsToSelector:@selector(setAction:)])
1810 {
1811 [c setTarget: c];
e32090ba 1812 [c setAction: @selector(controlAction:)];
4dd9fdf8
SC
1813 if ([c respondsToSelector:@selector(setDoubleAction:)])
1814 {
e32090ba 1815 [c setDoubleAction: @selector(controlDoubleAction:)];
4dd9fdf8 1816 }
03647350 1817
4dd9fdf8 1818 }
c4825ef7
SC
1819}
1820
f0e0116e
KO
1821bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
1822{
4d61ae5f 1823 wxKeyEvent wxevent(wxEVT_CHAR);
f0e0116e 1824 SetupKeyEvent( wxevent, event, text );
f0e0116e
KO
1825
1826 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1827}
1828
ffad7b0d
SC
1829bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1830{
1831 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1832 SetupKeyEvent( wxevent, event );
f0e0116e 1833 bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
19c7ac3d 1834
f0e0116e
KO
1835 // this will fire higher level events, like insertText, to help
1836 // us handle EVT_CHAR, etc.
715824d5
SC
1837
1838 if ( m_wxPeer->MacIsUserPane() && [event type] == NSKeyDown)
f0e0116e 1839 {
4d61ae5f 1840 if ( !result )
7cb2a241
SC
1841 {
1842 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1843 [[(NSScrollView*)m_osxView documentView] interpretKeyEvents:[NSArray arrayWithObject:event]];
1844 else
1845 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
1846 result = true;
1847 }
f0e0116e 1848 }
715824d5 1849
f0e0116e 1850 return result;
ffad7b0d
SC
1851}
1852
b466e85a 1853bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
4850cc8b 1854{
03647350
VZ
1855 NSPoint clickLocation;
1856 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
4850cc8b
SC
1857 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1858 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
ddbc8ac9 1859 SetupMouseEvent(wxevent , event) ;
4850cc8b
SC
1860 wxevent.m_x = pt.x;
1861 wxevent.m_y = pt.y;
b466e85a
SC
1862
1863 return GetWXPeer()->HandleWindowEvent(wxevent);
4850cc8b
SC
1864}
1865
b42865ce 1866void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
0c530e5a
SC
1867{
1868 wxWindow* thisWindow = GetWXPeer();
1869 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1870 {
1871 thisWindow->MacInvalidateBorders();
1872 }
1873
1874 if ( receivedFocus )
1875 {
9a83f860 1876 wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow));
0c530e5a
SC
1877 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1878 thisWindow->HandleWindowEvent(eventFocus);
1879
1880#if wxUSE_CARET
1881 if ( thisWindow->GetCaret() )
1882 thisWindow->GetCaret()->OnSetFocus();
1883#endif
1884
1885 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1886 event.SetEventObject(thisWindow);
b42865ce
KO
1887 if (otherWindow)
1888 event.SetWindow(otherWindow->GetWXPeer());
0c530e5a
SC
1889 thisWindow->HandleWindowEvent(event) ;
1890 }
1891 else // !receivedFocuss
1892 {
1893#if wxUSE_CARET
1894 if ( thisWindow->GetCaret() )
1895 thisWindow->GetCaret()->OnKillFocus();
1896#endif
1897
9a83f860 1898 wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow));
03647350 1899
0c530e5a
SC
1900 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1901 event.SetEventObject(thisWindow);
b42865ce
KO
1902 if (otherWindow)
1903 event.SetWindow(otherWindow->GetWXPeer());
0c530e5a
SC
1904 thisWindow->HandleWindowEvent(event) ;
1905 }
1906}
1907
54f11060
SC
1908void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1909{
1910 NSPoint location = [NSEvent mouseLocation];
1911 location = [[m_osxView window] convertScreenToBase:location];
1912 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1913
1914 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1915 {
1916 [(NSCursor*)cursor.GetHCURSOR() set];
1917 }
1918 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1919}
1920
1921void wxWidgetCocoaImpl::CaptureMouse()
1922{
1923 [[m_osxView window] disableCursorRects];
1924}
1925
1926void wxWidgetCocoaImpl::ReleaseMouse()
1927{
1928 [[m_osxView window] enableCursorRects];
1929}
4850cc8b 1930
4dd9fdf8
SC
1931void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1932{
1933 m_isFlipped = flipped;
1934}
1935
33e90275
SC
1936//
1937// Factory methods
1938//
1939
03647350 1940wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
d8207702
SC
1941 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
1942 long WXUNUSED(style), long WXUNUSED(extraStyle))
33e90275 1943{
dbeddfb9 1944 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
33e90275 1945 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
4dd9fdf8
SC
1946
1947 // temporary hook for dnd
1948 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1949 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
03647350 1950
33e90275 1951 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
33e90275
SC
1952 return c;
1953}
1954
03647350 1955wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
33e90275
SC
1956{
1957 NSWindow* tlw = now->GetWXWindow();
1958 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1959 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
7e06ee6d 1960 c->InstallEventHandler();
33e90275
SC
1961 [tlw setContentView:v];
1962 return c;
1963}