using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Diagram.UI.Interfaces;
using DiagramUI.Labels;
namespace DiagramUI
{
/// <summary>
/// Performer of static operations
/// </summary>
public static class StaticPerformer
{
#region Fields
/// <summary>
/// All forms
/// </summary>
private static List<Form> forms = new List<Form>();
#endregion
/// <summary>
/// Performs action with form
/// </summary>
/// <param name="form">Form for action</param>
/// <param name="type">Action type</param>
/// <param name="start">True in start and false in stop</param>
public static void Action(this Form form, object type, bool start)
{
if (form != null)
{
if (!form.IsDisposed)
{
if (form is IStartStop)
{
IStartStop ss = form as IStartStop;
ss.Action(type, start);
}
}
}
}
/// <summary>
/// Shows form
/// </summary>
/// <param name="sf">Object which shows form</param>
public static void Show(this IShowForm sf)
{
sf.ShowForm();
Form form = sf.Form as Form;
if (form == null)
{
return;
}
if (forms.Contains(form))
{
if (form.IsDisposed)
{
forms.Remove(form);
}
return;
}
else
{
forms.Add(form);
if (sf is IEnabled)
{
IEnabled en = sf as IEnabled;
form.FormClosed += (object senrer, FormClosedEventArgs ev) =>
{
try
{
if (forms.Contains(form))
{
forms.Remove(form);
}
if (!en.Enabled)
{
en.Enabled = true;
}
}
catch (Exception ex)
{
ex.Log();
}
};
if (en.Enabled)
{
en.Enabled = false;
}
}
}
}
/// <summary>
/// Post set arrow operation
/// </summary>
/// <param name="control">Control</param>
public static void PostSetArrow(this Control control)
{
if (control is CategoryTheory.IPostSetArrow)
{
CategoryTheory.IPostSetArrow ps = control as CategoryTheory.IPostSetArrow;
ps.PostSetArrow();
}
foreach (Control c in control.Controls)
{
c.PostSetArrow();
}
}
/// <summary>
/// Checks whether button preesd for arrow motion
/// </summary>
/// <param name="args">Mouse arguments</param>
/// <returns>True for arrow motion and false otherwise</returns>
static internal bool IsArrowClick(this MouseEventArgs args)
{
return (args.Button == MouseButtons.Left) & ((Control.ModifierKeys & Keys.Shift) != Keys.Shift);
}
/// <summary>
/// Gets root label of control
/// </summary>
/// <param name="control">The control</param>
/// <returns>The root label</returns>
public static IObjectLabel GetRootLabel(this Control control)
{
IObjectLabel l = null;
GetRootLabel(control, ref l);
return l;
}
/// <summary>
/// Gets form of control
/// </summary>
/// <param name="control">The control</param>
/// <returns>The form</returns>
public static Form GetForm(this Control control)
{
if (control is Form)
{
return control as Form;
}
return control.Parent.GetForm();
}
/// <summary>
/// Gets palette buttons from control
/// </summary>
/// <param name="control">The control</param>
/// <returns>Palette buttons</returns>
public static Dictionary<Type, Dictionary<string, IPaletteButton>> GetPaletteButtons(this Control control)
{
Dictionary<Type, Dictionary<string, IPaletteButton>> dictionary =
new Dictionary<Type, Dictionary<string, IPaletteButton>>();
GetButtons(control, dictionary);
return dictionary;
}
private static void GetButtons(Control control, Dictionary<Type, Dictionary<string, IPaletteButton>> dictionary)
{
if (control is IPaletteButton)
{
IPaletteButton b = control as IPaletteButton;
Dictionary<string, IPaletteButton> d = null;
Type t = b.ReflectionType;
if (dictionary.ContainsKey(t))
{
d = dictionary[t];
}
else
{
d = new Dictionary<string, IPaletteButton>();
dictionary[t] = d;
}
d[b.Kind] = b;
}
foreach (Control c in control.Controls)
{
GetButtons(c, dictionary);
}
if (control is ToolBar)
{
ToolBar tb = control as ToolBar;
foreach (object o in tb.Buttons)
{
if (o is IPaletteButton)
{
IPaletteButton b = o as IPaletteButton;
Dictionary<string, IPaletteButton> d = null;
Type t = b.ReflectionType;
if (dictionary.ContainsKey(t))
{
d = dictionary[t];
}
else
{
d = new Dictionary<string, IPaletteButton>();
dictionary[t] = d;
}
d[b.Kind] = b;
}
}
}
}
private static void GetRootLabel(Control c, ref IObjectLabel l)
{
if (c is IObjectLabel)
{
l = c as IObjectLabel;
}
Control p = c.Parent;
if (p == null)
{
return;
}
GetRootLabel(p, ref l);
}
}
}