]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/window.mm
move wxGridCellRenderer::Draw before other derived classes implementations (no real...
[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
0c530e5a
SC
24#if wxUSE_CARET
25 #include "wx/caret.h"
26#endif
27
4dd9fdf8
SC
28#if wxUSE_DRAG_AND_DROP
29 #include "wx/dnd.h"
30#endif
31
32#include <objc/objc-runtime.h>
33
dbeddfb9
SC
34NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
35{
36 int x, y, w, h ;
37
38 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
39 wxRect bounds(x,y,w,h);
40 NSView* sv = (window->GetParent()->GetHandle() );
41
42 return wxToNSRect( sv, bounds );
43}
33e90275
SC
44
45@interface wxNSView : NSView
46{
fc099495 47 NSTrackingRectTag rectTag;
33e90275
SC
48}
49
524c47aa 50- (BOOL) canBecomeKeyView;
fc099495
KO
51// the tracking tag is needed to track mouse enter / exit events
52- (void) setTrackingTag: (NSTrackingRectTag)tag;
53- (NSTrackingRectTag) trackingTag;
33e90275
SC
54@end // wxNSView
55
ffad7b0d 56@interface NSView(PossibleMethods)
ffad7b0d
SC
57- (void)setTitle:(NSString *)aString;
58- (void)setStringValue:(NSString *)aString;
59- (void)setIntValue:(int)anInt;
60- (void)setFloatValue:(float)aFloat;
61- (void)setDoubleValue:(double)aDouble;
62
19c7ac3d
SC
63- (double)minValue;
64- (double)maxValue;
ffad7b0d
SC
65- (void)setMinValue:(double)aDouble;
66- (void)setMaxValue:(double)aDouble;
67
68- (void)sizeToFit;
69
70- (BOOL)isEnabled;
71- (void)setEnabled:(BOOL)flag;
72
73- (void)setImage:(NSImage *)image;
74- (void)setControlSize:(NSControlSize)size;
0c530e5a
SC
75
76- (id)contentView;
4dd9fdf8
SC
77
78- (void)setTarget:(id)anObject;
79- (void)setAction:(SEL)aSelector;
80- (void)setDoubleAction:(SEL)aSelector;
ffad7b0d
SC
81@end
82
63a6419c 83long wxOSXTranslateCocoaKey( NSEvent* event )
524c47aa 84{
63a6419c
KO
85 long retval = 0;
86
87 if ([event type] != NSFlagsChanged)
88 {
89 NSString* s = [event charactersIgnoringModifiers];
90 // backspace char reports as delete w/modifiers for some reason
91 if ([s length] == 1)
92 {
93 switch ( [s characterAtIndex:0] )
94 {
95 // backspace key
96 case 0x7F :
97 case 8 :
98 retval = WXK_BACK;
99 break;
100 case NSUpArrowFunctionKey :
101 retval = WXK_UP;
102 break;
103 case NSDownArrowFunctionKey :
104 retval = WXK_DOWN;
105 break;
106 case NSLeftArrowFunctionKey :
107 retval = WXK_LEFT;
108 break;
109 case NSRightArrowFunctionKey :
110 retval = WXK_RIGHT;
111 break;
112 case NSInsertFunctionKey :
113 retval = WXK_INSERT;
114 break;
115 case NSDeleteFunctionKey :
116 retval = WXK_DELETE;
117 break;
118 case NSHomeFunctionKey :
119 retval = WXK_HOME;
120 break;
121 // case NSBeginFunctionKey :
122 // retval = WXK_BEGIN;
123 // break;
124 case NSEndFunctionKey :
125 retval = WXK_END;
126 break;
127 case NSPageUpFunctionKey :
128 retval = WXK_PAGEUP;
129 break;
130 case NSPageDownFunctionKey :
131 retval = WXK_PAGEDOWN;
132 break;
133 case NSHelpFunctionKey :
134 retval = WXK_HELP;
135 break;
136 default:
137 int intchar = [[event characters] characterAtIndex: 0];
138 if ( intchar >= NSF1FunctionKey && intchar >= NSF24FunctionKey )
139 retval = WXK_F1 + (intchar - NSF1FunctionKey );
140 break;
141 }
142 }
143 }
144
145 // Some keys don't seem to have constants. The code mimics the approach
146 // taken by WebKit. See:
147 // http://trac.webkit.org/browser/trunk/WebCore/platform/mac/KeyEventMac.mm
148 switch( [event keyCode] )
524c47aa 149 {
63a6419c
KO
150 // command key
151 case 54:
152 case 55:
153 retval = WXK_COMMAND;
524c47aa 154 break;
63a6419c
KO
155 // caps locks key
156 case 57: // Capslock
157 retval = WXK_CAPITAL;
524c47aa 158 break;
63a6419c
KO
159 // shift key
160 case 56: // Left Shift
161 case 60: // Right Shift
162 retval = WXK_SHIFT;
524c47aa 163 break;
63a6419c
KO
164 // alt key
165 case 58: // Left Alt
166 case 61: // Right Alt
167 retval = WXK_ALT;
524c47aa 168 break;
63a6419c
KO
169 // ctrl key
170 case 59: // Left Ctrl
171 case 62: // Right Ctrl
172 retval = WXK_CONTROL;
524c47aa 173 break;
63a6419c
KO
174 // clear key
175 case 71:
176 retval = WXK_CLEAR;
524c47aa 177 break;
63a6419c
KO
178 // tab key
179 case 48:
180 retval = WXK_TAB;
524c47aa
SC
181 break;
182
183 default :
524c47aa
SC
184 break;
185 }
186 return retval;
187}
188
f0e0116e 189void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charString = NULL )
524c47aa
SC
190{
191 UInt32 modifiers = [nsEvent modifierFlags] ;
fc39cf72 192 int eventType = [nsEvent type];
524c47aa
SC
193
194 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
195 wxevent.m_controlDown = modifiers & NSControlKeyMask;
196 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
197 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
524c47aa 198
19c7ac3d 199 wxevent.m_rawCode = [nsEvent keyCode];
524c47aa
SC
200 wxevent.m_rawFlags = modifiers;
201
202 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
524c47aa
SC
203 switch (eventType)
204 {
205 case NSKeyDown :
206 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
207 break;
208 case NSKeyUp :
209 wxevent.SetEventType( wxEVT_KEY_UP ) ;
210 break;
211 case NSFlagsChanged :
212 // setup common code here
213 break;
214 default :
215 break ;
216 }
19c7ac3d
SC
217
218 wxString chars;
219 if ( eventType != NSFlagsChanged )
220 {
221 NSString* nschars = [nsEvent characters];
f0e0116e 222 if ( charString )
63a6419c
KO
223 {
224 // if charString is set, it did not come from key up / key down
225 wxevent.SetEventType( wxEVT_CHAR );
226 wxCFStringRef cfchars((CFStringRef)[charString retain]);
227 chars = cfchars.AsString();
228 }
229 else if ( nschars )
19c7ac3d
SC
230 {
231 wxCFStringRef cfchars((CFStringRef)[nschars retain]);
232 chars = cfchars.AsString();
233 }
234 }
235
63a6419c
KO
236 int aunichar = chars.Length() > 0 ? chars[0] : 0;
237 long keyval = 0;
238
239 if (wxevent.GetEventType() != wxEVT_CHAR)
240 keyval = wxOSXTranslateCocoaKey(nsEvent) ;
19c7ac3d 241
63a6419c
KO
242 if ( !keyval )
243 {
244 if ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN )
245 keyval = wxToupper( aunichar ) ;
246 else
247 keyval = aunichar;
248 }
249
19c7ac3d 250#if wxUSE_UNICODE
63a6419c 251 wxevent.m_uniChar = aunichar;
19c7ac3d
SC
252#endif
253 wxevent.m_keyCode = keyval;
524c47aa
SC
254}
255
54f11060
SC
256UInt32 g_lastButton = 0 ;
257bool g_lastButtonWasFakeRight = false ;
258
33e90275
SC
259void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
260{
4800ca79 261 int eventType = [nsEvent type];
33e90275
SC
262 UInt32 modifiers = [nsEvent modifierFlags] ;
263 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
264
265 // these parameters are not given for all events
266 UInt32 button = [nsEvent buttonNumber];
4800ca79 267 UInt32 clickCount = 0;
f1f15003 268 if ( [nsEvent respondsToSelector:@selector(clickCount:)] )
4800ca79 269 [nsEvent clickCount];
33e90275
SC
270
271 wxevent.m_x = screenMouseLocation.x;
272 wxevent.m_y = screenMouseLocation.y;
273 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
274 wxevent.m_controlDown = modifiers & NSControlKeyMask;
275 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
276 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
277 wxevent.m_clickCount = clickCount;
dbeddfb9 278 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
54f11060
SC
279
280 UInt32 mouseChord = 0;
54f11060
SC
281
282 switch (eventType)
283 {
284 case NSLeftMouseDown :
285 case NSLeftMouseDragged :
286 mouseChord = 1U;
287 break;
288 case NSRightMouseDown :
289 case NSRightMouseDragged :
290 mouseChord = 2U;
291 break;
292 case NSOtherMouseDown :
293 case NSOtherMouseDragged :
294 mouseChord = 4U;
295 break;
296 }
297
33e90275
SC
298 // a control click is interpreted as a right click
299 bool thisButtonIsFakeRight = false ;
54f11060 300 if ( button == 0 && (modifiers & NSControlKeyMask) )
33e90275 301 {
54f11060 302 button = 1 ;
33e90275
SC
303 thisButtonIsFakeRight = true ;
304 }
305
306 // otherwise we report double clicks by connecting a left click with a ctrl-left click
307 if ( clickCount > 1 && button != g_lastButton )
308 clickCount = 1 ;
54f11060 309
33e90275
SC
310 // we must make sure that our synthetic 'right' button corresponds in
311 // mouse down, moved and mouse up, and does not deliver a right down and left up
54f11060 312 switch (eventType)
33e90275 313 {
54f11060
SC
314 case NSLeftMouseDown :
315 case NSRightMouseDown :
316 case NSOtherMouseDown :
317 g_lastButton = button ;
318 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
319 break;
320 }
33e90275
SC
321
322 if ( button == 0 )
323 {
324 g_lastButton = 0 ;
325 g_lastButtonWasFakeRight = false ;
326 }
54f11060 327 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
33e90275
SC
328 button = g_lastButton ;
329
330 // Adjust the chord mask to remove the primary button and add the
331 // secondary button. It is possible that the secondary button is
332 // already pressed, e.g. on a mouse connected to a laptop, but this
333 // possibility is ignored here:
334 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
335 mouseChord = ((mouseChord & ~1U) | 2U);
336
337 if(mouseChord & 1U)
338 wxevent.m_leftDown = true ;
339 if(mouseChord & 2U)
340 wxevent.m_rightDown = true ;
341 if(mouseChord & 4U)
342 wxevent.m_middleDown = true ;
343
33e90275 344 // translate into wx types
33e90275
SC
345 switch (eventType)
346 {
347 case NSLeftMouseDown :
348 case NSRightMouseDown :
349 case NSOtherMouseDown :
350 switch ( button )
351 {
352 case 0 :
353 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
354 break ;
355
356 case 1 :
357 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
358 break ;
359
360 case 2 :
361 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
362 break ;
363
364 default:
365 break ;
366 }
367 break ;
368
369 case NSLeftMouseUp :
370 case NSRightMouseUp :
371 case NSOtherMouseUp :
372 switch ( button )
373 {
374 case 0 :
375 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
376 break ;
377
378 case 1 :
379 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
380 break ;
381
382 case 2 :
383 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
384 break ;
385
386 default:
387 break ;
388 }
389 break ;
390
391 case NSScrollWheel :
392 {
393 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
e32090ba 394 wxevent.m_wheelDelta = 10;
33e90275 395 wxevent.m_linesPerAction = 1;
e32090ba 396 NSLog(@"deltaX %f, deltaY %f",[nsEvent deltaX], [nsEvent deltaY]);
4800ca79
SC
397 if ( abs([nsEvent deltaX]) > abs([nsEvent deltaY]) )
398 {
33e90275 399 wxevent.m_wheelAxis = 1;
e32090ba 400 wxevent.m_wheelRotation = [nsEvent deltaX] * 10.0;
4800ca79
SC
401 }
402 else
403 {
e32090ba 404 wxevent.m_wheelRotation = [nsEvent deltaY] * 10.0;
4800ca79 405 }
33e90275
SC
406 }
407 break ;
408
409 case NSMouseEntered :
f1f15003
KO
410 wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
411 break;
33e90275 412 case NSMouseExited :
f1f15003
KO
413 wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
414 break;
33e90275
SC
415 case NSLeftMouseDragged :
416 case NSRightMouseDragged :
417 case NSOtherMouseDragged :
418 case NSMouseMoved :
419 wxevent.SetEventType( wxEVT_MOTION ) ;
420 break;
421 default :
422 break ;
423 }
424}
425
426@implementation wxNSView
427
4dd9fdf8
SC
428+ (void)initialize
429{
430 static BOOL initialized = NO;
431 if (!initialized)
432 {
433 initialized = YES;
434 wxOSXCocoaClassAddWXMethods( self );
435 }
436}
437
438- (BOOL) canBecomeKeyView
439{
440 return YES;
441}
442
fc099495
KO
443- (void) setTrackingTag: (NSTrackingRectTag)tag
444{
445 rectTag = tag;
446}
447
448- (NSTrackingRectTag) trackingTag
449{
450 return rectTag;
451}
452
4dd9fdf8
SC
453@end // wxNSView
454
455//
456// event handlers
457//
458
459#if wxUSE_DRAG_AND_DROP
460
461// see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
462// for details on the NSPasteboard -> PasteboardRef conversion
463
464NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
465{
466 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
467 if (impl == NULL)
468 return NSDragOperationNone;
469
470 return impl->draggingEntered(sender, self, _cmd);
471}
472
473void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
474{
475 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
476 if (impl == NULL)
477 return ;
478
479 return impl->draggingExited(sender, self, _cmd);
480}
481
482NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
483{
484 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
485 if (impl == NULL)
486 return NSDragOperationNone;
487
488 return impl->draggingUpdated(sender, self, _cmd);
489}
490
491BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
492{
493 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
494 if (impl == NULL)
495 return NSDragOperationNone;
496
497 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
498}
499
500void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
501{
502 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
503 if (impl == NULL)
504 return;
505
506 impl->mouseEvent(event, self, _cmd);
507}
508
509void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
510{
511 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
512 if (impl == NULL)
513 return;
514
515 impl->keyEvent(event, self, _cmd);
516}
517
f0e0116e
KO
518void wxOSX_insertText(NSView* self, SEL _cmd, NSString* text)
519{
520 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
521 if (impl == NULL)
522 return;
523
524 impl->insertText(text, self, _cmd);
525}
526
4dd9fdf8
SC
527BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
528{
529 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
530 if (impl == NULL)
531 return NO;
532
533 return impl->performKeyEquivalent(event, self, _cmd);
534}
535
536BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
537{
538 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
539 if (impl == NULL)
540 return NO;
541
542 return impl->becomeFirstResponder(self, _cmd);
543}
544
545BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
546{
547 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
548 if (impl == NULL)
549 return NO;
550
551 return impl->resignFirstResponder(self, _cmd);
552}
553
554void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
555{
556 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
557 if (impl == NULL)
558 return;
559
560 impl->resetCursorRects(self, _cmd);
561}
562
563BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
564{
565 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
566 if (impl == NULL)
567 return NO;
568
569 return impl->isFlipped(self, _cmd) ? YES:NO;
570}
571
572void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
573{
574 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
575 if (impl == NULL)
576 return;
577
578 return impl->drawRect(&rect, self, _cmd);
579}
580
e32090ba 581void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
4dd9fdf8
SC
582{
583 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
584 if (impl == NULL)
585 return;
586
e32090ba 587 impl->controlAction(self, _cmd, sender);
4dd9fdf8
SC
588}
589
e32090ba 590void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
4dd9fdf8
SC
591{
592 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
593 if (impl == NULL)
594 return;
595
e32090ba 596 impl->controlDoubleAction(self, _cmd, sender);
4dd9fdf8 597}
dbeddfb9 598
4dd9fdf8 599unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget slf, void *_cmd)
33e90275 600{
4dd9fdf8
SC
601 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
602 NSPasteboard *pboard = [sender draggingPasteboard];
603 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
604
605 wxWindow* wxpeer = GetWXPeer();
606 if ( wxpeer == NULL )
607 return NSDragOperationNone;
608
609 wxDropTarget* target = wxpeer->GetDropTarget();
610 if ( target == NULL )
611 return NSDragOperationNone;
612
613 wxDragResult result = wxDragNone;
614 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
615
616 if ( sourceDragMask & NSDragOperationLink )
617 result = wxDragLink;
618 else if ( sourceDragMask & NSDragOperationCopy )
619 result = wxDragCopy;
620 else if ( sourceDragMask & NSDragOperationMove )
621 result = wxDragMove;
622
623 PasteboardRef pboardRef;
624 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
625 target->SetCurrentDragPasteboard(pboardRef);
626 result = target->OnEnter(pt.x, pt.y, result);
627 CFRelease(pboardRef);
628
629 NSDragOperation nsresult = NSDragOperationNone;
630 switch (result )
33e90275 631 {
4dd9fdf8
SC
632 case wxDragLink:
633 nsresult = NSDragOperationLink;
634 case wxDragMove:
635 nsresult = NSDragOperationMove;
636 case wxDragCopy:
637 nsresult = NSDragOperationCopy;
638 default :
639 break;
640 }
641 return nsresult;
642}
33e90275 643
4dd9fdf8
SC
644void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget slf, void *_cmd)
645{
646 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
647 NSPasteboard *pboard = [sender draggingPasteboard];
648
649 wxWindow* wxpeer = GetWXPeer();
650 if ( wxpeer == NULL )
651 return;
652
653 wxDropTarget* target = wxpeer->GetDropTarget();
654 if ( target == NULL )
655 return;
656
657 PasteboardRef pboardRef;
658 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
659 target->SetCurrentDragPasteboard(pboardRef);
660 target->OnLeave();
661 CFRelease(pboardRef);
662 }
663
664unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget slf, void *_cmd)
665{
666 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
667 NSPasteboard *pboard = [sender draggingPasteboard];
668 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
dbeddfb9 669
4dd9fdf8
SC
670 wxWindow* wxpeer = GetWXPeer();
671 if ( wxpeer == NULL )
672 return NSDragOperationNone;
673
674 wxDropTarget* target = wxpeer->GetDropTarget();
675 if ( target == NULL )
676 return NSDragOperationNone;
677
678 wxDragResult result = wxDragNone;
679 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
680
681 if ( sourceDragMask & NSDragOperationLink )
682 result = wxDragLink;
683 else if ( sourceDragMask & NSDragOperationCopy )
684 result = wxDragCopy;
685 else if ( sourceDragMask & NSDragOperationMove )
686 result = wxDragMove;
687
688 PasteboardRef pboardRef;
689 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
690 target->SetCurrentDragPasteboard(pboardRef);
691 result = target->OnDragOver(pt.x, pt.y, result);
692 CFRelease(pboardRef);
693
694 NSDragOperation nsresult = NSDragOperationNone;
695 switch (result )
696 {
697 case wxDragLink:
698 nsresult = NSDragOperationLink;
699 case wxDragMove:
700 nsresult = NSDragOperationMove;
701 case wxDragCopy:
702 nsresult = NSDragOperationCopy;
703 default :
704 break;
705 }
706 return nsresult;
707}
708
709bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget slf, void *_cmd)
710{
711 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
dbeddfb9 712
4dd9fdf8
SC
713 NSPasteboard *pboard = [sender draggingPasteboard];
714 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
715
716 wxWindow* wxpeer = GetWXPeer();
717 wxDropTarget* target = wxpeer->GetDropTarget();
718 wxDragResult result = wxDragNone;
719 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
720
721 if ( sourceDragMask & NSDragOperationLink )
722 result = wxDragLink;
723 else if ( sourceDragMask & NSDragOperationCopy )
724 result = wxDragCopy;
725 else if ( sourceDragMask & NSDragOperationMove )
726 result = wxDragMove;
727
728 PasteboardRef pboardRef;
729 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
730 target->SetCurrentDragPasteboard(pboardRef);
731 result = target->OnData(pt.x, pt.y, result);
732 CFRelease(pboardRef);
733
734 return result != wxDragNone;
735}
736
737#endif
738
63a6419c 739typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event);
4dd9fdf8
SC
740typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
741typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
742typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
743typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
744typedef BOOL (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
745
746void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
747{
748 if ( !DoHandleMouseEvent(event) )
749 {
750 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
751 superimpl(slf, (SEL)_cmd, event);
752 }
753}
754
755void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
756{
f0e0116e 757 if ( [[slf window] firstResponder] != slf || !DoHandleKeyEvent(event) )
4dd9fdf8
SC
758 {
759 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
760 superimpl(slf, (SEL)_cmd, event);
761 }
762}
763
f0e0116e 764void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
4dd9fdf8 765{
f0e0116e 766 if (m_lastKeyDownEvent && !DoHandleCharEvent(m_lastKeyDownEvent, text) )
4dd9fdf8 767 {
63a6419c
KO
768 wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
769 superimpl(slf, (SEL)_cmd, text);
4dd9fdf8 770 }
f0e0116e
KO
771 m_lastKeyDownEvent = NULL;
772}
4dd9fdf8 773
f0e0116e
KO
774
775bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
776{
777 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
778 return superimpl(slf, (SEL)_cmd, event);
4dd9fdf8
SC
779}
780
781bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
782{
783 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
784 BOOL r = superimpl(slf, (SEL)_cmd);
785 if ( r )
786 DoNotifyFocusEvent( true );
787 return r;
788}
789
790bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
791{
792 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
793 BOOL r = superimpl(slf, (SEL)_cmd);
794 if ( r )
795 DoNotifyFocusEvent( false );
796 return r;
797}
798
799void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
800{
801 wxWindow* wxpeer = GetWXPeer();
802 if ( wxpeer )
803 {
804 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
805 if (cursor == NULL)
dbeddfb9 806 {
4dd9fdf8
SC
807 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
808 superimpl(slf, (SEL)_cmd);
dbeddfb9 809 }
4dd9fdf8 810 else
57c0a8ac 811 {
4dd9fdf8
SC
812 [slf addCursorRect: [slf bounds]
813 cursor: cursor];
57c0a8ac 814 }
4dd9fdf8
SC
815 }
816}
817
818bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd)
819{
820 return m_isFlipped;
821}
33e90275 822
4dd9fdf8
SC
823
824#define OSX_DEBUG_DRAWING 0
825
826void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd)
827{
828 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
829 CGContextSaveGState( context );
830
831#if OSX_DEBUG_DRAWING
832 CGContextBeginPath( context );
833 CGContextMoveToPoint(context, 0, 0);
834 NSRect bounds = [self bounds];
835 CGContextAddLineToPoint(context, 10, 0);
836 CGContextMoveToPoint(context, 0, 0);
837 CGContextAddLineToPoint(context, 0, 10);
838 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
839 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
840 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
841 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
842 CGContextClosePath( context );
843 CGContextStrokePath(context);
844#endif
845
846 if ( !m_isFlipped )
847 {
848 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
849 CGContextScaleCTM( context, 1, -1 );
850 }
851
852 wxRegion updateRgn;
853 const NSRect *rects;
854 NSInteger count;
855
856 [slf getRectsBeingDrawn:&rects count:&count];
857 for ( int i = 0 ; i < count ; ++i )
858 {
859 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
860 }
861
862 wxWindow* wxpeer = GetWXPeer();
863 wxpeer->GetUpdateRegion() = updateRgn;
864 wxpeer->MacSetCGContextRef( context );
865
09489833 866 bool handled = wxpeer->MacDoRedraw( 0 );
4dd9fdf8
SC
867
868 CGContextRestoreGState( context );
09489833
SC
869
870 CGContextSaveGState( context );
4dd9fdf8
SC
871 if ( !handled )
872 {
873 // call super
874 SEL _cmd = @selector(drawRect:);
875 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
876 superimpl(slf, _cmd, *(NSRect*)rect);
09489833
SC
877 CGContextRestoreGState( context );
878 CGContextSaveGState( context );
33e90275 879 }
09489833 880 wxpeer->MacPaintChildrenBorders();
15fc716c 881 wxpeer->MacSetCGContextRef( NULL );
09489833 882 CGContextRestoreGState( context );
33e90275
SC
883}
884
e32090ba 885void wxWidgetCocoaImpl::controlAction( WXWidget slf, void *_cmd, void *sender)
4dd9fdf8
SC
886{
887 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
888 if ( wxpeer )
889 wxpeer->OSXHandleClicked(0);
890}
33e90275 891
e32090ba 892void wxWidgetCocoaImpl::controlDoubleAction( WXWidget slf, void *_cmd, void *sender)
524c47aa 893{
524c47aa 894}
33e90275 895
4dd9fdf8
SC
896//
897
898#if OBJC_API_VERSION >= 2
899
900#define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
901 class_addMethod(c, s, i, t );
902
903#else
904
905#define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
906 { s, t, i },
907
908#endif
909
910void wxOSXCocoaClassAddWXMethods(Class c)
911{
912
913#if OBJC_API_VERSION < 2
914 static objc_method wxmethods[] =
915 {
916#endif
917
918 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
919 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
920 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
921
922 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
923 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
924 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
925
926 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
927
928 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
929 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
930 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
931
932 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
933 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
934 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
935
936 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
937 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
938 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
f0e0116e
KO
939
940 wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
4dd9fdf8
SC
941
942 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" )
943
4dd9fdf8
SC
944 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
945 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
946 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
947
948 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
949 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
950
e32090ba
SC
951 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
952 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
4dd9fdf8
SC
953
954#if wxUSE_DRAG_AND_DROP
955 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
956 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
957 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
958 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
959#endif
960
961#if OBJC_API_VERSION < 2
962 } ;
963 static int method_count = WXSIZEOF( wxmethods );
964 static objc_method_list *wxmethodlist = NULL;
965 if ( wxmethodlist == NULL )
966 {
967 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
968 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
969 wxmethodlist->method_count = method_count;
970 wxmethodlist->obsolete = 0;
971 }
972 class_addMethods( c, wxmethodlist );
973#endif
974}
975
976//
977// C++ implementation class
978//
33e90275
SC
979
980IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
981
982wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
4dd9fdf8 983 wxWidgetImpl( peer, isRootControl )
33e90275 984{
4dd9fdf8
SC
985 Init();
986 m_osxView = w;
33e90275
SC
987}
988
989wxWidgetCocoaImpl::wxWidgetCocoaImpl()
990{
4dd9fdf8 991 Init();
33e90275
SC
992}
993
994void wxWidgetCocoaImpl::Init()
995{
996 m_osxView = NULL;
4dd9fdf8 997 m_isFlipped = true;
f0e0116e 998 m_lastKeyDownEvent = NULL;
33e90275
SC
999}
1000
1001wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
1002{
4dd9fdf8
SC
1003 RemoveAssociations( this );
1004
dbeddfb9
SC
1005 if ( !IsRootControl() )
1006 {
1007 NSView *sv = [m_osxView superview];
1008 if ( sv != nil )
1009 [m_osxView removeFromSuperview];
1010 }
33e90275
SC
1011 [m_osxView release];
1012}
1013
1014bool wxWidgetCocoaImpl::IsVisible() const
1015{
1016 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
1017}
1018
dbeddfb9
SC
1019void wxWidgetCocoaImpl::SetVisibility( bool visible )
1020{
1021 [m_osxView setHidden:(visible ? NO:YES)];
1022}
1023
33e90275
SC
1024void wxWidgetCocoaImpl::Raise()
1025{
1026}
1027
1028void wxWidgetCocoaImpl::Lower()
1029{
1030}
1031
1032void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
1033{
54f11060
SC
1034#if 1
1035 SetNeedsDisplay() ;
1036#else
1037 // We should do something like this, but it wasn't working in 10.4.
1038 if (GetNeedsDisplay() )
1039 {
1040 SetNeedsDisplay() ;
1041 }
1042 NSRect r = wxToNSRect( [m_osxView superview], *rect );
1043 NSSize offset = NSMakeSize((float)dx, (float)dy);
1044 [m_osxView scrollRect:r by:offset];
1045#endif
33e90275
SC
1046}
1047
1048void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
1049{
0c530e5a
SC
1050 wxWindowMac* parent = GetWXPeer()->GetParent();
1051 // under Cocoa we might have a contentView in the wxParent to which we have to
1052 // adjust the coordinates
1053 if (parent)
1054 {
57c0a8ac
SC
1055 int cx = 0,cy = 0,cw = 0,ch = 0;
1056 if ( parent->GetPeer() )
1057 {
1058 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
1059 x -= cx;
1060 y -= cy;
1061 }
0c530e5a 1062 }
33e90275
SC
1063 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
1064 [m_osxView setFrame:r];
fc099495 1065
41af81ea 1066 if ([m_osxView respondsToSelector:@selector(trackingTag)] )
fc099495
KO
1067 {
1068 if ( [(wxNSView*)m_osxView trackingTag] )
1069 [m_osxView removeTrackingRect: [(wxNSView*)m_osxView trackingTag]];
1070
1071 [(wxNSView*)m_osxView setTrackingTag: [m_osxView addTrackingRect: [m_osxView bounds] owner: m_osxView userData: nil assumeInside: NO]];
1072 }
33e90275
SC
1073}
1074
1075void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1076{
1077 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1078 x = r.GetLeft();
1079 y = r.GetTop();
1080}
1081
1082void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
1083{
1084 NSRect rect = [m_osxView frame];
1085 width = rect.size.width;
1086 height = rect.size.height;
1087}
1088
dbeddfb9 1089void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
33e90275 1090{
0c530e5a
SC
1091 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1092 {
1093 NSView* cv = [m_osxView contentView];
1094
1095 NSRect bounds = [m_osxView bounds];
1096 NSRect rect = [cv frame];
1097
1098 int y = rect.origin.y;
1099 int x = rect.origin.x;
1100 if ( ![ m_osxView isFlipped ] )
1101 y = bounds.size.height - (rect.origin.y + rect.size.height);
1102 left = x;
1103 top = y;
1104 width = rect.size.width;
1105 height = rect.size.height;
1106 }
1107 else
1108 {
1109 left = top = 0;
1110 GetSize( width, height );
1111 }
33e90275
SC
1112}
1113
1114void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1115{
1116 if ( where )
1117 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1118 else
1119 [m_osxView setNeedsDisplay:YES];
1120}
1121
1122bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1123{
1124 return [m_osxView needsDisplay];
1125}
1126
dbeddfb9 1127bool wxWidgetCocoaImpl::CanFocus() const
33e90275 1128{
dbeddfb9 1129 return [m_osxView canBecomeKeyView] == YES;
33e90275
SC
1130}
1131
1132bool wxWidgetCocoaImpl::HasFocus() const
1133{
dbeddfb9 1134 return ( [[m_osxView window] firstResponder] == m_osxView );
33e90275
SC
1135}
1136
1137bool wxWidgetCocoaImpl::SetFocus()
1138{
dbeddfb9
SC
1139 if ( [m_osxView canBecomeKeyView] == NO )
1140 return false;
1141
1142 [[m_osxView window] makeFirstResponder: m_osxView] ;
1143 return true;
33e90275
SC
1144}
1145
1146
1147void wxWidgetCocoaImpl::RemoveFromParent()
1148{
1149 [m_osxView removeFromSuperview];
1150}
1151
1152void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1153{
1154 NSView* container = parent->GetWXWidget() ;
1155 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1156 [container addSubview:m_osxView];
1157}
1158
dbeddfb9
SC
1159void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1160{
1161 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1162}
1163
1164void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1165{
1166 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1167 {
1168 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1169 [m_osxView setTitle:cf.AsNSString()];
1170 }
e32090ba
SC
1171 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1172 {
1173 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1174 [m_osxView setStringValue:cf.AsNSString()];
1175 }
dbeddfb9
SC
1176}
1177
1178
1179void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1180{
1181 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1182 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1183 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1184}
1185
1186wxInt32 wxWidgetCocoaImpl::GetValue() const
1187{
1188 return [(NSControl*)m_osxView intValue];
1189}
1190
1191void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1192{
1193 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1194 {
1195 [m_osxView setIntValue:v];
1196 }
1197 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1198 {
1199 [m_osxView setFloatValue:(double)v];
1200 }
1201 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1202 {
1203 [m_osxView setDoubleValue:(double)v];
1204 }
1205}
1206
1207void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1208{
1209 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1210 {
1211 [m_osxView setMinValue:(double)v];
1212 }
1213}
1214
1215void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1216{
1217 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1218 {
1219 [m_osxView setMaxValue:(double)v];
1220 }
1221}
1222
19c7ac3d
SC
1223wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1224{
1225 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1226 {
1227 return [m_osxView minValue];
1228 }
1229 return 0;
1230}
1231
1232wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1233{
1234 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1235 {
1236 return [m_osxView maxValue];
1237 }
1238 return 0;
1239}
1240
dbeddfb9
SC
1241void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1242{
1243 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1244 {
1245 [m_osxView setImage:bitmap.GetNSImage()];
1246 }
1247}
1248
1249void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
1250{
1251 // implementation in subclass
1252}
1253
1254void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1255{
1256 r->x = r->y = r->width = r->height = 0;
1257// if ( [m_osxView isKindOfClass:[NSControl class]] )
1258 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1259 {
1260 NSRect former = [m_osxView frame];
1261 [m_osxView sizeToFit];
1262 NSRect best = [m_osxView frame];
1263 [m_osxView setFrame:former];
1264 r->width = best.size.width;
1265 r->height = best.size.height;
1266 }
1267}
1268
1269bool wxWidgetCocoaImpl::IsEnabled() const
1270{
0f9b48d1
SC
1271 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1272 return [m_osxView isEnabled];
1273 return true;
dbeddfb9
SC
1274}
1275
1276void wxWidgetCocoaImpl::Enable( bool enable )
1277{
0f9b48d1
SC
1278 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1279 [m_osxView setEnabled:enable];
dbeddfb9
SC
1280}
1281
1282void wxWidgetCocoaImpl::PulseGauge()
1283{
1284}
1285
1286void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
1287{
1288}
33e90275 1289
0f9b48d1
SC
1290void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1291{
1292 NSControlSize size = NSRegularControlSize;
1293
1294 switch ( variant )
1295 {
1296 case wxWINDOW_VARIANT_NORMAL :
1297 size = NSRegularControlSize;
1298 break ;
1299
1300 case wxWINDOW_VARIANT_SMALL :
1301 size = NSSmallControlSize;
1302 break ;
1303
1304 case wxWINDOW_VARIANT_MINI :
1305 size = NSMiniControlSize;
1306 break ;
1307
1308 case wxWINDOW_VARIANT_LARGE :
1309 size = NSRegularControlSize;
1310 break ;
1311
1312 default:
1313 wxFAIL_MSG(_T("unexpected window variant"));
1314 break ;
1315 }
1316 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1317 [m_osxView setControlSize:size];
1318}
1319
1e181c7a
SC
1320void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
1321{
1322 // TODO
1323}
1324
c4825ef7
SC
1325void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1326{
4dd9fdf8
SC
1327 WXWidget c = control ? control : (WXWidget) m_osxView;
1328 wxWidgetImpl::Associate( c, this ) ;
1329 if ([c respondsToSelector:@selector(setAction:)])
1330 {
1331 [c setTarget: c];
e32090ba 1332 [c setAction: @selector(controlAction:)];
4dd9fdf8
SC
1333 if ([c respondsToSelector:@selector(setDoubleAction:)])
1334 {
e32090ba 1335 [c setDoubleAction: @selector(controlDoubleAction:)];
4dd9fdf8
SC
1336 }
1337
1338 }
c4825ef7
SC
1339}
1340
f0e0116e
KO
1341bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
1342{
1343 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1344 SetupKeyEvent( wxevent, event, text );
f0e0116e
KO
1345
1346 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1347}
1348
ffad7b0d
SC
1349bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1350{
1351 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1352 SetupKeyEvent( wxevent, event );
f0e0116e
KO
1353
1354 bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
19c7ac3d 1355
f0e0116e
KO
1356 // this will fire higher level events, like insertText, to help
1357 // us handle EVT_CHAR, etc.
1358 if ([event type] == NSKeyDown)
1359 {
1360 m_lastKeyDownEvent = event;
1361 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
1362 }
f0e0116e 1363 return result;
ffad7b0d
SC
1364}
1365
b466e85a 1366bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
4850cc8b
SC
1367{
1368 NSPoint clickLocation;
1369 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1370 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1371 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1372 SetupMouseEvent( wxevent , event ) ;
1373 wxevent.m_x = pt.x;
1374 wxevent.m_y = pt.y;
b466e85a
SC
1375
1376 return GetWXPeer()->HandleWindowEvent(wxevent);
4850cc8b
SC
1377}
1378
0c530e5a
SC
1379void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
1380{
1381 wxWindow* thisWindow = GetWXPeer();
1382 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1383 {
1384 thisWindow->MacInvalidateBorders();
1385 }
1386
1387 if ( receivedFocus )
1388 {
1389 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
1390 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1391 thisWindow->HandleWindowEvent(eventFocus);
1392
1393#if wxUSE_CARET
1394 if ( thisWindow->GetCaret() )
1395 thisWindow->GetCaret()->OnSetFocus();
1396#endif
1397
1398 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1399 event.SetEventObject(thisWindow);
1400 // TODO how to find out the targetFocusWindow ?
1401 // event.SetWindow(targetFocusWindow);
1402 thisWindow->HandleWindowEvent(event) ;
1403 }
1404 else // !receivedFocuss
1405 {
1406#if wxUSE_CARET
1407 if ( thisWindow->GetCaret() )
1408 thisWindow->GetCaret()->OnKillFocus();
1409#endif
1410
1411 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
1412
1413 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1414 event.SetEventObject(thisWindow);
1415 // TODO how to find out the targetFocusWindow ?
1416 // event.SetWindow(targetFocusWindow);
1417 thisWindow->HandleWindowEvent(event) ;
1418 }
1419}
1420
54f11060
SC
1421void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1422{
1423 NSPoint location = [NSEvent mouseLocation];
1424 location = [[m_osxView window] convertScreenToBase:location];
1425 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1426
1427 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1428 {
1429 [(NSCursor*)cursor.GetHCURSOR() set];
1430 }
1431 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1432}
1433
1434void wxWidgetCocoaImpl::CaptureMouse()
1435{
1436 [[m_osxView window] disableCursorRects];
1437}
1438
1439void wxWidgetCocoaImpl::ReleaseMouse()
1440{
1441 [[m_osxView window] enableCursorRects];
1442}
4850cc8b 1443
4dd9fdf8
SC
1444void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1445{
1446 m_isFlipped = flipped;
1447}
1448
33e90275
SC
1449//
1450// Factory methods
1451//
1452
dbeddfb9
SC
1453wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1454 long style, long extraStyle)
33e90275 1455{
dbeddfb9 1456 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
33e90275 1457 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
4dd9fdf8
SC
1458
1459 // temporary hook for dnd
1460 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1461 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1462
33e90275 1463 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
33e90275
SC
1464 return c;
1465}
1466
1467wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1468{
1469 NSWindow* tlw = now->GetWXWindow();
1470 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1471 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
7e06ee6d 1472 c->InstallEventHandler();
33e90275
SC
1473 [tlw setContentView:v];
1474 return c;
1475}