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