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 #import <AppKit/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;
149 @implementation wxDummyNSView : NSView
150 - (NSView *)hitTest:(NSPoint)aPoint
157 // ========================================================================
158 // wxWindowCocoaHider
159 // ========================================================================
160 wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner)
164 wxASSERT(owner->GetNSViewForHiding());
165 m_dummyNSView = [[wxDummyNSView alloc]
166 initWithFrame:[owner->GetNSViewForHiding() frame]];
167 [m_dummyNSView setAutoresizingMask: [owner->GetNSViewForHiding() autoresizingMask]];
168 AssociateNSView(m_dummyNSView);
171 wxWindowCocoaHider::~wxWindowCocoaHider()
173 DisassociateNSView(m_dummyNSView);
174 [m_dummyNSView release];
177 void wxWindowCocoaHider::Cocoa_FrameChanged(void)
179 // Keep the real window in synch with the dummy
180 wxASSERT(m_dummyNSView);
181 [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]];
185 #ifdef WXCOCOA_FILL_DUMMY_VIEW
186 bool wxWindowCocoaHider::Cocoa_drawRect(const NSRect& rect)
188 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:rect];
189 [[NSColor greenColor] set];
194 #endif //def WXCOCOA_FILL_DUMMY_VIEW
196 // ========================================================================
197 // wxFlippedNSClipView
198 // ========================================================================
199 @interface wxFlippedNSClipView : NSClipView
203 @implementation wxFlippedNSClipView : NSClipView
211 // ========================================================================
212 // wxWindowCocoaScrollView
213 // ========================================================================
214 wxWindowCocoaScrollView::wxWindowCocoaScrollView(wxWindow *owner)
217 wxAutoNSAutoreleasePool pool;
219 wxASSERT(owner->GetNSView());
220 m_cocoaNSScrollView = [[NSScrollView alloc]
221 initWithFrame:[owner->GetNSView() frame]];
222 AssociateNSView(m_cocoaNSScrollView);
224 /* Replace the default NSClipView with a flipped one. This ensures
225 scrolling is "pinned" to the top-left instead of bottom-right. */
226 NSClipView *flippedClip = [[wxFlippedNSClipView alloc]
227 initWithFrame: [[m_cocoaNSScrollView contentView] frame]];
228 [m_cocoaNSScrollView setContentView:flippedClip];
229 [flippedClip release];
231 [m_cocoaNSScrollView setBackgroundColor: [NSColor windowBackgroundColor]];
232 [m_cocoaNSScrollView setHasHorizontalScroller: YES];
233 [m_cocoaNSScrollView setHasVerticalScroller: YES];
237 void wxWindowCocoaScrollView::Encapsulate()
239 // Set the scroll view autoresizingMask to match the current NSView
240 [m_cocoaNSScrollView setAutoresizingMask: [m_owner->GetNSView() autoresizingMask]];
241 [m_owner->GetNSView() setAutoresizingMask: NSViewNotSizable];
242 // NOTE: replaceSubView will cause m_cocaNSView to be released
243 // except when it hasn't been added into an NSView hierarchy in which
244 // case it doesn't need to be and this should work out to a no-op
245 m_owner->CocoaReplaceView(m_owner->GetNSView(), m_cocoaNSScrollView);
246 // The NSView is still retained by owner
247 [m_cocoaNSScrollView setDocumentView: m_owner->GetNSView()];
248 // Now it's also retained by the NSScrollView
251 void wxWindowCocoaScrollView::Unencapsulate()
253 [m_cocoaNSScrollView setDocumentView: nil];
254 m_owner->CocoaReplaceView(m_cocoaNSScrollView, m_owner->GetNSView());
255 if(![[m_owner->GetNSView() superview] isFlipped])
256 [m_owner->GetNSView() setAutoresizingMask: NSViewMinYMargin];
259 wxWindowCocoaScrollView::~wxWindowCocoaScrollView()
261 DisassociateNSView(m_cocoaNSScrollView);
262 [m_cocoaNSScrollView release];
265 void wxWindowCocoaScrollView::ClientSizeToSize(int &width, int &height)
267 NSSize frameSize = [NSScrollView
268 frameSizeForContentSize: NSMakeSize(width,height)
269 hasHorizontalScroller: [m_cocoaNSScrollView hasHorizontalScroller]
270 hasVerticalScroller: [m_cocoaNSScrollView hasVerticalScroller]
271 borderType: [m_cocoaNSScrollView borderType]];
272 width = (int)frameSize.width;
273 height = (int)frameSize.height;
276 void wxWindowCocoaScrollView::DoGetClientSize(int *x, int *y) const
278 NSSize nssize = [m_cocoaNSScrollView contentSize];
280 *x = (int)nssize.width;
282 *y = (int)nssize.height;
285 void wxWindowCocoaScrollView::Cocoa_FrameChanged(void)
287 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
288 wxSizeEvent event(m_owner->GetSize(), m_owner->GetId());
289 event.SetEventObject(m_owner);
290 m_owner->GetEventHandler()->ProcessEvent(event);
293 // ========================================================================
295 // ========================================================================
296 // normally the base classes aren't included, but wxWindow is special
297 #ifdef __WXUNIVERSAL__
298 IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
300 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
303 BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
306 wxWindow *wxWindowCocoa::sm_capturedWindow = NULL;
309 void wxWindowCocoa::Init()
311 m_cocoaNSView = NULL;
313 m_wxCocoaScrollView = NULL;
314 m_isBeingDeleted = false;
316 m_shouldBeEnabled = true;
320 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
324 const wxString& name)
326 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
329 // TODO: create the window
330 m_cocoaNSView = NULL;
331 SetNSView([[NSView alloc] initWithFrame: MakeDefaultNSRect(size)]);
332 [m_cocoaNSView release];
336 m_parent->AddChild(this);
337 m_parent->CocoaAddChild(this);
338 SetInitialFrameRect(pos,size);
345 wxWindow::~wxWindow()
347 wxAutoNSAutoreleasePool pool;
350 // Make sure our parent (in the wxWidgets sense) is our superview
351 // before we go removing from it.
352 if(m_parent && m_parent->GetNSView()==[GetNSViewForSuperview() superview])
353 CocoaRemoveFromParent();
355 delete m_wxCocoaScrollView;
361 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
363 NSView *childView = child->GetNSViewForSuperview();
366 [m_cocoaNSView addSubview: childView];
367 child->m_isShown = !m_cocoaHider;
370 void wxWindowCocoa::CocoaRemoveFromParent(void)
372 [GetNSViewForSuperview() removeFromSuperview];
375 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
377 bool need_debug = cocoaNSView || m_cocoaNSView;
378 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d"),this,m_cocoaNSView,[m_cocoaNSView retainCount]);
379 DisassociateNSView(m_cocoaNSView);
380 [cocoaNSView retain];
381 [m_cocoaNSView release];
382 m_cocoaNSView = cocoaNSView;
383 AssociateNSView(m_cocoaNSView);
384 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d"),this,cocoaNSView,[cocoaNSView retainCount]);
387 WX_NSView wxWindowCocoa::GetNSViewForSuperview() const
390 ? m_cocoaHider->GetNSView()
391 : m_wxCocoaScrollView
392 ? m_wxCocoaScrollView->GetNSScrollView()
396 WX_NSView wxWindowCocoa::GetNSViewForHiding() const
398 return m_wxCocoaScrollView
399 ? m_wxCocoaScrollView->GetNSScrollView()
403 NSPoint wxWindowCocoa::CocoaTransformBoundsToWx(NSPoint pointBounds)
405 // TODO: Handle scrolling offset
406 return CocoaTransformNSViewBoundsToWx(GetNSView(), pointBounds);
409 NSRect wxWindowCocoa::CocoaTransformBoundsToWx(NSRect rectBounds)
411 // TODO: Handle scrolling offset
412 return CocoaTransformNSViewBoundsToWx(GetNSView(), rectBounds);
415 NSPoint wxWindowCocoa::CocoaTransformWxToBounds(NSPoint pointWx)
417 // TODO: Handle scrolling offset
418 return CocoaTransformNSViewWxToBounds(GetNSView(), pointWx);
421 NSRect wxWindowCocoa::CocoaTransformWxToBounds(NSRect rectWx)
423 // TODO: Handle scrolling offset
424 return CocoaTransformNSViewWxToBounds(GetNSView(), rectWx);
427 WX_NSAffineTransform wxWindowCocoa::CocoaGetWxToBoundsTransform()
429 // TODO: Handle scrolling offset
430 NSAffineTransform *transform = wxDC::CocoaGetWxToBoundsTransform([GetNSView() isFlipped], [GetNSView() bounds].size.height);
434 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
436 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_drawRect"));
437 // Recursion can happen if the event loop runs from within the paint
438 // handler. For instance, if an assertion dialog is shown.
439 // FIXME: This seems less than ideal.
442 wxLogDebug(wxT("Paint event recursion!"));
447 // Set m_updateRegion
448 const NSRect *rects = ▭ // The bounding box of the region
450 // Try replacing the larger rectangle with a list of smaller ones:
451 if ([GetNSView() respondsToSelector:@selector(getRectsBeingDrawn:count:)])
452 [GetNSView() getRectsBeingDrawn:&rects count:&countRects];
454 NSRect *transformedRects = (NSRect*)malloc(sizeof(NSRect)*countRects);
455 for(int i=0; i<countRects; i++)
457 transformedRects[i] = CocoaTransformBoundsToWx(rects[i]);
459 m_updateRegion = wxRegion(transformedRects,countRects);
460 free(transformedRects);
462 wxPaintEvent event(m_windowId);
463 event.SetEventObject(this);
464 bool ret = GetEventHandler()->ProcessEvent(event);
469 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
471 wxASSERT_MSG([m_cocoaNSView window]==[cocoaEvent window],wxT("Mouse event for different NSWindow"));
472 // Mouse events happen at the NSWindow level so we need to convert
473 // into our bounds coordinates then convert to wx coordinates.
474 NSPoint cocoaPoint = [m_cocoaNSView convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
475 NSPoint pointWx = CocoaTransformBoundsToWx(cocoaPoint);
476 // FIXME: Should we be adjusting for client area origin?
477 const wxPoint &clientorigin = GetClientAreaOrigin();
478 event.m_x = (wxCoord)pointWx.x - clientorigin.x;
479 event.m_y = (wxCoord)pointWx.y - clientorigin.y;
481 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
482 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
483 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
484 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
486 // TODO: set timestamp?
487 event.SetEventObject(this);
488 event.SetId(GetId());
491 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
493 wxMouseEvent event(wxEVT_MOTION);
494 InitMouseEvent(event,theEvent);
495 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
496 return GetEventHandler()->ProcessEvent(event);
499 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
504 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
509 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
511 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
512 InitMouseEvent(event,theEvent);
513 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]);
514 return GetEventHandler()->ProcessEvent(event);
517 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
519 wxMouseEvent event(wxEVT_MOTION);
520 InitMouseEvent(event,theEvent);
521 event.m_leftDown = true;
522 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
523 return GetEventHandler()->ProcessEvent(event);
526 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
528 wxMouseEvent event(wxEVT_LEFT_UP);
529 InitMouseEvent(event,theEvent);
530 wxLogTrace(wxTRACE_COCOA,wxT("Mouse Up @%d,%d"),event.m_x,event.m_y);
531 return GetEventHandler()->ProcessEvent(event);
534 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
536 wxMouseEvent event([theEvent clickCount]<2?wxEVT_RIGHT_DOWN:wxEVT_RIGHT_DCLICK);
537 InitMouseEvent(event,theEvent);
538 wxLogDebug(wxT("Mouse Down @%d,%d num clicks=%d"),event.m_x,event.m_y,[theEvent clickCount]);
539 return GetEventHandler()->ProcessEvent(event);
542 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
544 wxMouseEvent event(wxEVT_MOTION);
545 InitMouseEvent(event,theEvent);
546 event.m_rightDown = true;
547 wxLogDebug(wxT("Mouse Drag @%d,%d"),event.m_x,event.m_y);
548 return GetEventHandler()->ProcessEvent(event);
551 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
553 wxMouseEvent event(wxEVT_RIGHT_UP);
554 InitMouseEvent(event,theEvent);
555 wxLogDebug(wxT("Mouse Up @%d,%d"),event.m_x,event.m_y);
556 return GetEventHandler()->ProcessEvent(event);
559 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
564 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
569 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
574 void wxWindowCocoa::Cocoa_FrameChanged(void)
576 wxLogTrace(wxTRACE_COCOA,wxT("Cocoa_FrameChanged"));
577 wxSizeEvent event(GetSize(), m_windowId);
578 event.SetEventObject(this);
579 GetEventHandler()->ProcessEvent(event);
582 bool wxWindowCocoa::Cocoa_resetCursorRects()
584 if(!m_cursor.GetNSCursor())
587 [GetNSView() addCursorRect: [GetNSView() visibleRect] cursor: m_cursor.GetNSCursor()];
592 bool wxWindow::Close(bool force)
594 // The only reason this function exists is that it is virtual and
595 // wxTopLevelWindowCocoa will override it.
596 return wxWindowBase::Close(force);
599 void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
601 [[oldView superview] replaceSubview:oldView with:newView];
604 bool wxWindow::EnableSelfAndChildren(bool enable)
606 // If the state isn't changing, don't do anything
607 if(!wxWindowBase::Enable(enable && m_shouldBeEnabled))
609 // Set the state of the Cocoa window
610 CocoaSetEnabled(m_isEnabled);
611 // Disable all children or (if enabling) return them to their proper state
612 for(wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
613 node; node = node->GetNext())
615 node->GetData()->EnableSelfAndChildren(enable);
620 bool wxWindow::Enable(bool enable)
622 // Keep track of what the window SHOULD be doing
623 m_shouldBeEnabled = enable;
624 // If the parent is disabled for any reason, then this window will be too.
625 if(!IsTopLevel() && GetParent())
627 enable = enable && GetParent()->IsEnabled();
629 return EnableSelfAndChildren(enable);
632 bool wxWindow::Show(bool show)
634 wxAutoNSAutoreleasePool pool;
635 // If the window is marked as visible, then it shouldn't have a dummy view
636 // If the window is marked hidden, then it should have a dummy view
637 // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
638 // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),wxT("wxWindow: m_isShown does not agree with m_dummyNSView"));
639 // Return false if there isn't a window to show or hide
640 NSView *cocoaView = GetNSViewForHiding();
645 // If state isn't changing, return false
648 CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView);
649 wxASSERT(![m_cocoaHider->GetNSView() superview]);
652 wxASSERT([cocoaView superview]);
656 // If state isn't changing, return false
659 m_cocoaHider = new wxWindowCocoaHider(this);
660 // NOTE: replaceSubview:with will cause m_cocaNSView to be
661 // (auto)released which balances out addSubview
662 CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView());
663 // m_coocaNSView is now only retained by us
664 wxASSERT([m_cocoaHider->GetNSView() superview]);
665 wxASSERT(![cocoaView superview]);
671 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
673 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":".");
674 int currentX, currentY;
675 int currentW, currentH;
676 DoGetPosition(¤tX, ¤tY);
677 DoGetSize(¤tW, ¤tH);
678 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
680 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
683 AdjustForParentClientOrigin(x,y,sizeFlags);
685 wxSize size(wxDefaultSize);
687 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
689 if(sizeFlags&wxSIZE_AUTO_WIDTH)
691 size=DoGetBestSize();
697 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
699 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
702 size=DoGetBestSize();
708 DoMoveWindow(x,y,width,height);
713 void wxWindowCocoa::DoSetToolTip( wxToolTip *tip )
715 wxWindowBase::DoSetToolTip(tip);
719 m_tooltip->SetWindow((wxWindow *)this);
725 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
727 wxAutoNSAutoreleasePool pool;
728 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
730 NSView *nsview = GetNSViewForSuperview();
731 NSView *superview = [nsview superview];
733 wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent"));
735 NSRect oldFrameRect = [nsview frame];
736 NSRect newFrameRect = GetParent()->CocoaTransformWxToBounds(NSMakeRect(x,y,width,height));
737 [nsview setFrame:newFrameRect];
738 // Be sure to redraw the parent to reflect the changed position
739 [superview setNeedsDisplayInRect:oldFrameRect];
740 [superview setNeedsDisplayInRect:newFrameRect];
743 void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size)
745 NSView *nsview = GetNSViewForSuperview();
746 NSView *superview = [nsview superview];
747 wxCHECK_RET(superview,wxT("NSView does not have a superview"));
748 wxCHECK_RET(GetParent(), wxT("Window can only be placed correctly when it has a parent"));
749 NSRect frameRect = [nsview frame];
751 frameRect.size.width = size.x;
753 frameRect.size.height = size.y;
754 frameRect.origin.x = pos.x;
755 frameRect.origin.y = pos.y;
756 // Tell Cocoa to change the margin between the bottom of the superview
757 // and the bottom of the control. Keeps the control pinned to the top
758 // of its superview so that its position in the wxWidgets coordinate
759 // system doesn't change.
760 if(![superview isFlipped])
761 [nsview setAutoresizingMask: NSViewMinYMargin];
762 // MUST set the mask before setFrame: which can generate a size event
763 // and cause a scroller to be added!
764 frameRect = GetParent()->CocoaTransformWxToBounds(frameRect);
765 [nsview setFrame: frameRect];
769 void wxWindow::DoGetSize(int *w, int *h) const
771 NSRect cocoaRect = [GetNSViewForSuperview() frame];
773 *w=(int)cocoaRect.size.width;
775 *h=(int)cocoaRect.size.height;
776 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
779 void wxWindow::DoGetPosition(int *x, int *y) const
781 NSView *nsview = GetNSViewForSuperview();
783 NSRect cocoaRect = [nsview frame];
784 NSRect rectWx = GetParent()->CocoaTransformBoundsToWx(cocoaRect);
786 *x=(int)rectWx.origin.x;
788 *y=(int)rectWx.origin.y;
789 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
792 WXWidget wxWindow::GetHandle() const
794 return m_cocoaNSView;
797 wxWindow* wxWindow::GetWxWindow() const
799 return (wxWindow*) this;
802 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
804 [m_cocoaNSView setNeedsDisplay:YES];
807 void wxWindow::SetFocus()
809 if([GetNSView() acceptsFirstResponder])
810 [[GetNSView() window] makeFirstResponder: GetNSView()];
813 void wxWindow::DoCaptureMouse()
816 sm_capturedWindow = this;
819 void wxWindow::DoReleaseMouse()
822 sm_capturedWindow = NULL;
825 void wxWindow::DoScreenToClient(int *x, int *y) const
830 void wxWindow::DoClientToScreen(int *x, int *y) const
835 // Get size *available for subwindows* i.e. excluding menu bar etc.
836 void wxWindow::DoGetClientSize(int *x, int *y) const
838 wxLogTrace(wxTRACE_COCOA,wxT("DoGetClientSize:"));
839 if(m_wxCocoaScrollView)
840 m_wxCocoaScrollView->DoGetClientSize(x,y);
842 wxWindowCocoa::DoGetSize(x,y);
845 void wxWindow::DoSetClientSize(int width, int height)
847 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("DoSetClientSize=(%d,%d)"),width,height);
848 if(m_wxCocoaScrollView)
849 m_wxCocoaScrollView->ClientSizeToSize(width,height);
850 CocoaSetWxWindowSize(width,height);
853 void wxWindow::CocoaSetWxWindowSize(int width, int height)
855 wxWindowCocoa::DoSetSize(wxDefaultCoord,wxDefaultCoord,width,height,wxSIZE_USE_EXISTING);
858 void wxWindow::SetLabel(const wxString& WXUNUSED(label))
863 wxString wxWindow::GetLabel() const
866 return wxEmptyString;
869 int wxWindow::GetCharHeight() const
875 int wxWindow::GetCharWidth() const
881 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
882 int *descent, int *externalLeading, const wxFont *theFont) const
887 // Coordinates relative to the window
888 void wxWindow::WarpPointer (int x_pos, int y_pos)
893 int wxWindow::GetScrollPos(int orient) const
899 // This now returns the whole range, not just the number
900 // of positions that we can scroll.
901 int wxWindow::GetScrollRange(int orient) const
907 int wxWindow::GetScrollThumb(int orient) const
913 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
918 void wxWindow::CocoaCreateNSScrollView()
920 if(!m_wxCocoaScrollView)
922 m_wxCocoaScrollView = new wxWindowCocoaScrollView(this);
926 // New function that will replace some of the above.
927 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
928 int range, bool refresh)
930 CocoaCreateNSScrollView();
934 // Does a physical scroll
935 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
940 void wxWindow::DoSetVirtualSize( int x, int y )
942 wxWindowBase::DoSetVirtualSize(x,y);
943 CocoaCreateNSScrollView();
944 [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)];
947 bool wxWindow::SetFont(const wxFont& font)
953 static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
955 // first should be ordered higher
957 return NSOrderedDescending;
958 // second should be ordered higher
960 return NSOrderedAscending;
961 return NSOrderedSame;
964 // Raise the window to the top of the Z order
965 void wxWindow::Raise()
967 // wxAutoNSAutoreleasePool pool;
968 NSView *nsview = GetNSViewForSuperview();
969 [[nsview superview] sortSubviewsUsingFunction:
970 CocoaRaiseWindowCompareFunction
974 static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
976 // first should be ordered lower
978 return NSOrderedAscending;
979 // second should be ordered lower
981 return NSOrderedDescending;
982 return NSOrderedSame;
985 // Lower the window to the bottom of the Z order
986 void wxWindow::Lower()
988 NSView *nsview = GetNSViewForSuperview();
989 [[nsview superview] sortSubviewsUsingFunction:
990 CocoaLowerWindowCompareFunction
994 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
999 // Get the window with the focus
1000 wxWindow *wxWindowBase::DoFindFocus()
1002 // Basically we are somewhat emulating the responder chain here except
1003 // we are only loking for the first responder in the key window or
1004 // upon failing to find one if the main window is different we look
1005 // for the first responder in the main window.
1007 // Note that the firstResponder doesn't necessarily have to be an
1008 // NSView but wxCocoaNSView::GetFromCocoa() will simply return
1009 // NULL unless it finds its argument in its hash map.
1013 NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
1014 win = wxCocoaNSView::GetFromCocoa(static_cast<NSView*>([keyWindow firstResponder]));
1016 return win->GetWxWindow();
1018 NSWindow *mainWindow = [[NSApplication sharedApplication] keyWindow];
1019 if(mainWindow == keyWindow)
1021 win = wxCocoaNSView::GetFromCocoa(static_cast<NSView*>([mainWindow firstResponder]));
1023 return win->GetWxWindow();
1028 /* static */ wxWindow *wxWindowBase::GetCapture()
1031 return wxWindowCocoa::sm_capturedWindow;
1034 wxWindow *wxGetActiveWindow()
1040 wxPoint wxGetMousePosition()
1043 return wxDefaultPosition;
1046 wxMouseState wxGetMouseState()
1053 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1055 pt = wxGetMousePosition();