mattercontrol/CustomWidgets/DockingTabControl.cs

270 lines
8.1 KiB
C#
Raw Normal View History

/*
2017-06-12 14:19:04 -07:00
Copyright (c) 2017, Lars Brubaker, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
2017-06-12 14:19:04 -07:00
using System.Collections.Generic;
using MatterHackers.Agg;
using MatterHackers.Agg.Font;
2017-06-12 14:19:04 -07:00
using MatterHackers.Agg.ImageProcessing;
using MatterHackers.Agg.PlatformAbstract;
using MatterHackers.Agg.Transform;
2017-06-11 15:17:25 -07:00
using MatterHackers.Agg.UI;
using MatterHackers.Agg.VertexSource;
2017-06-11 15:17:25 -07:00
using MatterHackers.MatterControl.PartPreviewWindow;
using MatterHackers.VectorMath;
namespace MatterHackers.MatterControl.CustomWidgets
{
public class DockingTabControl : GuiWidget
{
// TODO: Pinned state should preferably come from MCWS, default to local data if guest and be per user not printer
public bool ControlIsPinned { get; set; } = false;
2017-06-11 15:17:25 -07:00
private GuiWidget topToBottom;
2017-06-12 10:01:50 -07:00
Dictionary<string, GuiWidget> allTabs = new Dictionary<string, GuiWidget>();
2017-06-11 15:17:25 -07:00
public DockingTabControl()
{
}
public void AddPage(string name, GuiWidget widget)
{
2017-06-12 10:01:50 -07:00
allTabs.Add(name, widget);
Rebuild();
}
2017-06-11 15:17:25 -07:00
2017-06-12 10:01:50 -07:00
void Rebuild()
{
foreach(var nameWidget in allTabs)
2017-06-11 15:17:25 -07:00
{
2017-06-12 10:01:50 -07:00
nameWidget.Value.Parent?.RemoveChild(nameWidget.Value);
nameWidget.Value.ClearRemovedFlag();
}
2017-06-11 15:17:25 -07:00
2017-06-12 10:01:50 -07:00
topToBottom.RemoveAllChildren();
2017-06-11 15:17:25 -07:00
2017-06-12 10:01:50 -07:00
var tabControl = new TabControl();
2017-06-11 15:17:25 -07:00
2017-06-12 10:01:50 -07:00
if (ControlIsPinned)
{
var resizePage = new ResizeContainer()
2017-06-11 15:17:25 -07:00
{
2017-06-12 10:01:50 -07:00
Width = 640,
VAnchor = VAnchor.ParentBottomTop,
2017-06-12 14:19:04 -07:00
BorderColor = ApplicationController.Instance.Theme.SplitterBackground
2017-06-11 15:17:25 -07:00
};
2017-06-12 14:19:04 -07:00
tabControl = ApplicationController.Instance.Theme.CreateTabControl();
2017-06-12 10:01:50 -07:00
resizePage.AddChild(tabControl);
topToBottom.AddChild(resizePage);
}
2017-06-11 15:17:25 -07:00
2017-06-12 10:01:50 -07:00
foreach (var nameWidget in allTabs)
{
if (ControlIsPinned)
2017-06-11 15:17:25 -07:00
{
2017-06-12 10:08:13 -07:00
var content = new DockWindowContent(this, nameWidget.Value, nameWidget.Key, ControlIsPinned);
2017-06-12 14:19:04 -07:00
var tabPage = new TabPage(content, nameWidget.Key);
tabControl.AddTab(new SimpleTextTabWidget(
tabPage,
nameWidget.Key + " Tab",
12,
ActiveTheme.Instance.TabLabelSelected,
RGBA_Bytes.Transparent,
ActiveTheme.Instance.TabLabelUnselected,
RGBA_Bytes.Transparent));
2017-06-12 10:01:50 -07:00
}
else // control is floating
{
2017-06-12 14:19:04 -07:00
var rotatedLabel = new VertexSourceApplyTransform(
new TypeFacePrinter(nameWidget.Key, 12),
2017-06-12 14:19:04 -07:00
Affine.NewRotation(MathHelper.DegreesToRadians(-90)));
2017-06-12 10:01:50 -07:00
2017-06-12 14:19:04 -07:00
var bounds = rotatedLabel.Bounds();
rotatedLabel.Transform = ((Affine)rotatedLabel.Transform) * Affine.NewTranslation(new Vector2(0, -bounds.Bottom + 0));
2017-06-12 10:01:50 -07:00
2017-06-12 14:19:04 -07:00
var optionsText = new GuiWidget(bounds.Width, bounds.Height)
2017-06-12 10:01:50 -07:00
{
DoubleBuffer = true,
};
optionsText.AfterDraw += (s, e) =>
{
2017-06-12 14:19:04 -07:00
e.graphics2D.Render(rotatedLabel, ActiveTheme.Instance.PrimaryTextColor);
};
2017-06-11 15:17:25 -07:00
2017-06-12 14:19:04 -07:00
var settingsButton = new PopupButton(optionsText)
2017-06-12 10:01:50 -07:00
{
AlignToRightEdge = true,
};
2017-06-12 14:19:04 -07:00
settingsButton.PopupContent = new DockWindowContent(this, nameWidget.Value, nameWidget.Key, ControlIsPinned)
{
BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor
};
2017-06-12 10:01:50 -07:00
topToBottom.AddChild(settingsButton);
}
2017-06-11 15:17:25 -07:00
}
if (ControlIsPinned)
{
tabControl.TabBar.AddChild(new HorizontalSpacer());
var icon = StaticData.Instance.LoadIcon("Pushpin_16x.png", 16, 16).InvertLightness();
var imageWidget = new ImageWidget(icon)
{
VAnchor = VAnchor.ParentCenter
};
imageWidget.Margin = new BorderDouble(right: 25, top: 6);
imageWidget.DebugShowBounds = true;
imageWidget.MinimumSize = new Vector2(16, 16);
imageWidget.Click += (s, e) =>
{
ControlIsPinned = !ControlIsPinned;
UiThread.RunOnIdle(() => Rebuild());
};
tabControl.TabBar.AddChild(imageWidget);
}
2017-06-11 15:17:25 -07:00
}
public override void Initialize()
{
base.Initialize();
Width = 30;
VAnchor = VAnchor.ParentBottomTop;
HAnchor = HAnchor.FitToChildren;
2017-06-12 14:19:04 -07:00
//BackgroundColor = RGBA_Bytes.Red;
topToBottom = new FlowLayoutWidget(FlowDirection.TopToBottom)
{
HAnchor = HAnchor.FitToChildren,
VAnchor = VAnchor.ParentBottomTop
};
AddChild(topToBottom);
}
2017-06-11 15:17:25 -07:00
internal class ResizeContainer : FlowLayoutWidget
{
private double downWidth = 0;
private bool mouseDownOnBar = false;
private double mouseDownX;
private int resizeWidth = 10;
internal ResizeContainer()
{
2017-06-12 14:19:04 -07:00
this.Padding = new BorderDouble(resizeWidth, 0, 0, 0);
this.HAnchor = HAnchor.AbsolutePosition;
this.Cursor = Cursors.WaitCursor;
2017-06-11 15:17:25 -07:00
}
public override void OnDraw(Graphics2D graphics2D)
{
2017-06-12 14:19:04 -07:00
graphics2D.FillRectangle(LocalBounds.Left, LocalBounds.Bottom, LocalBounds.Left + resizeWidth, LocalBounds.Top, this.BorderColor);
2017-06-11 15:17:25 -07:00
base.OnDraw(graphics2D);
}
2017-06-12 14:19:04 -07:00
public RGBA_Bytes BorderColor { get; set; } = ActiveTheme.Instance.TertiaryBackgroundColor;
2017-06-11 15:17:25 -07:00
public override void OnMouseDown(MouseEventArgs mouseEvent)
{
if (mouseEvent.Position.x < resizeWidth)
{
mouseDownOnBar = true;
mouseDownX = TransformToScreenSpace(mouseEvent.Position).x;
downWidth = Width;
}
base.OnMouseDown(mouseEvent);
}
public override void OnMouseMove(MouseEventArgs mouseEvent)
{
if (mouseDownOnBar)
{
int currentMouseX = (int)TransformToScreenSpace(mouseEvent.Position).x;
2017-06-12 10:01:50 -07:00
UiThread.RunOnIdle(() => Width = downWidth + mouseDownX - currentMouseX);
2017-06-11 15:17:25 -07:00
}
base.OnMouseMove(mouseEvent);
}
public override void OnMouseUp(MouseEventArgs mouseEvent)
{
mouseDownOnBar = false;
base.OnMouseUp(mouseEvent);
}
}
private class DockWindowContent : GuiWidget, IIgnoredPopupChild
{
2017-06-12 10:08:13 -07:00
internal DockWindowContent(DockingTabControl parent, GuiWidget child, string title, bool isDocked)
2017-06-11 10:14:32 -07:00
{
2017-06-12 14:19:04 -07:00
var topToBottom = new FlowLayoutWidget(FlowDirection.TopToBottom)
2017-06-11 10:14:32 -07:00
{
VAnchor = VAnchor.ParentBottomTop,
HAnchor = HAnchor.ParentLeftRight
};
2017-06-12 14:19:04 -07:00
if (!isDocked)
2017-06-12 10:08:13 -07:00
{
var titleBar = new FlowLayoutWidget()
{
HAnchor = HAnchor.ParentLeftRight,
VAnchor = VAnchor.FitToChildren,
};
2017-06-12 14:19:04 -07:00
titleBar.AddChild(new TextWidget(title, textColor: ActiveTheme.Instance.PrimaryTextColor)
{
Margin = new BorderDouble(left: 12)
});
titleBar.AddChild(new HorizontalSpacer() { Height = 5, DebugShowBounds = false });
2017-06-12 14:19:04 -07:00
var icon = StaticData.Instance.LoadIcon((isDocked) ? "Pushpin_16x.png" : "PushpinUnpin_16x.png", 16, 16).InvertLightness();
var imageWidget = new ImageWidget(icon);
imageWidget.Margin = new BorderDouble(right: 25, top: 6);
imageWidget.MinimumSize = new Vector2(16, 16);
imageWidget.Click += (s, e) =>
{
parent.ControlIsPinned = !parent.ControlIsPinned;
UiThread.RunOnIdle(() => parent.Rebuild());
};
titleBar.AddChild(imageWidget);
topToBottom.AddChild(titleBar);
}
2017-06-11 10:14:32 -07:00
Width = 500;
Height = 640;
topToBottom.AddChild(child);
2017-06-11 10:14:32 -07:00
AddChild(topToBottom);
}
}
}
2017-06-11 15:17:25 -07:00
}