Associate/Disassociate now check for non-NULL pointer instead of requiring
[wxWidgets.git] / src / cocoa / window.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/window.mm
3 // Purpose:     wxWindowCocoa
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2002/12/26
7 // RCS-ID:      $Id:
8 // Copyright:   (c) 2002 David Elliott
9 // Licence:     wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/window.h"
13 #include "wx/log.h"
14
15 #import <Appkit/NSView.h>
16 #import <AppKit/NSEvent.h>
17
18 // normally the base classes aren't included, but wxWindow is special
19 #ifdef __WXUNIVERSAL__
20 IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
21 #else
22 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
23 #endif
24
25 BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
26 END_EVENT_TABLE()
27
28 // Constructor
29 void wxWindowCocoa::Init()
30 {
31     InitBase();
32
33     m_cocoaNSView = NULL;
34     m_dummyNSView = NULL;
35     m_isBeingDeleted = FALSE;
36     m_isInPaint = FALSE;
37 }
38
39 // Constructor
40 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
41            const wxPoint& pos,
42            const wxSize& size,
43            long style,
44            const wxString& name)
45 {
46     if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
47         return false;
48
49     // TODO: create the window
50     NSRect cocoaRect = NSMakeRect(10,10,20,20);
51     m_cocoaNSView = NULL;
52     SetNSView([[NSView alloc] initWithFrame: cocoaRect]);
53     [m_cocoaNSView release];
54
55     if (m_parent)
56     {
57         m_parent->AddChild(this);
58         m_parent->CocoaAddChild(this);
59     }
60
61     return TRUE;
62 }
63
64 // Destructor
65 wxWindow::~wxWindow()
66 {
67     DestroyChildren();
68
69     if(m_parent)
70         m_parent->RemoveChild(this);
71
72     CocoaRemoveFromParent();
73     SetNSView(NULL);
74 }
75
76 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
77 {
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;
88 }
89
90 void wxWindowCocoa::CocoaRemoveFromParent(void)
91 {
92     if(m_dummyNSView)
93     {
94         wxASSERT(m_cocoaNSView);
95         // balances the alloc
96         [m_dummyNSView removeFromSuperview];
97         // But since we also retained it ourselves
98         [m_dummyNSView release];
99         m_dummyNSView = nil;
100         // m_cocoaNSView has of course already been removed by virtue of
101         // replaceSubview: m_cocoaNSView with: m_dummyNSView
102     }
103     else
104         [m_cocoaNSView removeFromSuperview];
105 }
106
107 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
108 {
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]);
117 }
118
119 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
120 {
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.
125     if(m_isInPaint)
126     {
127         wxLogDebug("Paint event recursion!");
128         return false;
129     }
130     //FIXME: should probably turn that rect into the update region
131     m_isInPaint = TRUE;
132     wxPaintEvent event(m_windowId);
133     event.SetEventObject(this);
134     bool ret = GetEventHandler()->ProcessEvent(event);
135     m_isInPaint = FALSE;
136     return ret;
137 }
138
139 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
140 {
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;
148
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;
153
154     // TODO: set timestamp?
155     event.SetEventObject(this);
156     event.SetId(GetId());
157 }
158
159 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
160 {
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);
165 }
166
167 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
168 {
169     return false;
170 }
171
172 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
173 {
174     return false;
175 }
176
177 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
178 {
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);
183 }
184
185 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
186 {
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);
192 }
193
194 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
195 {
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);
200 }
201
202 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
203 {
204     return false;
205 }
206
207 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
208 {
209     return false;
210 }
211
212 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
213 {
214     return false;
215 }
216
217 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
218 {
219     return false;
220 }
221
222 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
223 {
224     return false;
225 }
226
227 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
228 {
229     return false;
230 }
231
232 void wxWindowCocoa::Cocoa_FrameChanged(void)
233 {
234     wxLogDebug("Cocoa_FrameChanged");
235     wxSizeEvent event(GetSize(), m_windowId);
236     event.SetEventObject(this);
237     GetEventHandler()->ProcessEvent(event);
238 }
239
240 bool wxWindow::Close(bool force)
241 {
242     return false;
243 }
244
245 bool wxWindow::Show(bool show)
246 {
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
251     if(!m_cocoaNSView)
252         return false;
253     // Return false if the state isn't changing
254     if( show == m_isShown )
255         return false;
256     if(show)
257     {
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];
264         m_dummyNSView = nil;
265         wxASSERT([m_cocoaNSView superview]);
266     }
267     else
268     {
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]);
276     }
277     m_isShown = show;
278     return true;
279 }
280
281 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
282 {
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(&currentX, &currentY);
287     DoGetSize(&currentW, &currentH);
288     if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
289         x=currentX;
290     if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
291         y=currentY;
292
293     AdjustForParentClientOrigin(x,y,sizeFlags);
294
295     wxSize size(-1,-1);
296
297     if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
298     {
299         if(sizeFlags&wxSIZE_AUTO_WIDTH)
300         {
301             size=DoGetBestSize();
302             width=size.x;
303         }
304         else
305             width=currentW;
306     }
307     if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
308     {
309         if(sizeFlags&wxSIZE_AUTO_HEIGHT)
310         {
311             if(size.x==-1)
312                 size=DoGetBestSize();
313             height=size.y;
314         }
315         else
316             height=currentH;
317     }
318     DoMoveWindow(x,y,width,height);
319 }
320
321 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
322 {
323 //    wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
324
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];
329
330     NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
331     [m_cocoaNSView setFrame: cocoaRect];
332     // Also change the dummy's size
333     if(m_dummyNSView)
334         [m_dummyNSView setFrame: cocoaRect];
335 }
336
337 // Get total size
338 void wxWindow::DoGetSize(int *w, int *h) const
339 {
340     NSRect cocoaRect = [m_cocoaNSView frame];
341     if(w)
342         *w=(int)cocoaRect.size.width;
343     if(h)
344         *h=(int)cocoaRect.size.height;
345 //    wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
346 }
347
348 void wxWindow::DoGetPosition(int *x, int *y) const
349 {
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];
354
355     NSRect cocoaRect = [nsview frame];
356     if(x)
357         *x=(int)cocoaRect.origin.x;
358     if(y)
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);
361 }
362
363 WXWidget wxWindow::GetHandle() const
364 {
365     return m_cocoaNSView;
366 }
367
368 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
369 {
370     [m_cocoaNSView setNeedsDisplay:YES];
371 }
372
373 void wxWindow::SetFocus()
374 {
375     // TODO
376 }
377
378 void wxWindow::DoCaptureMouse()
379 {
380     // TODO
381 }
382
383 void wxWindow::DoReleaseMouse()
384 {
385     // TODO
386 }
387
388 void wxWindow::DoScreenToClient(int *x, int *y) const
389 {
390     // TODO
391 }
392
393 void wxWindow::DoClientToScreen(int *x, int *y) const
394 {
395     // TODO
396 }
397
398 // Get size *available for subwindows* i.e. excluding menu bar etc.
399 void wxWindow::DoGetClientSize(int *x, int *y) const
400 {
401     wxLogDebug("DoGetClientSize:");
402     wxWindowCocoa::DoGetSize(x,y);
403     // TODO: Actually account for menubar, borders, etc...
404 }
405
406 void wxWindow::DoSetClientSize(int width, int height)
407 {
408     wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
409     // TODO
410 }
411
412 int wxWindow::GetCharHeight() const
413 {
414     // TODO
415     return 0;
416 }
417
418 int wxWindow::GetCharWidth() const
419 {
420     // TODO
421     return 0;
422 }
423
424 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
425         int *descent, int *externalLeading, const wxFont *theFont) const
426 {
427     // TODO
428 }
429
430 // Coordinates relative to the window
431 void wxWindow::WarpPointer (int x_pos, int y_pos)
432 {
433     // TODO
434 }
435
436 int wxWindow::GetScrollPos(int orient) const
437 {
438     // TODO
439     return 0;
440 }
441
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
445 {
446     // TODO
447     return 0;
448 }
449
450 int wxWindow::GetScrollThumb(int orient) const
451 {
452     // TODO
453     return 0;
454 }
455
456 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
457 {
458     // TODO
459 }
460
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)
464 {
465     // TODO
466 }
467
468 // Does a physical scroll
469 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
470 {
471     // TODO
472 }
473
474 bool wxWindow::SetFont(const wxFont& font)
475 {
476     // TODO
477     return TRUE;
478 }
479
480 void wxWindow::Clear()
481 {
482     // TODO
483 }
484
485 // Raise the window to the top of the Z order
486 void wxWindow::Raise()
487 {
488     NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
489     NSView *superview = [nsview superview];
490     [nsview retain];
491     [nsview removeFromSuperview];
492     [superview addSubview:nsview];
493 }
494
495 // Lower the window to the bottom of the Z order
496 void wxWindow::Lower()
497 {
498     // TODO
499 }
500
501 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
502 {
503     return FALSE;
504 }
505
506 // Get the window with the focus
507 wxWindow *wxWindowBase::FindFocus()
508 {
509     // TODO
510     return NULL;
511 }
512
513 /* static */ wxWindow *wxWindowBase::GetCapture()
514 {
515     // TODO
516     return NULL;
517 }
518
519 wxWindow *wxGetActiveWindow()
520 {
521     // TODO
522     return NULL;
523 }
524