In destructor, pass the NSView ownership to the base class
[wxWidgets.git] / src / cocoa / toplevel.mm
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        cocoa/toplevel.mm
3 // Purpose:     implements wxTopLevelWindow for Cocoa
4 // Author:      David Elliott 
5 // Modified by:
6 // Created:     2002/11/27
7 // RCS-ID:      $Id:
8 // Copyright:   (c) 2002 David Elliott
9 // License:     wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22 #ifndef WX_PRECOMP
23     #include "wx/window.h"
24     #include "wx/toplevel.h"
25     #include "wx/menuitem.h"
26     #include "wx/frame.h"
27     #include "wx/log.h"
28     #include "wx/app.h"
29 #endif //WX_PRECOMP
30
31 #import <AppKit/NSView.h>
32 #import <AppKit/NSWindow.h>
33 // ----------------------------------------------------------------------------
34 // globals
35 // ----------------------------------------------------------------------------
36
37 // list of all frames and modeless dialogs
38 wxWindowList       wxModelessWindows;
39
40 // ============================================================================
41 // wxTopLevelWindowCocoa implementation
42 // ============================================================================
43
44 // ----------------------------------------------------------------------------
45 // wxTopLevelWindowCocoa creation
46 // ----------------------------------------------------------------------------
47 BEGIN_EVENT_TABLE(wxTopLevelWindowCocoa,wxTopLevelWindowBase)
48     EVT_CLOSE(wxTopLevelWindowCocoa::OnCloseWindow)
49 END_EVENT_TABLE()
50
51 void wxTopLevelWindowCocoa::Init()
52 {
53     m_iconized =
54     m_maximizeOnShow =
55     m_closed = false;
56 }
57
58 bool wxTopLevelWindowCocoa::Create(wxWindow *parent,
59                                  wxWindowID winid,
60                                  const wxString& title,
61                                  const wxPoint& pos,
62                                  const wxSize& size,
63                                  long style,
64                                  const wxString& name)
65 {
66     wxTopLevelWindows.Append(this);
67
68     if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
69         return FALSE;
70
71     if ( parent )
72         parent->AddChild(this);
73
74     // TODO: get rect from given position/size
75     NSRect cocoaRect =  NSMakeRect(100,100,200,200);
76
77     // TODO: Set flags given wxWindows style
78     unsigned int cocoaStyle = 0;
79     cocoaStyle |= NSTitledWindowMask;
80     cocoaStyle |= NSClosableWindowMask;
81     cocoaStyle |= NSMiniaturizableWindowMask;
82     cocoaStyle |= NSResizableWindowMask;
83
84     m_cocoaNSWindow = NULL;
85     m_cocoaNSView = NULL;
86     SetNSWindow([[NSWindow alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
87     // NOTE: SetNSWindow has retained the Cocoa object for this object.
88     // Because we do not release on close, the following release matches the
89     // above alloc and thus the retain count will be 1.
90     [m_cocoaNSWindow release];
91
92     return TRUE;
93 }
94
95 wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
96 {
97     // Hand ownership of the content view to wxWindow so it can destroy
98     // itself properly.
99     NSView *view = [m_cocoaNSView retain];
100     SetNSWindow(NULL);
101     SetNSView(view);
102     [view release];
103 }
104
105 // ----------------------------------------------------------------------------
106 // wxTopLevelWindowCocoa Cocoa Specifics
107 // ----------------------------------------------------------------------------
108
109 void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
110 {
111     bool need_debug = cocoaNSWindow || m_cocoaNSWindow;
112     if(need_debug) wxLogDebug("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d",this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]);
113     DisassociateNSWindow(m_cocoaNSWindow);
114     [cocoaNSWindow retain];
115     [m_cocoaNSWindow release];
116     m_cocoaNSWindow = cocoaNSWindow;
117     if(m_cocoaNSWindow)
118         SetNSView([m_cocoaNSWindow contentView]);
119     else
120         SetNSView(NULL);
121     AssociateNSWindow(m_cocoaNSWindow);
122     if(need_debug) wxLogDebug("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d",this,cocoaNSWindow,[cocoaNSWindow retainCount]);
123 }
124
125 void wxTopLevelWindowCocoa::Cocoa_wxMenuItemAction(wxMenuItem& item)
126 {
127 }
128
129 void wxTopLevelWindowCocoa::Cocoa_close(void)
130 {
131     m_closed = true;
132     Destroy();
133     /* Be SURE that idle events get ran.  If the window was not active when
134     it was closed, then there will be no more events to trigger this and
135     therefore it must be done here */
136     wxTheApp->CocoaInstallRequestedIdleHandler();
137 }
138
139 bool wxTopLevelWindowCocoa::Cocoa_windowShouldClose()
140 {
141     return wxWindowBase::Close(false);
142 }
143
144 // ----------------------------------------------------------------------------
145 // wxTopLevelWindowCocoa maximize/minimize
146 // ----------------------------------------------------------------------------
147
148 void wxTopLevelWindowCocoa::Maximize(bool maximize)
149 {
150 }
151
152 bool wxTopLevelWindowCocoa::IsMaximized() const
153 {
154     return false ; 
155 }
156
157 void wxTopLevelWindowCocoa::Iconize(bool iconize)
158 {
159 }
160
161 bool wxTopLevelWindowCocoa::IsIconized() const
162 {
163     return FALSE;
164 }
165
166 void wxTopLevelWindowCocoa::Restore()
167 {
168 }
169
170 bool wxTopLevelWindowCocoa::Show(bool show)
171 {
172     if(show)
173         [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
174     else
175         [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
176     return true;
177 }
178
179 bool wxTopLevelWindowCocoa::Close(bool force)
180 {
181     if(force)
182         return wxWindowBase::Close(force);
183     // performClose  will fake the user clicking the close button which
184     // will invoke windowShouldClose which will call the base class version
185     // of Close() which will NOT Destroy() the window (see below) but
186     // if closing is not stopped, then performClose will go ahead and
187     // close the window which will invoke Cocoa_close() setting m_closed
188     // to true and Destroy()ing the window.
189     [m_cocoaNSWindow performClose:m_cocoaNSWindow];
190     return m_closed;
191 }
192
193 void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
194 {
195     // If the event was forced, close the window which will Destroy() it
196     if(!event.CanVeto())
197         [m_cocoaNSWindow close];
198     // if the event was not forced, it's probably because the user clicked
199     // the close button, or Close(false) was called which (see above) is
200     // redirected to performClose and thus Cocoa itself will close the window
201 }
202
203 // ----------------------------------------------------------------------------
204 // wxTopLevelWindowCocoa misc
205 // ----------------------------------------------------------------------------
206
207 bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
208 {
209     return FALSE;
210 }
211
212 bool wxTopLevelWindowCocoa::IsFullScreen() const
213 {
214     return FALSE;
215 }
216
217 void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
218 {
219     wxLogDebug("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
220
221     NSRect cocoaRect = NSMakeRect(x,y,width,height);
222     [m_cocoaNSWindow setFrame: cocoaRect display:NO];
223 }
224
225 void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
226 {
227     NSRect cocoaRect = [m_cocoaNSWindow frame];
228     if(w)
229         *w=(int)cocoaRect.size.width;
230     if(h)
231         *h=(int)cocoaRect.size.height;
232     wxLogDebug("wxTopLevelWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
233 }
234
235 void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
236 {
237     NSRect cocoaRect = [m_cocoaNSWindow frame];
238     if(x)
239         *x=(int)cocoaRect.origin.x;
240     if(y)
241         *y=(int)cocoaRect.origin.y;
242     wxLogDebug("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
243 }
244