]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/window.mm
wxBusyInfo now uses wxGenericStaticText under GTK+, more Refresh/Update updates
[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:
b480b80a
KO
137 int intchar = [s characterAtIndex: 0];
138 if ( intchar >= NSF1FunctionKey && intchar <= NSF24FunctionKey )
63a6419c
KO
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;
33e90275
SC
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;
dbeddfb9 275 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
54f11060
SC
276
277 UInt32 mouseChord = 0;
54f11060
SC
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
33e90275
SC
295 // a control click is interpreted as a right click
296 bool thisButtonIsFakeRight = false ;
54f11060 297 if ( button == 0 && (modifiers & NSControlKeyMask) )
33e90275 298 {
54f11060 299 button = 1 ;
33e90275
SC
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 ;
54f11060 306
33e90275
SC
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
54f11060 309 switch (eventType)
33e90275 310 {
54f11060
SC
311 case NSLeftMouseDown :
312 case NSRightMouseDown :
313 case NSOtherMouseDown :
314 g_lastButton = button ;
315 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
316 break;
317 }
33e90275
SC
318
319 if ( button == 0 )
320 {
321 g_lastButton = 0 ;
322 g_lastButtonWasFakeRight = false ;
323 }
54f11060 324 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
33e90275
SC
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
33e90275 341 // translate into wx types
33e90275
SC
342 switch (eventType)
343 {
344 case NSLeftMouseDown :
345 case NSRightMouseDown :
346 case NSOtherMouseDown :
08c1b134 347 clickCount = [nsEvent clickCount];
33e90275
SC
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 :
08c1b134 370 clickCount = [nsEvent clickCount];
33e90275
SC
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 ) ;
e32090ba 393 wxevent.m_wheelDelta = 10;
33e90275 394 wxevent.m_linesPerAction = 1;
e32090ba 395 NSLog(@"deltaX %f, deltaY %f",[nsEvent deltaX], [nsEvent deltaY]);
4800ca79
SC
396 if ( abs([nsEvent deltaX]) > abs([nsEvent deltaY]) )
397 {
33e90275 398 wxevent.m_wheelAxis = 1;
e32090ba 399 wxevent.m_wheelRotation = [nsEvent deltaX] * 10.0;
4800ca79
SC
400 }
401 else
402 {
e32090ba 403 wxevent.m_wheelRotation = [nsEvent deltaY] * 10.0;
4800ca79 404 }
33e90275
SC
405 }
406 break ;
407
408 case NSMouseEntered :
f1f15003
KO
409 wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
410 break;
33e90275 411 case NSMouseExited :
f1f15003
KO
412 wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
413 break;
33e90275
SC
414 case NSLeftMouseDragged :
415 case NSRightMouseDragged :
416 case NSOtherMouseDragged :
417 case NSMouseMoved :
418 wxevent.SetEventType( wxEVT_MOTION ) ;
419 break;
420 default :
421 break ;
422 }
08c1b134
KO
423
424 wxevent.m_clickCount = clickCount;
425
33e90275
SC
426}
427
428@implementation wxNSView
429
4dd9fdf8
SC
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
fc099495
KO
445- (void) setTrackingTag: (NSTrackingRectTag)tag
446{
447 rectTag = tag;
448}
449
450- (NSTrackingRectTag) trackingTag
451{
452 return rectTag;
453}
454
4dd9fdf8
SC
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
466NSDragOperation 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
475void 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
484NSDragOperation 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
493BOOL 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
502void 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
511void 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
f0e0116e
KO
520void 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
4dd9fdf8
SC
529BOOL 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
538BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
539{
540 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
541 if (impl == NULL)
542 return NO;
543
544 return impl->becomeFirstResponder(self, _cmd);
545}
546
547BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
548{
549 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
550 if (impl == NULL)
551 return NO;
552
553 return impl->resignFirstResponder(self, _cmd);
554}
555
556void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
557{
558 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
559 if (impl == NULL)
560 return;
561
562 impl->resetCursorRects(self, _cmd);
563}
564
565BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
566{
567 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
568 if (impl == NULL)
569 return NO;
570
571 return impl->isFlipped(self, _cmd) ? YES:NO;
572}
573
574void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
575{
576 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
577 if (impl == NULL)
578 return;
579
580 return impl->drawRect(&rect, self, _cmd);
581}
582
e32090ba 583void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
4dd9fdf8
SC
584{
585 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
586 if (impl == NULL)
587 return;
588
e32090ba 589 impl->controlAction(self, _cmd, sender);
4dd9fdf8
SC
590}
591
e32090ba 592void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
4dd9fdf8
SC
593{
594 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
595 if (impl == NULL)
596 return;
597
e32090ba 598 impl->controlDoubleAction(self, _cmd, sender);
4dd9fdf8 599}
dbeddfb9 600
4dd9fdf8 601unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget slf, void *_cmd)
33e90275 602{
4dd9fdf8
SC
603 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
604 NSPasteboard *pboard = [sender draggingPasteboard];
605 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
606
607 wxWindow* wxpeer = GetWXPeer();
608 if ( wxpeer == NULL )
609 return NSDragOperationNone;
610
611 wxDropTarget* target = wxpeer->GetDropTarget();
612 if ( target == NULL )
613 return NSDragOperationNone;
614
615 wxDragResult result = wxDragNone;
616 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
617
618 if ( sourceDragMask & NSDragOperationLink )
619 result = wxDragLink;
620 else if ( sourceDragMask & NSDragOperationCopy )
621 result = wxDragCopy;
622 else if ( sourceDragMask & NSDragOperationMove )
623 result = wxDragMove;
624
625 PasteboardRef pboardRef;
626 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
627 target->SetCurrentDragPasteboard(pboardRef);
628 result = target->OnEnter(pt.x, pt.y, result);
629 CFRelease(pboardRef);
630
631 NSDragOperation nsresult = NSDragOperationNone;
632 switch (result )
33e90275 633 {
4dd9fdf8
SC
634 case wxDragLink:
635 nsresult = NSDragOperationLink;
636 case wxDragMove:
637 nsresult = NSDragOperationMove;
638 case wxDragCopy:
639 nsresult = NSDragOperationCopy;
640 default :
641 break;
642 }
643 return nsresult;
644}
33e90275 645
4dd9fdf8
SC
646void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget slf, void *_cmd)
647{
648 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
649 NSPasteboard *pboard = [sender draggingPasteboard];
650
651 wxWindow* wxpeer = GetWXPeer();
652 if ( wxpeer == NULL )
653 return;
654
655 wxDropTarget* target = wxpeer->GetDropTarget();
656 if ( target == NULL )
657 return;
658
659 PasteboardRef pboardRef;
660 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
661 target->SetCurrentDragPasteboard(pboardRef);
662 target->OnLeave();
663 CFRelease(pboardRef);
664 }
665
666unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget slf, void *_cmd)
667{
668 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
669 NSPasteboard *pboard = [sender draggingPasteboard];
670 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
dbeddfb9 671
4dd9fdf8
SC
672 wxWindow* wxpeer = GetWXPeer();
673 if ( wxpeer == NULL )
674 return NSDragOperationNone;
675
676 wxDropTarget* target = wxpeer->GetDropTarget();
677 if ( target == NULL )
678 return NSDragOperationNone;
679
680 wxDragResult result = wxDragNone;
681 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
682
683 if ( sourceDragMask & NSDragOperationLink )
684 result = wxDragLink;
685 else if ( sourceDragMask & NSDragOperationCopy )
686 result = wxDragCopy;
687 else if ( sourceDragMask & NSDragOperationMove )
688 result = wxDragMove;
689
690 PasteboardRef pboardRef;
691 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
692 target->SetCurrentDragPasteboard(pboardRef);
693 result = target->OnDragOver(pt.x, pt.y, result);
694 CFRelease(pboardRef);
695
696 NSDragOperation nsresult = NSDragOperationNone;
697 switch (result )
698 {
699 case wxDragLink:
700 nsresult = NSDragOperationLink;
701 case wxDragMove:
702 nsresult = NSDragOperationMove;
703 case wxDragCopy:
704 nsresult = NSDragOperationCopy;
705 default :
706 break;
707 }
708 return nsresult;
709}
710
711bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget slf, void *_cmd)
712{
713 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
dbeddfb9 714
4dd9fdf8
SC
715 NSPasteboard *pboard = [sender draggingPasteboard];
716 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
717
718 wxWindow* wxpeer = GetWXPeer();
719 wxDropTarget* target = wxpeer->GetDropTarget();
720 wxDragResult result = wxDragNone;
721 wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
722
723 if ( sourceDragMask & NSDragOperationLink )
724 result = wxDragLink;
725 else if ( sourceDragMask & NSDragOperationCopy )
726 result = wxDragCopy;
727 else if ( sourceDragMask & NSDragOperationMove )
728 result = wxDragMove;
729
730 PasteboardRef pboardRef;
731 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
732 target->SetCurrentDragPasteboard(pboardRef);
733 result = target->OnData(pt.x, pt.y, result);
734 CFRelease(pboardRef);
735
736 return result != wxDragNone;
737}
738
739#endif
740
63a6419c 741typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event);
4dd9fdf8
SC
742typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
743typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
744typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
745typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
746typedef BOOL (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
747
748void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
749{
750 if ( !DoHandleMouseEvent(event) )
751 {
752 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
753 superimpl(slf, (SEL)_cmd, event);
754 }
755}
756
757void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
758{
f0e0116e 759 if ( [[slf window] firstResponder] != slf || !DoHandleKeyEvent(event) )
4dd9fdf8
SC
760 {
761 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
762 superimpl(slf, (SEL)_cmd, event);
763 }
764}
765
f0e0116e 766void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
4dd9fdf8 767{
f0e0116e 768 if (m_lastKeyDownEvent && !DoHandleCharEvent(m_lastKeyDownEvent, text) )
4dd9fdf8 769 {
63a6419c
KO
770 wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
771 superimpl(slf, (SEL)_cmd, text);
4dd9fdf8 772 }
f0e0116e
KO
773 m_lastKeyDownEvent = NULL;
774}
4dd9fdf8 775
f0e0116e
KO
776
777bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
778{
779 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
780 return superimpl(slf, (SEL)_cmd, event);
4dd9fdf8
SC
781}
782
783bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
784{
785 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
786 BOOL r = superimpl(slf, (SEL)_cmd);
787 if ( r )
788 DoNotifyFocusEvent( true );
789 return r;
790}
791
792bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
793{
794 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
795 BOOL r = superimpl(slf, (SEL)_cmd);
796 if ( r )
797 DoNotifyFocusEvent( false );
798 return r;
799}
800
801void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
802{
803 wxWindow* wxpeer = GetWXPeer();
804 if ( wxpeer )
805 {
806 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
807 if (cursor == NULL)
dbeddfb9 808 {
4dd9fdf8
SC
809 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
810 superimpl(slf, (SEL)_cmd);
dbeddfb9 811 }
4dd9fdf8 812 else
57c0a8ac 813 {
4dd9fdf8
SC
814 [slf addCursorRect: [slf bounds]
815 cursor: cursor];
57c0a8ac 816 }
4dd9fdf8
SC
817 }
818}
819
820bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd)
821{
822 return m_isFlipped;
823}
33e90275 824
4dd9fdf8
SC
825
826#define OSX_DEBUG_DRAWING 0
827
828void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd)
829{
830 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
831 CGContextSaveGState( context );
832
833#if OSX_DEBUG_DRAWING
834 CGContextBeginPath( context );
835 CGContextMoveToPoint(context, 0, 0);
836 NSRect bounds = [self bounds];
837 CGContextAddLineToPoint(context, 10, 0);
838 CGContextMoveToPoint(context, 0, 0);
839 CGContextAddLineToPoint(context, 0, 10);
840 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
841 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
842 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
843 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
844 CGContextClosePath( context );
845 CGContextStrokePath(context);
846#endif
847
848 if ( !m_isFlipped )
849 {
850 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
851 CGContextScaleCTM( context, 1, -1 );
852 }
853
854 wxRegion updateRgn;
855 const NSRect *rects;
856 NSInteger count;
857
858 [slf getRectsBeingDrawn:&rects count:&count];
859 for ( int i = 0 ; i < count ; ++i )
860 {
861 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
862 }
863
864 wxWindow* wxpeer = GetWXPeer();
865 wxpeer->GetUpdateRegion() = updateRgn;
866 wxpeer->MacSetCGContextRef( context );
867
09489833 868 bool handled = wxpeer->MacDoRedraw( 0 );
4dd9fdf8
SC
869
870 CGContextRestoreGState( context );
09489833
SC
871
872 CGContextSaveGState( context );
4dd9fdf8
SC
873 if ( !handled )
874 {
875 // call super
876 SEL _cmd = @selector(drawRect:);
877 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
878 superimpl(slf, _cmd, *(NSRect*)rect);
09489833
SC
879 CGContextRestoreGState( context );
880 CGContextSaveGState( context );
33e90275 881 }
09489833 882 wxpeer->MacPaintChildrenBorders();
15fc716c 883 wxpeer->MacSetCGContextRef( NULL );
09489833 884 CGContextRestoreGState( context );
33e90275
SC
885}
886
e32090ba 887void wxWidgetCocoaImpl::controlAction( WXWidget slf, void *_cmd, void *sender)
4dd9fdf8
SC
888{
889 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
890 if ( wxpeer )
891 wxpeer->OSXHandleClicked(0);
892}
33e90275 893
e32090ba 894void wxWidgetCocoaImpl::controlDoubleAction( WXWidget slf, void *_cmd, void *sender)
524c47aa 895{
524c47aa 896}
33e90275 897
4dd9fdf8
SC
898//
899
900#if OBJC_API_VERSION >= 2
901
902#define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
903 class_addMethod(c, s, i, t );
904
905#else
906
907#define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
908 { s, t, i },
909
910#endif
911
912void wxOSXCocoaClassAddWXMethods(Class c)
913{
914
915#if OBJC_API_VERSION < 2
916 static objc_method wxmethods[] =
917 {
918#endif
919
920 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
921 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
922 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
923
924 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
925 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
926 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
927
928 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
929
930 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
931 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
932 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
933
934 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
935 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
936 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
937
938 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
939 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
940 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
f0e0116e
KO
941
942 wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
4dd9fdf8
SC
943
944 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" )
945
4dd9fdf8
SC
946 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
947 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
948 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
949
950 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
951 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
952
e32090ba
SC
953 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
954 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
4dd9fdf8
SC
955
956#if wxUSE_DRAG_AND_DROP
957 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
958 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
959 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
960 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
961#endif
962
963#if OBJC_API_VERSION < 2
964 } ;
965 static int method_count = WXSIZEOF( wxmethods );
966 static objc_method_list *wxmethodlist = NULL;
967 if ( wxmethodlist == NULL )
968 {
969 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
970 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
971 wxmethodlist->method_count = method_count;
972 wxmethodlist->obsolete = 0;
973 }
974 class_addMethods( c, wxmethodlist );
975#endif
976}
977
978//
979// C++ implementation class
980//
33e90275
SC
981
982IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
983
984wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
4dd9fdf8 985 wxWidgetImpl( peer, isRootControl )
33e90275 986{
4dd9fdf8
SC
987 Init();
988 m_osxView = w;
33e90275
SC
989}
990
991wxWidgetCocoaImpl::wxWidgetCocoaImpl()
992{
4dd9fdf8 993 Init();
33e90275
SC
994}
995
996void wxWidgetCocoaImpl::Init()
997{
998 m_osxView = NULL;
4dd9fdf8 999 m_isFlipped = true;
f0e0116e 1000 m_lastKeyDownEvent = NULL;
33e90275
SC
1001}
1002
1003wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
1004{
4dd9fdf8
SC
1005 RemoveAssociations( this );
1006
dbeddfb9
SC
1007 if ( !IsRootControl() )
1008 {
1009 NSView *sv = [m_osxView superview];
1010 if ( sv != nil )
1011 [m_osxView removeFromSuperview];
1012 }
33e90275
SC
1013 [m_osxView release];
1014}
1015
1016bool wxWidgetCocoaImpl::IsVisible() const
1017{
1018 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
1019}
1020
dbeddfb9
SC
1021void wxWidgetCocoaImpl::SetVisibility( bool visible )
1022{
1023 [m_osxView setHidden:(visible ? NO:YES)];
1024}
1025
33e90275
SC
1026void wxWidgetCocoaImpl::Raise()
1027{
1028}
1029
1030void wxWidgetCocoaImpl::Lower()
1031{
1032}
1033
1034void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
1035{
54f11060
SC
1036#if 1
1037 SetNeedsDisplay() ;
1038#else
1039 // We should do something like this, but it wasn't working in 10.4.
1040 if (GetNeedsDisplay() )
1041 {
1042 SetNeedsDisplay() ;
1043 }
1044 NSRect r = wxToNSRect( [m_osxView superview], *rect );
1045 NSSize offset = NSMakeSize((float)dx, (float)dy);
1046 [m_osxView scrollRect:r by:offset];
1047#endif
33e90275
SC
1048}
1049
1050void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
1051{
0c530e5a
SC
1052 wxWindowMac* parent = GetWXPeer()->GetParent();
1053 // under Cocoa we might have a contentView in the wxParent to which we have to
1054 // adjust the coordinates
1055 if (parent)
1056 {
57c0a8ac
SC
1057 int cx = 0,cy = 0,cw = 0,ch = 0;
1058 if ( parent->GetPeer() )
1059 {
1060 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
1061 x -= cx;
1062 y -= cy;
1063 }
0c530e5a 1064 }
33e90275
SC
1065 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
1066 [m_osxView setFrame:r];
fc099495 1067
41af81ea 1068 if ([m_osxView respondsToSelector:@selector(trackingTag)] )
fc099495
KO
1069 {
1070 if ( [(wxNSView*)m_osxView trackingTag] )
1071 [m_osxView removeTrackingRect: [(wxNSView*)m_osxView trackingTag]];
1072
1073 [(wxNSView*)m_osxView setTrackingTag: [m_osxView addTrackingRect: [m_osxView bounds] owner: m_osxView userData: nil assumeInside: NO]];
1074 }
33e90275
SC
1075}
1076
1077void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1078{
1079 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1080 x = r.GetLeft();
1081 y = r.GetTop();
1082}
1083
1084void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
1085{
1086 NSRect rect = [m_osxView frame];
1087 width = rect.size.width;
1088 height = rect.size.height;
1089}
1090
dbeddfb9 1091void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
33e90275 1092{
0c530e5a
SC
1093 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1094 {
1095 NSView* cv = [m_osxView contentView];
1096
1097 NSRect bounds = [m_osxView bounds];
1098 NSRect rect = [cv frame];
1099
1100 int y = rect.origin.y;
1101 int x = rect.origin.x;
1102 if ( ![ m_osxView isFlipped ] )
1103 y = bounds.size.height - (rect.origin.y + rect.size.height);
1104 left = x;
1105 top = y;
1106 width = rect.size.width;
1107 height = rect.size.height;
1108 }
1109 else
1110 {
1111 left = top = 0;
1112 GetSize( width, height );
1113 }
33e90275
SC
1114}
1115
1116void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1117{
1118 if ( where )
1119 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1120 else
1121 [m_osxView setNeedsDisplay:YES];
1122}
1123
1124bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1125{
1126 return [m_osxView needsDisplay];
1127}
1128
dbeddfb9 1129bool wxWidgetCocoaImpl::CanFocus() const
33e90275 1130{
dbeddfb9 1131 return [m_osxView canBecomeKeyView] == YES;
33e90275
SC
1132}
1133
1134bool wxWidgetCocoaImpl::HasFocus() const
1135{
dbeddfb9 1136 return ( [[m_osxView window] firstResponder] == m_osxView );
33e90275
SC
1137}
1138
1139bool wxWidgetCocoaImpl::SetFocus()
1140{
dbeddfb9
SC
1141 if ( [m_osxView canBecomeKeyView] == NO )
1142 return false;
1143
1144 [[m_osxView window] makeFirstResponder: m_osxView] ;
1145 return true;
33e90275
SC
1146}
1147
1148
1149void wxWidgetCocoaImpl::RemoveFromParent()
1150{
1151 [m_osxView removeFromSuperview];
1152}
1153
1154void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1155{
1156 NSView* container = parent->GetWXWidget() ;
1157 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1158 [container addSubview:m_osxView];
1159}
1160
dbeddfb9
SC
1161void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1162{
1163 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1164}
1165
1166void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1167{
1168 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1169 {
1170 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1171 [m_osxView setTitle:cf.AsNSString()];
1172 }
e32090ba
SC
1173 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1174 {
1175 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1176 [m_osxView setStringValue:cf.AsNSString()];
1177 }
dbeddfb9
SC
1178}
1179
1180
1181void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1182{
1183 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1184 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1185 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1186}
1187
1188wxInt32 wxWidgetCocoaImpl::GetValue() const
1189{
1190 return [(NSControl*)m_osxView intValue];
1191}
1192
1193void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1194{
1195 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1196 {
1197 [m_osxView setIntValue:v];
1198 }
1199 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1200 {
1201 [m_osxView setFloatValue:(double)v];
1202 }
1203 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1204 {
1205 [m_osxView setDoubleValue:(double)v];
1206 }
1207}
1208
1209void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1210{
1211 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1212 {
1213 [m_osxView setMinValue:(double)v];
1214 }
1215}
1216
1217void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1218{
1219 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1220 {
1221 [m_osxView setMaxValue:(double)v];
1222 }
1223}
1224
19c7ac3d
SC
1225wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1226{
1227 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1228 {
1229 return [m_osxView minValue];
1230 }
1231 return 0;
1232}
1233
1234wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1235{
1236 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1237 {
1238 return [m_osxView maxValue];
1239 }
1240 return 0;
1241}
1242
dbeddfb9
SC
1243void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1244{
1245 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1246 {
1247 [m_osxView setImage:bitmap.GetNSImage()];
1248 }
1249}
1250
1251void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
1252{
1253 // implementation in subclass
1254}
1255
1256void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1257{
1258 r->x = r->y = r->width = r->height = 0;
1259// if ( [m_osxView isKindOfClass:[NSControl class]] )
1260 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1261 {
1262 NSRect former = [m_osxView frame];
1263 [m_osxView sizeToFit];
1264 NSRect best = [m_osxView frame];
1265 [m_osxView setFrame:former];
1266 r->width = best.size.width;
1267 r->height = best.size.height;
1268 }
1269}
1270
1271bool wxWidgetCocoaImpl::IsEnabled() const
1272{
0f9b48d1
SC
1273 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1274 return [m_osxView isEnabled];
1275 return true;
dbeddfb9
SC
1276}
1277
1278void wxWidgetCocoaImpl::Enable( bool enable )
1279{
0f9b48d1
SC
1280 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1281 [m_osxView setEnabled:enable];
dbeddfb9
SC
1282}
1283
1284void wxWidgetCocoaImpl::PulseGauge()
1285{
1286}
1287
1288void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
1289{
1290}
33e90275 1291
0f9b48d1
SC
1292void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1293{
1294 NSControlSize size = NSRegularControlSize;
1295
1296 switch ( variant )
1297 {
1298 case wxWINDOW_VARIANT_NORMAL :
1299 size = NSRegularControlSize;
1300 break ;
1301
1302 case wxWINDOW_VARIANT_SMALL :
1303 size = NSSmallControlSize;
1304 break ;
1305
1306 case wxWINDOW_VARIANT_MINI :
1307 size = NSMiniControlSize;
1308 break ;
1309
1310 case wxWINDOW_VARIANT_LARGE :
1311 size = NSRegularControlSize;
1312 break ;
1313
1314 default:
1315 wxFAIL_MSG(_T("unexpected window variant"));
1316 break ;
1317 }
1318 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1319 [m_osxView setControlSize:size];
1320}
1321
1e181c7a
SC
1322void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
1323{
1324 // TODO
1325}
1326
c4825ef7
SC
1327void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1328{
4dd9fdf8
SC
1329 WXWidget c = control ? control : (WXWidget) m_osxView;
1330 wxWidgetImpl::Associate( c, this ) ;
1331 if ([c respondsToSelector:@selector(setAction:)])
1332 {
1333 [c setTarget: c];
e32090ba 1334 [c setAction: @selector(controlAction:)];
4dd9fdf8
SC
1335 if ([c respondsToSelector:@selector(setDoubleAction:)])
1336 {
e32090ba 1337 [c setDoubleAction: @selector(controlDoubleAction:)];
4dd9fdf8
SC
1338 }
1339
1340 }
c4825ef7
SC
1341}
1342
f0e0116e
KO
1343bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
1344{
1345 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1346 SetupKeyEvent( wxevent, event, text );
540f4445 1347 wxevent.SetEventObject(GetWXPeer());
f0e0116e
KO
1348
1349 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1350}
1351
ffad7b0d
SC
1352bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1353{
1354 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1355 SetupKeyEvent( wxevent, event );
540f4445 1356 wxevent.SetEventObject(GetWXPeer());
f0e0116e 1357 bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
19c7ac3d 1358
f0e0116e
KO
1359 // this will fire higher level events, like insertText, to help
1360 // us handle EVT_CHAR, etc.
1361 if ([event type] == NSKeyDown)
1362 {
1363 m_lastKeyDownEvent = event;
1364 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
1365 }
f0e0116e 1366 return result;
ffad7b0d
SC
1367}
1368
b466e85a 1369bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
4850cc8b
SC
1370{
1371 NSPoint clickLocation;
1372 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1373 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1374 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1375 SetupMouseEvent( wxevent , event ) ;
540f4445 1376 wxevent.SetEventObject(GetWXPeer());
4850cc8b
SC
1377 wxevent.m_x = pt.x;
1378 wxevent.m_y = pt.y;
b466e85a
SC
1379
1380 return GetWXPeer()->HandleWindowEvent(wxevent);
4850cc8b
SC
1381}
1382
0c530e5a
SC
1383void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
1384{
1385 wxWindow* thisWindow = GetWXPeer();
1386 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1387 {
1388 thisWindow->MacInvalidateBorders();
1389 }
1390
1391 if ( receivedFocus )
1392 {
1393 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
1394 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1395 thisWindow->HandleWindowEvent(eventFocus);
1396
1397#if wxUSE_CARET
1398 if ( thisWindow->GetCaret() )
1399 thisWindow->GetCaret()->OnSetFocus();
1400#endif
1401
1402 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1403 event.SetEventObject(thisWindow);
1404 // TODO how to find out the targetFocusWindow ?
1405 // event.SetWindow(targetFocusWindow);
1406 thisWindow->HandleWindowEvent(event) ;
1407 }
1408 else // !receivedFocuss
1409 {
1410#if wxUSE_CARET
1411 if ( thisWindow->GetCaret() )
1412 thisWindow->GetCaret()->OnKillFocus();
1413#endif
1414
1415 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
1416
1417 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1418 event.SetEventObject(thisWindow);
1419 // TODO how to find out the targetFocusWindow ?
1420 // event.SetWindow(targetFocusWindow);
1421 thisWindow->HandleWindowEvent(event) ;
1422 }
1423}
1424
54f11060
SC
1425void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1426{
1427 NSPoint location = [NSEvent mouseLocation];
1428 location = [[m_osxView window] convertScreenToBase:location];
1429 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1430
1431 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1432 {
1433 [(NSCursor*)cursor.GetHCURSOR() set];
1434 }
1435 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1436}
1437
1438void wxWidgetCocoaImpl::CaptureMouse()
1439{
1440 [[m_osxView window] disableCursorRects];
1441}
1442
1443void wxWidgetCocoaImpl::ReleaseMouse()
1444{
1445 [[m_osxView window] enableCursorRects];
1446}
4850cc8b 1447
4dd9fdf8
SC
1448void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1449{
1450 m_isFlipped = flipped;
1451}
1452
33e90275
SC
1453//
1454// Factory methods
1455//
1456
dbeddfb9
SC
1457wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1458 long style, long extraStyle)
33e90275 1459{
dbeddfb9 1460 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
33e90275 1461 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
4dd9fdf8
SC
1462
1463 // temporary hook for dnd
1464 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1465 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1466
33e90275 1467 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
33e90275
SC
1468 return c;
1469}
1470
1471wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1472{
1473 NSWindow* tlw = now->GetWXWindow();
1474 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1475 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
7e06ee6d 1476 c->InstallEventHandler();
33e90275
SC
1477 [tlw setContentView:v];
1478 return c;
1479}