198 lines
6.9 KiB
C#

using DevComponents.DotNetBar.Rendering;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Text;
namespace DevComponents.DotNetBar.Design
{
/// <summary>
/// Designer for ProgressSteps control.
/// </summary>
public class ProgressStepsDesigner : BarBaseControlDesigner
{
public ProgressStepsDesigner()
{
this.EnableItemDragDrop=true;
}
public override void Initialize(IComponent component)
{
base.Initialize(component);
if (component == null || component.Site == null || !component.Site.DesignMode)
return;
#if !TRIAL
IDesignerHost dh = this.GetService(typeof(IDesignerHost)) as IDesignerHost;
if (dh != null)
dh.LoadComplete += new EventHandler(dh_LoadComplete);
#endif
}
#if FRAMEWORK20
public override void InitializeNewComponent(IDictionary defaultValues)
{
base.InitializeNewComponent(defaultValues);
SetDesignTimeDefaults();
}
#else
public override void OnSetComponentDefaults()
{
SetDesignTimeDefaults();
base.OnSetComponentDefaults();
}
#endif
protected virtual void SetDesignTimeDefaults()
{
ProgressSteps ps = this.Control as ProgressSteps;
ps.BackgroundStyle.Class = ElementStyleClassKeys.ProgressStepsKey;
BaseItem cont = this.GetDefaultNewItemContainer();
StepItem item = CreateStepItem(cont);
item.Text = "Step 1";
item = CreateStepItem(cont);
item.Text = "Step 2";
item = CreateStepItem(cont);
item.Text = "Step 3";
#if !TRIAL
string key = GetLicenseKey();
ps.LicenseKey = key;
#endif
}
public override DesignerVerbCollection Verbs
{
get
{
DesignerVerb[] verbs = null;
verbs = new DesignerVerb[]
{
new DesignerVerb("Add Item", new EventHandler(CreateItem))
};
return new DesignerVerbCollection(verbs);
}
}
//protected override bool GetHitTest(Point pt)
//{
// ItemPanel panel = this.Control as ItemPanel;
// if (panel != null && panel.IsHandleCreated && panel.AutoScroll)
// {
// Point p = panel.PointToClient(pt);
// if (panel.VScrollBar != null && panel.VScrollBar.Bounds.Contains(p))
// return true;
// if (panel.HScrollBar != null && panel.HScrollBar.Bounds.Contains(p))
// return true;
// }
// return base.GetHitTest(pt);
//}
private void CreateItem(object sender, EventArgs e)
{
OnSubItemsChanging();
CreateStepItem(this.GetDefaultNewItemContainer());
OnSubItemsChanged();
}
protected virtual StepItem CreateStepItem(BaseItem parent)
{
if (parent == null)
return null;
StepItem item = null;
IDesignerHost dh = (IDesignerHost)GetService(typeof(IDesignerHost));
if (dh != null)
{
DesignerTransaction trans = dh.CreateTransaction("Adding new item");
try
{
m_CreatingItem = true;
item = dh.CreateComponent(typeof(StepItem)) as StepItem;
if (item == null)
return null;
TypeDescriptor.GetProperties(item)["Text"].SetValue(item, item.Name);
IComponentChangeService cc = this.GetService(typeof(IComponentChangeService)) as IComponentChangeService;
if (parent != this.GetDefaultNewItemContainer() && cc != null)
cc.OnComponentChanging(parent, TypeDescriptor.GetProperties(typeof(BaseItem))["SubItems"]);
parent.SubItems.Add(item);
this.RecalcLayout();
if (parent != this.GetDefaultNewItemContainer() && cc != null)
cc.OnComponentChanged(parent, TypeDescriptor.GetProperties(typeof(BaseItem))["SubItems"], null, null);
OnitemCreated(item);
}
catch
{
trans.Cancel();
}
finally
{
if (!trans.Canceled)
trans.Commit();
m_CreatingItem = false;
}
}
return item;
}
private void RemoveDescriptors(System.Collections.IDictionary properties, String[] propNames)
{
foreach (String propName in propNames)
{
if (properties.Contains(propName))
{
if (properties[propName] is PropertyDescriptor)
properties[propName] = TypeDescriptor.CreateProperty(this.Component.GetType(), (PropertyDescriptor)properties[propName], new BrowsableAttribute(false));
else if (properties[propName] is EventDescriptor)
properties[propName] = TypeDescriptor.CreateEvent(this.Component.GetType(), (EventDescriptor)properties[propName], new BrowsableAttribute(false));
else
properties.Remove(propName);
}
}
}
protected override void PreFilterProperties(System.Collections.IDictionary properties)
{
base.PreFilterProperties(properties);
RemoveDescriptors(properties,
new String[] { "ForeColor" });
}
#region Licensing Stuff
#if !TRIAL
protected string GetLicenseKey()
{
string key = "";
Microsoft.Win32.RegistryKey regkey = Microsoft.Win32.Registry.LocalMachine;
regkey = regkey.OpenSubKey("Software\\DevComponents\\Licenses", false);
if (regkey != null)
{
object keyValue = regkey.GetValue("DevComponents.DotNetBar.DotNetBarManager2");
if (keyValue != null)
key = keyValue.ToString();
}
return key;
}
private void dh_LoadComplete(object sender, EventArgs e)
{
IDesignerHost dh = this.GetService(typeof(IDesignerHost)) as IDesignerHost;
if (dh != null)
dh.LoadComplete -= new EventHandler(dh_LoadComplete);
string key = GetLicenseKey();
ProgressSteps ps = this.Control as ProgressSteps;
if (key != "" && ps != null && ps.LicenseKey == "" && ps.LicenseKey != key)
TypeDescriptor.GetProperties(ps)["LicenseKey"].SetValue(ps, key);
}
#endif
#endregion
}
}