Implemented toolbar/statusbar positioning
[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 #include "wx/cocoa/autorelease.h"
32
33 #import <AppKit/NSView.h>
34 #import <AppKit/NSWindow.h>
35 // ----------------------------------------------------------------------------
36 // globals
37 // ----------------------------------------------------------------------------
38
39 // list of all frames and modeless dialogs
40 wxWindowList       wxModelessWindows;
41
42 // ============================================================================
43 // wxTopLevelWindowCocoa implementation
44 // ============================================================================
45
46 // ----------------------------------------------------------------------------
47 // wxTopLevelWindowCocoa creation
48 // ----------------------------------------------------------------------------
49 BEGIN_EVENT_TABLE(wxTopLevelWindowCocoa,wxTopLevelWindowBase)
50     EVT_CLOSE(wxTopLevelWindowCocoa::OnCloseWindow)
51 END_EVENT_TABLE()
52
53 void wxTopLevelWindowCocoa::Init()
54 {
55     m_iconized =
56     m_maximizeOnShow =
57     m_closed = false;
58 }
59
60 bool wxTopLevelWindowCocoa::Create(wxWindow *parent,
61                                  wxWindowID winid,
62                                  const wxString& title,
63                                  const wxPoint& pos,
64                                  const wxSize& size,
65                                  long style,
66                                  const wxString& name)
67 {
68     wxAutoNSAutoreleasePool pool;
69     wxTopLevelWindows.Append(this);
70
71     if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
72         return FALSE;
73
74     if ( parent )
75         parent->AddChild(this);
76
77     // TODO: get rect from given position/size
78     NSRect cocoaRect =  NSMakeRect(100,100,200,200);
79
80     // TODO: Set flags given wxWindows style
81     unsigned int cocoaStyle = 0;
82     cocoaStyle |= NSTitledWindowMask;
83     cocoaStyle |= NSClosableWindowMask;
84     cocoaStyle |= NSMiniaturizableWindowMask;
85     cocoaStyle |= NSResizableWindowMask;
86
87     m_cocoaNSWindow = NULL;
88     m_cocoaNSView = NULL;
89     SetNSWindow([[NSWindow alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
90     // NOTE: SetNSWindow has retained the Cocoa object for this object.
91     // Because we do not release on close, the following release matches the
92     // above alloc and thus the retain count will be 1.
93     [m_cocoaNSWindow release];
94
95     return TRUE;
96 }
97
98 wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
99 {
100     wxAutoNSAutoreleasePool pool;
101     DestroyChildren();
102     SetNSWindow(NULL);
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::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
126 {
127     if([m_cocoaNSWindow contentView] == oldView)
128         [m_cocoaNSWindow setContentView:newView];
129 }
130
131 void wxTopLevelWindowCocoa::Cocoa_wxMenuItemAction(wxMenuItem& item)
132 {
133 }
134
135 void wxTopLevelWindowCocoa::Cocoa_close(void)
136 {
137     m_closed = true;
138     Destroy();
139     /* Be SURE that idle events get ran.  If the window was not active when
140     it was closed, then there will be no more events to trigger this and
141     therefore it must be done here */
142     wxTheApp->CocoaInstallRequestedIdleHandler();
143 }
144
145 bool wxTopLevelWindowCocoa::Cocoa_windowShouldClose()
146 {
147     return wxWindowBase::Close(false);
148 }
149
150 // ----------------------------------------------------------------------------
151 // wxTopLevelWindowCocoa maximize/minimize
152 // ----------------------------------------------------------------------------
153
154 void wxTopLevelWindowCocoa::Maximize(bool maximize)
155 {
156 }
157
158 bool wxTopLevelWindowCocoa::IsMaximized() const
159 {
160     return false ; 
161 }
162
163 void wxTopLevelWindowCocoa::Iconize(bool iconize)
164 {
165 }
166
167 bool wxTopLevelWindowCocoa::IsIconized() const
168 {
169     return FALSE;
170 }
171
172 void wxTopLevelWindowCocoa::Restore()
173 {
174 }
175
176 bool wxTopLevelWindowCocoa::Show(bool show)
177 {
178     wxAutoNSAutoreleasePool pool;
179     if(show)
180         [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
181     else
182         [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
183     return true;
184 }
185
186 bool wxTopLevelWindowCocoa::Close(bool force)
187 {
188     if(force)
189         return wxWindowBase::Close(force);
190     // performClose  will fake the user clicking the close button which
191     // will invoke windowShouldClose which will call the base class version
192     // of Close() which will NOT Destroy() the window (see below) but
193     // if closing is not stopped, then performClose will go ahead and
194     // close the window which will invoke Cocoa_close() setting m_closed
195     // to true and Destroy()ing the window.
196     [m_cocoaNSWindow performClose:m_cocoaNSWindow];
197     return m_closed;
198 }
199
200 void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
201 {
202     // If the event was forced, close the window which will Destroy() it
203     if(!event.CanVeto())
204         [m_cocoaNSWindow close];
205     // if the event was not forced, it's probably because the user clicked
206     // the close button, or Close(false) was called which (see above) is
207     // redirected to performClose and thus Cocoa itself will close the window
208 }
209
210 // ----------------------------------------------------------------------------
211 // wxTopLevelWindowCocoa misc
212 // ----------------------------------------------------------------------------
213
214 bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
215 {
216     return FALSE;
217 }
218
219 bool wxTopLevelWindowCocoa::IsFullScreen() const
220 {
221     return FALSE;
222 }
223
224 void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
225 {
226     wxLogDebug("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
227
228     NSRect cocoaRect = NSMakeRect(x,y,width,height);
229     [m_cocoaNSWindow setFrame: cocoaRect display:NO];
230 }
231
232 void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
233 {
234     NSRect cocoaRect = [m_cocoaNSWindow frame];
235     if(w)
236         *w=(int)cocoaRect.size.width;
237     if(h)
238         *h=(int)cocoaRect.size.height;
239     wxLogDebug("wxTopLevelWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
240 }
241
242 void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
243 {
244     NSRect cocoaRect = [m_cocoaNSWindow frame];
245     if(x)
246         *x=(int)cocoaRect.origin.x;
247     if(y)
248         *y=(int)cocoaRect.origin.y;
249     wxLogDebug("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
250 }
251