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