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 NSPoint CocoaTransformNSViewBoundsToWx(NSView *nsview, NSPoint pointBounds)
46 wxCHECK_MSG(nsview, pointBounds, wxT("Need to have a Cocoa view to do translation"));
47 if([nsview isFlipped])
49 NSRect ourBounds = [nsview bounds];
52 , ourBounds.size.height - pointBounds.y
56 NSRect CocoaTransformNSViewBoundsToWx(NSView *nsview, NSRect rectBounds)
58 wxCHECK_MSG(nsview, rectBounds, wxT("Need to have a Cocoa view to do translation"));
59 if([nsview isFlipped])
61 NSRect ourBounds = [nsview bounds];
64 , ourBounds.size.height - (rectBounds.origin.y + rectBounds.size.height)
65 , rectBounds.size.width
66 , rectBounds.size.height
70 NSPoint CocoaTransformNSViewWxToBounds(NSView *nsview, NSPoint pointWx)
72 wxCHECK_MSG(nsview, pointWx, wxT("Need to have a Cocoa view to do translation"));
73 if([nsview isFlipped])
75 NSRect ourBounds = [nsview bounds];
78 , ourBounds.size.height - pointWx.y
82 NSRect CocoaTransformNSViewWxToBounds(NSView *nsview, NSRect rectWx)
84 wxCHECK_MSG(nsview, rectWx, wxT("Need to have a Cocoa view to do translation"));
85 if([nsview isFlipped])
87 NSRect ourBounds = [nsview bounds];
90 , ourBounds.size.height - (rectWx.origin.y + rectWx.size.height)
96 // ========================================================================
98 // ========================================================================
99 class wxWindowCocoaHider: protected wxCocoaNSView
101 DECLARE_NO_COPY_CLASS(wxWindowCocoaHider)
103 wxWindowCocoaHider(wxWindow *owner);
104 virtual ~wxWindowCocoaHider();
105 inline WX_NSView GetNSView() { return m_dummyNSView; }
107 wxWindowCocoa *m_owner;
108 WX_NSView m_dummyNSView;
109 virtual void Cocoa_FrameChanged(void);
110 #ifdef WXCOCOA_FILL_DUMMY_VIEW
111 virtual bool Cocoa_drawRect(const NSRect& rect);
112 #endif //def WXCOCOA_FILL_DUMMY_VIEW
114 wxWindowCocoaHider();
117 // ========================================================================
118 // wxWindowCocoaScrollView
119 // ========================================================================
120 class wxWindowCocoaScrollView: protected wxCocoaNSView
122 DECLARE_NO_COPY_CLASS(wxWindowCocoaScrollView)
124 wxWindowCocoaScrollView(wxWindow *owner);
125 virtual ~wxWindowCocoaScrollView();
126 inline WX_NSScrollView GetNSScrollView() { return m_cocoaNSScrollView; }
127 void ClientSizeToSize(int &width, int &height);
128 void DoGetClientSize(int *x, int *y) const;
130 void Unencapsulate();
132 wxWindowCocoa *m_owner;
133 WX_NSScrollView m_cocoaNSScrollView;
134 virtual void Cocoa_FrameChanged(void);
136 wxWindowCocoaScrollView();
139 // ========================================================================
141 // ========================================================================
142 @interface wxDummyNSView : NSView
143 - (NSView *)hitTest:(NSPoint)aPoint;
146 @implementation wxDummyNSView : NSView
147 - (NSView *)hitTest:(NSPoint)aPoint
154 // ========================================================================
155 // wxWindowCocoaHider
156 // ========================================================================
157 wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner)
161 wxASSERT(owner->GetNSViewForHiding());
162 m_dummyNSView = [[wxDummyNSView alloc]
163 initWithFrame:[owner->GetNSViewForHiding() frame]];
164 [m_dummyNSView setAutoresizingMask: [owner->GetNSViewForHiding() autoresizingMask]];
165 AssociateNSView(m_dummyNSView);
168 wxWindowCocoaHider::~wxWindowCocoaHider()
170 DisassociateNSView(m_dummyNSView);
171 [m_dummyNSView release];
174 void wxWindowCocoaHider::Cocoa_FrameChanged(void)
176 // Keep the real window in synch with the dummy
177 wxASSERT(m_dummyNSView);
178 [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]];
182 #ifdef WXCOCOA_FILL_DUMMY_VIEW
183 bool wxWindowCocoaHider::Cocoa_drawRect(const NSRect& rect)
185 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:rect];
186 [[NSColor greenColor] set];
191 #endif //def WXCOCOA_FILL_DUMMY_VIEW
193 // ========================================================================
194 // wxFlippedNSClipView
195 // ========================================================================
196 @interface wxFlippedNSClipView : NSClipView
200 @implementation wxFlippedNSClipView : NSClipView
208 // ========================================================================
209 // wxWindowCocoaScrollView
210 // ========================================================================
211 wxWindowCocoaScrollView::wxWindowCocoaScrollView(wxWindow *owner)
214 wxAutoNSAutoreleasePool pool;
216 wxASSERT(owner->GetNSView());
217 m_cocoaNSScrollView = [[NSScrollView alloc]
218 initWithFrame:[owner->GetNSView() frame]];
219 AssociateNSView(m_cocoaNSScrollView);
221 /* Replace the default NSClipView with a flipped one. This ensures
222 scrolling is "pinned" to the top-left instead of bottom-right. */
223 NSClipView *flippedClip = [[wxFlippedNSClipView alloc]
224 initWithFrame: [[m_cocoaNSScrollView contentView] frame]];
225 [m_cocoaNSScrollView setContentView:flippedClip];
226 [flippedClip release];
228 [m_cocoaNSScrollView setBackgroundColor: [NSColor windowBackgroundColor]];
229 [m_cocoaNSScrollView setHasHorizontalScroller: YES];
230 [m_cocoaNSScrollView setHasVerticalScroller: YES];
234 void wxWindowCocoaScrollView::Encapsulate()
236 // Set the scroll view autoresizingMask to match the current NSView
237 [m_cocoaNSScrollView setAutoresizingMask: [m_owner->GetNSView() autoresizingMask]];
238 [m_owner->GetNSView() setAutoresizingMask: NSViewNotSizable];
239 // NOTE: replaceSubView will cause m_cocaNSView to be released
240 // except when it hasn't been added into an NSView hierarchy in which
241 // case it doesn't need to be and this should work out to a no-op
242 m_owner->CocoaReplaceView(m_owner->GetNSView(), m_cocoaNSScrollView);
243 // The NSView is still retained by owner
244 [m_cocoaNSScrollView setDocumentView: m_owner->GetNSView()];
245 // Now it's also retained by the NSScrollView
248 void wxWindowCocoaScrollView::Unencapsulate()
250 [m_cocoaNSScrollView setDocumentView: nil];
251 m_owner->CocoaReplaceView(m_cocoaNSScrollView, m_owner->GetNSView());
252 if(![[m_owner->GetNSView() superview] isFlipped])
253 [m_owner->GetNSView() setAutoresizingMask: NSViewMinYMargin];
256 wxWindowCocoaScrollView::~wxWindowCocoaScrollView()
258 DisassociateNSView(m_cocoaNSScrollView);
259 [m_cocoaNSScrollView release];
262 void wxWindowCocoaScrollView::ClientSizeToSize(int &width, int &height)
264 NSSize frameSize = [NSScrollView
265 frameSizeForContentSize: NSMakeSize(width,height)
266 hasHorizontalScroller: [m_cocoaNSScrollView hasHorizontalScroller]
267 hasVerticalScroller: [m_cocoaNSScrollView hasVerticalScroller]
268 borderType: [m_cocoaNSScrollView borderType]];
269 width = (int)frameSize.width;
270 height = (int)frameSize.height;
273 void wxWindowCocoaScrollView::DoGetClientSize(int *x, int *y) const
275 NSSize nssize = [m_cocoaNSScrollView contentSize];
277 *x = (int)nssize.width;
279 *y = (int)nssize.height;
282 void wxWindowCocoaScrollView::Cocoa_FrameChanged(void)
284 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
285 wxSizeEvent event(m_owner->GetSize(), m_owner->GetId());
286 event.SetEventObject(m_owner);
287 m_owner->GetEventHandler()->ProcessEvent(event);
290 // ========================================================================
292 // ========================================================================
293 // normally the base classes aren't included, but wxWindow is special
294 #ifdef __WXUNIVERSAL__
295 IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
297 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
300 BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
303 wxWindow *wxWindowCocoa::sm_capturedWindow = NULL;
306 void wxWindowCocoa::Init()
308 m_cocoaNSView = NULL;
310 m_wxCocoaScrollView = NULL;
311 m_isBeingDeleted = FALSE;
313 m_shouldBeEnabled = true;
317 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
321 const wxString& name)
323 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
326 // TODO: create the window
327 m_cocoaNSView = NULL;
328 SetNSView([[NSView alloc] initWithFrame: MakeDefaultNSRect(size)]);
329 [m_cocoaNSView release];
333 m_parent->AddChild(this);
334 m_parent->CocoaAddChild(this);
335 SetInitialFrameRect(pos,size);
342 wxWindow::~wxWindow()
344 wxAutoNSAutoreleasePool pool;
347 // Make sure our parent (in the wxWidgets sense) is our superview
348 // before we go removing from it.
349 if(m_parent && m_parent->GetNSView()==[GetNSViewForSuperview() superview])
350 CocoaRemoveFromParent();
352 delete m_wxCocoaScrollView;
358 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
360 NSView *childView = child->GetNSViewForSuperview();
363 [m_cocoaNSView addSubview: childView];
364 child->m_isShown = !m_cocoaHider;
367 void wxWindowCocoa::CocoaRemoveFromParent(void)
369 [GetNSViewForSuperview() removeFromSuperview];
372 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
374 bool need_debug = cocoaNSView || m_cocoaNSView;
375 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d"),this,m_cocoaNSView,[m_cocoaNSView retainCount]);
376 DisassociateNSView(m_cocoaNSView);
377 [cocoaNSView retain];
378 [m_cocoaNSView release];
379 m_cocoaNSView = cocoaNSView;
380 AssociateNSView(m_cocoaNSView);
381 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d"),this,cocoaNSView,[cocoaNSView retainCount]);
384 WX_NSView wxWindowCocoa::GetNSViewForSuperview() const
387 ? m_cocoaHider->GetNSView()
388 : m_wxCocoaScrollView
389 ? m_wxCocoaScrollView->GetNSScrollView()
393 WX_NSView wxWindowCocoa::GetNSViewForHiding() const
395 return m_wxCocoaScrollView
396 ? m_wxCocoaScrollView->GetNSScrollView()
400 NSPoint wxWindowCocoa::CocoaTransformBoundsToWx(NSPoint pointBounds)
402 // TODO: Handle scrolling offset
403 return CocoaTransformNSViewBoundsToWx(GetNSView(), pointBounds);
406 NSRect wxWindowCocoa::CocoaTransformBoundsToWx(NSRect rectBounds)
408 // TODO: Handle scrolling offset
409 return CocoaTransformNSViewBoundsToWx(GetNSView(), rectBounds);
412 NSPoint wxWindowCocoa::CocoaTransformWxToBounds(NSPoint pointWx)
414 // TODO: Handle scrolling offset
415 return CocoaTransformNSViewWxToBounds(GetNSView(), pointWx);
418 NSRect wxWindowCocoa::CocoaTransformWxToBounds(NSRect rectWx)
420 // TODO: Handle scrolling offset
421 return CocoaTransformNSViewWxToBounds(GetNSView(), rectWx);
424 WX_NSAffineTransform wxWindowCocoa::CocoaGetWxToBoundsTransform()
426 // TODO: Handle scrolling offset
427 NSAffineTransform *transform = wxDC::CocoaGetWxToBoundsTransform([GetNSView() isFlipped], [GetNSView() bounds].size.height);
431 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
433 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_drawRect"));
434 // Recursion can happen if the event loop runs from within the paint
435 // handler. For instance, if an assertion dialog is shown.
436 // FIXME: This seems less than ideal.
439 wxLogDebug(wxT("Paint event recursion!"));
444 // Set m_updateRegion
445 const NSRect *rects = ▭ // The bounding box of the region
447 // Try replacing the larger rectangle with a list of smaller ones:
448 if ([GetNSView() respondsToSelector:@selector(getRectsBeingDrawn:count:)])
449 [GetNSView() getRectsBeingDrawn:&rects count:&countRects];
451 NSRect *transformedRects = (NSRect*)malloc(sizeof(NSRect)*countRects);
452 for(int i=0; i<countRects; i++)
454 transformedRects[i] = CocoaTransformBoundsToWx(rects[i]);
456 m_updateRegion = wxRegion(transformedRects,countRects);
457 free(transformedRects);
459 wxPaintEvent event(m_windowId);
460 event.SetEventObject(this);
461 bool ret = GetEventHandler()->ProcessEvent(event);
466 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
468 wxASSERT_MSG([m_cocoaNSView window]==[cocoaEvent window],wxT("Mouse event for different NSWindow"));
469 // Mouse events happen at the NSWindow level so we need to convert
470 // into our bounds coordinates then convert to wx coordinates.
471 NSPoint cocoaPoint = [m_cocoaNSView convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
472 NSPoint pointWx = CocoaTransformBoundsToWx(cocoaPoint);
473 // FIXME: Should we be adjusting for client area origin?
474 const wxPoint &clientorigin = GetClientAreaOrigin();
475 event.m_x = (wxCoord)pointWx.x - clientorigin.x;
476 event.m_y = (wxCoord)pointWx.y - clientorigin.y;
478 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
479 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
480 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
481 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
483 // TODO: set timestamp?
484 event.SetEventObject(this);
485 event.SetId(GetId());
488 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
490 wxMouseEvent event(wxEVT_MOTION);
491 InitMouseEvent(event,theEvent);
492 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
493 return GetEventHandler()->ProcessEvent(event);
496 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
501 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
506 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
508 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
509 InitMouseEvent(event,theEvent);
510 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]);
511 return GetEventHandler()->ProcessEvent(event);
514 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
516 wxMouseEvent event(wxEVT_MOTION);
517 InitMouseEvent(event,theEvent);
518 event.m_leftDown = true;
519 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
520 return GetEventHandler()->ProcessEvent(event);
523 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
525 wxMouseEvent event(wxEVT_LEFT_UP);
526 InitMouseEvent(event,theEvent);
527 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Up @%d,%d"),event.m_x,event.m_y);
528 return GetEventHandler()->ProcessEvent(event);
531 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
533 wxMouseEvent event([theEvent clickCount]<2?wxEVT_RIGHT_DOWN:wxEVT_RIGHT_DCLICK);
534 InitMouseEvent(event,theEvent);
535 wxLogDebug(wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]);
536 return GetEventHandler()->ProcessEvent(event);
539 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
541 wxMouseEvent event(wxEVT_MOTION);
542 InitMouseEvent(event,theEvent);
543 event.m_rightDown = true;
544 wxLogDebug(wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
545 return GetEventHandler()->ProcessEvent(event);
548 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
550 wxMouseEvent event(wxEVT_RIGHT_UP);
551 InitMouseEvent(event,theEvent);
552 wxLogDebug(wxT("Mouse Up @%d,%d"),event.m_x,event.m_y);
553 return GetEventHandler()->ProcessEvent(event);
556 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
561 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
566 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
571 void wxWindowCocoa::Cocoa_FrameChanged(void)
573 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
574 wxSizeEvent event(GetSize(), m_windowId);
575 event.SetEventObject(this);
576 GetEventHandler()->ProcessEvent(event);
579 bool wxWindowCocoa::Cocoa_resetCursorRects()
581 if(!m_cursor.GetNSCursor())
584 [GetNSView() addCursorRect: [GetNSView() visibleRect] cursor: m_cursor.GetNSCursor()];
589 bool wxWindow::Close(bool force)
591 // The only reason this function exists is that it is virtual and
592 // wxTopLevelWindowCocoa will override it.
593 return wxWindowBase::Close(force);
596 void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
598 [[oldView superview] replaceSubview:oldView with:newView];
601 bool wxWindow::EnableSelfAndChildren(bool enable)
603 // If the state isn't changing, don't do anything
604 if(!wxWindowBase::Enable(enable && m_shouldBeEnabled))
606 // Set the state of the Cocoa window
607 CocoaSetEnabled(m_isEnabled);
608 // Disable all children or (if enabling) return them to their proper state
609 for(wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
610 node; node = node->GetNext())
612 node->GetData()->EnableSelfAndChildren(enable);
617 bool wxWindow::Enable(bool enable)
619 // Keep track of what the window SHOULD be doing
620 m_shouldBeEnabled = enable;
621 // If the parent is disabled for any reason, then this window will be too.
622 if(!IsTopLevel() && GetParent())
624 enable = enable && GetParent()->IsEnabled();
626 return EnableSelfAndChildren(enable);
629 bool wxWindow::Show(bool show)
631 wxAutoNSAutoreleasePool pool;
632 // If the window is marked as visible, then it shouldn't have a dummy view
633 // If the window is marked hidden, then it should have a dummy view
634 // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
635 // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),wxT("wxWindow: m_isShown does not agree with m_dummyNSView"));
636 // Return false if there isn't a window to show or hide
637 NSView *cocoaView = GetNSViewForHiding();
642 // If state isn't changing, return false
645 CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView);
646 wxASSERT(![m_cocoaHider->GetNSView() superview]);
649 wxASSERT([cocoaView superview]);
653 // If state isn't changing, return false
656 m_cocoaHider = new wxWindowCocoaHider(this);
657 // NOTE: replaceSubview:with will cause m_cocaNSView to be
658 // (auto)released which balances out addSubview
659 CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView());
660 // m_coocaNSView is now only retained by us
661 wxASSERT([m_cocoaHider->GetNSView() superview]);
662 wxASSERT(![cocoaView superview]);
668 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
670 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":".");
671 int currentX, currentY;
672 int currentW, currentH;
673 DoGetPosition(¤tX, ¤tY);
674 DoGetSize(¤tW, ¤tH);
675 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
677 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
680 AdjustForParentClientOrigin(x,y,sizeFlags);
684 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
686 if(sizeFlags&wxSIZE_AUTO_WIDTH)
688 size=DoGetBestSize();
694 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
696 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
699 size=DoGetBestSize();
705 DoMoveWindow(x,y,width,height);
710 void wxWindowCocoa::DoSetToolTip( wxToolTip *tip )
712 wxWindowBase::DoSetToolTip(tip);
716 m_tooltip->SetWindow((wxWindow *)this);
722 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
724 wxAutoNSAutoreleasePool pool;
725 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
727 NSView *nsview = GetNSViewForSuperview();
728 NSView *superview = [nsview superview];
730 wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent"));
732 NSRect oldFrameRect = [nsview frame];
733 NSRect newFrameRect = GetParent()->CocoaTransformWxToBounds(NSMakeRect(x,y,width,height));
734 [nsview setFrame:newFrameRect];
735 // Be sure to redraw the parent to reflect the changed position
736 [superview setNeedsDisplayInRect:oldFrameRect];
737 [superview setNeedsDisplayInRect:newFrameRect];
740 void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size)
742 NSView *nsview = GetNSViewForSuperview();
743 NSView *superview = [nsview superview];
744 wxCHECK_RET(superview,wxT("NSView does not have a superview"));
745 wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent"));
746 NSRect frameRect = [nsview frame];
748 frameRect.size.width = size.x;
750 frameRect.size.height = size.y;
751 frameRect.origin.x = pos.x;
752 frameRect.origin.y = pos.y;
753 // Tell Cocoa to change the margin between the bottom of the superview
754 // and the bottom of the control. Keeps the control pinned to the top
755 // of its superview so that its position in the wxWidgets coordinate
756 // system doesn't change.
757 if(![superview isFlipped])
758 [nsview setAutoresizingMask: NSViewMinYMargin];
759 // MUST set the mask before setFrame: which can generate a size event
760 // and cause a scroller to be added!
761 frameRect = GetParent()->CocoaTransformWxToBounds(frameRect);
762 [nsview setFrame: frameRect];
766 void wxWindow::DoGetSize(int *w, int *h) const
768 NSRect cocoaRect = [GetNSViewForSuperview() frame];
770 *w=(int)cocoaRect.size.width;
772 *h=(int)cocoaRect.size.height;
773 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
776 void wxWindow::DoGetPosition(int *x, int *y) const
778 NSView *nsview = GetNSViewForSuperview();
780 NSRect cocoaRect = [nsview frame];
781 NSRect rectWx = GetParent()->CocoaTransformBoundsToWx(cocoaRect);
783 *x=(int)rectWx.origin.x;
785 *y=(int)rectWx.origin.y;
786 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
789 WXWidget wxWindow::GetHandle() const
791 return m_cocoaNSView;
794 wxWindow* wxWindow::GetWxWindow() const
796 return (wxWindow*) this;
799 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
801 [m_cocoaNSView setNeedsDisplay:YES];
804 void wxWindow::SetFocus()
806 if([GetNSView() acceptsFirstResponder])
807 [[GetNSView() window] makeFirstResponder: GetNSView()];
810 void wxWindow::DoCaptureMouse()
813 sm_capturedWindow = this;
816 void wxWindow::DoReleaseMouse()
819 sm_capturedWindow = NULL;
822 void wxWindow::DoScreenToClient(int *x, int *y) const
827 void wxWindow::DoClientToScreen(int *x, int *y) const
832 // Get size *available for subwindows* i.e. excluding menu bar etc.
833 void wxWindow::DoGetClientSize(int *x, int *y) const
835 wxLogTrace(wxTRACE_COCOA,wxT("DoGetClientSize:"));
836 if(m_wxCocoaScrollView)
837 m_wxCocoaScrollView->DoGetClientSize(x,y);
839 wxWindowCocoa::DoGetSize(x,y);
842 void wxWindow::DoSetClientSize(int width, int height)
844 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("DoSetClientSize=(%d,%d)"),width,height);
845 if(m_wxCocoaScrollView)
846 m_wxCocoaScrollView->ClientSizeToSize(width,height);
847 CocoaSetWxWindowSize(width,height);
850 void wxWindow::CocoaSetWxWindowSize(int width, int height)
852 wxWindowCocoa::DoSetSize(-1,-1,width,height,wxSIZE_USE_EXISTING);
855 int wxWindow::GetCharHeight() const
861 int wxWindow::GetCharWidth() const
867 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
868 int *descent, int *externalLeading, const wxFont *theFont) const
873 // Coordinates relative to the window
874 void wxWindow::WarpPointer (int x_pos, int y_pos)
879 int wxWindow::GetScrollPos(int orient) const
885 // This now returns the whole range, not just the number
886 // of positions that we can scroll.
887 int wxWindow::GetScrollRange(int orient) const
893 int wxWindow::GetScrollThumb(int orient) const
899 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
904 void wxWindow::CocoaCreateNSScrollView()
906 if(!m_wxCocoaScrollView)
908 m_wxCocoaScrollView = new wxWindowCocoaScrollView(this);
912 // New function that will replace some of the above.
913 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
914 int range, bool refresh)
916 CocoaCreateNSScrollView();
920 // Does a physical scroll
921 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
926 void wxWindow::DoSetVirtualSize( int x, int y )
928 wxWindowBase::DoSetVirtualSize(x,y);
929 CocoaCreateNSScrollView();
930 [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)];
933 bool wxWindow::SetFont(const wxFont& font)
939 static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
941 // first should be ordered higher
943 return NSOrderedDescending;
944 // second should be ordered higher
946 return NSOrderedAscending;
947 return NSOrderedSame;
950 // Raise the window to the top of the Z order
951 void wxWindow::Raise()
953 // wxAutoNSAutoreleasePool pool;
954 NSView *nsview = GetNSViewForSuperview();
955 [[nsview superview] sortSubviewsUsingFunction:
956 CocoaRaiseWindowCompareFunction
960 static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
962 // first should be ordered lower
964 return NSOrderedAscending;
965 // second should be ordered lower
967 return NSOrderedDescending;
968 return NSOrderedSame;
971 // Lower the window to the bottom of the Z order
972 void wxWindow::Lower()
974 NSView *nsview = GetNSViewForSuperview();
975 [[nsview superview] sortSubviewsUsingFunction:
976 CocoaLowerWindowCompareFunction
980 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
985 // Get the window with the focus
986 wxWindow *wxWindowBase::DoFindFocus()
988 // Basically we are somewhat emulating the responder chain here except
989 // we are only loking for the first responder in the key window or
990 // upon failing to find one if the main window is different we look
991 // for the first responder in the main window.
993 // Note that the firstResponder doesn't necessarily have to be an
994 // NSView but wxCocoaNSView::GetFromCocoa() will simply return
995 // NULL unless it finds its argument in its hash map.
999 NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
1000 win = wxCocoaNSView::GetFromCocoa([keyWindow firstResponder]);
1002 return win->GetWxWindow();
1004 NSWindow *mainWindow = [[NSApplication sharedApplication] keyWindow];
1005 if(mainWindow == keyWindow)
1007 win = wxCocoaNSView::GetFromCocoa([mainWindow firstResponder]);
1009 return win->GetWxWindow();
1014 /* static */ wxWindow *wxWindowBase::GetCapture()
1017 return wxWindowCocoa::sm_capturedWindow;
1020 wxWindow *wxGetActiveWindow()
1026 wxPoint wxGetMousePosition()
1029 return wxDefaultPosition;
1032 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1034 pt = wxGetMousePosition();