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"
16 #include "wx/window.h"
21 #include "wx/tooltip.h"
23 #include "wx/cocoa/autorelease.h"
24 #include "wx/cocoa/string.h"
26 #include "wx/cocoa/objc/NSView.h"
27 #import <AppKit/NSEvent.h>
28 #import <AppKit/NSScrollView.h>
29 #import <AppKit/NSColor.h>
30 #import <AppKit/NSClipView.h>
31 #import <Foundation/NSException.h>
32 #import <AppKit/NSApplication.h>
33 #import <AppKit/NSWindow.h>
35 // Turn this on to paint green over the dummy views for debugging
36 #undef WXCOCOA_FILL_DUMMY_VIEW
38 #ifdef WXCOCOA_FILL_DUMMY_VIEW
39 #import <AppKit/NSBezierPath.h>
40 #endif //def WXCOCOA_FILL_DUMMY_VIEW
42 // A category for methods that are only present in Panther's SDK
43 @interface NSView(wxNSViewPrePantherCompatibility)
44 - (void)getRectsBeingDrawn:(const NSRect **)rects count:(int *)count;
47 NSPoint CocoaTransformNSViewBoundsToWx(NSView *nsview, NSPoint pointBounds)
49 wxCHECK_MSG(nsview, pointBounds, wxT("Need to have a Cocoa view to do translation"));
50 if([nsview isFlipped])
52 NSRect ourBounds = [nsview bounds];
55 , ourBounds.size.height - pointBounds.y
59 NSRect CocoaTransformNSViewBoundsToWx(NSView *nsview, NSRect rectBounds)
61 wxCHECK_MSG(nsview, rectBounds, wxT("Need to have a Cocoa view to do translation"));
62 if([nsview isFlipped])
64 NSRect ourBounds = [nsview bounds];
67 , ourBounds.size.height - (rectBounds.origin.y + rectBounds.size.height)
68 , rectBounds.size.width
69 , rectBounds.size.height
73 NSPoint CocoaTransformNSViewWxToBounds(NSView *nsview, NSPoint pointWx)
75 wxCHECK_MSG(nsview, pointWx, wxT("Need to have a Cocoa view to do translation"));
76 if([nsview isFlipped])
78 NSRect ourBounds = [nsview bounds];
81 , ourBounds.size.height - pointWx.y
85 NSRect CocoaTransformNSViewWxToBounds(NSView *nsview, NSRect rectWx)
87 wxCHECK_MSG(nsview, rectWx, wxT("Need to have a Cocoa view to do translation"));
88 if([nsview isFlipped])
90 NSRect ourBounds = [nsview bounds];
93 , ourBounds.size.height - (rectWx.origin.y + rectWx.size.height)
99 // ========================================================================
100 // wxWindowCocoaHider
101 // ========================================================================
102 class wxWindowCocoaHider: protected wxCocoaNSView
104 DECLARE_NO_COPY_CLASS(wxWindowCocoaHider)
106 wxWindowCocoaHider(wxWindow *owner);
107 virtual ~wxWindowCocoaHider();
108 inline WX_NSView GetNSView() { return m_dummyNSView; }
110 wxWindowCocoa *m_owner;
111 WX_NSView m_dummyNSView;
112 virtual void Cocoa_FrameChanged(void);
113 #ifdef WXCOCOA_FILL_DUMMY_VIEW
114 virtual bool Cocoa_drawRect(const NSRect& rect);
115 #endif //def WXCOCOA_FILL_DUMMY_VIEW
117 wxWindowCocoaHider();
120 // ========================================================================
121 // wxWindowCocoaScrollView
122 // ========================================================================
123 class wxWindowCocoaScrollView: protected wxCocoaNSView
125 DECLARE_NO_COPY_CLASS(wxWindowCocoaScrollView)
127 wxWindowCocoaScrollView(wxWindow *owner);
128 virtual ~wxWindowCocoaScrollView();
129 inline WX_NSScrollView GetNSScrollView() { return m_cocoaNSScrollView; }
130 void ClientSizeToSize(int &width, int &height);
131 void DoGetClientSize(int *x, int *y) const;
133 void Unencapsulate();
135 wxWindowCocoa *m_owner;
136 WX_NSScrollView m_cocoaNSScrollView;
137 virtual void Cocoa_FrameChanged(void);
139 wxWindowCocoaScrollView();
142 // ========================================================================
144 // ========================================================================
145 @interface wxDummyNSView : NSView
146 - (NSView *)hitTest:(NSPoint)aPoint;
148 WX_DECLARE_GET_OBJC_CLASS(wxDummyNSView,NSView)
150 @implementation wxDummyNSView : NSView
151 - (NSView *)hitTest:(NSPoint)aPoint
157 WX_IMPLEMENT_GET_OBJC_CLASS(wxDummyNSView,NSView)
159 // ========================================================================
160 // wxWindowCocoaHider
161 // ========================================================================
162 wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner)
166 wxASSERT(owner->GetNSViewForHiding());
167 m_dummyNSView = [[WX_GET_OBJC_CLASS(wxDummyNSView) alloc]
168 initWithFrame:[owner->GetNSViewForHiding() frame]];
169 [m_dummyNSView setAutoresizingMask: [owner->GetNSViewForHiding() autoresizingMask]];
170 AssociateNSView(m_dummyNSView);
173 wxWindowCocoaHider::~wxWindowCocoaHider()
175 DisassociateNSView(m_dummyNSView);
176 [m_dummyNSView release];
179 void wxWindowCocoaHider::Cocoa_FrameChanged(void)
181 // Keep the real window in synch with the dummy
182 wxASSERT(m_dummyNSView);
183 [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]];
187 #ifdef WXCOCOA_FILL_DUMMY_VIEW
188 bool wxWindowCocoaHider::Cocoa_drawRect(const NSRect& rect)
190 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:rect];
191 [[NSColor greenColor] set];
196 #endif //def WXCOCOA_FILL_DUMMY_VIEW
198 // ========================================================================
199 // wxFlippedNSClipView
200 // ========================================================================
201 @interface wxFlippedNSClipView : NSClipView
204 WX_DECLARE_GET_OBJC_CLASS(wxFlippedNSClipView,NSClipView)
206 @implementation wxFlippedNSClipView : NSClipView
213 WX_IMPLEMENT_GET_OBJC_CLASS(wxFlippedNSClipView,NSClipView)
215 // ========================================================================
216 // wxWindowCocoaScrollView
217 // ========================================================================
218 wxWindowCocoaScrollView::wxWindowCocoaScrollView(wxWindow *owner)
221 wxAutoNSAutoreleasePool pool;
223 wxASSERT(owner->GetNSView());
224 m_cocoaNSScrollView = [[NSScrollView alloc]
225 initWithFrame:[owner->GetNSView() frame]];
226 AssociateNSView(m_cocoaNSScrollView);
228 /* Replace the default NSClipView with a flipped one. This ensures
229 scrolling is "pinned" to the top-left instead of bottom-right. */
230 NSClipView *flippedClip = [[WX_GET_OBJC_CLASS(wxFlippedNSClipView) alloc]
231 initWithFrame: [[m_cocoaNSScrollView contentView] frame]];
232 [m_cocoaNSScrollView setContentView:flippedClip];
233 [flippedClip release];
235 [m_cocoaNSScrollView setBackgroundColor: [NSColor windowBackgroundColor]];
236 [m_cocoaNSScrollView setHasHorizontalScroller: YES];
237 [m_cocoaNSScrollView setHasVerticalScroller: YES];
241 void wxWindowCocoaScrollView::Encapsulate()
243 // Set the scroll view autoresizingMask to match the current NSView
244 [m_cocoaNSScrollView setAutoresizingMask: [m_owner->GetNSView() autoresizingMask]];
245 [m_owner->GetNSView() setAutoresizingMask: NSViewNotSizable];
246 // NOTE: replaceSubView will cause m_cocaNSView to be released
247 // except when it hasn't been added into an NSView hierarchy in which
248 // case it doesn't need to be and this should work out to a no-op
249 m_owner->CocoaReplaceView(m_owner->GetNSView(), m_cocoaNSScrollView);
250 // The NSView is still retained by owner
251 [m_cocoaNSScrollView setDocumentView: m_owner->GetNSView()];
252 // Now it's also retained by the NSScrollView
255 void wxWindowCocoaScrollView::Unencapsulate()
257 [m_cocoaNSScrollView setDocumentView: nil];
258 m_owner->CocoaReplaceView(m_cocoaNSScrollView, m_owner->GetNSView());
259 if(![[m_owner->GetNSView() superview] isFlipped])
260 [m_owner->GetNSView() setAutoresizingMask: NSViewMinYMargin];
263 wxWindowCocoaScrollView::~wxWindowCocoaScrollView()
265 DisassociateNSView(m_cocoaNSScrollView);
266 [m_cocoaNSScrollView release];
269 void wxWindowCocoaScrollView::ClientSizeToSize(int &width, int &height)
271 NSSize frameSize = [NSScrollView
272 frameSizeForContentSize: NSMakeSize(width,height)
273 hasHorizontalScroller: [m_cocoaNSScrollView hasHorizontalScroller]
274 hasVerticalScroller: [m_cocoaNSScrollView hasVerticalScroller]
275 borderType: [m_cocoaNSScrollView borderType]];
276 width = (int)frameSize.width;
277 height = (int)frameSize.height;
280 void wxWindowCocoaScrollView::DoGetClientSize(int *x, int *y) const
282 NSSize nssize = [m_cocoaNSScrollView contentSize];
284 *x = (int)nssize.width;
286 *y = (int)nssize.height;
289 void wxWindowCocoaScrollView::Cocoa_FrameChanged(void)
291 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
292 wxSizeEvent event(m_owner->GetSize(), m_owner->GetId());
293 event.SetEventObject(m_owner);
294 m_owner->GetEventHandler()->ProcessEvent(event);
297 // ========================================================================
299 // ========================================================================
300 // normally the base classes aren't included, but wxWindow is special
301 #ifdef __WXUNIVERSAL__
302 IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
304 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
307 BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
310 wxWindow *wxWindowCocoa::sm_capturedWindow = NULL;
313 void wxWindowCocoa::Init()
315 m_cocoaNSView = NULL;
317 m_wxCocoaScrollView = NULL;
318 m_isBeingDeleted = false;
323 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
327 const wxString& name)
329 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
332 // TODO: create the window
333 m_cocoaNSView = NULL;
334 SetNSView([[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: MakeDefaultNSRect(size)]);
335 [m_cocoaNSView release];
339 m_parent->AddChild(this);
340 m_parent->CocoaAddChild(this);
341 SetInitialFrameRect(pos,size);
348 wxWindow::~wxWindow()
350 wxAutoNSAutoreleasePool pool;
353 // Make sure our parent (in the wxWidgets sense) is our superview
354 // before we go removing from it.
355 if(m_parent && m_parent->GetNSView()==[GetNSViewForSuperview() superview])
356 CocoaRemoveFromParent();
358 delete m_wxCocoaScrollView;
364 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
366 NSView *childView = child->GetNSViewForSuperview();
369 [m_cocoaNSView addSubview: childView];
370 child->m_isShown = !m_cocoaHider;
373 void wxWindowCocoa::CocoaRemoveFromParent(void)
375 [GetNSViewForSuperview() removeFromSuperview];
378 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
380 bool need_debug = cocoaNSView || m_cocoaNSView;
381 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d"),this,m_cocoaNSView,[m_cocoaNSView retainCount]);
382 DisassociateNSView(m_cocoaNSView);
383 [cocoaNSView retain];
384 [m_cocoaNSView release];
385 m_cocoaNSView = cocoaNSView;
386 AssociateNSView(m_cocoaNSView);
387 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d"),this,cocoaNSView,[cocoaNSView retainCount]);
390 WX_NSView wxWindowCocoa::GetNSViewForSuperview() const
393 ? m_cocoaHider->GetNSView()
394 : m_wxCocoaScrollView
395 ? m_wxCocoaScrollView->GetNSScrollView()
399 WX_NSView wxWindowCocoa::GetNSViewForHiding() const
401 return m_wxCocoaScrollView
402 ? m_wxCocoaScrollView->GetNSScrollView()
406 NSPoint wxWindowCocoa::CocoaTransformBoundsToWx(NSPoint pointBounds)
408 // TODO: Handle scrolling offset
409 return CocoaTransformNSViewBoundsToWx(GetNSView(), pointBounds);
412 NSRect wxWindowCocoa::CocoaTransformBoundsToWx(NSRect rectBounds)
414 // TODO: Handle scrolling offset
415 return CocoaTransformNSViewBoundsToWx(GetNSView(), rectBounds);
418 NSPoint wxWindowCocoa::CocoaTransformWxToBounds(NSPoint pointWx)
420 // TODO: Handle scrolling offset
421 return CocoaTransformNSViewWxToBounds(GetNSView(), pointWx);
424 NSRect wxWindowCocoa::CocoaTransformWxToBounds(NSRect rectWx)
426 // TODO: Handle scrolling offset
427 return CocoaTransformNSViewWxToBounds(GetNSView(), rectWx);
430 WX_NSAffineTransform wxWindowCocoa::CocoaGetWxToBoundsTransform()
432 // TODO: Handle scrolling offset
433 NSAffineTransform *transform = wxDC::CocoaGetWxToBoundsTransform([GetNSView() isFlipped], [GetNSView() bounds].size.height);
437 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
439 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_drawRect"));
440 // Recursion can happen if the event loop runs from within the paint
441 // handler. For instance, if an assertion dialog is shown.
442 // FIXME: This seems less than ideal.
445 wxLogDebug(wxT("Paint event recursion!"));
450 // Set m_updateRegion
451 const NSRect *rects = ▭ // The bounding box of the region
453 // Try replacing the larger rectangle with a list of smaller ones:
454 if ([GetNSView() respondsToSelector:@selector(getRectsBeingDrawn:count:)])
455 [GetNSView() getRectsBeingDrawn:&rects count:&countRects];
457 NSRect *transformedRects = (NSRect*)malloc(sizeof(NSRect)*countRects);
458 for(int i=0; i<countRects; i++)
460 transformedRects[i] = CocoaTransformBoundsToWx(rects[i]);
462 m_updateRegion = wxRegion(transformedRects,countRects);
463 free(transformedRects);
465 wxPaintEvent event(m_windowId);
466 event.SetEventObject(this);
467 bool ret = GetEventHandler()->ProcessEvent(event);
472 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
474 wxASSERT_MSG([m_cocoaNSView window]==[cocoaEvent window],wxT("Mouse event for different NSWindow"));
475 // Mouse events happen at the NSWindow level so we need to convert
476 // into our bounds coordinates then convert to wx coordinates.
477 NSPoint cocoaPoint = [m_cocoaNSView convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
478 NSPoint pointWx = CocoaTransformBoundsToWx(cocoaPoint);
479 // FIXME: Should we be adjusting for client area origin?
480 const wxPoint &clientorigin = GetClientAreaOrigin();
481 event.m_x = (wxCoord)pointWx.x - clientorigin.x;
482 event.m_y = (wxCoord)pointWx.y - clientorigin.y;
484 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
485 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
486 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
487 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
489 // TODO: set timestamp?
490 event.SetEventObject(this);
491 event.SetId(GetId());
494 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
496 wxMouseEvent event(wxEVT_MOTION);
497 InitMouseEvent(event,theEvent);
498 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
499 return GetEventHandler()->ProcessEvent(event);
502 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
507 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
512 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
514 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
515 InitMouseEvent(event,theEvent);
516 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]);
517 return GetEventHandler()->ProcessEvent(event);
520 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
522 wxMouseEvent event(wxEVT_MOTION);
523 InitMouseEvent(event,theEvent);
524 event.m_leftDown = true;
525 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
526 return GetEventHandler()->ProcessEvent(event);
529 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
531 wxMouseEvent event(wxEVT_LEFT_UP);
532 InitMouseEvent(event,theEvent);
533 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Up @%d,%d"),event.m_x,event.m_y);
534 return GetEventHandler()->ProcessEvent(event);
537 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
539 wxMouseEvent event([theEvent clickCount]<2?wxEVT_RIGHT_DOWN:wxEVT_RIGHT_DCLICK);
540 InitMouseEvent(event,theEvent);
541 wxLogDebug(wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]);
542 return GetEventHandler()->ProcessEvent(event);
545 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
547 wxMouseEvent event(wxEVT_MOTION);
548 InitMouseEvent(event,theEvent);
549 event.m_rightDown = true;
550 wxLogDebug(wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
551 return GetEventHandler()->ProcessEvent(event);
554 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
556 wxMouseEvent event(wxEVT_RIGHT_UP);
557 InitMouseEvent(event,theEvent);
558 wxLogDebug(wxT("Mouse Up @%d,%d"),event.m_x,event.m_y);
559 return GetEventHandler()->ProcessEvent(event);
562 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
567 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
572 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
577 void wxWindowCocoa::Cocoa_FrameChanged(void)
579 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
580 wxSizeEvent event(GetSize(), m_windowId);
581 event.SetEventObject(this);
582 GetEventHandler()->ProcessEvent(event);
585 bool wxWindowCocoa::Cocoa_resetCursorRects()
587 if(!m_cursor.GetNSCursor())
590 [GetNSView() addCursorRect: [GetNSView() visibleRect] cursor: m_cursor.GetNSCursor()];
595 bool wxWindow::Close(bool force)
597 // The only reason this function exists is that it is virtual and
598 // wxTopLevelWindowCocoa will override it.
599 return wxWindowBase::Close(force);
602 void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
604 [[oldView superview] replaceSubview:oldView with:newView];
607 void wxWindow::DoEnable(bool enable)
609 CocoaSetEnabled(enable);
612 bool wxWindow::Show(bool show)
614 wxAutoNSAutoreleasePool pool;
615 // If the window is marked as visible, then it shouldn't have a dummy view
616 // If the window is marked hidden, then it should have a dummy view
617 // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
618 // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),wxT("wxWindow: m_isShown does not agree with m_dummyNSView"));
619 // Return false if there isn't a window to show or hide
620 NSView *cocoaView = GetNSViewForHiding();
625 // If state isn't changing, return false
628 CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView);
629 wxASSERT(![m_cocoaHider->GetNSView() superview]);
632 wxASSERT([cocoaView superview]);
636 // If state isn't changing, return false
639 m_cocoaHider = new wxWindowCocoaHider(this);
640 // NOTE: replaceSubview:with will cause m_cocaNSView to be
641 // (auto)released which balances out addSubview
642 CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView());
643 // m_coocaNSView is now only retained by us
644 wxASSERT([m_cocoaHider->GetNSView() superview]);
645 wxASSERT(![cocoaView superview]);
651 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
653 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":".");
654 int currentX, currentY;
655 int currentW, currentH;
656 DoGetPosition(¤tX, ¤tY);
657 DoGetSize(¤tW, ¤tH);
658 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
660 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
663 AdjustForParentClientOrigin(x,y,sizeFlags);
665 wxSize size(wxDefaultSize);
667 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
669 if(sizeFlags&wxSIZE_AUTO_WIDTH)
671 size=DoGetBestSize();
677 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
679 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
682 size=DoGetBestSize();
688 DoMoveWindow(x,y,width,height);
693 void wxWindowCocoa::DoSetToolTip( wxToolTip *tip )
695 wxWindowBase::DoSetToolTip(tip);
699 m_tooltip->SetWindow((wxWindow *)this);
705 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
707 wxAutoNSAutoreleasePool pool;
708 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
710 NSView *nsview = GetNSViewForSuperview();
711 NSView *superview = [nsview superview];
713 wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent"));
715 NSRect oldFrameRect = [nsview frame];
716 NSRect newFrameRect = GetParent()->CocoaTransformWxToBounds(NSMakeRect(x,y,width,height));
717 [nsview setFrame:newFrameRect];
718 // Be sure to redraw the parent to reflect the changed position
719 [superview setNeedsDisplayInRect:oldFrameRect];
720 [superview setNeedsDisplayInRect:newFrameRect];
723 void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size)
725 NSView *nsview = GetNSViewForSuperview();
726 NSView *superview = [nsview superview];
727 wxCHECK_RET(superview,wxT("NSView does not have a superview"));
728 wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent"));
729 NSRect frameRect = [nsview frame];
731 frameRect.size.width = size.x;
733 frameRect.size.height = size.y;
734 frameRect.origin.x = pos.x;
735 frameRect.origin.y = pos.y;
736 // Tell Cocoa to change the margin between the bottom of the superview
737 // and the bottom of the control. Keeps the control pinned to the top
738 // of its superview so that its position in the wxWidgets coordinate
739 // system doesn't change.
740 if(![superview isFlipped])
741 [nsview setAutoresizingMask: NSViewMinYMargin];
742 // MUST set the mask before setFrame: which can generate a size event
743 // and cause a scroller to be added!
744 frameRect = GetParent()->CocoaTransformWxToBounds(frameRect);
745 [nsview setFrame: frameRect];
749 void wxWindow::DoGetSize(int *w, int *h) const
751 NSRect cocoaRect = [GetNSViewForSuperview() frame];
753 *w=(int)cocoaRect.size.width;
755 *h=(int)cocoaRect.size.height;
756 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
759 void wxWindow::DoGetPosition(int *x, int *y) const
761 NSView *nsview = GetNSViewForSuperview();
763 NSRect cocoaRect = [nsview frame];
764 NSRect rectWx = GetParent()->CocoaTransformBoundsToWx(cocoaRect);
766 *x=(int)rectWx.origin.x;
768 *y=(int)rectWx.origin.y;
769 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
772 WXWidget wxWindow::GetHandle() const
774 return m_cocoaNSView;
777 wxWindow* wxWindow::GetWxWindow() const
779 return (wxWindow*) this;
782 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
784 [m_cocoaNSView setNeedsDisplay:YES];
787 void wxWindow::SetFocus()
789 if([GetNSView() acceptsFirstResponder])
790 [[GetNSView() window] makeFirstResponder: GetNSView()];
793 void wxWindow::DoCaptureMouse()
796 sm_capturedWindow = this;
799 void wxWindow::DoReleaseMouse()
802 sm_capturedWindow = NULL;
805 void wxWindow::DoScreenToClient(int *x, int *y) const
810 void wxWindow::DoClientToScreen(int *x, int *y) const
815 // Get size *available for subwindows* i.e. excluding menu bar etc.
816 void wxWindow::DoGetClientSize(int *x, int *y) const
818 wxLogTrace(wxTRACE_COCOA,wxT("DoGetClientSize:"));
819 if(m_wxCocoaScrollView)
820 m_wxCocoaScrollView->DoGetClientSize(x,y);
822 wxWindowCocoa::DoGetSize(x,y);
825 void wxWindow::DoSetClientSize(int width, int height)
827 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("DoSetClientSize=(%d,%d)"),width,height);
828 if(m_wxCocoaScrollView)
829 m_wxCocoaScrollView->ClientSizeToSize(width,height);
830 CocoaSetWxWindowSize(width,height);
833 void wxWindow::CocoaSetWxWindowSize(int width, int height)
835 wxWindowCocoa::DoSetSize(wxDefaultCoord,wxDefaultCoord,width,height,wxSIZE_USE_EXISTING);
838 void wxWindow::SetLabel(const wxString& WXUNUSED(label))
840 // Intentional no-op.
843 wxString wxWindow::GetLabel() const
845 // General Get/Set of labels is implemented in wxControlBase
846 wxLogDebug(wxT("wxWindow::GetLabel: Should be overridden if needed."));
847 return wxEmptyString;
850 int wxWindow::GetCharHeight() const
856 int wxWindow::GetCharWidth() const
862 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
863 int *descent, int *externalLeading, const wxFont *theFont) const
868 // Coordinates relative to the window
869 void wxWindow::WarpPointer (int x_pos, int y_pos)
874 int wxWindow::GetScrollPos(int orient) const
880 // This now returns the whole range, not just the number
881 // of positions that we can scroll.
882 int wxWindow::GetScrollRange(int orient) const
888 int wxWindow::GetScrollThumb(int orient) const
894 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
899 void wxWindow::CocoaCreateNSScrollView()
901 if(!m_wxCocoaScrollView)
903 m_wxCocoaScrollView = new wxWindowCocoaScrollView(this);
907 // New function that will replace some of the above.
908 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
909 int range, bool refresh)
911 CocoaCreateNSScrollView();
915 // Does a physical scroll
916 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
921 void wxWindow::DoSetVirtualSize( int x, int y )
923 wxWindowBase::DoSetVirtualSize(x,y);
924 CocoaCreateNSScrollView();
925 [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)];
928 bool wxWindow::SetFont(const wxFont& font)
934 static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
936 // first should be ordered higher
938 return NSOrderedDescending;
939 // second should be ordered higher
941 return NSOrderedAscending;
942 return NSOrderedSame;
945 // Raise the window to the top of the Z order
946 void wxWindow::Raise()
948 // wxAutoNSAutoreleasePool pool;
949 NSView *nsview = GetNSViewForSuperview();
950 [[nsview superview] sortSubviewsUsingFunction:
951 CocoaRaiseWindowCompareFunction
955 static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
957 // first should be ordered lower
959 return NSOrderedAscending;
960 // second should be ordered lower
962 return NSOrderedDescending;
963 return NSOrderedSame;
966 // Lower the window to the bottom of the Z order
967 void wxWindow::Lower()
969 NSView *nsview = GetNSViewForSuperview();
970 [[nsview superview] sortSubviewsUsingFunction:
971 CocoaLowerWindowCompareFunction
975 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
980 // Get the window with the focus
981 wxWindow *wxWindowBase::DoFindFocus()
983 // Basically we are somewhat emulating the responder chain here except
984 // we are only loking for the first responder in the key window or
985 // upon failing to find one if the main window is different we look
986 // for the first responder in the main window.
988 // Note that the firstResponder doesn't necessarily have to be an
989 // NSView but wxCocoaNSView::GetFromCocoa() will simply return
990 // NULL unless it finds its argument in its hash map.
994 NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
995 win = wxCocoaNSView::GetFromCocoa(static_cast<NSView*>([keyWindow firstResponder]));
997 return win->GetWxWindow();
999 NSWindow *mainWindow = [[NSApplication sharedApplication] keyWindow];
1000 if(mainWindow == keyWindow)
1002 win = wxCocoaNSView::GetFromCocoa(static_cast<NSView*>([mainWindow firstResponder]));
1004 return win->GetWxWindow();
1009 /* static */ wxWindow *wxWindowBase::GetCapture()
1012 return wxWindowCocoa::sm_capturedWindow;
1015 wxWindow *wxGetActiveWindow()
1021 wxPoint wxGetMousePosition()
1024 return wxDefaultPosition;
1027 wxMouseState wxGetMouseState()
1034 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1036 pt = wxGetMousePosition();