Skip to content

Commit

Permalink
Added X button for SWQL Studio Tabs (solarwinds#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
nothrow authored and tdanner committed Jan 12, 2017
1 parent c86f002 commit 772b7b9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
73 changes: 73 additions & 0 deletions Src/SwqlStudio/Controls/TabControlEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SwqlStudio.Controls
{
/// <summary>
/// Tabcontrol with the 'X' button for each tab
/// </summary>
internal class TabControlEx : TabControl
{
private const int CloseButtonPadding = 15;
private const string CloseButtonText = "×";
private readonly Lazy<Size> _closeButtonSize;
public TabControlEx()
{
DrawMode = TabDrawMode.OwnerDrawFixed;
SizeMode = TabSizeMode.Fixed;

_closeButtonSize = new Lazy<Size>(() => TextRenderer.MeasureText(CloseButtonText, Font));
}

protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);

// recompute tab widths
int maxTabLength = TabPages.Cast<TabPage>()
.Select(currentPage => TextRenderer.MeasureText(currentPage.Text, Font).Width + Padding.X + Padding.X + CloseButtonPadding)
.DefaultIfEmpty(0)
.Max();

var newItemWidth = Math.Max(maxTabLength, ItemSize.Width);

var newItemSize = new Size(newItemWidth, ItemSize.Height);

if (ItemSize != newItemSize)
{
ItemSize = newItemSize;
}
}

protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);

var closeButtonSize = _closeButtonSize.Value;

for (int i = 0; i < TabPages.Count; i++)
{
var tabRectangle = GetTabRect(i);
var tabCloseButton = new Rectangle(tabRectangle.Right - CloseButtonPadding, tabRectangle.Top + Padding.Y, closeButtonSize.Width, closeButtonSize.Height);

if (tabCloseButton.Contains(e.Location))
{
TabPages.RemoveAt(i);
break;
}
}
}

protected override void OnDrawItem(DrawItemEventArgs e)
{
TextRenderer.DrawText(e.Graphics, CloseButtonText, e.Font, new Point(e.Bounds.Right - CloseButtonPadding, e.Bounds.Top + Padding.Y), Color.Black);
TextRenderer.DrawText(e.Graphics, TabPages[e.Index].Text, e.Font, new Point(e.Bounds.Location.X + Padding.X, e.Bounds.Top + Padding.Y), Color.Black);

e.DrawFocusRectangle();
}
}
}
4 changes: 2 additions & 2 deletions Src/SwqlStudio/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Src/SwqlStudio/SwqlStudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
</Compile>
<Compile Include="ConnectionHistory.cs" />
<Compile Include="ConnectionInfo.cs" />
<Compile Include="Controls\TabControlEx.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="CustomCertificateValidator.cs" />
<Compile Include="DataGridExporter.cs" />
<Compile Include="EntityClassGraphForm.cs">
Expand Down

0 comments on commit 772b7b9

Please sign in to comment.