private ApplicationScreen ConstructScreen(IApplicationBlock owner, Type screenType) { #region Pre-conditions Debug.Assert(owner != null); Debug.Assert(screenType != null); Debug.Assert(screenType.IsSubclassOf(typeof(ApplicationScreen))); #endregion ApplicationScreen screen = null; if (LongRunningScreens.ContainsKey(screenType)) { screen = LongRunningScreens[screenType]; } else { try { // try to create with parameters screen = Activator.CreateInstance(screenType, owner, this) as ApplicationScreen; } catch (MissingMethodException) { try { // try to create with just block parameter screen = Activator.CreateInstance(screenType, owner) as ApplicationScreen; } catch (MissingMethodException) { // try to create without parameters screen = Activator.CreateInstance(screenType) as ApplicationScreen; } } #region Validate if (screen == null) { throw new ApplicationException( string.Format( "Could not instantiate the screen of type '{0}' as it did not inherit from '{1}'.", screenType.Name, typeof(ApplicationScreen).Name ) ); } #endregion // set screen context if activation did not do so //if (screen.ApplicationServices == null) { // screen.ApplicationServices = base.ApplicationServices; //} if (screen.ApplicationBlock == null) { screen.ApplicationBlock = owner; } // add this screen to long running screens if it is not to be destroyed. if (screen.ActivationMode == ScreenActivationMode.KeepAlive) { LongRunningScreens.Add(screenType, screen); } } #region Post-conditions Debug.Assert(screen != null); #endregion return(screen); }
private void UnregisterScreenFromMenu(ApplicationScreen screen) { for (int i = 0; i < MenuStrip.Items.Count; i++) { if (MenuStrip.Items[i].Tag == screen) { MenuStrip.Items.RemoveAt(i); break; } } }
private void RegisterScreenInMenu(ApplicationScreen screen) { ToolStripMenuItem headerMenu = new ToolStripMenuItem( screen.ApplicationMenuStripText ); headerMenu.Tag = screen; foreach (ToolStripItem menuItem in screen.MenuItems) { headerMenu.DropDownItems.Add( menuItem ); } InsertMenuItemBeforeHelpMenu(headerMenu); }
private void ShowScreen(ApplicationScreen screen) { #region Pre-conditions Debug.Assert(screen != null); #endregion var cancelRemove = false; // notify current view for permission to remove ActiveScreen?.NotifyHideScreen(ref cancelRemove); // remove if current view granted permission if (!cancelRemove) { _splitContainer.Panel2.SuspendLayout(); // remove the view toolbar items if any if (ActiveScreen != null) { UnregisterScreenFromMenu(ActiveScreen); // put the tool buttons back into the screen toolbar ActiveScreen.ToolBar?.Items.AddRange(ActiveViewButtons.ToArray()); ActiveViewButtons.Clear(); // remove the view control if (_splitContainer.Panel2.Controls.Contains(ActiveScreen)) { _splitContainer.Panel2.Controls.Remove(ActiveScreen); } // dispose view and release its resources if (ActiveScreen.ActivationMode == ScreenActivationMode.AlwaysCreate) { ActiveScreen.Dispose(); } } // collapse menu if new screen demands it bool collapseMenu = screen.DisplayMode == ScreenDisplayMode.Filled || screen.DisplayMode == ScreenDisplayMode.FilledAndMaximized; _splitContainer.Panel1Collapsed = collapseMenu; // maximize if new screen demands it bool maximize = screen.DisplayMode == ScreenDisplayMode.FilledAndMaximized || screen.DisplayMode == ScreenDisplayMode.Maximized; if (maximize) { base.WindowState = FormWindowState.Maximized; } // place the new view in the content panel screen.Dock = DockStyle.Fill; screen.Location = new Point(0, 0); screen.Name = "VIEW"; screen.TabIndex = 0; screen.Location = new Point( _splitContainer.Panel2.Padding.Left, _splitContainer.Panel2.Padding.Top ); Size newSize = new Size( _splitContainer.Width - (_splitContainer.SplitterDistance + _splitContainer.SplitterWidth) - (_splitContainer.Panel2.Padding.Left + _splitContainer.Panel2.Padding.Right), _splitContainer.Height - (_splitContainer.Panel2.Padding.Top + _splitContainer.Panel2.Padding.Bottom) ); if (collapseMenu) { newSize.Width = newSize.Width + _splitContainer.SplitterDistance; } ; screen.Size = newSize; screen.PerformLayout(); _splitContainer.Panel2.Controls.Add(screen); _splitContainer.Panel2.ResumeLayout(false); // set current view sa the active view ActiveScreen = screen; // place new tool strip items if (ActiveScreen.ShowInApplicationMenuStrip) { RegisterScreenInMenu(ActiveScreen); } RebuildToolBar(); // let view know that it has begun ActiveScreen.NotifyShow(); } }