]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/window.mm
compilation fixes for some cases
[wxWidgets.git] / src / cocoa / window.mm
CommitLineData
fb896a32
DE
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/cocoa/window.mm
3// Purpose: wxWindowCocoa
4// Author: David Elliott
5// Modified by:
6// Created: 2002/12/26
8d8d3633 7// RCS-ID: $Id$
fb896a32 8// Copyright: (c) 2002 David Elliott
8d8d3633 9// Licence: wxWidgets licence
fb896a32
DE
10/////////////////////////////////////////////////////////////////////////////
11
449c5673 12#include "wx/wxprec.h"
da80ae71 13
449c5673
DE
14#ifndef WX_PRECOMP
15 #include "wx/log.h"
16 #include "wx/window.h"
4db3c8ac 17 #include "wx/dc.h"
ca5db7b2 18 #include "wx/utils.h"
449c5673 19#endif //WX_PRECOMP
da80ae71 20
e73ae747 21#include "wx/tooltip.h"
fb896a32 22
7fc77f30 23#include "wx/cocoa/autorelease.h"
26191790 24#include "wx/cocoa/string.h"
7c5a378f 25#include "wx/cocoa/trackingrectmanager.h"
7fc77f30 26
7c5a378f 27#import <Foundation/NSRunLoop.h>
829a2e95 28#include "wx/cocoa/objc/NSView.h"
69dbb709 29#import <AppKit/NSEvent.h>
816c52cf
DE
30#import <AppKit/NSScrollView.h>
31#import <AppKit/NSColor.h>
32#import <AppKit/NSClipView.h>
dc5bcaef 33#import <Foundation/NSException.h>
67d2dac8
DE
34#import <AppKit/NSApplication.h>
35#import <AppKit/NSWindow.h>
dc5bcaef 36
75e4856b
DE
37// Turn this on to paint green over the dummy views for debugging
38#undef WXCOCOA_FILL_DUMMY_VIEW
39
40#ifdef WXCOCOA_FILL_DUMMY_VIEW
41#import <AppKit/NSBezierPath.h>
42#endif //def WXCOCOA_FILL_DUMMY_VIEW
43
5dc47140
DE
44// A category for methods that are only present in Panther's SDK
45@interface NSView(wxNSViewPrePantherCompatibility)
46- (void)getRectsBeingDrawn:(const NSRect **)rects count:(int *)count;
47@end
48
ee022549
DE
49NSPoint CocoaTransformNSViewBoundsToWx(NSView *nsview, NSPoint pointBounds)
50{
51 wxCHECK_MSG(nsview, pointBounds, wxT("Need to have a Cocoa view to do translation"));
52 if([nsview isFlipped])
53 return pointBounds;
54 NSRect ourBounds = [nsview bounds];
55 return NSMakePoint
56 ( pointBounds.x
57 , ourBounds.size.height - pointBounds.y
58 );
59}
60
61NSRect CocoaTransformNSViewBoundsToWx(NSView *nsview, NSRect rectBounds)
62{
63 wxCHECK_MSG(nsview, rectBounds, wxT("Need to have a Cocoa view to do translation"));
64 if([nsview isFlipped])
65 return rectBounds;
66 NSRect ourBounds = [nsview bounds];
67 return NSMakeRect
68 ( rectBounds.origin.x
69 , ourBounds.size.height - (rectBounds.origin.y + rectBounds.size.height)
70 , rectBounds.size.width
71 , rectBounds.size.height
72 );
73}
74
75NSPoint CocoaTransformNSViewWxToBounds(NSView *nsview, NSPoint pointWx)
76{
77 wxCHECK_MSG(nsview, pointWx, wxT("Need to have a Cocoa view to do translation"));
78 if([nsview isFlipped])
79 return pointWx;
80 NSRect ourBounds = [nsview bounds];
81 return NSMakePoint
82 ( pointWx.x
83 , ourBounds.size.height - pointWx.y
84 );
85}
86
87NSRect CocoaTransformNSViewWxToBounds(NSView *nsview, NSRect rectWx)
88{
89 wxCHECK_MSG(nsview, rectWx, wxT("Need to have a Cocoa view to do translation"));
90 if([nsview isFlipped])
91 return rectWx;
92 NSRect ourBounds = [nsview bounds];
93 return NSMakeRect
94 ( rectWx.origin.x
95 , ourBounds.size.height - (rectWx.origin.y + rectWx.size.height)
96 , rectWx.size.width
97 , rectWx.size.height
98 );
99}
100
a82b8141
DE
101// ========================================================================
102// wxWindowCocoaHider
103// ========================================================================
104class wxWindowCocoaHider: protected wxCocoaNSView
105{
106 DECLARE_NO_COPY_CLASS(wxWindowCocoaHider)
107public:
108 wxWindowCocoaHider(wxWindow *owner);
109 virtual ~wxWindowCocoaHider();
110 inline WX_NSView GetNSView() { return m_dummyNSView; }
111protected:
112 wxWindowCocoa *m_owner;
113 WX_NSView m_dummyNSView;
114 virtual void Cocoa_FrameChanged(void);
7c5a378f 115 virtual void Cocoa_synthesizeMouseMoved(void) {}
75e4856b
DE
116#ifdef WXCOCOA_FILL_DUMMY_VIEW
117 virtual bool Cocoa_drawRect(const NSRect& rect);
118#endif //def WXCOCOA_FILL_DUMMY_VIEW
a82b8141
DE
119private:
120 wxWindowCocoaHider();
121};
122
816c52cf 123// ========================================================================
f298b203 124// wxWindowCocoaScrollView
816c52cf 125// ========================================================================
f298b203 126class wxWindowCocoaScrollView: protected wxCocoaNSView
816c52cf 127{
f298b203 128 DECLARE_NO_COPY_CLASS(wxWindowCocoaScrollView)
816c52cf 129public:
f298b203
DE
130 wxWindowCocoaScrollView(wxWindow *owner);
131 virtual ~wxWindowCocoaScrollView();
816c52cf
DE
132 inline WX_NSScrollView GetNSScrollView() { return m_cocoaNSScrollView; }
133 void ClientSizeToSize(int &width, int &height);
134 void DoGetClientSize(int *x, int *y) const;
135 void Encapsulate();
136 void Unencapsulate();
137protected:
138 wxWindowCocoa *m_owner;
139 WX_NSScrollView m_cocoaNSScrollView;
140 virtual void Cocoa_FrameChanged(void);
7c5a378f 141 virtual void Cocoa_synthesizeMouseMoved(void) {}
816c52cf 142private:
f298b203 143 wxWindowCocoaScrollView();
816c52cf
DE
144};
145
75e4856b
DE
146// ========================================================================
147// wxDummyNSView
148// ========================================================================
149@interface wxDummyNSView : NSView
150- (NSView *)hitTest:(NSPoint)aPoint;
151@end
a24aa427 152WX_DECLARE_GET_OBJC_CLASS(wxDummyNSView,NSView)
75e4856b
DE
153
154@implementation wxDummyNSView : NSView
155- (NSView *)hitTest:(NSPoint)aPoint
156{
157 return nil;
158}
159
160@end
a24aa427 161WX_IMPLEMENT_GET_OBJC_CLASS(wxDummyNSView,NSView)
75e4856b 162
a82b8141
DE
163// ========================================================================
164// wxWindowCocoaHider
165// ========================================================================
166wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner)
167: m_owner(owner)
168{
169 wxASSERT(owner);
170 wxASSERT(owner->GetNSViewForHiding());
a24aa427 171 m_dummyNSView = [[WX_GET_OBJC_CLASS(wxDummyNSView) alloc]
a82b8141 172 initWithFrame:[owner->GetNSViewForHiding() frame]];
75e4856b 173 [m_dummyNSView setAutoresizingMask: [owner->GetNSViewForHiding() autoresizingMask]];
a82b8141
DE
174 AssociateNSView(m_dummyNSView);
175}
176
177wxWindowCocoaHider::~wxWindowCocoaHider()
178{
179 DisassociateNSView(m_dummyNSView);
180 [m_dummyNSView release];
181}
182
183void wxWindowCocoaHider::Cocoa_FrameChanged(void)
184{
185 // Keep the real window in synch with the dummy
186 wxASSERT(m_dummyNSView);
187 [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]];
188}
189
5558135c 190
75e4856b
DE
191#ifdef WXCOCOA_FILL_DUMMY_VIEW
192bool wxWindowCocoaHider::Cocoa_drawRect(const NSRect& rect)
193{
194 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:rect];
195 [[NSColor greenColor] set];
196 [bezpath stroke];
197 [bezpath fill];
198 return true;
199}
200#endif //def WXCOCOA_FILL_DUMMY_VIEW
201
816c52cf
DE
202// ========================================================================
203// wxFlippedNSClipView
204// ========================================================================
205@interface wxFlippedNSClipView : NSClipView
206- (BOOL)isFlipped;
207@end
a24aa427 208WX_DECLARE_GET_OBJC_CLASS(wxFlippedNSClipView,NSClipView)
816c52cf
DE
209
210@implementation wxFlippedNSClipView : NSClipView
211- (BOOL)isFlipped
212{
213 return YES;
214}
215
216@end
a24aa427 217WX_IMPLEMENT_GET_OBJC_CLASS(wxFlippedNSClipView,NSClipView)
816c52cf
DE
218
219// ========================================================================
f298b203 220// wxWindowCocoaScrollView
816c52cf 221// ========================================================================
f298b203 222wxWindowCocoaScrollView::wxWindowCocoaScrollView(wxWindow *owner)
816c52cf
DE
223: m_owner(owner)
224{
a9861c57 225 wxAutoNSAutoreleasePool pool;
816c52cf
DE
226 wxASSERT(owner);
227 wxASSERT(owner->GetNSView());
228 m_cocoaNSScrollView = [[NSScrollView alloc]
229 initWithFrame:[owner->GetNSView() frame]];
230 AssociateNSView(m_cocoaNSScrollView);
231
232 /* Replace the default NSClipView with a flipped one. This ensures
233 scrolling is "pinned" to the top-left instead of bottom-right. */
a24aa427 234 NSClipView *flippedClip = [[WX_GET_OBJC_CLASS(wxFlippedNSClipView) alloc]
816c52cf
DE
235 initWithFrame: [[m_cocoaNSScrollView contentView] frame]];
236 [m_cocoaNSScrollView setContentView:flippedClip];
237 [flippedClip release];
238
239 [m_cocoaNSScrollView setBackgroundColor: [NSColor windowBackgroundColor]];
240 [m_cocoaNSScrollView setHasHorizontalScroller: YES];
241 [m_cocoaNSScrollView setHasVerticalScroller: YES];
242 Encapsulate();
243}
244
f298b203 245void wxWindowCocoaScrollView::Encapsulate()
816c52cf 246{
6f2ec3c3
DE
247 // Set the scroll view autoresizingMask to match the current NSView
248 [m_cocoaNSScrollView setAutoresizingMask: [m_owner->GetNSView() autoresizingMask]];
249 [m_owner->GetNSView() setAutoresizingMask: NSViewNotSizable];
816c52cf
DE
250 // NOTE: replaceSubView will cause m_cocaNSView to be released
251 // except when it hasn't been added into an NSView hierarchy in which
252 // case it doesn't need to be and this should work out to a no-op
253 m_owner->CocoaReplaceView(m_owner->GetNSView(), m_cocoaNSScrollView);
254 // The NSView is still retained by owner
255 [m_cocoaNSScrollView setDocumentView: m_owner->GetNSView()];
256 // Now it's also retained by the NSScrollView
257}
258
f298b203 259void wxWindowCocoaScrollView::Unencapsulate()
816c52cf
DE
260{
261 [m_cocoaNSScrollView setDocumentView: nil];
262 m_owner->CocoaReplaceView(m_cocoaNSScrollView, m_owner->GetNSView());
6f2ec3c3
DE
263 if(![[m_owner->GetNSView() superview] isFlipped])
264 [m_owner->GetNSView() setAutoresizingMask: NSViewMinYMargin];
816c52cf
DE
265}
266
f298b203 267wxWindowCocoaScrollView::~wxWindowCocoaScrollView()
816c52cf
DE
268{
269 DisassociateNSView(m_cocoaNSScrollView);
270 [m_cocoaNSScrollView release];
271}
272
f298b203 273void wxWindowCocoaScrollView::ClientSizeToSize(int &width, int &height)
816c52cf
DE
274{
275 NSSize frameSize = [NSScrollView
276 frameSizeForContentSize: NSMakeSize(width,height)
277 hasHorizontalScroller: [m_cocoaNSScrollView hasHorizontalScroller]
278 hasVerticalScroller: [m_cocoaNSScrollView hasVerticalScroller]
279 borderType: [m_cocoaNSScrollView borderType]];
e9cece45
VZ
280 width = (int)frameSize.width;
281 height = (int)frameSize.height;
816c52cf
DE
282}
283
f298b203 284void wxWindowCocoaScrollView::DoGetClientSize(int *x, int *y) const
816c52cf
DE
285{
286 NSSize nssize = [m_cocoaNSScrollView contentSize];
287 if(x)
e9cece45 288 *x = (int)nssize.width;
816c52cf 289 if(y)
e9cece45 290 *y = (int)nssize.height;
816c52cf
DE
291}
292
f298b203 293void wxWindowCocoaScrollView::Cocoa_FrameChanged(void)
816c52cf 294{
48580976 295 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
816c52cf
DE
296 wxSizeEvent event(m_owner->GetSize(), m_owner->GetId());
297 event.SetEventObject(m_owner);
298 m_owner->GetEventHandler()->ProcessEvent(event);
299}
300
a82b8141
DE
301// ========================================================================
302// wxWindowCocoa
303// ========================================================================
fb896a32
DE
304// normally the base classes aren't included, but wxWindow is special
305#ifdef __WXUNIVERSAL__
306IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
307#else
308IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
309#endif
310
311BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
312END_EVENT_TABLE()
313
b9505233
DE
314wxWindow *wxWindowCocoa::sm_capturedWindow = NULL;
315
fb896a32
DE
316// Constructor
317void wxWindowCocoa::Init()
318{
fb896a32 319 m_cocoaNSView = NULL;
a82b8141 320 m_cocoaHider = NULL;
f298b203 321 m_wxCocoaScrollView = NULL;
8d8d3633
WS
322 m_isBeingDeleted = false;
323 m_isInPaint = false;
7c5a378f 324 m_visibleTrackingRectManager = NULL;
fb896a32
DE
325}
326
327// Constructor
328bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
329 const wxPoint& pos,
330 const wxSize& size,
331 long style,
332 const wxString& name)
333{
334 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
335 return false;
336
337 // TODO: create the window
fb896a32 338 m_cocoaNSView = NULL;
a24aa427 339 SetNSView([[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: MakeDefaultNSRect(size)]);
fb896a32
DE
340 [m_cocoaNSView release];
341
342 if (m_parent)
343 {
344 m_parent->AddChild(this);
345 m_parent->CocoaAddChild(this);
6d034f7d 346 SetInitialFrameRect(pos,size);
fb896a32
DE
347 }
348
8d8d3633 349 return true;
fb896a32
DE
350}
351
352// Destructor
353wxWindow::~wxWindow()
354{
7fc77f30 355 wxAutoNSAutoreleasePool pool;
fb896a32
DE
356 DestroyChildren();
357
065e208e 358 // Make sure our parent (in the wxWidgets sense) is our superview
6ba13ca4
DE
359 // before we go removing from it.
360 if(m_parent && m_parent->GetNSView()==[GetNSViewForSuperview() superview])
361 CocoaRemoveFromParent();
a82b8141 362 delete m_cocoaHider;
f298b203 363 delete m_wxCocoaScrollView;
9c85202a
DE
364 if(m_cocoaNSView)
365 SendDestroyEvent();
fb896a32
DE
366 SetNSView(NULL);
367}
368
369void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
370{
a82b8141
DE
371 NSView *childView = child->GetNSViewForSuperview();
372
373 wxASSERT(childView);
374 [m_cocoaNSView addSubview: childView];
375 child->m_isShown = !m_cocoaHider;
fb896a32
DE
376}
377
378void wxWindowCocoa::CocoaRemoveFromParent(void)
379{
a82b8141 380 [GetNSViewForSuperview() removeFromSuperview];
fb896a32
DE
381}
382
383void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
384{
7c5a378f
DE
385 // Clear the visible area tracking rect if we have one.
386 delete m_visibleTrackingRectManager;
387 m_visibleTrackingRectManager = NULL;
388
fb896a32 389 bool need_debug = cocoaNSView || m_cocoaNSView;
48580976 390 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d"),this,m_cocoaNSView,[m_cocoaNSView retainCount]);
bac6f234 391 DisassociateNSView(m_cocoaNSView);
fb896a32
DE
392 [cocoaNSView retain];
393 [m_cocoaNSView release];
394 m_cocoaNSView = cocoaNSView;
bac6f234 395 AssociateNSView(m_cocoaNSView);
48580976 396 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d"),this,cocoaNSView,[cocoaNSView retainCount]);
fb896a32
DE
397}
398
a82b8141
DE
399WX_NSView wxWindowCocoa::GetNSViewForSuperview() const
400{
401 return m_cocoaHider
402 ? m_cocoaHider->GetNSView()
f298b203
DE
403 : m_wxCocoaScrollView
404 ? m_wxCocoaScrollView->GetNSScrollView()
816c52cf 405 : m_cocoaNSView;
a82b8141
DE
406}
407
408WX_NSView wxWindowCocoa::GetNSViewForHiding() const
409{
f298b203
DE
410 return m_wxCocoaScrollView
411 ? m_wxCocoaScrollView->GetNSScrollView()
816c52cf 412 : m_cocoaNSView;
a82b8141
DE
413}
414
34c9978d
DE
415NSPoint wxWindowCocoa::CocoaTransformBoundsToWx(NSPoint pointBounds)
416{
417 // TODO: Handle scrolling offset
ee022549 418 return CocoaTransformNSViewBoundsToWx(GetNSView(), pointBounds);
34c9978d
DE
419}
420
421NSRect wxWindowCocoa::CocoaTransformBoundsToWx(NSRect rectBounds)
422{
423 // TODO: Handle scrolling offset
ee022549 424 return CocoaTransformNSViewBoundsToWx(GetNSView(), rectBounds);
34c9978d
DE
425}
426
427NSPoint wxWindowCocoa::CocoaTransformWxToBounds(NSPoint pointWx)
428{
429 // TODO: Handle scrolling offset
ee022549 430 return CocoaTransformNSViewWxToBounds(GetNSView(), pointWx);
34c9978d
DE
431}
432
433NSRect wxWindowCocoa::CocoaTransformWxToBounds(NSRect rectWx)
434{
435 // TODO: Handle scrolling offset
ee022549 436 return CocoaTransformNSViewWxToBounds(GetNSView(), rectWx);
34c9978d
DE
437}
438
4db3c8ac
DE
439WX_NSAffineTransform wxWindowCocoa::CocoaGetWxToBoundsTransform()
440{
441 // TODO: Handle scrolling offset
442 NSAffineTransform *transform = wxDC::CocoaGetWxToBoundsTransform([GetNSView() isFlipped], [GetNSView() bounds].size.height);
443 return transform;
444}
445
8ea5271e
DE
446bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
447{
48580976 448 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_drawRect"));
55c5be5e
DE
449 // Recursion can happen if the event loop runs from within the paint
450 // handler. For instance, if an assertion dialog is shown.
451 // FIXME: This seems less than ideal.
452 if(m_isInPaint)
453 {
2b030203 454 wxLogDebug(wxT("Paint event recursion!"));
55c5be5e
DE
455 return false;
456 }
8d8d3633 457 m_isInPaint = true;
dc5bcaef
DE
458
459 // Set m_updateRegion
460 const NSRect *rects = &rect; // The bounding box of the region
461 int countRects = 1;
462 // Try replacing the larger rectangle with a list of smaller ones:
5dc47140
DE
463 if ([GetNSView() respondsToSelector:@selector(getRectsBeingDrawn:count:)])
464 [GetNSView() getRectsBeingDrawn:&rects count:&countRects];
34c9978d
DE
465
466 NSRect *transformedRects = (NSRect*)malloc(sizeof(NSRect)*countRects);
467 for(int i=0; i<countRects; i++)
468 {
469 transformedRects[i] = CocoaTransformBoundsToWx(rects[i]);
470 }
471 m_updateRegion = wxRegion(transformedRects,countRects);
472 free(transformedRects);
dc5bcaef 473
8ea5271e
DE
474 wxPaintEvent event(m_windowId);
475 event.SetEventObject(this);
55c5be5e 476 bool ret = GetEventHandler()->ProcessEvent(event);
8d8d3633 477 m_isInPaint = false;
55c5be5e 478 return ret;
8ea5271e
DE
479}
480
69dbb709
DE
481void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
482{
2b030203 483 wxASSERT_MSG([m_cocoaNSView window]==[cocoaEvent window],wxT("Mouse event for different NSWindow"));
34c9978d
DE
484 // Mouse events happen at the NSWindow level so we need to convert
485 // into our bounds coordinates then convert to wx coordinates.
a82b8141 486 NSPoint cocoaPoint = [m_cocoaNSView convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
34c9978d
DE
487 NSPoint pointWx = CocoaTransformBoundsToWx(cocoaPoint);
488 // FIXME: Should we be adjusting for client area origin?
69dbb709 489 const wxPoint &clientorigin = GetClientAreaOrigin();
34c9978d
DE
490 event.m_x = (wxCoord)pointWx.x - clientorigin.x;
491 event.m_y = (wxCoord)pointWx.y - clientorigin.y;
69dbb709
DE
492
493 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
494 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
495 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
496 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
497
498 // TODO: set timestamp?
499 event.SetEventObject(this);
500 event.SetId(GetId());
501}
502
503bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
504{
505 wxMouseEvent event(wxEVT_MOTION);
506 InitMouseEvent(event,theEvent);
7c5a378f 507 wxLogTrace(wxTRACE_COCOA,wxT("wxWindow=%p::Cocoa_mouseMoved @%d,%d"),this,event.m_x,event.m_y);
69dbb709
DE
508 return GetEventHandler()->ProcessEvent(event);
509}
510
7c5a378f
DE
511void wxWindowCocoa::Cocoa_synthesizeMouseMoved()
512{
513 wxMouseEvent event(wxEVT_MOTION);
514 NSWindow *window = [GetNSView() window];
515 NSPoint locationInWindow = [window mouseLocationOutsideOfEventStream];
516 NSPoint cocoaPoint = [m_cocoaNSView convertPoint:locationInWindow fromView:nil];
517
518 NSPoint pointWx = CocoaTransformBoundsToWx(cocoaPoint);
519 // FIXME: Should we be adjusting for client area origin?
520 const wxPoint &clientorigin = GetClientAreaOrigin();
521 event.m_x = (wxCoord)pointWx.x - clientorigin.x;
522 event.m_y = (wxCoord)pointWx.y - clientorigin.y;
523
524 // TODO: Handle shift, control, alt, meta flags
525 event.SetEventObject(this);
526 event.SetId(GetId());
527
528 wxLogTrace(wxTRACE_COCOA,wxT("wxwin=%p Synthesized Mouse Moved @%d,%d"),this,event.m_x,event.m_y);
529 GetEventHandler()->ProcessEvent(event);
530}
531
69dbb709
DE
532bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
533{
7c5a378f
DE
534 if(m_visibleTrackingRectManager != NULL && m_visibleTrackingRectManager->IsOwnerOfEvent(theEvent))
535 {
536 m_visibleTrackingRectManager->BeginSynthesizingEvents();
537
538 // Although we synthesize the mouse moved events we don't poll for them but rather send them only when
539 // some other event comes in. That other event is (guess what) mouse moved events that will be sent
540 // to the NSWindow which will forward them on to the first responder. We are not likely to be the
541 // first responder, so the mouseMoved: events are effectively discarded.
542 [[GetNSView() window] setAcceptsMouseMovedEvents:YES];
543
544 wxMouseEvent event(wxEVT_ENTER_WINDOW);
545 InitMouseEvent(event,theEvent);
546 wxLogTrace(wxTRACE_COCOA,wxT("wxwin=%p Mouse Entered @%d,%d"),this,event.m_x,event.m_y);
547 return GetEventHandler()->ProcessEvent(event);
548 }
549 else
550 return false;
69dbb709
DE
551}
552
553bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
554{
7c5a378f
DE
555 if(m_visibleTrackingRectManager != NULL && m_visibleTrackingRectManager->IsOwnerOfEvent(theEvent))
556 {
557 m_visibleTrackingRectManager->StopSynthesizingEvents();
558
559 wxMouseEvent event(wxEVT_LEAVE_WINDOW);
560 InitMouseEvent(event,theEvent);
561 wxLogTrace(wxTRACE_COCOA,wxT("wxwin=%p Mouse Exited @%d,%d"),this,event.m_x,event.m_y);
562 return GetEventHandler()->ProcessEvent(event);
563 }
564 else
565 return false;
69dbb709
DE
566}
567
568bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
569{
570 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
571 InitMouseEvent(event,theEvent);
48580976 572 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]);
69dbb709
DE
573 return GetEventHandler()->ProcessEvent(event);
574}
575
576bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
577{
578 wxMouseEvent event(wxEVT_MOTION);
579 InitMouseEvent(event,theEvent);
580 event.m_leftDown = true;
48580976 581 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
69dbb709
DE
582 return GetEventHandler()->ProcessEvent(event);
583}
584
585bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
586{
587 wxMouseEvent event(wxEVT_LEFT_UP);
588 InitMouseEvent(event,theEvent);
48580976 589 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Up @%d,%d"),event.m_x,event.m_y);
69dbb709
DE
590 return GetEventHandler()->ProcessEvent(event);
591}
592
593bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
594{
eafde5c7
DE
595 wxMouseEvent event([theEvent clickCount]<2?wxEVT_RIGHT_DOWN:wxEVT_RIGHT_DCLICK);
596 InitMouseEvent(event,theEvent);
597 wxLogDebug(wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]);
598 return GetEventHandler()->ProcessEvent(event);
69dbb709
DE
599}
600
601bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
602{
eafde5c7
DE
603 wxMouseEvent event(wxEVT_MOTION);
604 InitMouseEvent(event,theEvent);
605 event.m_rightDown = true;
606 wxLogDebug(wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
607 return GetEventHandler()->ProcessEvent(event);
69dbb709
DE
608}
609
610bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
611{
eafde5c7
DE
612 wxMouseEvent event(wxEVT_RIGHT_UP);
613 InitMouseEvent(event,theEvent);
614 wxLogDebug(wxT("Mouse Up @%d,%d"),event.m_x,event.m_y);
615 return GetEventHandler()->ProcessEvent(event);
69dbb709
DE
616}
617
618bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
619{
620 return false;
621}
622
623bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
624{
625 return false;
626}
627
628bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
629{
630 return false;
631}
632
fb896a32
DE
633void wxWindowCocoa::Cocoa_FrameChanged(void)
634{
7c5a378f
DE
635 wxLogTrace(wxTRACE_COCOA,wxT("wxWindow=%p::Cocoa_FrameChanged"),this);
636 if(m_visibleTrackingRectManager != NULL)
637 m_visibleTrackingRectManager->RebuildTrackingRect();
fb896a32
DE
638 wxSizeEvent event(GetSize(), m_windowId);
639 event.SetEventObject(this);
640 GetEventHandler()->ProcessEvent(event);
641}
642
5558135c
RN
643bool wxWindowCocoa::Cocoa_resetCursorRects()
644{
7c5a378f
DE
645 wxLogTrace(wxTRACE_COCOA,wxT("wxWindow=%p::Cocoa_resetCursorRects"),this);
646 if(m_visibleTrackingRectManager != NULL)
647 m_visibleTrackingRectManager->RebuildTrackingRect();
648
5558135c
RN
649 if(!m_cursor.GetNSCursor())
650 return false;
8d8d3633
WS
651
652 [GetNSView() addCursorRect: [GetNSView() visibleRect] cursor: m_cursor.GetNSCursor()];
653
5558135c
RN
654 return true;
655}
656
7c5a378f
DE
657bool wxWindowCocoa::Cocoa_viewDidMoveToWindow()
658{
659 wxLogTrace(wxTRACE_COCOA,wxT("wxWindow=%p::viewDidMoveToWindow"),this);
660 // Set up new tracking rects. I am reasonably sure the new window must be set before doing this.
661 if(m_visibleTrackingRectManager != NULL)
662 m_visibleTrackingRectManager->BuildTrackingRect();
663 return false;
664}
665
666bool wxWindowCocoa::Cocoa_viewWillMoveToWindow(WX_NSWindow newWindow)
667{
668 wxLogTrace(wxTRACE_COCOA,wxT("wxWindow=%p::viewWillMoveToWindow:%p"),this, newWindow);
669 // Clear tracking rects. It is imperative this be done before the new window is set.
670 if(m_visibleTrackingRectManager != NULL)
671 m_visibleTrackingRectManager->ClearTrackingRect();
672 return false;
673}
674
fb896a32
DE
675bool wxWindow::Close(bool force)
676{
cc6f960f
DE
677 // The only reason this function exists is that it is virtual and
678 // wxTopLevelWindowCocoa will override it.
679 return wxWindowBase::Close(force);
fb896a32
DE
680}
681
a82b8141
DE
682void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
683{
684 [[oldView superview] replaceSubview:oldView with:newView];
685}
686
47a8a4d5 687void wxWindow::DoEnable(bool enable)
adb4816c 688{
47a8a4d5 689 CocoaSetEnabled(enable);
adb4816c
DE
690}
691
fb896a32
DE
692bool wxWindow::Show(bool show)
693{
7fc77f30 694 wxAutoNSAutoreleasePool pool;
fb896a32
DE
695 // If the window is marked as visible, then it shouldn't have a dummy view
696 // If the window is marked hidden, then it should have a dummy view
addbdd29 697 // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
2b030203 698// wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),wxT("wxWindow: m_isShown does not agree with m_dummyNSView"));
fb896a32 699 // Return false if there isn't a window to show or hide
a82b8141
DE
700 NSView *cocoaView = GetNSViewForHiding();
701 if(!cocoaView)
fb896a32 702 return false;
fb896a32
DE
703 if(show)
704 {
addbdd29 705 // If state isn't changing, return false
a82b8141 706 if(!m_cocoaHider)
addbdd29 707 return false;
a82b8141
DE
708 CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView);
709 wxASSERT(![m_cocoaHider->GetNSView() superview]);
710 delete m_cocoaHider;
711 m_cocoaHider = NULL;
712 wxASSERT([cocoaView superview]);
fb896a32
DE
713 }
714 else
715 {
addbdd29 716 // If state isn't changing, return false
a82b8141 717 if(m_cocoaHider)
addbdd29 718 return false;
a82b8141
DE
719 m_cocoaHider = new wxWindowCocoaHider(this);
720 // NOTE: replaceSubview:with will cause m_cocaNSView to be
721 // (auto)released which balances out addSubview
722 CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView());
fb896a32 723 // m_coocaNSView is now only retained by us
a82b8141
DE
724 wxASSERT([m_cocoaHider->GetNSView() superview]);
725 wxASSERT(![cocoaView superview]);
fb896a32 726 }
a6b4ff2e
DE
727 m_isShown = show;
728 return true;
fb896a32
DE
729}
730
731void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
732{
9879fa84 733 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoSetSizeWindow(%d,%d,%d,%d,Auto: %s%s)"),this,x,y,width,height,(sizeFlags&wxSIZE_AUTO_WIDTH)?"W":".",sizeFlags&wxSIZE_AUTO_HEIGHT?"H":".");
fb896a32
DE
734 int currentX, currentY;
735 int currentW, currentH;
736 DoGetPosition(&currentX, &currentY);
737 DoGetSize(&currentW, &currentH);
738 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
739 x=currentX;
740 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
741 y=currentY;
742
743 AdjustForParentClientOrigin(x,y,sizeFlags);
744
8d8d3633 745 wxSize size(wxDefaultSize);
fb896a32
DE
746
747 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
748 {
749 if(sizeFlags&wxSIZE_AUTO_WIDTH)
750 {
751 size=DoGetBestSize();
752 width=size.x;
753 }
754 else
755 width=currentW;
756 }
757 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
758 {
759 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
760 {
761 if(size.x==-1)
762 size=DoGetBestSize();
763 height=size.y;
764 }
765 else
766 height=currentH;
767 }
768 DoMoveWindow(x,y,width,height);
769}
770
1e151594 771#if wxUSE_TOOLTIPS
26191790
RN
772
773void wxWindowCocoa::DoSetToolTip( wxToolTip *tip )
774{
775 wxWindowBase::DoSetToolTip(tip);
776
26191790
RN
777 if ( m_tooltip )
778 {
779 m_tooltip->SetWindow((wxWindow *)this);
26191790
RN
780 }
781}
782
1e151594
RN
783#endif
784
fb896a32
DE
785void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
786{
bed6fe0c 787 wxAutoNSAutoreleasePool pool;
9879fa84 788 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
fb896a32 789
a82b8141 790 NSView *nsview = GetNSViewForSuperview();
d449cf47 791 NSView *superview = [nsview superview];
fb896a32 792
34c9978d
DE
793 wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent"));
794
795 NSRect oldFrameRect = [nsview frame];
796 NSRect newFrameRect = GetParent()->CocoaTransformWxToBounds(NSMakeRect(x,y,width,height));
797 [nsview setFrame:newFrameRect];
b915b805 798 // Be sure to redraw the parent to reflect the changed position
34c9978d
DE
799 [superview setNeedsDisplayInRect:oldFrameRect];
800 [superview setNeedsDisplayInRect:newFrameRect];
fb896a32
DE
801}
802
d139c3a8
DE
803void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size)
804{
805 NSView *nsview = GetNSViewForSuperview();
806 NSView *superview = [nsview superview];
2b030203 807 wxCHECK_RET(superview,wxT("NSView does not have a superview"));
34c9978d 808 wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent"));
d139c3a8
DE
809 NSRect frameRect = [nsview frame];
810 if(size.x!=-1)
811 frameRect.size.width = size.x;
812 if(size.y!=-1)
813 frameRect.size.height = size.y;
814 frameRect.origin.x = pos.x;
34c9978d 815 frameRect.origin.y = pos.y;
c5bd9191
DE
816 // Tell Cocoa to change the margin between the bottom of the superview
817 // and the bottom of the control. Keeps the control pinned to the top
065e208e 818 // of its superview so that its position in the wxWidgets coordinate
c5bd9191
DE
819 // system doesn't change.
820 if(![superview isFlipped])
821 [nsview setAutoresizingMask: NSViewMinYMargin];
6f2ec3c3
DE
822 // MUST set the mask before setFrame: which can generate a size event
823 // and cause a scroller to be added!
34c9978d 824 frameRect = GetParent()->CocoaTransformWxToBounds(frameRect);
6f2ec3c3 825 [nsview setFrame: frameRect];
d139c3a8
DE
826}
827
fb896a32
DE
828// Get total size
829void wxWindow::DoGetSize(int *w, int *h) const
830{
a82b8141 831 NSRect cocoaRect = [GetNSViewForSuperview() frame];
fb896a32
DE
832 if(w)
833 *w=(int)cocoaRect.size.width;
834 if(h)
835 *h=(int)cocoaRect.size.height;
9879fa84 836 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
fb896a32
DE
837}
838
839void wxWindow::DoGetPosition(int *x, int *y) const
840{
a82b8141 841 NSView *nsview = GetNSViewForSuperview();
fb896a32 842
576a1544 843 NSRect cocoaRect = [nsview frame];
34c9978d 844 NSRect rectWx = GetParent()->CocoaTransformBoundsToWx(cocoaRect);
fb896a32 845 if(x)
34c9978d 846 *x=(int)rectWx.origin.x;
fb896a32 847 if(y)
34c9978d 848 *y=(int)rectWx.origin.y;
9879fa84 849 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
fb896a32
DE
850}
851
852WXWidget wxWindow::GetHandle() const
853{
854 return m_cocoaNSView;
855}
856
f7e98dee
RN
857wxWindow* wxWindow::GetWxWindow() const
858{
859 return (wxWindow*) this;
860}
861
ddf7346a
DE
862void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
863{
864 [m_cocoaNSView setNeedsDisplay:YES];
865}
866
fb896a32
DE
867void wxWindow::SetFocus()
868{
67d2dac8
DE
869 if([GetNSView() acceptsFirstResponder])
870 [[GetNSView() window] makeFirstResponder: GetNSView()];
fb896a32
DE
871}
872
873void wxWindow::DoCaptureMouse()
874{
875 // TODO
b9505233 876 sm_capturedWindow = this;
fb896a32
DE
877}
878
879void wxWindow::DoReleaseMouse()
880{
881 // TODO
b9505233 882 sm_capturedWindow = NULL;
fb896a32
DE
883}
884
885void wxWindow::DoScreenToClient(int *x, int *y) const
886{
887 // TODO
888}
889
890void wxWindow::DoClientToScreen(int *x, int *y) const
891{
892 // TODO
893}
894
895// Get size *available for subwindows* i.e. excluding menu bar etc.
896void wxWindow::DoGetClientSize(int *x, int *y) const
897{
48580976 898 wxLogTrace(wxTRACE_COCOA,wxT("DoGetClientSize:"));
f298b203
DE
899 if(m_wxCocoaScrollView)
900 m_wxCocoaScrollView->DoGetClientSize(x,y);
816c52cf
DE
901 else
902 wxWindowCocoa::DoGetSize(x,y);
fb896a32
DE
903}
904
905void wxWindow::DoSetClientSize(int width, int height)
906{
48580976 907 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("DoSetClientSize=(%d,%d)"),width,height);
f298b203
DE
908 if(m_wxCocoaScrollView)
909 m_wxCocoaScrollView->ClientSizeToSize(width,height);
e08efb8d
DE
910 CocoaSetWxWindowSize(width,height);
911}
912
913void wxWindow::CocoaSetWxWindowSize(int width, int height)
914{
8d8d3633
WS
915 wxWindowCocoa::DoSetSize(wxDefaultCoord,wxDefaultCoord,width,height,wxSIZE_USE_EXISTING);
916}
917
918void wxWindow::SetLabel(const wxString& WXUNUSED(label))
919{
ba64d0b6 920 // Intentional no-op.
8d8d3633
WS
921}
922
923wxString wxWindow::GetLabel() const
924{
ba64d0b6
DE
925 // General Get/Set of labels is implemented in wxControlBase
926 wxLogDebug(wxT("wxWindow::GetLabel: Should be overridden if needed."));
8d8d3633 927 return wxEmptyString;
fb896a32
DE
928}
929
930int wxWindow::GetCharHeight() const
931{
932 // TODO
933 return 0;
934}
935
936int wxWindow::GetCharWidth() const
937{
938 // TODO
939 return 0;
940}
941
942void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
943 int *descent, int *externalLeading, const wxFont *theFont) const
944{
945 // TODO
946}
947
fb896a32
DE
948// Coordinates relative to the window
949void wxWindow::WarpPointer (int x_pos, int y_pos)
950{
951 // TODO
952}
953
954int wxWindow::GetScrollPos(int orient) const
955{
956 // TODO
957 return 0;
958}
959
960// This now returns the whole range, not just the number
961// of positions that we can scroll.
962int wxWindow::GetScrollRange(int orient) const
963{
964 // TODO
965 return 0;
966}
967
968int wxWindow::GetScrollThumb(int orient) const
969{
970 // TODO
971 return 0;
972}
973
974void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
975{
976 // TODO
977}
978
816c52cf
DE
979void wxWindow::CocoaCreateNSScrollView()
980{
f298b203 981 if(!m_wxCocoaScrollView)
816c52cf 982 {
f298b203 983 m_wxCocoaScrollView = new wxWindowCocoaScrollView(this);
816c52cf
DE
984 }
985}
986
fb896a32
DE
987// New function that will replace some of the above.
988void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
989 int range, bool refresh)
990{
e08efb8d 991 CocoaCreateNSScrollView();
fb896a32
DE
992 // TODO
993}
994
995// Does a physical scroll
996void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
997{
998 // TODO
999}
1000
816c52cf
DE
1001void wxWindow::DoSetVirtualSize( int x, int y )
1002{
1003 wxWindowBase::DoSetVirtualSize(x,y);
1004 CocoaCreateNSScrollView();
1005 [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)];
1006}
1007
fb896a32
DE
1008bool wxWindow::SetFont(const wxFont& font)
1009{
1010 // TODO
8d8d3633 1011 return true;
fb896a32
DE
1012}
1013
4328a6ba
DE
1014static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
1015{
1016 // first should be ordered higher
1017 if(first==target)
1018 return NSOrderedDescending;
1019 // second should be ordered higher
1020 if(second==target)
1021 return NSOrderedAscending;
1022 return NSOrderedSame;
1023}
1024
fb896a32
DE
1025// Raise the window to the top of the Z order
1026void wxWindow::Raise()
1027{
4328a6ba 1028// wxAutoNSAutoreleasePool pool;
a82b8141 1029 NSView *nsview = GetNSViewForSuperview();
4328a6ba
DE
1030 [[nsview superview] sortSubviewsUsingFunction:
1031 CocoaRaiseWindowCompareFunction
1032 context: nsview];
1033}
1034
1035static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
1036{
1037 // first should be ordered lower
1038 if(first==target)
1039 return NSOrderedAscending;
1040 // second should be ordered lower
1041 if(second==target)
1042 return NSOrderedDescending;
1043 return NSOrderedSame;
fb896a32
DE
1044}
1045
1046// Lower the window to the bottom of the Z order
1047void wxWindow::Lower()
1048{
4328a6ba
DE
1049 NSView *nsview = GetNSViewForSuperview();
1050 [[nsview superview] sortSubviewsUsingFunction:
1051 CocoaLowerWindowCompareFunction
1052 context: nsview];
fb896a32
DE
1053}
1054
1055bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
1056{
8d8d3633 1057 return false;
fb896a32
DE
1058}
1059
1060// Get the window with the focus
dcb68102 1061wxWindow *wxWindowBase::DoFindFocus()
fb896a32 1062{
67d2dac8
DE
1063 // Basically we are somewhat emulating the responder chain here except
1064 // we are only loking for the first responder in the key window or
1065 // upon failing to find one if the main window is different we look
1066 // for the first responder in the main window.
1067
1068 // Note that the firstResponder doesn't necessarily have to be an
1069 // NSView but wxCocoaNSView::GetFromCocoa() will simply return
1070 // NULL unless it finds its argument in its hash map.
1071
1072 wxCocoaNSView *win;
1073
1074 NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
9151dcec 1075 win = wxCocoaNSView::GetFromCocoa(static_cast<NSView*>([keyWindow firstResponder]));
67d2dac8
DE
1076 if(win)
1077 return win->GetWxWindow();
1078
1079 NSWindow *mainWindow = [[NSApplication sharedApplication] keyWindow];
1080 if(mainWindow == keyWindow)
f7e98dee 1081 return NULL;
9151dcec 1082 win = wxCocoaNSView::GetFromCocoa(static_cast<NSView*>([mainWindow firstResponder]));
67d2dac8
DE
1083 if(win)
1084 return win->GetWxWindow();
1085
1086 return NULL;
fb896a32
DE
1087}
1088
1089/* static */ wxWindow *wxWindowBase::GetCapture()
1090{
1091 // TODO
b9505233 1092 return wxWindowCocoa::sm_capturedWindow;
fb896a32
DE
1093}
1094
1095wxWindow *wxGetActiveWindow()
1096{
1097 // TODO
1098 return NULL;
1099}
1100
7c9428ab
DE
1101wxPoint wxGetMousePosition()
1102{
1103 // TODO
1104 return wxDefaultPosition;
1105}
1106
ca5db7b2
WS
1107wxMouseState wxGetMouseState()
1108{
1109 wxMouseState ms;
1110 // TODO
1111 return ms;
1112}
1113
7c9428ab
DE
1114wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1115{
1116 pt = wxGetMousePosition();
1117 return NULL;
1118}
7c5a378f
DE
1119
1120
1121// ========================================================================
1122// wxCocoaTrackingRectManager
1123// ========================================================================
1124
1125wxCocoaTrackingRectManager::wxCocoaTrackingRectManager(wxWindow *window)
1126: m_window(window)
1127{
1128 m_isTrackingRectActive = false;
1129 m_runLoopObserver = NULL;
1130 BuildTrackingRect();
1131}
1132
1133void wxCocoaTrackingRectManager::ClearTrackingRect()
1134{
1135 if(m_isTrackingRectActive)
1136 {
1137 [m_window->GetNSView() removeTrackingRect:m_trackingRectTag];
1138 m_isTrackingRectActive = false;
1139 }
1140 // If we were doing periodic events we need to clear those too
1141 StopSynthesizingEvents();
1142}
1143
1144void wxCocoaTrackingRectManager::StopSynthesizingEvents()
1145{
1146 if(m_runLoopObserver != NULL)
1147 {
1148 CFRunLoopRemoveObserver([[NSRunLoop currentRunLoop] getCFRunLoop], m_runLoopObserver, kCFRunLoopCommonModes);
1149 CFRelease(m_runLoopObserver);
1150 m_runLoopObserver = NULL;
1151 }
1152}
1153
1154void wxCocoaTrackingRectManager::BuildTrackingRect()
1155{
1156 wxASSERT_MSG(!m_isTrackingRectActive, wxT("Tracking rect was not cleared"));
1157 if([m_window->GetNSView() window] != nil)
1158 {
1159 m_trackingRectTag = [m_window->GetNSView() addTrackingRect:[m_window->GetNSView() visibleRect] owner:m_window->GetNSView() userData:NULL assumeInside:NO];
1160 m_isTrackingRectActive = true;
1161 }
1162}
1163
1164static NSPoint s_lastScreenMouseLocation = NSZeroPoint;
1165
1166static void SynthesizeMouseMovedEvent(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info)
1167{
1168 NSPoint screenMouseLocation = [NSEvent mouseLocation];
1169 if(screenMouseLocation.x != s_lastScreenMouseLocation.x || screenMouseLocation.y != s_lastScreenMouseLocation.y)
1170 {
1171 wxCocoaNSView *win = reinterpret_cast<wxCocoaNSView*>(info);
1172 win->Cocoa_synthesizeMouseMoved();
1173 }
1174}
1175
1176void wxCocoaTrackingRectManager::BeginSynthesizingEvents()
1177{
1178 CFRunLoopObserverContext observerContext =
1179 { 0
1180 , static_cast<wxCocoaNSView*>(m_window)
1181 , NULL
1182 , NULL
1183 , NULL
1184 };
1185 m_runLoopObserver = CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopBeforeWaiting, TRUE, 0, SynthesizeMouseMovedEvent, &observerContext);
1186 CFRunLoopAddObserver([[NSRunLoop currentRunLoop] getCFRunLoop], m_runLoopObserver, kCFRunLoopCommonModes);
1187}
1188
1189void wxCocoaTrackingRectManager::RebuildTrackingRect()
1190{
1191 ClearTrackingRect();
1192 BuildTrackingRect();
1193}
1194
1195wxCocoaTrackingRectManager::~wxCocoaTrackingRectManager()
1196{
1197 ClearTrackingRect();
1198}
1199
1200bool wxCocoaTrackingRectManager::IsOwnerOfEvent(NSEvent *anEvent)
1201{
1202 return m_isTrackingRectActive && (m_trackingRectTag == [anEvent trackingNumber]);
1203}
1204