1 /////////////////////////////////////////////////////////////////////////////
 
   2 // Name:        src/cocoa/mdi.mm
 
   3 // Purpose:     wxMDIParentFrame, wxMDIChildFrame, wxMDIClientWindow
 
   4 // Author:      David Elliott
 
   8 // Copyright:   (c) 2003 David Elliott
 
   9 // Licence:     wxWidgets licence
 
  10 /////////////////////////////////////////////////////////////////////////////
 
  12 #include "wx/wxprec.h"
 
  18 #include "wx/cocoa/objc/objc_uniquifying.h"
 
  24 // #include "wx/cocoa/autorelease.h"
 
  25 #include "wx/cocoa/mbarman.h"
 
  27 #import <AppKit/NSWindow.h>
 
  28 #import <Foundation/NSNotification.h>
 
  29 // #import <AppKit/NSApplication.h>
 
  30 // #import <AppKit/NSView.h>
 
  32 #include "wx/listimpl.cpp"
 
  33 WX_DEFINE_LIST(wxCocoaMDIChildFrameList);
 
  35 WX_DECLARE_HASH_MAP(int, wxMDIChildFrame*, wxIntegerHash, wxIntegerEqual, wxIntMDIChildFrameHashMap);
 
  37 // ============================================================================
 
  38 // wxMDIParentFrameObserver
 
  39 // ============================================================================
 
  40 @interface wxMDIParentFrameObserver : NSObject
 
  42     wxMDIParentFrame *m_mdiParent;
 
  46 - (id)initWithWxMDIParentFrame: (wxMDIParentFrame *)mdiParent;
 
  47 - (void)windowDidBecomeMain: (NSNotification *)notification;
 
  48 @end // interface wxMDIParentFrameObserver : NSObject
 
  49 WX_DECLARE_GET_OBJC_CLASS(wxMDIParentFrameObserver,NSObject)
 
  51 @implementation wxMDIParentFrameObserver : NSObject
 
  54     wxFAIL_MSG(wxT("[wxMDIParentFrameObserver -init] should never be called!"));
 
  59 - (id)initWithWxMDIParentFrame: (wxMDIParentFrame *)mdiParent
 
  62     m_mdiParent = mdiParent;
 
  66 - (void)windowDidBecomeMain: (NSNotification *)notification
 
  68     wxASSERT(m_mdiParent);
 
  69     m_mdiParent->WindowDidBecomeMain(notification);
 
  72 @end // implementation wxMDIParentFrameObserver : NSObject
 
  73 WX_IMPLEMENT_GET_OBJC_CLASS(wxMDIParentFrameObserver,NSObject)
 
  75 // ========================================================================
 
  77 // ========================================================================
 
  78 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame,wxFrame)
 
  79 BEGIN_EVENT_TABLE(wxMDIParentFrame,wxFrame)
 
  82 void wxMDIParentFrame::Init()
 
  84     m_clientWindow = NULL;
 
  85     m_currentChild = NULL;
 
  86     m_observer = [[WX_GET_OBJC_CLASS(wxMDIParentFrameObserver) alloc]
 
  87             initWithWxMDIParentFrame:this];
 
  88     [[NSNotificationCenter defaultCenter] addObserver:m_observer
 
  89             selector:@selector(windowDidBecomeMain:)
 
  90             name:NSWindowDidBecomeMainNotification object:nil];
 
  93 bool wxMDIParentFrame::Create(wxWindow *parent,
 
  94         wxWindowID winid, const wxString& title,
 
  95         const wxPoint& pos, const wxSize& size,
 
  96         long style, const wxString& name)
 
  98     if ( !wxFrame::Create(parent,winid,title,pos,size,style,name) )
 
 101     m_clientWindow = OnCreateClient();
 
 103     return m_clientWindow != NULL;
 
 106 wxMDIParentFrame::~wxMDIParentFrame()
 
 108     for(wxCocoaMDIChildFrameList::compatibility_iterator node =
 
 109             m_mdiChildren.GetFirst(); node; node = m_mdiChildren.GetFirst())
 
 111         wxMDIChildFrame *child = node->GetData();
 
 114         wxASSERT_MSG(!m_mdiChildren.Find(child),
 
 115             wxT("MDI child didn't remove itself using RemoveMDIChild()"));
 
 117     [m_observer release];
 
 120 void wxMDIParentFrame::AddMDIChild(wxMDIChildFrame *child)
 
 122     m_mdiChildren.Append(child);
 
 125 void wxMDIParentFrame::RemoveMDIChild(wxMDIChildFrame *child)
 
 127     m_mdiChildren.DeleteObject(child);
 
 128     if(child==m_currentChild)
 
 129         SetActiveChild(NULL);
 
 132 void wxMDIParentFrame::SetActiveChild(wxMDIChildFrame *child)
 
 134     m_currentChild = child;
 
 135     wxMenuBarManager::GetInstance()->UpdateMenuBar();
 
 138 wxMenuBar *wxMDIParentFrame::GetAppMenuBar(wxCocoaNSWindow *win)
 
 140     if(m_currentChild && (win==this))
 
 141         return m_currentChild->GetAppMenuBar(win);
 
 142     return wxFrame::GetAppMenuBar(win);
 
 145 void wxMDIParentFrame::CocoaDelegate_windowDidBecomeKey(void)
 
 147     wxLogTrace(wxTRACE_COCOA,wxT("wxMDIParentFrame=%p::CocoaDelegate_windowDidBecomeKey"),this);
 
 148     if(sm_cocoaDeactivateWindow && sm_cocoaDeactivateWindow==m_currentChild)
 
 150         sm_cocoaDeactivateWindow = NULL;
 
 153     else if(sm_cocoaDeactivateWindow == this)
 
 155         sm_cocoaDeactivateWindow = NULL;
 
 162             NSWindow *nswin = m_currentChild->GetNSWindow();
 
 163             if(![nswin isMainWindow])
 
 164                 [nswin makeMainWindow];
 
 166         wxFrame::CocoaDelegate_windowDidBecomeKey();
 
 170 void wxMDIParentFrame::CocoaDelegate_windowDidResignKey(void)
 
 172     wxLogTrace(wxTRACE_COCOA,wxT("wxMDIParentFrame=%p::CocoaDelegate_windowDidResignKey"),this);
 
 174         wxFrame::CocoaDelegate_windowDidResignKey();
 
 176         sm_cocoaDeactivateWindow = this;
 
 179 // We should not become the main window as we aren't a document window
 
 180 // MDI "Children" should be the main window
 
 181 bool wxMDIParentFrame::Cocoa_canBecomeMainWindow(bool &canBecome)
 
 183     canBecome = m_mdiChildren.IsEmpty(); return true;
 
 186 void wxMDIParentFrame::WindowDidBecomeMain(NSNotification *notification)
 
 188     // If we aren't the key window, we don't care
 
 189     if(![m_cocoaNSWindow isKeyWindow])
 
 191     wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
 
 192     // If we are key and becoming main, that's great
 
 195     // If one of our children is becoming main, also great
 
 196     for(wxCocoaMDIChildFrameList::compatibility_iterator node =
 
 197             m_mdiChildren.GetFirst(); node; node = node->GetNext())
 
 199         wxMDIChildFrame *child = node->GetData();
 
 203     // Some other window is becoming main, but we are key
 
 204     // Make the new main window the key window
 
 205     [[notification object] makeKeyWindow];
 
 208         wxIntMDIChildFrameHashMap hashmap;
 
 209         for(wxCocoaMDIChildFrameList::compatibility_iterator node =
 
 210                 m_mdiChildren.GetFirst(); node; node = node->GetNext())
 
 212             wxMDIChildFrame *child = node->GetData();
 
 213             hashmap.insert(wxIntMDIChildFrameHashMap::value_type([child->m_cocoaNSWindow windowNumber],child));
 
 217             NSInteger windowCount = 0;
 
 218             NSCountWindows(&windowCount);
 
 219             wxASSERT(windowCount>0);
 
 220             NSInteger *windowList = new NSInteger[windowCount];
 
 221             NSWindowList(windowCount, windowList);
 
 222             wxIntMDIChildFrameHashMap::iterator iter = hashmap.end();
 
 223             for(int i=0; i<windowCount && iter == hashmap.end(); i++)
 
 224                 iter=hashmap.find(windowList[i]);
 
 225             if(iter != hashmap.end())
 
 226                 m_currentChild = iter->second;
 
 231 // ========================================================================
 
 233 // ========================================================================
 
 234 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame,wxFrame)
 
 235 BEGIN_EVENT_TABLE(wxMDIChildFrame,wxFrame)
 
 238 void wxMDIChildFrame::Init()
 
 243 bool wxMDIChildFrame::Create(wxMDIParentFrame *parent,
 
 244         wxWindowID winid, const wxString& title,
 
 245         const wxPoint& pos, const wxSize& size,
 
 246         long style, const wxString& name)
 
 248     bool success = wxFrame::Create(parent,winid,title,pos,size,style,name);
 
 251         m_mdiParent = parent;
 
 252         parent->AddMDIChild(this);
 
 257 wxMDIChildFrame::~wxMDIChildFrame()
 
 259     // Just in case Destroy() wasn't called
 
 260     m_mdiParent->RemoveMDIChild(this);
 
 263 void wxMDIChildFrame::Activate()
 
 267 void wxMDIChildFrame::CocoaDelegate_windowDidBecomeKey(void)
 
 269     wxLogTrace(wxTRACE_COCOA,wxT("wxMDIChildFrame=%p::CocoaDelegate_windowDidBecomeKey"),this);
 
 270     if(sm_cocoaDeactivateWindow && sm_cocoaDeactivateWindow==m_mdiParent)
 
 272         sm_cocoaDeactivateWindow = NULL;
 
 273         if(m_mdiParent->GetActiveChild() != this)
 
 274             sm_cocoaDeactivateWindow = m_mdiParent->GetActiveChild();
 
 276     m_mdiParent->SetActiveChild(this);
 
 277     wxFrame::CocoaDelegate_windowDidBecomeKey();
 
 280 void wxMDIChildFrame::CocoaDelegate_windowDidBecomeMain(void)
 
 282     m_mdiParent->SetActiveChild(this);
 
 283     wxFrame::CocoaDelegate_windowDidBecomeMain();
 
 286 void wxMDIChildFrame::CocoaDelegate_windowDidResignKey(void)
 
 288     wxLogTrace(wxTRACE_COCOA,wxT("wxMDIChildFrame=%p::CocoaDelegate_windowDidResignKey"),this);
 
 289     sm_cocoaDeactivateWindow = this;
 
 292 bool wxMDIChildFrame::Destroy()
 
 294     // It's good to do this here before we are really closed
 
 295     m_mdiParent->RemoveMDIChild(this);
 
 296     return wxFrame::Destroy();
 
 299 // ========================================================================
 
 301 // ========================================================================
 
 302 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow,wxWindow)
 
 304 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style)
 
 306     return Create(parent, wxID_ANY, wxPoint(0, 0), wxSize(0, 0), style);