/// <summary> /// /// </summary> /// <param name="dictPerFileName"></param> /// <param name="componentWrapperList"></param> internal static void CreateDictionaryPerForm(Dictionary <String, Dictionary <object, ComponentWrapper> > dictPerFileName, Dictionary <object, ComponentWrapper> componentWrapperList) { List <ControlItem> serializeObjectList = new List <ControlItem>(); foreach (var key in componentWrapperList.Keys) { // get the file name that this control need to be save ControlDesignerInfo cdf = ((Control)key).Tag as ControlDesignerInfo; Dictionary <object, ComponentWrapper> dictControlsForForm = null; if (cdf != null && !String.IsNullOrEmpty(cdf.FileName)) { // if we don't have dict for this file name , create Dictionary and add it to the dictPerFileName if (!dictPerFileName.ContainsKey(cdf.FileName)) { dictControlsForForm = new Dictionary <object, ComponentWrapper>(); dictPerFileName.Add(cdf.FileName, dictControlsForForm); } else { dictControlsForForm = dictPerFileName[cdf.FileName]; } // add the control to the relevant Dictionary dictControlsForForm.Add((Control)key, componentWrapperList[(Control)key]); } } }
/// <summary> /// check if the /// </summary> /// <param name="controlItemsList"></param> /// <returns></returns> private bool IsControlItemsListValid(string fileName, List <ControlItem> controlItemsList) { if (controlItemsList != null) { // loop on all components foreach (var keyValue in runtimeHostSurface.ComponentsDictionary) // key - component, value - ComponentWrapper { Control control = (Control)keyValue.Key; ControlDesignerInfo controlDesignerInfo = ((ControlDesignerInfo)control.Tag); if (fileName.Equals(controlDesignerInfo.FileName)) { int isn = controlDesignerInfo.Isn; // try and get the info for this component from the file info ControlItem controlItem = controlItemsList.Find(x => x.Isn == isn); if (controlItem != null && controlItem.Properties != null) { if (!controlDesignerInfo.ControlType.ToString().Equals(controlItem.ControlType)) { return(false); } } } } } return(true); }
/// <summary> /// /// </summary> /// <param name="control"></param> internal void AddControlToControlDictionary(Control control) { // while create ComponentWrapper add it to the controlDictionary (isn is the key and Control is the value) ControlDesignerInfo controlDesignerInfo = control.Tag as ControlDesignerInfo; if (controlDesignerInfo != null) { controlDictionary[controlDesignerInfo.Id] = control; } }
/// <summary> /// set the minimum size - a control shouldn't be smaller than its placement size /// </summary> /// <param name="control"></param> void SetMimimumSize(Control control) { ControlDesignerInfo cdi = control.Tag as ControlDesignerInfo; if (cdi != null) { // make sure the size is not smaller than the placement bounds control.MinimumSize = new Size(cdi.PreviousPlacementBounds.Width, cdi.PreviousPlacementBounds.Height); } }
/// <summary> /// /// </summary> /// <param name="control"></param> /// <returns></returns> bool IsFrame(Control control) { bool isFrame = false; if (control != null) { ControlDesignerInfo controlDesignerInfo = (control.Tag as ControlDesignerInfo); isFrame = (controlDesignerInfo != null && controlDesignerInfo.IsFrame) || controlDesignerInfo.ControlType == MgControlType.CTRL_TYPE_CONTAINER; } return(isFrame); }
/// <summary> /// return Should Serialize Control /// </summary> /// <param name="cdf"></param> /// <returns></returns> static bool ShouldSerializeControl(ControlDesignerInfo cdf) { bool shouldSerializeControl = true; //don't serilize frame control if (cdf == null || cdf.IsFrame) { shouldSerializeControl = false; } return(shouldSerializeControl); }
/// <summary> /// Clones controls /// </summary> /// <param name="ctrl"></param> /// <param name="host"></param> /// <returns></returns> internal Control CloneCtrl(Control sourceControl, IDesignerHost host) { SerializedControlData copiesControl = new SerializedControlData(sourceControl); ControlDesignerInfo newTagData = getControlDesignerInfo(sourceControl); Control newCtrl = CreateComponent(sourceControl, newTagData != null ? newTagData.ControlType : 0); if (sourceControl is Form) //this is needed for tests { newCtrl.Controls.Clear(); } newCtrl.Tag = newTagData; if (SetControlPropertiesIsAllowed(sourceControl)) { SetControlProperties(newCtrl, copiesControl.PropertyList); } // If the parent is scrolled, change the control's location. The scroll state can not be set on the parent, as the // parent is not really created yet. ScrollableControl s = sourceControl.Parent as ScrollableControl; if (s != null) { newCtrl.Location = new Point(newCtrl.Location.X - s.AutoScrollPosition.X, newCtrl.Location.Y - s.AutoScrollPosition.Y); } SetStudioValues(newCtrl); if (newCtrl.Tag != null && ((ControlDesignerInfo)newCtrl.Tag).Properties != null) { runtimeHostSurface.CreateComponentWrapper(newCtrl); } if (newCtrl.Tag != null) { runtimeHostSurface.AddControlToControlDictionary(newCtrl); } if (newCtrl is ICanParent) { ((ICanParent)newCtrl).CanParentEvent += RuntimeHostSurface.CanParent; } newCtrl.LocationChanged += RuntimeHostSurface.LocationChanged; newCtrl.SizeChanged += RuntimeHostSurface.SizeChanged; SetMimimumSize(newCtrl); return(newCtrl); }
ControlDesignerInfo GetControlDesigner(object component) { ControlDesignerInfo c = new ControlDesignerInfo(); c.FileName = @"c:\temp\testfile.xml"; c.Properties = new Dictionary <string, DesignerPropertyInfo>(); if (component is Form) { c.Properties.Add(Constants.ConfigurationFilePropertyName, new DesignerPropertyInfo() { VisibleInPropertyGrid = true, Value = c.FileName, IsNativeProperty = false }); } else if (component is Control) { Control control = ((Control)component); c.Properties.Add(Constants.WinPropLeft, new DesignerPropertyInfo() { VisibleInPropertyGrid = true, Value = control.Left }); c.Properties.Add(Constants.WinPropWidth, new DesignerPropertyInfo() { VisibleInPropertyGrid = true, Value = control.Width }); c.Properties.Add(Constants.WinPropTop, new DesignerPropertyInfo() { VisibleInPropertyGrid = true, Value = control.Top }); c.Properties.Add(Constants.WinPropHeight, new DesignerPropertyInfo() { VisibleInPropertyGrid = true, Value = control.Height }); c.Properties.Add(Constants.WinPropVisible, new DesignerPropertyInfo()); c.Properties.Add(Constants.WinPropName, new DesignerPropertyInfo() { VisibleInPropertyGrid = true, Value = control.Name }); c.Isn = component.GetHashCode(); Control parent = control.Parent; if (parent != null && !(parent is Form)) { c.ParentId = parent.GetHashCode(); } } //TODO : link Isn c.Id = c.Isn; return(c); }
private void DeserializeXMLfile(String fileName) { // get the file info List <ControlItem> controlItemsList = RuntimeDesignerSerializer.DeSerializeFromFile(fileName); if (controlItemsList != null && IsControlItemsListValid(fileName, controlItemsList)) { // loop on all components foreach (var keyValue in runtimeHostSurface.ComponentsDictionary) // key - component, value - ComponentWrapper { Control control = (Control)keyValue.Key; ComponentWrapper cw = keyValue.Value; ControlDesignerInfo controlDesignerInfo = ((ControlDesignerInfo)control.Tag); if (fileName.Equals(controlDesignerInfo.FileName)) { int isn = controlDesignerInfo.Isn; // try and get the info for this component from the file info ControlItem controlItem = controlItemsList.Find(x => x.Isn == isn); if (controlItem != null && controlItem.Properties != null) { if (!controlDesignerInfo.ControlType.ToString().Equals(controlItem.ControlType)) { return; } // set the value for and every each property foreach (var item in controlItem.Properties) { object value = item.GetValue(); if (ComponentWrapper.IsCoordinateProperty(item.Key)) { value = ((int)value) + controlDesignerInfo.GetPlacementForProp(item.Key); } cw.PropertiesDescriptors[item.Key].SetValue(keyValue.Key, value); if (item.Key == Constants.WinPropVisible) { control.Visible = false; } } } } } } }
/// <summary> /// /// </summary> /// <param name="component"></param> /// <param name="value"></param> public override void SetValue(object component, object value) { PropertyDescriptorCollection originalPropDescriptors = TypeDescriptor.GetProvider(component).GetTypeDescriptor(component).GetProperties(); ControlDesignerInfo controlDesignerInfo = ((ControlDesignerInfo)((Control)component).Tag); if (SetDataStrategy != null) { SetDataStrategy.SetData(originalPropertyDescriptor.GetValue(component), ref value); } if (translator != null) { value = translator.AdjustSetValue(value); } originalPropertyDescriptor.SetValue(component, value); }
/// <summary> /// /// </summary> /// <param name="sourceControl"></param> /// <returns></returns> Type GetCreateType(Control sourceControl) { Type newCtrlType = sourceControl.GetType(); if (sourceControl is TableControlUnlimitedItems || sourceControl is TableControlLimitedItems) { newCtrlType = typeof(TableControlUnlimitedItemsRuntimeDesigner); } else if (NeedToCreateImageFromControl(sourceControl)) { newCtrlType = typeof(MgDummyImage); } else if (sourceControl is MgTextBox) { newCtrlType = typeof(MgTextBoxRuntimeDesigner); } else if (sourceControl is MgRichTextBox) { ControlDesignerInfo controlDesignerInfo = getControlDesignerInfo(sourceControl); if (controlDesignerInfo.ControlType == MgControlType.CTRL_TYPE_RICH_TEXT) { newCtrlType = typeof(MgRichTextBoxRuntimeDesigner); } else { newCtrlType = typeof(MgRichEditBoxRuntimeDesigner); } } else if (sourceControl is MgWebBrowser) { newCtrlType = typeof(MgWebBrowserRuntimeDesigner); } else if (sourceControl is MgPanel) { newCtrlType = typeof(MgPanelRuntimeDesigner); } else { newCtrlType = sourceControl.GetType(); } return(newCtrlType); }
/// <summary> /// checks if the value is not below the allowed minimum, considering the scrollbar state /// </summary> /// <param name="val"></param> /// <returns></returns> internal int EnsureValueIsLegal(int val) { ControlDesignerInfo cdi = control.Tag as ControlDesignerInfo; int originalPlacement = cdi == null ? 0 : (xy == Axe.X ? cdi.PreviousPlacementBounds.X : cdi.PreviousPlacementBounds.Y); int scrollShift = xy == Axe.X ? GetDX() : GetDY(); if (control.Parent != null && control.Parent.RightToLeft == RightToLeft.Yes && xy == Axe.X) { //no need to change the location if RTL - controls taken over the right edge will cause a scrollbar, not disappear } else { if (val - scrollShift < originalPlacement) { val = scrollShift + originalPlacement; } } return(val); }
/// <summary> /// init the properties descriptors collection /// </summary> void ProcessOriginalProperties(bool adminMode) { // list of property descriptors for this object's properties collection List <PropertyDescriptor> descriptorsToCreate = new List <PropertyDescriptor>(); if (((Control)component).Tag != null) { // Get default property descriptors PropertyDescriptorCollection originalPropDescriptors = TypeDescriptor.GetProvider(component).GetTypeDescriptor(component).GetProperties(); Dictionary <string, DesignerPropertyInfo> properties = ((ControlDesignerInfo)((Control)component).Tag).Properties; ControlDesignerInfo controlDesignerInfo = ((ControlDesignerInfo)((Control)component).Tag); foreach (var keyProp in properties) { descriptorsToCreate.Add(ComponentWrapperPropertyFactory.CreatePropertyDescriptor(originalPropDescriptors, keyProp, controlDesignerInfo, component, adminMode)); } // create the new PropertyDescriptorCollection propertiesDescriptors = new PropertyDescriptorCollection(descriptorsToCreate.ToArray()); } }
/// <summary> /// /// </summary> /// <param name="key"></param> /// <param name="componentWrapper"></param> /// <returns></returns> static ControlItem BuildControlItemFromComponentWrapper(Control key, ComponentWrapper componentWrapper) { ControlItem serializeObject = null; if (componentWrapper != null) { // get the isn of the control ControlDesignerInfo cdf = ((Control)key).Tag as ControlDesignerInfo; // don't save items of the frame if (ShouldSerializeControl(cdf)) { serializeObject = new ControlItem(); serializeObject.Isn = cdf.Isn; // the control type serializeObject.ControlType = cdf.ControlType.ToString(); serializeObject.Properties = BuildProperties(componentWrapper, cdf); } } return(serializeObject); }
/// <summary> /// set child visibility for choice control /// </summary> /// <param name="parentControl"></param> private void SetChildrenVisibilityForControl(Control parentControl, int currentVisiableLayer) { ControlDesignerInfo controlDesignerInfo = parentControl.Tag as ControlDesignerInfo; if (controlDesignerInfo != null && controlDesignerInfo.LinkedIds != null) { foreach (int controlId in controlDesignerInfo.LinkedIds) { if (ControlDictionary.ContainsKey(controlId)) { Control linkedControl = ControlDictionary[controlId]; if (ComponentsDictionary.ContainsKey(linkedControl)) { int valueLayer = (int)ComponentsDictionary[linkedControl].PropertiesDescriptors[Constants.WinPropLayer].GetValue(linkedControl); if (parentControl.Visible) { PropertyDescriptor prop = ComponentsDictionary[linkedControl].PropertiesDescriptors[Constants.WinPropVisible]; bool visible = prop == null ? true : (bool)prop.GetValue(linkedControl); linkedControl.Visible = (valueLayer == 0 || valueLayer == currentVisiableLayer + 1) && visible; } else { linkedControl.Visible = false; } HandleVisibility(linkedControl); } } else { MessageBox.Show("try to use control that didn't created", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } } } }
/// <summary> /// bild properties to serialize /// </summary> /// <param name="componentWrapper"></param> /// <param name="previousPlacementBounds"></param> /// <returns></returns> static List <PropertyItem> BuildProperties(ComponentWrapper componentWrapper, ControlDesignerInfo controlDesignerInfo) { List <PropertyItem> list = new List <PropertyItem>(); foreach (PropertyDescriptor item in componentWrapper.PropertiesDescriptors) { Object component = componentWrapper.GetPropertyOwner(item); if (item.ShouldSerializeValue(component)) { object valueItem = item.GetValue(component); object offsetValueItem = valueItem; // value to hold offset for runtime tab-order calculations if (valueItem != null) { if (ComponentWrapper.IsCoordinateProperty(item.Name)) { valueItem = ((int)valueItem) - controlDesignerInfo.GetPlacementForProp(item.Name); offsetValueItem = componentWrapper.GetTabOrderOffset(item.Name, (int)valueItem); } TypeConverter converter = TypeDescriptor.GetConverter(valueItem.GetType()); String serString = converter.ConvertToString(valueItem); if (valueItem == offsetValueItem) { list.Add(new PropertyItem(item.Name, serString, valueItem.GetType())); } else { // we have an offset for tab-order calculation list.Add(new TabOrderOffsetPropertyItem(item.Name, serString, valueItem.GetType(), converter.ConvertToString(offsetValueItem))); } } } } return(list); }
/// <summary> /// /// </summary> /// <param name="originalPropDescriptors"></param> /// <param name="keyProp"></param> /// <param name="controlDesignerInfo"></param> /// <param name="component"></param> /// <returns></returns> static internal PropertyDescriptor CreatePropertyDescriptor(PropertyDescriptorCollection originalPropDescriptors, KeyValuePair <string, DesignerPropertyInfo> keyProp, ControlDesignerInfo controlDesignerInfo, object component, bool adminMode) { string name = null; PropertyDescriptor nativeProp = GetNativePropDescriptor(originalPropDescriptors, keyProp.Key, component, ref name); if (nativeProp != null && keyProp.Value.IsNativeProperty) { return(CreateDescriptorForRealProperty(keyProp, controlDesignerInfo, component, nativeProp, name)); } else { return(CreateDescriptorForFakeProperty(keyProp, component, adminMode)); } }
/// <summary> /// returns the SetPropertyData strategy for this control and property /// </summary> /// <param name="keyProp"></param> /// <param name="valueItem"></param> /// <param name="controlDesignerInfo"></param> /// <param name="setPropertyData"></param> /// <param name="component"></param> private static ISetPropertyData GetSetDataStrategy(ref KeyValuePair <string, DesignerPropertyInfo> keyProp, ref object valueItem, ControlDesignerInfo controlDesignerInfo, object component) { ISetPropertyData setPropertyData = null; if (ComponentWrapper.IsCoordinateProperty(keyProp.Key)) { valueItem = ((int)valueItem) + controlDesignerInfo.GetPlacementForProp(keyProp.Key); setPropertyData = new RuntimeControlCoordinateStrategy((Control)component, keyProp.Key); } else if (keyProp.Key.Equals(Constants.WinPropBackColor)) { setPropertyData = new BackgroundColorStrategy((Control)component); } else if (keyProp.Key.Equals(Constants.WinPropForeColor)) { setPropertyData = new ForegroundColorStrategy((Control)component); } else if (keyProp.Key.Equals(Constants.WinPropFont)) { setPropertyData = new FontStrategy((Control)component); } return(setPropertyData); }
/// <summary> /// Create a PropertyDescriptor which wraps a real property of the control /// </summary> /// <param name="keyProp"></param> /// <param name="controlDesignerInfo"></param> /// <param name="component"></param> /// <param name="nativeProp"></param> /// <returns></returns> private static PropertyDescriptor CreateDescriptorForRealProperty(KeyValuePair <string, DesignerPropertyInfo> keyProp, ControlDesignerInfo controlDesignerInfo, object component, PropertyDescriptor nativeProp, string name) { object valueItem = keyProp.Value.Value; ISetPropertyData setPropertyData = GetSetDataStrategy(ref keyProp, ref valueItem, controlDesignerInfo, component); Attribute[] attrs = GetAttribute(keyProp); RTDesignerPropertyDescriptor propertyDescriptor = new RTDesignerPropertyDescriptor(component, nativeProp, valueItem, name ?? nativeProp.DisplayName, attrs) { SetDataStrategy = setPropertyData }; propertyDescriptor.SetTranslator(GetTranslator(component, keyProp.Key)); propertyDescriptor.CanResetStrategy = GetCanRestStrategy(component, keyProp.Key); return(propertyDescriptor); }