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