Added ContentAdded and ContentRemoved events.

This commit is contained in:
Robin 2015-12-06 18:58:30 +00:00
parent ba2997b872
commit 45bedfcbd7
5 changed files with 66 additions and 32 deletions

View File

@ -47,7 +47,6 @@
public enum DarkDockArea
{
None,
Document,
Left,
Right,

View File

@ -45,7 +45,7 @@ namespace DarkUI.Docking
[Category("Layout")]
[Description("Determines which area of the dock panel this content will dock to.")]
[DefaultValue(DarkDockArea.None)]
[DefaultValue(DarkDockArea.Document)]
public DarkDockArea DockArea { get; set; }
[Browsable(false)]

View File

@ -233,7 +233,7 @@ namespace DarkUI.Docking
{
// Check if previous iteration of loop met the difference
if (differenceMadeUp >= difference)
continue;
break;
if (tab.ClientRectangle.Width >= largest)
{

View File

@ -12,6 +12,8 @@ namespace DarkUI.Docking
#region Event Region
public event EventHandler<DockContentEventArgs> ActiveContentChanged;
public event EventHandler<DockContentEventArgs> ContentAdded;
public event EventHandler<DockContentEventArgs> ContentRemoved;
#endregion
@ -111,14 +113,17 @@ namespace DarkUI.Docking
if (_contents.Contains(dockContent))
return;
if (dockContent.DockArea == DarkDockArea.None)
return;
if (dockContent.DockArea != dockGroup.DockArea)
throw new Exception($"Attempting to add '{dockContent.DockArea}' content to '{dockGroup.DockArea}' group.");
dockContent.DockPanel = this;
_contents.Add(dockContent);
var region = _regions[dockContent.DockArea];
region.AddContent(dockContent, dockGroup);
if (ContentAdded != null)
ContentAdded(this, new DockContentEventArgs(dockContent));
}
public void RemoveContent(DarkDockContent dockContent)
@ -131,6 +136,14 @@ namespace DarkUI.Docking
var region = _regions[dockContent.DockArea];
region.RemoveContent(dockContent);
if (ContentRemoved != null)
ContentRemoved(this, new DockContentEventArgs(dockContent));
}
public bool ContainsContent(DarkDockContent dockContent)
{
return _contents.Contains(dockContent);
}
private void CreateRegions()

View File

@ -2,6 +2,7 @@
using DarkUI.Forms;
using DarkUI.Win32;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Example
@ -10,6 +11,8 @@ namespace Example
{
#region Field Region
private List<DarkDockContent> _toolWindows = new List<DarkDockContent>();
private DockProject _dockProject;
private DockProperties _dockProperties;
private DockConsole _dockConsole;
@ -32,6 +35,9 @@ namespace Example
// input before letting events pass through to the rest of the application.
Application.AddMessageFilter(DockPanel.MessageFilter);
// Hook in all the UI events manually for clarity.
HookEvents();
// Build the tool windows and add them to the dock panel
_dockProject = new DockProject();
_dockProperties = new DockProperties();
@ -39,26 +45,24 @@ namespace Example
_dockLayers = new DockLayers();
_dockHistory = new DockHistory();
DockPanel.AddContent(_dockProject);
DockPanel.AddContent(_dockProperties);
DockPanel.AddContent(_dockConsole);
DockPanel.AddContent(_dockLayers);
DockPanel.AddContent(_dockHistory, _dockLayers.DockGroup);
// Add the tool windows to a list
_toolWindows.Add(_dockProject);
_toolWindows.Add(_dockProperties);
_toolWindows.Add(_dockConsole);
_toolWindows.Add(_dockLayers);
_toolWindows.Add(_dockHistory);
// Add the tool window list contents to the dock panel
foreach (var toolWindow in _toolWindows)
DockPanel.AddContent(toolWindow);
// Check window menu items which are contained in the dock panel
BuildWindowMenu();
// Add dummy documents to the main document area of the dock panel
DockPanel.AddContent(new DockDocument("Document 1"));
DockPanel.AddContent(new DockDocument("Document 2"));
DockPanel.AddContent(new DockDocument("Document 3"));
// Show the tool windows as visible in the 'Window' menu
mnuProject.Checked = true;
mnuProperties.Checked = true;
mnuConsole.Checked = true;
mnuLayers.Checked = true;
mnuHistory.Checked = true;
// Hook in all the UI events manually for clarity.
HookEvents();
}
#endregion
@ -67,6 +71,9 @@ namespace Example
private void HookEvents()
{
DockPanel.ContentAdded += DockPanel_ContentAdded;
DockPanel.ContentRemoved += DockPanel_ContentRemoved;
mnuNewFile.Click += NewFile_Click;
mnuClose.Click += Close_Click;
@ -83,24 +90,39 @@ namespace Example
mnuAbout.Click += About_Click;
}
private void ToggleToolWindow(DarkToolWindow toolWindow, ToolStripMenuItem menuItem)
private void ToggleToolWindow(DarkToolWindow toolWindow)
{
if (toolWindow.DockPanel == null)
{
DockPanel.AddContent(toolWindow);
menuItem.Checked = true;
}
else
{
DockPanel.RemoveContent(toolWindow);
menuItem.Checked = false;
}
}
private void BuildWindowMenu()
{
mnuProject.Checked = DockPanel.ContainsContent(_dockProject);
mnuProperties.Checked = DockPanel.ContainsContent(_dockProperties);
mnuConsole.Checked = DockPanel.ContainsContent(_dockConsole);
mnuLayers.Checked = DockPanel.Contains(_dockLayers);
mnuHistory.Checked = DockPanel.Contains(_dockHistory);
}
#endregion
#region Event Handler Region
private void DockPanel_ContentAdded(object sender, DockContentEventArgs e)
{
if (_toolWindows.Contains(e.Content))
BuildWindowMenu();
}
private void DockPanel_ContentRemoved(object sender, DockContentEventArgs e)
{
if (_toolWindows.Contains(e.Content))
BuildWindowMenu();
}
private void NewFile_Click(object sender, EventArgs e)
{
var newFile = new DockDocument("New document");
@ -120,27 +142,27 @@ namespace Example
private void Project_Click(object sender, EventArgs e)
{
ToggleToolWindow(_dockProject, mnuProject);
ToggleToolWindow(_dockProject);
}
private void Properties_Click(object sender, EventArgs e)
{
ToggleToolWindow(_dockProperties, mnuProperties);
ToggleToolWindow(_dockProperties);
}
private void Console_Click(object sender, EventArgs e)
{
ToggleToolWindow(_dockConsole, mnuConsole);
ToggleToolWindow(_dockConsole);
}
private void Layers_Click(object sender, EventArgs e)
{
ToggleToolWindow(_dockLayers, mnuLayers);
ToggleToolWindow(_dockLayers);
}
private void History_Click(object sender, EventArgs e)
{
ToggleToolWindow(_dockHistory, mnuHistory);
ToggleToolWindow(_dockHistory);
}
private void About_Click(object sender, EventArgs e)