1 ///////////////////////////////////////////////////////////////////////////////
 
   2 // Name:        src/cocoa/toplevel.mm
 
   3 // Purpose:     implements wxTopLevelWindow for Cocoa
 
   4 // Author:      David Elliott
 
   8 // Copyright:   (c) 2002 David Elliott
 
   9 // Licence:     wxWidgets licence
 
  10 ///////////////////////////////////////////////////////////////////////////////
 
  12 // ============================================================================
 
  14 // ============================================================================
 
  16 // ----------------------------------------------------------------------------
 
  18 // ----------------------------------------------------------------------------
 
  20 // For compilers that support precompilation, includes "wx.h".
 
  21 #include "wx/wxprec.h"
 
  23 #include "wx/toplevel.h"
 
  26     #include "wx/window.h"
 
  27     #include "wx/menuitem.h"
 
  33 #include "wx/cocoa/autorelease.h"
 
  34 #include "wx/cocoa/string.h"
 
  35 #include "wx/cocoa/ObjcRef.h"
 
  37 #include "wx/cocoa/objc/NSView.h"
 
  38 #include "wx/cocoa/objc/NSWindow.h"
 
  39 #import <AppKit/NSPanel.h>
 
  40 #import <AppKit/NSButtonCell.h>
 
  41 #import <AppKit/NSControl.h>
 
  43 // ----------------------------------------------------------------------------
 
  45 // ----------------------------------------------------------------------------
 
  47 // list of all frames and modeless dialogs
 
  48 wxWindowList       wxModelessWindows;
 
  50 // ============================================================================
 
  51 // wxTopLevelWindowCocoa implementation
 
  52 // ============================================================================
 
  54 wxTopLevelWindowCocoa *wxTopLevelWindowCocoa::sm_cocoaDeactivateWindow = NULL;
 
  56 // ----------------------------------------------------------------------------
 
  57 // wxTopLevelWindowCocoa creation
 
  58 // ----------------------------------------------------------------------------
 
  59 BEGIN_EVENT_TABLE(wxTopLevelWindowCocoa,wxTopLevelWindowBase)
 
  60     EVT_CLOSE(wxTopLevelWindowCocoa::OnCloseWindow)
 
  63 void wxTopLevelWindowCocoa::Init()
 
  70 unsigned int wxTopLevelWindowCocoa::NSWindowStyleForWxStyle(long style)
 
  72     unsigned int styleMask = 0;
 
  74         styleMask |= NSTitledWindowMask;
 
  75     if(style & wxMINIMIZE_BOX)
 
  76         styleMask |= NSMiniaturizableWindowMask;
 
  78     if(style & wxMAXIMIZE_BOX)
 
  79         styleMask |= NSWindowMask;
 
  81     if(style & wxCLOSE_BOX)
 
  82         styleMask |= NSClosableWindowMask;
 
  83     if(style & wxRESIZE_BORDER)
 
  84         styleMask |= NSResizableWindowMask;
 
  85     if(style & wxSIMPLE_BORDER)
 
  86         styleMask |= NSBorderlessWindowMask;
 
  90 NSRect wxTopLevelWindowCocoa::MakeInitialNSWindowContentRect(const wxPoint& pos, const wxSize& size, unsigned int cocoaStyleMask)
 
  92     // Arbitrarily use (100,100) as the origin when default coords are given.
 
  93     wxCoord x = pos.x!=wxDefaultCoord ? pos.x : 100;
 
  94     wxCoord y = pos.y!=wxDefaultCoord ? pos.y : 100;
 
  96     wxCoord w = WidthDefault(size.x);
 
  97     wxCoord h = HeightDefault(size.y);
 
  99     NSPoint cocoaOrigin = OriginInCocoaScreenCoordinatesForRectInWxDisplayCoordinates(x,y,w,h,true);
 
 102             contentRectForFrameRect:NSMakeRect(cocoaOrigin.x,cocoaOrigin.y,w,h)
 
 103             styleMask:cocoaStyleMask];
 
 107 bool wxTopLevelWindowCocoa::Create(wxWindow *parent,
 
 109                                  const wxString& title,
 
 113                                  const wxString& name)
 
 115     wxAutoNSAutoreleasePool pool;
 
 116     wxTopLevelWindows.Append(this);
 
 118     if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
 
 122         parent->AddChild(this);
 
 124     unsigned int cocoaStyle = NSWindowStyleForWxStyle(style);
 
 125     if(style & wxFRAME_TOOL_WINDOW)
 
 126         cocoaStyle |= NSUtilityWindowMask;
 
 128     NSRect cocoaRect = MakeInitialNSWindowContentRect(pos,size,cocoaStyle);
 
 130     m_cocoaNSWindow = NULL;
 
 131     m_cocoaNSView = NULL;
 
 132     // NOTE: We may need to deal with the contentView becoming a wx NSView as well.
 
 134     // Create a WXNSPanel or a WXNSWindow depending on what type of window is desired.
 
 135     if(style & wxFRAME_TOOL_WINDOW)
 
 136         newWindow = [[WX_GET_OBJC_CLASS(WXNSPanel) alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO];
 
 138         newWindow = [[WX_GET_OBJC_CLASS(WXNSWindow) alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO];
 
 139     // Make sure the default content view is a WXNSView
 
 140     [newWindow setContentView: [[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: [[newWindow contentView] frame]]];
 
 141     // Associate the window and view
 
 142     SetNSWindow(newWindow);
 
 144     // NOTE: SetNSWindow has retained the Cocoa object for this object.
 
 145     // Because we do not release on close, the following release matches the
 
 146     // above alloc and thus the retain count will be 1.
 
 147     [m_cocoaNSWindow release];
 
 149     if(style & wxFRAME_NO_TASKBAR)
 
 150         [m_cocoaNSWindow setExcludedFromWindowsMenu: YES];
 
 151     if(style & wxSTAY_ON_TOP)
 
 152         [m_cocoaNSWindow setLevel:NSFloatingWindowLevel];
 
 153     [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
 
 157 wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
 
 159     wxASSERT(sm_cocoaDeactivateWindow!=this);
 
 160     wxAutoNSAutoreleasePool pool;
 
 167 bool wxTopLevelWindowCocoa::Destroy()
 
 169     if(sm_cocoaDeactivateWindow==this)
 
 171         sm_cocoaDeactivateWindow = NULL;
 
 172         wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
 
 174     return wxTopLevelWindowBase::Destroy();
 
 177 // ----------------------------------------------------------------------------
 
 178 // wxTopLevelWindowCocoa Cocoa Specifics
 
 179 // ----------------------------------------------------------------------------
 
 181 wxMenuBar* wxTopLevelWindowCocoa::GetAppMenuBar(wxCocoaNSWindow *win)
 
 183     wxTopLevelWindowCocoa *parent = wxDynamicCast(GetParent(),wxTopLevelWindow);
 
 185         return parent->GetAppMenuBar(win);
 
 189 void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
 
 191     bool need_debug = cocoaNSWindow || m_cocoaNSWindow;
 
 192     if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d"),this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]);
 
 193     DisassociateNSWindow(m_cocoaNSWindow);
 
 194     wxGCSafeRetain(cocoaNSWindow);
 
 195     wxGCSafeRelease(m_cocoaNSWindow);
 
 196     m_cocoaNSWindow = cocoaNSWindow;
 
 197     // NOTE: We are no longer using posing so we won't get events on the
 
 198     // window's view unless it was explicitly created as the wx view class.
 
 200         SetNSView([m_cocoaNSWindow contentView]);
 
 203     AssociateNSWindow(m_cocoaNSWindow);
 
 204     if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d"),this,cocoaNSWindow,[cocoaNSWindow retainCount]);
 
 207 void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
 
 209     if([m_cocoaNSWindow contentView] == (id)oldView)
 
 210         [m_cocoaNSWindow setContentView:newView];
 
 213 /*static*/ void wxTopLevelWindowCocoa::DeactivatePendingWindow()
 
 215     if(sm_cocoaDeactivateWindow)
 
 216         sm_cocoaDeactivateWindow->wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
 
 217     sm_cocoaDeactivateWindow = NULL;
 
 220 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeKey(void)
 
 222     DeactivatePendingWindow();
 
 223     wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeKey"),this);
 
 224     wxActivateEvent event(wxEVT_ACTIVATE, true, GetId());
 
 225     event.SetEventObject(this);
 
 226     HandleWindowEvent(event);
 
 229 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(void)
 
 231     wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignKey"),this);
 
 232     wxActivateEvent event(wxEVT_ACTIVATE, false, GetId());
 
 233     event.SetEventObject(this);
 
 234     HandleWindowEvent(event);
 
 237 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeMain(void)
 
 239     wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeMain"),this);
 
 242 void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignMain(void)
 
 244     wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignMain"),this);
 
 247 void wxTopLevelWindowCocoa::CocoaDelegate_windowWillClose(void)
 
 253 bool wxTopLevelWindowCocoa::CocoaDelegate_windowShouldClose()
 
 255     return wxWindowBase::Close(false);
 
 258 void wxTopLevelWindowCocoa::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
 
 262 bool wxTopLevelWindowCocoa::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
 
 267 // ----------------------------------------------------------------------------
 
 268 // wxTopLevelWindowCocoa maximize/minimize
 
 269 // ----------------------------------------------------------------------------
 
 271 void wxTopLevelWindowCocoa::Maximize(bool maximize)
 
 275 bool wxTopLevelWindowCocoa::IsMaximized() const
 
 280 void wxTopLevelWindowCocoa::Iconize(bool iconize)
 
 284 bool wxTopLevelWindowCocoa::IsIconized() const
 
 289 void wxTopLevelWindowCocoa::Restore()
 
 293 bool wxTopLevelWindowCocoa::Show(bool show)
 
 295     if(m_isShown == show)
 
 297     wxAutoNSAutoreleasePool pool;
 
 300         // Send the window a size event because wxWidgets apps expect it
 
 301         // NOTE: This should really only be done the first time a window
 
 302         // is shown.  I doubt this will cause any problems though.
 
 303         wxSizeEvent event(GetSize(), GetId());
 
 304         event.SetEventObject(this);
 
 305         HandleWindowEvent(event);
 
 307         [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
 
 310         [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
 
 315 bool wxTopLevelWindowCocoa::Close(bool force)
 
 318         return wxWindowBase::Close(force);
 
 319     // performClose  will fake the user clicking the close button which
 
 320     // will invoke windowShouldClose which will call the base class version
 
 321     // of Close() which will NOT Destroy() the window (see below) but
 
 322     // if closing is not stopped, then performClose will go ahead and
 
 323     // close the window which will send the close notifications setting
 
 324     // m_closed to true and Destroy()ing the window.
 
 325     [m_cocoaNSWindow performClose:m_cocoaNSWindow];
 
 329 void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
 
 331     // If the event was forced, close the window which will Destroy() it
 
 333         [m_cocoaNSWindow close];
 
 334     // if the event was not forced, it's probably because the user clicked
 
 335     // the close button, or Close(false) was called which (see above) is
 
 336     // redirected to performClose and thus Cocoa itself will close the window
 
 339 // ----------------------------------------------------------------------------
 
 340 // wxTopLevelWindowCocoa misc
 
 341 // ----------------------------------------------------------------------------
 
 343 void wxTopLevelWindowCocoa::SetTitle(const wxString& title)
 
 345     [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
 
 348 wxString wxTopLevelWindowCocoa::GetTitle() const
 
 350     return wxStringWithNSString([m_cocoaNSWindow title]);
 
 353 wxWindow* wxTopLevelWindowCocoa::SetDefaultItem(wxWindow *win)
 
 355     wxWindow *old = wxTopLevelWindowBase::SetDefaultItem(win);
 
 360         NSView *newView = win->GetNSView();
 
 361         // newView does not have to be an NSControl, we only cast to NSControl*
 
 362         // to silence the warning about cell not being implemented.
 
 363         if(newView != nil && [newView respondsToSelector:@selector(cell)])
 
 364             newCell = [(NSControl*)newView cell];
 
 368         if(newCell != nil && ![newCell isKindOfClass:[NSButtonCell class]])
 
 369         {   // It's not an NSButtonCell, set the default to nil.
 
 376     [GetNSWindow() setDefaultButtonCell:(NSButtonCell*)newCell];
 
 380 bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
 
 385 bool wxTopLevelWindowCocoa::IsFullScreen() const
 
 390 void wxTopLevelWindowCocoa::CocoaSetWxWindowSize(int width, int height)
 
 392     // Set the NSView size by setting the frame size to enclose it
 
 393     unsigned int styleMask = [m_cocoaNSWindow styleMask];
 
 394     NSRect oldFrameRect = [m_cocoaNSWindow frame];
 
 395     NSRect contentRect = [NSWindow
 
 396         contentRectForFrameRect: oldFrameRect
 
 397         styleMask: styleMask];
 
 398     contentRect.size.width = width;
 
 399     contentRect.size.height = height;
 
 400     NSRect newFrameRect = [NSWindow
 
 401         frameRectForContentRect: contentRect
 
 402         styleMask: styleMask];
 
 404     // Cocoa uses +y is up but wxWidgets uses +y is down.  We want an increase/decrease in height
 
 405     // to not effect where the top of the window is placed so we set the new y origin relative the
 
 406     // old one taking the height change into account.
 
 407     newFrameRect.origin.y = oldFrameRect.origin.y + oldFrameRect.size.height - newFrameRect.size.height;
 
 409     [m_cocoaNSWindow setFrame: newFrameRect display: NO];
 
 412 void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
 
 414     wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
 
 416     NSPoint cocoaOrigin = OriginInCocoaScreenCoordinatesForRectInWxDisplayCoordinates(x,y,width,height,false);
 
 418     NSRect cocoaRect = NSMakeRect(cocoaOrigin.x,cocoaOrigin.y,width,height);
 
 419     [m_cocoaNSWindow setFrame: cocoaRect display:NO];
 
 422 void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
 
 424     NSRect cocoaRect = [m_cocoaNSWindow frame];
 
 426         *w=(int)cocoaRect.size.width;
 
 428         *h=(int)cocoaRect.size.height;
 
 429     wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
 
 432 void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
 
 434     NSRect windowFrame = [m_cocoaNSWindow frame];
 
 436     wxPoint theWxOrigin = OriginInWxDisplayCoordinatesForRectInCocoaScreenCoordinates(windowFrame);
 
 443     wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)"),this,(int)theWxOrigin.x,(int)theWxOrigin.y);