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