1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/window.mm
3 // Purpose: wxWindowCocoa
4 // Author: David Elliott
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/window.h"
15 #include "wx/cocoa/autorelease.h"
17 #import <Appkit/NSView.h>
18 #import <AppKit/NSEvent.h>
19 #import <AppKit/NSScrollView.h>
20 #import <AppKit/NSColor.h>
21 #import <AppKit/NSClipView.h>
23 // ========================================================================
25 // ========================================================================
26 class wxWindowCocoaHider: protected wxCocoaNSView
28 DECLARE_NO_COPY_CLASS(wxWindowCocoaHider)
30 wxWindowCocoaHider(wxWindow *owner);
31 virtual ~wxWindowCocoaHider();
32 inline WX_NSView GetNSView() { return m_dummyNSView; }
34 wxWindowCocoa *m_owner;
35 WX_NSView m_dummyNSView;
36 virtual void Cocoa_FrameChanged(void);
41 // ========================================================================
42 // wxWindowCocoaScroller
43 // ========================================================================
44 class wxWindowCocoaScroller: protected wxCocoaNSView
46 DECLARE_NO_COPY_CLASS(wxWindowCocoaScroller)
48 wxWindowCocoaScroller(wxWindow *owner);
49 virtual ~wxWindowCocoaScroller();
50 inline WX_NSScrollView GetNSScrollView() { return m_cocoaNSScrollView; }
51 void ClientSizeToSize(int &width, int &height);
52 void DoGetClientSize(int *x, int *y) const;
56 wxWindowCocoa *m_owner;
57 WX_NSScrollView m_cocoaNSScrollView;
58 virtual void Cocoa_FrameChanged(void);
60 wxWindowCocoaScroller();
63 // ========================================================================
65 // ========================================================================
66 wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner)
70 wxASSERT(owner->GetNSViewForHiding());
71 m_dummyNSView = [[NSView alloc]
72 initWithFrame:[owner->GetNSViewForHiding() frame]];
73 AssociateNSView(m_dummyNSView);
76 wxWindowCocoaHider::~wxWindowCocoaHider()
78 DisassociateNSView(m_dummyNSView);
79 [m_dummyNSView release];
82 void wxWindowCocoaHider::Cocoa_FrameChanged(void)
84 // Keep the real window in synch with the dummy
85 wxASSERT(m_dummyNSView);
86 [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]];
89 // ========================================================================
90 // wxFlippedNSClipView
91 // ========================================================================
92 @interface wxFlippedNSClipView : NSClipView
96 @implementation wxFlippedNSClipView : NSClipView
104 // ========================================================================
105 // wxWindowCocoaScroller
106 // ========================================================================
107 wxWindowCocoaScroller::wxWindowCocoaScroller(wxWindow *owner)
111 wxASSERT(owner->GetNSView());
112 m_cocoaNSScrollView = [[NSScrollView alloc]
113 initWithFrame:[owner->GetNSView() frame]];
114 AssociateNSView(m_cocoaNSScrollView);
116 /* Replace the default NSClipView with a flipped one. This ensures
117 scrolling is "pinned" to the top-left instead of bottom-right. */
118 NSClipView *flippedClip = [[wxFlippedNSClipView alloc]
119 initWithFrame: [[m_cocoaNSScrollView contentView] frame]];
120 [m_cocoaNSScrollView setContentView:flippedClip];
121 [flippedClip release];
123 [m_cocoaNSScrollView setBackgroundColor: [NSColor windowBackgroundColor]];
124 [m_cocoaNSScrollView setHasHorizontalScroller: YES];
125 [m_cocoaNSScrollView setHasVerticalScroller: YES];
129 void wxWindowCocoaScroller::Encapsulate()
131 // NOTE: replaceSubView will cause m_cocaNSView to be released
132 // except when it hasn't been added into an NSView hierarchy in which
133 // case it doesn't need to be and this should work out to a no-op
134 m_owner->CocoaReplaceView(m_owner->GetNSView(), m_cocoaNSScrollView);
135 // The NSView is still retained by owner
136 [m_cocoaNSScrollView setDocumentView: m_owner->GetNSView()];
137 // Now it's also retained by the NSScrollView
140 void wxWindowCocoaScroller::Unencapsulate()
142 [m_cocoaNSScrollView setDocumentView: nil];
143 m_owner->CocoaReplaceView(m_cocoaNSScrollView, m_owner->GetNSView());
146 wxWindowCocoaScroller::~wxWindowCocoaScroller()
148 DisassociateNSView(m_cocoaNSScrollView);
149 [m_cocoaNSScrollView release];
152 void wxWindowCocoaScroller::ClientSizeToSize(int &width, int &height)
154 NSSize frameSize = [NSScrollView
155 frameSizeForContentSize: NSMakeSize(width,height)
156 hasHorizontalScroller: [m_cocoaNSScrollView hasHorizontalScroller]
157 hasVerticalScroller: [m_cocoaNSScrollView hasVerticalScroller]
158 borderType: [m_cocoaNSScrollView borderType]];
159 width = frameSize.width;
160 height = frameSize.height;
163 void wxWindowCocoaScroller::DoGetClientSize(int *x, int *y) const
165 NSSize nssize = [m_cocoaNSScrollView contentSize];
172 void wxWindowCocoaScroller::Cocoa_FrameChanged(void)
174 wxLogDebug("Cocoa_FrameChanged");
175 wxSizeEvent event(m_owner->GetSize(), m_owner->GetId());
176 event.SetEventObject(m_owner);
177 m_owner->GetEventHandler()->ProcessEvent(event);
180 // ========================================================================
182 // ========================================================================
183 // normally the base classes aren't included, but wxWindow is special
184 #ifdef __WXUNIVERSAL__
185 IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
187 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
190 BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
193 wxWindow *wxWindowCocoa::sm_capturedWindow = NULL;
196 void wxWindowCocoa::Init()
200 m_cocoaNSView = NULL;
202 m_cocoaScroller = NULL;
203 m_isBeingDeleted = FALSE;
205 m_shouldBeEnabled = true;
209 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
213 const wxString& name)
215 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
218 // TODO: create the window
219 m_cocoaNSView = NULL;
220 SetNSView([[NSView alloc] initWithFrame: MakeDefaultNSRect(size)]);
221 [m_cocoaNSView release];
225 m_parent->AddChild(this);
226 m_parent->CocoaAddChild(this);
228 SetInitialFrameRect(pos,size);
234 wxWindow::~wxWindow()
236 wxAutoNSAutoreleasePool pool;
240 m_parent->RemoveChild(this);
242 CocoaRemoveFromParent();
244 delete m_cocoaScroller;
248 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
250 NSView *childView = child->GetNSViewForSuperview();
253 [m_cocoaNSView addSubview: childView];
254 child->m_isShown = !m_cocoaHider;
257 void wxWindowCocoa::CocoaRemoveFromParent(void)
259 [GetNSViewForSuperview() removeFromSuperview];
262 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
264 bool need_debug = cocoaNSView || m_cocoaNSView;
265 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d",this,m_cocoaNSView,[m_cocoaNSView retainCount]);
266 DisassociateNSView(m_cocoaNSView);
267 [cocoaNSView retain];
268 [m_cocoaNSView release];
269 m_cocoaNSView = cocoaNSView;
270 AssociateNSView(m_cocoaNSView);
271 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d",this,cocoaNSView,[cocoaNSView retainCount]);
274 WX_NSView wxWindowCocoa::GetNSViewForSuperview() const
277 ? m_cocoaHider->GetNSView()
279 ? m_cocoaScroller->GetNSScrollView()
283 WX_NSView wxWindowCocoa::GetNSViewForHiding() const
285 return m_cocoaScroller
286 ? m_cocoaScroller->GetNSScrollView()
290 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
292 wxLogDebug("Cocoa_drawRect");
293 // Recursion can happen if the event loop runs from within the paint
294 // handler. For instance, if an assertion dialog is shown.
295 // FIXME: This seems less than ideal.
298 wxLogDebug("Paint event recursion!");
301 //FIXME: should probably turn that rect into the update region
303 wxPaintEvent event(m_windowId);
304 event.SetEventObject(this);
305 bool ret = GetEventHandler()->ProcessEvent(event);
310 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
312 wxASSERT_MSG([m_cocoaNSView window]==[cocoaEvent window],"Mouse event for different NSWindow");
313 NSPoint cocoaPoint = [m_cocoaNSView convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
314 NSRect cocoaRect = [m_cocoaNSView frame];
315 const wxPoint &clientorigin = GetClientAreaOrigin();
316 event.m_x = (wxCoord)cocoaPoint.x - clientorigin.x;
317 event.m_y = (wxCoord)(cocoaRect.size.height - cocoaPoint.y) - clientorigin.y;
319 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
320 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
321 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
322 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
324 // TODO: set timestamp?
325 event.SetEventObject(this);
326 event.SetId(GetId());
329 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
331 wxMouseEvent event(wxEVT_MOTION);
332 InitMouseEvent(event,theEvent);
333 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
334 return GetEventHandler()->ProcessEvent(event);
337 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
342 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
347 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
349 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
350 InitMouseEvent(event,theEvent);
351 wxLogDebug("Mouse Down @%d,%d num clicks=%d",event.m_x,event.m_y,[theEvent clickCount]);
352 return GetEventHandler()->ProcessEvent(event);
355 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
357 wxMouseEvent event(wxEVT_MOTION);
358 InitMouseEvent(event,theEvent);
359 event.m_leftDown = true;
360 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
361 return GetEventHandler()->ProcessEvent(event);
364 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
366 wxMouseEvent event(wxEVT_LEFT_UP);
367 InitMouseEvent(event,theEvent);
368 wxLogDebug("Mouse Up @%d,%d",event.m_x,event.m_y);
369 return GetEventHandler()->ProcessEvent(event);
372 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
377 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
382 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
387 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
392 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
397 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
402 void wxWindowCocoa::Cocoa_FrameChanged(void)
404 wxLogDebug("Cocoa_FrameChanged");
405 wxSizeEvent event(GetSize(), m_windowId);
406 event.SetEventObject(this);
407 GetEventHandler()->ProcessEvent(event);
410 bool wxWindow::Close(bool force)
412 // The only reason this function exists is that it is virtual and
413 // wxTopLevelWindowCocoa will override it.
414 return wxWindowBase::Close(force);
417 void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
419 [[oldView superview] replaceSubview:oldView with:newView];
422 bool wxWindow::EnableSelfAndChildren(bool enable)
424 // If the state isn't changing, don't do anything
425 if(!wxWindowBase::Enable(enable && m_shouldBeEnabled))
427 // Set the state of the Cocoa window
428 CocoaSetEnabled(m_isEnabled);
429 // Disable all children or (if enabling) return them to their proper state
430 for(wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
431 node; node = node->GetNext())
433 node->GetData()->EnableSelfAndChildren(enable);
438 bool wxWindow::Enable(bool enable)
440 // Keep track of what the window SHOULD be doing
441 m_shouldBeEnabled = enable;
442 // If the parent is disabled for any reason, then this window will be too.
443 if(!IsTopLevel() && GetParent())
445 enable = enable && GetParent()->IsEnabled();
447 return EnableSelfAndChildren(enable);
450 bool wxWindow::Show(bool show)
452 wxAutoNSAutoreleasePool pool;
453 // If the window is marked as visible, then it shouldn't have a dummy view
454 // If the window is marked hidden, then it should have a dummy view
455 // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
456 // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),"wxWindow: m_isShown does not agree with m_dummyNSView");
457 // Return false if there isn't a window to show or hide
458 NSView *cocoaView = GetNSViewForHiding();
463 // If state isn't changing, return false
466 CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView);
467 wxASSERT(![m_cocoaHider->GetNSView() superview]);
470 wxASSERT([cocoaView superview]);
474 // If state isn't changing, return false
477 m_cocoaHider = new wxWindowCocoaHider(this);
478 // NOTE: replaceSubview:with will cause m_cocaNSView to be
479 // (auto)released which balances out addSubview
480 CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView());
481 // m_coocaNSView is now only retained by us
482 wxASSERT([m_cocoaHider->GetNSView() superview]);
483 wxASSERT(![cocoaView superview]);
489 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
491 // wxLogDebug("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":".");
492 int currentX, currentY;
493 int currentW, currentH;
494 DoGetPosition(¤tX, ¤tY);
495 DoGetSize(¤tW, ¤tH);
496 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
498 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
501 AdjustForParentClientOrigin(x,y,sizeFlags);
505 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
507 if(sizeFlags&wxSIZE_AUTO_WIDTH)
509 size=DoGetBestSize();
515 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
517 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
520 size=DoGetBestSize();
526 DoMoveWindow(x,y,width,height);
529 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
531 // wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
533 NSView *nsview = GetNSViewForSuperview();
534 NSView *superview = [nsview superview];
535 wxCHECK_RET(superview,"NSView does not have a superview");
536 NSRect parentRect = [superview bounds];
538 NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
539 [nsview setFrame: cocoaRect];
540 // Be sure to redraw the parent to reflect the changed position
541 [superview setNeedsDisplay:YES];
544 void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size)
546 NSView *nsview = GetNSViewForSuperview();
547 NSView *superview = [nsview superview];
548 wxCHECK_RET(superview,"NSView does not have a superview");
549 NSRect parentRect = [superview bounds];
550 NSRect frameRect = [nsview frame];
552 frameRect.size.width = size.x;
554 frameRect.size.height = size.y;
555 frameRect.origin.x = pos.x;
556 frameRect.origin.y = parentRect.size.height-(pos.y+frameRect.size.height);
557 [nsview setFrame: frameRect];
558 // Tell Cocoa to change the margin between the bottom of the superview
559 // and the bottom of the control. Keeps the control pinned to the top
560 // of its superview so that its position in the wxWindows coordinate
561 // system doesn't change.
562 if(![superview isFlipped])
563 [nsview setAutoresizingMask: NSViewMinYMargin];
567 void wxWindow::DoGetSize(int *w, int *h) const
569 NSRect cocoaRect = [GetNSViewForSuperview() frame];
571 *w=(int)cocoaRect.size.width;
573 *h=(int)cocoaRect.size.height;
574 // wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
577 void wxWindow::DoGetPosition(int *x, int *y) const
579 NSView *nsview = GetNSViewForSuperview();
580 NSView *superview = [nsview superview];
581 wxCHECK_RET(superview,"NSView does not have a superview");
582 NSRect parentRect = [superview bounds];
584 NSRect cocoaRect = [nsview frame];
586 *x=(int)cocoaRect.origin.x;
588 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
589 // wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
592 WXWidget wxWindow::GetHandle() const
594 return m_cocoaNSView;
597 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
599 [m_cocoaNSView setNeedsDisplay:YES];
602 void wxWindow::SetFocus()
607 void wxWindow::DoCaptureMouse()
610 sm_capturedWindow = this;
613 void wxWindow::DoReleaseMouse()
616 sm_capturedWindow = NULL;
619 void wxWindow::DoScreenToClient(int *x, int *y) const
624 void wxWindow::DoClientToScreen(int *x, int *y) const
629 // Get size *available for subwindows* i.e. excluding menu bar etc.
630 void wxWindow::DoGetClientSize(int *x, int *y) const
632 wxLogDebug("DoGetClientSize:");
634 m_cocoaScroller->DoGetClientSize(x,y);
636 wxWindowCocoa::DoGetSize(x,y);
639 void wxWindow::DoSetClientSize(int width, int height)
641 wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
643 m_cocoaScroller->ClientSizeToSize(width,height);
644 CocoaSetWxWindowSize(width,height);
647 void wxWindow::CocoaSetWxWindowSize(int width, int height)
649 wxWindowCocoa::DoSetSize(-1,-1,width,height,wxSIZE_USE_EXISTING);
652 int wxWindow::GetCharHeight() const
658 int wxWindow::GetCharWidth() const
664 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
665 int *descent, int *externalLeading, const wxFont *theFont) const
670 // Coordinates relative to the window
671 void wxWindow::WarpPointer (int x_pos, int y_pos)
676 int wxWindow::GetScrollPos(int orient) const
682 // This now returns the whole range, not just the number
683 // of positions that we can scroll.
684 int wxWindow::GetScrollRange(int orient) const
690 int wxWindow::GetScrollThumb(int orient) const
696 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
701 void wxWindow::CocoaCreateNSScrollView()
705 m_cocoaScroller = new wxWindowCocoaScroller(this);
709 // New function that will replace some of the above.
710 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
711 int range, bool refresh)
713 CocoaCreateNSScrollView();
717 // Does a physical scroll
718 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
723 void wxWindow::DoSetVirtualSize( int x, int y )
725 wxWindowBase::DoSetVirtualSize(x,y);
726 CocoaCreateNSScrollView();
727 [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)];
730 bool wxWindow::SetFont(const wxFont& font)
736 static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
738 // first should be ordered higher
740 return NSOrderedDescending;
741 // second should be ordered higher
743 return NSOrderedAscending;
744 return NSOrderedSame;
747 // Raise the window to the top of the Z order
748 void wxWindow::Raise()
750 // wxAutoNSAutoreleasePool pool;
751 NSView *nsview = GetNSViewForSuperview();
752 [[nsview superview] sortSubviewsUsingFunction:
753 CocoaRaiseWindowCompareFunction
757 static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
759 // first should be ordered lower
761 return NSOrderedAscending;
762 // second should be ordered lower
764 return NSOrderedDescending;
765 return NSOrderedSame;
768 // Lower the window to the bottom of the Z order
769 void wxWindow::Lower()
771 NSView *nsview = GetNSViewForSuperview();
772 [[nsview superview] sortSubviewsUsingFunction:
773 CocoaLowerWindowCompareFunction
777 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
782 // Get the window with the focus
783 wxWindow *wxWindowBase::FindFocus()
789 /* static */ wxWindow *wxWindowBase::GetCapture()
792 return wxWindowCocoa::sm_capturedWindow;
795 wxWindow *wxGetActiveWindow()
801 wxPoint wxGetMousePosition()
804 return wxDefaultPosition;
807 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
809 pt = wxGetMousePosition();