1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/window.mm
3 // Purpose: wxWindowCocoa
4 // Author: David Elliott
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
15 #include "wx/window.h"
18 #include "wx/tooltip.h"
20 #include "wx/cocoa/autorelease.h"
21 #include "wx/cocoa/string.h"
23 #import <AppKit/NSView.h>
24 #import <AppKit/NSEvent.h>
25 #import <AppKit/NSScrollView.h>
26 #import <AppKit/NSColor.h>
27 #import <AppKit/NSClipView.h>
28 #import <Foundation/NSException.h>
29 #import <AppKit/NSApplication.h>
30 #import <AppKit/NSWindow.h>
32 // Turn this on to paint green over the dummy views for debugging
33 #undef WXCOCOA_FILL_DUMMY_VIEW
35 #ifdef WXCOCOA_FILL_DUMMY_VIEW
36 #import <AppKit/NSBezierPath.h>
37 #endif //def WXCOCOA_FILL_DUMMY_VIEW
39 // A category for methods that are only present in Panther's SDK
40 @interface NSView(wxNSViewPrePantherCompatibility)
41 - (void)getRectsBeingDrawn:(const NSRect **)rects count:(int *)count;
44 // ========================================================================
46 // ========================================================================
47 class wxWindowCocoaHider: protected wxCocoaNSView
49 DECLARE_NO_COPY_CLASS(wxWindowCocoaHider)
51 wxWindowCocoaHider(wxWindow *owner);
52 virtual ~wxWindowCocoaHider();
53 inline WX_NSView GetNSView() { return m_dummyNSView; }
55 wxWindowCocoa *m_owner;
56 WX_NSView m_dummyNSView;
57 virtual void Cocoa_FrameChanged(void);
58 #ifdef WXCOCOA_FILL_DUMMY_VIEW
59 virtual bool Cocoa_drawRect(const NSRect& rect);
60 #endif //def WXCOCOA_FILL_DUMMY_VIEW
65 // ========================================================================
66 // wxWindowCocoaScrollView
67 // ========================================================================
68 class wxWindowCocoaScrollView: protected wxCocoaNSView
70 DECLARE_NO_COPY_CLASS(wxWindowCocoaScrollView)
72 wxWindowCocoaScrollView(wxWindow *owner);
73 virtual ~wxWindowCocoaScrollView();
74 inline WX_NSScrollView GetNSScrollView() { return m_cocoaNSScrollView; }
75 void ClientSizeToSize(int &width, int &height);
76 void DoGetClientSize(int *x, int *y) const;
80 wxWindowCocoa *m_owner;
81 WX_NSScrollView m_cocoaNSScrollView;
82 virtual void Cocoa_FrameChanged(void);
84 wxWindowCocoaScrollView();
87 // ========================================================================
89 // ========================================================================
90 @interface wxDummyNSView : NSView
91 - (NSView *)hitTest:(NSPoint)aPoint;
94 @implementation wxDummyNSView : NSView
95 - (NSView *)hitTest:(NSPoint)aPoint
102 // ========================================================================
103 // wxWindowCocoaHider
104 // ========================================================================
105 wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner)
109 wxASSERT(owner->GetNSViewForHiding());
110 m_dummyNSView = [[wxDummyNSView alloc]
111 initWithFrame:[owner->GetNSViewForHiding() frame]];
112 [m_dummyNSView setAutoresizingMask: [owner->GetNSViewForHiding() autoresizingMask]];
113 AssociateNSView(m_dummyNSView);
116 wxWindowCocoaHider::~wxWindowCocoaHider()
118 DisassociateNSView(m_dummyNSView);
119 [m_dummyNSView release];
122 void wxWindowCocoaHider::Cocoa_FrameChanged(void)
124 // Keep the real window in synch with the dummy
125 wxASSERT(m_dummyNSView);
126 [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]];
130 #ifdef WXCOCOA_FILL_DUMMY_VIEW
131 bool wxWindowCocoaHider::Cocoa_drawRect(const NSRect& rect)
133 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:rect];
134 [[NSColor greenColor] set];
139 #endif //def WXCOCOA_FILL_DUMMY_VIEW
141 // ========================================================================
142 // wxFlippedNSClipView
143 // ========================================================================
144 @interface wxFlippedNSClipView : NSClipView
148 @implementation wxFlippedNSClipView : NSClipView
156 // ========================================================================
157 // wxWindowCocoaScrollView
158 // ========================================================================
159 wxWindowCocoaScrollView::wxWindowCocoaScrollView(wxWindow *owner)
162 wxAutoNSAutoreleasePool pool;
164 wxASSERT(owner->GetNSView());
165 m_cocoaNSScrollView = [[NSScrollView alloc]
166 initWithFrame:[owner->GetNSView() frame]];
167 AssociateNSView(m_cocoaNSScrollView);
169 /* Replace the default NSClipView with a flipped one. This ensures
170 scrolling is "pinned" to the top-left instead of bottom-right. */
171 NSClipView *flippedClip = [[wxFlippedNSClipView alloc]
172 initWithFrame: [[m_cocoaNSScrollView contentView] frame]];
173 [m_cocoaNSScrollView setContentView:flippedClip];
174 [flippedClip release];
176 [m_cocoaNSScrollView setBackgroundColor: [NSColor windowBackgroundColor]];
177 [m_cocoaNSScrollView setHasHorizontalScroller: YES];
178 [m_cocoaNSScrollView setHasVerticalScroller: YES];
182 void wxWindowCocoaScrollView::Encapsulate()
184 // Set the scroll view autoresizingMask to match the current NSView
185 [m_cocoaNSScrollView setAutoresizingMask: [m_owner->GetNSView() autoresizingMask]];
186 [m_owner->GetNSView() setAutoresizingMask: NSViewNotSizable];
187 // NOTE: replaceSubView will cause m_cocaNSView to be released
188 // except when it hasn't been added into an NSView hierarchy in which
189 // case it doesn't need to be and this should work out to a no-op
190 m_owner->CocoaReplaceView(m_owner->GetNSView(), m_cocoaNSScrollView);
191 // The NSView is still retained by owner
192 [m_cocoaNSScrollView setDocumentView: m_owner->GetNSView()];
193 // Now it's also retained by the NSScrollView
196 void wxWindowCocoaScrollView::Unencapsulate()
198 [m_cocoaNSScrollView setDocumentView: nil];
199 m_owner->CocoaReplaceView(m_cocoaNSScrollView, m_owner->GetNSView());
200 if(![[m_owner->GetNSView() superview] isFlipped])
201 [m_owner->GetNSView() setAutoresizingMask: NSViewMinYMargin];
204 wxWindowCocoaScrollView::~wxWindowCocoaScrollView()
206 DisassociateNSView(m_cocoaNSScrollView);
207 [m_cocoaNSScrollView release];
210 void wxWindowCocoaScrollView::ClientSizeToSize(int &width, int &height)
212 NSSize frameSize = [NSScrollView
213 frameSizeForContentSize: NSMakeSize(width,height)
214 hasHorizontalScroller: [m_cocoaNSScrollView hasHorizontalScroller]
215 hasVerticalScroller: [m_cocoaNSScrollView hasVerticalScroller]
216 borderType: [m_cocoaNSScrollView borderType]];
217 width = (int)frameSize.width;
218 height = (int)frameSize.height;
221 void wxWindowCocoaScrollView::DoGetClientSize(int *x, int *y) const
223 NSSize nssize = [m_cocoaNSScrollView contentSize];
225 *x = (int)nssize.width;
227 *y = (int)nssize.height;
230 void wxWindowCocoaScrollView::Cocoa_FrameChanged(void)
232 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
233 wxSizeEvent event(m_owner->GetSize(), m_owner->GetId());
234 event.SetEventObject(m_owner);
235 m_owner->GetEventHandler()->ProcessEvent(event);
238 // ========================================================================
240 // ========================================================================
241 // normally the base classes aren't included, but wxWindow is special
242 #ifdef __WXUNIVERSAL__
243 IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
245 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
248 BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
251 wxWindow *wxWindowCocoa::sm_capturedWindow = NULL;
254 void wxWindowCocoa::Init()
256 m_cocoaNSView = NULL;
258 m_wxCocoaScrollView = NULL;
259 m_isBeingDeleted = FALSE;
261 m_shouldBeEnabled = true;
265 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
269 const wxString& name)
271 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
274 // TODO: create the window
275 m_cocoaNSView = NULL;
276 SetNSView([[NSView alloc] initWithFrame: MakeDefaultNSRect(size)]);
277 [m_cocoaNSView release];
281 m_parent->AddChild(this);
282 m_parent->CocoaAddChild(this);
283 SetInitialFrameRect(pos,size);
290 wxWindow::~wxWindow()
292 wxAutoNSAutoreleasePool pool;
295 // Make sure our parent (in the wxWidgets sense) is our superview
296 // before we go removing from it.
297 if(m_parent && m_parent->GetNSView()==[GetNSViewForSuperview() superview])
298 CocoaRemoveFromParent();
300 delete m_wxCocoaScrollView;
306 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
308 NSView *childView = child->GetNSViewForSuperview();
311 [m_cocoaNSView addSubview: childView];
312 child->m_isShown = !m_cocoaHider;
315 void wxWindowCocoa::CocoaRemoveFromParent(void)
317 [GetNSViewForSuperview() removeFromSuperview];
320 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
322 bool need_debug = cocoaNSView || m_cocoaNSView;
323 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d"),this,m_cocoaNSView,[m_cocoaNSView retainCount]);
324 DisassociateNSView(m_cocoaNSView);
325 [cocoaNSView retain];
326 [m_cocoaNSView release];
327 m_cocoaNSView = cocoaNSView;
328 AssociateNSView(m_cocoaNSView);
329 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d"),this,cocoaNSView,[cocoaNSView retainCount]);
332 WX_NSView wxWindowCocoa::GetNSViewForSuperview() const
335 ? m_cocoaHider->GetNSView()
336 : m_wxCocoaScrollView
337 ? m_wxCocoaScrollView->GetNSScrollView()
341 WX_NSView wxWindowCocoa::GetNSViewForHiding() const
343 return m_wxCocoaScrollView
344 ? m_wxCocoaScrollView->GetNSScrollView()
348 NSPoint wxWindowCocoa::CocoaTransformBoundsToWx(NSPoint pointBounds)
350 // TODO: Handle scrolling offset
351 wxCHECK_MSG(GetNSView(), pointBounds, wxT("Need to have a Cocoa view to do translation"));
352 if([GetNSView() isFlipped])
354 NSRect ourBounds = [GetNSView() bounds];
357 , ourBounds.size.height - pointBounds.y
361 NSRect wxWindowCocoa::CocoaTransformBoundsToWx(NSRect rectBounds)
363 // TODO: Handle scrolling offset
364 wxCHECK_MSG(GetNSView(), rectBounds, wxT("Need to have a Cocoa view to do translation"));
365 if([GetNSView() isFlipped])
367 NSRect ourBounds = [GetNSView() bounds];
369 ( rectBounds.origin.x
370 , ourBounds.size.height - (rectBounds.origin.y + rectBounds.size.height)
371 , rectBounds.size.width
372 , rectBounds.size.height
376 NSPoint wxWindowCocoa::CocoaTransformWxToBounds(NSPoint pointWx)
378 // TODO: Handle scrolling offset
379 wxCHECK_MSG(GetNSView(), pointWx, wxT("Need to have a Cocoa view to do translation"));
380 if([GetNSView() isFlipped])
382 NSRect ourBounds = [GetNSView() bounds];
385 , ourBounds.size.height - pointWx.y
389 NSRect wxWindowCocoa::CocoaTransformWxToBounds(NSRect rectWx)
391 // TODO: Handle scrolling offset
392 wxCHECK_MSG(GetNSView(), rectWx, wxT("Need to have a Cocoa view to do translation"));
393 if([GetNSView() isFlipped])
395 NSRect ourBounds = [GetNSView() bounds];
398 , ourBounds.size.height - (rectWx.origin.y + rectWx.size.height)
404 WX_NSAffineTransform wxWindowCocoa::CocoaGetWxToBoundsTransform()
406 // TODO: Handle scrolling offset
407 NSAffineTransform *transform = wxDC::CocoaGetWxToBoundsTransform([GetNSView() isFlipped], [GetNSView() bounds].size.height);
411 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
413 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_drawRect"));
414 // Recursion can happen if the event loop runs from within the paint
415 // handler. For instance, if an assertion dialog is shown.
416 // FIXME: This seems less than ideal.
419 wxLogDebug(wxT("Paint event recursion!"));
424 // Set m_updateRegion
425 const NSRect *rects = ▭ // The bounding box of the region
427 // Try replacing the larger rectangle with a list of smaller ones:
428 if ([GetNSView() respondsToSelector:@selector(getRectsBeingDrawn:count:)])
429 [GetNSView() getRectsBeingDrawn:&rects count:&countRects];
431 NSRect *transformedRects = (NSRect*)malloc(sizeof(NSRect)*countRects);
432 for(int i=0; i<countRects; i++)
434 transformedRects[i] = CocoaTransformBoundsToWx(rects[i]);
436 m_updateRegion = wxRegion(transformedRects,countRects);
437 free(transformedRects);
439 wxPaintEvent event(m_windowId);
440 event.SetEventObject(this);
441 bool ret = GetEventHandler()->ProcessEvent(event);
446 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
448 wxASSERT_MSG([m_cocoaNSView window]==[cocoaEvent window],wxT("Mouse event for different NSWindow"));
449 // Mouse events happen at the NSWindow level so we need to convert
450 // into our bounds coordinates then convert to wx coordinates.
451 NSPoint cocoaPoint = [m_cocoaNSView convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
452 NSPoint pointWx = CocoaTransformBoundsToWx(cocoaPoint);
453 // FIXME: Should we be adjusting for client area origin?
454 const wxPoint &clientorigin = GetClientAreaOrigin();
455 event.m_x = (wxCoord)pointWx.x - clientorigin.x;
456 event.m_y = (wxCoord)pointWx.y - clientorigin.y;
458 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
459 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
460 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
461 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
463 // TODO: set timestamp?
464 event.SetEventObject(this);
465 event.SetId(GetId());
468 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
470 wxMouseEvent event(wxEVT_MOTION);
471 InitMouseEvent(event,theEvent);
472 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
473 return GetEventHandler()->ProcessEvent(event);
476 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
481 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
486 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
488 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
489 InitMouseEvent(event,theEvent);
490 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]);
491 return GetEventHandler()->ProcessEvent(event);
494 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
496 wxMouseEvent event(wxEVT_MOTION);
497 InitMouseEvent(event,theEvent);
498 event.m_leftDown = true;
499 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
500 return GetEventHandler()->ProcessEvent(event);
503 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
505 wxMouseEvent event(wxEVT_LEFT_UP);
506 InitMouseEvent(event,theEvent);
507 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Up @%d,%d"),event.m_x,event.m_y);
508 return GetEventHandler()->ProcessEvent(event);
511 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
513 wxMouseEvent event([theEvent clickCount]<2?wxEVT_RIGHT_DOWN:wxEVT_RIGHT_DCLICK);
514 InitMouseEvent(event,theEvent);
515 wxLogDebug(wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]);
516 return GetEventHandler()->ProcessEvent(event);
519 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
521 wxMouseEvent event(wxEVT_MOTION);
522 InitMouseEvent(event,theEvent);
523 event.m_rightDown = true;
524 wxLogDebug(wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
525 return GetEventHandler()->ProcessEvent(event);
528 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
530 wxMouseEvent event(wxEVT_RIGHT_UP);
531 InitMouseEvent(event,theEvent);
532 wxLogDebug(wxT("Mouse Up @%d,%d"),event.m_x,event.m_y);
533 return GetEventHandler()->ProcessEvent(event);
536 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
541 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
546 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
551 void wxWindowCocoa::Cocoa_FrameChanged(void)
553 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
554 wxSizeEvent event(GetSize(), m_windowId);
555 event.SetEventObject(this);
556 GetEventHandler()->ProcessEvent(event);
559 bool wxWindowCocoa::Cocoa_resetCursorRects()
561 if(!m_cursor.GetNSCursor())
564 [GetNSView() addCursorRect: [GetNSView() visibleRect] cursor: m_cursor.GetNSCursor()];
569 bool wxWindow::Close(bool force)
571 // The only reason this function exists is that it is virtual and
572 // wxTopLevelWindowCocoa will override it.
573 return wxWindowBase::Close(force);
576 void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
578 [[oldView superview] replaceSubview:oldView with:newView];
581 bool wxWindow::EnableSelfAndChildren(bool enable)
583 // If the state isn't changing, don't do anything
584 if(!wxWindowBase::Enable(enable && m_shouldBeEnabled))
586 // Set the state of the Cocoa window
587 CocoaSetEnabled(m_isEnabled);
588 // Disable all children or (if enabling) return them to their proper state
589 for(wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
590 node; node = node->GetNext())
592 node->GetData()->EnableSelfAndChildren(enable);
597 bool wxWindow::Enable(bool enable)
599 // Keep track of what the window SHOULD be doing
600 m_shouldBeEnabled = enable;
601 // If the parent is disabled for any reason, then this window will be too.
602 if(!IsTopLevel() && GetParent())
604 enable = enable && GetParent()->IsEnabled();
606 return EnableSelfAndChildren(enable);
609 bool wxWindow::Show(bool show)
611 wxAutoNSAutoreleasePool pool;
612 // If the window is marked as visible, then it shouldn't have a dummy view
613 // If the window is marked hidden, then it should have a dummy view
614 // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
615 // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),wxT("wxWindow: m_isShown does not agree with m_dummyNSView"));
616 // Return false if there isn't a window to show or hide
617 NSView *cocoaView = GetNSViewForHiding();
622 // If state isn't changing, return false
625 CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView);
626 wxASSERT(![m_cocoaHider->GetNSView() superview]);
629 wxASSERT([cocoaView superview]);
633 // If state isn't changing, return false
636 m_cocoaHider = new wxWindowCocoaHider(this);
637 // NOTE: replaceSubview:with will cause m_cocaNSView to be
638 // (auto)released which balances out addSubview
639 CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView());
640 // m_coocaNSView is now only retained by us
641 wxASSERT([m_cocoaHider->GetNSView() superview]);
642 wxASSERT(![cocoaView superview]);
648 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
650 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":".");
651 int currentX, currentY;
652 int currentW, currentH;
653 DoGetPosition(¤tX, ¤tY);
654 DoGetSize(¤tW, ¤tH);
655 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
657 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
660 AdjustForParentClientOrigin(x,y,sizeFlags);
664 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
666 if(sizeFlags&wxSIZE_AUTO_WIDTH)
668 size=DoGetBestSize();
674 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
676 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
679 size=DoGetBestSize();
685 DoMoveWindow(x,y,width,height);
690 void wxWindowCocoa::DoSetToolTip( wxToolTip *tip )
692 wxWindowBase::DoSetToolTip(tip);
696 m_tooltip->SetWindow((wxWindow *)this);
702 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
704 wxAutoNSAutoreleasePool pool;
705 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
707 NSView *nsview = GetNSViewForSuperview();
708 NSView *superview = [nsview superview];
710 wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent"));
712 NSRect oldFrameRect = [nsview frame];
713 NSRect newFrameRect = GetParent()->CocoaTransformWxToBounds(NSMakeRect(x,y,width,height));
714 [nsview setFrame:newFrameRect];
715 // Be sure to redraw the parent to reflect the changed position
716 [superview setNeedsDisplayInRect:oldFrameRect];
717 [superview setNeedsDisplayInRect:newFrameRect];
720 void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size)
722 NSView *nsview = GetNSViewForSuperview();
723 NSView *superview = [nsview superview];
724 wxCHECK_RET(superview,wxT("NSView does not have a superview"));
725 wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent"));
726 NSRect frameRect = [nsview frame];
728 frameRect.size.width = size.x;
730 frameRect.size.height = size.y;
731 frameRect.origin.x = pos.x;
732 frameRect.origin.y = pos.y;
733 // Tell Cocoa to change the margin between the bottom of the superview
734 // and the bottom of the control. Keeps the control pinned to the top
735 // of its superview so that its position in the wxWidgets coordinate
736 // system doesn't change.
737 if(![superview isFlipped])
738 [nsview setAutoresizingMask: NSViewMinYMargin];
739 // MUST set the mask before setFrame: which can generate a size event
740 // and cause a scroller to be added!
741 frameRect = GetParent()->CocoaTransformWxToBounds(frameRect);
742 [nsview setFrame: frameRect];
746 void wxWindow::DoGetSize(int *w, int *h) const
748 NSRect cocoaRect = [GetNSViewForSuperview() frame];
750 *w=(int)cocoaRect.size.width;
752 *h=(int)cocoaRect.size.height;
753 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
756 void wxWindow::DoGetPosition(int *x, int *y) const
758 NSView *nsview = GetNSViewForSuperview();
760 NSRect cocoaRect = [nsview frame];
761 NSRect rectWx = GetParent()->CocoaTransformBoundsToWx(cocoaRect);
763 *x=(int)rectWx.origin.x;
765 *y=(int)rectWx.origin.y;
766 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
769 WXWidget wxWindow::GetHandle() const
771 return m_cocoaNSView;
774 wxWindow* wxWindow::GetWxWindow() const
776 return (wxWindow*) this;
779 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
781 [m_cocoaNSView setNeedsDisplay:YES];
784 void wxWindow::SetFocus()
786 if([GetNSView() acceptsFirstResponder])
787 [[GetNSView() window] makeFirstResponder: GetNSView()];
790 void wxWindow::DoCaptureMouse()
793 sm_capturedWindow = this;
796 void wxWindow::DoReleaseMouse()
799 sm_capturedWindow = NULL;
802 void wxWindow::DoScreenToClient(int *x, int *y) const
807 void wxWindow::DoClientToScreen(int *x, int *y) const
812 // Get size *available for subwindows* i.e. excluding menu bar etc.
813 void wxWindow::DoGetClientSize(int *x, int *y) const
815 wxLogTrace(wxTRACE_COCOA,wxT("DoGetClientSize:"));
816 if(m_wxCocoaScrollView)
817 m_wxCocoaScrollView->DoGetClientSize(x,y);
819 wxWindowCocoa::DoGetSize(x,y);
822 void wxWindow::DoSetClientSize(int width, int height)
824 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("DoSetClientSize=(%d,%d)"),width,height);
825 if(m_wxCocoaScrollView)
826 m_wxCocoaScrollView->ClientSizeToSize(width,height);
827 CocoaSetWxWindowSize(width,height);
830 void wxWindow::CocoaSetWxWindowSize(int width, int height)
832 wxWindowCocoa::DoSetSize(-1,-1,width,height,wxSIZE_USE_EXISTING);
835 int wxWindow::GetCharHeight() const
841 int wxWindow::GetCharWidth() const
847 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
848 int *descent, int *externalLeading, const wxFont *theFont) const
853 // Coordinates relative to the window
854 void wxWindow::WarpPointer (int x_pos, int y_pos)
859 int wxWindow::GetScrollPos(int orient) const
865 // This now returns the whole range, not just the number
866 // of positions that we can scroll.
867 int wxWindow::GetScrollRange(int orient) const
873 int wxWindow::GetScrollThumb(int orient) const
879 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
884 void wxWindow::CocoaCreateNSScrollView()
886 if(!m_wxCocoaScrollView)
888 m_wxCocoaScrollView = new wxWindowCocoaScrollView(this);
892 // New function that will replace some of the above.
893 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
894 int range, bool refresh)
896 CocoaCreateNSScrollView();
900 // Does a physical scroll
901 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
906 void wxWindow::DoSetVirtualSize( int x, int y )
908 wxWindowBase::DoSetVirtualSize(x,y);
909 CocoaCreateNSScrollView();
910 [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)];
913 bool wxWindow::SetFont(const wxFont& font)
919 static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
921 // first should be ordered higher
923 return NSOrderedDescending;
924 // second should be ordered higher
926 return NSOrderedAscending;
927 return NSOrderedSame;
930 // Raise the window to the top of the Z order
931 void wxWindow::Raise()
933 // wxAutoNSAutoreleasePool pool;
934 NSView *nsview = GetNSViewForSuperview();
935 [[nsview superview] sortSubviewsUsingFunction:
936 CocoaRaiseWindowCompareFunction
940 static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
942 // first should be ordered lower
944 return NSOrderedAscending;
945 // second should be ordered lower
947 return NSOrderedDescending;
948 return NSOrderedSame;
951 // Lower the window to the bottom of the Z order
952 void wxWindow::Lower()
954 NSView *nsview = GetNSViewForSuperview();
955 [[nsview superview] sortSubviewsUsingFunction:
956 CocoaLowerWindowCompareFunction
960 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
965 // Get the window with the focus
966 wxWindow *wxWindowBase::DoFindFocus()
968 // Basically we are somewhat emulating the responder chain here except
969 // we are only loking for the first responder in the key window or
970 // upon failing to find one if the main window is different we look
971 // for the first responder in the main window.
973 // Note that the firstResponder doesn't necessarily have to be an
974 // NSView but wxCocoaNSView::GetFromCocoa() will simply return
975 // NULL unless it finds its argument in its hash map.
979 NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
980 win = wxCocoaNSView::GetFromCocoa([keyWindow firstResponder]);
982 return win->GetWxWindow();
984 NSWindow *mainWindow = [[NSApplication sharedApplication] keyWindow];
985 if(mainWindow == keyWindow)
987 win = wxCocoaNSView::GetFromCocoa([mainWindow firstResponder]);
989 return win->GetWxWindow();
994 /* static */ wxWindow *wxWindowBase::GetCapture()
997 return wxWindowCocoa::sm_capturedWindow;
1000 wxWindow *wxGetActiveWindow()
1006 wxPoint wxGetMousePosition()
1009 return wxDefaultPosition;
1012 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1014 pt = wxGetMousePosition();