]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/toplevel.mm
fixing define
[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
829a2e95
DE
36#include "wx/cocoa/objc/NSView.h"
37#include "wx/cocoa/objc/NSWindow.h"
080c7d56 38#import <AppKit/NSPanel.h>
e875c17d
DE
39#import <AppKit/NSButtonCell.h>
40#import <AppKit/NSControl.h>
a24aa427 41
fb896a32
DE
42// ----------------------------------------------------------------------------
43// globals
44// ----------------------------------------------------------------------------
45
46// list of all frames and modeless dialogs
47wxWindowList wxModelessWindows;
48
49// ============================================================================
50// wxTopLevelWindowCocoa implementation
51// ============================================================================
52
39050120
DE
53wxTopLevelWindowCocoa *wxTopLevelWindowCocoa::sm_cocoaDeactivateWindow = NULL;
54
fb896a32
DE
55// ----------------------------------------------------------------------------
56// wxTopLevelWindowCocoa creation
57// ----------------------------------------------------------------------------
fb896a32
DE
58BEGIN_EVENT_TABLE(wxTopLevelWindowCocoa,wxTopLevelWindowBase)
59 EVT_CLOSE(wxTopLevelWindowCocoa::OnCloseWindow)
60END_EVENT_TABLE()
61
62void wxTopLevelWindowCocoa::Init()
63{
64 m_iconized =
65 m_maximizeOnShow =
66 m_closed = false;
67}
68
080c7d56
DE
69unsigned int wxTopLevelWindowCocoa::NSWindowStyleForWxStyle(long style)
70{
71 unsigned int styleMask = 0;
72 if(style & wxCAPTION)
73 styleMask |= NSTitledWindowMask;
74 if(style & wxMINIMIZE_BOX)
75 styleMask |= NSMiniaturizableWindowMask;
76 #if 0
77 if(style & wxMAXIMIZE_BOX)
78 styleMask |= NSWindowMask;
79 #endif
80 if(style & wxCLOSE_BOX)
81 styleMask |= NSClosableWindowMask;
82 if(style & wxRESIZE_BORDER)
83 styleMask |= NSResizableWindowMask;
84 if(style & wxSIMPLE_BORDER)
85 styleMask |= NSBorderlessWindowMask;
86 return styleMask;
87}
88
15f37147
DE
89NSRect wxTopLevelWindowCocoa::MakeInitialNSWindowContentRect(const wxPoint& pos, const wxSize& size, unsigned int cocoaStyleMask)
90{
91 // Arbitrarily use (100,100) as the origin when default coords are given.
92 wxCoord x = pos.x!=wxDefaultCoord ? pos.x : 100;
93 wxCoord y = pos.y!=wxDefaultCoord ? pos.y : 100;
94
95 wxCoord w = WidthDefault(size.x);
96 wxCoord h = HeightDefault(size.y);
97
98 NSPoint cocoaOrigin = OriginInCocoaScreenCoordinatesForRectInWxDisplayCoordinates(x,y,w,h,true);
99
100 return [NSWindow
101 contentRectForFrameRect:NSMakeRect(cocoaOrigin.x,cocoaOrigin.y,w,h)
102 styleMask:cocoaStyleMask];
103
104}
105
fb896a32
DE
106bool wxTopLevelWindowCocoa::Create(wxWindow *parent,
107 wxWindowID winid,
108 const wxString& title,
109 const wxPoint& pos,
110 const wxSize& size,
111 long style,
112 const wxString& name)
113{
7fc77f30 114 wxAutoNSAutoreleasePool pool;
fb896a32
DE
115 wxTopLevelWindows.Append(this);
116
117 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
8d8d3633 118 return false;
fb896a32
DE
119
120 if ( parent )
121 parent->AddChild(this);
122
080c7d56
DE
123 unsigned int cocoaStyle = NSWindowStyleForWxStyle(style);
124 if(style & wxFRAME_TOOL_WINDOW)
125 cocoaStyle |= NSUtilityWindowMask;
fb896a32 126
15f37147 127 NSRect cocoaRect = MakeInitialNSWindowContentRect(pos,size,cocoaStyle);
fb896a32
DE
128
129 m_cocoaNSWindow = NULL;
130 m_cocoaNSView = NULL;
829a2e95
DE
131 // NOTE: We may need to deal with the contentView becoming a wx NSView as well.
132 NSWindow *newWindow;
133 // Create a WXNSPanel or a WXNSWindow depending on what type of window is desired.
080c7d56 134 if(style & wxFRAME_TOOL_WINDOW)
dc834029 135 newWindow = [[WX_GET_OBJC_CLASS(WXNSPanel) alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO];
080c7d56 136 else
dc834029 137 newWindow = [[WX_GET_OBJC_CLASS(WXNSWindow) alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO];
829a2e95 138 // Make sure the default content view is a WXNSView
a24aa427 139 [newWindow setContentView: [[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: [[newWindow contentView] frame]]];
829a2e95
DE
140 // Associate the window and view
141 SetNSWindow(newWindow);
142
fb896a32
DE
143 // NOTE: SetNSWindow has retained the Cocoa object for this object.
144 // Because we do not release on close, the following release matches the
145 // above alloc and thus the retain count will be 1.
146 [m_cocoaNSWindow release];
147
080c7d56
DE
148 if(style & wxFRAME_NO_TASKBAR)
149 [m_cocoaNSWindow setExcludedFromWindowsMenu: YES];
150 if(style & wxSTAY_ON_TOP)
151 [m_cocoaNSWindow setLevel:NSFloatingWindowLevel];
03cebc22 152 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
8d8d3633 153 return true;
fb896a32
DE
154}
155
156wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
157{
39050120 158 wxASSERT(sm_cocoaDeactivateWindow!=this);
7fc77f30 159 wxAutoNSAutoreleasePool pool;
0b659b0a 160 DestroyChildren();
9c85202a
DE
161 if(m_cocoaNSView)
162 SendDestroyEvent();
fb896a32
DE
163 SetNSWindow(NULL);
164}
165
39050120
DE
166bool wxTopLevelWindowCocoa::Destroy()
167{
168 if(sm_cocoaDeactivateWindow==this)
169 {
170 sm_cocoaDeactivateWindow = NULL;
171 wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
172 }
173 return wxTopLevelWindowBase::Destroy();
174}
175
fb896a32
DE
176// ----------------------------------------------------------------------------
177// wxTopLevelWindowCocoa Cocoa Specifics
178// ----------------------------------------------------------------------------
179
8ded703d 180wxMenuBar* wxTopLevelWindowCocoa::GetAppMenuBar(wxCocoaNSWindow *win)
3e84f98f
DE
181{
182 wxTopLevelWindowCocoa *parent = wxDynamicCast(GetParent(),wxTopLevelWindow);
183 if(parent)
8ded703d 184 return parent->GetAppMenuBar(win);
3e84f98f
DE
185 return NULL;
186}
187
fb896a32
DE
188void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
189{
190 bool need_debug = cocoaNSWindow || m_cocoaNSWindow;
48580976 191 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d"),this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]);
fe169919 192 DisassociateNSWindow(m_cocoaNSWindow);
fb896a32
DE
193 [cocoaNSWindow retain];
194 [m_cocoaNSWindow release];
195 m_cocoaNSWindow = cocoaNSWindow;
829a2e95
DE
196 // NOTE: We are no longer using posing so we won't get events on the
197 // window's view unless it was explicitly created as the wx view class.
fb896a32
DE
198 if(m_cocoaNSWindow)
199 SetNSView([m_cocoaNSWindow contentView]);
200 else
201 SetNSView(NULL);
fe169919 202 AssociateNSWindow(m_cocoaNSWindow);
48580976 203 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d"),this,cocoaNSWindow,[cocoaNSWindow retainCount]);
fb896a32
DE
204}
205
448cbf1d
DE
206void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
207{
911e17c6 208 if([m_cocoaNSWindow contentView] == (id)oldView)
448cbf1d
DE
209 [m_cocoaNSWindow setContentView:newView];
210}
211
39050120
DE
212/*static*/ void wxTopLevelWindowCocoa::DeactivatePendingWindow()
213{
214 if(sm_cocoaDeactivateWindow)
215 sm_cocoaDeactivateWindow->wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
216 sm_cocoaDeactivateWindow = NULL;
217}
218
aa992c59 219void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeKey(void)
5aa417d5 220{
39050120 221 DeactivatePendingWindow();
48580976 222 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeKey"),this);
8d8d3633 223 wxActivateEvent event(wxEVT_ACTIVATE, true, GetId());
5aa417d5 224 event.SetEventObject(this);
937013e0 225 HandleWindowEvent(event);
5aa417d5
DE
226}
227
aa992c59 228void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(void)
5aa417d5 229{
48580976 230 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignKey"),this);
8d8d3633 231 wxActivateEvent event(wxEVT_ACTIVATE, false, GetId());
5aa417d5 232 event.SetEventObject(this);
937013e0 233 HandleWindowEvent(event);
33faea0a
DE
234}
235
236void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeMain(void)
237{
48580976 238 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeMain"),this);
33faea0a
DE
239}
240
241void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignMain(void)
242{
48580976 243 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignMain"),this);
5aa417d5
DE
244}
245
9692f42b 246void wxTopLevelWindowCocoa::CocoaDelegate_windowWillClose(void)
fb896a32
DE
247{
248 m_closed = true;
249 Destroy();
fb896a32
DE
250}
251
aa992c59 252bool wxTopLevelWindowCocoa::CocoaDelegate_windowShouldClose()
fb896a32
DE
253{
254 return wxWindowBase::Close(false);
255}
256
7dd8b1ea 257void wxTopLevelWindowCocoa::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
86adc758
DE
258{
259}
260
7dd8b1ea 261bool wxTopLevelWindowCocoa::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
86adc758
DE
262{
263 return false;
264}
265
fb896a32
DE
266// ----------------------------------------------------------------------------
267// wxTopLevelWindowCocoa maximize/minimize
268// ----------------------------------------------------------------------------
269
270void wxTopLevelWindowCocoa::Maximize(bool maximize)
271{
272}
273
274bool wxTopLevelWindowCocoa::IsMaximized() const
275{
8d8d3633 276 return false ;
fb896a32
DE
277}
278
279void wxTopLevelWindowCocoa::Iconize(bool iconize)
280{
281}
282
283bool wxTopLevelWindowCocoa::IsIconized() const
284{
8d8d3633 285 return false;
fb896a32
DE
286}
287
288void wxTopLevelWindowCocoa::Restore()
289{
290}
291
292bool wxTopLevelWindowCocoa::Show(bool show)
293{
cbb2499e
DE
294 if(m_isShown == show)
295 return false;
7fc77f30 296 wxAutoNSAutoreleasePool pool;
fb896a32 297 if(show)
275341c0 298 {
065e208e 299 // Send the window a size event because wxWidgets apps expect it
275341c0
DE
300 // NOTE: This should really only be done the first time a window
301 // is shown. I doubt this will cause any problems though.
302 wxSizeEvent event(GetSize(), GetId());
303 event.SetEventObject(this);
937013e0 304 HandleWindowEvent(event);
275341c0 305
fb896a32 306 [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
275341c0 307 }
fb896a32
DE
308 else
309 [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
cbb2499e 310 m_isShown = show;
fb896a32
DE
311 return true;
312}
313
314bool wxTopLevelWindowCocoa::Close(bool force)
315{
316 if(force)
317 return wxWindowBase::Close(force);
318 // performClose will fake the user clicking the close button which
319 // will invoke windowShouldClose which will call the base class version
320 // of Close() which will NOT Destroy() the window (see below) but
321 // if closing is not stopped, then performClose will go ahead and
9692f42b
DE
322 // close the window which will send the close notifications setting
323 // m_closed to true and Destroy()ing the window.
fb896a32
DE
324 [m_cocoaNSWindow performClose:m_cocoaNSWindow];
325 return m_closed;
326}
327
328void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
329{
330 // If the event was forced, close the window which will Destroy() it
331 if(!event.CanVeto())
332 [m_cocoaNSWindow close];
333 // if the event was not forced, it's probably because the user clicked
334 // the close button, or Close(false) was called which (see above) is
335 // redirected to performClose and thus Cocoa itself will close the window
336}
337
338// ----------------------------------------------------------------------------
339// wxTopLevelWindowCocoa misc
340// ----------------------------------------------------------------------------
341
8b6fd08a 342void wxTopLevelWindowCocoa::SetTitle(const wxString& title)
8d8d3633 343{
65aeaf19 344 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
8d8d3633
WS
345}
346
347wxString wxTopLevelWindowCocoa::GetTitle() const
348{
65aeaf19 349 return wxStringWithNSString([m_cocoaNSWindow title]);
8d8d3633
WS
350}
351
e875c17d
DE
352wxWindow* wxTopLevelWindowCocoa::SetDefaultItem(wxWindow *win)
353{
354 wxWindow *old = wxTopLevelWindowBase::SetDefaultItem(win);
355 NSView *newView = win->GetNSView();
356
357 NSCell *newCell;
358 // newView does not have to be an NSControl, we only cast to NSControl*
359 // to silence the warning about cell not being implemented.
360 if(newView != nil && [newView respondsToSelector:@selector(cell)])
361 newCell = [(NSControl*)newView cell];
362 else
363 newCell = nil;
364
365 if(newCell != nil && ![newCell isKindOfClass:[NSButtonCell class]])
366 { // It's not an NSButtonCell, set the default to nil.
367 newCell = nil;
368 }
369
370 [GetNSWindow() setDefaultButtonCell:(NSButtonCell*)newCell];
371 return old;
372}
373
fb896a32
DE
374bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
375{
8d8d3633 376 return false;
fb896a32
DE
377}
378
379bool wxTopLevelWindowCocoa::IsFullScreen() const
380{
8d8d3633 381 return false;
fb896a32
DE
382}
383
e08efb8d
DE
384void wxTopLevelWindowCocoa::CocoaSetWxWindowSize(int width, int height)
385{
386 // Set the NSView size by setting the frame size to enclose it
387 unsigned int styleMask = [m_cocoaNSWindow styleMask];
03859c91 388 NSRect oldFrameRect = [m_cocoaNSWindow frame];
e08efb8d 389 NSRect contentRect = [NSWindow
03859c91 390 contentRectForFrameRect: oldFrameRect
e08efb8d
DE
391 styleMask: styleMask];
392 contentRect.size.width = width;
393 contentRect.size.height = height;
03859c91 394 NSRect newFrameRect = [NSWindow
e08efb8d
DE
395 frameRectForContentRect: contentRect
396 styleMask: styleMask];
03859c91
DE
397
398 // Cocoa uses +y is up but wxWidgets uses +y is down. We want an increase/decrease in height
399 // to not effect where the top of the window is placed so we set the new y origin relative the
400 // old one taking the height change into account.
401 newFrameRect.origin.y = oldFrameRect.origin.y + oldFrameRect.size.height - newFrameRect.size.height;
402
403 [m_cocoaNSWindow setFrame: newFrameRect display: NO];
e08efb8d
DE
404}
405
fb896a32
DE
406void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
407{
9879fa84 408 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
fb896a32 409
15f37147
DE
410 NSPoint cocoaOrigin = OriginInCocoaScreenCoordinatesForRectInWxDisplayCoordinates(x,y,width,height,false);
411
412 NSRect cocoaRect = NSMakeRect(cocoaOrigin.x,cocoaOrigin.y,width,height);
fb896a32
DE
413 [m_cocoaNSWindow setFrame: cocoaRect display:NO];
414}
415
416void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
417{
418 NSRect cocoaRect = [m_cocoaNSWindow frame];
419 if(w)
420 *w=(int)cocoaRect.size.width;
421 if(h)
422 *h=(int)cocoaRect.size.height;
9879fa84 423 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
fb896a32
DE
424}
425
426void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
427{
15f37147
DE
428 NSRect windowFrame = [m_cocoaNSWindow frame];
429
430 wxPoint theWxOrigin = OriginInWxDisplayCoordinatesForRectInCocoaScreenCoordinates(windowFrame);
431
432 if(*x)
433 *x = theWxOrigin.x;
434 if(*y)
435 *y = theWxOrigin.y;
436
437 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)"),this,(int)theWxOrigin.x,(int)theWxOrigin.y);
fb896a32 438}