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