]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/toplevel.mm
updated version to match latest wxPython release
[wxWidgets.git] / src / cocoa / toplevel.mm
CommitLineData
fb896a32
DE
1///////////////////////////////////////////////////////////////////////////////
2// Name: cocoa/toplevel.mm
3// Purpose: implements wxTopLevelWindow for Cocoa
4// Author: David Elliott
5// Modified by:
6// Created: 2002/11/27
46cdffaf 7// RCS-ID: $Id$
fb896a32 8// Copyright: (c) 2002 David Elliott
065e208e 9// Licence: wxWidgets licence
fb896a32
DE
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
7fc77f30 31#include "wx/cocoa/autorelease.h"
da026811 32#include "wx/cocoa/string.h"
7fc77f30 33
fe169919 34#import <AppKit/NSView.h>
fb896a32 35#import <AppKit/NSWindow.h>
080c7d56 36#import <AppKit/NSPanel.h>
fb896a32
DE
37// ----------------------------------------------------------------------------
38// globals
39// ----------------------------------------------------------------------------
40
41// list of all frames and modeless dialogs
42wxWindowList wxModelessWindows;
43
44// ============================================================================
45// wxTopLevelWindowCocoa implementation
46// ============================================================================
47
39050120
DE
48wxTopLevelWindowCocoa *wxTopLevelWindowCocoa::sm_cocoaDeactivateWindow = NULL;
49
fb896a32
DE
50// ----------------------------------------------------------------------------
51// wxTopLevelWindowCocoa creation
52// ----------------------------------------------------------------------------
fb896a32
DE
53BEGIN_EVENT_TABLE(wxTopLevelWindowCocoa,wxTopLevelWindowBase)
54 EVT_CLOSE(wxTopLevelWindowCocoa::OnCloseWindow)
55END_EVENT_TABLE()
56
57void wxTopLevelWindowCocoa::Init()
58{
59 m_iconized =
60 m_maximizeOnShow =
61 m_closed = false;
62}
63
080c7d56
DE
64unsigned int wxTopLevelWindowCocoa::NSWindowStyleForWxStyle(long style)
65{
66 unsigned int styleMask = 0;
67 if(style & wxCAPTION)
68 styleMask |= NSTitledWindowMask;
69 if(style & wxMINIMIZE_BOX)
70 styleMask |= NSMiniaturizableWindowMask;
71 #if 0
72 if(style & wxMAXIMIZE_BOX)
73 styleMask |= NSWindowMask;
74 #endif
75 if(style & wxCLOSE_BOX)
76 styleMask |= NSClosableWindowMask;
77 if(style & wxRESIZE_BORDER)
78 styleMask |= NSResizableWindowMask;
79 if(style & wxSIMPLE_BORDER)
80 styleMask |= NSBorderlessWindowMask;
81 return styleMask;
82}
83
fb896a32
DE
84bool wxTopLevelWindowCocoa::Create(wxWindow *parent,
85 wxWindowID winid,
86 const wxString& title,
87 const wxPoint& pos,
88 const wxSize& size,
89 long style,
90 const wxString& name)
91{
7fc77f30 92 wxAutoNSAutoreleasePool pool;
fb896a32
DE
93 wxTopLevelWindows.Append(this);
94
95 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
96 return FALSE;
97
98 if ( parent )
99 parent->AddChild(this);
100
080c7d56
DE
101 unsigned int cocoaStyle = NSWindowStyleForWxStyle(style);
102 if(style & wxFRAME_TOOL_WINDOW)
103 cocoaStyle |= NSUtilityWindowMask;
fb896a32 104
9f13cca8
DE
105 wxPoint realpos = pos;
106 wxSize realsize = size;
107 // FIXME: this is lame
108 if(realpos.x==-1)
109 realpos.x=100;
110 if(realpos.y==-1)
111 realpos.y=100;
112 if(realsize.x==-1)
113 realsize.x=200;
114 if(realsize.y==-1)
115 realsize.y=200;
080c7d56 116 // NOTE: y-origin needs to be flipped.
9f13cca8 117 NSRect cocoaRect = [NSWindow contentRectForFrameRect:NSMakeRect(realpos.x,realpos.y,realsize.x,realsize.y) styleMask:cocoaStyle];
fb896a32
DE
118
119 m_cocoaNSWindow = NULL;
120 m_cocoaNSView = NULL;
080c7d56
DE
121 if(style & wxFRAME_TOOL_WINDOW)
122 SetNSWindow([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
123 else
124 SetNSWindow([[NSWindow alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
fb896a32
DE
125 // NOTE: SetNSWindow has retained the Cocoa object for this object.
126 // Because we do not release on close, the following release matches the
127 // above alloc and thus the retain count will be 1.
128 [m_cocoaNSWindow release];
129
080c7d56
DE
130 if(style & wxFRAME_NO_TASKBAR)
131 [m_cocoaNSWindow setExcludedFromWindowsMenu: YES];
132 if(style & wxSTAY_ON_TOP)
133 [m_cocoaNSWindow setLevel:NSFloatingWindowLevel];
da026811 134 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
fb896a32
DE
135 return TRUE;
136}
137
138wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
139{
39050120 140 wxASSERT(sm_cocoaDeactivateWindow!=this);
7fc77f30 141 wxAutoNSAutoreleasePool pool;
0b659b0a 142 DestroyChildren();
9c85202a
DE
143 if(m_cocoaNSView)
144 SendDestroyEvent();
fb896a32
DE
145 SetNSWindow(NULL);
146}
147
39050120
DE
148bool wxTopLevelWindowCocoa::Destroy()
149{
150 if(sm_cocoaDeactivateWindow==this)
151 {
152 sm_cocoaDeactivateWindow = NULL;
153 wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
154 }
155 return wxTopLevelWindowBase::Destroy();
156}
157
fb896a32
DE
158// ----------------------------------------------------------------------------
159// wxTopLevelWindowCocoa Cocoa Specifics
160// ----------------------------------------------------------------------------
161
8ded703d 162wxMenuBar* wxTopLevelWindowCocoa::GetAppMenuBar(wxCocoaNSWindow *win)
3e84f98f
DE
163{
164 wxTopLevelWindowCocoa *parent = wxDynamicCast(GetParent(),wxTopLevelWindow);
165 if(parent)
8ded703d 166 return parent->GetAppMenuBar(win);
3e84f98f
DE
167 return NULL;
168}
169
fb896a32
DE
170void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
171{
172 bool need_debug = cocoaNSWindow || m_cocoaNSWindow;
48580976 173 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d"),this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]);
fe169919 174 DisassociateNSWindow(m_cocoaNSWindow);
fb896a32
DE
175 [cocoaNSWindow retain];
176 [m_cocoaNSWindow release];
177 m_cocoaNSWindow = cocoaNSWindow;
178 if(m_cocoaNSWindow)
179 SetNSView([m_cocoaNSWindow contentView]);
180 else
181 SetNSView(NULL);
fe169919 182 AssociateNSWindow(m_cocoaNSWindow);
48580976 183 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d"),this,cocoaNSWindow,[cocoaNSWindow retainCount]);
fb896a32
DE
184}
185
448cbf1d
DE
186void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
187{
911e17c6 188 if([m_cocoaNSWindow contentView] == (id)oldView)
448cbf1d
DE
189 [m_cocoaNSWindow setContentView:newView];
190}
191
39050120
DE
192/*static*/ void wxTopLevelWindowCocoa::DeactivatePendingWindow()
193{
194 if(sm_cocoaDeactivateWindow)
195 sm_cocoaDeactivateWindow->wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
196 sm_cocoaDeactivateWindow = NULL;
197}
198
aa992c59 199void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeKey(void)
5aa417d5 200{
39050120 201 DeactivatePendingWindow();
48580976 202 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeKey"),this);
5aa417d5
DE
203 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, GetId());
204 event.SetEventObject(this);
205 GetEventHandler()->ProcessEvent(event);
206}
207
aa992c59 208void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(void)
5aa417d5 209{
48580976 210 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignKey"),this);
5aa417d5
DE
211 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, GetId());
212 event.SetEventObject(this);
213 GetEventHandler()->ProcessEvent(event);
33faea0a
DE
214}
215
216void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeMain(void)
217{
48580976 218 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeMain"),this);
33faea0a
DE
219}
220
221void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignMain(void)
222{
48580976 223 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignMain"),this);
5aa417d5
DE
224}
225
9692f42b 226void wxTopLevelWindowCocoa::CocoaDelegate_windowWillClose(void)
fb896a32
DE
227{
228 m_closed = true;
229 Destroy();
fb896a32
DE
230}
231
aa992c59 232bool wxTopLevelWindowCocoa::CocoaDelegate_windowShouldClose()
fb896a32
DE
233{
234 return wxWindowBase::Close(false);
235}
236
7dd8b1ea 237void wxTopLevelWindowCocoa::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
86adc758
DE
238{
239}
240
7dd8b1ea 241bool wxTopLevelWindowCocoa::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
86adc758
DE
242{
243 return false;
244}
245
fb896a32
DE
246// ----------------------------------------------------------------------------
247// wxTopLevelWindowCocoa maximize/minimize
248// ----------------------------------------------------------------------------
249
250void wxTopLevelWindowCocoa::Maximize(bool maximize)
251{
252}
253
254bool wxTopLevelWindowCocoa::IsMaximized() const
255{
256 return false ;
257}
258
259void wxTopLevelWindowCocoa::Iconize(bool iconize)
260{
261}
262
263bool wxTopLevelWindowCocoa::IsIconized() const
264{
265 return FALSE;
266}
267
268void wxTopLevelWindowCocoa::Restore()
269{
270}
271
272bool wxTopLevelWindowCocoa::Show(bool show)
273{
cbb2499e
DE
274 if(m_isShown == show)
275 return false;
7fc77f30 276 wxAutoNSAutoreleasePool pool;
fb896a32 277 if(show)
275341c0 278 {
065e208e 279 // Send the window a size event because wxWidgets apps expect it
275341c0
DE
280 // NOTE: This should really only be done the first time a window
281 // is shown. I doubt this will cause any problems though.
282 wxSizeEvent event(GetSize(), GetId());
283 event.SetEventObject(this);
284 GetEventHandler()->ProcessEvent(event);
285
fb896a32 286 [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
275341c0 287 }
fb896a32
DE
288 else
289 [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
cbb2499e 290 m_isShown = show;
fb896a32
DE
291 return true;
292}
293
294bool wxTopLevelWindowCocoa::Close(bool force)
295{
296 if(force)
297 return wxWindowBase::Close(force);
298 // performClose will fake the user clicking the close button which
299 // will invoke windowShouldClose which will call the base class version
300 // of Close() which will NOT Destroy() the window (see below) but
301 // if closing is not stopped, then performClose will go ahead and
9692f42b
DE
302 // close the window which will send the close notifications setting
303 // m_closed to true and Destroy()ing the window.
fb896a32
DE
304 [m_cocoaNSWindow performClose:m_cocoaNSWindow];
305 return m_closed;
306}
307
308void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
309{
310 // If the event was forced, close the window which will Destroy() it
311 if(!event.CanVeto())
312 [m_cocoaNSWindow close];
313 // if the event was not forced, it's probably because the user clicked
314 // the close button, or Close(false) was called which (see above) is
315 // redirected to performClose and thus Cocoa itself will close the window
316}
317
318// ----------------------------------------------------------------------------
319// wxTopLevelWindowCocoa misc
320// ----------------------------------------------------------------------------
321
322bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
323{
324 return FALSE;
325}
326
327bool wxTopLevelWindowCocoa::IsFullScreen() const
328{
329 return FALSE;
330}
331
e08efb8d
DE
332void wxTopLevelWindowCocoa::CocoaSetWxWindowSize(int width, int height)
333{
334 // Set the NSView size by setting the frame size to enclose it
335 unsigned int styleMask = [m_cocoaNSWindow styleMask];
336 NSRect frameRect = [m_cocoaNSWindow frame];
337 NSRect contentRect = [NSWindow
338 contentRectForFrameRect: frameRect
339 styleMask: styleMask];
340 contentRect.size.width = width;
341 contentRect.size.height = height;
342 frameRect = [NSWindow
343 frameRectForContentRect: contentRect
344 styleMask: styleMask];
345 [m_cocoaNSWindow setFrame: frameRect display: NO];
346}
347
fb896a32
DE
348void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
349{
9879fa84 350 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
fb896a32
DE
351
352 NSRect cocoaRect = NSMakeRect(x,y,width,height);
353 [m_cocoaNSWindow setFrame: cocoaRect display:NO];
354}
355
356void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
357{
358 NSRect cocoaRect = [m_cocoaNSWindow frame];
359 if(w)
360 *w=(int)cocoaRect.size.width;
361 if(h)
362 *h=(int)cocoaRect.size.height;
9879fa84 363 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
fb896a32
DE
364}
365
366void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
367{
368 NSRect cocoaRect = [m_cocoaNSWindow frame];
369 if(x)
370 *x=(int)cocoaRect.origin.x;
371 if(y)
372 *y=(int)cocoaRect.origin.y;
9879fa84 373 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)"),this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
fb896a32
DE
374}
375