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