mattercontrol/CustomWidgets/DockingTabControl.cs

225 lines
6.7 KiB
C#
Raw Normal View History

/*
Copyright (c) 2017, Lars Brubaker
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.
*/
using MatterHackers.Agg;
using MatterHackers.Agg.Font;
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;
2017-06-12 10:01:50 -07:00
using System.Collections.Generic;
using System;
namespace MatterHackers.MatterControl.CustomWidgets
{
public class DockingTabControl : GuiWidget
{
2017-06-12 10:01:50 -07:00
public bool ControlIsPinned { get; set; } = true;
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()
{
// load up the state data for this control and printer
// ActiveSliceSettings.Instance.PrinterSelected
2017-06-12 10:01:50 -07:00
ControlIsPinned = true;
2017-06-11 15:17:25 -07:00
}
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-11 15:17:25 -07:00
};
2017-06-12 10:01:50 -07:00
tabControl = new TabControl();
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:01:50 -07:00
var content = new DockWindowContent(this, nameWidget.Value, nameWidget.Key);
tabControl.AddTab(new TabPage(content, nameWidget.Key), nameWidget.Key);
}
else // control is floating
{
TypeFacePrinter stringPrinter = new TypeFacePrinter(nameWidget.Key, 12);
var stringPrinter2 = new VertexSourceApplyTransform(stringPrinter, Affine.NewTranslation(new Vector2(200, 200)));
//graphics2D.Render(stringPrinter2, RGBA_Bytes.Black);
var stringPrinter3 = new VertexSourceApplyTransform(stringPrinter, Affine.NewRotation(MathHelper.DegreesToRadians(-90)));
var bounds = stringPrinter3.Bounds();
stringPrinter3.Transform = ((Affine)stringPrinter3.Transform) * Affine.NewTranslation(new Vector2(0, -bounds.Bottom + 0));
GuiWidget optionsText = new GuiWidget(bounds.Width, bounds.Height)
{
DoubleBuffer = true,
//BackgroundColor = RGBA_Bytes.Green,
};
optionsText.AfterDraw += (s, e) =>
{
e.graphics2D.Render(stringPrinter3, RGBA_Bytes.Black);
2017-06-11 15:17:25 -07:00
//e.graphics2D.DrawString(name, 0, 0);
};
2017-06-12 10:01:50 -07:00
PopupButton settingsButton = new PopupButton(optionsText)
{
AlignToRightEdge = true,
};
2017-06-11 15:17:25 -07:00
2017-06-12 10:01:50 -07:00
settingsButton.PopupContent = new DockWindowContent(this, nameWidget.Value, nameWidget.Key);
2017-06-11 15:17:25 -07:00
2017-06-12 10:01:50 -07:00
topToBottom.AddChild(settingsButton);
}
2017-06-11 15:17:25 -07:00
}
}
public override void Initialize()
{
base.Initialize();
Width = 30;
VAnchor = VAnchor.ParentBottomTop;
HAnchor = HAnchor.FitToChildren;
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()
{
Padding = new BorderDouble(resizeWidth, 0, 0, 0);
HAnchor = HAnchor.AbsolutePosition;
}
public override void OnDraw(Graphics2D graphics2D)
{
graphics2D.FillRectangle(LocalBounds.Left, LocalBounds.Bottom, LocalBounds.Left + resizeWidth, LocalBounds.Top, RGBA_Bytes.Black);
base.OnDraw(graphics2D);
}
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:01:50 -07:00
internal DockWindowContent(DockingTabControl parent, GuiWidget child, string title)
2017-06-11 10:14:32 -07:00
{
FlowLayoutWidget topToBottom = new FlowLayoutWidget(FlowDirection.TopToBottom)
{
VAnchor = VAnchor.ParentBottomTop,
HAnchor = HAnchor.ParentLeftRight
};
FlowLayoutWidget titleBar = new FlowLayoutWidget()
{
HAnchor = HAnchor.ParentLeftRight
};
titleBar.AddChild(new TextWidget(title));
titleBar.AddChild(new GuiWidget() { HAnchor = HAnchor.ParentLeftRight });
2017-06-12 10:01:50 -07:00
var checkBox = new CheckBox("[pin icon]");
titleBar.AddChild(checkBox);
checkBox.CheckedStateChanged += (s, e) =>
{
parent.ControlIsPinned = !parent.ControlIsPinned;
parent.Rebuild();
};
2017-06-11 10:14:32 -07:00
topToBottom.AddChild(titleBar);
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
}