mattercontrol/original/MatterControlLib/ControlElements/StyledMessageBoxWindow.cs

377 lines
10 KiB
C#
Raw Normal View History

/*
Copyright (c) 2017, Lars Brubaker, Kevin Pope, 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.
*/
using System;
using Markdig.Agg;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.Font;
2015-04-08 15:20:10 -07:00
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.VectorMath;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl
{
public static class StyledMessageBox
2015-04-08 15:20:10 -07:00
{
2020-07-13 18:08:09 -07:00
public enum MessageType
{
OK,
YES_NO,
YES_NO_WITHOUT_HIGHLIGHT
}
2015-04-08 15:20:10 -07:00
2020-07-13 18:08:09 -07:00
public static void ShowMessageBox(string message,
string caption,
MessageType messageType = MessageType.OK,
string yesOk = "",
string noCancel = "",
bool useMarkdown = false,
int width = 400,
int height = 300)
{
2020-07-13 18:08:09 -07:00
ShowMessageBox(null, message, caption, null, messageType, yesOk, noCancel, useMarkdown, 0, width, height);
}
public static void ShowMessageBox(Action<bool> callback,
string message,
string caption,
MessageType messageType = MessageType.OK,
2020-07-13 18:08:09 -07:00
string yesOk = "",
string noCancel = "",
bool useMarkdown = false,
2020-07-13 18:08:09 -07:00
int instanceIndex = 0,
int width = 400,
int height = 300)
2015-04-08 15:20:10 -07:00
{
2020-07-13 18:08:09 -07:00
ShowMessageBox(callback, message, caption, null, messageType, yesOk, noCancel, useMarkdown, instanceIndex, width, height);
2015-04-08 15:20:10 -07:00
}
public static void ShowMessageBox(Action<bool> callback,
string message,
string caption,
GuiWidget[] extraWidgetsToAdd,
MessageType messageType,
string yesOk = "",
string noCancel = "",
bool useMarkdown = false,
2020-07-13 18:08:09 -07:00
int instanceIndex = 0,
int width = 400,
int height = 300)
2015-04-08 15:20:10 -07:00
{
DialogWindow.Show(
2020-07-13 18:08:09 -07:00
new MessageBoxPage(callback, message, caption, messageType, extraWidgetsToAdd, width, height, yesOk, noCancel, ApplicationController.Instance.Theme, useMarkdown),
instanceIndex);
2015-04-08 15:20:10 -07:00
}
public class MessageBoxPage : DialogPage
2015-04-08 15:20:10 -07:00
{
private string unwrappedMessage;
private GuiWidget messageContainer;
private Action<bool> responseCallback;
2020-07-13 18:08:09 -07:00
private bool haveResponded = false;
public MessageBoxPage(Action<bool> callback, string message, string caption, MessageType messageType, GuiWidget[] extraWidgetsToAdd, double width, double height, string yesOk, string noCancel, ThemeConfig theme, bool useMarkdown = false)
: base((noCancel == "") ? "No".Localize() : noCancel)
{
2020-07-25 11:00:56 -07:00
this.WindowSize = new Vector2(width * GuiWidget.DeviceScale, height * GuiWidget.DeviceScale);
if (yesOk == "")
{
yesOk = (messageType == MessageType.OK) ? "Ok".Localize() : "Yes".Localize();
}
this.HeaderText = caption;
2020-07-13 18:08:09 -07:00
// this.IsModal = true;
2015-04-08 15:20:10 -07:00
responseCallback = callback;
unwrappedMessage = message;
2015-04-08 15:20:10 -07:00
if (useMarkdown)
{
contentRow.AddChild(messageContainer = new MarkdownWidget(theme)
{
Markdown = message,
});
}
else
{
var scrollable = new ScrollableWidget(true);
scrollable.AnchorAll();
scrollable.ScrollArea.HAnchor = HAnchor.Stretch;
contentRow.AddChild(scrollable);
2020-08-04 08:15:20 -07:00
scrollable.AddChild(messageContainer = new TextWidget(message, textColor: theme.TextColor, pointSize: 12)
{
AutoExpandBoundsToText = true,
HAnchor = HAnchor.Left
});
}
2015-04-08 15:20:10 -07:00
if (extraWidgetsToAdd != null)
{
foreach (GuiWidget widget in extraWidgetsToAdd)
{
contentRow.AddChild(widget);
}
}
2015-04-08 15:20:10 -07:00
2018-04-14 20:51:01 -07:00
var affirmativeButton = theme.CreateDialogButton(yesOk);
affirmativeButton.Click += (s, e) =>
{
// If applicable, invoke the callback
responseCallback?.Invoke(true);
2018-07-17 16:20:43 -07:00
haveResponded = true;
this.DialogWindow.Close();
};
2015-04-08 15:20:10 -07:00
this.AddPageAction(affirmativeButton, messageType != MessageType.YES_NO_WITHOUT_HIGHLIGHT);
switch (messageType)
2015-04-08 15:20:10 -07:00
{
case MessageType.YES_NO_WITHOUT_HIGHLIGHT:
case MessageType.YES_NO:
this.WindowTitle = "MatterControl - " + "Please Confirm".Localize();
affirmativeButton.Name = "Yes Button";
this.SetCancelButtonName("No Button");
break;
case MessageType.OK:
this.WindowTitle = "MatterControl - " + "Alert".Localize();
affirmativeButton.Name = "Ok Button";
this.HideCancelButton();
break;
default:
throw new NotImplementedException();
2015-04-08 15:20:10 -07:00
}
this.AdjustTextWrap();
}
2015-04-08 15:20:10 -07:00
public override void OnBoundsChanged(EventArgs e)
2015-04-08 15:20:10 -07:00
{
AdjustTextWrap();
base.OnBoundsChanged(e);
2015-04-08 15:20:10 -07:00
}
2014-01-29 19:09:30 -08:00
private void AdjustTextWrap()
2015-04-08 15:20:10 -07:00
{
2020-07-11 19:28:30 -07:00
if (messageContainer != null
&& messageContainer is TextWidget textWidget)
{
double wrappingSize = contentRow.Width - (contentRow.Padding.Width + messageContainer.Margin.Width);
if (wrappingSize > 0)
2015-04-08 15:20:10 -07:00
{
2020-07-11 19:28:30 -07:00
var wrapper = new EnglishTextWrapping(textWidget.PointSize * GuiWidget.DeviceScale);
messageContainer.Text = wrapper.InsertCRs(unwrappedMessage, wrappingSize);
2015-04-08 15:20:10 -07:00
}
}
2015-04-08 15:20:10 -07:00
}
public override void OnClosed(EventArgs e)
2018-07-17 16:20:43 -07:00
{
if (!haveResponded)
{
responseCallback?.Invoke(false);
}
2020-07-13 18:08:09 -07:00
2018-07-17 16:20:43 -07:00
base.OnClosed(e);
}
protected override void OnCancel(out bool abortCancel)
2015-04-08 15:20:10 -07:00
{
responseCallback?.Invoke(false);
2018-07-17 16:20:43 -07:00
haveResponded = true;
base.OnCancel(out abortCancel);
2015-04-08 15:20:10 -07:00
}
}
public enum ResponseType
{
YES,
NO,
CANCEL
}
public static void ShowYNCMessageBox(Action<ResponseType> callback,
string message,
string caption,
string yesText = "",
string noText = "",
string cancelText = "",
bool useMarkdown = false,
int instanceIndex = 0,
int width = 450,
int height = 300)
{
ShowYNCMessageBox(callback, message, caption, null, yesText, noText, cancelText, useMarkdown, instanceIndex, width, height);
}
public static void ShowYNCMessageBox(Action<ResponseType> callback,
string message,
string caption,
GuiWidget[] extraWidgetsToAdd,
string yesText = "",
string noText = "",
string cancelText = "",
bool useMarkdown = false,
int instanceIndex = 0,
int width = 450,
int height = 300)
{
DialogWindow.Show(
new YesNoCancelMessageBox(callback, message, caption, extraWidgetsToAdd, width, height, yesText, noText, cancelText, ApplicationController.Instance.Theme, useMarkdown),
instanceIndex);
}
public class YesNoCancelMessageBox : DialogPage
{
private string unwrappedMessage;
private GuiWidget messageContainer;
private Action<ResponseType> responseCallback;
private bool haveResponded = false;
public YesNoCancelMessageBox(Action<ResponseType> callback, string message, string caption, GuiWidget[] extraWidgetsToAdd, double width, double height, string yesText, string noText, string cancelText, ThemeConfig theme, bool useMarkdown = false)
: base((cancelText == "") ? "Cancel".Localize() : cancelText)
{
this.WindowSize = new Vector2(width * GuiWidget.DeviceScale, height * GuiWidget.DeviceScale);
if (yesText == "")
{
yesText = "Yes".Localize();
}
this.HeaderText = caption;
// this.IsModal = true;
responseCallback = callback;
unwrappedMessage = message;
if (useMarkdown)
{
contentRow.AddChild(messageContainer = new MarkdownWidget(theme)
{
Markdown = message,
});
}
else
{
var scrollable = new ScrollableWidget(true);
scrollable.AnchorAll();
scrollable.ScrollArea.HAnchor = HAnchor.Stretch;
contentRow.AddChild(scrollable);
scrollable.AddChild(messageContainer = new TextWidget(message, textColor: theme.TextColor, pointSize: 12)
{
AutoExpandBoundsToText = true,
HAnchor = HAnchor.Left
});
}
if (extraWidgetsToAdd != null)
{
foreach (GuiWidget widget in extraWidgetsToAdd)
{
contentRow.AddChild(widget);
}
}
var yesButton = theme.CreateDialogButton(yesText);
2022-02-03 17:21:51 -08:00
yesButton.Name = "Yes Button";
yesButton.Click += (s, e) =>
{
// If applicable, invoke the callback
responseCallback?.Invoke(ResponseType.YES);
haveResponded = true;
this.DialogWindow.Close();
};
this.AddPageAction(yesButton, true);
var noButton = theme.CreateDialogButton(noText);
2022-02-03 17:21:51 -08:00
noButton.Name = "No Button";
noButton.Click += (s, e) =>
{
// If applicable, invoke the callback
responseCallback?.Invoke(ResponseType.NO);
haveResponded = true;
this.DialogWindow.Close();
};
this.AddPageAction(noButton);
this.WindowTitle = "MatterControl - " + "Please Confirm".Localize();
2022-02-03 17:21:51 -08:00
this.SetCancelButtonName("Cancel Button");
this.AdjustTextWrap();
}
public override void OnBoundsChanged(EventArgs e)
{
AdjustTextWrap();
base.OnBoundsChanged(e);
}
private void AdjustTextWrap()
{
if (messageContainer != null
&& messageContainer is TextWidget textWidget)
{
double wrappingSize = contentRow.Width - (contentRow.Padding.Width + messageContainer.Margin.Width);
if (wrappingSize > 0)
{
var wrapper = new EnglishTextWrapping(textWidget.PointSize * GuiWidget.DeviceScale);
messageContainer.Text = wrapper.InsertCRs(unwrappedMessage, wrappingSize);
}
}
}
public override void OnClosed(EventArgs e)
{
if (!haveResponded)
{
responseCallback?.Invoke(ResponseType.CANCEL);
}
base.OnClosed(e);
}
protected override void OnCancel(out bool abortCancel)
{
responseCallback?.Invoke(ResponseType.CANCEL);
haveResponded = true;
base.OnCancel(out abortCancel);
}
}
2015-04-08 15:20:10 -07:00
}
}