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;
40 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
46 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
49 // TODO: create the window
50 NSRect cocoaRect = NSMakeRect(10,10,20,20);
52 SetNSView([[NSView alloc] initWithFrame: cocoaRect]);
53 [m_cocoaNSView release];
57 m_parent->AddChild(this);
58 m_parent->CocoaAddChild(this);
70 m_parent->RemoveChild(this);
72 CocoaRemoveFromParent();
76 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
78 [child->m_cocoaNSView retain];
79 // NOTE: addSubView takes ownership of, but does not retain the subview
80 // Upon a removeFromView or closing the super view, the child WILL be
81 // released!!! I think the idea here is that normally you would alloc
82 // the subview and add it to the superview and this way you don't have
83 // to release what you just alloced. Unfortunately, that doesn't
84 // make sense for wxCocoa, so we do this instead.
85 [m_cocoaNSView addSubview: child->m_cocoaNSView];
86 wxASSERT(!child->m_dummyNSView);
87 child->m_isShown = true;
90 void wxWindowCocoa::CocoaRemoveFromParent(void)
94 wxASSERT(m_cocoaNSView);
96 [m_dummyNSView removeFromSuperview];
97 // But since we also retained it ourselves
98 [m_dummyNSView release];
100 // m_cocoaNSView has of course already been removed by virtue of
101 // replaceSubview: m_cocoaNSView with: m_dummyNSView
104 [m_cocoaNSView removeFromSuperview];
107 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
109 bool need_debug = cocoaNSView || m_cocoaNSView;
110 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d",this,m_cocoaNSView,[m_cocoaNSView retainCount]);
112 DisassociateNSView(m_cocoaNSView);
113 [cocoaNSView retain];
114 [m_cocoaNSView release];
115 m_cocoaNSView = cocoaNSView;
117 AssociateNSView(m_cocoaNSView);
118 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d",this,cocoaNSView,[cocoaNSView retainCount]);
121 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
123 wxLogDebug("Cocoa_drawRect");
124 // Recursion can happen if the event loop runs from within the paint
125 // handler. For instance, if an assertion dialog is shown.
126 // FIXME: This seems less than ideal.
129 wxLogDebug("Paint event recursion!");
132 //FIXME: should probably turn that rect into the update region
134 wxPaintEvent event(m_windowId);
135 event.SetEventObject(this);
136 bool ret = GetEventHandler()->ProcessEvent(event);
141 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
143 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
144 wxASSERT_MSG([nsview window]==[cocoaEvent window],"Mouse event for different NSWindow");
145 NSPoint cocoaPoint = [nsview convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
146 NSRect cocoaRect = [nsview frame];
147 const wxPoint &clientorigin = GetClientAreaOrigin();
148 event.m_x = (wxCoord)cocoaPoint.x - clientorigin.x;
149 event.m_y = (wxCoord)(cocoaRect.size.height - cocoaPoint.y) - clientorigin.y;
151 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
152 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
153 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
154 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
156 // TODO: set timestamp?
157 event.SetEventObject(this);
158 event.SetId(GetId());
161 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
163 wxMouseEvent event(wxEVT_MOTION);
164 InitMouseEvent(event,theEvent);
165 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
166 return GetEventHandler()->ProcessEvent(event);
169 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
174 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
179 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
181 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
182 InitMouseEvent(event,theEvent);
183 wxLogDebug("Mouse Down @%d,%d num clicks=%d",event.m_x,event.m_y,[theEvent clickCount]);
184 return GetEventHandler()->ProcessEvent(event);
187 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
189 wxMouseEvent event(wxEVT_MOTION);
190 InitMouseEvent(event,theEvent);
191 event.m_leftDown = true;
192 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
193 return GetEventHandler()->ProcessEvent(event);
196 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
198 wxMouseEvent event(wxEVT_LEFT_UP);
199 InitMouseEvent(event,theEvent);
200 wxLogDebug("Mouse Up @%d,%d",event.m_x,event.m_y);
201 return GetEventHandler()->ProcessEvent(event);
204 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
209 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
214 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
219 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
224 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
229 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
234 void wxWindowCocoa::Cocoa_FrameChanged(void)
236 wxLogDebug("Cocoa_FrameChanged");
237 wxSizeEvent event(GetSize(), m_windowId);
238 event.SetEventObject(this);
239 GetEventHandler()->ProcessEvent(event);
242 bool wxWindow::Close(bool force)
247 bool wxWindow::Show(bool show)
249 // If the window is marked as visible, then it shouldn't have a dummy view
250 // If the window is marked hidden, then it should have a dummy view
251 wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),"wxWindow: m_isShown does not agree with m_dummyNSView");
252 // Return false if there isn't a window to show or hide
255 // Return false if the state isn't changing
256 if( show == m_isShown )
260 // replaceSubView releases m_dummyNSView, balancing the alloc
261 [m_cocoaNSView retain];
262 [[m_dummyNSView superview] replaceSubview:m_dummyNSView with:m_cocoaNSView];
263 // But since we also retained it ourselves
264 wxASSERT(![m_dummyNSView superview]);
265 [m_dummyNSView release];
267 wxASSERT([m_cocoaNSView superview]);
271 m_dummyNSView = [[NSView alloc] initWithFrame: [m_cocoaNSView frame]];
272 [m_dummyNSView retain];
273 // NOTE: replaceSubView will cause m_cocaNSView to be released
274 [[m_cocoaNSView superview] replaceSubview:m_cocoaNSView with:m_dummyNSView];
275 // m_coocaNSView is now only retained by us
276 wxASSERT([m_dummyNSView superview]);
277 wxASSERT(![m_cocoaNSView superview]);
283 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
285 // 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":".");
286 int currentX, currentY;
287 int currentW, currentH;
288 DoGetPosition(¤tX, ¤tY);
289 DoGetSize(¤tW, ¤tH);
290 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
292 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
295 AdjustForParentClientOrigin(x,y,sizeFlags);
299 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
301 if(sizeFlags&wxSIZE_AUTO_WIDTH)
303 size=DoGetBestSize();
309 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
311 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
314 size=DoGetBestSize();
320 DoMoveWindow(x,y,width,height);
323 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
325 // wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
327 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
328 NSView *superview = [nsview superview];
329 wxCHECK_RET(superview,"NSView does not have a superview");
330 NSRect parentRect = [superview frame];
332 NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
333 [m_cocoaNSView setFrame: cocoaRect];
334 // Also change the dummy's size
336 [m_dummyNSView setFrame: cocoaRect];
340 void wxWindow::DoGetSize(int *w, int *h) const
342 NSRect cocoaRect = [m_cocoaNSView frame];
344 *w=(int)cocoaRect.size.width;
346 *h=(int)cocoaRect.size.height;
347 // wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
350 void wxWindow::DoGetPosition(int *x, int *y) const
352 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
353 NSView *superview = [nsview superview];
354 wxCHECK_RET(superview,"NSView does not have a superview");
355 NSRect parentRect = [superview frame];
357 NSRect cocoaRect = [nsview frame];
359 *x=(int)cocoaRect.origin.x;
361 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
362 // wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
365 WXWidget wxWindow::GetHandle() const
367 return m_cocoaNSView;
370 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
372 [m_cocoaNSView setNeedsDisplay:YES];
375 void wxWindow::SetFocus()
380 void wxWindow::DoCaptureMouse()
385 void wxWindow::DoReleaseMouse()
390 void wxWindow::DoScreenToClient(int *x, int *y) const
395 void wxWindow::DoClientToScreen(int *x, int *y) const
400 // Get size *available for subwindows* i.e. excluding menu bar etc.
401 void wxWindow::DoGetClientSize(int *x, int *y) const
403 wxLogDebug("DoGetClientSize:");
404 wxWindowCocoa::DoGetSize(x,y);
405 // TODO: Actually account for menubar, borders, etc...
408 void wxWindow::DoSetClientSize(int width, int height)
410 wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
414 int wxWindow::GetCharHeight() const
420 int wxWindow::GetCharWidth() const
426 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
427 int *descent, int *externalLeading, const wxFont *theFont) const
432 // Coordinates relative to the window
433 void wxWindow::WarpPointer (int x_pos, int y_pos)
438 int wxWindow::GetScrollPos(int orient) const
444 // This now returns the whole range, not just the number
445 // of positions that we can scroll.
446 int wxWindow::GetScrollRange(int orient) const
452 int wxWindow::GetScrollThumb(int orient) const
458 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
463 // New function that will replace some of the above.
464 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
465 int range, bool refresh)
470 // Does a physical scroll
471 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
476 bool wxWindow::SetFont(const wxFont& font)
482 void wxWindow::Clear()
487 // Raise the window to the top of the Z order
488 void wxWindow::Raise()
490 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
491 NSView *superview = [nsview superview];
493 [nsview removeFromSuperview];
494 [superview addSubview:nsview];
497 // Lower the window to the bottom of the Z order
498 void wxWindow::Lower()
503 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
508 // Get the window with the focus
509 wxWindow *wxWindowBase::FindFocus()
515 /* static */ wxWindow *wxWindowBase::GetCapture()
521 wxWindow *wxGetActiveWindow()