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