Contrary to what I had thought, the addSubview and replaceSubview:with:
[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 #include "wx/cocoa/autorelease.h"
16
17 #import <Appkit/NSView.h>
18 #import <AppKit/NSEvent.h>
19
20 // normally the base classes aren't included, but wxWindow is special
21 #ifdef __WXUNIVERSAL__
22 IMPLEMENT_ABSTRACT_CLASS(wxWindowCocoa, wxWindowBase)
23 #else
24 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
25 #endif
26
27 BEGIN_EVENT_TABLE(wxWindowCocoa, wxWindowBase)
28 END_EVENT_TABLE()
29
30 wxWindow *wxWindowCocoa::sm_capturedWindow = NULL;
31
32 // Constructor
33 void wxWindowCocoa::Init()
34 {
35     InitBase();
36
37     m_cocoaNSView = NULL;
38     m_dummyNSView = NULL;
39     m_isBeingDeleted = FALSE;
40     m_isInPaint = FALSE;
41 }
42
43 // Constructor
44 bool wxWindow::Create(wxWindow *parent, wxWindowID winid,
45            const wxPoint& pos,
46            const wxSize& size,
47            long style,
48            const wxString& name)
49 {
50     if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
51         return false;
52
53     // TODO: create the window
54     NSRect cocoaRect = NSMakeRect(10,10,20,20);
55     m_cocoaNSView = NULL;
56     SetNSView([[NSView alloc] initWithFrame: cocoaRect]);
57     [m_cocoaNSView release];
58
59     if (m_parent)
60     {
61         m_parent->AddChild(this);
62         m_parent->CocoaAddChild(this);
63     }
64
65     return TRUE;
66 }
67
68 // Destructor
69 wxWindow::~wxWindow()
70 {
71     wxAutoNSAutoreleasePool pool;
72     DestroyChildren();
73
74     if(m_parent)
75         m_parent->RemoveChild(this);
76
77     CocoaRemoveFromParent();
78     SetNSView(NULL);
79 }
80
81 void wxWindowCocoa::CocoaAddChild(wxWindowCocoa *child)
82 {
83     [m_cocoaNSView addSubview: child->m_cocoaNSView];
84     wxASSERT(!child->m_dummyNSView);
85     child->m_isShown = true;
86 }
87
88 void wxWindowCocoa::CocoaRemoveFromParent(void)
89 {
90     if(m_dummyNSView)
91     {
92         wxASSERT(m_cocoaNSView);
93         // balances the replaceSubView:with:
94         [m_dummyNSView removeFromSuperview];
95         // But since we were the ones to alloc it
96         [m_dummyNSView release];
97         m_dummyNSView = nil;
98         // m_cocoaNSView has of course already been removed by virtue of
99         // replaceSubview: m_cocoaNSView with: m_dummyNSView
100     }
101     else
102         [m_cocoaNSView removeFromSuperview];
103 }
104
105 void wxWindowCocoa::SetNSView(WX_NSView cocoaNSView)
106 {
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]);
109     DisassociateNSView(m_cocoaNSView);
110     [cocoaNSView retain];
111     [m_cocoaNSView release];
112     m_cocoaNSView = cocoaNSView;
113     AssociateNSView(m_cocoaNSView);
114     if(need_debug) wxLogDebug("wxWindowCocoa=%p::SetNSView [cocoaNSView=%p retainCount]=%d",this,cocoaNSView,[cocoaNSView retainCount]);
115 }
116
117 bool wxWindowCocoa::Cocoa_drawRect(const NSRect &rect)
118 {
119     wxLogDebug("Cocoa_drawRect");
120     // Recursion can happen if the event loop runs from within the paint
121     // handler.  For instance, if an assertion dialog is shown.
122     // FIXME: This seems less than ideal.
123     if(m_isInPaint)
124     {
125         wxLogDebug("Paint event recursion!");
126         return false;
127     }
128     //FIXME: should probably turn that rect into the update region
129     m_isInPaint = TRUE;
130     wxPaintEvent event(m_windowId);
131     event.SetEventObject(this);
132     bool ret = GetEventHandler()->ProcessEvent(event);
133     m_isInPaint = FALSE;
134     return ret;
135 }
136
137 void wxWindowCocoa::InitMouseEvent(wxMouseEvent& event, WX_NSEvent cocoaEvent)
138 {
139     NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
140     wxASSERT_MSG([nsview window]==[cocoaEvent window],"Mouse event for different NSWindow");
141     NSPoint cocoaPoint = [nsview convertPoint:[(NSEvent*)cocoaEvent locationInWindow] fromView:nil];
142     NSRect cocoaRect = [nsview frame];
143     const wxPoint &clientorigin = GetClientAreaOrigin();
144     event.m_x = (wxCoord)cocoaPoint.x - clientorigin.x;
145     event.m_y = (wxCoord)(cocoaRect.size.height - cocoaPoint.y) - clientorigin.y;
146
147     event.m_shiftDown = [cocoaEvent modifierFlags] & NSShiftKeyMask;
148     event.m_controlDown = [cocoaEvent modifierFlags] & NSControlKeyMask;
149     event.m_altDown = [cocoaEvent modifierFlags] & NSAlternateKeyMask;
150     event.m_metaDown = [cocoaEvent modifierFlags] & NSCommandKeyMask;
151
152     // TODO: set timestamp?
153     event.SetEventObject(this);
154     event.SetId(GetId());
155 }
156
157 bool wxWindowCocoa::Cocoa_mouseMoved(WX_NSEvent theEvent)
158 {
159     wxMouseEvent event(wxEVT_MOTION);
160     InitMouseEvent(event,theEvent);
161     wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
162     return GetEventHandler()->ProcessEvent(event);
163 }
164
165 bool wxWindowCocoa::Cocoa_mouseEntered(WX_NSEvent theEvent)
166 {
167     return false;
168 }
169
170 bool wxWindowCocoa::Cocoa_mouseExited(WX_NSEvent theEvent)
171 {
172     return false;
173 }
174
175 bool wxWindowCocoa::Cocoa_mouseDown(WX_NSEvent theEvent)
176 {
177     wxMouseEvent event([theEvent clickCount]<2?wxEVT_LEFT_DOWN:wxEVT_LEFT_DCLICK);
178     InitMouseEvent(event,theEvent);
179     wxLogDebug("Mouse Down @%d,%d num clicks=%d",event.m_x,event.m_y,[theEvent clickCount]);
180     return GetEventHandler()->ProcessEvent(event);
181 }
182
183 bool wxWindowCocoa::Cocoa_mouseDragged(WX_NSEvent theEvent)
184 {
185     wxMouseEvent event(wxEVT_MOTION);
186     InitMouseEvent(event,theEvent);
187     event.m_leftDown = true;
188     wxLogDebug("Mouse Drag @%d,%d",event.m_x,event.m_y);
189     return GetEventHandler()->ProcessEvent(event);
190 }
191
192 bool wxWindowCocoa::Cocoa_mouseUp(WX_NSEvent theEvent)
193 {
194     wxMouseEvent event(wxEVT_LEFT_UP);
195     InitMouseEvent(event,theEvent);
196     wxLogDebug("Mouse Up @%d,%d",event.m_x,event.m_y);
197     return GetEventHandler()->ProcessEvent(event);
198 }
199
200 bool wxWindowCocoa::Cocoa_rightMouseDown(WX_NSEvent theEvent)
201 {
202     return false;
203 }
204
205 bool wxWindowCocoa::Cocoa_rightMouseDragged(WX_NSEvent theEvent)
206 {
207     return false;
208 }
209
210 bool wxWindowCocoa::Cocoa_rightMouseUp(WX_NSEvent theEvent)
211 {
212     return false;
213 }
214
215 bool wxWindowCocoa::Cocoa_otherMouseDown(WX_NSEvent theEvent)
216 {
217     return false;
218 }
219
220 bool wxWindowCocoa::Cocoa_otherMouseDragged(WX_NSEvent theEvent)
221 {
222     return false;
223 }
224
225 bool wxWindowCocoa::Cocoa_otherMouseUp(WX_NSEvent theEvent)
226 {
227     return false;
228 }
229
230 void wxWindowCocoa::Cocoa_FrameChanged(void)
231 {
232     wxLogDebug("Cocoa_FrameChanged");
233     wxSizeEvent event(GetSize(), m_windowId);
234     event.SetEventObject(this);
235     GetEventHandler()->ProcessEvent(event);
236 }
237
238 bool wxWindow::Close(bool force)
239 {
240     return false;
241 }
242
243 bool wxWindow::Show(bool show)
244 {
245     wxAutoNSAutoreleasePool pool;
246     // If the window is marked as visible, then it shouldn't have a dummy view
247     // If the window is marked hidden, then it should have a dummy view
248     // wxSpinCtrl (generic) abuses m_isShown, don't use it for any logic
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     if(show)
254     {
255         // If state isn't changing, return false
256         if(!m_dummyNSView)
257             return false;
258         // replaceSubView:with: releases m_dummyNSView, balancing the
259         // previous replaceSubView:with
260         [[m_dummyNSView superview] replaceSubview:m_dummyNSView with:m_cocoaNSView];
261         wxASSERT(![m_dummyNSView superview]);
262         // But since we were the ones to alloc it
263         [m_dummyNSView release];
264         m_dummyNSView = nil;
265         wxASSERT([m_cocoaNSView superview]);
266     }
267     else
268     {
269         // If state isn't changing, return false
270         if(m_dummyNSView)
271             return false;
272         m_dummyNSView = [[NSView alloc] initWithFrame: [m_cocoaNSView frame]];
273         // NOTE: replaceSubView will cause m_cocaNSView to be (auto)released
274         // which balances out addSubView
275         [[m_cocoaNSView superview] replaceSubview:m_cocoaNSView with:m_dummyNSView];
276         // m_coocaNSView is now only retained by us
277         wxASSERT([m_dummyNSView superview]);
278         wxASSERT(![m_cocoaNSView superview]);
279     }
280     m_isShown = show;
281     return true;
282 }
283
284 void wxWindowCocoa::DoSetSize(int x, int y, int width, int height, int sizeFlags)
285 {
286 //    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":".");
287     int currentX, currentY;
288     int currentW, currentH;
289     DoGetPosition(&currentX, &currentY);
290     DoGetSize(&currentW, &currentH);
291     if((x==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
292         x=currentX;
293     if((y==-1) && !(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
294         y=currentY;
295
296     AdjustForParentClientOrigin(x,y,sizeFlags);
297
298     wxSize size(-1,-1);
299
300     if((width==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
301     {
302         if(sizeFlags&wxSIZE_AUTO_WIDTH)
303         {
304             size=DoGetBestSize();
305             width=size.x;
306         }
307         else
308             width=currentW;
309     }
310     if((height==-1)&&!(sizeFlags&wxSIZE_ALLOW_MINUS_ONE))
311     {
312         if(sizeFlags&wxSIZE_AUTO_HEIGHT)
313         {
314             if(size.x==-1)
315                 size=DoGetBestSize();
316             height=size.y;
317         }
318         else
319             height=currentH;
320     }
321     DoMoveWindow(x,y,width,height);
322 }
323
324 void wxWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
325 {
326 //    wxLogDebug("wxWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
327
328     NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
329     NSView *superview = [nsview superview];
330     wxCHECK_RET(superview,"NSView does not have a superview");
331     NSRect parentRect = [superview frame];
332
333     NSRect cocoaRect = NSMakeRect(x,parentRect.size.height-(y+height),width,height);
334     [m_cocoaNSView setFrame: cocoaRect];
335     // Also change the dummy's size
336     if(m_dummyNSView)
337         [m_dummyNSView setFrame: cocoaRect];
338 }
339
340 // Get total size
341 void wxWindow::DoGetSize(int *w, int *h) const
342 {
343     NSRect cocoaRect = [m_cocoaNSView frame];
344     if(w)
345         *w=(int)cocoaRect.size.width;
346     if(h)
347         *h=(int)cocoaRect.size.height;
348 //    wxLogDebug("wxWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
349 }
350
351 void wxWindow::DoGetPosition(int *x, int *y) const
352 {
353     NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
354     NSView *superview = [nsview superview];
355     wxCHECK_RET(superview,"NSView does not have a superview");
356     NSRect parentRect = [superview frame];
357
358     NSRect cocoaRect = [nsview frame];
359     if(x)
360         *x=(int)cocoaRect.origin.x;
361     if(y)
362         *y=(int)(parentRect.size.height-(cocoaRect.origin.y+cocoaRect.size.height));
363 //    wxLogDebug("wxWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
364 }
365
366 WXWidget wxWindow::GetHandle() const
367 {
368     return m_cocoaNSView;
369 }
370
371 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
372 {
373     [m_cocoaNSView setNeedsDisplay:YES];
374 }
375
376 void wxWindow::SetFocus()
377 {
378     // TODO
379 }
380
381 void wxWindow::DoCaptureMouse()
382 {
383     // TODO
384     sm_capturedWindow = this;
385 }
386
387 void wxWindow::DoReleaseMouse()
388 {
389     // TODO
390     sm_capturedWindow = NULL;
391 }
392
393 void wxWindow::DoScreenToClient(int *x, int *y) const
394 {
395     // TODO
396 }
397
398 void wxWindow::DoClientToScreen(int *x, int *y) const
399 {
400     // TODO
401 }
402
403 // Get size *available for subwindows* i.e. excluding menu bar etc.
404 void wxWindow::DoGetClientSize(int *x, int *y) const
405 {
406     wxLogDebug("DoGetClientSize:");
407     wxWindowCocoa::DoGetSize(x,y);
408     // TODO: Actually account for menubar, borders, etc...
409 }
410
411 void wxWindow::DoSetClientSize(int width, int height)
412 {
413     wxLogDebug("DoSetClientSize=(%d,%d)",width,height);
414     // TODO
415 }
416
417 int wxWindow::GetCharHeight() const
418 {
419     // TODO
420     return 0;
421 }
422
423 int wxWindow::GetCharWidth() const
424 {
425     // TODO
426     return 0;
427 }
428
429 void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
430         int *descent, int *externalLeading, const wxFont *theFont) const
431 {
432     // TODO
433 }
434
435 // Coordinates relative to the window
436 void wxWindow::WarpPointer (int x_pos, int y_pos)
437 {
438     // TODO
439 }
440
441 int wxWindow::GetScrollPos(int orient) const
442 {
443     // TODO
444     return 0;
445 }
446
447 // This now returns the whole range, not just the number
448 // of positions that we can scroll.
449 int wxWindow::GetScrollRange(int orient) const
450 {
451     // TODO
452     return 0;
453 }
454
455 int wxWindow::GetScrollThumb(int orient) const
456 {
457     // TODO
458     return 0;
459 }
460
461 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
462 {
463     // TODO
464 }
465
466 // New function that will replace some of the above.
467 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
468     int range, bool refresh)
469 {
470     // TODO
471 }
472
473 // Does a physical scroll
474 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
475 {
476     // TODO
477 }
478
479 bool wxWindow::SetFont(const wxFont& font)
480 {
481     // TODO
482     return TRUE;
483 }
484
485 void wxWindow::Clear()
486 {
487     // TODO
488 }
489
490 // Raise the window to the top of the Z order
491 void wxWindow::Raise()
492 {
493     wxAutoNSAutoreleasePool pool;
494     NSView *nsview = m_dummyNSView?m_dummyNSView:m_cocoaNSView;
495     NSView *superview = [nsview superview];
496     [nsview removeFromSuperview];
497     [superview addSubview:nsview];
498 }
499
500 // Lower the window to the bottom of the Z order
501 void wxWindow::Lower()
502 {
503     // TODO
504 }
505
506 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
507 {
508     return FALSE;
509 }
510
511 // Get the window with the focus
512 wxWindow *wxWindowBase::FindFocus()
513 {
514     // TODO
515     return NULL;
516 }
517
518 /* static */ wxWindow *wxWindowBase::GetCapture()
519 {
520     // TODO
521     return wxWindowCocoa::sm_capturedWindow;
522 }
523
524 wxWindow *wxGetActiveWindow()
525 {
526     // TODO
527     return NULL;
528 }
529
530 wxPoint wxGetMousePosition()
531 {
532     // TODO
533     return wxDefaultPosition;
534 }
535
536 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
537 {
538     pt = wxGetMousePosition();
539     return NULL;
540 }
541