various cleanups
[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
29 #include <objc/objc-runtime.h>
30
31 // Turn this on to paint green over the dummy views for debugging
32 #undef WXCOCOA_FILL_DUMMY_VIEW
33
34 #ifdef WXCOCOA_FILL_DUMMY_VIEW
35 #import <AppKit/NSBezierPath.h>
36 #endif //def WXCOCOA_FILL_DUMMY_VIEW
37
38 // ========================================================================
39 // wxWindowCocoaHider
40 // ========================================================================
41 class wxWindowCocoaHider: protected wxCocoaNSView
42 {
43     DECLARE_NO_COPY_CLASS(wxWindowCocoaHider)
44 public:
45     wxWindowCocoaHider(wxWindow *owner);
46     virtual ~wxWindowCocoaHider();
47     inline WX_NSView GetNSView() { return m_dummyNSView; }
48 protected:
49     wxWindowCocoa *m_owner;
50     WX_NSView m_dummyNSView;
51     virtual void Cocoa_FrameChanged(void);
52 #ifdef WXCOCOA_FILL_DUMMY_VIEW
53     virtual bool Cocoa_drawRect(const NSRect& rect);
54 #endif //def WXCOCOA_FILL_DUMMY_VIEW
55 private:
56     wxWindowCocoaHider();
57 };
58
59 // ========================================================================
60 // wxWindowCocoaScroller
61 // ========================================================================
62 class wxWindowCocoaScroller: protected wxCocoaNSView
63 {
64     DECLARE_NO_COPY_CLASS(wxWindowCocoaScroller)
65 public:
66     wxWindowCocoaScroller(wxWindow *owner);
67     virtual ~wxWindowCocoaScroller();
68     inline WX_NSScrollView GetNSScrollView() { return m_cocoaNSScrollView; }
69     void ClientSizeToSize(int &width, int &height);
70     void DoGetClientSize(int *x, int *y) const;
71     void Encapsulate();
72     void Unencapsulate();
73 protected:
74     wxWindowCocoa *m_owner;
75     WX_NSScrollView m_cocoaNSScrollView;
76     virtual void Cocoa_FrameChanged(void);
77 private:
78     wxWindowCocoaScroller();
79 };
80
81 // ========================================================================
82 // wxDummyNSView
83 // ========================================================================
84 @interface wxDummyNSView : NSView
85 - (NSView *)hitTest:(NSPoint)aPoint;
86 @end
87
88 @implementation wxDummyNSView : NSView
89 - (NSView *)hitTest:(NSPoint)aPoint
90 {
91     return nil;
92 }
93
94 @end
95
96 // ========================================================================
97 // wxWindowCocoaHider
98 // ========================================================================
99 wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner)
100 :   m_owner(owner)
101 {
102     wxASSERT(owner);
103     wxASSERT(owner->GetNSViewForHiding());
104     m_dummyNSView = [[wxDummyNSView alloc]
105         initWithFrame:[owner->GetNSViewForHiding() frame]];
106     [m_dummyNSView setAutoresizingMask: [owner->GetNSViewForHiding() autoresizingMask]];
107     AssociateNSView(m_dummyNSView);
108 }
109
110 wxWindowCocoaHider::~wxWindowCocoaHider()
111 {
112     DisassociateNSView(m_dummyNSView);
113     [m_dummyNSView release];
114 }
115
116 void wxWindowCocoaHider::Cocoa_FrameChanged(void)
117 {
118     // Keep the real window in synch with the dummy
119     wxASSERT(m_dummyNSView);
120     [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]];
121 }
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 wxWindowCocoa::Cocoa_resetCursorRects()
485 {
486     if(!m_cursor.GetNSCursor())
487         return false;
488     
489     [GetNSView() addCursorRect: [GetNSView() visibleRect]  cursor: m_cursor.GetNSCursor()];    
490         
491     return true;
492 }
493
494 bool wxWindow::Close(bool force)
495 {
496     // The only reason this function exists is that it is virtual and
497     // wxTopLevelWindowCocoa will override it.
498     return wxWindowBase::Close(force);
499 }
500
501 void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
502 {
503     [[oldView superview] replaceSubview:oldView with:newView];
504 }
505
506 bool wxWindow::EnableSelfAndChildren(bool enable)
507 {
508     // If the state isn't changing, don't do anything
509     if(!wxWindowBase::Enable(enable && m_shouldBeEnabled))
510         return false;
511     // Set the state of the Cocoa window
512     CocoaSetEnabled(m_isEnabled);
513     // Disable all children or (if enabling) return them to their proper state
514     for(wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
515         node; node = node->GetNext())
516     {
517         node->GetData()->EnableSelfAndChildren(enable);
518     }
519     return true;
520 }
521
522 bool wxWindow::Enable(bool enable)
523 {
524     // Keep track of what the window SHOULD be doing
525     m_shouldBeEnabled = enable;
526     // If the parent is disabled for any reason, then this window will be too.
527     if(!IsTopLevel() && GetParent())
528     {
529         enable = enable && GetParent()->IsEnabled();
530     }
531     return EnableSelfAndChildren(enable);
532 }
533
534 bool wxWindow::Show(bool show)
535 {
536     wxAutoNSAutoreleasePool pool;
537     // If the window is marked as visible, then it shouldn't have a dummy view
538     // If the window is marked hidden, then it should have a dummy view
539     // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
540 //    wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),wxT("wxWindow: m_isShown does not agree with m_dummyNSView"));
541     // Return false if there isn't a window to show or hide
542     NSView *cocoaView = GetNSViewForHiding();
543     if(!cocoaView)
544         return false;
545     if(show)
546     {
547         // If state isn't changing, return false
548         if(!m_cocoaHider)
549             return false;
550         CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView);
551         wxASSERT(![m_cocoaHider->GetNSView() superview]);
552         delete m_cocoaHider;
553         m_cocoaHider = NULL;
554         wxASSERT([cocoaView superview]);
555     }
556     else
557     {
558         // If state isn't changing, return false
559         if(m_cocoaHider)
560             return false;
561         m_cocoaHider = new wxWindowCocoaHider(this);
562         // NOTE: replaceSubview:with will cause m_cocaNSView to be
563         // (auto)released which balances out addSubview
564         CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView());
565         // m_coocaNSView is now only retained by us
566         wxASSERT([m_cocoaHider->GetNSView() superview]);
567         wxASSERT(![cocoaView superview]);
568     }
569     m_isShown = show;
570     return true;
571 }
572
573 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
574 {
575     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":".");
576     int currentX, currentY;
577     int currentW, currentH;
578     DoGetPosition(&currentX, &currentY);
579     DoGetSize(&currentW, &currentH);
580     if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
581         x=currentX;
582     if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
583         y=currentY;
584
585     AdjustForParentClientOrigin(x,y,sizeFlags);
586
587     wxSize size(-1,-1);
588
589     if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
590     {
591         if(sizeFlags&wxSIZE_AUTO_WIDTH)
592         {
593             size=DoGetBestSize();
594             width=size.x;
595         }
596         else
597             width=currentW;
598     }
599     if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
600     {
601         if(sizeFlags&wxSIZE_AUTO_HEIGHT)
602         {
603             if(size.x==-1)
604                 size=DoGetBestSize();
605             height=size.y;
606         }
607         else
608             height=currentH;
609     }
610     DoMoveWindow(x,y,width,height);
611 }
612
613 #if wxUSE_TOOLTIPS
614
615 void wxWindowCocoa::DoSetToolTip( wxToolTip *tip )
616 {
617     wxWindowBase::DoSetToolTip(tip);
618
619     if ( m_tooltip )
620     {
621         m_tooltip->SetWindow((wxWindow *)this);
622     }
623 }
624
625 #endif
626
627 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
628 {
629     wxAutoNSAutoreleasePool pool;
630     wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
631
632     NSView *nsview = GetNSViewForSuperview();
633     NSView *superview = [nsview superview];
634     wxCHECK_RET(superview,wxT("NSView does not have a superview"));
635     NSRect parentRect = [superview bounds];
636
637     NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
638     [nsview setFrame: cocoaRect];
639     // Be sure to redraw the parent to reflect the changed position
640     [superview setNeedsDisplay:YES];
641 }
642
643 void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size)
644 {
645     NSView *nsview = GetNSViewForSuperview();
646     NSView *superview = [nsview superview];
647     wxCHECK_RET(superview,wxT("NSView does not have a superview"));
648     NSRect parentRect = [superview bounds];
649     NSRect frameRect = [nsview frame];
650     if(size.x!=-1)
651         frameRect.size.width = size.x;
652     if(size.y!=-1)
653         frameRect.size.height = size.y;
654     frameRect.origin.x = pos.x;
655     frameRect.origin.y = parentRect.size.height-(pos.y+frameRect.size.height);
656     // Tell Cocoa to change the margin between the bottom of the superview
657     // and the bottom of the control.  Keeps the control pinned to the top
658     // of its superview so that its position in the wxWidgets coordinate
659     // system doesn't change.
660     if(![superview isFlipped])
661         [nsview setAutoresizingMask: NSViewMinYMargin];
662     // MUST set the mask before setFrame: which can generate a size event
663     // and cause a scroller to be added!
664     [nsview setFrame: frameRect];
665 }
666
667 // Get total size
668 void wxWindow::DoGetSize(int *w, int *h) const
669 {
670     NSRect cocoaRect = [GetNSViewForSuperview() frame];
671     if(w)
672         *w=(int)cocoaRect.size.width;
673     if(h)
674         *h=(int)cocoaRect.size.height;
675     wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
676 }
677
678 void wxWindow::DoGetPosition(int *x, int *y) const
679 {
680     NSView *nsview = GetNSViewForSuperview();
681     NSView *superview = [nsview superview];
682     wxCHECK_RET(superview,wxT("NSView does not have a superview"));
683     NSRect parentRect = [superview bounds];
684
685     NSRect cocoaRect = [nsview frame];
686     if(x)
687         *x=(int)cocoaRect.origin.x;
688     if(y)
689         *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
690     wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
691 }
692
693 WXWidget wxWindow::GetHandle() const
694 {
695     return m_cocoaNSView;
696 }
697
698 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
699 {
700     [m_cocoaNSView setNeedsDisplay:YES];
701 }
702
703 void wxWindow::SetFocus()
704 {
705     // TODO
706 }
707
708 void wxWindow::DoCaptureMouse()
709 {
710     // TODO
711     sm_capturedWindow = this;
712 }
713
714 void wxWindow::DoReleaseMouse()
715 {
716     // TODO
717     sm_capturedWindow = NULL;
718 }
719
720 void wxWindow::DoScreenToClient(int *x, int *y) const
721 {
722     // TODO
723 }
724
725 void wxWindow::DoClientToScreen(int *x, int *y) const
726 {
727     // TODO
728 }
729
730 // Get size *available for subwindows* i.e. excluding menu bar etc.
731 void wxWindow::DoGetClientSize(int *x, int *y) const
732 {
733     wxLogTrace(wxTRACE_COCOA,wxT("DoGetClientSize:"));
734     if(m_cocoaScroller)
735         m_cocoaScroller->DoGetClientSize(x,y);
736     else
737         wxWindowCocoa::DoGetSize(x,y);
738 }
739
740 void wxWindow::DoSetClientSize(int width, int height)
741 {
742     wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("DoSetClientSize=(%d,%d)"),width,height);
743     if(m_cocoaScroller)
744         m_cocoaScroller->ClientSizeToSize(width,height);
745     CocoaSetWxWindowSize(width,height);
746 }
747
748 void wxWindow::CocoaSetWxWindowSize(int width, int height)
749 {
750     wxWindowCocoa::DoSetSize(-1,-1,width,height,wxSIZE_USE_EXISTING);
751 }
752
753 int wxWindow::GetCharHeight() const
754 {
755     // TODO
756     return 0;
757 }
758
759 int wxWindow::GetCharWidth() const
760 {
761     // TODO
762     return 0;
763 }
764
765 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
766         int *descent, int *externalLeading, const wxFont *theFont) const
767 {
768     // TODO
769 }
770
771 // Coordinates relative to the window
772 void wxWindow::WarpPointer (int x_pos, int y_pos)
773 {
774     // TODO
775 }
776
777 int wxWindow::GetScrollPos(int orient) const
778 {
779     // TODO
780     return 0;
781 }
782
783 // This now returns the whole range, not just the number
784 // of positions that we can scroll.
785 int wxWindow::GetScrollRange(int orient) const
786 {
787     // TODO
788     return 0;
789 }
790
791 int wxWindow::GetScrollThumb(int orient) const
792 {
793     // TODO
794     return 0;
795 }
796
797 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
798 {
799     // TODO
800 }
801
802 void wxWindow::CocoaCreateNSScrollView()
803 {
804     if(!m_cocoaScroller)
805     {
806         m_cocoaScroller = new wxWindowCocoaScroller(this);
807     }
808 }
809
810 // New function that will replace some of the above.
811 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
812     int range, bool refresh)
813 {
814     CocoaCreateNSScrollView();
815     // TODO
816 }
817
818 // Does a physical scroll
819 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
820 {
821     // TODO
822 }
823
824 void wxWindow::DoSetVirtualSize( int x, int y )
825 {
826     wxWindowBase::DoSetVirtualSize(x,y);
827     CocoaCreateNSScrollView();
828     [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)];
829 }
830
831 bool wxWindow::SetFont(const wxFont& font)
832 {
833     // TODO
834     return TRUE;
835 }
836
837 static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
838 {
839     // first should be ordered higher
840     if(first==target)
841         return NSOrderedDescending;
842     // second should be ordered higher
843     if(second==target)
844         return NSOrderedAscending;
845     return NSOrderedSame;
846 }
847
848 // Raise the window to the top of the Z order
849 void wxWindow::Raise()
850 {
851 //    wxAutoNSAutoreleasePool pool;
852     NSView *nsview = GetNSViewForSuperview();
853     [[nsview superview] sortSubviewsUsingFunction:
854             CocoaRaiseWindowCompareFunction
855         context: nsview];
856 }
857
858 static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
859 {
860     // first should be ordered lower
861     if(first==target)
862         return NSOrderedAscending;
863     // second should be ordered lower
864     if(second==target)
865         return NSOrderedDescending;
866     return NSOrderedSame;
867 }
868
869 // Lower the window to the bottom of the Z order
870 void wxWindow::Lower()
871 {
872     NSView *nsview = GetNSViewForSuperview();
873     [[nsview superview] sortSubviewsUsingFunction:
874             CocoaLowerWindowCompareFunction
875         context: nsview];
876 }
877
878 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
879 {
880     return FALSE;
881 }
882
883 // Get the window with the focus
884 wxWindow *wxWindowBase::DoFindFocus()
885 {
886     // TODO
887     return NULL;
888 }
889
890 /* static */ wxWindow *wxWindowBase::GetCapture()
891 {
892     // TODO
893     return wxWindowCocoa::sm_capturedWindow;
894 }
895
896 wxWindow *wxGetActiveWindow()
897 {
898     // TODO
899     return NULL;
900 }
901
902 wxPoint wxGetMousePosition()
903 {
904     // TODO
905     return wxDefaultPosition;
906 }
907
908 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
909 {
910     pt = wxGetMousePosition();
911     return NULL;
912 }
913