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;
|
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;
|
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
|
|
|
|
{
|
|
|
|
|
|
public enum MessageType { OK, YES_NO };
|
|
|
|
|
|
|
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-11-08 15:56:37 -08:00
|
|
|
|
DialogWindow.Show(
|
2017-10-18 22:50:57 -07:00
|
|
|
|
new MessageBoxPage(callback, message, caption, messageType, extraWidgetsToAdd, 400, 300, yesOk, noCancel, ApplicationController.Instance.Theme));
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-08 15:56:37 -08:00
|
|
|
|
private class MessageBoxPage : DialogPage
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2017-10-18 22:50:57 -07:00
|
|
|
|
private string unwrappedMessage;
|
|
|
|
|
|
private TextWidget messageContainer;
|
|
|
|
|
|
private Action<bool> responseCallback;
|
2015-10-19 15:59:42 -07:00
|
|
|
|
|
2017-10-18 22:50:57 -07:00
|
|
|
|
private double extraTextScaling = (UserSettings.Instance.IsTouchScreen) ? 1.33333 : 1;
|
|
|
|
|
|
|
|
|
|
|
|
public MessageBoxPage(Action<bool> callback, string message, string caption, MessageType messageType, GuiWidget[] extraWidgetsToAdd, double width, double height, string yesOk, string noCancel, ThemeConfig theme)
|
|
|
|
|
|
: base((noCancel == "") ? "No" : noCancel)
|
2015-10-19 15:59:42 -07:00
|
|
|
|
{
|
2017-10-18 22:50:57 -07:00
|
|
|
|
this.WindowSize = new VectorMath.Vector2(width, height);
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-10-18 22:50:57 -07:00
|
|
|
|
contentRow.AddChild(messageContainer = new TextWidget(message, textColor: ActiveTheme.Instance.PrimaryTextColor, pointSize: 12 * extraTextScaling)
|
|
|
|
|
|
{
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
contentRow.AddChild(widget);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2017-10-18 22:50:57 -07:00
|
|
|
|
Button affirmativeButton = textImageButtonFactory.Generate(yesOk);
|
|
|
|
|
|
affirmativeButton.Click += (s, e) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
UiThread.RunOnIdle(this.WizardWindow.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);
|
|
|
|
|
|
};
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2017-10-18 22:50:57 -07:00
|
|
|
|
this.AddPageAction(affirmativeButton);
|
|
|
|
|
|
|
|
|
|
|
|
switch (messageType)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
double wrappingSize = contentRow.Width - (contentRow.Padding.Width + messageContainer.Margin.Width);
|
|
|
|
|
|
if (wrappingSize > 0)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2017-10-18 22:50:57 -07:00
|
|
|
|
var wrapper = new EnglishTextWrapping(12 * extraTextScaling * GuiWidget.DeviceScale);
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
base.OnCancel(out abortCancel);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2017-10-18 22:50:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
|