SetInitialFrameRect: use frameRect.size.height to calculate frameRect.origin.y
[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 }
206
207 // Constructor
208 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
209            const wxPoint& pos,
210            const wxSize& size,
211            long style,
212            const wxString& name)
213 {
214     if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
215         return false;
216
217     // TODO: create the window
218     m_cocoaNSView = NULL;
219     SetNSView([[NSView alloc] initWithFrame: MakeDefaultNSRect(size)]);
220     [m_cocoaNSView release];
221
222     if (m_parent)
223     {
224         m_parent->AddChild(this);
225         m_parent->CocoaAddChild(this);
226     }
227     SetInitialFrameRect(pos,size);
228
229     return TRUE;
230 }
231
232 // Destructor
233 wxWindow::~wxWindow()
234 {
235     wxAutoNSAutoreleasePool pool;
236     DestroyChildren();
237
238     if(m_parent)
239         m_parent->RemoveChild(this);
240
241     CocoaRemoveFromParent();
242     delete m_cocoaHider;
243     delete m_cocoaScroller;
244     SetNSView(NULL);
245 }
246
247 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
248 {
249     NSView *childView = child->GetNSViewForSuperview();
250
251     wxASSERT(childView);
252     [m_cocoaNSView addSubview: childView];
253     child->m_isShown = !m_cocoaHider;
254 }
255
256 void wxWindowCocoa::CocoaRemoveFromParent(void)
257 {
258     [GetNSViewForSuperview() removeFromSuperview];
259 }
260
261 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
262 {
263     bool need_debug = cocoaNSView || m_cocoaNSView;
264     if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d",this,m_cocoaNSView,[m_cocoaNSView retainCount]);
265     DisassociateNSView(m_cocoaNSView);
266     [cocoaNSView retain];
267     [m_cocoaNSView release];
268     m_cocoaNSView = cocoaNSView;
269     AssociateNSView(m_cocoaNSView);
270     if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d",this,cocoaNSView,[cocoaNSView retainCount]);
271 }
272
273 WX_NSView wxWindowCocoa::GetNSViewForSuperview() const
274 {
275     return m_cocoaHider
276         ?   m_cocoaHider->GetNSView()
277         :   m_cocoaScroller
278             ?   m_cocoaScroller->GetNSScrollView()
279             :   m_cocoaNSView;
280 }
281
282 WX_NSView wxWindowCocoa::GetNSViewForHiding() const
283 {
284     return m_cocoaScroller
285         ?   m_cocoaScroller->GetNSScrollView()
286         :   m_cocoaNSView;
287 }
288
289 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
290 {
291     wxLogDebug("Cocoa_drawRect");
292     // Recursion can happen if the event loop runs from within the paint
293     // handler.  For instance, if an assertion dialog is shown.
294     // FIXME: This seems less than ideal.
295     if(m_isInPaint)
296     {
297         wxLogDebug("Paint event recursion!");
298         return false;
299     }
300     //FIXME: should probably turn that rect into the update region
301     m_isInPaint = TRUE;
302     wxPaintEvent event(m_windowId);
303     event.SetEventObject(this);
304     bool ret = GetEventHandler()->ProcessEvent(event);
305     m_isInPaint = FALSE;
306     return ret;
307 }
308
309 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
310 {
311     wxASSERT_MSG([m_cocoaNSView window]==[cocoaEvent window],"Mouse event for different NSWindow");
312     NSPoint cocoaPoint = [m_cocoaNSView convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
313     NSRect cocoaRect = [m_cocoaNSView frame];
314     const wxPoint &clientorigin = GetClientAreaOrigin();
315     event.m_x = (wxCoord)cocoaPoint.x - clientorigin.x;
316     event.m_y = (wxCoord)(cocoaRect.size.height - cocoaPoint.y) - clientorigin.y;
317
318     event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
319     event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
320     event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
321     event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
322
323     // TODO: set timestamp?
324     event.SetEventObject(this);
325     event.SetId(GetId());
326 }
327
328 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
329 {
330     wxMouseEvent event(wxEVT_MOTION);
331     InitMouseEvent(event,theEvent);
332     wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
333     return GetEventHandler()->ProcessEvent(event);
334 }
335
336 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
337 {
338     return false;
339 }
340
341 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
342 {
343     return false;
344 }
345
346 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
347 {
348     wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
349     InitMouseEvent(event,theEvent);
350     wxLogDebug("Mouse Down @%d,%d num clicks=%d",event.m_x,event.m_y,[theEvent clickCount]);
351     return GetEventHandler()->ProcessEvent(event);
352 }
353
354 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
355 {
356     wxMouseEvent event(wxEVT_MOTION);
357     InitMouseEvent(event,theEvent);
358     event.m_leftDown = true;
359     wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
360     return GetEventHandler()->ProcessEvent(event);
361 }
362
363 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
364 {
365     wxMouseEvent event(wxEVT_LEFT_UP);
366     InitMouseEvent(event,theEvent);
367     wxLogDebug("Mouse Up @%d,%d",event.m_x,event.m_y);
368     return GetEventHandler()->ProcessEvent(event);
369 }
370
371 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
372 {
373     return false;
374 }
375
376 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
377 {
378     return false;
379 }
380
381 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
382 {
383     return false;
384 }
385
386 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
387 {
388     return false;
389 }
390
391 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
392 {
393     return false;
394 }
395
396 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
397 {
398     return false;
399 }
400
401 void wxWindowCocoa::Cocoa_FrameChanged(void)
402 {
403     wxLogDebug("Cocoa_FrameChanged");
404     wxSizeEvent event(GetSize(), m_windowId);
405     event.SetEventObject(this);
406     GetEventHandler()->ProcessEvent(event);
407 }
408
409 bool wxWindow::Close(bool force)
410 {
411     return false;
412 }
413
414 void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
415 {
416     [[oldView superview] replaceSubview:oldView with:newView];
417 }
418
419 bool wxWindow::Show(bool show)
420 {
421     wxAutoNSAutoreleasePool pool;
422     // If the window is marked as visible, then it shouldn't have a dummy view
423     // If the window is marked hidden, then it should have a dummy view
424     // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
425 //    wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),"wxWindow: m_isShown does not agree with m_dummyNSView");
426     // Return false if there isn't a window to show or hide
427     NSView *cocoaView = GetNSViewForHiding();
428     if(!cocoaView)
429         return false;
430     if(show)
431     {
432         // If state isn't changing, return false
433         if(!m_cocoaHider)
434             return false;
435         CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView);
436         wxASSERT(![m_cocoaHider->GetNSView() superview]);
437         delete m_cocoaHider;
438         m_cocoaHider = NULL;
439         wxASSERT([cocoaView superview]);
440     }
441     else
442     {
443         // If state isn't changing, return false
444         if(m_cocoaHider)
445             return false;
446         m_cocoaHider = new wxWindowCocoaHider(this);
447         // NOTE: replaceSubview:with will cause m_cocaNSView to be
448         // (auto)released which balances out addSubview
449         CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView());
450         // m_coocaNSView is now only retained by us
451         wxASSERT([m_cocoaHider->GetNSView() superview]);
452         wxASSERT(![cocoaView superview]);
453     }
454     m_isShown = show;
455     return true;
456 }
457
458 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
459 {
460 //    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":".");
461     int currentX, currentY;
462     int currentW, currentH;
463     DoGetPosition(&currentX, &currentY);
464     DoGetSize(&currentW, &currentH);
465     if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
466         x=currentX;
467     if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
468         y=currentY;
469
470     AdjustForParentClientOrigin(x,y,sizeFlags);
471
472     wxSize size(-1,-1);
473
474     if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
475     {
476         if(sizeFlags&wxSIZE_AUTO_WIDTH)
477         {
478             size=DoGetBestSize();
479             width=size.x;
480         }
481         else
482             width=currentW;
483     }
484     if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
485     {
486         if(sizeFlags&wxSIZE_AUTO_HEIGHT)
487         {
488             if(size.x==-1)
489                 size=DoGetBestSize();
490             height=size.y;
491         }
492         else
493             height=currentH;
494     }
495     DoMoveWindow(x,y,width,height);
496 }
497
498 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
499 {
500 //    wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
501
502     NSView *nsview = GetNSViewForSuperview();
503     NSView *superview = [nsview superview];
504     wxCHECK_RET(superview,"NSView does not have a superview");
505     NSRect parentRect = [superview frame];
506
507     NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
508     [nsview setFrame: cocoaRect];
509     // Be sure to redraw the parent to reflect the changed position
510     [superview setNeedsDisplay:YES];
511 }
512
513 void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size)
514 {
515     NSView *nsview = GetNSViewForSuperview();
516     NSView *superview = [nsview superview];
517     wxCHECK_RET(superview,"NSView does not have a superview");
518     NSRect parentRect = [superview frame];
519     NSRect frameRect = [nsview frame];
520     if(size.x!=-1)
521         frameRect.size.width = size.x;
522     if(size.y!=-1)
523         frameRect.size.height = size.y;
524     frameRect.origin.x = pos.x;
525     frameRect.origin.y = parentRect.size.height-(pos.y+frameRect.size.height);
526     [nsview setFrame: frameRect];
527 }
528
529 // Get total size
530 void wxWindow::DoGetSize(int *w, int *h) const
531 {
532     NSRect cocoaRect = [GetNSViewForSuperview() frame];
533     if(w)
534         *w=(int)cocoaRect.size.width;
535     if(h)
536         *h=(int)cocoaRect.size.height;
537 //    wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
538 }
539
540 void wxWindow::DoGetPosition(int *x, int *y) const
541 {
542     NSView *nsview = GetNSViewForSuperview();
543     NSView *superview = [nsview superview];
544     wxCHECK_RET(superview,"NSView does not have a superview");
545     NSRect parentRect = [superview frame];
546
547     NSRect cocoaRect = [nsview frame];
548     if(x)
549         *x=(int)cocoaRect.origin.x;
550     if(y)
551         *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
552 //    wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
553 }
554
555 WXWidget wxWindow::GetHandle() const
556 {
557     return m_cocoaNSView;
558 }
559
560 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
561 {
562     [m_cocoaNSView setNeedsDisplay:YES];
563 }
564
565 void wxWindow::SetFocus()
566 {
567     // TODO
568 }
569
570 void wxWindow::DoCaptureMouse()
571 {
572     // TODO
573     sm_capturedWindow = this;
574 }
575
576 void wxWindow::DoReleaseMouse()
577 {
578     // TODO
579     sm_capturedWindow = NULL;
580 }
581
582 void wxWindow::DoScreenToClient(int *x, int *y) const
583 {
584     // TODO
585 }
586
587 void wxWindow::DoClientToScreen(int *x, int *y) const
588 {
589     // TODO
590 }
591
592 // Get size *available for subwindows* i.e. excluding menu bar etc.
593 void wxWindow::DoGetClientSize(int *x, int *y) const
594 {
595     wxLogDebug("DoGetClientSize:");
596     if(m_cocoaScroller)
597         m_cocoaScroller->DoGetClientSize(x,y);
598     else
599         wxWindowCocoa::DoGetSize(x,y);
600 }
601
602 void wxWindow::DoSetClientSize(int width, int height)
603 {
604     wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
605     if(m_cocoaScroller)
606         m_cocoaScroller->ClientSizeToSize(width,height);
607     CocoaSetWxWindowSize(width,height);
608 }
609
610 void wxWindow::CocoaSetWxWindowSize(int width, int height)
611 {
612     wxWindowCocoa::DoSetSize(-1,-1,width,height,wxSIZE_USE_EXISTING);
613 }
614
615 int wxWindow::GetCharHeight() const
616 {
617     // TODO
618     return 0;
619 }
620
621 int wxWindow::GetCharWidth() const
622 {
623     // TODO
624     return 0;
625 }
626
627 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
628         int *descent, int *externalLeading, const wxFont *theFont) const
629 {
630     // TODO
631 }
632
633 // Coordinates relative to the window
634 void wxWindow::WarpPointer (int x_pos, int y_pos)
635 {
636     // TODO
637 }
638
639 int wxWindow::GetScrollPos(int orient) const
640 {
641     // TODO
642     return 0;
643 }
644
645 // This now returns the whole range, not just the number
646 // of positions that we can scroll.
647 int wxWindow::GetScrollRange(int orient) const
648 {
649     // TODO
650     return 0;
651 }
652
653 int wxWindow::GetScrollThumb(int orient) const
654 {
655     // TODO
656     return 0;
657 }
658
659 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
660 {
661     // TODO
662 }
663
664 void wxWindow::CocoaCreateNSScrollView()
665 {
666     if(!m_cocoaScroller)
667     {
668         m_cocoaScroller = new wxWindowCocoaScroller(this);
669     }
670 }
671
672 // New function that will replace some of the above.
673 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
674     int range, bool refresh)
675 {
676     CocoaCreateNSScrollView();
677     // TODO
678 }
679
680 // Does a physical scroll
681 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
682 {
683     // TODO
684 }
685
686 void wxWindow::DoSetVirtualSize( int x, int y )
687 {
688     wxWindowBase::DoSetVirtualSize(x,y);
689     CocoaCreateNSScrollView();
690     [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)];
691 }
692
693 bool wxWindow::SetFont(const wxFont& font)
694 {
695     // TODO
696     return TRUE;
697 }
698
699 static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
700 {
701     // first should be ordered higher
702     if(first==target)
703         return NSOrderedDescending;
704     // second should be ordered higher
705     if(second==target)
706         return NSOrderedAscending;
707     return NSOrderedSame;
708 }
709
710 // Raise the window to the top of the Z order
711 void wxWindow::Raise()
712 {
713 //    wxAutoNSAutoreleasePool pool;
714     NSView *nsview = GetNSViewForSuperview();
715     [[nsview superview] sortSubviewsUsingFunction:
716             CocoaRaiseWindowCompareFunction
717         context: nsview];
718 }
719
720 static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
721 {
722     // first should be ordered lower
723     if(first==target)
724         return NSOrderedAscending;
725     // second should be ordered lower
726     if(second==target)
727         return NSOrderedDescending;
728     return NSOrderedSame;
729 }
730
731 // Lower the window to the bottom of the Z order
732 void wxWindow::Lower()
733 {
734     NSView *nsview = GetNSViewForSuperview();
735     [[nsview superview] sortSubviewsUsingFunction:
736             CocoaLowerWindowCompareFunction
737         context: nsview];
738 }
739
740 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
741 {
742     return FALSE;
743 }
744
745 // Get the window with the focus
746 wxWindow *wxWindowBase::FindFocus()
747 {
748     // TODO
749     return NULL;
750 }
751
752 /* static */ wxWindow *wxWindowBase::GetCapture()
753 {
754     // TODO
755     return wxWindowCocoa::sm_capturedWindow;
756 }
757
758 wxWindow *wxGetActiveWindow()
759 {
760     // TODO
761     return NULL;
762 }
763
764 wxPoint wxGetMousePosition()
765 {
766     // TODO
767     return wxDefaultPosition;
768 }
769
770 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
771 {
772     pt = wxGetMousePosition();
773     return NULL;
774 }
775