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>
17 // normally the base classes aren't included, but wxWindow is special
18 #ifdef __WXUNIVERSAL__
19 IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
21 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
24 BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
28 void wxWindowCocoa::Init()
34 m_isBeingDeleted = FALSE;
38 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
44 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
47 // TODO: create the window
48 NSRect cocoaRect = NSMakeRect(10,10,20,20);
50 SetNSView([[NSView alloc] initWithFrame: cocoaRect]);
51 [m_cocoaNSView release];
55 m_parent->AddChild(this);
56 m_parent->CocoaAddChild(this);
68 m_parent->RemoveChild(this);
70 CocoaRemoveFromParent();
74 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
76 [child->m_cocoaNSView retain];
77 // NOTE: addSubView takes ownership of, but does not retain the subview
78 // Upon a removeFromView or closing the super view, the child WILL be
79 // released!!! I think the idea here is that normally you would alloc
80 // the subview and add it to the superview and this way you don't have
81 // to release what you just alloced. Unfortunately, that doesn't
82 // make sense for wxCocoa, so we do this instead.
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);
94 [m_dummyNSView removeFromSuperview];
95 // But since we also retained it ourselves
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]);
110 DisassociateNSView(m_cocoaNSView);
111 [cocoaNSView retain];
112 [m_cocoaNSView release];
113 m_cocoaNSView = cocoaNSView;
115 AssociateNSView(m_cocoaNSView);
116 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d",this,cocoaNSView,[cocoaNSView retainCount]);
119 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
121 wxLogDebug("Cocoa_drawRect");
122 //FIXME: should probably turn that rect into the update region
123 wxPaintEvent event(m_windowId);
124 event.SetEventObject(this);
125 return GetEventHandler()->ProcessEvent(event);
128 void wxWindowCocoa::Cocoa_FrameChanged(void)
130 wxLogDebug("Cocoa_FrameChanged");
131 wxSizeEvent event(GetSize(), m_windowId);
132 event.SetEventObject(this);
133 GetEventHandler()->ProcessEvent(event);
136 bool wxWindow::Close(bool force)
141 bool wxWindow::Show(bool show)
143 // If the window is marked as visible, then it shouldn't have a dummy view
144 // If the window is marked hidden, then it should have a dummy view
145 wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),"wxWindow: m_isShown does not agree with m_dummyNSView");
146 // Return false if there isn't a window to show or hide
149 // Return false if the state isn't changing
150 if( show == m_isShown )
154 // replaceSubView releases m_dummyNSView, balancing the alloc
155 [m_cocoaNSView retain];
156 [[m_dummyNSView superview] replaceSubview:m_dummyNSView with:m_cocoaNSView];
157 // But since we also retained it ourselves
158 [m_dummyNSView release];
163 m_dummyNSView = [[NSView alloc] initWithFrame: [m_cocoaNSView frame]];
164 [m_dummyNSView retain];
165 // NOTE: replaceSubView will cause m_cocaNSView to be released
166 [[m_cocoaNSView superview] replaceSubview:m_cocoaNSView with:m_dummyNSView];
167 // m_coocaNSView is now only retained by us
173 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
175 // 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":".");
176 int currentX, currentY;
177 int currentW, currentH;
178 DoGetPosition(¤tX, ¤tY);
179 DoGetSize(¤tW, ¤tH);
180 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
182 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
185 AdjustForParentClientOrigin(x,y,sizeFlags);
189 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
191 if(sizeFlags&wxSIZE_AUTO_WIDTH)
193 size=DoGetBestSize();
199 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
201 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
204 size=DoGetBestSize();
210 DoMoveWindow(x,y,width,height);
213 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
215 // wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
217 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
218 NSView *superview = [nsview superview];
219 wxCHECK_RET(superview,"NSView does not have a superview");
220 NSRect parentRect = [superview frame];
222 NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
223 [m_cocoaNSView setFrame: cocoaRect];
224 // Also change the dummy's size
226 [m_dummyNSView setFrame: cocoaRect];
230 void wxWindow::DoGetSize(int *w, int *h) const
232 NSRect cocoaRect = [m_cocoaNSView frame];
234 *w=(int)cocoaRect.size.width;
236 *h=(int)cocoaRect.size.height;
237 // wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
240 void wxWindow::DoGetPosition(int *x, int *y) const
242 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
243 NSView *superview = [nsview superview];
244 wxCHECK_RET(superview,"NSView does not have a superview");
245 NSRect parentRect = [superview frame];
247 NSRect cocoaRect = [nsview frame];
249 *x=(int)cocoaRect.origin.x;
251 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
252 // wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
255 WXWidget wxWindow::GetHandle() const
257 return m_cocoaNSView;
260 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
262 [m_cocoaNSView setNeedsDisplay:YES];
265 void wxWindow::SetFocus()
270 void wxWindow::DoCaptureMouse()
275 void wxWindow::DoReleaseMouse()
280 void wxWindow::DoScreenToClient(int *x, int *y) const
285 void wxWindow::DoClientToScreen(int *x, int *y) const
290 // Get size *available for subwindows* i.e. excluding menu bar etc.
291 void wxWindow::DoGetClientSize(int *x, int *y) const
293 wxLogDebug("DoGetClientSize:");
294 wxWindowCocoa::DoGetSize(x,y);
295 // TODO: Actually account for menubar, borders, etc...
298 void wxWindow::DoSetClientSize(int width, int height)
300 wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
304 int wxWindow::GetCharHeight() const
310 int wxWindow::GetCharWidth() const
316 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
317 int *descent, int *externalLeading, const wxFont *theFont) const
322 // Coordinates relative to the window
323 void wxWindow::WarpPointer (int x_pos, int y_pos)
328 int wxWindow::GetScrollPos(int orient) const
334 // This now returns the whole range, not just the number
335 // of positions that we can scroll.
336 int wxWindow::GetScrollRange(int orient) const
342 int wxWindow::GetScrollThumb(int orient) const
348 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
353 // New function that will replace some of the above.
354 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
355 int range, bool refresh)
360 // Does a physical scroll
361 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
366 bool wxWindow::SetFont(const wxFont& font)
372 void wxWindow::Clear()
377 // Raise the window to the top of the Z order
378 void wxWindow::Raise()
380 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
381 NSView *superview = [nsview superview];
383 [nsview removeFromSuperview];
384 [superview addSubview:nsview];
387 // Lower the window to the bottom of the Z order
388 void wxWindow::Lower()
393 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
398 // Get the window with the focus
399 wxWindow *wxWindowBase::FindFocus()
405 /* static */ wxWindow *wxWindowBase::GetCapture()
411 wxWindow *wxGetActiveWindow()