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)
30 wxWindow *wxWindowCocoa::sm_capturedWindow = NULL;
33 void wxWindowCocoa::Init()
39 m_isBeingDeleted = FALSE;
44 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
50 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
53 // TODO: create the window
54 NSRect cocoaRect = NSMakeRect(10,10,20,20);
56 SetNSView([[NSView alloc] initWithFrame: cocoaRect]);
57 [m_cocoaNSView release];
61 m_parent->AddChild(this);
62 m_parent->CocoaAddChild(this);
71 wxAutoNSAutoreleasePool pool;
75 m_parent->RemoveChild(this);
77 CocoaRemoveFromParent();
81 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
83 [m_cocoaNSView addSubview: child->m_cocoaNSView];
84 wxASSERT(!child->m_dummyNSView);
85 child->m_isShown = true;
88 void wxWindowCocoa::CocoaRemoveFromParent(void)
92 wxASSERT(m_cocoaNSView);
93 // balances the replaceSubView:with:
94 [m_dummyNSView removeFromSuperview];
95 // But since we were the ones to alloc it
96 [m_dummyNSView release];
98 // m_cocoaNSView has of course already been removed by virtue of
99 // replaceSubview: m_cocoaNSView with: m_dummyNSView
102 [m_cocoaNSView removeFromSuperview];
105 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
107 bool need_debug = cocoaNSView || m_cocoaNSView;
108 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d",this,m_cocoaNSView,[m_cocoaNSView retainCount]);
109 DisassociateNSView(m_cocoaNSView);
110 [cocoaNSView retain];
111 [m_cocoaNSView release];
112 m_cocoaNSView = cocoaNSView;
113 AssociateNSView(m_cocoaNSView);
114 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d",this,cocoaNSView,[cocoaNSView retainCount]);
117 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
119 wxLogDebug("Cocoa_drawRect");
120 // Recursion can happen if the event loop runs from within the paint
121 // handler. For instance, if an assertion dialog is shown.
122 // FIXME: This seems less than ideal.
125 wxLogDebug("Paint event recursion!");
128 //FIXME: should probably turn that rect into the update region
130 wxPaintEvent event(m_windowId);
131 event.SetEventObject(this);
132 bool ret = GetEventHandler()->ProcessEvent(event);
137 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
139 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
140 wxASSERT_MSG([nsview window]==[cocoaEvent window],"Mouse event for different NSWindow");
141 NSPoint cocoaPoint = [nsview convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
142 NSRect cocoaRect = [nsview frame];
143 const wxPoint &clientorigin = GetClientAreaOrigin();
144 event.m_x = (wxCoord)cocoaPoint.x - clientorigin.x;
145 event.m_y = (wxCoord)(cocoaRect.size.height - cocoaPoint.y) - clientorigin.y;
147 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
148 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
149 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
150 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
152 // TODO: set timestamp?
153 event.SetEventObject(this);
154 event.SetId(GetId());
157 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
159 wxMouseEvent event(wxEVT_MOTION);
160 InitMouseEvent(event,theEvent);
161 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
162 return GetEventHandler()->ProcessEvent(event);
165 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
170 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
175 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
177 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
178 InitMouseEvent(event,theEvent);
179 wxLogDebug("Mouse Down @%d,%d num clicks=%d",event.m_x,event.m_y,[theEvent clickCount]);
180 return GetEventHandler()->ProcessEvent(event);
183 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
185 wxMouseEvent event(wxEVT_MOTION);
186 InitMouseEvent(event,theEvent);
187 event.m_leftDown = true;
188 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
189 return GetEventHandler()->ProcessEvent(event);
192 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
194 wxMouseEvent event(wxEVT_LEFT_UP);
195 InitMouseEvent(event,theEvent);
196 wxLogDebug("Mouse Up @%d,%d",event.m_x,event.m_y);
197 return GetEventHandler()->ProcessEvent(event);
200 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
205 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
210 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
215 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
220 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
225 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
230 void wxWindowCocoa::Cocoa_FrameChanged(void)
232 wxLogDebug("Cocoa_FrameChanged");
233 wxSizeEvent event(GetSize(), m_windowId);
234 event.SetEventObject(this);
235 GetEventHandler()->ProcessEvent(event);
238 bool wxWindow::Close(bool force)
243 bool wxWindow::Show(bool show)
245 wxAutoNSAutoreleasePool pool;
246 // If the window is marked as visible, then it shouldn't have a dummy view
247 // If the window is marked hidden, then it should have a dummy view
248 // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
249 // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),"wxWindow: m_isShown does not agree with m_dummyNSView");
250 // Return false if there isn't a window to show or hide
255 // If state isn't changing, return false
258 // replaceSubView:with: releases m_dummyNSView, balancing the
259 // previous replaceSubView:with
260 [[m_dummyNSView superview] replaceSubview:m_dummyNSView with:m_cocoaNSView];
261 wxASSERT(![m_dummyNSView superview]);
262 // But since we were the ones to alloc it
263 [m_dummyNSView release];
265 wxASSERT([m_cocoaNSView superview]);
269 // If state isn't changing, return false
272 m_dummyNSView = [[NSView alloc] initWithFrame: [m_cocoaNSView frame]];
273 // NOTE: replaceSubView will cause m_cocaNSView to be (auto)released
274 // which balances out addSubView
275 [[m_cocoaNSView superview] replaceSubview:m_cocoaNSView with:m_dummyNSView];
276 // m_coocaNSView is now only retained by us
277 wxASSERT([m_dummyNSView superview]);
278 wxASSERT(![m_cocoaNSView superview]);
284 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
286 // 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":".");
287 int currentX, currentY;
288 int currentW, currentH;
289 DoGetPosition(¤tX, ¤tY);
290 DoGetSize(¤tW, ¤tH);
291 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
293 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
296 AdjustForParentClientOrigin(x,y,sizeFlags);
300 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
302 if(sizeFlags&wxSIZE_AUTO_WIDTH)
304 size=DoGetBestSize();
310 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
312 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
315 size=DoGetBestSize();
321 DoMoveWindow(x,y,width,height);
324 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
326 // wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
328 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
329 NSView *superview = [nsview superview];
330 wxCHECK_RET(superview,"NSView does not have a superview");
331 NSRect parentRect = [superview frame];
333 NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
334 [m_cocoaNSView setFrame: cocoaRect];
335 // Also change the dummy's size
337 [m_dummyNSView setFrame: cocoaRect];
341 void wxWindow::DoGetSize(int *w, int *h) const
343 NSRect cocoaRect = [m_cocoaNSView frame];
345 *w=(int)cocoaRect.size.width;
347 *h=(int)cocoaRect.size.height;
348 // wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
351 void wxWindow::DoGetPosition(int *x, int *y) const
353 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
354 NSView *superview = [nsview superview];
355 wxCHECK_RET(superview,"NSView does not have a superview");
356 NSRect parentRect = [superview frame];
358 NSRect cocoaRect = [nsview frame];
360 *x=(int)cocoaRect.origin.x;
362 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
363 // wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
366 WXWidget wxWindow::GetHandle() const
368 return m_cocoaNSView;
371 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
373 [m_cocoaNSView setNeedsDisplay:YES];
376 void wxWindow::SetFocus()
381 void wxWindow::DoCaptureMouse()
384 sm_capturedWindow = this;
387 void wxWindow::DoReleaseMouse()
390 sm_capturedWindow = NULL;
393 void wxWindow::DoScreenToClient(int *x, int *y) const
398 void wxWindow::DoClientToScreen(int *x, int *y) const
403 // Get size *available for subwindows* i.e. excluding menu bar etc.
404 void wxWindow::DoGetClientSize(int *x, int *y) const
406 wxLogDebug("DoGetClientSize:");
407 wxWindowCocoa::DoGetSize(x,y);
408 // TODO: Actually account for menubar, borders, etc...
411 void wxWindow::DoSetClientSize(int width, int height)
413 wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
417 int wxWindow::GetCharHeight() const
423 int wxWindow::GetCharWidth() const
429 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
430 int *descent, int *externalLeading, const wxFont *theFont) const
435 // Coordinates relative to the window
436 void wxWindow::WarpPointer (int x_pos, int y_pos)
441 int wxWindow::GetScrollPos(int orient) const
447 // This now returns the whole range, not just the number
448 // of positions that we can scroll.
449 int wxWindow::GetScrollRange(int orient) const
455 int wxWindow::GetScrollThumb(int orient) const
461 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
466 // New function that will replace some of the above.
467 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
468 int range, bool refresh)
473 // Does a physical scroll
474 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
479 bool wxWindow::SetFont(const wxFont& font)
485 void wxWindow::Clear()
490 // Raise the window to the top of the Z order
491 void wxWindow::Raise()
493 wxAutoNSAutoreleasePool pool;
494 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
495 NSView *superview = [nsview superview];
496 [nsview removeFromSuperview];
497 [superview addSubview:nsview];
500 // Lower the window to the bottom of the Z order
501 void wxWindow::Lower()
506 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
511 // Get the window with the focus
512 wxWindow *wxWindowBase::FindFocus()
518 /* static */ wxWindow *wxWindowBase::GetCapture()
521 return wxWindowCocoa::sm_capturedWindow;
524 wxWindow *wxGetActiveWindow()
530 wxPoint wxGetMousePosition()
533 return wxDefaultPosition;
536 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
538 pt = wxGetMousePosition();