ShareX/ShareX.HelpersLib/Controls/TabToTreeView.cs

214 lines
6 KiB
C#
Raw Normal View History

#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2020-02-05 20:19:48 +13:00
Copyright (c) 2007-2020 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
2014-12-06 15:06:33 +13:00
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
2014-12-11 09:25:20 +13:00
namespace ShareX.HelpersLib
{
public partial class TabToTreeView : UserControl
{
public delegate void TabChangedEventHandler(TabPage tabPage);
public event TabChangedEventHandler TabChanged;
private TabControl mainTabControl;
2014-12-06 15:06:33 +13:00
[Browsable(false)]
public TabControl MainTabControl
{
get
{
return mainTabControl;
}
set
{
if (mainTabControl != value)
{
mainTabControl = value;
FillTreeView(tvMain.Nodes, mainTabControl);
tvMain.ExpandAll();
}
}
}
private int treeViewSize;
public int TreeViewSize
{
get
{
return treeViewSize;
}
set
{
treeViewSize = value;
scMain.SplitterDistance = treeViewSize;
}
}
public Font TreeViewFont
{
get
{
return tvMain.Font;
}
set
{
tvMain.Font = value;
}
}
public ImageList ImageList
{
get
{
return tvMain.ImageList;
}
set
{
tvMain.ImageList = value;
}
}
[DefaultValue(false)]
public bool AutoSelectChild { get; set; }
public TabToTreeView()
{
InitializeComponent();
TreeViewSize = 150;
}
2014-12-06 15:06:33 +13:00
private void FillTreeView(TreeNodeCollection nodeCollection, TabControl tab, TreeNode parent = null)
{
if (nodeCollection != null && tab != null)
{
foreach (TabPage tabPage in tab.TabPages)
{
2014-12-07 03:10:24 +13:00
if (parent != null && string.IsNullOrEmpty(tabPage.Text))
2014-12-06 15:06:33 +13:00
{
parent.Tag = tabPage;
continue;
}
TreeNode treeNode = new TreeNode(tabPage.Text);
if (!string.IsNullOrEmpty(tabPage.ImageKey))
{
treeNode.ImageKey = treeNode.SelectedImageKey = tabPage.ImageKey;
}
treeNode.Tag = tabPage;
nodeCollection.Add(treeNode);
foreach (Control control in tabPage.Controls)
{
if (control is TabControl)
{
2014-12-06 15:06:33 +13:00
FillTreeView(treeNode.Nodes, control as TabControl, treeNode);
break;
}
}
}
}
}
private void tvMain_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
{
e.Cancel = true;
}
private void tvMain_AfterSelect(object sender, TreeViewEventArgs e)
{
TabPage tabPage = e.Node.Tag as TabPage;
2020-02-05 03:23:53 +13:00
if (tabPage != null)
{
2020-02-05 03:23:53 +13:00
if (AutoSelectChild && tabPage.Controls.Count == 1 && tabPage.Controls[0] is TabControl)
{
SelectChild();
}
else
{
SelectTab(tabPage);
}
}
}
private void SelectTab(TabPage tabPage)
{
if (tabPage != null)
{
2014-12-06 15:06:33 +13:00
tvMain.BeginUpdate();
tcMain.Visible = true;
tcMain.TabPages.Clear();
tcMain.TabPages.Add(tabPage);
2014-12-06 15:06:33 +13:00
tvMain.Focus();
tvMain.EndUpdate();
OnTabChanged(tabPage);
}
}
public void NavigateToTabPage(TabPage tabPage)
{
if (tabPage != null)
{
foreach (TreeNode node in tvMain.Nodes.All())
{
TabPage nodeTabPage = node.Tag as TabPage;
if (nodeTabPage == tabPage)
{
tvMain.SelectedNode = node;
return;
}
}
}
}
public void SelectChild()
{
if (tvMain.SelectedNode.Nodes.Count > 0)
{
tvMain.SelectedNode = tvMain.SelectedNode.Nodes[0];
}
}
protected void OnTabChanged(TabPage tabPage)
{
if (TabChanged != null)
{
TabChanged(tabPage);
}
}
protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
{
base.ScaleControl(factor, specified);
scMain.SplitterDistance = (int)Math.Round(scMain.SplitterDistance * factor.Width);
}
}
}