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;
208 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
212 const wxString& name)
214 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
217 // TODO: create the window
218 m_cocoaNSView = NULL;
219 SetNSView([[NSView alloc] initWithFrame: MakeDefaultNSRect(size)]);
220 [m_cocoaNSView release];
224 m_parent->AddChild(this);
225 m_parent->CocoaAddChild(this);
227 SetInitialFrameRect(pos,size);
233 wxWindow::~wxWindow()
235 wxAutoNSAutoreleasePool pool;
239 m_parent->RemoveChild(this);
241 CocoaRemoveFromParent();
243 delete m_cocoaScroller;
247 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
249 NSView *childView = child->GetNSViewForSuperview();
252 [m_cocoaNSView addSubview: childView];
253 child->m_isShown = !m_cocoaHider;
256 void wxWindowCocoa::CocoaRemoveFromParent(void)
258 [GetNSViewForSuperview() removeFromSuperview];
261 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
263 bool need_debug = cocoaNSView || m_cocoaNSView;
264 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d",this,m_cocoaNSView,[m_cocoaNSView retainCount]);
265 DisassociateNSView(m_cocoaNSView);
266 [cocoaNSView retain];
267 [m_cocoaNSView release];
268 m_cocoaNSView = cocoaNSView;
269 AssociateNSView(m_cocoaNSView);
270 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d",this,cocoaNSView,[cocoaNSView retainCount]);
273 WX_NSView wxWindowCocoa::GetNSViewForSuperview() const
276 ? m_cocoaHider->GetNSView()
278 ? m_cocoaScroller->GetNSScrollView()
282 WX_NSView wxWindowCocoa::GetNSViewForHiding() const
284 return m_cocoaScroller
285 ? m_cocoaScroller->GetNSScrollView()
289 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
291 wxLogDebug("Cocoa_drawRect");
292 // Recursion can happen if the event loop runs from within the paint
293 // handler. For instance, if an assertion dialog is shown.
294 // FIXME: This seems less than ideal.
297 wxLogDebug("Paint event recursion!");
300 //FIXME: should probably turn that rect into the update region
302 wxPaintEvent event(m_windowId);
303 event.SetEventObject(this);
304 bool ret = GetEventHandler()->ProcessEvent(event);
309 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
311 wxASSERT_MSG([m_cocoaNSView window]==[cocoaEvent window],"Mouse event for different NSWindow");
312 NSPoint cocoaPoint = [m_cocoaNSView convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
313 NSRect cocoaRect = [m_cocoaNSView frame];
314 const wxPoint &clientorigin = GetClientAreaOrigin();
315 event.m_x = (wxCoord)cocoaPoint.x - clientorigin.x;
316 event.m_y = (wxCoord)(cocoaRect.size.height - cocoaPoint.y) - clientorigin.y;
318 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
319 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
320 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
321 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
323 // TODO: set timestamp?
324 event.SetEventObject(this);
325 event.SetId(GetId());
328 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
330 wxMouseEvent event(wxEVT_MOTION);
331 InitMouseEvent(event,theEvent);
332 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
333 return GetEventHandler()->ProcessEvent(event);
336 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
341 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
346 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
348 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
349 InitMouseEvent(event,theEvent);
350 wxLogDebug("Mouse Down @%d,%d num clicks=%d",event.m_x,event.m_y,[theEvent clickCount]);
351 return GetEventHandler()->ProcessEvent(event);
354 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
356 wxMouseEvent event(wxEVT_MOTION);
357 InitMouseEvent(event,theEvent);
358 event.m_leftDown = true;
359 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
360 return GetEventHandler()->ProcessEvent(event);
363 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
365 wxMouseEvent event(wxEVT_LEFT_UP);
366 InitMouseEvent(event,theEvent);
367 wxLogDebug("Mouse Up @%d,%d",event.m_x,event.m_y);
368 return GetEventHandler()->ProcessEvent(event);
371 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
376 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
381 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
386 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
391 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
396 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
401 void wxWindowCocoa::Cocoa_FrameChanged(void)
403 wxLogDebug("Cocoa_FrameChanged");
404 wxSizeEvent event(GetSize(), m_windowId);
405 event.SetEventObject(this);
406 GetEventHandler()->ProcessEvent(event);
409 bool wxWindow::Close(bool force)
414 void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
416 [[oldView superview] replaceSubview:oldView with:newView];
419 bool wxWindow::Show(bool show)
421 wxAutoNSAutoreleasePool pool;
422 // If the window is marked as visible, then it shouldn't have a dummy view
423 // If the window is marked hidden, then it should have a dummy view
424 // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
425 // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),"wxWindow: m_isShown does not agree with m_dummyNSView");
426 // Return false if there isn't a window to show or hide
427 NSView *cocoaView = GetNSViewForHiding();
432 // If state isn't changing, return false
435 CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView);
436 wxASSERT(![m_cocoaHider->GetNSView() superview]);
439 wxASSERT([cocoaView superview]);
443 // If state isn't changing, return false
446 m_cocoaHider = new wxWindowCocoaHider(this);
447 // NOTE: replaceSubview:with will cause m_cocaNSView to be
448 // (auto)released which balances out addSubview
449 CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView());
450 // m_coocaNSView is now only retained by us
451 wxASSERT([m_cocoaHider->GetNSView() superview]);
452 wxASSERT(![cocoaView superview]);
458 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
460 // 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":".");
461 int currentX, currentY;
462 int currentW, currentH;
463 DoGetPosition(¤tX, ¤tY);
464 DoGetSize(¤tW, ¤tH);
465 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
467 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
470 AdjustForParentClientOrigin(x,y,sizeFlags);
474 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
476 if(sizeFlags&wxSIZE_AUTO_WIDTH)
478 size=DoGetBestSize();
484 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
486 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
489 size=DoGetBestSize();
495 DoMoveWindow(x,y,width,height);
498 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
500 // wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
502 NSView *nsview = GetNSViewForSuperview();
503 NSView *superview = [nsview superview];
504 wxCHECK_RET(superview,"NSView does not have a superview");
505 NSRect parentRect = [superview frame];
507 NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
508 [nsview setFrame: cocoaRect];
511 void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size)
513 NSView *nsview = GetNSViewForSuperview();
514 NSView *superview = [nsview superview];
515 wxCHECK_RET(superview,"NSView does not have a superview");
516 NSRect parentRect = [superview frame];
517 NSRect frameRect = [nsview frame];
519 frameRect.size.width = size.x;
521 frameRect.size.height = size.y;
522 frameRect.origin.x = pos.x;
523 frameRect.origin.y = parentRect.size.height-(pos.y+size.y);
524 [nsview setFrame: frameRect];
528 void wxWindow::DoGetSize(int *w, int *h) const
530 NSRect cocoaRect = [GetNSViewForSuperview() frame];
532 *w=(int)cocoaRect.size.width;
534 *h=(int)cocoaRect.size.height;
535 // wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
538 void wxWindow::DoGetPosition(int *x, int *y) const
540 NSView *nsview = GetNSViewForSuperview();
541 NSView *superview = [nsview superview];
542 wxCHECK_RET(superview,"NSView does not have a superview");
543 NSRect parentRect = [superview frame];
545 NSRect cocoaRect = [nsview frame];
547 *x=(int)cocoaRect.origin.x;
549 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
550 // wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
553 WXWidget wxWindow::GetHandle() const
555 return m_cocoaNSView;
558 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
560 [m_cocoaNSView setNeedsDisplay:YES];
563 void wxWindow::SetFocus()
568 void wxWindow::DoCaptureMouse()
571 sm_capturedWindow = this;
574 void wxWindow::DoReleaseMouse()
577 sm_capturedWindow = NULL;
580 void wxWindow::DoScreenToClient(int *x, int *y) const
585 void wxWindow::DoClientToScreen(int *x, int *y) const
590 // Get size *available for subwindows* i.e. excluding menu bar etc.
591 void wxWindow::DoGetClientSize(int *x, int *y) const
593 wxLogDebug("DoGetClientSize:");
595 m_cocoaScroller->DoGetClientSize(x,y);
597 wxWindowCocoa::DoGetSize(x,y);
600 void wxWindow::DoSetClientSize(int width, int height)
602 wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
606 int wxWindow::GetCharHeight() const
612 int wxWindow::GetCharWidth() const
618 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
619 int *descent, int *externalLeading, const wxFont *theFont) const
624 // Coordinates relative to the window
625 void wxWindow::WarpPointer (int x_pos, int y_pos)
630 int wxWindow::GetScrollPos(int orient) const
636 // This now returns the whole range, not just the number
637 // of positions that we can scroll.
638 int wxWindow::GetScrollRange(int orient) const
644 int wxWindow::GetScrollThumb(int orient) const
650 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
655 void wxWindow::CocoaCreateNSScrollView()
659 m_cocoaScroller = new wxWindowCocoaScroller(this);
663 // New function that will replace some of the above.
664 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
665 int range, bool refresh)
670 // Does a physical scroll
671 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
676 void wxWindow::DoSetVirtualSize( int x, int y )
678 wxWindowBase::DoSetVirtualSize(x,y);
679 CocoaCreateNSScrollView();
680 [m_cocoaNSView setFrameSize:NSMakeSize(m_virtualSize.x,m_virtualSize.y)];
683 bool wxWindow::SetFont(const wxFont& font)
689 static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
691 // first should be ordered higher
693 return NSOrderedDescending;
694 // second should be ordered higher
696 return NSOrderedAscending;
697 return NSOrderedSame;
700 // Raise the window to the top of the Z order
701 void wxWindow::Raise()
703 // wxAutoNSAutoreleasePool pool;
704 NSView *nsview = GetNSViewForSuperview();
705 [[nsview superview] sortSubviewsUsingFunction:
706 CocoaRaiseWindowCompareFunction
710 static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
712 // first should be ordered lower
714 return NSOrderedAscending;
715 // second should be ordered lower
717 return NSOrderedDescending;
718 return NSOrderedSame;
721 // Lower the window to the bottom of the Z order
722 void wxWindow::Lower()
724 NSView *nsview = GetNSViewForSuperview();
725 [[nsview superview] sortSubviewsUsingFunction:
726 CocoaLowerWindowCompareFunction
730 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
735 // Get the window with the focus
736 wxWindow *wxWindowBase::FindFocus()
742 /* static */ wxWindow *wxWindowBase::GetCapture()
745 return wxWindowCocoa::sm_capturedWindow;
748 wxWindow *wxGetActiveWindow()
754 wxPoint wxGetMousePosition()
757 return wxDefaultPosition;
760 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
762 pt = wxGetMousePosition();