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 m_cocoaNSView = NULL;
102 SetNSView([[NSView alloc] initWithFrame: MakeDefaultNSRect(size)]);
103 [m_cocoaNSView release];
107 m_parent->AddChild(this);
108 m_parent->CocoaAddChild(this);
110 SetInitialFrameRect(pos,size);
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];
389 void wxWindowCocoa::SetInitialFrameRect(const wxPoint& pos, const wxSize& size)
391 NSView *nsview = GetNSViewForSuperview();
392 NSView *superview = [nsview superview];
393 wxCHECK_RET(superview,"NSView does not have a superview");
394 NSRect parentRect = [superview frame];
395 NSRect frameRect = [nsview frame];
397 frameRect.size.width = size.x;
399 frameRect.size.height = size.y;
400 frameRect.origin.x = pos.x;
401 frameRect.origin.y = parentRect.size.height-(pos.y+size.y);
402 [nsview setFrame: frameRect];
406 void wxWindow::DoGetSize(int *w, int *h) const
408 NSRect cocoaRect = [GetNSViewForSuperview() frame];
410 *w=(int)cocoaRect.size.width;
412 *h=(int)cocoaRect.size.height;
413 // wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
416 void wxWindow::DoGetPosition(int *x, int *y) const
418 NSView *nsview = GetNSViewForSuperview();
419 NSView *superview = [nsview superview];
420 wxCHECK_RET(superview,"NSView does not have a superview");
421 NSRect parentRect = [superview frame];
423 NSRect cocoaRect = [nsview frame];
425 *x=(int)cocoaRect.origin.x;
427 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
428 // wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
431 WXWidget wxWindow::GetHandle() const
433 return m_cocoaNSView;
436 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
438 [m_cocoaNSView setNeedsDisplay:YES];
441 void wxWindow::SetFocus()
446 void wxWindow::DoCaptureMouse()
449 sm_capturedWindow = this;
452 void wxWindow::DoReleaseMouse()
455 sm_capturedWindow = NULL;
458 void wxWindow::DoScreenToClient(int *x, int *y) const
463 void wxWindow::DoClientToScreen(int *x, int *y) const
468 // Get size *available for subwindows* i.e. excluding menu bar etc.
469 void wxWindow::DoGetClientSize(int *x, int *y) const
471 wxLogDebug("DoGetClientSize:");
472 wxWindowCocoa::DoGetSize(x,y);
473 // TODO: Actually account for menubar, borders, etc...
476 void wxWindow::DoSetClientSize(int width, int height)
478 wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
482 int wxWindow::GetCharHeight() const
488 int wxWindow::GetCharWidth() const
494 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
495 int *descent, int *externalLeading, const wxFont *theFont) const
500 // Coordinates relative to the window
501 void wxWindow::WarpPointer (int x_pos, int y_pos)
506 int wxWindow::GetScrollPos(int orient) const
512 // This now returns the whole range, not just the number
513 // of positions that we can scroll.
514 int wxWindow::GetScrollRange(int orient) const
520 int wxWindow::GetScrollThumb(int orient) const
526 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
531 // New function that will replace some of the above.
532 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
533 int range, bool refresh)
538 // Does a physical scroll
539 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
544 bool wxWindow::SetFont(const wxFont& font)
550 static int CocoaRaiseWindowCompareFunction(id first, id second, void *target)
552 // first should be ordered higher
554 return NSOrderedDescending;
555 // second should be ordered higher
557 return NSOrderedAscending;
558 return NSOrderedSame;
561 // Raise the window to the top of the Z order
562 void wxWindow::Raise()
564 // wxAutoNSAutoreleasePool pool;
565 NSView *nsview = GetNSViewForSuperview();
566 [[nsview superview] sortSubviewsUsingFunction:
567 CocoaRaiseWindowCompareFunction
571 static int CocoaLowerWindowCompareFunction(id first, id second, void *target)
573 // first should be ordered lower
575 return NSOrderedAscending;
576 // second should be ordered lower
578 return NSOrderedDescending;
579 return NSOrderedSame;
582 // Lower the window to the bottom of the Z order
583 void wxWindow::Lower()
585 NSView *nsview = GetNSViewForSuperview();
586 [[nsview superview] sortSubviewsUsingFunction:
587 CocoaLowerWindowCompareFunction
591 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
596 // Get the window with the focus
597 wxWindow *wxWindowBase::FindFocus()
603 /* static */ wxWindow *wxWindowBase::GetCapture()
606 return wxWindowCocoa::sm_capturedWindow;
609 wxWindow *wxGetActiveWindow()
615 wxPoint wxGetMousePosition()
618 return wxDefaultPosition;
621 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
623 pt = wxGetMousePosition();