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 // normally the base classes aren't included, but wxWindow is special
21 #ifdef __WXUNIVERSAL__
22 IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
24 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
27 BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
31 void wxWindowCocoa::Init()
37 m_isBeingDeleted = FALSE;
42 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
48 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
51 // TODO: create the window
52 NSRect cocoaRect = NSMakeRect(10,10,20,20);
54 SetNSView([[NSView alloc] initWithFrame: cocoaRect]);
55 [m_cocoaNSView release];
59 m_parent->AddChild(this);
60 m_parent->CocoaAddChild(this);
69 wxAutoNSAutoreleasePool pool;
73 m_parent->RemoveChild(this);
75 CocoaRemoveFromParent();
79 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
81 [child->m_cocoaNSView retain];
82 // NOTE: addSubView takes ownership of, but does not retain the subview
83 // Upon a removeFromView or closing the super view, the child WILL be
84 // released!!! I think the idea here is that normally you would alloc
85 // the subview and add it to the superview and this way you don't have
86 // to release what you just alloced. Unfortunately, that doesn't
87 // make sense for wxCocoa, so we do this instead.
88 [m_cocoaNSView addSubview: child->m_cocoaNSView];
89 wxASSERT(!child->m_dummyNSView);
90 child->m_isShown = true;
93 void wxWindowCocoa::CocoaRemoveFromParent(void)
97 wxASSERT(m_cocoaNSView);
99 [m_dummyNSView removeFromSuperview];
100 // But since we also retained it ourselves
101 [m_dummyNSView release];
103 // m_cocoaNSView has of course already been removed by virtue of
104 // replaceSubview: m_cocoaNSView with: m_dummyNSView
107 [m_cocoaNSView removeFromSuperview];
110 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
112 bool need_debug = cocoaNSView || m_cocoaNSView;
113 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d",this,m_cocoaNSView,[m_cocoaNSView retainCount]);
114 DisassociateNSView(m_cocoaNSView);
115 [cocoaNSView retain];
116 [m_cocoaNSView release];
117 m_cocoaNSView = cocoaNSView;
118 AssociateNSView(m_cocoaNSView);
119 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d",this,cocoaNSView,[cocoaNSView retainCount]);
122 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
124 wxLogDebug("Cocoa_drawRect");
125 // Recursion can happen if the event loop runs from within the paint
126 // handler. For instance, if an assertion dialog is shown.
127 // FIXME: This seems less than ideal.
130 wxLogDebug("Paint event recursion!");
133 //FIXME: should probably turn that rect into the update region
135 wxPaintEvent event(m_windowId);
136 event.SetEventObject(this);
137 bool ret = GetEventHandler()->ProcessEvent(event);
142 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
144 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
145 wxASSERT_MSG([nsview window]==[cocoaEvent window],"Mouse event for different NSWindow");
146 NSPoint cocoaPoint = [nsview convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
147 NSRect cocoaRect = [nsview frame];
148 const wxPoint &clientorigin = GetClientAreaOrigin();
149 event.m_x = (wxCoord)cocoaPoint.x - clientorigin.x;
150 event.m_y = (wxCoord)(cocoaRect.size.height - cocoaPoint.y) - clientorigin.y;
152 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
153 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
154 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
155 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
157 // TODO: set timestamp?
158 event.SetEventObject(this);
159 event.SetId(GetId());
162 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
164 wxMouseEvent event(wxEVT_MOTION);
165 InitMouseEvent(event,theEvent);
166 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
167 return GetEventHandler()->ProcessEvent(event);
170 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
175 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
180 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
182 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
183 InitMouseEvent(event,theEvent);
184 wxLogDebug("Mouse Down @%d,%d num clicks=%d",event.m_x,event.m_y,[theEvent clickCount]);
185 return GetEventHandler()->ProcessEvent(event);
188 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
190 wxMouseEvent event(wxEVT_MOTION);
191 InitMouseEvent(event,theEvent);
192 event.m_leftDown = true;
193 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
194 return GetEventHandler()->ProcessEvent(event);
197 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
199 wxMouseEvent event(wxEVT_LEFT_UP);
200 InitMouseEvent(event,theEvent);
201 wxLogDebug("Mouse Up @%d,%d",event.m_x,event.m_y);
202 return GetEventHandler()->ProcessEvent(event);
205 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
210 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
215 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
220 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
225 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
230 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
235 void wxWindowCocoa::Cocoa_FrameChanged(void)
237 wxLogDebug("Cocoa_FrameChanged");
238 wxSizeEvent event(GetSize(), m_windowId);
239 event.SetEventObject(this);
240 GetEventHandler()->ProcessEvent(event);
243 bool wxWindow::Close(bool force)
248 bool wxWindow::Show(bool show)
250 wxAutoNSAutoreleasePool pool;
251 // If the window is marked as visible, then it shouldn't have a dummy view
252 // If the window is marked hidden, then it should have a dummy view
253 // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
254 // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),"wxWindow: m_isShown does not agree with m_dummyNSView");
255 // Return false if there isn't a window to show or hide
260 // If state isn't changing, return false
263 // replaceSubView releases m_dummyNSView, balancing the alloc
264 [m_cocoaNSView retain];
265 [[m_dummyNSView superview] replaceSubview:m_dummyNSView with:m_cocoaNSView];
266 // But since we also retained it ourselves
267 wxASSERT(![m_dummyNSView superview]);
268 [m_dummyNSView release];
270 wxASSERT([m_cocoaNSView superview]);
274 // If state isn't changing, return false
277 m_dummyNSView = [[NSView alloc] initWithFrame: [m_cocoaNSView frame]];
278 [m_dummyNSView retain];
279 // NOTE: replaceSubView will cause m_cocaNSView to be released
280 [[m_cocoaNSView superview] replaceSubview:m_cocoaNSView with:m_dummyNSView];
281 // m_coocaNSView is now only retained by us
282 wxASSERT([m_dummyNSView superview]);
283 wxASSERT(![m_cocoaNSView superview]);
289 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
291 // 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":".");
292 int currentX, currentY;
293 int currentW, currentH;
294 DoGetPosition(¤tX, ¤tY);
295 DoGetSize(¤tW, ¤tH);
296 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
298 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
301 AdjustForParentClientOrigin(x,y,sizeFlags);
305 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
307 if(sizeFlags&wxSIZE_AUTO_WIDTH)
309 size=DoGetBestSize();
315 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
317 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
320 size=DoGetBestSize();
326 DoMoveWindow(x,y,width,height);
329 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
331 // wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
333 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
334 NSView *superview = [nsview superview];
335 wxCHECK_RET(superview,"NSView does not have a superview");
336 NSRect parentRect = [superview frame];
338 NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
339 [m_cocoaNSView setFrame: cocoaRect];
340 // Also change the dummy's size
342 [m_dummyNSView setFrame: cocoaRect];
346 void wxWindow::DoGetSize(int *w, int *h) const
348 NSRect cocoaRect = [m_cocoaNSView frame];
350 *w=(int)cocoaRect.size.width;
352 *h=(int)cocoaRect.size.height;
353 // wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
356 void wxWindow::DoGetPosition(int *x, int *y) const
358 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
359 NSView *superview = [nsview superview];
360 wxCHECK_RET(superview,"NSView does not have a superview");
361 NSRect parentRect = [superview frame];
363 NSRect cocoaRect = [nsview frame];
365 *x=(int)cocoaRect.origin.x;
367 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
368 // wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
371 WXWidget wxWindow::GetHandle() const
373 return m_cocoaNSView;
376 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
378 [m_cocoaNSView setNeedsDisplay:YES];
381 void wxWindow::SetFocus()
386 void wxWindow::DoCaptureMouse()
391 void wxWindow::DoReleaseMouse()
396 void wxWindow::DoScreenToClient(int *x, int *y) const
401 void wxWindow::DoClientToScreen(int *x, int *y) const
406 // Get size *available for subwindows* i.e. excluding menu bar etc.
407 void wxWindow::DoGetClientSize(int *x, int *y) const
409 wxLogDebug("DoGetClientSize:");
410 wxWindowCocoa::DoGetSize(x,y);
411 // TODO: Actually account for menubar, borders, etc...
414 void wxWindow::DoSetClientSize(int width, int height)
416 wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
420 int wxWindow::GetCharHeight() const
426 int wxWindow::GetCharWidth() const
432 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
433 int *descent, int *externalLeading, const wxFont *theFont) const
438 // Coordinates relative to the window
439 void wxWindow::WarpPointer (int x_pos, int y_pos)
444 int wxWindow::GetScrollPos(int orient) const
450 // This now returns the whole range, not just the number
451 // of positions that we can scroll.
452 int wxWindow::GetScrollRange(int orient) const
458 int wxWindow::GetScrollThumb(int orient) const
464 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
469 // New function that will replace some of the above.
470 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
471 int range, bool refresh)
476 // Does a physical scroll
477 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
482 bool wxWindow::SetFont(const wxFont& font)
488 void wxWindow::Clear()
493 // Raise the window to the top of the Z order
494 void wxWindow::Raise()
496 wxAutoNSAutoreleasePool pool;
497 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
498 NSView *superview = [nsview superview];
500 [nsview removeFromSuperview];
501 [superview addSubview:nsview];
504 // Lower the window to the bottom of the Z order
505 void wxWindow::Lower()
510 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
515 // Get the window with the focus
516 wxWindow *wxWindowBase::FindFocus()
522 /* static */ wxWindow *wxWindowBase::GetCapture()
528 wxWindow *wxGetActiveWindow()
534 wxPoint wxGetMousePosition()
537 return wxDefaultPosition;
540 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
542 pt = wxGetMousePosition();