private static bool TryGetRectangle(DelphiComponent component, out Rectangle rectangle)
 {
     int left;
     int top = 0;
     int width = 0;
     int height = 0;
     bool success = (component.TryGetPropertyValue("Left", out left) &&
                     component.TryGetPropertyValue("Top", out top) &&
                     component.TryGetPropertyValue("Width", out width) &&
                     component.TryGetPropertyValue("Height", out height));
     rectangle = (success
                      ? new Rectangle(left, top, width, height)
                      : Rectangle.Empty);
     return success;
 }
        private void InternalSimplify(DelphiComponent component)
        {
            string cGuid;
            byte[] data;
            IDictionary<string, DelphiComponent> tabControlOwners = null;

            if (component.TryGetPropertyValue("CGUID", out cGuid))
            {
                Type type = StandardControls.LookupType(cGuid);

                if (type != null && component.TryGetPropertyValue("ControlData", out data))
                {
                    DelphiComponent controlData = ParseControlData(data);
                    component.Type = controlData.Type;

                    foreach (KeyValuePair<string, object> property in controlData.Properties)
                    {
                        if (!component.Properties.ContainsKey(property.Key))
                        {
                            component.Properties.Add(property);
                        }
                    }

                    /*if (type == typeof (AxTabControl))
                    {
                        tabControlOwners = new Dictionary<string, DelphiComponent>(StringComparer.InvariantCultureIgnoreCase);

                        foreach (DelphiComponent subComponent in controlData.Components)
                        {
                            InternalSimplify(subComponent);
                            string controlNames;

                            if (subComponent.TryGetPropertyValue("ControlNames", out controlNames))
                            {
                                foreach (string controlName in controlNames.Split(new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries))
                                {
                                    if (!tabControlOwners.ContainsKey(controlName))
                                    {
                                        tabControlOwners.Add(controlName, subComponent);
                                    }
                                }

                                //subComponent.Properties.Remove("ControlNames");
                            }

                            component.Components.Add(subComponent);
                        }
                    }
                    else if (type == typeof (AxDataGrid))
                    {
                        foreach (DelphiComponent subComponent in controlData.Components)
                        {
                            InternalSimplify(subComponent);
                            component.Components.Add(subComponent);
                        }
                    }
                    else if (type == typeof(AxPanel))
                    {
                        foreach (DelphiComponent subComponent in controlData.Components)
                        {
                            InternalSimplify(subComponent);
                            component.Components.Add(subComponent);
                        }
                    }
                    else
                    {
                        Debug.Assert(controlData.Components.Count == 0);
                    }
                     */

                    //component.Properties.Remove("ControlData");

                    foreach (DelphiComponent subComponent in controlData.Components)
                    {
                        InternalSimplify(subComponent);
                        component.Components.Add(subComponent);
                    }
                }
            }

            _componentSimplifier.Simplify(component);

            if (component.TryGetPropertyValue("ChildControls", out data))
            {
                ICollection<DelphiComponent> childControls = ParseChildControls(data);

                foreach (DelphiComponent childControl in childControls)
                {
                    InternalSimplify(childControl);

                    if (tabControlOwners == null)
                    {
                        component.Components.Add(childControl);
                    }
                    else
                    {
                        DelphiComponent ownerTab;

                        if (tabControlOwners.TryGetValue(childControl.Name, out ownerTab))
                        {
                            ownerTab.Components.Add(childControl);
                        }
                    }
                }

                component.Properties.Remove("ChildControls");
            }
        }
        public static ControlBuilder CreateBuilder(string name, DelphiComponent component)
        {
            //invisible controls are mapped to the HiddenControl control
            bool visible = (!component.TryGetPropertyValue("Visible", out visible) || visible);
            if (!visible)
            {
                ControlBuilder builder = new HiddenControlBuilder(component);
                builder._component = component;
                return builder;

            }

            Type builderType = null;

            foreach (KeyValuePair<BuilderMappingAttribute, Type> builder in _builders)
            {
                if (builderType != builder.Value && builder.Key.IsApplicable(name, component.Properties))
                {
                    bool isSpecific = (builder.Key.GetType() != typeof (BuilderMappingAttribute));

                    if (builderType == null || isSpecific)
                    {
                        builderType = builder.Value;

                        if (isSpecific)
                        {
                            break;
                        }
                    }
                }
            }

            ControlBuilder colBuilder;

            if (builderType != null)
            {
                colBuilder = (ControlBuilder) Activator.CreateInstance(builderType);
                colBuilder._component = component;
                return colBuilder;
            }
            else
            {
                colBuilder = null;
            }

            return colBuilder;
        }