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;
|
|
|
|
|
|
using MatterHackers.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;
|
2015-10-19 15:59:42 -07:00
|
|
|
|
using MatterHackers.MatterControl.CustomWidgets;
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl
|
|
|
|
|
|
{
|
2015-04-08 15:20:10 -07:00
|
|
|
|
public class StyledMessageBox : SystemWindow
|
|
|
|
|
|
{
|
2017-10-18 19:43:33 -07:00
|
|
|
|
private string unwrappedMessage;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
private TextWidget messageContainer;
|
|
|
|
|
|
private FlowLayoutWidget middleRowContainer;
|
2017-10-18 19:43:33 -07:00
|
|
|
|
|
2015-08-06 15:20:04 -07:00
|
|
|
|
private Action<bool> responseCallback;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
|
|
|
|
|
public enum MessageType { OK, YES_NO };
|
2017-10-18 19:43:33 -07:00
|
|
|
|
private double extraTextScaling = 1;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2017-10-18 19:54:06 -07:00
|
|
|
|
public static void ShowMessageBox(string message, string caption, MessageType messageType = MessageType.OK, string yesOk = "", string noCancel = "")
|
|
|
|
|
|
{
|
|
|
|
|
|
ShowMessageBox(null, message, caption, null, messageType, yesOk, noCancel);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-18 19:43:33 -07:00
|
|
|
|
public static void ShowMessageBox(Action<bool> callback, string message, string caption, MessageType messageType = MessageType.OK, string yesOk = "", string noCancel = "")
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2017-05-19 14:39:57 -07:00
|
|
|
|
ShowMessageBox(callback, message, caption, null, messageType, yesOk, noCancel);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-19 14:39:57 -07:00
|
|
|
|
public static void ShowMessageBox(Action<bool> callback, string message, string caption, GuiWidget[] extraWidgetsToAdd, MessageType messageType, string yesOk = "", string noCancel = "")
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2017-05-19 14:39:57 -07:00
|
|
|
|
StyledMessageBox messageBox = new StyledMessageBox(callback, message, caption, messageType, extraWidgetsToAdd, 400, 300, yesOk, noCancel);
|
2016-06-20 15:45:17 -07:00
|
|
|
|
messageBox.CenterInParent = true;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
messageBox.ShowAsSystemWindow();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-18 19:43:33 -07:00
|
|
|
|
public StyledMessageBox(Action<bool> callback, string message, string windowTitle, MessageType messageType, GuiWidget[] extraWidgetsToAdd, double width, double height, string yesOk, string noCancel)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
: base(width, height)
|
|
|
|
|
|
{
|
2016-04-29 07:45:15 -07:00
|
|
|
|
if (UserSettings.Instance.IsTouchScreen)
|
2015-10-19 15:59:42 -07:00
|
|
|
|
{
|
|
|
|
|
|
extraTextScaling = 1.33333;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (yesOk == "")
|
|
|
|
|
|
{
|
|
|
|
|
|
if (messageType == MessageType.OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
yesOk = "Ok".Localize();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
yesOk = "Yes".Localize();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-05-19 14:39:57 -07:00
|
|
|
|
if (noCancel == "")
|
2015-10-19 15:59:42 -07:00
|
|
|
|
{
|
2017-05-19 14:39:57 -07:00
|
|
|
|
noCancel = "No".Localize();
|
2015-10-19 15:59:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-04-08 15:20:10 -07:00
|
|
|
|
responseCallback = callback;
|
|
|
|
|
|
unwrappedMessage = message;
|
|
|
|
|
|
FlowLayoutWidget topToBottom = new FlowLayoutWidget(FlowDirection.TopToBottom);
|
|
|
|
|
|
topToBottom.AnchorAll();
|
2016-04-29 07:45:15 -07:00
|
|
|
|
if (UserSettings.Instance.IsTouchScreen)
|
2015-10-19 15:59:42 -07:00
|
|
|
|
{
|
|
|
|
|
|
topToBottom.Padding = new BorderDouble(12, 12, 13, 8);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-05-06 17:56:27 -07:00
|
|
|
|
topToBottom.Padding = new BorderDouble(3, 0, 3, 5);
|
2015-10-19 15:59:42 -07:00
|
|
|
|
}
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
|
|
|
|
|
// Creates Header
|
|
|
|
|
|
FlowLayoutWidget headerRow = new FlowLayoutWidget(FlowDirection.LeftToRight);
|
2017-08-07 15:47:27 -07:00
|
|
|
|
headerRow.HAnchor = HAnchor.Stretch;
|
2016-05-06 17:56:27 -07:00
|
|
|
|
headerRow.Margin = new BorderDouble(0, 3, 0, 0);
|
|
|
|
|
|
headerRow.Padding = new BorderDouble(0, 3, 0, 3);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor;
|
|
|
|
|
|
|
|
|
|
|
|
//Creates Text and adds into header
|
|
|
|
|
|
{
|
2015-10-19 15:59:42 -07:00
|
|
|
|
TextWidget elementHeader = new TextWidget(windowTitle, pointSize: 14 * extraTextScaling);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
elementHeader.TextColor = ActiveTheme.Instance.PrimaryTextColor;
|
2017-08-07 15:47:27 -07:00
|
|
|
|
elementHeader.HAnchor = HAnchor.Stretch;
|
|
|
|
|
|
elementHeader.VAnchor = Agg.UI.VAnchor.Bottom;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
|
|
|
|
|
headerRow.AddChild(elementHeader);
|
|
|
|
|
|
topToBottom.AddChild(headerRow);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Creates container in the middle of window
|
|
|
|
|
|
middleRowContainer = new FlowLayoutWidget(FlowDirection.TopToBottom);
|
|
|
|
|
|
{
|
2017-08-07 15:47:27 -07:00
|
|
|
|
middleRowContainer.HAnchor = HAnchor.Stretch;
|
|
|
|
|
|
middleRowContainer.VAnchor = VAnchor.Stretch;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
// normally the padding for the middle container should be just (5) all around. The has extra top space
|
2016-05-06 17:56:27 -07:00
|
|
|
|
middleRowContainer.Padding = new BorderDouble(5, 5, 5, 15);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
middleRowContainer.BackgroundColor = ActiveTheme.Instance.SecondaryBackgroundColor;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-19 15:59:42 -07:00
|
|
|
|
messageContainer = new TextWidget(message, textColor: ActiveTheme.Instance.PrimaryTextColor, pointSize: 12 * extraTextScaling);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
messageContainer.AutoExpandBoundsToText = true;
|
2017-08-07 15:47:27 -07:00
|
|
|
|
messageContainer.HAnchor = Agg.UI.HAnchor.Left;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
middleRowContainer.AddChild(messageContainer);
|
|
|
|
|
|
|
|
|
|
|
|
if (extraWidgetsToAdd != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (GuiWidget widget in extraWidgetsToAdd)
|
|
|
|
|
|
{
|
|
|
|
|
|
middleRowContainer.AddChild(widget);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
topToBottom.AddChild(middleRowContainer);
|
|
|
|
|
|
|
|
|
|
|
|
//Creates button container on the bottom of window
|
|
|
|
|
|
FlowLayoutWidget buttonRow = new FlowLayoutWidget(FlowDirection.LeftToRight);
|
|
|
|
|
|
{
|
|
|
|
|
|
BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor;
|
2017-08-07 15:47:27 -07:00
|
|
|
|
buttonRow.HAnchor = HAnchor.Stretch;
|
2016-05-06 17:56:27 -07:00
|
|
|
|
buttonRow.Padding = new BorderDouble(0, 3);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
2017-08-03 18:59:21 -07:00
|
|
|
|
var textImageButtonFactory = ApplicationController.Instance.Theme.ButtonFactory;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
|
|
|
|
|
switch (messageType)
|
|
|
|
|
|
{
|
|
|
|
|
|
case MessageType.YES_NO:
|
|
|
|
|
|
{
|
|
|
|
|
|
Title = "MatterControl - " + "Please Confirm".Localize();
|
2017-08-13 14:17:34 -07:00
|
|
|
|
Button yesButton = textImageButtonFactory.Generate(yesOk);
|
2016-09-07 17:00:17 -07:00
|
|
|
|
yesButton.Name = "Yes Button";
|
2017-01-17 14:47:04 -08:00
|
|
|
|
yesButton.Click += okButton_Click;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
yesButton.Cursor = Cursors.Hand;
|
|
|
|
|
|
buttonRow.AddChild(yesButton);
|
|
|
|
|
|
|
2015-10-19 15:59:42 -07:00
|
|
|
|
buttonRow.AddChild(new HorizontalSpacer());
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2017-08-13 14:17:34 -07:00
|
|
|
|
Button noButton = textImageButtonFactory.Generate(noCancel);
|
2016-09-07 17:00:17 -07:00
|
|
|
|
noButton.Name = "No Button";
|
2017-01-17 14:47:04 -08:00
|
|
|
|
noButton.Click += noButton_Click;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
noButton.Cursor = Cursors.Hand;
|
|
|
|
|
|
buttonRow.AddChild(noButton);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case MessageType.OK:
|
|
|
|
|
|
{
|
|
|
|
|
|
Title = "MatterControl - " + "Alert".Localize();
|
2017-08-13 14:17:34 -07:00
|
|
|
|
Button okButton = textImageButtonFactory.Generate(yesOk);
|
2015-10-27 18:32:05 -07:00
|
|
|
|
okButton.Name = "Ok Button";
|
2015-04-08 15:20:10 -07:00
|
|
|
|
okButton.Cursor = Cursors.Hand;
|
2017-01-17 14:47:04 -08:00
|
|
|
|
okButton.Click += okButton_Click;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
buttonRow.AddChild(okButton);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
topToBottom.AddChild(buttonRow);
|
|
|
|
|
|
this.AddChild(topToBottom);
|
|
|
|
|
|
|
|
|
|
|
|
IsModal = true;
|
|
|
|
|
|
AdjustTextWrap();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void OnBoundsChanged(EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
AdjustTextWrap();
|
|
|
|
|
|
base.OnBoundsChanged(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void AdjustTextWrap()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (messageContainer != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
double wrappingSize = middleRowContainer.Width - (middleRowContainer.Padding.Width + messageContainer.Margin.Width);
|
|
|
|
|
|
if (wrappingSize > 0)
|
|
|
|
|
|
{
|
2016-05-06 17:56:27 -07:00
|
|
|
|
EnglishTextWrapping wrapper = new EnglishTextWrapping(12 * extraTextScaling * GuiWidget.DeviceScale);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
string wrappedMessage = wrapper.InsertCRs(unwrappedMessage, wrappingSize);
|
|
|
|
|
|
messageContainer.Text = wrappedMessage;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void noButton_Click(object sender, EventArgs mouseEvent)
|
|
|
|
|
|
{
|
2015-06-11 14:34:26 -07:00
|
|
|
|
UiThread.RunOnIdle(Close);
|
2017-10-18 19:54:06 -07:00
|
|
|
|
responseCallback?.Invoke(false);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void okButton_Click(object sender, EventArgs mouseEvent)
|
|
|
|
|
|
{
|
2015-06-11 14:34:26 -07:00
|
|
|
|
UiThread.RunOnIdle(Close);
|
2017-10-18 19:54:06 -07:00
|
|
|
|
responseCallback?.Invoke(true);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|