Added original works and stubs to CVS repository
[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/NSWindow.h>
32 // ----------------------------------------------------------------------------
33 // globals
34 // ----------------------------------------------------------------------------
35
36 // list of all frames and modeless dialogs
37 wxWindowList       wxModelessWindows;
38
39 // ============================================================================
40 // wxTopLevelWindowCocoa implementation
41 // ============================================================================
42
43 // ----------------------------------------------------------------------------
44 // wxTopLevelWindowCocoa creation
45 // ----------------------------------------------------------------------------
46 IMPLEMENT_CLASS(wxTopLevelWindowCocoa,wxWindowCocoa)
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     SetNSWindow(NULL);
98 }
99
100 // ----------------------------------------------------------------------------
101 // wxTopLevelWindowCocoa Cocoa Specifics
102 // ----------------------------------------------------------------------------
103
104 void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
105 {
106     bool need_debug = cocoaNSWindow || m_cocoaNSWindow;
107     if(need_debug) wxLogDebug("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d",this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]);
108     if(m_cocoaNSWindow)
109         DisassociateNSWindow(m_cocoaNSWindow);
110     [cocoaNSWindow retain];
111     [m_cocoaNSWindow release];
112     m_cocoaNSWindow = cocoaNSWindow;
113     if(m_cocoaNSWindow)
114         SetNSView([m_cocoaNSWindow contentView]);
115     else
116         SetNSView(NULL);
117     if(m_cocoaNSWindow)
118         AssociateNSWindow(m_cocoaNSWindow);
119     if(need_debug) wxLogDebug("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d",this,cocoaNSWindow,[cocoaNSWindow retainCount]);
120 }
121
122 void wxTopLevelWindowCocoa::Cocoa_wxMenuItemAction(wxMenuItem& item)
123 {
124 }
125
126 void wxTopLevelWindowCocoa::Cocoa_close(void)
127 {
128     m_closed = true;
129     Destroy();
130     /* Be SURE that idle events get ran.  If the window was not active when
131     it was closed, then there will be no more events to trigger this and
132     therefore it must be done here */
133     wxTheApp->CocoaInstallRequestedIdleHandler();
134 }
135
136 bool wxTopLevelWindowCocoa::Cocoa_windowShouldClose()
137 {
138     return wxWindowBase::Close(false);
139 }
140
141 // ----------------------------------------------------------------------------
142 // wxTopLevelWindowCocoa maximize/minimize
143 // ----------------------------------------------------------------------------
144
145 void wxTopLevelWindowCocoa::Maximize(bool maximize)
146 {
147 }
148
149 bool wxTopLevelWindowCocoa::IsMaximized() const
150 {
151     return false ; 
152 }
153
154 void wxTopLevelWindowCocoa::Iconize(bool iconize)
155 {
156 }
157
158 bool wxTopLevelWindowCocoa::IsIconized() const
159 {
160     return FALSE;
161 }
162
163 void wxTopLevelWindowCocoa::Restore()
164 {
165 }
166
167 bool wxTopLevelWindowCocoa::Show(bool show)
168 {
169     if(show)
170         [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
171     else
172         [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
173     return true;
174 }
175
176 bool wxTopLevelWindowCocoa::Close(bool force)
177 {
178     if(force)
179         return wxWindowBase::Close(force);
180     // performClose  will fake the user clicking the close button which
181     // will invoke windowShouldClose which will call the base class version
182     // of Close() which will NOT Destroy() the window (see below) but
183     // if closing is not stopped, then performClose will go ahead and
184     // close the window which will invoke Cocoa_close() setting m_closed
185     // to true and Destroy()ing the window.
186     [m_cocoaNSWindow performClose:m_cocoaNSWindow];
187     return m_closed;
188 }
189
190 void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
191 {
192     // If the event was forced, close the window which will Destroy() it
193     if(!event.CanVeto())
194         [m_cocoaNSWindow close];
195     // if the event was not forced, it's probably because the user clicked
196     // the close button, or Close(false) was called which (see above) is
197     // redirected to performClose and thus Cocoa itself will close the window
198 }
199
200 // ----------------------------------------------------------------------------
201 // wxTopLevelWindowCocoa misc
202 // ----------------------------------------------------------------------------
203
204 bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
205 {
206     return FALSE;
207 }
208
209 bool wxTopLevelWindowCocoa::IsFullScreen() const
210 {
211     return FALSE;
212 }
213
214 void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
215 {
216     wxLogDebug("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
217
218     NSRect cocoaRect = NSMakeRect(x,y,width,height);
219     [m_cocoaNSWindow setFrame: cocoaRect display:NO];
220 }
221
222 void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
223 {
224     NSRect cocoaRect = [m_cocoaNSWindow frame];
225     if(w)
226         *w=(int)cocoaRect.size.width;
227     if(h)
228         *h=(int)cocoaRect.size.height;
229     wxLogDebug("wxTopLevelWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
230 }
231
232 void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
233 {
234     NSRect cocoaRect = [m_cocoaNSWindow frame];
235     if(x)
236         *x=(int)cocoaRect.origin.x;
237     if(y)
238         *y=(int)cocoaRect.origin.y;
239     wxLogDebug("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
240 }
241