Files
duplicati/WizardForm/WizardControl.cs
T

261 lines
8.7 KiB
C#
Raw Normal View History

#region Disclaimer / License
2011-05-09 19:52:28 +00:00
// Copyright (C) 2011, Kenneth Skovhede
// http://www.hexad.dk, opensource@hexad.dk
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace System.Windows.Forms.Wizard
{
2009-02-01 23:20:07 +00:00
public class WizardControl : UserControl, IWizardControl
{
2009-02-01 23:20:07 +00:00
protected string m_title;
protected string m_helpText;
protected Image m_image;
protected bool m_fullSize;
protected bool m_autoFillValues = true;
protected bool m_valuesAutoLoaded = false;
protected IWizardForm m_owner;
protected Dictionary<string, object> m_settings;
protected event PageChangeHandler PageEnter;
protected event PageChangeHandler PageLeave;
protected event PageChangeHandler PageDisplay;
2009-02-01 23:20:07 +00:00
//Windows forms designer support
private WizardControl()
{
}
2009-02-01 23:20:07 +00:00
public WizardControl(string title, string helptext)
: this(title, helptext, null)
{
}
2009-02-01 23:20:07 +00:00
public WizardControl(string title, string helptext, Image image)
: this(title, helptext, image, false)
{
}
public WizardControl(string title, string helptext, Image image, bool fullsize)
{
m_title = title;
m_helpText = helptext;
m_image = image;
m_fullSize = fullsize;
}
#region IWizardControl Members
2009-02-01 23:20:07 +00:00
public virtual Control Control
{
2009-02-01 23:20:07 +00:00
get { return this; }
}
2009-02-01 23:20:07 +00:00
public virtual string Title
{
get { return m_title; }
}
2009-02-01 23:20:07 +00:00
public virtual string HelpText
{
get { return m_helpText; }
}
2009-02-01 23:20:07 +00:00
public virtual Image Image
{
get { return m_image; }
}
2009-02-01 23:20:07 +00:00
public virtual bool FullSize
{
get { return m_fullSize; }
}
void IWizardControl.Display(IWizardForm owner, PageChangedArgs args)
{
if (PageDisplay != null)
PageDisplay(owner, args);
}
2009-02-01 23:20:07 +00:00
void IWizardControl.Enter(IWizardForm owner, PageChangedArgs args)
2008-06-18 21:37:21 +00:00
{
2009-02-01 23:20:07 +00:00
m_owner = args.Owner;
m_settings = args.Settings;
if (m_autoFillValues)
m_valuesAutoLoaded = LoadDialogSettings();
if (PageEnter != null)
PageEnter(owner, args);
2008-06-18 21:37:21 +00:00
}
2009-02-01 23:20:07 +00:00
void IWizardControl.Leave(IWizardForm owner, PageChangedArgs args)
{
2009-02-01 23:20:07 +00:00
if (PageLeave != null)
PageLeave(owner, args);
if (m_autoFillValues)
SaveDialogSettings();
}
#endregion
2009-02-01 23:20:07 +00:00
2010-12-12 12:15:19 +00:00
protected Dictionary<string, Control> FindAllControls()
2009-02-01 23:20:07 +00:00
{
Dictionary<Control, object> visited = new Dictionary<Control, object>();
2010-12-12 12:15:19 +00:00
Dictionary<Control, string> names = new Dictionary<Control, string>();
2009-02-01 23:20:07 +00:00
List<Control> items = new List<Control>();
List<Control> result = new List<Control>();
foreach (Control c in this.Controls)
2010-12-12 12:15:19 +00:00
{
names.Add(c, this.GetType().FullName + "." + c.Name);
2009-02-01 23:20:07 +00:00
items.Add(c);
2010-12-12 12:15:19 +00:00
}
2009-02-01 23:20:07 +00:00
while (items.Count > 0)
{
Control c = items[0];
items.RemoveAt(0);
if (visited.ContainsKey(c))
continue;
else
visited.Add(c, null);
//Filter out display items
if (!(c is Label || c is Button || c is GroupBox || c is Panel))
result.Add(c);
2010-12-12 12:15:19 +00:00
string prefix = names[c];
2009-02-01 23:20:07 +00:00
foreach (Control cx in c.Controls)
2010-12-12 12:15:19 +00:00
{
names.Add(cx, prefix + "." + cx.Name);
2009-02-01 23:20:07 +00:00
items.Add(cx);
2010-12-12 12:15:19 +00:00
}
2009-02-01 23:20:07 +00:00
}
2010-12-12 12:15:19 +00:00
Dictionary<string, Control> results = new Dictionary<string, Control>();
foreach (Control c in result)
results[names[c]] = c;
return results;
2009-02-01 23:20:07 +00:00
}
/// <summary>
/// Loads previously saved settings
/// </summary>
/// <returns>True if there were settings loaded, false otherwise. Use this flag to detect if the control should get default values.</returns>
protected virtual bool LoadDialogSettings()
{
bool anyloaded = false;
2010-12-12 12:15:19 +00:00
foreach (KeyValuePair<string, Control> kv in FindAllControls())
2009-02-01 23:20:07 +00:00
{
2010-12-12 12:15:19 +00:00
Control c = kv.Value;
if (m_settings.ContainsKey(kv.Key))
2009-02-01 23:20:07 +00:00
{
anyloaded = true;
if (c is CheckBox)
2010-12-12 12:15:19 +00:00
((CheckBox)c).Checked = (bool)m_settings[kv.Key];
2009-02-01 23:20:07 +00:00
else if (c is TextBox)
2010-12-12 12:15:19 +00:00
c.Text = (string)m_settings[kv.Key];
2009-02-01 23:20:07 +00:00
else if (c is ComboBox)
{
if (((ComboBox)c).DropDownStyle == ComboBoxStyle.DropDownList)
2010-12-12 12:15:19 +00:00
((ComboBox)c).SelectedIndex = (int)m_settings[kv.Key];
2009-02-01 23:20:07 +00:00
else
2010-12-12 12:15:19 +00:00
c.Text = (string)m_settings[kv.Key];
2009-02-01 23:20:07 +00:00
}
else if (c is RadioButton)
2010-12-12 12:15:19 +00:00
((RadioButton)c).Checked = (bool)m_settings[kv.Key];
2009-02-01 23:20:07 +00:00
else if (c is NumericUpDown)
2010-12-12 12:15:19 +00:00
((NumericUpDown)c).Value = (decimal)m_settings[kv.Key];
2009-02-01 23:20:07 +00:00
else if (c is DateTimePicker)
2010-12-12 12:15:19 +00:00
((DateTimePicker)c).Value = (DateTime)m_settings[kv.Key];
2009-02-01 23:20:07 +00:00
else
{
//Default to "Value" if it exists, otherwise use "Text"
System.Reflection.PropertyInfo pi = c.GetType().GetProperty("Value");
if (pi == null)
2010-12-12 12:15:19 +00:00
c.Text = (string)m_settings[kv.Key];
2009-02-01 23:20:07 +00:00
else
2010-12-12 12:15:19 +00:00
pi.SetValue(c, m_settings[kv.Key], null);
2009-02-01 23:20:07 +00:00
}
}
}
return anyloaded;
}
protected virtual void SaveDialogSettings()
{
2010-12-12 12:15:19 +00:00
foreach (KeyValuePair<string, Control> kv in FindAllControls())
{
Control c = kv.Value;
if (kv.Value is CheckBox)
m_settings[kv.Key] = ((CheckBox)c).Checked;
2009-02-01 23:20:07 +00:00
else if (c is TextBox)
2010-12-12 12:15:19 +00:00
m_settings[kv.Key] = c.Text;
2009-02-01 23:20:07 +00:00
else if (c is ComboBox)
{
if (((ComboBox)c).DropDownStyle == ComboBoxStyle.DropDownList)
2010-12-12 12:15:19 +00:00
m_settings[kv.Key] = ((ComboBox)c).SelectedIndex;
2009-02-01 23:20:07 +00:00
else
2010-12-12 12:15:19 +00:00
m_settings[kv.Key] = c.Text;
2009-02-01 23:20:07 +00:00
}
else if (c is RadioButton)
2010-12-12 12:15:19 +00:00
m_settings[kv.Key] = ((RadioButton)c).Checked;
else if (c is NumericUpDown)
m_settings[kv.Key] = ((NumericUpDown)c).Value;
2009-02-01 23:20:07 +00:00
else if (c is DateTimePicker)
2010-12-12 12:15:19 +00:00
m_settings[kv.Key] = ((DateTimePicker)c).Value;
2009-02-01 23:20:07 +00:00
else
{
//Default to "Value" if it exists, otherwise use "Text"
System.Reflection.PropertyInfo pi = c.GetType().GetProperty("Value");
if (pi == null)
2010-12-12 12:15:19 +00:00
m_settings[kv.Key] = c.Text;
2009-02-01 23:20:07 +00:00
else
2010-12-12 12:15:19 +00:00
m_settings[kv.Key] = pi.GetValue(c, null);
2009-02-01 23:20:07 +00:00
}
2010-12-12 12:15:19 +00:00
}
2009-02-01 23:20:07 +00:00
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// WizardControl
//
this.Name = "WizardControl";
this.Size = new System.Drawing.Size(506, 242);
this.ResumeLayout(false);
}
}
}