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