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 [child->m_cocoaNSView retain];
84 // NOTE: addSubView takes ownership of, but does not retain the subview
85 // Upon a removeFromView or closing the super view, the child WILL be
86 // released!!! I think the idea here is that normally you would alloc
87 // the subview and add it to the superview and this way you don't have
88 // to release what you just alloced. Unfortunately, that doesn't
89 // make sense for wxCocoa, so we do this instead.
90 [m_cocoaNSView addSubview: child->m_cocoaNSView];
91 wxASSERT(!child->m_dummyNSView);
92 child->m_isShown = true;
95 void wxWindowCocoa::CocoaRemoveFromParent(void)
99 wxASSERT(m_cocoaNSView);
100 // balances the alloc
101 [m_dummyNSView removeFromSuperview];
102 // But since we also retained it ourselves
103 [m_dummyNSView release];
105 // m_cocoaNSView has of course already been removed by virtue of
106 // replaceSubview: m_cocoaNSView with: m_dummyNSView
109 [m_cocoaNSView removeFromSuperview];
112 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
114 bool need_debug = cocoaNSView || m_cocoaNSView;
115 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [m_cocoaNSView=%p retainCount]=%d",this,m_cocoaNSView,[m_cocoaNSView retainCount]);
116 DisassociateNSView(m_cocoaNSView);
117 [cocoaNSView retain];
118 [m_cocoaNSView release];
119 m_cocoaNSView = cocoaNSView;
120 AssociateNSView(m_cocoaNSView);
121 if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d",this,cocoaNSView,[cocoaNSView retainCount]);
124 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
126 wxLogDebug("Cocoa_drawRect");
127 // Recursion can happen if the event loop runs from within the paint
128 // handler. For instance, if an assertion dialog is shown.
129 // FIXME: This seems less than ideal.
132 wxLogDebug("Paint event recursion!");
135 //FIXME: should probably turn that rect into the update region
137 wxPaintEvent event(m_windowId);
138 event.SetEventObject(this);
139 bool ret = GetEventHandler()->ProcessEvent(event);
144 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
146 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
147 wxASSERT_MSG([nsview window]==[cocoaEvent window],"Mouse event for different NSWindow");
148 NSPoint cocoaPoint = [nsview convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
149 NSRect cocoaRect = [nsview frame];
150 const wxPoint &clientorigin = GetClientAreaOrigin();
151 event.m_x = (wxCoord)cocoaPoint.x - clientorigin.x;
152 event.m_y = (wxCoord)(cocoaRect.size.height - cocoaPoint.y) - clientorigin.y;
154 event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
155 event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
156 event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
157 event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
159 // TODO: set timestamp?
160 event.SetEventObject(this);
161 event.SetId(GetId());
164 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
166 wxMouseEvent event(wxEVT_MOTION);
167 InitMouseEvent(event,theEvent);
168 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
169 return GetEventHandler()->ProcessEvent(event);
172 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
177 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
182 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
184 wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
185 InitMouseEvent(event,theEvent);
186 wxLogDebug("Mouse Down @%d,%d num clicks=%d",event.m_x,event.m_y,[theEvent clickCount]);
187 return GetEventHandler()->ProcessEvent(event);
190 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
192 wxMouseEvent event(wxEVT_MOTION);
193 InitMouseEvent(event,theEvent);
194 event.m_leftDown = true;
195 wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
196 return GetEventHandler()->ProcessEvent(event);
199 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
201 wxMouseEvent event(wxEVT_LEFT_UP);
202 InitMouseEvent(event,theEvent);
203 wxLogDebug("Mouse Up @%d,%d",event.m_x,event.m_y);
204 return GetEventHandler()->ProcessEvent(event);
207 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
212 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
217 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
222 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
227 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
232 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
237 void wxWindowCocoa::Cocoa_FrameChanged(void)
239 wxLogDebug("Cocoa_FrameChanged");
240 wxSizeEvent event(GetSize(), m_windowId);
241 event.SetEventObject(this);
242 GetEventHandler()->ProcessEvent(event);
245 bool wxWindow::Close(bool force)
250 bool wxWindow::Show(bool show)
252 wxAutoNSAutoreleasePool pool;
253 // If the window is marked as visible, then it shouldn't have a dummy view
254 // If the window is marked hidden, then it should have a dummy view
255 // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
256 // wxASSERT_MSG( (m_isShown && !m_dummyNSView) || (!m_isShown && m_dummyNSView),"wxWindow: m_isShown does not agree with m_dummyNSView");
257 // Return false if there isn't a window to show or hide
262 // If state isn't changing, return false
265 // replaceSubView releases m_dummyNSView, balancing the alloc
266 [m_cocoaNSView retain];
267 [[m_dummyNSView superview] replaceSubview:m_dummyNSView with:m_cocoaNSView];
268 // But since we also retained it ourselves
269 wxASSERT(![m_dummyNSView superview]);
270 [m_dummyNSView release];
272 wxASSERT([m_cocoaNSView superview]);
276 // If state isn't changing, return false
279 m_dummyNSView = [[NSView alloc] initWithFrame: [m_cocoaNSView frame]];
280 [m_dummyNSView retain];
281 // NOTE: replaceSubView will cause m_cocaNSView to be released
282 [[m_cocoaNSView superview] replaceSubview:m_cocoaNSView with:m_dummyNSView];
283 // m_coocaNSView is now only retained by us
284 wxASSERT([m_dummyNSView superview]);
285 wxASSERT(![m_cocoaNSView superview]);
291 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
293 // 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":".");
294 int currentX, currentY;
295 int currentW, currentH;
296 DoGetPosition(¤tX, ¤tY);
297 DoGetSize(¤tW, ¤tH);
298 if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
300 if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
303 AdjustForParentClientOrigin(x,y,sizeFlags);
307 if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
309 if(sizeFlags&wxSIZE_AUTO_WIDTH)
311 size=DoGetBestSize();
317 if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
319 if(sizeFlags&wxSIZE_AUTO_HEIGHT)
322 size=DoGetBestSize();
328 DoMoveWindow(x,y,width,height);
331 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
333 // wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
335 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
336 NSView *superview = [nsview superview];
337 wxCHECK_RET(superview,"NSView does not have a superview");
338 NSRect parentRect = [superview frame];
340 NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
341 [m_cocoaNSView setFrame: cocoaRect];
342 // Also change the dummy's size
344 [m_dummyNSView setFrame: cocoaRect];
348 void wxWindow::DoGetSize(int *w, int *h) const
350 NSRect cocoaRect = [m_cocoaNSView frame];
352 *w=(int)cocoaRect.size.width;
354 *h=(int)cocoaRect.size.height;
355 // wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
358 void wxWindow::DoGetPosition(int *x, int *y) const
360 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
361 NSView *superview = [nsview superview];
362 wxCHECK_RET(superview,"NSView does not have a superview");
363 NSRect parentRect = [superview frame];
365 NSRect cocoaRect = [nsview frame];
367 *x=(int)cocoaRect.origin.x;
369 *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
370 // wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
373 WXWidget wxWindow::GetHandle() const
375 return m_cocoaNSView;
378 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
380 [m_cocoaNSView setNeedsDisplay:YES];
383 void wxWindow::SetFocus()
388 void wxWindow::DoCaptureMouse()
391 sm_capturedWindow = this;
394 void wxWindow::DoReleaseMouse()
397 sm_capturedWindow = NULL;
400 void wxWindow::DoScreenToClient(int *x, int *y) const
405 void wxWindow::DoClientToScreen(int *x, int *y) const
410 // Get size *available for subwindows* i.e. excluding menu bar etc.
411 void wxWindow::DoGetClientSize(int *x, int *y) const
413 wxLogDebug("DoGetClientSize:");
414 wxWindowCocoa::DoGetSize(x,y);
415 // TODO: Actually account for menubar, borders, etc...
418 void wxWindow::DoSetClientSize(int width, int height)
420 wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
424 int wxWindow::GetCharHeight() const
430 int wxWindow::GetCharWidth() const
436 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
437 int *descent, int *externalLeading, const wxFont *theFont) const
442 // Coordinates relative to the window
443 void wxWindow::WarpPointer (int x_pos, int y_pos)
448 int wxWindow::GetScrollPos(int orient) const
454 // This now returns the whole range, not just the number
455 // of positions that we can scroll.
456 int wxWindow::GetScrollRange(int orient) const
462 int wxWindow::GetScrollThumb(int orient) const
468 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
473 // New function that will replace some of the above.
474 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
475 int range, bool refresh)
480 // Does a physical scroll
481 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
486 bool wxWindow::SetFont(const wxFont& font)
492 void wxWindow::Clear()
497 // Raise the window to the top of the Z order
498 void wxWindow::Raise()
500 wxAutoNSAutoreleasePool pool;
501 NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
502 NSView *superview = [nsview superview];
504 [nsview removeFromSuperview];
505 [superview addSubview:nsview];
508 // Lower the window to the bottom of the Z order
509 void wxWindow::Lower()
514 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
519 // Get the window with the focus
520 wxWindow *wxWindowBase::FindFocus()
526 /* static */ wxWindow *wxWindowBase::GetCapture()
529 return wxWindowCocoa::sm_capturedWindow;
532 wxWindow *wxGetActiveWindow()
538 wxPoint wxGetMousePosition()
541 return wxDefaultPosition;
544 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
546 pt = wxGetMousePosition();