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 #import <Appkit/NSView.h>
16 #import <AppKit/NSEvent.h>
18 // normally the base classes aren't included, but wxWindow is special
19 #ifdef __WXUNIVERSAL__
20 IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
22 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
25 BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
29 void wxWindowCocoa::Init()
35 m_isBeingDeleted = FALSE;
39 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
45 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
48 // TODO: create the window
49 NSRect cocoaRect = NSMakeRect(10,10,20,20);
51 SetNSView([[NSView alloc] initWithFrame: cocoaRect]);
52 [m_cocoaNSView release];
56 m_parent->AddChild(this);
57 m_parent->CocoaAddChild(this);
69 m_parent->RemoveChild(this);
71 CocoaRemoveFromParent();
75 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
77 [child->m_cocoaNSView retain];
78 // NOTE: addSubView takes ownership of, but does not retain the subview
79 // Upon a removeFromView or closing the super view, the child WILL be
80 // released!!! I think the idea here is that normally you would alloc
81 // the subview and add it to the superview and this way you don't have
82 // to release what you just alloced. Unfortunately, that doesn't
83 // make sense for wxCocoa, so we do this instead.
84 [m_cocoaNSView addSubview: child->m_cocoaNSView];
85 wxASSERT(!child->m_dummyNSView);
86 child->m_isShown = true;
89 void wxWindowCocoa::CocoaRemoveFromParent(void)
93 wxASSERT(m_cocoaNSView);
95 [m_dummyNSView removeFromSuperview];
96 // But since we also retained it ourselves
97 [m_dummyNSView release];
99 // m_cocoaNSView has of course already been removed by virtue of
100 // replaceSubview: m_cocoaNSView with: m_dummyNSView
103 [m_cocoaNSView removeFromSuperview];
106 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
108 bool need_debug = cocoaNSView || m_cocoaNSView;
109 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d",this,m_cocoaNSView,[m_cocoaNSView retainCount]);
111 DisassociateNSView(m_cocoaNSView);
112 [cocoaNSView retain];
113 [m_cocoaNSView release];
114 m_cocoaNSView = cocoaNSView;
116 AssociateNSView(m_cocoaNSView);
117 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d",this,cocoaNSView,[cocoaNSView retainCount]);
120 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
122 wxLogDebug("Cocoa_drawRect");
123 //FIXME: should probably turn that rect into the update region
124 wxPaintEvent event(m_windowId);
125 event.SetEventObject(this);
126 return GetEventHandler()->ProcessEvent(event);
129 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
131 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
132 wxASSERT_MSG([nsview window]==[cocoaEvent window],"Mouse event for different NSWindow");
133 NSPoint cocoaPoint = [nsview convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
134 NSRect cocoaRect = [nsview frame];
135 const wxPoint &clientorigin = GetClientAreaOrigin();
136 event.m_x = (wxCoord)cocoaPoint.x - clientorigin.x;
137 event.m_y = (wxCoord)(cocoaRect.size.height - cocoaPoint.y) - clientorigin.y;
139 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
140 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
141 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
142 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
144 // TODO: set timestamp?
145 event.SetEventObject(this);
146 event.SetId(GetId());
149 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
151 wxMouseEvent event(wxEVT_MOTION);
152 InitMouseEvent(event,theEvent);
153 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
154 return GetEventHandler()->ProcessEvent(event);
157 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
162 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
167 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
169 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
170 InitMouseEvent(event,theEvent);
171 wxLogDebug("Mouse Down @%d,%d num clicks=%d",event.m_x,event.m_y,[theEvent clickCount]);
172 return GetEventHandler()->ProcessEvent(event);
175 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
177 wxMouseEvent event(wxEVT_MOTION);
178 InitMouseEvent(event,theEvent);
179 event.m_leftDown = true;
180 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
181 return GetEventHandler()->ProcessEvent(event);
184 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
186 wxMouseEvent event(wxEVT_LEFT_UP);
187 InitMouseEvent(event,theEvent);
188 wxLogDebug("Mouse Up @%d,%d",event.m_x,event.m_y);
189 return GetEventHandler()->ProcessEvent(event);
192 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
197 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
202 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
207 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
212 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
217 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
222 void wxWindowCocoa::Cocoa_FrameChanged(void)
224 wxLogDebug("Cocoa_FrameChanged");
225 wxSizeEvent event(GetSize(), m_windowId);
226 event.SetEventObject(this);
227 GetEventHandler()->ProcessEvent(event);
230 bool wxWindow::Close(bool force)
235 bool wxWindow::Show(bool show)
237 // If the window is marked as visible, then it shouldn't have a dummy view
238 // If the window is marked hidden, then it should have a dummy view
239 wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),"wxWindow: m_isShown does not agree with m_dummyNSView");
240 // Return false if there isn't a window to show or hide
243 // Return false if the state isn't changing
244 if( show == m_isShown )
248 // replaceSubView releases m_dummyNSView, balancing the alloc
249 [m_cocoaNSView retain];
250 [[m_dummyNSView superview] replaceSubview:m_dummyNSView with:m_cocoaNSView];
251 // But since we also retained it ourselves
252 wxASSERT(![m_dummyNSView superview]);
253 [m_dummyNSView release];
255 wxASSERT([m_cocoaNSView superview]);
259 m_dummyNSView = [[NSView alloc] initWithFrame: [m_cocoaNSView frame]];
260 [m_dummyNSView retain];
261 // NOTE: replaceSubView will cause m_cocaNSView to be released
262 [[m_cocoaNSView superview] replaceSubview:m_cocoaNSView with:m_dummyNSView];
263 // m_coocaNSView is now only retained by us
264 wxASSERT([m_dummyNSView superview]);
265 wxASSERT(![m_cocoaNSView superview]);
271 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
273 // 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":".");
274 int currentX, currentY;
275 int currentW, currentH;
276 DoGetPosition(¤tX, ¤tY);
277 DoGetSize(¤tW, ¤tH);
278 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
280 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
283 AdjustForParentClientOrigin(x,y,sizeFlags);
287 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
289 if(sizeFlags&wxSIZE_AUTO_WIDTH)
291 size=DoGetBestSize();
297 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
299 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
302 size=DoGetBestSize();
308 DoMoveWindow(x,y,width,height);
311 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
313 // wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
315 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
316 NSView *superview = [nsview superview];
317 wxCHECK_RET(superview,"NSView does not have a superview");
318 NSRect parentRect = [superview frame];
320 NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
321 [m_cocoaNSView setFrame: cocoaRect];
322 // Also change the dummy's size
324 [m_dummyNSView setFrame: cocoaRect];
328 void wxWindow::DoGetSize(int *w, int *h) const
330 NSRect cocoaRect = [m_cocoaNSView frame];
332 *w=(int)cocoaRect.size.width;
334 *h=(int)cocoaRect.size.height;
335 // wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
338 void wxWindow::DoGetPosition(int *x, int *y) const
340 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
341 NSView *superview = [nsview superview];
342 wxCHECK_RET(superview,"NSView does not have a superview");
343 NSRect parentRect = [superview frame];
345 NSRect cocoaRect = [nsview frame];
347 *x=(int)cocoaRect.origin.x;
349 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
350 // wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
353 WXWidget wxWindow::GetHandle() const
355 return m_cocoaNSView;
358 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
360 [m_cocoaNSView setNeedsDisplay:YES];
363 void wxWindow::SetFocus()
368 void wxWindow::DoCaptureMouse()
373 void wxWindow::DoReleaseMouse()
378 void wxWindow::DoScreenToClient(int *x, int *y) const
383 void wxWindow::DoClientToScreen(int *x, int *y) const
388 // Get size *available for subwindows* i.e. excluding menu bar etc.
389 void wxWindow::DoGetClientSize(int *x, int *y) const
391 wxLogDebug("DoGetClientSize:");
392 wxWindowCocoa::DoGetSize(x,y);
393 // TODO: Actually account for menubar, borders, etc...
396 void wxWindow::DoSetClientSize(int width, int height)
398 wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
402 int wxWindow::GetCharHeight() const
408 int wxWindow::GetCharWidth() const
414 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
415 int *descent, int *externalLeading, const wxFont *theFont) const
420 // Coordinates relative to the window
421 void wxWindow::WarpPointer (int x_pos, int y_pos)
426 int wxWindow::GetScrollPos(int orient) const
432 // This now returns the whole range, not just the number
433 // of positions that we can scroll.
434 int wxWindow::GetScrollRange(int orient) const
440 int wxWindow::GetScrollThumb(int orient) const
446 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
451 // New function that will replace some of the above.
452 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
453 int range, bool refresh)
458 // Does a physical scroll
459 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
464 bool wxWindow::SetFont(const wxFont& font)
470 void wxWindow::Clear()
475 // Raise the window to the top of the Z order
476 void wxWindow::Raise()
478 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
479 NSView *superview = [nsview superview];
481 [nsview removeFromSuperview];
482 [superview addSubview:nsview];
485 // Lower the window to the bottom of the Z order
486 void wxWindow::Lower()
491 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
496 // Get the window with the focus
497 wxWindow *wxWindowBase::FindFocus()
503 /* static */ wxWindow *wxWindowBase::GetCapture()
509 wxWindow *wxGetActiveWindow()