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>
20 // ========================================================================
22 // ========================================================================
23 class wxWindowCocoaHider: protected wxCocoaNSView
25 DECLARE_NO_COPY_CLASS(wxWindowCocoaHider)
27 wxWindowCocoaHider(wxWindow *owner);
28 virtual ~wxWindowCocoaHider();
29 inline WX_NSView GetNSView() { return m_dummyNSView; }
31 wxWindowCocoa *m_owner;
32 WX_NSView m_dummyNSView;
33 virtual void Cocoa_FrameChanged(void);
38 // ========================================================================
40 // ========================================================================
41 wxWindowCocoaHider::wxWindowCocoaHider(wxWindow *owner)
45 wxASSERT(owner->GetNSViewForHiding());
46 m_dummyNSView = [[NSView alloc]
47 initWithFrame:[owner->GetNSViewForHiding() frame]];
48 AssociateNSView(m_dummyNSView);
51 wxWindowCocoaHider::~wxWindowCocoaHider()
53 DisassociateNSView(m_dummyNSView);
54 [m_dummyNSView release];
57 void wxWindowCocoaHider::Cocoa_FrameChanged(void)
59 // Keep the real window in synch with the dummy
60 wxASSERT(m_dummyNSView);
61 [m_owner->GetNSViewForHiding() setFrame:[m_dummyNSView frame]];
64 // ========================================================================
66 // ========================================================================
67 // normally the base classes aren't included, but wxWindow is special
68 #ifdef __WXUNIVERSAL__
69 IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
71 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
74 BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
77 wxWindow *wxWindowCocoa::sm_capturedWindow = NULL;
80 void wxWindowCocoa::Init()
86 m_isBeingDeleted = FALSE;
91 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
97 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
100 // TODO: create the window
101 NSRect cocoaRect = NSMakeRect(10,10,20,20);
102 m_cocoaNSView = NULL;
103 SetNSView([[NSView alloc] initWithFrame: cocoaRect]);
104 [m_cocoaNSView release];
108 m_parent->AddChild(this);
109 m_parent->CocoaAddChild(this);
116 wxWindow::~wxWindow()
118 wxAutoNSAutoreleasePool pool;
122 m_parent->RemoveChild(this);
124 CocoaRemoveFromParent();
129 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
131 NSView *childView = child->GetNSViewForSuperview();
134 [m_cocoaNSView addSubview: childView];
135 child->m_isShown = !m_cocoaHider;
138 void wxWindowCocoa::CocoaRemoveFromParent(void)
140 [GetNSViewForSuperview() removeFromSuperview];
143 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
145 bool need_debug = cocoaNSView || m_cocoaNSView;
146 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d",this,m_cocoaNSView,[m_cocoaNSView retainCount]);
147 DisassociateNSView(m_cocoaNSView);
148 [cocoaNSView retain];
149 [m_cocoaNSView release];
150 m_cocoaNSView = cocoaNSView;
151 AssociateNSView(m_cocoaNSView);
152 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d",this,cocoaNSView,[cocoaNSView retainCount]);
155 WX_NSView wxWindowCocoa::GetNSViewForSuperview() const
158 ? m_cocoaHider->GetNSView()
162 WX_NSView wxWindowCocoa::GetNSViewForHiding() const
164 return m_cocoaNSView;
167 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
169 wxLogDebug("Cocoa_drawRect");
170 // Recursion can happen if the event loop runs from within the paint
171 // handler. For instance, if an assertion dialog is shown.
172 // FIXME: This seems less than ideal.
175 wxLogDebug("Paint event recursion!");
178 //FIXME: should probably turn that rect into the update region
180 wxPaintEvent event(m_windowId);
181 event.SetEventObject(this);
182 bool ret = GetEventHandler()->ProcessEvent(event);
187 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
189 wxASSERT_MSG([m_cocoaNSView window]==[cocoaEvent window],"Mouse event for different NSWindow");
190 NSPoint cocoaPoint = [m_cocoaNSView convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
191 NSRect cocoaRect = [m_cocoaNSView frame];
192 const wxPoint &clientorigin = GetClientAreaOrigin();
193 event.m_x = (wxCoord)cocoaPoint.x - clientorigin.x;
194 event.m_y = (wxCoord)(cocoaRect.size.height - cocoaPoint.y) - clientorigin.y;
196 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
197 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
198 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
199 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
201 // TODO: set timestamp?
202 event.SetEventObject(this);
203 event.SetId(GetId());
206 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
208 wxMouseEvent event(wxEVT_MOTION);
209 InitMouseEvent(event,theEvent);
210 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
211 return GetEventHandler()->ProcessEvent(event);
214 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
219 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
224 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
226 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
227 InitMouseEvent(event,theEvent);
228 wxLogDebug("Mouse Down @%d,%d num clicks=%d",event.m_x,event.m_y,[theEvent clickCount]);
229 return GetEventHandler()->ProcessEvent(event);
232 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
234 wxMouseEvent event(wxEVT_MOTION);
235 InitMouseEvent(event,theEvent);
236 event.m_leftDown = true;
237 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
238 return GetEventHandler()->ProcessEvent(event);
241 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
243 wxMouseEvent event(wxEVT_LEFT_UP);
244 InitMouseEvent(event,theEvent);
245 wxLogDebug("Mouse Up @%d,%d",event.m_x,event.m_y);
246 return GetEventHandler()->ProcessEvent(event);
249 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
254 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
259 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
264 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
269 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
274 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
279 void wxWindowCocoa::Cocoa_FrameChanged(void)
281 wxLogDebug("Cocoa_FrameChanged");
282 wxSizeEvent event(GetSize(), m_windowId);
283 event.SetEventObject(this);
284 GetEventHandler()->ProcessEvent(event);
287 bool wxWindow::Close(bool force)
292 void wxWindow::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
294 [[oldView superview] replaceSubview:oldView with:newView];
297 bool wxWindow::Show(bool show)
299 wxAutoNSAutoreleasePool pool;
300 // If the window is marked as visible, then it shouldn't have a dummy view
301 // If the window is marked hidden, then it should have a dummy view
302 // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
303 // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),"wxWindow: m_isShown does not agree with m_dummyNSView");
304 // Return false if there isn't a window to show or hide
305 NSView *cocoaView = GetNSViewForHiding();
310 // If state isn't changing, return false
313 CocoaReplaceView(m_cocoaHider->GetNSView(), cocoaView);
314 wxASSERT(![m_cocoaHider->GetNSView() superview]);
317 wxASSERT([cocoaView superview]);
321 // If state isn't changing, return false
324 m_cocoaHider = new wxWindowCocoaHider(this);
325 // NOTE: replaceSubview:with will cause m_cocaNSView to be
326 // (auto)released which balances out addSubview
327 CocoaReplaceView(cocoaView, m_cocoaHider->GetNSView());
328 // m_coocaNSView is now only retained by us
329 wxASSERT([m_cocoaHider->GetNSView() superview]);
330 wxASSERT(![cocoaView superview]);
336 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
338 // 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":".");
339 int currentX, currentY;
340 int currentW, currentH;
341 DoGetPosition(¤tX, ¤tY);
342 DoGetSize(¤tW, ¤tH);
343 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
345 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
348 AdjustForParentClientOrigin(x,y,sizeFlags);
352 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
354 if(sizeFlags&wxSIZE_AUTO_WIDTH)
356 size=DoGetBestSize();
362 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
364 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
367 size=DoGetBestSize();
373 DoMoveWindow(x,y,width,height);
376 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
378 // wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
380 NSView *nsview = GetNSViewForSuperview();
381 NSView *superview = [nsview superview];
382 wxCHECK_RET(superview,"NSView does not have a superview");
383 NSRect parentRect = [superview frame];
385 NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
386 [nsview setFrame: cocoaRect];
390 void wxWindow::DoGetSize(int *w, int *h) const
392 NSRect cocoaRect = [GetNSViewForSuperview() frame];
394 *w=(int)cocoaRect.size.width;
396 *h=(int)cocoaRect.size.height;
397 // wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
400 void wxWindow::DoGetPosition(int *x, int *y) const
402 NSView *nsview = GetNSViewForSuperview();
403 NSView *superview = [nsview superview];
404 wxCHECK_RET(superview,"NSView does not have a superview");
405 NSRect parentRect = [superview frame];
407 NSRect cocoaRect = [nsview frame];
409 *x=(int)cocoaRect.origin.x;
411 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
412 // wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
415 WXWidget wxWindow::GetHandle() const
417 return m_cocoaNSView;
420 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
422 [m_cocoaNSView setNeedsDisplay:YES];
425 void wxWindow::SetFocus()
430 void wxWindow::DoCaptureMouse()
433 sm_capturedWindow = this;
436 void wxWindow::DoReleaseMouse()
439 sm_capturedWindow = NULL;
442 void wxWindow::DoScreenToClient(int *x, int *y) const
447 void wxWindow::DoClientToScreen(int *x, int *y) const
452 // Get size *available for subwindows* i.e. excluding menu bar etc.
453 void wxWindow::DoGetClientSize(int *x, int *y) const
455 wxLogDebug("DoGetClientSize:");
456 wxWindowCocoa::DoGetSize(x,y);
457 // TODO: Actually account for menubar, borders, etc...
460 void wxWindow::DoSetClientSize(int width, int height)
462 wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
466 int wxWindow::GetCharHeight() const
472 int wxWindow::GetCharWidth() const
478 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
479 int *descent, int *externalLeading, const wxFont *theFont) const
484 // Coordinates relative to the window
485 void wxWindow::WarpPointer (int x_pos, int y_pos)
490 int wxWindow::GetScrollPos(int orient) const
496 // This now returns the whole range, not just the number
497 // of positions that we can scroll.
498 int wxWindow::GetScrollRange(int orient) const
504 int wxWindow::GetScrollThumb(int orient) const
510 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
515 // New function that will replace some of the above.
516 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
517 int range, bool refresh)
522 // Does a physical scroll
523 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
528 bool wxWindow::SetFont(const wxFont& font)
534 static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
536 // first should be ordered higher
538 return NSOrderedDescending;
539 // second should be ordered higher
541 return NSOrderedAscending;
542 return NSOrderedSame;
545 // Raise the window to the top of the Z order
546 void wxWindow::Raise()
548 // wxAutoNSAutoreleasePool pool;
549 NSView *nsview = GetNSViewForSuperview();
550 [[nsview superview] sortSubviewsUsingFunction:
551 CocoaRaiseWindowCompareFunction
555 static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
557 // first should be ordered lower
559 return NSOrderedAscending;
560 // second should be ordered lower
562 return NSOrderedDescending;
563 return NSOrderedSame;
566 // Lower the window to the bottom of the Z order
567 void wxWindow::Lower()
569 NSView *nsview = GetNSViewForSuperview();
570 [[nsview superview] sortSubviewsUsingFunction:
571 CocoaLowerWindowCompareFunction
575 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
580 // Get the window with the focus
581 wxWindow *wxWindowBase::FindFocus()
587 /* static */ wxWindow *wxWindowBase::GetCapture()
590 return wxWindowCocoa::sm_capturedWindow;
593 wxWindow *wxGetActiveWindow()
599 wxPoint wxGetMousePosition()
602 return wxDefaultPosition;
605 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
607 pt = wxGetMousePosition();