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"
20 #include "wx/tooltip.h"
22 #include "wx/cocoa/autorelease.h"
23 #include "wx/cocoa/string.h"
25 #import <AppKit/NSView.h>
26 #import <AppKit/NSEvent.h>
27 #import <AppKit/NSScrollView.h>
28 #import <AppKit/NSColor.h>
29 #import <AppKit/NSClipView.h>
30 #import <Foundation/NSException.h>
31 #import <AppKit/NSApplication.h>
32 #import <AppKit/NSWindow.h>
34 // Turn this on to paint green over the dummy views for debugging
35 #undef WXCOCOA_FILL_DUMMY_VIEW
37 #ifdef WXCOCOA_FILL_DUMMY_VIEW
38 #import <AppKit/NSBezierPath.h>
39 #endif //def WXCOCOA_FILL_DUMMY_VIEW
41 // A category for methods that are only present in Panther's SDK
42 @interface NSView(wxNSViewPrePantherCompatibility)
43 - (void)getRectsBeingDrawn:(const NSRect **)rects count:(int *)count;
46 NSPoint CocoaTransformNSViewBoundsToWx(NSView *nsview, NSPoint pointBounds)
48 wxCHECK_MSG(nsview, pointBounds, wxT("Need to have a Cocoa view to do translation"));
49 if([nsview isFlipped])
51 NSRect ourBounds = [nsview bounds];
54 , ourBounds.size.height - pointBounds.y
58 NSRect CocoaTransformNSViewBoundsToWx(NSView *nsview, NSRect rectBounds)
60 wxCHECK_MSG(nsview, rectBounds, wxT("Need to have a Cocoa view to do translation"));
61 if([nsview isFlipped])
63 NSRect ourBounds = [nsview bounds];
66 , ourBounds.size.height - (rectBounds.origin.y + rectBounds.size.height)
67 , rectBounds.size.width
68 , rectBounds.size.height
72 NSPoint CocoaTransformNSViewWxToBounds(NSView *nsview, NSPoint pointWx)
74 wxCHECK_MSG(nsview, pointWx, wxT("Need to have a Cocoa view to do translation"));
75 if([nsview isFlipped])
77 NSRect ourBounds = [nsview bounds];
80 , ourBounds.size.height - pointWx.y
84 NSRect CocoaTransformNSViewWxToBounds(NSView *nsview, NSRect rectWx)
86 wxCHECK_MSG(nsview, rectWx, wxT("Need to have a Cocoa view to do translation"));
87 if([nsview isFlipped])
89 NSRect ourBounds = [nsview bounds];
92 , ourBounds.size.height - (rectWx.origin.y + rectWx.size.height)
98 // ========================================================================
100 // ========================================================================
101 class wxWindowCocoaHider: protected wxCocoaNSView
103 DECLARE_NO_COPY_CLASS(wxWindowCocoaHider)
105 wxWindowCocoaHider(wxWindow *owner);
106 virtual ~wxWindowCocoaHider();
107 inline WX_NSView GetNSView() { return m_dummyNSView; }
109 wxWindowCocoa *m_owner;
110 WX_NSView m_dummyNSView;
111 virtual void Cocoa_FrameChanged(void);
112 #ifdef WXCOCOA_FILL_DUMMY_VIEW
113 virtual bool Cocoa_drawRect(const NSRect& rect);
114 #endif //def WXCOCOA_FILL_DUMMY_VIEW
116 wxWindowCocoaHider();
119 // ========================================================================
120 // wxWindowCocoaScrollView
121 // ========================================================================
122 class wxWindowCocoaScrollView: protected wxCocoaNSView
124 DECLARE_NO_COPY_CLASS(wxWindowCocoaScrollView)
126 wxWindowCocoaScrollView(wxWindow *owner);
127 virtual ~wxWindowCocoaScrollView();
128 inline WX_NSScrollView GetNSScrollView() { return m_cocoaNSScrollView; }
129 void ClientSizeToSize(int &width, int &height);
130 void DoGetClientSize(int *x, int *y) const;
132 void Unencapsulate();
134 wxWindowCocoa *m_owner;
135 WX_NSScrollView m_cocoaNSScrollView;
136 virtual void Cocoa_FrameChanged(void);
138 wxWindowCocoaScrollView();
141 // ========================================================================
143 // ========================================================================
144 @interface wxDummyNSView : NSView
145 - (NSView *)hitTest:(NSPoint)aPoint;
148 @implementation wxDummyNSView : NSView
149 - (NSView *)hitTest:(NSPoint)aPoint
156 // ========================================================================
157 // wxWindowCocoaHider
158 // ========================================================================
159 wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner)
163 wxASSERT(owner->GetNSViewForHiding());
164 m_dummyNSView = [[wxDummyNSView alloc]
165 initWithFrame:[owner->GetNSViewForHiding() frame]];
166 [m_dummyNSView setAutoresizingMask: [owner->GetNSViewForHiding() autoresizingMask]];
167 AssociateNSView(m_dummyNSView);
170 wxWindowCocoaHider::~wxWindowCocoaHider()
172 DisassociateNSView(m_dummyNSView);
173 [m_dummyNSView release];
176 void wxWindowCocoaHider::Cocoa_FrameChanged(void)
178 // Keep the real window in synch with the dummy
179 wxASSERT(m_dummyNSView);
180 [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]];
184 #ifdef WXCOCOA_FILL_DUMMY_VIEW
185 bool wxWindowCocoaHider::Cocoa_drawRect(const NSRect& rect)
187 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:rect];
188 [[NSColor greenColor] set];
193 #endif //def WXCOCOA_FILL_DUMMY_VIEW
195 // ========================================================================
196 // wxFlippedNSClipView
197 // ========================================================================
198 @interface wxFlippedNSClipView : NSClipView
202 @implementation wxFlippedNSClipView : NSClipView
210 // ========================================================================
211 // wxWindowCocoaScrollView
212 // ========================================================================
213 wxWindowCocoaScrollView::wxWindowCocoaScrollView(wxWindow *owner)
216 wxAutoNSAutoreleasePool pool;
218 wxASSERT(owner->GetNSView());
219 m_cocoaNSScrollView = [[NSScrollView alloc]
220 initWithFrame:[owner->GetNSView() frame]];
221 AssociateNSView(m_cocoaNSScrollView);
223 /* Replace the default NSClipView with a flipped one. This ensures
224 scrolling is "pinned" to the top-left instead of bottom-right. */
225 NSClipView *flippedClip = [[wxFlippedNSClipView alloc]
226 initWithFrame: [[m_cocoaNSScrollView contentView] frame]];
227 [m_cocoaNSScrollView setContentView:flippedClip];
228 [flippedClip release];
230 [m_cocoaNSScrollView setBackgroundColor: [NSColor windowBackgroundColor]];
231 [m_cocoaNSScrollView setHasHorizontalScroller: YES];
232 [m_cocoaNSScrollView setHasVerticalScroller: YES];
236 void wxWindowCocoaScrollView::Encapsulate()
238 // Set the scroll view autoresizingMask to match the current NSView
239 [m_cocoaNSScrollView setAutoresizingMask: [m_owner->GetNSView() autoresizingMask]];
240 [m_owner->GetNSView() setAutoresizingMask: NSViewNotSizable];
241 // NOTE: replaceSubView will cause m_cocaNSView to be released
242 // except when it hasn't been added into an NSView hierarchy in which
243 // case it doesn't need to be and this should work out to a no-op
244 m_owner->CocoaReplaceView(m_owner->GetNSView(), m_cocoaNSScrollView);
245 // The NSView is still retained by owner
246 [m_cocoaNSScrollView setDocumentView: m_owner->GetNSView()];
247 // Now it's also retained by the NSScrollView
250 void wxWindowCocoaScrollView::Unencapsulate()
252 [m_cocoaNSScrollView setDocumentView: nil];
253 m_owner->CocoaReplaceView(m_cocoaNSScrollView, m_owner->GetNSView());
254 if(![[m_owner->GetNSView() superview] isFlipped])
255 [m_owner->GetNSView() setAutoresizingMask: NSViewMinYMargin];
258 wxWindowCocoaScrollView::~wxWindowCocoaScrollView()
260 DisassociateNSView(m_cocoaNSScrollView);
261 [m_cocoaNSScrollView release];
264 void wxWindowCocoaScrollView::ClientSizeToSize(int &width, int &height)
266 NSSize frameSize = [NSScrollView
267 frameSizeForContentSize: NSMakeSize(width,height)
268 hasHorizontalScroller: [m_cocoaNSScrollView hasHorizontalScroller]
269 hasVerticalScroller: [m_cocoaNSScrollView hasVerticalScroller]
270 borderType: [m_cocoaNSScrollView borderType]];
271 width = (int)frameSize.width;
272 height = (int)frameSize.height;
275 void wxWindowCocoaScrollView::DoGetClientSize(int *x, int *y) const
277 NSSize nssize = [m_cocoaNSScrollView contentSize];
279 *x = (int)nssize.width;
281 *y = (int)nssize.height;
284 void wxWindowCocoaScrollView::Cocoa_FrameChanged(void)
286 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
287 wxSizeEvent event(m_owner->GetSize(), m_owner->GetId());
288 event.SetEventObject(m_owner);
289 m_owner->GetEventHandler()->ProcessEvent(event);
292 // ========================================================================
294 // ========================================================================
295 // normally the base classes aren't included, but wxWindow is special
296 #ifdef __WXUNIVERSAL__
297 IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
299 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
302 BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
305 wxWindow *wxWindowCocoa::sm_capturedWindow = NULL;
308 void wxWindowCocoa::Init()
310 m_cocoaNSView = NULL;
312 m_wxCocoaScrollView = NULL;
313 m_isBeingDeleted = false;
315 m_shouldBeEnabled = true;
319 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
323 const wxString& name)
325 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
328 // TODO: create the window
329 m_cocoaNSView = NULL;
330 SetNSView([[NSView alloc] initWithFrame: MakeDefaultNSRect(size)]);
331 [m_cocoaNSView release];
335 m_parent->AddChild(this);
336 m_parent->CocoaAddChild(this);
337 SetInitialFrameRect(pos,size);
344 wxWindow::~wxWindow()
346 wxAutoNSAutoreleasePool pool;
349 // Make sure our parent (in the wxWidgets sense) is our superview
350 // before we go removing from it.
351 if(m_parent && m_parent->GetNSView()==[GetNSViewForSuperview() superview])
352 CocoaRemoveFromParent();
354 delete m_wxCocoaScrollView;
360 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
362 NSView *childView = child->GetNSViewForSuperview();
365 [m_cocoaNSView addSubview: childView];
366 child->m_isShown = !m_cocoaHider;
369 void wxWindowCocoa::CocoaRemoveFromParent(void)
371 [GetNSViewForSuperview() removeFromSuperview];
374 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
376 bool need_debug = cocoaNSView || m_cocoaNSView;
377 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d"),this,m_cocoaNSView,[m_cocoaNSView retainCount]);
378 DisassociateNSView(m_cocoaNSView);
379 [cocoaNSView retain];
380 [m_cocoaNSView release];
381 m_cocoaNSView = cocoaNSView;
382 AssociateNSView(m_cocoaNSView);
383 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d"),this,cocoaNSView,[cocoaNSView retainCount]);
386 WX_NSView wxWindowCocoa::GetNSViewForSuperview() const
389 ? m_cocoaHider->GetNSView()
390 : m_wxCocoaScrollView
391 ? m_wxCocoaScrollView->GetNSScrollView()
395 WX_NSView wxWindowCocoa::GetNSViewForHiding() const
397 return m_wxCocoaScrollView
398 ? m_wxCocoaScrollView->GetNSScrollView()
402 NSPoint wxWindowCocoa::CocoaTransformBoundsToWx(NSPoint pointBounds)
404 // TODO: Handle scrolling offset
405 return CocoaTransformNSViewBoundsToWx(GetNSView(), pointBounds);
408 NSRect wxWindowCocoa::CocoaTransformBoundsToWx(NSRect rectBounds)
410 // TODO: Handle scrolling offset
411 return CocoaTransformNSViewBoundsToWx(GetNSView(), rectBounds);
414 NSPoint wxWindowCocoa::CocoaTransformWxToBounds(NSPoint pointWx)
416 // TODO: Handle scrolling offset
417 return CocoaTransformNSViewWxToBounds(GetNSView(), pointWx);
420 NSRect wxWindowCocoa::CocoaTransformWxToBounds(NSRect rectWx)
422 // TODO: Handle scrolling offset
423 return CocoaTransformNSViewWxToBounds(GetNSView(), rectWx);
426 WX_NSAffineTransform wxWindowCocoa::CocoaGetWxToBoundsTransform()
428 // TODO: Handle scrolling offset
429 NSAffineTransform *transform = wxDC::CocoaGetWxToBoundsTransform([GetNSView() isFlipped], [GetNSView() bounds].size.height);
433 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
435 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_drawRect"));
436 // Recursion can happen if the event loop runs from within the paint
437 // handler. For instance, if an assertion dialog is shown.
438 // FIXME: This seems less than ideal.
441 wxLogDebug(wxT("Paint event recursion!"));
446 // Set m_updateRegion
447 const NSRect *rects = ▭ // The bounding box of the region
449 // Try replacing the larger rectangle with a list of smaller ones:
450 if ([GetNSView() respondsToSelector:@selector(getRectsBeingDrawn:count:)])
451 [GetNSView() getRectsBeingDrawn:&rects count:&countRects];
453 NSRect *transformedRects = (NSRect*)malloc(sizeof(NSRect)*countRects);
454 for(int i=0; i<countRects; i++)
456 transformedRects[i] = CocoaTransformBoundsToWx(rects[i]);
458 m_updateRegion = wxRegion(transformedRects,countRects);
459 free(transformedRects);
461 wxPaintEvent event(m_windowId);
462 event.SetEventObject(this);
463 bool ret = GetEventHandler()->ProcessEvent(event);
468 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
470 wxASSERT_MSG([m_cocoaNSView window]==[cocoaEvent window],wxT("Mouse event for different NSWindow"));
471 // Mouse events happen at the NSWindow level so we need to convert
472 // into our bounds coordinates then convert to wx coordinates.
473 NSPoint cocoaPoint = [m_cocoaNSView convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
474 NSPoint pointWx = CocoaTransformBoundsToWx(cocoaPoint);
475 // FIXME: Should we be adjusting for client area origin?
476 const wxPoint &clientorigin = GetClientAreaOrigin();
477 event.m_x = (wxCoord)pointWx.x - clientorigin.x;
478 event.m_y = (wxCoord)pointWx.y - clientorigin.y;
480 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
481 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
482 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
483 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
485 // TODO: set timestamp?
486 event.SetEventObject(this);
487 event.SetId(GetId());
490 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
492 wxMouseEvent event(wxEVT_MOTION);
493 InitMouseEvent(event,theEvent);
494 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
495 return GetEventHandler()->ProcessEvent(event);
498 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
503 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
508 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
510 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
511 InitMouseEvent(event,theEvent);
512 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]);
513 return GetEventHandler()->ProcessEvent(event);
516 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
518 wxMouseEvent event(wxEVT_MOTION);
519 InitMouseEvent(event,theEvent);
520 event.m_leftDown = true;
521 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
522 return GetEventHandler()->ProcessEvent(event);
525 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
527 wxMouseEvent event(wxEVT_LEFT_UP);
528 InitMouseEvent(event,theEvent);
529 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Up @%d,%d"),event.m_x,event.m_y);
530 return GetEventHandler()->ProcessEvent(event);
533 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
535 wxMouseEvent event([theEvent clickCount]<2?wxEVT_RIGHT_DOWN:wxEVT_RIGHT_DCLICK);
536 InitMouseEvent(event,theEvent);
537 wxLogDebug(wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]);
538 return GetEventHandler()->ProcessEvent(event);
541 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
543 wxMouseEvent event(wxEVT_MOTION);
544 InitMouseEvent(event,theEvent);
545 event.m_rightDown = true;
546 wxLogDebug(wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
547 return GetEventHandler()->ProcessEvent(event);
550 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
552 wxMouseEvent event(wxEVT_RIGHT_UP);
553 InitMouseEvent(event,theEvent);
554 wxLogDebug(wxT("Mouse Up @%d,%d"),event.m_x,event.m_y);
555 return GetEventHandler()->ProcessEvent(event);
558 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
563 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
568 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
573 void wxWindowCocoa::Cocoa_FrameChanged(void)
575 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
576 wxSizeEvent event(GetSize(), m_windowId);
577 event.SetEventObject(this);
578 GetEventHandler()->ProcessEvent(event);
581 bool wxWindowCocoa::Cocoa_resetCursorRects()
583 if(!m_cursor.GetNSCursor())
586 [GetNSView() addCursorRect: [GetNSView() visibleRect] cursor: m_cursor.GetNSCursor()];
591 bool wxWindow::Close(bool force)
593 // The only reason this function exists is that it is virtual and
594 // wxTopLevelWindowCocoa will override it.
595 return wxWindowBase::Close(force);
598 void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
600 [[oldView superview] replaceSubview:oldView with:newView];
603 bool wxWindow::EnableSelfAndChildren(bool enable)
605 // If the state isn't changing, don't do anything
606 if(!wxWindowBase::Enable(enable && m_shouldBeEnabled))
608 // Set the state of the Cocoa window
609 CocoaSetEnabled(m_isEnabled);
610 // Disable all children or (if enabling) return them to their proper state
611 for(wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
612 node; node = node->GetNext())
614 node->GetData()->EnableSelfAndChildren(enable);
619 bool wxWindow::Enable(bool enable)
621 // Keep track of what the window SHOULD be doing
622 m_shouldBeEnabled = enable;
623 // If the parent is disabled for any reason, then this window will be too.
624 if(!IsTopLevel() && GetParent())
626 enable = enable && GetParent()->IsEnabled();
628 return EnableSelfAndChildren(enable);
631 bool wxWindow::Show(bool show)
633 wxAutoNSAutoreleasePool pool;
634 // If the window is marked as visible, then it shouldn't have a dummy view
635 // If the window is marked hidden, then it should have a dummy view
636 // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
637 // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),wxT("wxWindow: m_isShown does not agree with m_dummyNSView"));
638 // Return false if there isn't a window to show or hide
639 NSView *cocoaView = GetNSViewForHiding();
644 // If state isn't changing, return false
647 CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView);
648 wxASSERT(![m_cocoaHider->GetNSView() superview]);
651 wxASSERT([cocoaView superview]);
655 // If state isn't changing, return false
658 m_cocoaHider = new wxWindowCocoaHider(this);
659 // NOTE: replaceSubview:with will cause m_cocaNSView to be
660 // (auto)released which balances out addSubview
661 CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView());
662 // m_coocaNSView is now only retained by us
663 wxASSERT([m_cocoaHider->GetNSView() superview]);
664 wxASSERT(![cocoaView superview]);
670 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
672 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":".");
673 int currentX, currentY;
674 int currentW, currentH;
675 DoGetPosition(¤tX, ¤tY);
676 DoGetSize(¤tW, ¤tH);
677 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
679 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
682 AdjustForParentClientOrigin(x,y,sizeFlags);
684 wxSize size(wxDefaultSize);
686 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
688 if(sizeFlags&wxSIZE_AUTO_WIDTH)
690 size=DoGetBestSize();
696 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
698 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
701 size=DoGetBestSize();
707 DoMoveWindow(x,y,width,height);
712 void wxWindowCocoa::DoSetToolTip( wxToolTip *tip )
714 wxWindowBase::DoSetToolTip(tip);
718 m_tooltip->SetWindow((wxWindow *)this);
724 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
726 wxAutoNSAutoreleasePool pool;
727 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
729 NSView *nsview = GetNSViewForSuperview();
730 NSView *superview = [nsview superview];
732 wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent"));
734 NSRect oldFrameRect = [nsview frame];
735 NSRect newFrameRect = GetParent()->CocoaTransformWxToBounds(NSMakeRect(x,y,width,height));
736 [nsview setFrame:newFrameRect];
737 // Be sure to redraw the parent to reflect the changed position
738 [superview setNeedsDisplayInRect:oldFrameRect];
739 [superview setNeedsDisplayInRect:newFrameRect];
742 void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size)
744 NSView *nsview = GetNSViewForSuperview();
745 NSView *superview = [nsview superview];
746 wxCHECK_RET(superview,wxT("NSView does not have a superview"));
747 wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent"));
748 NSRect frameRect = [nsview frame];
750 frameRect.size.width = size.x;
752 frameRect.size.height = size.y;
753 frameRect.origin.x = pos.x;
754 frameRect.origin.y = pos.y;
755 // Tell Cocoa to change the margin between the bottom of the superview
756 // and the bottom of the control. Keeps the control pinned to the top
757 // of its superview so that its position in the wxWidgets coordinate
758 // system doesn't change.
759 if(![superview isFlipped])
760 [nsview setAutoresizingMask: NSViewMinYMargin];
761 // MUST set the mask before setFrame: which can generate a size event
762 // and cause a scroller to be added!
763 frameRect = GetParent()->CocoaTransformWxToBounds(frameRect);
764 [nsview setFrame: frameRect];
768 void wxWindow::DoGetSize(int *w, int *h) const
770 NSRect cocoaRect = [GetNSViewForSuperview() frame];
772 *w=(int)cocoaRect.size.width;
774 *h=(int)cocoaRect.size.height;
775 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
778 void wxWindow::DoGetPosition(int *x, int *y) const
780 NSView *nsview = GetNSViewForSuperview();
782 NSRect cocoaRect = [nsview frame];
783 NSRect rectWx = GetParent()->CocoaTransformBoundsToWx(cocoaRect);
785 *x=(int)rectWx.origin.x;
787 *y=(int)rectWx.origin.y;
788 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
791 WXWidget wxWindow::GetHandle() const
793 return m_cocoaNSView;
796 wxWindow* wxWindow::GetWxWindow() const
798 return (wxWindow*) this;
801 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
803 [m_cocoaNSView setNeedsDisplay:YES];
806 void wxWindow::SetFocus()
808 if([GetNSView() acceptsFirstResponder])
809 [[GetNSView() window] makeFirstResponder: GetNSView()];
812 void wxWindow::DoCaptureMouse()
815 sm_capturedWindow = this;
818 void wxWindow::DoReleaseMouse()
821 sm_capturedWindow = NULL;
824 void wxWindow::DoScreenToClient(int *x, int *y) const
829 void wxWindow::DoClientToScreen(int *x, int *y) const
834 // Get size *available for subwindows* i.e. excluding menu bar etc.
835 void wxWindow::DoGetClientSize(int *x, int *y) const
837 wxLogTrace(wxTRACE_COCOA,wxT("DoGetClientSize:"));
838 if(m_wxCocoaScrollView)
839 m_wxCocoaScrollView->DoGetClientSize(x,y);
841 wxWindowCocoa::DoGetSize(x,y);
844 void wxWindow::DoSetClientSize(int width, int height)
846 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("DoSetClientSize=(%d,%d)"),width,height);
847 if(m_wxCocoaScrollView)
848 m_wxCocoaScrollView->ClientSizeToSize(width,height);
849 CocoaSetWxWindowSize(width,height);
852 void wxWindow::CocoaSetWxWindowSize(int width, int height)
854 wxWindowCocoa::DoSetSize(wxDefaultCoord,wxDefaultCoord,width,height,wxSIZE_USE_EXISTING);
857 void wxWindow::SetLabel(const wxString& WXUNUSED(label))
862 wxString wxWindow::GetLabel() const
865 return wxEmptyString;
868 int wxWindow::GetCharHeight() const
874 int wxWindow::GetCharWidth() const
880 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
881 int *descent, int *externalLeading, const wxFont *theFont) const
886 // Coordinates relative to the window
887 void wxWindow::WarpPointer (int x_pos, int y_pos)
892 int wxWindow::GetScrollPos(int orient) const
898 // This now returns the whole range, not just the number
899 // of positions that we can scroll.
900 int wxWindow::GetScrollRange(int orient) const
906 int wxWindow::GetScrollThumb(int orient) const
912 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
917 void wxWindow::CocoaCreateNSScrollView()
919 if(!m_wxCocoaScrollView)
921 m_wxCocoaScrollView = new wxWindowCocoaScrollView(this);
925 // New function that will replace some of the above.
926 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
927 int range, bool refresh)
929 CocoaCreateNSScrollView();
933 // Does a physical scroll
934 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
939 void wxWindow::DoSetVirtualSize( int x, int y )
941 wxWindowBase::DoSetVirtualSize(x,y);
942 CocoaCreateNSScrollView();
943 [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)];
946 bool wxWindow::SetFont(const wxFont& font)
952 static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
954 // first should be ordered higher
956 return NSOrderedDescending;
957 // second should be ordered higher
959 return NSOrderedAscending;
960 return NSOrderedSame;
963 // Raise the window to the top of the Z order
964 void wxWindow::Raise()
966 // wxAutoNSAutoreleasePool pool;
967 NSView *nsview = GetNSViewForSuperview();
968 [[nsview superview] sortSubviewsUsingFunction:
969 CocoaRaiseWindowCompareFunction
973 static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
975 // first should be ordered lower
977 return NSOrderedAscending;
978 // second should be ordered lower
980 return NSOrderedDescending;
981 return NSOrderedSame;
984 // Lower the window to the bottom of the Z order
985 void wxWindow::Lower()
987 NSView *nsview = GetNSViewForSuperview();
988 [[nsview superview] sortSubviewsUsingFunction:
989 CocoaLowerWindowCompareFunction
993 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
998 // Get the window with the focus
999 wxWindow *wxWindowBase::DoFindFocus()
1001 // Basically we are somewhat emulating the responder chain here except
1002 // we are only loking for the first responder in the key window or
1003 // upon failing to find one if the main window is different we look
1004 // for the first responder in the main window.
1006 // Note that the firstResponder doesn't necessarily have to be an
1007 // NSView but wxCocoaNSView::GetFromCocoa() will simply return
1008 // NULL unless it finds its argument in its hash map.
1012 NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
1013 win = wxCocoaNSView::GetFromCocoa(static_cast<NSView*>([keyWindow firstResponder]));
1015 return win->GetWxWindow();
1017 NSWindow *mainWindow = [[NSApplication sharedApplication] keyWindow];
1018 if(mainWindow == keyWindow)
1020 win = wxCocoaNSView::GetFromCocoa(static_cast<NSView*>([mainWindow firstResponder]));
1022 return win->GetWxWindow();
1027 /* static */ wxWindow *wxWindowBase::GetCapture()
1030 return wxWindowCocoa::sm_capturedWindow;
1033 wxWindow *wxGetActiveWindow()
1039 wxPoint wxGetMousePosition()
1042 return wxDefaultPosition;
1045 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1047 pt = wxGetMousePosition();