More name 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/window.h"
16 #endif //WX_PRECOMP
17
18 #include "wx/cocoa/autorelease.h"
19
20 #import <AppKit/NSView.h>
21 #import <AppKit/NSEvent.h>
22 #import <AppKit/NSScrollView.h>
23 #import <AppKit/NSColor.h>
24 #import <AppKit/NSClipView.h>
25 #import <Foundation/NSException.h>
26
27 #include <objc/objc-runtime.h>
28
29 // Turn this on to paint green over the dummy views for debugging
30 #undef WXCOCOA_FILL_DUMMY_VIEW
31
32 #ifdef WXCOCOA_FILL_DUMMY_VIEW
33 #import <AppKit/NSBezierPath.h>
34 #endif //def WXCOCOA_FILL_DUMMY_VIEW
35
36 // ========================================================================
37 // wxWindowCocoaHider
38 // ========================================================================
39 class wxWindowCocoaHider: protected wxCocoaNSView
40 {
41     DECLARE_NO_COPY_CLASS(wxWindowCocoaHider)
42 public:
43     wxWindowCocoaHider(wxWindow *owner);
44     virtual ~wxWindowCocoaHider();
45     inline WX_NSView GetNSView() { return m_dummyNSView; }
46 protected:
47     wxWindowCocoa *m_owner;
48     WX_NSView m_dummyNSView;
49     virtual void Cocoa_FrameChanged(void);
50 #ifdef WXCOCOA_FILL_DUMMY_VIEW
51     virtual bool Cocoa_drawRect(const NSRect& rect);
52 #endif //def WXCOCOA_FILL_DUMMY_VIEW
53 private:
54     wxWindowCocoaHider();
55 };
56
57 // ========================================================================
58 // wxWindowCocoaScroller
59 // ========================================================================
60 class wxWindowCocoaScroller: protected wxCocoaNSView
61 {
62     DECLARE_NO_COPY_CLASS(wxWindowCocoaScroller)
63 public:
64     wxWindowCocoaScroller(wxWindow *owner);
65     virtual ~wxWindowCocoaScroller();
66     inline WX_NSScrollView GetNSScrollView() { return m_cocoaNSScrollView; }
67     void ClientSizeToSize(int &width, int &height);
68     void DoGetClientSize(int *x, int *y) const;
69     void Encapsulate();
70     void Unencapsulate();
71 protected:
72     wxWindowCocoa *m_owner;
73     WX_NSScrollView m_cocoaNSScrollView;
74     virtual void Cocoa_FrameChanged(void);
75 private:
76     wxWindowCocoaScroller();
77 };
78
79 // ========================================================================
80 // wxDummyNSView
81 // ========================================================================
82 @interface wxDummyNSView : NSView
83 - (NSView *)hitTest:(NSPoint)aPoint;
84 @end
85
86 @implementation wxDummyNSView : NSView
87 - (NSView *)hitTest:(NSPoint)aPoint
88 {
89     return nil;
90 }
91
92 @end
93
94 // ========================================================================
95 // wxWindowCocoaHider
96 // ========================================================================
97 wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner)
98 :   m_owner(owner)
99 {
100     wxASSERT(owner);
101     wxASSERT(owner->GetNSViewForHiding());
102     m_dummyNSView = [[wxDummyNSView alloc]
103         initWithFrame:[owner->GetNSViewForHiding() frame]];
104     [m_dummyNSView setAutoresizingMask: [owner->GetNSViewForHiding() autoresizingMask]];
105     AssociateNSView(m_dummyNSView);
106 }
107
108 wxWindowCocoaHider::~wxWindowCocoaHider()
109 {
110     DisassociateNSView(m_dummyNSView);
111     [m_dummyNSView release];
112 }
113
114 void wxWindowCocoaHider::Cocoa_FrameChanged(void)
115 {
116     // Keep the real window in synch with the dummy
117     wxASSERT(m_dummyNSView);
118     [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]];
119 }
120
121 #ifdef WXCOCOA_FILL_DUMMY_VIEW
122 bool wxWindowCocoaHider::Cocoa_drawRect(const NSRect& rect)
123 {
124     NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:rect];
125     [[NSColor greenColor] set];
126     [bezpath stroke];
127     [bezpath fill];
128     return true;
129 }
130 #endif //def WXCOCOA_FILL_DUMMY_VIEW
131
132 // ========================================================================
133 // wxFlippedNSClipView
134 // ========================================================================
135 @interface wxFlippedNSClipView : NSClipView
136 - (BOOL)isFlipped;
137 @end
138
139 @implementation wxFlippedNSClipView : NSClipView
140 - (BOOL)isFlipped
141 {
142     return YES;
143 }
144
145 @end
146
147 // ========================================================================
148 // wxWindowCocoaScroller
149 // ========================================================================
150 wxWindowCocoaScroller::wxWindowCocoaScroller(wxWindow *owner)
151 :   m_owner(owner)
152 {
153     wxAutoNSAutoreleasePool pool;
154     wxASSERT(owner);
155     wxASSERT(owner->GetNSView());
156     m_cocoaNSScrollView = [[NSScrollView alloc]
157         initWithFrame:[owner->GetNSView() frame]];
158     AssociateNSView(m_cocoaNSScrollView);
159
160     /* Replace the default NSClipView with a flipped one.  This ensures
161        scrolling is "pinned" to the top-left instead of bottom-right. */
162     NSClipView *flippedClip = [[wxFlippedNSClipView alloc]
163         initWithFrame: [[m_cocoaNSScrollView contentView] frame]];
164     [m_cocoaNSScrollView setContentView:flippedClip];
165     [flippedClip release];
166
167     [m_cocoaNSScrollView setBackgroundColor: [NSColor windowBackgroundColor]];
168     [m_cocoaNSScrollView setHasHorizontalScroller: YES];
169     [m_cocoaNSScrollView setHasVerticalScroller: YES];
170     Encapsulate();
171 }
172
173 void wxWindowCocoaScroller::Encapsulate()
174 {
175     // Set the scroll view autoresizingMask to match the current NSView
176     [m_cocoaNSScrollView setAutoresizingMask: [m_owner->GetNSView() autoresizingMask]];
177     [m_owner->GetNSView() setAutoresizingMask: NSViewNotSizable];
178     // NOTE: replaceSubView will cause m_cocaNSView to be released
179     // except when it hasn't been added into an NSView hierarchy in which
180     // case it doesn't need to be and this should work out to a no-op
181     m_owner->CocoaReplaceView(m_owner->GetNSView(), m_cocoaNSScrollView);
182     // The NSView is still retained by owner
183     [m_cocoaNSScrollView setDocumentView: m_owner->GetNSView()];
184     // Now it's also retained by the NSScrollView
185 }
186
187 void wxWindowCocoaScroller::Unencapsulate()
188 {
189     [m_cocoaNSScrollView setDocumentView: nil];
190     m_owner->CocoaReplaceView(m_cocoaNSScrollView, m_owner->GetNSView());
191     if(![[m_owner->GetNSView() superview] isFlipped])
192         [m_owner->GetNSView() setAutoresizingMask: NSViewMinYMargin];
193 }
194
195 wxWindowCocoaScroller::~wxWindowCocoaScroller()
196 {
197     DisassociateNSView(m_cocoaNSScrollView);
198     [m_cocoaNSScrollView release];
199 }
200
201 void wxWindowCocoaScroller::ClientSizeToSize(int &width, int &height)
202 {
203     NSSize frameSize = [NSScrollView
204         frameSizeForContentSize: NSMakeSize(width,height)
205         hasHorizontalScroller: [m_cocoaNSScrollView hasHorizontalScroller]
206         hasVerticalScroller: [m_cocoaNSScrollView hasVerticalScroller]
207         borderType: [m_cocoaNSScrollView borderType]];
208     width = (int)frameSize.width;
209     height = (int)frameSize.height;
210 }
211
212 void wxWindowCocoaScroller::DoGetClientSize(int *x, int *y) const
213 {
214     NSSize nssize = [m_cocoaNSScrollView contentSize];
215     if(x)
216         *x = (int)nssize.width;
217     if(y)
218         *y = (int)nssize.height;
219 }
220
221 void wxWindowCocoaScroller::Cocoa_FrameChanged(void)
222 {
223     wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
224     wxSizeEvent event(m_owner->GetSize(), m_owner->GetId());
225     event.SetEventObject(m_owner);
226     m_owner->GetEventHandler()->ProcessEvent(event);
227 }
228
229 // ========================================================================
230 // wxWindowCocoa
231 // ========================================================================
232 // normally the base classes aren't included, but wxWindow is special
233 #ifdef __WXUNIVERSAL__
234 IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
235 #else
236 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
237 #endif
238
239 BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
240 END_EVENT_TABLE()
241
242 wxWindow *wxWindowCocoa::sm_capturedWindow = NULL;
243
244 // Constructor
245 void wxWindowCocoa::Init()
246 {
247     m_cocoaNSView = NULL;
248     m_cocoaHider = NULL;
249     m_cocoaScroller = NULL;
250     m_isBeingDeleted = FALSE;
251     m_isInPaint = FALSE;
252     m_shouldBeEnabled = true;
253 }
254
255 // Constructor
256 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
257            const wxPoint& pos,
258            const wxSize& size,
259            long style,
260            const wxString& name)
261 {
262     if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
263         return false;
264
265     // TODO: create the window
266     m_cocoaNSView = NULL;
267     SetNSView([[NSView alloc] initWithFrame: MakeDefaultNSRect(size)]);
268     [m_cocoaNSView release];
269
270     if (m_parent)
271     {
272         m_parent->AddChild(this);
273         m_parent->CocoaAddChild(this);
274         SetInitialFrameRect(pos,size);
275     }
276
277     return TRUE;
278 }
279
280 // Destructor
281 wxWindow::~wxWindow()
282 {
283     wxAutoNSAutoreleasePool pool;
284     DestroyChildren();
285
286     // Make sure our parent (in the wxWidgets sense) is our superview
287     // before we go removing from it.
288     if(m_parent && m_parent->GetNSView()==[GetNSViewForSuperview() superview])
289         CocoaRemoveFromParent();
290     delete m_cocoaHider;
291     delete m_cocoaScroller;
292     SetNSView(NULL);
293 }
294
295 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
296 {
297     NSView *childView = child->GetNSViewForSuperview();
298
299     wxASSERT(childView);
300     [m_cocoaNSView addSubview: childView];
301     child->m_isShown = !m_cocoaHider;
302 }
303
304 void wxWindowCocoa::CocoaRemoveFromParent(void)
305 {
306     [GetNSViewForSuperview() removeFromSuperview];
307 }
308
309 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
310 {
311     // Assume setting the NSView to NULL means this wxWindow is being destroyed
312     if(m_cocoaNSView && !cocoaNSView)
313         SendDestroyEvent();
314
315     bool need_debug = cocoaNSView || m_cocoaNSView;
316     if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d"),this,m_cocoaNSView,[m_cocoaNSView retainCount]);
317     DisassociateNSView(m_cocoaNSView);
318     [cocoaNSView retain];
319     [m_cocoaNSView release];
320     m_cocoaNSView = cocoaNSView;
321     AssociateNSView(m_cocoaNSView);
322     if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d"),this,cocoaNSView,[cocoaNSView retainCount]);
323 }
324
325 WX_NSView wxWindowCocoa::GetNSViewForSuperview() const
326 {
327     return m_cocoaHider
328         ?   m_cocoaHider->GetNSView()
329         :   m_cocoaScroller
330             ?   m_cocoaScroller->GetNSScrollView()
331             :   m_cocoaNSView;
332 }
333
334 WX_NSView wxWindowCocoa::GetNSViewForHiding() const
335 {
336     return m_cocoaScroller
337         ?   m_cocoaScroller->GetNSScrollView()
338         :   m_cocoaNSView;
339 }
340
341 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
342 {
343     wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_drawRect"));
344     // Recursion can happen if the event loop runs from within the paint
345     // handler.  For instance, if an assertion dialog is shown.
346     // FIXME: This seems less than ideal.
347     if(m_isInPaint)
348     {
349         wxLogDebug(wxT("Paint event recursion!"));
350         return false;
351     }
352     m_isInPaint = TRUE;
353
354     // Set m_updateRegion
355     const NSRect *rects = &rect; // The bounding box of the region
356     int countRects = 1;
357     // Try replacing the larger rectangle with a list of smaller ones:
358 NS_DURING
359     // This only works on Panther
360 //    [GetNSView() getRectsBeingDrawn:&rects count:&countRects];
361     // This compiles everywhere (and still only works on Panther)
362     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 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
604 {
605     wxAutoNSAutoreleasePool pool;
606     wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
607
608     NSView *nsview = GetNSViewForSuperview();
609     NSView *superview = [nsview superview];
610     wxCHECK_RET(superview,wxT("NSView does not have a superview"));
611     NSRect parentRect = [superview bounds];
612
613     NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
614     [nsview setFrame: cocoaRect];
615     // Be sure to redraw the parent to reflect the changed position
616     [superview setNeedsDisplay:YES];
617 }
618
619 void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size)
620 {
621     NSView *nsview = GetNSViewForSuperview();
622     NSView *superview = [nsview superview];
623     wxCHECK_RET(superview,wxT("NSView does not have a superview"));
624     NSRect parentRect = [superview bounds];
625     NSRect frameRect = [nsview frame];
626     if(size.x!=-1)
627         frameRect.size.width = size.x;
628     if(size.y!=-1)
629         frameRect.size.height = size.y;
630     frameRect.origin.x = pos.x;
631     frameRect.origin.y = parentRect.size.height-(pos.y+frameRect.size.height);
632     // Tell Cocoa to change the margin between the bottom of the superview
633     // and the bottom of the control.  Keeps the control pinned to the top
634     // of its superview so that its position in the wxWidgets coordinate
635     // system doesn't change.
636     if(![superview isFlipped])
637         [nsview setAutoresizingMask: NSViewMinYMargin];
638     // MUST set the mask before setFrame: which can generate a size event
639     // and cause a scroller to be added!
640     [nsview setFrame: frameRect];
641 }
642
643 // Get total size
644 void wxWindow::DoGetSize(int *w, int *h) const
645 {
646     NSRect cocoaRect = [GetNSViewForSuperview() frame];
647     if(w)
648         *w=(int)cocoaRect.size.width;
649     if(h)
650         *h=(int)cocoaRect.size.height;
651     wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
652 }
653
654 void wxWindow::DoGetPosition(int *x, int *y) const
655 {
656     NSView *nsview = GetNSViewForSuperview();
657     NSView *superview = [nsview superview];
658     wxCHECK_RET(superview,wxT("NSView does not have a superview"));
659     NSRect parentRect = [superview bounds];
660
661     NSRect cocoaRect = [nsview frame];
662     if(x)
663         *x=(int)cocoaRect.origin.x;
664     if(y)
665         *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
666     wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
667 }
668
669 WXWidget wxWindow::GetHandle() const
670 {
671     return m_cocoaNSView;
672 }
673
674 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
675 {
676     [m_cocoaNSView setNeedsDisplay:YES];
677 }
678
679 void wxWindow::SetFocus()
680 {
681     // TODO
682 }
683
684 void wxWindow::DoCaptureMouse()
685 {
686     // TODO
687     sm_capturedWindow = this;
688 }
689
690 void wxWindow::DoReleaseMouse()
691 {
692     // TODO
693     sm_capturedWindow = NULL;
694 }
695
696 void wxWindow::DoScreenToClient(int *x, int *y) const
697 {
698     // TODO
699 }
700
701 void wxWindow::DoClientToScreen(int *x, int *y) const
702 {
703     // TODO
704 }
705
706 // Get size *available for subwindows* i.e. excluding menu bar etc.
707 void wxWindow::DoGetClientSize(int *x, int *y) const
708 {
709     wxLogTrace(wxTRACE_COCOA,wxT("DoGetClientSize:"));
710     if(m_cocoaScroller)
711         m_cocoaScroller->DoGetClientSize(x,y);
712     else
713         wxWindowCocoa::DoGetSize(x,y);
714 }
715
716 void wxWindow::DoSetClientSize(int width, int height)
717 {
718     wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("DoSetClientSize=(%d,%d)"),width,height);
719     if(m_cocoaScroller)
720         m_cocoaScroller->ClientSizeToSize(width,height);
721     CocoaSetWxWindowSize(width,height);
722 }
723
724 void wxWindow::CocoaSetWxWindowSize(int width, int height)
725 {
726     wxWindowCocoa::DoSetSize(-1,-1,width,height,wxSIZE_USE_EXISTING);
727 }
728
729 int wxWindow::GetCharHeight() const
730 {
731     // TODO
732     return 0;
733 }
734
735 int wxWindow::GetCharWidth() const
736 {
737     // TODO
738     return 0;
739 }
740
741 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
742         int *descent, int *externalLeading, const wxFont *theFont) const
743 {
744     // TODO
745 }
746
747 // Coordinates relative to the window
748 void wxWindow::WarpPointer (int x_pos, int y_pos)
749 {
750     // TODO
751 }
752
753 int wxWindow::GetScrollPos(int orient) const
754 {
755     // TODO
756     return 0;
757 }
758
759 // This now returns the whole range, not just the number
760 // of positions that we can scroll.
761 int wxWindow::GetScrollRange(int orient) const
762 {
763     // TODO
764     return 0;
765 }
766
767 int wxWindow::GetScrollThumb(int orient) const
768 {
769     // TODO
770     return 0;
771 }
772
773 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
774 {
775     // TODO
776 }
777
778 void wxWindow::CocoaCreateNSScrollView()
779 {
780     if(!m_cocoaScroller)
781     {
782         m_cocoaScroller = new wxWindowCocoaScroller(this);
783     }
784 }
785
786 // New function that will replace some of the above.
787 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
788     int range, bool refresh)
789 {
790     CocoaCreateNSScrollView();
791     // TODO
792 }
793
794 // Does a physical scroll
795 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
796 {
797     // TODO
798 }
799
800 void wxWindow::DoSetVirtualSize( int x, int y )
801 {
802     wxWindowBase::DoSetVirtualSize(x,y);
803     CocoaCreateNSScrollView();
804     [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)];
805 }
806
807 bool wxWindow::SetFont(const wxFont& font)
808 {
809     // TODO
810     return TRUE;
811 }
812
813 static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
814 {
815     // first should be ordered higher
816     if(first==target)
817         return NSOrderedDescending;
818     // second should be ordered higher
819     if(second==target)
820         return NSOrderedAscending;
821     return NSOrderedSame;
822 }
823
824 // Raise the window to the top of the Z order
825 void wxWindow::Raise()
826 {
827 //    wxAutoNSAutoreleasePool pool;
828     NSView *nsview = GetNSViewForSuperview();
829     [[nsview superview] sortSubviewsUsingFunction:
830             CocoaRaiseWindowCompareFunction
831         context: nsview];
832 }
833
834 static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
835 {
836     // first should be ordered lower
837     if(first==target)
838         return NSOrderedAscending;
839     // second should be ordered lower
840     if(second==target)
841         return NSOrderedDescending;
842     return NSOrderedSame;
843 }
844
845 // Lower the window to the bottom of the Z order
846 void wxWindow::Lower()
847 {
848     NSView *nsview = GetNSViewForSuperview();
849     [[nsview superview] sortSubviewsUsingFunction:
850             CocoaLowerWindowCompareFunction
851         context: nsview];
852 }
853
854 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
855 {
856     return FALSE;
857 }
858
859 // Get the window with the focus
860 wxWindow *wxWindowBase::FindFocus()
861 {
862     // TODO
863     return NULL;
864 }
865
866 /* static */ wxWindow *wxWindowBase::GetCapture()
867 {
868     // TODO
869     return wxWindowCocoa::sm_capturedWindow;
870 }
871
872 wxWindow *wxGetActiveWindow()
873 {
874     // TODO
875     return NULL;
876 }
877
878 wxPoint wxGetMousePosition()
879 {
880     // TODO
881     return wxDefaultPosition;
882 }
883
884 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
885 {
886     pt = wxGetMousePosition();
887     return NULL;
888 }
889