]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/window.mm
OS/2 specific compilation fix for wxCStrData handling.
[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;
fb896a32
DE
316}
317
318// Constructor
319bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
320 const wxPoint& pos,
321 const wxSize& size,
322 long style,
323 const wxString& name)
324{
325 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
326 return false;
327
328 // TODO: create the window
fb896a32 329 m_cocoaNSView = NULL;
d139c3a8 330 SetNSView([[NSView alloc] initWithFrame: MakeDefaultNSRect(size)]);
fb896a32
DE
331 [m_cocoaNSView release];
332
333 if (m_parent)
334 {
335 m_parent->AddChild(this);
336 m_parent->CocoaAddChild(this);
6d034f7d 337 SetInitialFrameRect(pos,size);
fb896a32
DE
338 }
339
8d8d3633 340 return true;
fb896a32
DE
341}
342
343// Destructor
344wxWindow::~wxWindow()
345{
7fc77f30 346 wxAutoNSAutoreleasePool pool;
fb896a32
DE
347 DestroyChildren();
348
065e208e 349 // Make sure our parent (in the wxWidgets sense) is our superview
6ba13ca4
DE
350 // before we go removing from it.
351 if(m_parent && m_parent->GetNSView()==[GetNSViewForSuperview() superview])
352 CocoaRemoveFromParent();
a82b8141 353 delete m_cocoaHider;
f298b203 354 delete m_wxCocoaScrollView;
9c85202a
DE
355 if(m_cocoaNSView)
356 SendDestroyEvent();
fb896a32
DE
357 SetNSView(NULL);
358}
359
360void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
361{
a82b8141
DE
362 NSView *childView = child->GetNSViewForSuperview();
363
364 wxASSERT(childView);
365 [m_cocoaNSView addSubview: childView];
366 child->m_isShown = !m_cocoaHider;
fb896a32
DE
367}
368
369void wxWindowCocoa::CocoaRemoveFromParent(void)
370{
a82b8141 371 [GetNSViewForSuperview() removeFromSuperview];
fb896a32
DE
372}
373
374void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
375{
376 bool need_debug = cocoaNSView || m_cocoaNSView;
48580976 377 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d"),this,m_cocoaNSView,[m_cocoaNSView retainCount]);
bac6f234 378 DisassociateNSView(m_cocoaNSView);
fb896a32
DE
379 [cocoaNSView retain];
380 [m_cocoaNSView release];
381 m_cocoaNSView = cocoaNSView;
bac6f234 382 AssociateNSView(m_cocoaNSView);
48580976 383 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d"),this,cocoaNSView,[cocoaNSView retainCount]);
fb896a32
DE
384}
385
a82b8141
DE
386WX_NSView wxWindowCocoa::GetNSViewForSuperview() const
387{
388 return m_cocoaHider
389 ? m_cocoaHider->GetNSView()
f298b203
DE
390 : m_wxCocoaScrollView
391 ? m_wxCocoaScrollView->GetNSScrollView()
816c52cf 392 : m_cocoaNSView;
a82b8141
DE
393}
394
395WX_NSView wxWindowCocoa::GetNSViewForHiding() const
396{
f298b203
DE
397 return m_wxCocoaScrollView
398 ? m_wxCocoaScrollView->GetNSScrollView()
816c52cf 399 : m_cocoaNSView;
a82b8141
DE
400}
401
34c9978d
DE
402NSPoint wxWindowCocoa::CocoaTransformBoundsToWx(NSPoint pointBounds)
403{
404 // TODO: Handle scrolling offset
ee022549 405 return CocoaTransformNSViewBoundsToWx(GetNSView(), pointBounds);
34c9978d
DE
406}
407
408NSRect wxWindowCocoa::CocoaTransformBoundsToWx(NSRect rectBounds)
409{
410 // TODO: Handle scrolling offset
ee022549 411 return CocoaTransformNSViewBoundsToWx(GetNSView(), rectBounds);
34c9978d
DE
412}
413
414NSPoint wxWindowCocoa::CocoaTransformWxToBounds(NSPoint pointWx)
415{
416 // TODO: Handle scrolling offset
ee022549 417 return CocoaTransformNSViewWxToBounds(GetNSView(), pointWx);
34c9978d
DE
418}
419
420NSRect wxWindowCocoa::CocoaTransformWxToBounds(NSRect rectWx)
421{
422 // TODO: Handle scrolling offset
ee022549 423 return CocoaTransformNSViewWxToBounds(GetNSView(), rectWx);
34c9978d
DE
424}
425
4db3c8ac
DE
426WX_NSAffineTransform wxWindowCocoa::CocoaGetWxToBoundsTransform()
427{
428 // TODO: Handle scrolling offset
429 NSAffineTransform *transform = wxDC::CocoaGetWxToBoundsTransform([GetNSView() isFlipped], [GetNSView() bounds].size.height);
430 return transform;
431}
432
8ea5271e
DE
433bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
434{
48580976 435 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_drawRect"));
55c5be5e
DE
436 // Recursion can happen if the event loop runs from within the paint
437 // handler. For instance, if an assertion dialog is shown.
438 // FIXME: This seems less than ideal.
439 if(m_isInPaint)
440 {
2b030203 441 wxLogDebug(wxT("Paint event recursion!"));
55c5be5e
DE
442 return false;
443 }
8d8d3633 444 m_isInPaint = true;
dc5bcaef
DE
445
446 // Set m_updateRegion
447 const NSRect *rects = &rect; // The bounding box of the region
448 int countRects = 1;
449 // Try replacing the larger rectangle with a list of smaller ones:
5dc47140
DE
450 if ([GetNSView() respondsToSelector:@selector(getRectsBeingDrawn:count:)])
451 [GetNSView() getRectsBeingDrawn:&rects count:&countRects];
34c9978d
DE
452
453 NSRect *transformedRects = (NSRect*)malloc(sizeof(NSRect)*countRects);
454 for(int i=0; i<countRects; i++)
455 {
456 transformedRects[i] = CocoaTransformBoundsToWx(rects[i]);
457 }
458 m_updateRegion = wxRegion(transformedRects,countRects);
459 free(transformedRects);
dc5bcaef 460
8ea5271e
DE
461 wxPaintEvent event(m_windowId);
462 event.SetEventObject(this);
55c5be5e 463 bool ret = GetEventHandler()->ProcessEvent(event);
8d8d3633 464 m_isInPaint = false;
55c5be5e 465 return ret;
8ea5271e
DE
466}
467
69dbb709
DE
468void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
469{
2b030203 470 wxASSERT_MSG([m_cocoaNSView window]==[cocoaEvent window],wxT("Mouse event for different NSWindow"));
34c9978d
DE
471 // Mouse events happen at the NSWindow level so we need to convert
472 // into our bounds coordinates then convert to wx coordinates.
a82b8141 473 NSPoint cocoaPoint = [m_cocoaNSView convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
34c9978d
DE
474 NSPoint pointWx = CocoaTransformBoundsToWx(cocoaPoint);
475 // FIXME: Should we be adjusting for client area origin?
69dbb709 476 const wxPoint &clientorigin = GetClientAreaOrigin();
34c9978d
DE
477 event.m_x = (wxCoord)pointWx.x - clientorigin.x;
478 event.m_y = (wxCoord)pointWx.y - clientorigin.y;
69dbb709
DE
479
480 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
481 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
482 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
483 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
484
485 // TODO: set timestamp?
486 event.SetEventObject(this);
487 event.SetId(GetId());
488}
489
490bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
491{
492 wxMouseEvent event(wxEVT_MOTION);
493 InitMouseEvent(event,theEvent);
48580976 494 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
69dbb709
DE
495 return GetEventHandler()->ProcessEvent(event);
496}
497
498bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
499{
500 return false;
501}
502
503bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
504{
505 return false;
506}
507
508bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
509{
510 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
511 InitMouseEvent(event,theEvent);
48580976 512 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]);
69dbb709
DE
513 return GetEventHandler()->ProcessEvent(event);
514}
515
516bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
517{
518 wxMouseEvent event(wxEVT_MOTION);
519 InitMouseEvent(event,theEvent);
520 event.m_leftDown = true;
48580976 521 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
69dbb709
DE
522 return GetEventHandler()->ProcessEvent(event);
523}
524
525bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
526{
527 wxMouseEvent event(wxEVT_LEFT_UP);
528 InitMouseEvent(event,theEvent);
48580976 529 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Up @%d,%d"),event.m_x,event.m_y);
69dbb709
DE
530 return GetEventHandler()->ProcessEvent(event);
531}
532
533bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
534{
eafde5c7
DE
535 wxMouseEvent event([theEvent clickCount]<2?wxEVT_RIGHT_DOWN:wxEVT_RIGHT_DCLICK);
536 InitMouseEvent(event,theEvent);
537 wxLogDebug(wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]);
538 return GetEventHandler()->ProcessEvent(event);
69dbb709
DE
539}
540
541bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
542{
eafde5c7
DE
543 wxMouseEvent event(wxEVT_MOTION);
544 InitMouseEvent(event,theEvent);
545 event.m_rightDown = true;
546 wxLogDebug(wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
547 return GetEventHandler()->ProcessEvent(event);
69dbb709
DE
548}
549
550bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
551{
eafde5c7
DE
552 wxMouseEvent event(wxEVT_RIGHT_UP);
553 InitMouseEvent(event,theEvent);
554 wxLogDebug(wxT("Mouse Up @%d,%d"),event.m_x,event.m_y);
555 return GetEventHandler()->ProcessEvent(event);
69dbb709
DE
556}
557
558bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
559{
560 return false;
561}
562
563bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
564{
565 return false;
566}
567
568bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
569{
570 return false;
571}
572
fb896a32
DE
573void wxWindowCocoa::Cocoa_FrameChanged(void)
574{
48580976 575 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
fb896a32
DE
576 wxSizeEvent event(GetSize(), m_windowId);
577 event.SetEventObject(this);
578 GetEventHandler()->ProcessEvent(event);
579}
580
5558135c
RN
581bool wxWindowCocoa::Cocoa_resetCursorRects()
582{
583 if(!m_cursor.GetNSCursor())
584 return false;
8d8d3633
WS
585
586 [GetNSView() addCursorRect: [GetNSView() visibleRect] cursor: m_cursor.GetNSCursor()];
587
5558135c
RN
588 return true;
589}
590
fb896a32
DE
591bool wxWindow::Close(bool force)
592{
cc6f960f
DE
593 // The only reason this function exists is that it is virtual and
594 // wxTopLevelWindowCocoa will override it.
595 return wxWindowBase::Close(force);
fb896a32
DE
596}
597
a82b8141
DE
598void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
599{
600 [[oldView superview] replaceSubview:oldView with:newView];
601}
602
47a8a4d5 603void wxWindow::DoEnable(bool enable)
adb4816c 604{
47a8a4d5 605 CocoaSetEnabled(enable);
adb4816c
DE
606}
607
fb896a32
DE
608bool wxWindow::Show(bool show)
609{
7fc77f30 610 wxAutoNSAutoreleasePool pool;
fb896a32
DE
611 // If the window is marked as visible, then it shouldn't have a dummy view
612 // If the window is marked hidden, then it should have a dummy view
addbdd29 613 // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
2b030203 614// wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),wxT("wxWindow: m_isShown does not agree with m_dummyNSView"));
fb896a32 615 // Return false if there isn't a window to show or hide
a82b8141
DE
616 NSView *cocoaView = GetNSViewForHiding();
617 if(!cocoaView)
fb896a32 618 return false;
fb896a32
DE
619 if(show)
620 {
addbdd29 621 // If state isn't changing, return false
a82b8141 622 if(!m_cocoaHider)
addbdd29 623 return false;
a82b8141
DE
624 CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView);
625 wxASSERT(![m_cocoaHider->GetNSView() superview]);
626 delete m_cocoaHider;
627 m_cocoaHider = NULL;
628 wxASSERT([cocoaView superview]);
fb896a32
DE
629 }
630 else
631 {
addbdd29 632 // If state isn't changing, return false
a82b8141 633 if(m_cocoaHider)
addbdd29 634 return false;
a82b8141
DE
635 m_cocoaHider = new wxWindowCocoaHider(this);
636 // NOTE: replaceSubview:with will cause m_cocaNSView to be
637 // (auto)released which balances out addSubview
638 CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView());
fb896a32 639 // m_coocaNSView is now only retained by us
a82b8141
DE
640 wxASSERT([m_cocoaHider->GetNSView() superview]);
641 wxASSERT(![cocoaView superview]);
fb896a32 642 }
a6b4ff2e
DE
643 m_isShown = show;
644 return true;
fb896a32
DE
645}
646
647void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
648{
9879fa84 649 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
650 int currentX, currentY;
651 int currentW, currentH;
652 DoGetPosition(&currentX, &currentY);
653 DoGetSize(&currentW, &currentH);
654 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
655 x=currentX;
656 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
657 y=currentY;
658
659 AdjustForParentClientOrigin(x,y,sizeFlags);
660
8d8d3633 661 wxSize size(wxDefaultSize);
fb896a32
DE
662
663 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
664 {
665 if(sizeFlags&wxSIZE_AUTO_WIDTH)
666 {
667 size=DoGetBestSize();
668 width=size.x;
669 }
670 else
671 width=currentW;
672 }
673 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
674 {
675 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
676 {
677 if(size.x==-1)
678 size=DoGetBestSize();
679 height=size.y;
680 }
681 else
682 height=currentH;
683 }
684 DoMoveWindow(x,y,width,height);
685}
686
1e151594 687#if wxUSE_TOOLTIPS
26191790
RN
688
689void wxWindowCocoa::DoSetToolTip( wxToolTip *tip )
690{
691 wxWindowBase::DoSetToolTip(tip);
692
26191790
RN
693 if ( m_tooltip )
694 {
695 m_tooltip->SetWindow((wxWindow *)this);
26191790
RN
696 }
697}
698
1e151594
RN
699#endif
700
fb896a32
DE
701void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
702{
bed6fe0c 703 wxAutoNSAutoreleasePool pool;
9879fa84 704 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
fb896a32 705
a82b8141 706 NSView *nsview = GetNSViewForSuperview();
d449cf47 707 NSView *superview = [nsview superview];
fb896a32 708
34c9978d
DE
709 wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent"));
710
711 NSRect oldFrameRect = [nsview frame];
712 NSRect newFrameRect = GetParent()->CocoaTransformWxToBounds(NSMakeRect(x,y,width,height));
713 [nsview setFrame:newFrameRect];
b915b805 714 // Be sure to redraw the parent to reflect the changed position
34c9978d
DE
715 [superview setNeedsDisplayInRect:oldFrameRect];
716 [superview setNeedsDisplayInRect:newFrameRect];
fb896a32
DE
717}
718
d139c3a8
DE
719void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size)
720{
721 NSView *nsview = GetNSViewForSuperview();
722 NSView *superview = [nsview superview];
2b030203 723 wxCHECK_RET(superview,wxT("NSView does not have a superview"));
34c9978d 724 wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent"));
d139c3a8
DE
725 NSRect frameRect = [nsview frame];
726 if(size.x!=-1)
727 frameRect.size.width = size.x;
728 if(size.y!=-1)
729 frameRect.size.height = size.y;
730 frameRect.origin.x = pos.x;
34c9978d 731 frameRect.origin.y = pos.y;
c5bd9191
DE
732 // Tell Cocoa to change the margin between the bottom of the superview
733 // and the bottom of the control. Keeps the control pinned to the top
065e208e 734 // of its superview so that its position in the wxWidgets coordinate
c5bd9191
DE
735 // system doesn't change.
736 if(![superview isFlipped])
737 [nsview setAutoresizingMask: NSViewMinYMargin];
6f2ec3c3
DE
738 // MUST set the mask before setFrame: which can generate a size event
739 // and cause a scroller to be added!
34c9978d 740 frameRect = GetParent()->CocoaTransformWxToBounds(frameRect);
6f2ec3c3 741 [nsview setFrame: frameRect];
d139c3a8
DE
742}
743
fb896a32
DE
744// Get total size
745void wxWindow::DoGetSize(int *w, int *h) const
746{
a82b8141 747 NSRect cocoaRect = [GetNSViewForSuperview() frame];
fb896a32
DE
748 if(w)
749 *w=(int)cocoaRect.size.width;
750 if(h)
751 *h=(int)cocoaRect.size.height;
9879fa84 752 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
fb896a32
DE
753}
754
755void wxWindow::DoGetPosition(int *x, int *y) const
756{
a82b8141 757 NSView *nsview = GetNSViewForSuperview();
fb896a32 758
576a1544 759 NSRect cocoaRect = [nsview frame];
34c9978d 760 NSRect rectWx = GetParent()->CocoaTransformBoundsToWx(cocoaRect);
fb896a32 761 if(x)
34c9978d 762 *x=(int)rectWx.origin.x;
fb896a32 763 if(y)
34c9978d 764 *y=(int)rectWx.origin.y;
9879fa84 765 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
fb896a32
DE
766}
767
768WXWidget wxWindow::GetHandle() const
769{
770 return m_cocoaNSView;
771}
772
f7e98dee
RN
773wxWindow* wxWindow::GetWxWindow() const
774{
775 return (wxWindow*) this;
776}
777
ddf7346a
DE
778void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
779{
780 [m_cocoaNSView setNeedsDisplay:YES];
781}
782
fb896a32
DE
783void wxWindow::SetFocus()
784{
67d2dac8
DE
785 if([GetNSView() acceptsFirstResponder])
786 [[GetNSView() window] makeFirstResponder: GetNSView()];
fb896a32
DE
787}
788
789void wxWindow::DoCaptureMouse()
790{
791 // TODO
b9505233 792 sm_capturedWindow = this;
fb896a32
DE
793}
794
795void wxWindow::DoReleaseMouse()
796{
797 // TODO
b9505233 798 sm_capturedWindow = NULL;
fb896a32
DE
799}
800
801void wxWindow::DoScreenToClient(int *x, int *y) const
802{
803 // TODO
804}
805
806void wxWindow::DoClientToScreen(int *x, int *y) const
807{
808 // TODO
809}
810
811// Get size *available for subwindows* i.e. excluding menu bar etc.
812void wxWindow::DoGetClientSize(int *x, int *y) const
813{
48580976 814 wxLogTrace(wxTRACE_COCOA,wxT("DoGetClientSize:"));
f298b203
DE
815 if(m_wxCocoaScrollView)
816 m_wxCocoaScrollView->DoGetClientSize(x,y);
816c52cf
DE
817 else
818 wxWindowCocoa::DoGetSize(x,y);
fb896a32
DE
819}
820
821void wxWindow::DoSetClientSize(int width, int height)
822{
48580976 823 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("DoSetClientSize=(%d,%d)"),width,height);
f298b203
DE
824 if(m_wxCocoaScrollView)
825 m_wxCocoaScrollView->ClientSizeToSize(width,height);
e08efb8d
DE
826 CocoaSetWxWindowSize(width,height);
827}
828
829void wxWindow::CocoaSetWxWindowSize(int width, int height)
830{
8d8d3633
WS
831 wxWindowCocoa::DoSetSize(wxDefaultCoord,wxDefaultCoord,width,height,wxSIZE_USE_EXISTING);
832}
833
834void wxWindow::SetLabel(const wxString& WXUNUSED(label))
835{
836 // TODO
837}
838
839wxString wxWindow::GetLabel() const
840{
841 // TODO
842 return wxEmptyString;
fb896a32
DE
843}
844
845int wxWindow::GetCharHeight() const
846{
847 // TODO
848 return 0;
849}
850
851int wxWindow::GetCharWidth() const
852{
853 // TODO
854 return 0;
855}
856
857void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
858 int *descent, int *externalLeading, const wxFont *theFont) const
859{
860 // TODO
861}
862
fb896a32
DE
863// Coordinates relative to the window
864void wxWindow::WarpPointer (int x_pos, int y_pos)
865{
866 // TODO
867}
868
869int wxWindow::GetScrollPos(int orient) const
870{
871 // TODO
872 return 0;
873}
874
875// This now returns the whole range, not just the number
876// of positions that we can scroll.
877int wxWindow::GetScrollRange(int orient) const
878{
879 // TODO
880 return 0;
881}
882
883int wxWindow::GetScrollThumb(int orient) const
884{
885 // TODO
886 return 0;
887}
888
889void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
890{
891 // TODO
892}
893
816c52cf
DE
894void wxWindow::CocoaCreateNSScrollView()
895{
f298b203 896 if(!m_wxCocoaScrollView)
816c52cf 897 {
f298b203 898 m_wxCocoaScrollView = new wxWindowCocoaScrollView(this);
816c52cf
DE
899 }
900}
901
fb896a32
DE
902// New function that will replace some of the above.
903void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
904 int range, bool refresh)
905{
e08efb8d 906 CocoaCreateNSScrollView();
fb896a32
DE
907 // TODO
908}
909
910// Does a physical scroll
911void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
912{
913 // TODO
914}
915
816c52cf
DE
916void wxWindow::DoSetVirtualSize( int x, int y )
917{
918 wxWindowBase::DoSetVirtualSize(x,y);
919 CocoaCreateNSScrollView();
920 [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)];
921}
922
fb896a32
DE
923bool wxWindow::SetFont(const wxFont& font)
924{
925 // TODO
8d8d3633 926 return true;
fb896a32
DE
927}
928
4328a6ba
DE
929static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
930{
931 // first should be ordered higher
932 if(first==target)
933 return NSOrderedDescending;
934 // second should be ordered higher
935 if(second==target)
936 return NSOrderedAscending;
937 return NSOrderedSame;
938}
939
fb896a32
DE
940// Raise the window to the top of the Z order
941void wxWindow::Raise()
942{
4328a6ba 943// wxAutoNSAutoreleasePool pool;
a82b8141 944 NSView *nsview = GetNSViewForSuperview();
4328a6ba
DE
945 [[nsview superview] sortSubviewsUsingFunction:
946 CocoaRaiseWindowCompareFunction
947 context: nsview];
948}
949
950static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
951{
952 // first should be ordered lower
953 if(first==target)
954 return NSOrderedAscending;
955 // second should be ordered lower
956 if(second==target)
957 return NSOrderedDescending;
958 return NSOrderedSame;
fb896a32
DE
959}
960
961// Lower the window to the bottom of the Z order
962void wxWindow::Lower()
963{
4328a6ba
DE
964 NSView *nsview = GetNSViewForSuperview();
965 [[nsview superview] sortSubviewsUsingFunction:
966 CocoaLowerWindowCompareFunction
967 context: nsview];
fb896a32
DE
968}
969
970bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
971{
8d8d3633 972 return false;
fb896a32
DE
973}
974
975// Get the window with the focus
dcb68102 976wxWindow *wxWindowBase::DoFindFocus()
fb896a32 977{
67d2dac8
DE
978 // Basically we are somewhat emulating the responder chain here except
979 // we are only loking for the first responder in the key window or
980 // upon failing to find one if the main window is different we look
981 // for the first responder in the main window.
982
983 // Note that the firstResponder doesn't necessarily have to be an
984 // NSView but wxCocoaNSView::GetFromCocoa() will simply return
985 // NULL unless it finds its argument in its hash map.
986
987 wxCocoaNSView *win;
988
989 NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
9151dcec 990 win = wxCocoaNSView::GetFromCocoa(static_cast<NSView*>([keyWindow firstResponder]));
67d2dac8
DE
991 if(win)
992 return win->GetWxWindow();
993
994 NSWindow *mainWindow = [[NSApplication sharedApplication] keyWindow];
995 if(mainWindow == keyWindow)
f7e98dee 996 return NULL;
9151dcec 997 win = wxCocoaNSView::GetFromCocoa(static_cast<NSView*>([mainWindow firstResponder]));
67d2dac8
DE
998 if(win)
999 return win->GetWxWindow();
1000
1001 return NULL;
fb896a32
DE
1002}
1003
1004/* static */ wxWindow *wxWindowBase::GetCapture()
1005{
1006 // TODO
b9505233 1007 return wxWindowCocoa::sm_capturedWindow;
fb896a32
DE
1008}
1009
1010wxWindow *wxGetActiveWindow()
1011{
1012 // TODO
1013 return NULL;
1014}
1015
7c9428ab
DE
1016wxPoint wxGetMousePosition()
1017{
1018 // TODO
1019 return wxDefaultPosition;
1020}
1021
ca5db7b2
WS
1022wxMouseState wxGetMouseState()
1023{
1024 wxMouseState ms;
1025 // TODO
1026 return ms;
1027}
1028
7c9428ab
DE
1029wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1030{
1031 pt = wxGetMousePosition();
1032 return NULL;
1033}