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