]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/toplevel.mm
replace wxDocument::GetPrintableName(wxString&) and wxDocManager::MakeDefaultName...
[wxWidgets.git] / src / cocoa / toplevel.mm
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // Licence: wxWidgets licence
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
23 #include "wx/toplevel.h"
24
25 #ifndef WX_PRECOMP
26 #include "wx/window.h"
27 #include "wx/menuitem.h"
28 #include "wx/frame.h"
29 #include "wx/log.h"
30 #include "wx/app.h"
31 #endif //WX_PRECOMP
32
33 #include "wx/cocoa/autorelease.h"
34 #include "wx/cocoa/string.h"
35
36 #include "wx/cocoa/objc/NSView.h"
37 #include "wx/cocoa/objc/NSWindow.h"
38 #import <AppKit/NSPanel.h>
39
40 // ----------------------------------------------------------------------------
41 // globals
42 // ----------------------------------------------------------------------------
43
44 // list of all frames and modeless dialogs
45 wxWindowList wxModelessWindows;
46
47 // ============================================================================
48 // wxTopLevelWindowCocoa implementation
49 // ============================================================================
50
51 wxTopLevelWindowCocoa *wxTopLevelWindowCocoa::sm_cocoaDeactivateWindow = NULL;
52
53 // ----------------------------------------------------------------------------
54 // wxTopLevelWindowCocoa creation
55 // ----------------------------------------------------------------------------
56 BEGIN_EVENT_TABLE(wxTopLevelWindowCocoa,wxTopLevelWindowBase)
57 EVT_CLOSE(wxTopLevelWindowCocoa::OnCloseWindow)
58 END_EVENT_TABLE()
59
60 void wxTopLevelWindowCocoa::Init()
61 {
62 m_iconized =
63 m_maximizeOnShow =
64 m_closed = false;
65 }
66
67 unsigned int wxTopLevelWindowCocoa::NSWindowStyleForWxStyle(long style)
68 {
69 unsigned int styleMask = 0;
70 if(style & wxCAPTION)
71 styleMask |= NSTitledWindowMask;
72 if(style & wxMINIMIZE_BOX)
73 styleMask |= NSMiniaturizableWindowMask;
74 #if 0
75 if(style & wxMAXIMIZE_BOX)
76 styleMask |= NSWindowMask;
77 #endif
78 if(style & wxCLOSE_BOX)
79 styleMask |= NSClosableWindowMask;
80 if(style & wxRESIZE_BORDER)
81 styleMask |= NSResizableWindowMask;
82 if(style & wxSIMPLE_BORDER)
83 styleMask |= NSBorderlessWindowMask;
84 return styleMask;
85 }
86
87 bool wxTopLevelWindowCocoa::Create(wxWindow *parent,
88 wxWindowID winid,
89 const wxString& title,
90 const wxPoint& pos,
91 const wxSize& size,
92 long style,
93 const wxString& name)
94 {
95 wxAutoNSAutoreleasePool pool;
96 wxTopLevelWindows.Append(this);
97
98 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
99 return false;
100
101 if ( parent )
102 parent->AddChild(this);
103
104 unsigned int cocoaStyle = NSWindowStyleForWxStyle(style);
105 if(style & wxFRAME_TOOL_WINDOW)
106 cocoaStyle |= NSUtilityWindowMask;
107
108 // Create frame and check and handle default position and size
109 int realx,
110 realy;
111
112 // WX has no set default position - the carbon port caps the low
113 // end at 20, 50. Here we do the same, except instead of setting
114 // it to 20 and 50, we set it to 100 and 100 if the values are too low
115 if (pos.x < 20)
116 realx = 100;
117 else
118 realx = pos.x;
119
120 if (pos.y < 50)
121 realy = 100;
122 else
123 realy = pos.y;
124
125 int realw = WidthDefault(size.x);
126 int realh = HeightDefault(size.y);
127
128 // NOTE: y-origin needs to be flipped.
129 NSRect cocoaRect = [NSWindow
130 contentRectForFrameRect:NSMakeRect(realx,realy,realw,realh)
131 styleMask:cocoaStyle];
132
133 m_cocoaNSWindow = NULL;
134 m_cocoaNSView = NULL;
135 // NOTE: We may need to deal with the contentView becoming a wx NSView as well.
136 NSWindow *newWindow;
137 // Create a WXNSPanel or a WXNSWindow depending on what type of window is desired.
138 if(style & wxFRAME_TOOL_WINDOW)
139 newWindow = [[WXNSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO];
140 else
141 newWindow = [[WXNSWindow alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO];
142 // Make sure the default content view is a WXNSView
143 [newWindow setContentView: [[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: [[newWindow contentView] frame]]];
144 // Associate the window and view
145 SetNSWindow(newWindow);
146
147 // NOTE: SetNSWindow has retained the Cocoa object for this object.
148 // Because we do not release on close, the following release matches the
149 // above alloc and thus the retain count will be 1.
150 [m_cocoaNSWindow release];
151
152 if(style & wxFRAME_NO_TASKBAR)
153 [m_cocoaNSWindow setExcludedFromWindowsMenu: YES];
154 if(style & wxSTAY_ON_TOP)
155 [m_cocoaNSWindow setLevel:NSFloatingWindowLevel];
156 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
157 return true;
158 }
159
160 wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
161 {
162 wxASSERT(sm_cocoaDeactivateWindow!=this);
163 wxAutoNSAutoreleasePool pool;
164 DestroyChildren();
165 if(m_cocoaNSView)
166 SendDestroyEvent();
167 SetNSWindow(NULL);
168 }
169
170 bool wxTopLevelWindowCocoa::Destroy()
171 {
172 if(sm_cocoaDeactivateWindow==this)
173 {
174 sm_cocoaDeactivateWindow = NULL;
175 wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
176 }
177 return wxTopLevelWindowBase::Destroy();
178 }
179
180 // ----------------------------------------------------------------------------
181 // wxTopLevelWindowCocoa Cocoa Specifics
182 // ----------------------------------------------------------------------------
183
184 wxMenuBar* wxTopLevelWindowCocoa::GetAppMenuBar(wxCocoaNSWindow *win)
185 {
186 wxTopLevelWindowCocoa *parent = wxDynamicCast(GetParent(),wxTopLevelWindow);
187 if(parent)
188 return parent->GetAppMenuBar(win);
189 return NULL;
190 }
191
192 void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
193 {
194 bool need_debug = cocoaNSWindow || m_cocoaNSWindow;
195 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d"),this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]);
196 DisassociateNSWindow(m_cocoaNSWindow);
197 [cocoaNSWindow retain];
198 [m_cocoaNSWindow release];
199 m_cocoaNSWindow = cocoaNSWindow;
200 // NOTE: We are no longer using posing so we won't get events on the
201 // window's view unless it was explicitly created as the wx view class.
202 if(m_cocoaNSWindow)
203 SetNSView([m_cocoaNSWindow contentView]);
204 else
205 SetNSView(NULL);
206 AssociateNSWindow(m_cocoaNSWindow);
207 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d"),this,cocoaNSWindow,[cocoaNSWindow retainCount]);
208 }
209
210 void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
211 {
212 if([m_cocoaNSWindow contentView] == (id)oldView)
213 [m_cocoaNSWindow setContentView:newView];
214 }
215
216 /*static*/ void wxTopLevelWindowCocoa::DeactivatePendingWindow()
217 {
218 if(sm_cocoaDeactivateWindow)
219 sm_cocoaDeactivateWindow->wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
220 sm_cocoaDeactivateWindow = NULL;
221 }
222
223 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeKey(void)
224 {
225 DeactivatePendingWindow();
226 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeKey"),this);
227 wxActivateEvent event(wxEVT_ACTIVATE, true, GetId());
228 event.SetEventObject(this);
229 GetEventHandler()->ProcessEvent(event);
230 }
231
232 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(void)
233 {
234 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignKey"),this);
235 wxActivateEvent event(wxEVT_ACTIVATE, false, GetId());
236 event.SetEventObject(this);
237 GetEventHandler()->ProcessEvent(event);
238 }
239
240 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeMain(void)
241 {
242 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeMain"),this);
243 }
244
245 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignMain(void)
246 {
247 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignMain"),this);
248 }
249
250 void wxTopLevelWindowCocoa::CocoaDelegate_windowWillClose(void)
251 {
252 m_closed = true;
253 Destroy();
254 }
255
256 bool wxTopLevelWindowCocoa::CocoaDelegate_windowShouldClose()
257 {
258 return wxWindowBase::Close(false);
259 }
260
261 void wxTopLevelWindowCocoa::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
262 {
263 }
264
265 bool wxTopLevelWindowCocoa::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
266 {
267 return false;
268 }
269
270 // ----------------------------------------------------------------------------
271 // wxTopLevelWindowCocoa maximize/minimize
272 // ----------------------------------------------------------------------------
273
274 void wxTopLevelWindowCocoa::Maximize(bool maximize)
275 {
276 }
277
278 bool wxTopLevelWindowCocoa::IsMaximized() const
279 {
280 return false ;
281 }
282
283 void wxTopLevelWindowCocoa::Iconize(bool iconize)
284 {
285 }
286
287 bool wxTopLevelWindowCocoa::IsIconized() const
288 {
289 return false;
290 }
291
292 void wxTopLevelWindowCocoa::Restore()
293 {
294 }
295
296 bool wxTopLevelWindowCocoa::Show(bool show)
297 {
298 if(m_isShown == show)
299 return false;
300 wxAutoNSAutoreleasePool pool;
301 if(show)
302 {
303 // Send the window a size event because wxWidgets apps expect it
304 // NOTE: This should really only be done the first time a window
305 // is shown. I doubt this will cause any problems though.
306 wxSizeEvent event(GetSize(), GetId());
307 event.SetEventObject(this);
308 GetEventHandler()->ProcessEvent(event);
309
310 [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
311 }
312 else
313 [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
314 m_isShown = show;
315 return true;
316 }
317
318 bool wxTopLevelWindowCocoa::Close(bool force)
319 {
320 if(force)
321 return wxWindowBase::Close(force);
322 // performClose will fake the user clicking the close button which
323 // will invoke windowShouldClose which will call the base class version
324 // of Close() which will NOT Destroy() the window (see below) but
325 // if closing is not stopped, then performClose will go ahead and
326 // close the window which will send the close notifications setting
327 // m_closed to true and Destroy()ing the window.
328 [m_cocoaNSWindow performClose:m_cocoaNSWindow];
329 return m_closed;
330 }
331
332 void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
333 {
334 // If the event was forced, close the window which will Destroy() it
335 if(!event.CanVeto())
336 [m_cocoaNSWindow close];
337 // if the event was not forced, it's probably because the user clicked
338 // the close button, or Close(false) was called which (see above) is
339 // redirected to performClose and thus Cocoa itself will close the window
340 }
341
342 // ----------------------------------------------------------------------------
343 // wxTopLevelWindowCocoa misc
344 // ----------------------------------------------------------------------------
345
346 void wxTopLevelWindowCocoa::SetTitle(const wxString& title)
347 {
348 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
349 }
350
351 wxString wxTopLevelWindowCocoa::GetTitle() const
352 {
353 return wxStringWithNSString([m_cocoaNSWindow title]);
354 }
355
356 bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
357 {
358 return false;
359 }
360
361 bool wxTopLevelWindowCocoa::IsFullScreen() const
362 {
363 return false;
364 }
365
366 void wxTopLevelWindowCocoa::CocoaSetWxWindowSize(int width, int height)
367 {
368 // Set the NSView size by setting the frame size to enclose it
369 unsigned int styleMask = [m_cocoaNSWindow styleMask];
370 NSRect frameRect = [m_cocoaNSWindow frame];
371 NSRect contentRect = [NSWindow
372 contentRectForFrameRect: frameRect
373 styleMask: styleMask];
374 contentRect.size.width = width;
375 contentRect.size.height = height;
376 frameRect = [NSWindow
377 frameRectForContentRect: contentRect
378 styleMask: styleMask];
379 [m_cocoaNSWindow setFrame: frameRect display: NO];
380 }
381
382 void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
383 {
384 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
385
386 NSRect cocoaRect = NSMakeRect(x,y,width,height);
387 [m_cocoaNSWindow setFrame: cocoaRect display:NO];
388 }
389
390 void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
391 {
392 NSRect cocoaRect = [m_cocoaNSWindow frame];
393 if(w)
394 *w=(int)cocoaRect.size.width;
395 if(h)
396 *h=(int)cocoaRect.size.height;
397 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
398 }
399
400 void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
401 {
402 NSRect cocoaRect = [m_cocoaNSWindow frame];
403 if(x)
404 *x=(int)cocoaRect.origin.x;
405 if(y)
406 *y=(int)cocoaRect.origin.y;
407 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
408 }