2017-09-28 08:59:57 -07:00
|
|
|
|
/*
|
2019-06-19 13:35:45 -07:00
|
|
|
|
Copyright (c) 2019 Lars Brubaker, John Lewin
|
2017-09-28 08:59:57 -07:00
|
|
|
|
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.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2019-06-19 13:35:45 -07:00
|
|
|
|
using System;
|
2017-09-28 08:59:57 -07:00
|
|
|
|
using MatterHackers.Agg;
|
|
|
|
|
|
using MatterHackers.Agg.UI;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl.PartPreviewWindow
|
|
|
|
|
|
{
|
2018-10-11 15:04:03 -07:00
|
|
|
|
public class PopupButton : GuiWidget, IIgnoredPopupChild, IMenuCreator
|
2017-09-28 08:59:57 -07:00
|
|
|
|
{
|
2018-04-09 13:15:17 -07:00
|
|
|
|
protected GuiWidget buttonView;
|
2018-12-03 13:23:24 -08:00
|
|
|
|
private bool menuVisibileAtMouseDown = false;
|
2017-09-28 08:59:57 -07:00
|
|
|
|
private PopupWidget popupWidget;
|
2018-12-03 13:23:24 -08:00
|
|
|
|
private bool overridePopupHAnchor = false;
|
|
|
|
|
|
private bool overridePopupVAnchor = false;
|
2017-09-28 08:59:57 -07:00
|
|
|
|
|
2021-01-06 11:55:34 -08:00
|
|
|
|
public bool MenuVisible { get; private set; } = false;
|
|
|
|
|
|
|
2017-09-28 08:59:57 -07:00
|
|
|
|
public PopupButton()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public PopupButton(GuiWidget buttonView)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.HAnchor = HAnchor.Fit;
|
2018-01-21 21:07:14 -08:00
|
|
|
|
this.VAnchor = VAnchor.Fit | VAnchor.Center;
|
2017-09-28 08:59:57 -07:00
|
|
|
|
this.buttonView = buttonView;
|
|
|
|
|
|
this.buttonView.Selectable = false;
|
|
|
|
|
|
|
|
|
|
|
|
this.AddChild(buttonView);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
public event EventHandler BeforePopup;
|
2017-09-28 08:59:57 -07:00
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
public event EventHandler PopupWindowClosed;
|
|
|
|
|
|
|
|
|
|
|
|
public bool AlignToRightEdge { get; set; }
|
2019-06-19 13:35:45 -07:00
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
public bool AlwaysKeepOpen { get; set; }
|
2018-04-09 13:15:17 -07:00
|
|
|
|
|
2018-10-25 15:31:31 -07:00
|
|
|
|
public override Color BackgroundColor
|
|
|
|
|
|
{
|
2021-01-06 11:55:34 -08:00
|
|
|
|
get => MenuVisible ? this.OpenColor : base.BackgroundColor;
|
2018-10-25 15:31:31 -07:00
|
|
|
|
set => base.BackgroundColor = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
public virtual Func<GuiWidget> DynamicPopupContent { get; set; }
|
2019-06-19 13:35:45 -07:00
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
public Color HoverColor { get; set; } = new Color(0, 0, 0, 40);
|
|
|
|
|
|
|
2021-01-06 11:55:34 -08:00
|
|
|
|
public bool KeepMenuOpen => MenuVisible || this.AlwaysKeepOpen;
|
2019-06-19 13:35:45 -07:00
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
public bool MakeScrollable { get; set; } = true;
|
2019-06-19 13:35:45 -07:00
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
public Color OpenColor { get; set; } = new Color(0, 0, 0, 40);
|
2019-06-19 13:35:45 -07:00
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
public Direction PopDirection { get; set; } = Direction.Down;
|
2019-06-19 13:35:45 -07:00
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
public Color PopupBorderColor { get; set; } = Color.Transparent;
|
2019-06-19 13:35:45 -07:00
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
public virtual GuiWidget PopupContent { get; set; }
|
2019-06-19 13:35:45 -07:00
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
private HAnchor _popupHAnchor;
|
2019-06-19 13:35:45 -07:00
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
public HAnchor PopupHAnchor
|
2017-09-28 08:59:57 -07:00
|
|
|
|
{
|
2018-12-03 13:23:24 -08:00
|
|
|
|
get => _popupHAnchor;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
overridePopupHAnchor = true;
|
|
|
|
|
|
_popupHAnchor = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-06-19 13:35:45 -07:00
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
public IPopupLayoutEngine PopupLayoutEngine { get; set; }
|
2019-06-19 13:35:45 -07:00
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
private VAnchor _popupVAnchor;
|
2019-06-19 13:35:45 -07:00
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
public VAnchor PopupVAnchor
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _popupVAnchor;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
overridePopupVAnchor = true;
|
|
|
|
|
|
_popupVAnchor = value;
|
|
|
|
|
|
}
|
2017-09-28 08:59:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
public void CloseMenu() => popupWidget?.CloseMenu();
|
|
|
|
|
|
|
2019-06-04 11:02:50 -07:00
|
|
|
|
protected override void OnClick(MouseEventArgs mouseEvent)
|
2017-09-28 08:59:57 -07:00
|
|
|
|
{
|
2018-10-07 11:36:52 -07:00
|
|
|
|
if (!menuVisibileAtMouseDown)
|
2017-09-28 08:59:57 -07:00
|
|
|
|
{
|
2019-06-29 08:33:24 -07:00
|
|
|
|
this.ShowPopup();
|
2017-09-28 08:59:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-06 11:55:34 -08:00
|
|
|
|
menuVisibileAtMouseDown = false;
|
2018-10-07 11:36:52 -07:00
|
|
|
|
base.OnClick(mouseEvent);
|
2017-09-28 08:59:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-15 21:13:30 -08:00
|
|
|
|
public override void OnClosed(EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.PopupContent?.Close();
|
|
|
|
|
|
base.OnClosed(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
public override void OnMouseDown(MouseEventArgs mouseEvent)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Store the menu state at the time of mousedown
|
2021-01-06 11:55:34 -08:00
|
|
|
|
menuVisibileAtMouseDown = MenuVisible;
|
2018-12-03 13:23:24 -08:00
|
|
|
|
base.OnMouseDown(mouseEvent);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-28 08:59:57 -07:00
|
|
|
|
public void ShowPopup()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (PopupLayoutEngine == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
PopupLayoutEngine = new PopupLayoutEngine(this.PopupContent, this, this.PopDirection, 0, this.AlignToRightEdge);
|
|
|
|
|
|
}
|
2019-06-19 13:35:45 -07:00
|
|
|
|
|
2021-01-06 11:55:34 -08:00
|
|
|
|
MenuVisible = true;
|
2017-09-28 08:59:57 -07:00
|
|
|
|
|
|
|
|
|
|
this.PopupContent?.ClearRemovedFlag();
|
|
|
|
|
|
|
|
|
|
|
|
if (this.DynamicPopupContent != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.PopupContent = this.DynamicPopupContent();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-14 13:27:21 -07:00
|
|
|
|
if (this.PopupContent == null
|
|
|
|
|
|
|| this.PopupContent.Children.Count <= 0)
|
2017-09-28 08:59:57 -07:00
|
|
|
|
{
|
2021-01-06 11:55:34 -08:00
|
|
|
|
MenuVisible = false;
|
2017-09-28 08:59:57 -07:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-19 17:06:33 -08:00
|
|
|
|
this.OnBeforePopup();
|
2017-09-28 08:59:57 -07:00
|
|
|
|
|
|
|
|
|
|
popupWidget = new PopupWidget(this.PopupContent, PopupLayoutEngine, MakeScrollable)
|
|
|
|
|
|
{
|
|
|
|
|
|
BorderWidth = 1,
|
2018-04-09 13:15:17 -07:00
|
|
|
|
BorderColor = this.PopupBorderColor,
|
2017-09-28 08:59:57 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
popupWidget.Closed += (s, e) =>
|
|
|
|
|
|
{
|
2021-01-06 11:55:34 -08:00
|
|
|
|
MenuVisible = false;
|
2017-09-28 08:59:57 -07:00
|
|
|
|
popupWidget = null;
|
2017-11-07 11:31:30 -08:00
|
|
|
|
|
|
|
|
|
|
this.PopupWindowClosed?.Invoke(this, null);
|
2018-12-19 18:00:13 -08:00
|
|
|
|
|
|
|
|
|
|
// If DynamicPopupContent is used, we need to close the PopupContent reference as it's constructed every time
|
|
|
|
|
|
if (this.DynamicPopupContent != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.PopupContent.Close();
|
|
|
|
|
|
}
|
2017-09-28 08:59:57 -07:00
|
|
|
|
};
|
2018-03-14 13:23:19 -07:00
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
if (overridePopupHAnchor)
|
|
|
|
|
|
{
|
|
|
|
|
|
popupWidget.HAnchor = PopupHAnchor;
|
|
|
|
|
|
}
|
2019-06-19 13:35:45 -07:00
|
|
|
|
|
2018-12-03 13:23:24 -08:00
|
|
|
|
if (overridePopupVAnchor)
|
|
|
|
|
|
{
|
|
|
|
|
|
popupWidget.VAnchor = PopupVAnchor;
|
|
|
|
|
|
}
|
2018-10-16 16:23:25 -07:00
|
|
|
|
|
2017-09-28 08:59:57 -07:00
|
|
|
|
popupWidget.Focus();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-19 17:06:33 -08:00
|
|
|
|
protected virtual void OnBeforePopup()
|
2017-09-28 08:59:57 -07:00
|
|
|
|
{
|
2017-12-19 17:06:33 -08:00
|
|
|
|
this.BeforePopup?.Invoke(this, null);
|
2017-09-28 08:59:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|