protected virtual void OnNewWindowCreated(ToolsUIWindow newWindow)
 {
 }
        void ReplicateLayoutStates()
        {
            if (this.duplicatingWindow != null)
            {
                for (int i = 0; i < this.LayoutTabControl.Items.Count; i++)
                {
                    var ourLayout = this.LayoutTabControl.Items[i] as LayoutInstance;
                    var toCopyLayout = this.duplicatingWindow.LayoutTabControl.Items[i] as LayoutInstance;

                    ourLayout.ReplicateLayoutState(toCopyLayout);
                }

                this.duplicatingWindow = null;
            }
        }
 public DocumentWrapper(ToolsUIWindow window)
 {
     this.Window = window;
 }
 public ActiveDocumentCookie(ToolsUIWindow window) : base(window) { }
            void OnMainWindowClosed(object sender, EventArgs e)
            {
                this.thisWindow.Dispatcher.BeginInvoke((Action)(() =>
                {
                    // We need to re-establish the main window.  It might even be us now!
                    this.mainWindowDocumentTracker.ActiveDocumentChanged -= OnMainWindowActiveDocumentChanged;
                    this.mainWindow.Closed -= OnMainWindowClosed;

                    this.mainWindow = ToolsUIApplication.Instance.MainToolsUIWindow;

                    if (this.thisWindow != this.mainWindow)
                    {
                        this.mainWindowDocumentTracker = this.mainWindow.ServiceProvider.GetService(typeof(IActiveDocumentTracker)) as IActiveDocumentTracker;
                    }
                    else
                    {
                        this.mainWindowDocumentTracker = null;
                    }

                    if (this.mainWindowDocumentTracker != null)
                    {
                        this.mainWindowDocumentTracker.ActiveDocumentChanged += OnMainWindowActiveDocumentChanged;
                        this.activeDocumentCookie.WrappedDocument = this.mainWindowDocumentTracker.ActiveDocument;
                        this.IsTrackingMainWindow = true;
                        this.mainWindow.Closed += OnMainWindowClosed;
                    }
                    else
                    {
                        this.documentList.Remove(this.activeDocumentCookie);
                        this.activeDocumentCookie = null;
                        this.IsTrackingMainWindow = false;
                        AssignShortcuts();
                    }

                }), DispatcherPriority.Background);
            }
            public WindowDocumentTracker(ToolsUIWindow window, IServiceProvider serviceProvider)
                : base(serviceProvider)
            {
                this.thisWindow = window;
                this.mainWindow = ToolsUIApplication.Instance.MainToolsUIWindow ?? window;      // If MainToolsUIWindow is null, the window is (about to be) the main window
                this.documentList = new ObservableCollection<DocumentWrapper>();

                if (this.thisWindow != this.mainWindow)
                {
                    // This is not the main window.  All secondary windows are born tracking the main window's
                    // active document.
                    this.mainWindowDocumentTracker = this.mainWindow.ServiceProvider.GetService(typeof(IActiveDocumentTracker)) as IActiveDocumentTracker;

                    if (this.mainWindowDocumentTracker != null)
                    {
                        this.mainWindowDocumentTracker.ActiveDocumentChanged += OnMainWindowActiveDocumentChanged;
                        this.activeDocumentCookie = new ActiveDocumentCookie(this.thisWindow);
                        this.activeDocument = this.mainWindowDocumentTracker.ActiveDocument;
                        this.activeDocumentCookie.WrappedDocument = this.mainWindowDocumentTracker.ActiveDocument;
                        this.isTrackingMainWindow = true;
                        this.mainWindow.Closed += OnMainWindowClosed;
                    }
                }

                this.documentListAsINCC = this.DocumentManager.Documents as INotifyCollectionChanged;

                if (this.documentListAsINCC != null)
                {
                    this.documentListAsINCC.CollectionChanged += OnDocumentListChanged;
                }

                InitializeDocumentList();
                this.selectedDocument = this.activeDocumentCookie ?? this.documentList.FirstOrDefault(d => d.WrappedDocument == this.activeDocument);
            }
        internal void LoadWindowState(ToolsUIWindow mainWindow)
        {
            this.mainWindow = mainWindow;

            try
            {
                var defaultStateText = GetDefaultWindowStateResourceText();
                var stateDoc = XElement.Parse(defaultStateText);
                var layoutDefinitionsElement = stateDoc.Element("LayoutDefinitions");

                this.defaultLayoutDefinitions = layoutDefinitionsElement.Elements("LayoutDefinition")
                    .Select(e => LayoutDefinition.LoadFromState(e, this.viewCreators))
                    .ToList();
            }
            catch (Exception)
            {
                this.defaultLayoutDefinitions = new List<LayoutDefinition>();
            }

            LoadWindowState();
        }
        void OnWindowClosed(object sender, EventArgs e)
        {
            var window = sender as ToolsUIWindow;

            if (window == null)
            {
                return;
            }

            this.allWindows.Remove(window);

            if (this.allWindows.Count == 0)
            {
                // That was the last window. If we're not shutting down by now, do it.
                if (!this.ShuttingDown)
                {
                    this.ShuttingDown = true;
                    DoShutdown();
                }
            }

            if (window == this.mainWindow)
            {
                this.mainWindow = this.allWindows.OrderBy(w => -w.ActivationIndex).FirstOrDefault();
            }

            if (!this.ShuttingDown && (this.mainWindow != null))
            {
                RequestViewBindingUpdate();
                Notify("WindowCount");
            }
        }
        public ToolsUIWindow CreateWindow()
        {
            var window = CreateToolsUIWindow();

            if (this.mainWindow == null)
            {
                this.mainWindow = window;
            }

            this.allWindows.Add(window);
            window.Closed += OnWindowClosed;
            Notify("WindowCount");
            return window;
        }