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