2017-10-18 19:43:33 -07:00
|
|
|
|
/*
|
|
|
|
|
|
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;
|
2018-12-19 16:43:39 -08:00
|
|
|
|
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;
|
2014-02-05 18:29:58 -08:00
|
|
|
|
using MatterHackers.Localizations;
|
2019-01-18 09:49:38 -08:00
|
|
|
|
using MatterHackers.MatterControl.CustomWidgets;
|
2018-03-08 17:23:03 -08:00
|
|
|
|
using MatterHackers.VectorMath;
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl
|
|
|
|
|
|
{
|
2017-10-18 22:50:57 -07:00
|
|
|
|
public static class StyledMessageBox
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2018-06-26 08:47:12 -07:00
|
|
|
|
public enum MessageType { OK, YES_NO, YES_NO_WITHOUT_HIGHLIGHT };
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2019-01-03 16:58:05 -08:00
|
|
|
|
public static void ShowMessageBox(string message, string caption, MessageType messageType = MessageType.OK, string yesOk = "", string noCancel = "", bool useMarkdown = false)
|
2017-10-18 19:54:06 -07:00
|
|
|
|
{
|
2019-01-03 16:58:05 -08:00
|
|
|
|
ShowMessageBox(null, message, caption, null, messageType, yesOk, noCancel, useMarkdown);
|
2017-10-18 19:54:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-03 16:58:05 -08:00
|
|
|
|
public static void ShowMessageBox(Action<bool> callback, string message, string caption, MessageType messageType = MessageType.OK, string yesOk = "", string noCancel = "", bool useMarkdown = false)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2019-01-03 16:58:05 -08:00
|
|
|
|
ShowMessageBox(callback, message, caption, null, messageType, yesOk, noCancel, useMarkdown);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-03 16:58:05 -08:00
|
|
|
|
public static void ShowMessageBox(Action<bool> callback, string message, string caption, GuiWidget[] extraWidgetsToAdd, MessageType messageType, string yesOk = "", string noCancel = "", bool useMarkdown = false)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2017-11-08 15:56:37 -08:00
|
|
|
|
DialogWindow.Show(
|
2019-01-03 16:58:05 -08:00
|
|
|
|
new MessageBoxPage(callback, message, caption, messageType, extraWidgetsToAdd, 400, 300, yesOk, noCancel, ApplicationController.Instance.Theme, useMarkdown));
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-18 12:13:31 -08:00
|
|
|
|
public class MessageBoxPage : DialogPage
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2017-10-18 22:50:57 -07:00
|
|
|
|
private string unwrappedMessage;
|
2019-01-03 16:58:05 -08:00
|
|
|
|
private GuiWidget messageContainer;
|
2017-10-18 22:50:57 -07:00
|
|
|
|
private Action<bool> responseCallback;
|
2018-07-17 16:20:43 -07:00
|
|
|
|
bool haveResponded = false;
|
2015-10-19 15:59:42 -07:00
|
|
|
|
|
2019-01-03 16:58:05 -08:00
|
|
|
|
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)
|
2017-12-04 10:39:08 -08:00
|
|
|
|
: base((noCancel == "") ? "No".Localize() : noCancel)
|
2015-10-19 15:59:42 -07:00
|
|
|
|
{
|
2018-03-08 17:23:03 -08:00
|
|
|
|
this.WindowSize = new Vector2(width, height);
|
2017-10-18 22:50:57 -07:00
|
|
|
|
|
|
|
|
|
|
if (yesOk == "")
|
2015-10-19 15:59:42 -07:00
|
|
|
|
{
|
2017-10-18 22:50:57 -07:00
|
|
|
|
yesOk = (messageType == MessageType.OK) ? "Ok".Localize() : "Yes".Localize();
|
2015-10-19 15:59:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-18 22:50:57 -07:00
|
|
|
|
this.HeaderText = caption;
|
2017-10-18 23:09:14 -07:00
|
|
|
|
//this.IsModal = true;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2017-10-18 22:50:57 -07:00
|
|
|
|
responseCallback = callback;
|
|
|
|
|
|
unwrappedMessage = message;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2019-01-03 17:08:49 -08:00
|
|
|
|
var scrollable = new ScrollableWidget(true);
|
|
|
|
|
|
scrollable.AnchorAll();
|
|
|
|
|
|
scrollable.ScrollArea.HAnchor = HAnchor.Stretch;
|
|
|
|
|
|
contentRow.AddChild(scrollable);
|
|
|
|
|
|
|
2019-01-03 16:58:05 -08:00
|
|
|
|
if (useMarkdown)
|
2017-10-18 22:50:57 -07:00
|
|
|
|
{
|
2019-01-03 17:08:49 -08:00
|
|
|
|
scrollable.AddChild(messageContainer = new MarkdownWidget(theme)
|
2019-01-03 16:58:05 -08:00
|
|
|
|
{
|
|
|
|
|
|
Markdown = message,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2019-01-03 17:08:49 -08:00
|
|
|
|
scrollable.AddChild(messageContainer = new TextWidget(message, textColor: theme.TextColor, pointSize: 12 * DeviceScale)
|
2019-01-03 16:58:05 -08:00
|
|
|
|
{
|
|
|
|
|
|
AutoExpandBoundsToText = true,
|
|
|
|
|
|
HAnchor = HAnchor.Left
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2017-10-18 22:50:57 -07:00
|
|
|
|
if (extraWidgetsToAdd != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (GuiWidget widget in extraWidgetsToAdd)
|
|
|
|
|
|
{
|
2019-01-18 09:49:38 -08:00
|
|
|
|
contentRow.AddChild(widget);
|
2017-10-18 22:50:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2018-04-14 20:51:01 -07:00
|
|
|
|
var affirmativeButton = theme.CreateDialogButton(yesOk);
|
2017-10-18 22:50:57 -07:00
|
|
|
|
affirmativeButton.Click += (s, e) =>
|
|
|
|
|
|
{
|
2018-06-19 15:02:25 -07:00
|
|
|
|
UiThread.RunOnIdle(this.DialogWindow.Close);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2017-10-18 22:50:57 -07:00
|
|
|
|
// If applicable, invoke the callback
|
|
|
|
|
|
responseCallback?.Invoke(true);
|
2018-07-17 16:20:43 -07:00
|
|
|
|
haveResponded = true;
|
2017-10-18 22:50:57 -07:00
|
|
|
|
};
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2018-06-26 08:47:12 -07:00
|
|
|
|
this.AddPageAction(affirmativeButton, messageType != MessageType.YES_NO_WITHOUT_HIGHLIGHT);
|
2017-10-18 22:50:57 -07:00
|
|
|
|
|
|
|
|
|
|
switch (messageType)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2018-06-26 08:47:12 -07:00
|
|
|
|
case MessageType.YES_NO_WITHOUT_HIGHLIGHT:
|
2017-10-18 22:50:57 -07:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-18 22:50:57 -07:00
|
|
|
|
this.AdjustTextWrap();
|
|
|
|
|
|
}
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2017-10-18 22:50:57 -07:00
|
|
|
|
public override void OnBoundsChanged(EventArgs e)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2017-10-18 22:50:57 -07:00
|
|
|
|
AdjustTextWrap();
|
|
|
|
|
|
base.OnBoundsChanged(e);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
2017-10-18 22:50:57 -07:00
|
|
|
|
private void AdjustTextWrap()
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2017-10-18 22:50:57 -07:00
|
|
|
|
if (messageContainer != null)
|
|
|
|
|
|
{
|
2018-11-03 10:12:27 -07:00
|
|
|
|
double wrappingSize = contentRow.Width - (contentRow.Padding.Width + messageContainer.Margin.Width);
|
2017-10-18 22:50:57 -07:00
|
|
|
|
if (wrappingSize > 0)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2017-12-11 21:09:40 -08:00
|
|
|
|
var wrapper = new EnglishTextWrapping(12 * GuiWidget.DeviceScale);
|
2017-10-18 22:50:57 -07:00
|
|
|
|
messageContainer.Text = wrapper.InsertCRs(unwrappedMessage, wrappingSize);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
2017-10-18 22:50:57 -07:00
|
|
|
|
}
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-23 16:44:11 -07:00
|
|
|
|
public override void OnClosed(EventArgs e)
|
2018-07-17 16:20:43 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (!haveResponded)
|
|
|
|
|
|
{
|
|
|
|
|
|
responseCallback?.Invoke(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
base.OnClosed(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-18 22:50:57 -07:00
|
|
|
|
protected override void OnCancel(out bool abortCancel)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2017-10-18 22:50:57 -07:00
|
|
|
|
responseCallback?.Invoke(false);
|
2018-07-17 16:20:43 -07:00
|
|
|
|
haveResponded = true;
|
2017-10-18 22:50:57 -07:00
|
|
|
|
base.OnCancel(out abortCancel);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2017-10-18 22:50:57 -07:00
|
|
|
|
}
|