]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/toplevel.mm
Oops. Check in the correct Makefile.in.
[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
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
7fc77f30
DE
31#include "wx/cocoa/autorelease.h"
32
fe169919 33#import <AppKit/NSView.h>
fb896a32
DE
34#import <AppKit/NSWindow.h>
35// ----------------------------------------------------------------------------
36// globals
37// ----------------------------------------------------------------------------
38
39// list of all frames and modeless dialogs
40wxWindowList wxModelessWindows;
41
42// ============================================================================
43// wxTopLevelWindowCocoa implementation
44// ============================================================================
45
46// ----------------------------------------------------------------------------
47// wxTopLevelWindowCocoa creation
48// ----------------------------------------------------------------------------
fb896a32
DE
49BEGIN_EVENT_TABLE(wxTopLevelWindowCocoa,wxTopLevelWindowBase)
50 EVT_CLOSE(wxTopLevelWindowCocoa::OnCloseWindow)
51END_EVENT_TABLE()
52
53void wxTopLevelWindowCocoa::Init()
54{
55 m_iconized =
56 m_maximizeOnShow =
57 m_closed = false;
58}
59
60bool 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{
7fc77f30 68 wxAutoNSAutoreleasePool pool;
fb896a32
DE
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
98wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
99{
7fc77f30 100 wxAutoNSAutoreleasePool pool;
0b659b0a 101 DestroyChildren();
fb896a32
DE
102 SetNSWindow(NULL);
103}
104
105// ----------------------------------------------------------------------------
106// wxTopLevelWindowCocoa Cocoa Specifics
107// ----------------------------------------------------------------------------
108
109void 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]);
fe169919 113 DisassociateNSWindow(m_cocoaNSWindow);
fb896a32
DE
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);
fe169919 121 AssociateNSWindow(m_cocoaNSWindow);
fb896a32
DE
122 if(need_debug) wxLogDebug("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d",this,cocoaNSWindow,[cocoaNSWindow retainCount]);
123}
124
448cbf1d
DE
125void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
126{
911e17c6 127 if([m_cocoaNSWindow contentView] == (id)oldView)
448cbf1d
DE
128 [m_cocoaNSWindow setContentView:newView];
129}
130
aa992c59 131void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeKey(void)
5aa417d5 132{
aa992c59 133 wxLogDebug("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeKey",this);
5aa417d5
DE
134 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, GetId());
135 event.SetEventObject(this);
136 GetEventHandler()->ProcessEvent(event);
137}
138
aa992c59 139void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(void)
5aa417d5 140{
aa992c59 141 wxLogDebug("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignKey",this);
5aa417d5
DE
142 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, GetId());
143 event.SetEventObject(this);
144 GetEventHandler()->ProcessEvent(event);
145}
146
fb896a32
DE
147void wxTopLevelWindowCocoa::Cocoa_close(void)
148{
149 m_closed = true;
150 Destroy();
151 /* Be SURE that idle events get ran. If the window was not active when
152 it was closed, then there will be no more events to trigger this and
153 therefore it must be done here */
154 wxTheApp->CocoaInstallRequestedIdleHandler();
155}
156
aa992c59 157bool wxTopLevelWindowCocoa::CocoaDelegate_windowShouldClose()
fb896a32
DE
158{
159 return wxWindowBase::Close(false);
160}
161
162// ----------------------------------------------------------------------------
163// wxTopLevelWindowCocoa maximize/minimize
164// ----------------------------------------------------------------------------
165
166void wxTopLevelWindowCocoa::Maximize(bool maximize)
167{
168}
169
170bool wxTopLevelWindowCocoa::IsMaximized() const
171{
172 return false ;
173}
174
175void wxTopLevelWindowCocoa::Iconize(bool iconize)
176{
177}
178
179bool wxTopLevelWindowCocoa::IsIconized() const
180{
181 return FALSE;
182}
183
184void wxTopLevelWindowCocoa::Restore()
185{
186}
187
188bool wxTopLevelWindowCocoa::Show(bool show)
189{
cbb2499e
DE
190 if(m_isShown == show)
191 return false;
7fc77f30 192 wxAutoNSAutoreleasePool pool;
fb896a32 193 if(show)
275341c0
DE
194 {
195 // Send the window a size event because wxWindows apps expect it
196 // NOTE: This should really only be done the first time a window
197 // is shown. I doubt this will cause any problems though.
198 wxSizeEvent event(GetSize(), GetId());
199 event.SetEventObject(this);
200 GetEventHandler()->ProcessEvent(event);
201
fb896a32 202 [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
275341c0 203 }
fb896a32
DE
204 else
205 [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
cbb2499e 206 m_isShown = show;
fb896a32
DE
207 return true;
208}
209
210bool wxTopLevelWindowCocoa::Close(bool force)
211{
212 if(force)
213 return wxWindowBase::Close(force);
214 // performClose will fake the user clicking the close button which
215 // will invoke windowShouldClose which will call the base class version
216 // of Close() which will NOT Destroy() the window (see below) but
217 // if closing is not stopped, then performClose will go ahead and
218 // close the window which will invoke Cocoa_close() setting m_closed
219 // to true and Destroy()ing the window.
220 [m_cocoaNSWindow performClose:m_cocoaNSWindow];
221 return m_closed;
222}
223
224void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
225{
226 // If the event was forced, close the window which will Destroy() it
227 if(!event.CanVeto())
228 [m_cocoaNSWindow close];
229 // if the event was not forced, it's probably because the user clicked
230 // the close button, or Close(false) was called which (see above) is
231 // redirected to performClose and thus Cocoa itself will close the window
232}
233
234// ----------------------------------------------------------------------------
235// wxTopLevelWindowCocoa misc
236// ----------------------------------------------------------------------------
237
238bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
239{
240 return FALSE;
241}
242
243bool wxTopLevelWindowCocoa::IsFullScreen() const
244{
245 return FALSE;
246}
247
e08efb8d
DE
248void wxTopLevelWindowCocoa::CocoaSetWxWindowSize(int width, int height)
249{
250 // Set the NSView size by setting the frame size to enclose it
251 unsigned int styleMask = [m_cocoaNSWindow styleMask];
252 NSRect frameRect = [m_cocoaNSWindow frame];
253 NSRect contentRect = [NSWindow
254 contentRectForFrameRect: frameRect
255 styleMask: styleMask];
256 contentRect.size.width = width;
257 contentRect.size.height = height;
258 frameRect = [NSWindow
259 frameRectForContentRect: contentRect
260 styleMask: styleMask];
261 [m_cocoaNSWindow setFrame: frameRect display: NO];
262}
263
fb896a32
DE
264void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
265{
5aa417d5 266// wxLogDebug("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)",this,x,y,width,height);
fb896a32
DE
267
268 NSRect cocoaRect = NSMakeRect(x,y,width,height);
269 [m_cocoaNSWindow setFrame: cocoaRect display:NO];
270}
271
272void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
273{
274 NSRect cocoaRect = [m_cocoaNSWindow frame];
275 if(w)
276 *w=(int)cocoaRect.size.width;
277 if(h)
278 *h=(int)cocoaRect.size.height;
5aa417d5 279// wxLogDebug("wxTopLevelWindow=%p::DoGetSize = (%d,%d)",this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
fb896a32
DE
280}
281
282void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
283{
284 NSRect cocoaRect = [m_cocoaNSWindow frame];
285 if(x)
286 *x=(int)cocoaRect.origin.x;
287 if(y)
288 *y=(int)cocoaRect.origin.y;
5aa417d5 289// wxLogDebug("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)",this,(int)cocoaRect.origin.x,(int)cocoaRect.origin.y);
fb896a32
DE
290}
291