From 3466c3a241a94593e2d28a23f9b5dcf9235d8855 Mon Sep 17 00:00:00 2001 From: Robin Date: Sat, 5 Dec 2015 22:16:48 +0000 Subject: [PATCH] Added input to dock tabs --- DarkUI/Docking/DarkDockGroup.cs | 132 ++++++++++++++++++++++++ DarkUI/Docking/Items/DarkDockTab.cs | 2 - DarkUI/Docking/Items/DarkDockTabArea.cs | 6 +- Example/Forms/Docking/DockDocument.cs | 4 +- Example/Forms/MainForm.cs | 8 +- 5 files changed, 144 insertions(+), 8 deletions(-) diff --git a/DarkUI/Docking/DarkDockGroup.cs b/DarkUI/Docking/DarkDockGroup.cs index 55ce163..17dcca4 100644 --- a/DarkUI/Docking/DarkDockGroup.cs +++ b/DarkUI/Docking/DarkDockGroup.cs @@ -272,6 +272,29 @@ namespace DarkUI.Docking Invalidate(); } + public void EnsureVisible() + { + if (DockArea != DarkDockArea.Document) + return; + + if (DockPanel.ActiveContent == null) + return; + + var width = ClientRectangle.Width - Padding.Horizontal - _tabArea.DropdownRectangle.Width; + var offsetArea = new Rectangle(Padding.Left, 0, width, 0); + var tab = _tabs[DockPanel.ActiveContent]; + + if (tab.ClientRectangle.IsEmpty) + return; + + if (RectangleToTabArea(tab.ClientRectangle).Left < offsetArea.Left) + _tabArea.Offset = tab.ClientRectangle.Left; + else if (RectangleToTabArea(tab.ClientRectangle).Right > offsetArea.Right) + _tabArea.Offset = tab.ClientRectangle.Right - width; + + Invalidate(); + } + private Point PointToTabArea(Point point) { return new Point(point.X - _tabArea.Offset, point.Y); @@ -293,6 +316,115 @@ namespace DarkUI.Docking UpdateTabArea(); } + protected override void OnMouseMove(MouseEventArgs e) + { + base.OnMouseMove(e); + + if (_tabArea.DropdownRectangle.Contains(e.Location)) + { + _tabArea.DropdownHot = true; + + foreach (var tab in _tabs.Values) + tab.Hot = false; + + Invalidate(); + return; + } + + _tabArea.DropdownHot = false; + + foreach (var tab in _tabs.Values) + { + var rect = RectangleToTabArea(tab.ClientRectangle); + var hot = rect.Contains(e.Location); + + if (tab.Hot != hot) + { + tab.Hot = hot; + Invalidate(); + } + + var closeRect = RectangleToTabArea(tab.CloseButtonRectangle); + var closeHot = closeRect.Contains(e.Location); + + if (tab.CloseButtonHot != closeHot) + { + tab.CloseButtonHot = closeHot; + Invalidate(); + } + } + } + + protected override void OnMouseDown(MouseEventArgs e) + { + base.OnMouseDown(e); + + if (_tabArea.DropdownRectangle.Contains(e.Location)) + { + _tabArea.DropdownHot = true; + return; + } + + foreach (var tab in _tabs.Values) + { + var rect = RectangleToTabArea(tab.ClientRectangle); + if (rect.Contains(e.Location)) + { + if (e.Button == MouseButtons.Middle) + { + tab.DockContent.Close(); + return; + } + + var closeRect = RectangleToTabArea(tab.CloseButtonRectangle); + if (closeRect.Contains(e.Location)) + { + _tabArea.ClickedCloseButton = tab; + return; + } + else + { + DockPanel.ActiveContent = tab.DockContent; + EnsureVisible(); + return; + } + } + } + + if (VisibleContent != null) + DockPanel.ActiveContent = VisibleContent; + } + + protected override void OnMouseUp(MouseEventArgs e) + { + base.OnMouseUp(e); + + if (_tabArea.DropdownRectangle.Contains(e.Location)) + { + if (_tabArea.DropdownHot) + _tabArea.TabMenu.Show(this, new Point(_tabArea.DropdownRectangle.Left, _tabArea.DropdownRectangle.Bottom - 2)); + + return; + } + + if (_tabArea.ClickedCloseButton == null) + return; + + var closeRect = RectangleToTabArea(_tabArea.ClickedCloseButton.CloseButtonRectangle); + if (closeRect.Contains(e.Location)) + _tabArea.ClickedCloseButton.DockContent.Close(); + } + + protected override void OnMouseLeave(EventArgs e) + { + base.OnMouseLeave(e); + + foreach (var tab in _tabs.Values) + tab.Hot = false; + + Invalidate(); + } + private void TabMenuItem_Select(object sender, EventArgs e) { var menuItem = sender as ToolStripMenuItem; diff --git a/DarkUI/Docking/Items/DarkDockTab.cs b/DarkUI/Docking/Items/DarkDockTab.cs index d1fd586..907daec 100644 --- a/DarkUI/Docking/Items/DarkDockTab.cs +++ b/DarkUI/Docking/Items/DarkDockTab.cs @@ -14,8 +14,6 @@ namespace DarkUI.Docking public bool Hot { get; set; } - public bool Pressed { get; set; } - public bool CloseButtonHot { get; set; } public bool ShowSeparator { get; set; } diff --git a/DarkUI/Docking/Items/DarkDockTabArea.cs b/DarkUI/Docking/Items/DarkDockTabArea.cs index 5b8c0df..0464b85 100644 --- a/DarkUI/Docking/Items/DarkDockTabArea.cs +++ b/DarkUI/Docking/Items/DarkDockTabArea.cs @@ -5,7 +5,7 @@ using System.Drawing; namespace DarkUI.Docking { - public class DarkDockTabArea + internal class DarkDockTabArea { #region Field Region @@ -23,12 +23,16 @@ namespace DarkUI.Docking public Rectangle DropdownRectangle { get; set; } + public bool DropdownHot { get; set; } + public int Offset { get; set; } public bool Visible { get; set; } public DarkContextMenu TabMenu { get { return _tabMenu; } } + public DarkDockTab ClickedCloseButton { get; set; } + #endregion #region Constructor Region diff --git a/Example/Forms/Docking/DockDocument.cs b/Example/Forms/Docking/DockDocument.cs index 4ce1416..6063b68 100644 --- a/Example/Forms/Docking/DockDocument.cs +++ b/Example/Forms/Docking/DockDocument.cs @@ -9,10 +9,12 @@ namespace Example { #region Constructor Region - public DockDocument() + public DockDocument(string text) { InitializeComponent(); + DockText = text; + // Workaround to stop the textbox from highlight all text. txtDocument.SelectionStart = txtDocument.Text.Length; } diff --git a/Example/Forms/MainForm.cs b/Example/Forms/MainForm.cs index 2c0b030..690a1ea 100644 --- a/Example/Forms/MainForm.cs +++ b/Example/Forms/MainForm.cs @@ -46,9 +46,9 @@ namespace Example DockPanel.AddContent(_dockHistory, _dockLayers.DockGroup); // Add dummy documents to the main document area of the dock panel - DockPanel.AddContent(new DockDocument { DockText = "Document 1" }); - DockPanel.AddContent(new DockDocument { DockText = "Document 2" }); - DockPanel.AddContent(new DockDocument { DockText = "Document 3" }); + 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; @@ -103,7 +103,7 @@ namespace Example private void NewFile_Click(object sender, EventArgs e) { - var newFile = new DockDocument(); + var newFile = new DockDocument("New document"); DockPanel.AddContent(newFile); }