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]);
111 DisassociateNSView(m_cocoaNSView);
112 [cocoaNSView retain];
113 [m_cocoaNSView release];
114 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 // Recursion can happen if the event loop runs from within the paint
123 // handler. For instance, if an assertion dialog is shown.
124 // FIXME: This seems less than ideal.
127 wxLogDebug("Paint event recursion!");
130 //FIXME: should probably turn that rect into the update region
132 wxPaintEvent event(m_windowId);
133 event.SetEventObject(this);
134 bool ret = GetEventHandler()->ProcessEvent(event);
139 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
141 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
142 wxASSERT_MSG([nsview window]==[cocoaEvent window],"Mouse event for different NSWindow");
143 NSPoint cocoaPoint = [nsview convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
144 NSRect cocoaRect = [nsview frame];
145 const wxPoint &clientorigin = GetClientAreaOrigin();
146 event.m_x = (wxCoord)cocoaPoint.x - clientorigin.x;
147 event.m_y = (wxCoord)(cocoaRect.size.height - cocoaPoint.y) - clientorigin.y;
149 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
150 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
151 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
152 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
154 // TODO: set timestamp?
155 event.SetEventObject(this);
156 event.SetId(GetId());
159 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
161 wxMouseEvent event(wxEVT_MOTION);
162 InitMouseEvent(event,theEvent);
163 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
164 return GetEventHandler()->ProcessEvent(event);
167 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
172 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
177 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
179 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
180 InitMouseEvent(event,theEvent);
181 wxLogDebug("Mouse Down @%d,%d num clicks=%d",event.m_x,event.m_y,[theEvent clickCount]);
182 return GetEventHandler()->ProcessEvent(event);
185 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
187 wxMouseEvent event(wxEVT_MOTION);
188 InitMouseEvent(event,theEvent);
189 event.m_leftDown = true;
190 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
191 return GetEventHandler()->ProcessEvent(event);
194 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
196 wxMouseEvent event(wxEVT_LEFT_UP);
197 InitMouseEvent(event,theEvent);
198 wxLogDebug("Mouse Up @%d,%d",event.m_x,event.m_y);
199 return GetEventHandler()->ProcessEvent(event);
202 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
207 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
212 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
217 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
222 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
227 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
232 void wxWindowCocoa::Cocoa_FrameChanged(void)
234 wxLogDebug("Cocoa_FrameChanged");
235 wxSizeEvent event(GetSize(), m_windowId);
236 event.SetEventObject(this);
237 GetEventHandler()->ProcessEvent(event);
240 bool wxWindow::Close(bool force)
245 bool wxWindow::Show(bool show)
247 // If the window is marked as visible, then it shouldn't have a dummy view
248 // If the window is marked hidden, then it should have a dummy view
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
253 // Return false if the state isn't changing
254 if( show == m_isShown )
258 // replaceSubView releases m_dummyNSView, balancing the alloc
259 [m_cocoaNSView retain];
260 [[m_dummyNSView superview] replaceSubview:m_dummyNSView with:m_cocoaNSView];
261 // But since we also retained it ourselves
262 wxASSERT(![m_dummyNSView superview]);
263 [m_dummyNSView release];
265 wxASSERT([m_cocoaNSView superview]);
269 m_dummyNSView = [[NSView alloc] initWithFrame: [m_cocoaNSView frame]];
270 [m_dummyNSView retain];
271 // NOTE: replaceSubView will cause m_cocaNSView to be released
272 [[m_cocoaNSView superview] replaceSubview:m_cocoaNSView with:m_dummyNSView];
273 // m_coocaNSView is now only retained by us
274 wxASSERT([m_dummyNSView superview]);
275 wxASSERT(![m_cocoaNSView superview]);
281 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
283 // 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":".");
284 int currentX, currentY;
285 int currentW, currentH;
286 DoGetPosition(¤tX, ¤tY);
287 DoGetSize(¤tW, ¤tH);
288 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
290 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
293 AdjustForParentClientOrigin(x,y,sizeFlags);
297 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
299 if(sizeFlags&wxSIZE_AUTO_WIDTH)
301 size=DoGetBestSize();
307 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
309 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
312 size=DoGetBestSize();
318 DoMoveWindow(x,y,width,height);
321 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
323 // wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
325 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
326 NSView *superview = [nsview superview];
327 wxCHECK_RET(superview,"NSView does not have a superview");
328 NSRect parentRect = [superview frame];
330 NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
331 [m_cocoaNSView setFrame: cocoaRect];
332 // Also change the dummy's size
334 [m_dummyNSView setFrame: cocoaRect];
338 void wxWindow::DoGetSize(int *w, int *h) const
340 NSRect cocoaRect = [m_cocoaNSView frame];
342 *w=(int)cocoaRect.size.width;
344 *h=(int)cocoaRect.size.height;
345 // wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
348 void wxWindow::DoGetPosition(int *x, int *y) const
350 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
351 NSView *superview = [nsview superview];
352 wxCHECK_RET(superview,"NSView does not have a superview");
353 NSRect parentRect = [superview frame];
355 NSRect cocoaRect = [nsview frame];
357 *x=(int)cocoaRect.origin.x;
359 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
360 // wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
363 WXWidget wxWindow::GetHandle() const
365 return m_cocoaNSView;
368 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
370 [m_cocoaNSView setNeedsDisplay:YES];
373 void wxWindow::SetFocus()
378 void wxWindow::DoCaptureMouse()
383 void wxWindow::DoReleaseMouse()
388 void wxWindow::DoScreenToClient(int *x, int *y) const
393 void wxWindow::DoClientToScreen(int *x, int *y) const
398 // Get size *available for subwindows* i.e. excluding menu bar etc.
399 void wxWindow::DoGetClientSize(int *x, int *y) const
401 wxLogDebug("DoGetClientSize:");
402 wxWindowCocoa::DoGetSize(x,y);
403 // TODO: Actually account for menubar, borders, etc...
406 void wxWindow::DoSetClientSize(int width, int height)
408 wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
412 int wxWindow::GetCharHeight() const
418 int wxWindow::GetCharWidth() const
424 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
425 int *descent, int *externalLeading, const wxFont *theFont) const
430 // Coordinates relative to the window
431 void wxWindow::WarpPointer (int x_pos, int y_pos)
436 int wxWindow::GetScrollPos(int orient) const
442 // This now returns the whole range, not just the number
443 // of positions that we can scroll.
444 int wxWindow::GetScrollRange(int orient) const
450 int wxWindow::GetScrollThumb(int orient) const
456 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
461 // New function that will replace some of the above.
462 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
463 int range, bool refresh)
468 // Does a physical scroll
469 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
474 bool wxWindow::SetFont(const wxFont& font)
480 void wxWindow::Clear()
485 // Raise the window to the top of the Z order
486 void wxWindow::Raise()
488 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
489 NSView *superview = [nsview superview];
491 [nsview removeFromSuperview];
492 [superview addSubview:nsview];
495 // Lower the window to the bottom of the Z order
496 void wxWindow::Lower()
501 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
506 // Get the window with the focus
507 wxWindow *wxWindowBase::FindFocus()
513 /* static */ wxWindow *wxWindowBase::GetCapture()
519 wxWindow *wxGetActiveWindow()