]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/window.mm
[ 1508778 ] Fix for wxOwnerDrawnComboBox list selection rendering.
[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"
449c5673 18#endif //WX_PRECOMP
da80ae71 19
e73ae747 20#include "wx/tooltip.h"
fb896a32 21
7fc77f30 22#include "wx/cocoa/autorelease.h"
26191790 23#include "wx/cocoa/string.h"
7fc77f30 24
f910a887 25#import <AppKit/NSView.h>
69dbb709 26#import <AppKit/NSEvent.h>
816c52cf
DE
27#import <AppKit/NSScrollView.h>
28#import <AppKit/NSColor.h>
29#import <AppKit/NSClipView.h>
dc5bcaef 30#import <Foundation/NSException.h>
67d2dac8
DE
31#import <AppKit/NSApplication.h>
32#import <AppKit/NSWindow.h>
dc5bcaef 33
75e4856b
DE
34// Turn this on to paint green over the dummy views for debugging
35#undef WXCOCOA_FILL_DUMMY_VIEW
36
37#ifdef WXCOCOA_FILL_DUMMY_VIEW
38#import <AppKit/NSBezierPath.h>
39#endif //def WXCOCOA_FILL_DUMMY_VIEW
40
5dc47140
DE
41// A category for methods that are only present in Panther's SDK
42@interface NSView(wxNSViewPrePantherCompatibility)
43- (void)getRectsBeingDrawn:(const NSRect **)rects count:(int *)count;
44@end
45
ee022549
DE
46NSPoint CocoaTransformNSViewBoundsToWx(NSView *nsview, NSPoint pointBounds)
47{
48 wxCHECK_MSG(nsview, pointBounds, wxT("Need to have a Cocoa view to do translation"));
49 if([nsview isFlipped])
50 return pointBounds;
51 NSRect ourBounds = [nsview bounds];
52 return NSMakePoint
53 ( pointBounds.x
54 , ourBounds.size.height - pointBounds.y
55 );
56}
57
58NSRect CocoaTransformNSViewBoundsToWx(NSView *nsview, NSRect rectBounds)
59{
60 wxCHECK_MSG(nsview, rectBounds, wxT("Need to have a Cocoa view to do translation"));
61 if([nsview isFlipped])
62 return rectBounds;
63 NSRect ourBounds = [nsview bounds];
64 return NSMakeRect
65 ( rectBounds.origin.x
66 , ourBounds.size.height - (rectBounds.origin.y + rectBounds.size.height)
67 , rectBounds.size.width
68 , rectBounds.size.height
69 );
70}
71
72NSPoint CocoaTransformNSViewWxToBounds(NSView *nsview, NSPoint pointWx)
73{
74 wxCHECK_MSG(nsview, pointWx, wxT("Need to have a Cocoa view to do translation"));
75 if([nsview isFlipped])
76 return pointWx;
77 NSRect ourBounds = [nsview bounds];
78 return NSMakePoint
79 ( pointWx.x
80 , ourBounds.size.height - pointWx.y
81 );
82}
83
84NSRect CocoaTransformNSViewWxToBounds(NSView *nsview, NSRect rectWx)
85{
86 wxCHECK_MSG(nsview, rectWx, wxT("Need to have a Cocoa view to do translation"));
87 if([nsview isFlipped])
88 return rectWx;
89 NSRect ourBounds = [nsview bounds];
90 return NSMakeRect
91 ( rectWx.origin.x
92 , ourBounds.size.height - (rectWx.origin.y + rectWx.size.height)
93 , rectWx.size.width
94 , rectWx.size.height
95 );
96}
97
a82b8141
DE
98// ========================================================================
99// wxWindowCocoaHider
100// ========================================================================
101class wxWindowCocoaHider: protected wxCocoaNSView
102{
103 DECLARE_NO_COPY_CLASS(wxWindowCocoaHider)
104public:
105 wxWindowCocoaHider(wxWindow *owner);
106 virtual ~wxWindowCocoaHider();
107 inline WX_NSView GetNSView() { return m_dummyNSView; }
108protected:
109 wxWindowCocoa *m_owner;
110 WX_NSView m_dummyNSView;
111 virtual void Cocoa_FrameChanged(void);
75e4856b
DE
112#ifdef WXCOCOA_FILL_DUMMY_VIEW
113 virtual bool Cocoa_drawRect(const NSRect& rect);
114#endif //def WXCOCOA_FILL_DUMMY_VIEW
a82b8141
DE
115private:
116 wxWindowCocoaHider();
117};
118
816c52cf 119// ========================================================================
f298b203 120// wxWindowCocoaScrollView
816c52cf 121// ========================================================================
f298b203 122class wxWindowCocoaScrollView: protected wxCocoaNSView
816c52cf 123{
f298b203 124 DECLARE_NO_COPY_CLASS(wxWindowCocoaScrollView)
816c52cf 125public:
f298b203
DE
126 wxWindowCocoaScrollView(wxWindow *owner);
127 virtual ~wxWindowCocoaScrollView();
816c52cf
DE
128 inline WX_NSScrollView GetNSScrollView() { return m_cocoaNSScrollView; }
129 void ClientSizeToSize(int &width, int &height);
130 void DoGetClientSize(int *x, int *y) const;
131 void Encapsulate();
132 void Unencapsulate();
133protected:
134 wxWindowCocoa *m_owner;
135 WX_NSScrollView m_cocoaNSScrollView;
136 virtual void Cocoa_FrameChanged(void);
137private:
f298b203 138 wxWindowCocoaScrollView();
816c52cf
DE
139};
140
75e4856b
DE
141// ========================================================================
142// wxDummyNSView
143// ========================================================================
144@interface wxDummyNSView : NSView
145- (NSView *)hitTest:(NSPoint)aPoint;
146@end
147
148@implementation wxDummyNSView : NSView
149- (NSView *)hitTest:(NSPoint)aPoint
150{
151 return nil;
152}
153
154@end
155
a82b8141
DE
156// ========================================================================
157// wxWindowCocoaHider
158// ========================================================================
159wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner)
160: m_owner(owner)
161{
162 wxASSERT(owner);
163 wxASSERT(owner->GetNSViewForHiding());
75e4856b 164 m_dummyNSView = [[wxDummyNSView alloc]
a82b8141 165 initWithFrame:[owner->GetNSViewForHiding() frame]];
75e4856b 166 [m_dummyNSView setAutoresizingMask: [owner->GetNSViewForHiding() autoresizingMask]];
a82b8141
DE
167 AssociateNSView(m_dummyNSView);
168}
169
170wxWindowCocoaHider::~wxWindowCocoaHider()
171{
172 DisassociateNSView(m_dummyNSView);
173 [m_dummyNSView release];
174}
175
176void wxWindowCocoaHider::Cocoa_FrameChanged(void)
177{
178 // Keep the real window in synch with the dummy
179 wxASSERT(m_dummyNSView);
180 [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]];
181}
182
5558135c 183
75e4856b
DE
184#ifdef WXCOCOA_FILL_DUMMY_VIEW
185bool wxWindowCocoaHider::Cocoa_drawRect(const NSRect& rect)
186{
187 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:rect];
188 [[NSColor greenColor] set];
189 [bezpath stroke];
190 [bezpath fill];
191 return true;
192}
193#endif //def WXCOCOA_FILL_DUMMY_VIEW
194
816c52cf
DE
195// ========================================================================
196// wxFlippedNSClipView
197// ========================================================================
198@interface wxFlippedNSClipView : NSClipView
199- (BOOL)isFlipped;
200@end
201
202@implementation wxFlippedNSClipView : NSClipView
203- (BOOL)isFlipped
204{
205 return YES;
206}
207
208@end
209
210// ========================================================================
f298b203 211// wxWindowCocoaScrollView
816c52cf 212// ========================================================================
f298b203 213wxWindowCocoaScrollView::wxWindowCocoaScrollView(wxWindow *owner)
816c52cf
DE
214: m_owner(owner)
215{
a9861c57 216 wxAutoNSAutoreleasePool pool;
816c52cf
DE
217 wxASSERT(owner);
218 wxASSERT(owner->GetNSView());
219 m_cocoaNSScrollView = [[NSScrollView alloc]
220 initWithFrame:[owner->GetNSView() frame]];
221 AssociateNSView(m_cocoaNSScrollView);
222
223 /* Replace the default NSClipView with a flipped one. This ensures
224 scrolling is "pinned" to the top-left instead of bottom-right. */
225 NSClipView *flippedClip = [[wxFlippedNSClipView alloc]
226 initWithFrame: [[m_cocoaNSScrollView contentView] frame]];
227 [m_cocoaNSScrollView setContentView:flippedClip];
228 [flippedClip release];
229
230 [m_cocoaNSScrollView setBackgroundColor: [NSColor windowBackgroundColor]];
231 [m_cocoaNSScrollView setHasHorizontalScroller: YES];
232 [m_cocoaNSScrollView setHasVerticalScroller: YES];
233 Encapsulate();
234}
235
f298b203 236void wxWindowCocoaScrollView::Encapsulate()
816c52cf 237{
6f2ec3c3
DE
238 // Set the scroll view autoresizingMask to match the current NSView
239 [m_cocoaNSScrollView setAutoresizingMask: [m_owner->GetNSView() autoresizingMask]];
240 [m_owner->GetNSView() setAutoresizingMask: NSViewNotSizable];
816c52cf
DE
241 // NOTE: replaceSubView will cause m_cocaNSView to be released
242 // except when it hasn't been added into an NSView hierarchy in which
243 // case it doesn't need to be and this should work out to a no-op
244 m_owner->CocoaReplaceView(m_owner->GetNSView(), m_cocoaNSScrollView);
245 // The NSView is still retained by owner
246 [m_cocoaNSScrollView setDocumentView: m_owner->GetNSView()];
247 // Now it's also retained by the NSScrollView
248}
249
f298b203 250void wxWindowCocoaScrollView::Unencapsulate()
816c52cf
DE
251{
252 [m_cocoaNSScrollView setDocumentView: nil];
253 m_owner->CocoaReplaceView(m_cocoaNSScrollView, m_owner->GetNSView());
6f2ec3c3
DE
254 if(![[m_owner->GetNSView() superview] isFlipped])
255 [m_owner->GetNSView() setAutoresizingMask: NSViewMinYMargin];
816c52cf
DE
256}
257
f298b203 258wxWindowCocoaScrollView::~wxWindowCocoaScrollView()
816c52cf
DE
259{
260 DisassociateNSView(m_cocoaNSScrollView);
261 [m_cocoaNSScrollView release];
262}
263
f298b203 264void wxWindowCocoaScrollView::ClientSizeToSize(int &width, int &height)
816c52cf
DE
265{
266 NSSize frameSize = [NSScrollView
267 frameSizeForContentSize: NSMakeSize(width,height)
268 hasHorizontalScroller: [m_cocoaNSScrollView hasHorizontalScroller]
269 hasVerticalScroller: [m_cocoaNSScrollView hasVerticalScroller]
270 borderType: [m_cocoaNSScrollView borderType]];
e9cece45
VZ
271 width = (int)frameSize.width;
272 height = (int)frameSize.height;
816c52cf
DE
273}
274
f298b203 275void wxWindowCocoaScrollView::DoGetClientSize(int *x, int *y) const
816c52cf
DE
276{
277 NSSize nssize = [m_cocoaNSScrollView contentSize];
278 if(x)
e9cece45 279 *x = (int)nssize.width;
816c52cf 280 if(y)
e9cece45 281 *y = (int)nssize.height;
816c52cf
DE
282}
283
f298b203 284void wxWindowCocoaScrollView::Cocoa_FrameChanged(void)
816c52cf 285{
48580976 286 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
816c52cf
DE
287 wxSizeEvent event(m_owner->GetSize(), m_owner->GetId());
288 event.SetEventObject(m_owner);
289 m_owner->GetEventHandler()->ProcessEvent(event);
290}
291
a82b8141
DE
292// ========================================================================
293// wxWindowCocoa
294// ========================================================================
fb896a32
DE
295// normally the base classes aren't included, but wxWindow is special
296#ifdef __WXUNIVERSAL__
297IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
298#else
299IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
300#endif
301
302BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
303END_EVENT_TABLE()
304
b9505233
DE
305wxWindow *wxWindowCocoa::sm_capturedWindow = NULL;
306
fb896a32
DE
307// Constructor
308void wxWindowCocoa::Init()
309{
fb896a32 310 m_cocoaNSView = NULL;
a82b8141 311 m_cocoaHider = NULL;
f298b203 312 m_wxCocoaScrollView = NULL;
8d8d3633
WS
313 m_isBeingDeleted = false;
314 m_isInPaint = false;
adb4816c 315 m_shouldBeEnabled = true;
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
adb4816c
DE
603bool wxWindow::EnableSelfAndChildren(bool enable)
604{
605 // If the state isn't changing, don't do anything
606 if(!wxWindowBase::Enable(enable && m_shouldBeEnabled))
607 return false;
608 // Set the state of the Cocoa window
609 CocoaSetEnabled(m_isEnabled);
610 // Disable all children or (if enabling) return them to their proper state
611 for(wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
612 node; node = node->GetNext())
613 {
614 node->GetData()->EnableSelfAndChildren(enable);
615 }
616 return true;
617}
618
619bool wxWindow::Enable(bool enable)
620{
621 // Keep track of what the window SHOULD be doing
622 m_shouldBeEnabled = enable;
623 // If the parent is disabled for any reason, then this window will be too.
624 if(!IsTopLevel() && GetParent())
625 {
626 enable = enable && GetParent()->IsEnabled();
627 }
628 return EnableSelfAndChildren(enable);
629}
630
fb896a32
DE
631bool wxWindow::Show(bool show)
632{
7fc77f30 633 wxAutoNSAutoreleasePool pool;
fb896a32
DE
634 // If the window is marked as visible, then it shouldn't have a dummy view
635 // If the window is marked hidden, then it should have a dummy view
addbdd29 636 // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
2b030203 637// wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),wxT("wxWindow: m_isShown does not agree with m_dummyNSView"));
fb896a32 638 // Return false if there isn't a window to show or hide
a82b8141
DE
639 NSView *cocoaView = GetNSViewForHiding();
640 if(!cocoaView)
fb896a32 641 return false;
fb896a32
DE
642 if(show)
643 {
addbdd29 644 // If state isn't changing, return false
a82b8141 645 if(!m_cocoaHider)
addbdd29 646 return false;
a82b8141
DE
647 CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView);
648 wxASSERT(![m_cocoaHider->GetNSView() superview]);
649 delete m_cocoaHider;
650 m_cocoaHider = NULL;
651 wxASSERT([cocoaView superview]);
fb896a32
DE
652 }
653 else
654 {
addbdd29 655 // If state isn't changing, return false
a82b8141 656 if(m_cocoaHider)
addbdd29 657 return false;
a82b8141
DE
658 m_cocoaHider = new wxWindowCocoaHider(this);
659 // NOTE: replaceSubview:with will cause m_cocaNSView to be
660 // (auto)released which balances out addSubview
661 CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView());
fb896a32 662 // m_coocaNSView is now only retained by us
a82b8141
DE
663 wxASSERT([m_cocoaHider->GetNSView() superview]);
664 wxASSERT(![cocoaView superview]);
fb896a32 665 }
a6b4ff2e
DE
666 m_isShown = show;
667 return true;
fb896a32
DE
668}
669
670void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
671{
9879fa84 672 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
673 int currentX, currentY;
674 int currentW, currentH;
675 DoGetPosition(&currentX, &currentY);
676 DoGetSize(&currentW, &currentH);
677 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
678 x=currentX;
679 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
680 y=currentY;
681
682 AdjustForParentClientOrigin(x,y,sizeFlags);
683
8d8d3633 684 wxSize size(wxDefaultSize);
fb896a32
DE
685
686 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
687 {
688 if(sizeFlags&wxSIZE_AUTO_WIDTH)
689 {
690 size=DoGetBestSize();
691 width=size.x;
692 }
693 else
694 width=currentW;
695 }
696 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
697 {
698 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
699 {
700 if(size.x==-1)
701 size=DoGetBestSize();
702 height=size.y;
703 }
704 else
705 height=currentH;
706 }
707 DoMoveWindow(x,y,width,height);
708}
709
1e151594 710#if wxUSE_TOOLTIPS
26191790
RN
711
712void wxWindowCocoa::DoSetToolTip( wxToolTip *tip )
713{
714 wxWindowBase::DoSetToolTip(tip);
715
26191790
RN
716 if ( m_tooltip )
717 {
718 m_tooltip->SetWindow((wxWindow *)this);
26191790
RN
719 }
720}
721
1e151594
RN
722#endif
723
fb896a32
DE
724void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
725{
bed6fe0c 726 wxAutoNSAutoreleasePool pool;
9879fa84 727 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
fb896a32 728
a82b8141 729 NSView *nsview = GetNSViewForSuperview();
d449cf47 730 NSView *superview = [nsview superview];
fb896a32 731
34c9978d
DE
732 wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent"));
733
734 NSRect oldFrameRect = [nsview frame];
735 NSRect newFrameRect = GetParent()->CocoaTransformWxToBounds(NSMakeRect(x,y,width,height));
736 [nsview setFrame:newFrameRect];
b915b805 737 // Be sure to redraw the parent to reflect the changed position
34c9978d
DE
738 [superview setNeedsDisplayInRect:oldFrameRect];
739 [superview setNeedsDisplayInRect:newFrameRect];
fb896a32
DE
740}
741
d139c3a8
DE
742void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size)
743{
744 NSView *nsview = GetNSViewForSuperview();
745 NSView *superview = [nsview superview];
2b030203 746 wxCHECK_RET(superview,wxT("NSView does not have a superview"));
34c9978d 747 wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent"));
d139c3a8
DE
748 NSRect frameRect = [nsview frame];
749 if(size.x!=-1)
750 frameRect.size.width = size.x;
751 if(size.y!=-1)
752 frameRect.size.height = size.y;
753 frameRect.origin.x = pos.x;
34c9978d 754 frameRect.origin.y = pos.y;
c5bd9191
DE
755 // Tell Cocoa to change the margin between the bottom of the superview
756 // and the bottom of the control. Keeps the control pinned to the top
065e208e 757 // of its superview so that its position in the wxWidgets coordinate
c5bd9191
DE
758 // system doesn't change.
759 if(![superview isFlipped])
760 [nsview setAutoresizingMask: NSViewMinYMargin];
6f2ec3c3
DE
761 // MUST set the mask before setFrame: which can generate a size event
762 // and cause a scroller to be added!
34c9978d 763 frameRect = GetParent()->CocoaTransformWxToBounds(frameRect);
6f2ec3c3 764 [nsview setFrame: frameRect];
d139c3a8
DE
765}
766
fb896a32
DE
767// Get total size
768void wxWindow::DoGetSize(int *w, int *h) const
769{
a82b8141 770 NSRect cocoaRect = [GetNSViewForSuperview() frame];
fb896a32
DE
771 if(w)
772 *w=(int)cocoaRect.size.width;
773 if(h)
774 *h=(int)cocoaRect.size.height;
9879fa84 775 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
fb896a32
DE
776}
777
778void wxWindow::DoGetPosition(int *x, int *y) const
779{
a82b8141 780 NSView *nsview = GetNSViewForSuperview();
fb896a32 781
576a1544 782 NSRect cocoaRect = [nsview frame];
34c9978d 783 NSRect rectWx = GetParent()->CocoaTransformBoundsToWx(cocoaRect);
fb896a32 784 if(x)
34c9978d 785 *x=(int)rectWx.origin.x;
fb896a32 786 if(y)
34c9978d 787 *y=(int)rectWx.origin.y;
9879fa84 788 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
fb896a32
DE
789}
790
791WXWidget wxWindow::GetHandle() const
792{
793 return m_cocoaNSView;
794}
795
f7e98dee
RN
796wxWindow* wxWindow::GetWxWindow() const
797{
798 return (wxWindow*) this;
799}
800
ddf7346a
DE
801void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
802{
803 [m_cocoaNSView setNeedsDisplay:YES];
804}
805
fb896a32
DE
806void wxWindow::SetFocus()
807{
67d2dac8
DE
808 if([GetNSView() acceptsFirstResponder])
809 [[GetNSView() window] makeFirstResponder: GetNSView()];
fb896a32
DE
810}
811
812void wxWindow::DoCaptureMouse()
813{
814 // TODO
b9505233 815 sm_capturedWindow = this;
fb896a32
DE
816}
817
818void wxWindow::DoReleaseMouse()
819{
820 // TODO
b9505233 821 sm_capturedWindow = NULL;
fb896a32
DE
822}
823
824void wxWindow::DoScreenToClient(int *x, int *y) const
825{
826 // TODO
827}
828
829void wxWindow::DoClientToScreen(int *x, int *y) const
830{
831 // TODO
832}
833
834// Get size *available for subwindows* i.e. excluding menu bar etc.
835void wxWindow::DoGetClientSize(int *x, int *y) const
836{
48580976 837 wxLogTrace(wxTRACE_COCOA,wxT("DoGetClientSize:"));
f298b203
DE
838 if(m_wxCocoaScrollView)
839 m_wxCocoaScrollView->DoGetClientSize(x,y);
816c52cf
DE
840 else
841 wxWindowCocoa::DoGetSize(x,y);
fb896a32
DE
842}
843
844void wxWindow::DoSetClientSize(int width, int height)
845{
48580976 846 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("DoSetClientSize=(%d,%d)"),width,height);
f298b203
DE
847 if(m_wxCocoaScrollView)
848 m_wxCocoaScrollView->ClientSizeToSize(width,height);
e08efb8d
DE
849 CocoaSetWxWindowSize(width,height);
850}
851
852void wxWindow::CocoaSetWxWindowSize(int width, int height)
853{
8d8d3633
WS
854 wxWindowCocoa::DoSetSize(wxDefaultCoord,wxDefaultCoord,width,height,wxSIZE_USE_EXISTING);
855}
856
857void wxWindow::SetLabel(const wxString& WXUNUSED(label))
858{
859 // TODO
860}
861
862wxString wxWindow::GetLabel() const
863{
864 // TODO
865 return wxEmptyString;
fb896a32
DE
866}
867
868int wxWindow::GetCharHeight() const
869{
870 // TODO
871 return 0;
872}
873
874int wxWindow::GetCharWidth() const
875{
876 // TODO
877 return 0;
878}
879
880void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
881 int *descent, int *externalLeading, const wxFont *theFont) const
882{
883 // TODO
884}
885
fb896a32
DE
886// Coordinates relative to the window
887void wxWindow::WarpPointer (int x_pos, int y_pos)
888{
889 // TODO
890}
891
892int wxWindow::GetScrollPos(int orient) const
893{
894 // TODO
895 return 0;
896}
897
898// This now returns the whole range, not just the number
899// of positions that we can scroll.
900int wxWindow::GetScrollRange(int orient) const
901{
902 // TODO
903 return 0;
904}
905
906int wxWindow::GetScrollThumb(int orient) const
907{
908 // TODO
909 return 0;
910}
911
912void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
913{
914 // TODO
915}
916
816c52cf
DE
917void wxWindow::CocoaCreateNSScrollView()
918{
f298b203 919 if(!m_wxCocoaScrollView)
816c52cf 920 {
f298b203 921 m_wxCocoaScrollView = new wxWindowCocoaScrollView(this);
816c52cf
DE
922 }
923}
924
fb896a32
DE
925// New function that will replace some of the above.
926void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
927 int range, bool refresh)
928{
e08efb8d 929 CocoaCreateNSScrollView();
fb896a32
DE
930 // TODO
931}
932
933// Does a physical scroll
934void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
935{
936 // TODO
937}
938
816c52cf
DE
939void wxWindow::DoSetVirtualSize( int x, int y )
940{
941 wxWindowBase::DoSetVirtualSize(x,y);
942 CocoaCreateNSScrollView();
943 [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)];
944}
945
fb896a32
DE
946bool wxWindow::SetFont(const wxFont& font)
947{
948 // TODO
8d8d3633 949 return true;
fb896a32
DE
950}
951
4328a6ba
DE
952static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
953{
954 // first should be ordered higher
955 if(first==target)
956 return NSOrderedDescending;
957 // second should be ordered higher
958 if(second==target)
959 return NSOrderedAscending;
960 return NSOrderedSame;
961}
962
fb896a32
DE
963// Raise the window to the top of the Z order
964void wxWindow::Raise()
965{
4328a6ba 966// wxAutoNSAutoreleasePool pool;
a82b8141 967 NSView *nsview = GetNSViewForSuperview();
4328a6ba
DE
968 [[nsview superview] sortSubviewsUsingFunction:
969 CocoaRaiseWindowCompareFunction
970 context: nsview];
971}
972
973static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
974{
975 // first should be ordered lower
976 if(first==target)
977 return NSOrderedAscending;
978 // second should be ordered lower
979 if(second==target)
980 return NSOrderedDescending;
981 return NSOrderedSame;
fb896a32
DE
982}
983
984// Lower the window to the bottom of the Z order
985void wxWindow::Lower()
986{
4328a6ba
DE
987 NSView *nsview = GetNSViewForSuperview();
988 [[nsview superview] sortSubviewsUsingFunction:
989 CocoaLowerWindowCompareFunction
990 context: nsview];
fb896a32
DE
991}
992
993bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
994{
8d8d3633 995 return false;
fb896a32
DE
996}
997
998// Get the window with the focus
dcb68102 999wxWindow *wxWindowBase::DoFindFocus()
fb896a32 1000{
67d2dac8
DE
1001 // Basically we are somewhat emulating the responder chain here except
1002 // we are only loking for the first responder in the key window or
1003 // upon failing to find one if the main window is different we look
1004 // for the first responder in the main window.
1005
1006 // Note that the firstResponder doesn't necessarily have to be an
1007 // NSView but wxCocoaNSView::GetFromCocoa() will simply return
1008 // NULL unless it finds its argument in its hash map.
1009
1010 wxCocoaNSView *win;
1011
1012 NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
9151dcec 1013 win = wxCocoaNSView::GetFromCocoa(static_cast<NSView*>([keyWindow firstResponder]));
67d2dac8
DE
1014 if(win)
1015 return win->GetWxWindow();
1016
1017 NSWindow *mainWindow = [[NSApplication sharedApplication] keyWindow];
1018 if(mainWindow == keyWindow)
f7e98dee 1019 return NULL;
9151dcec 1020 win = wxCocoaNSView::GetFromCocoa(static_cast<NSView*>([mainWindow firstResponder]));
67d2dac8
DE
1021 if(win)
1022 return win->GetWxWindow();
1023
1024 return NULL;
fb896a32
DE
1025}
1026
1027/* static */ wxWindow *wxWindowBase::GetCapture()
1028{
1029 // TODO
b9505233 1030 return wxWindowCocoa::sm_capturedWindow;
fb896a32
DE
1031}
1032
1033wxWindow *wxGetActiveWindow()
1034{
1035 // TODO
1036 return NULL;
1037}
1038
7c9428ab
DE
1039wxPoint wxGetMousePosition()
1040{
1041 // TODO
1042 return wxDefaultPosition;
1043}
1044
1045wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1046{
1047 pt = wxGetMousePosition();
1048 return NULL;
1049}