openpdn /src/Core/IndirectUI/ControlInfo.cs

Language C# Lines 150
MD5 Hash e4cde36e12e3e35b2830f6f3b6a763c8 Estimated Cost $2,763 (why?)
Repository https://bitbucket.org/tuldok89/openpdn View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET                                                                   //
// Copyright (C) Rick Brewster, Tom Jackson, and past contributors.            //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved.          //
// See src/Resources/Files/License.txt for full licensing and attribution      //
// details.                                                                    //
// .                                                                           //
/////////////////////////////////////////////////////////////////////////////////

using System.Linq;
using PaintDotNet.Base;
using PaintDotNet.Base.PropertySystem;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace PaintDotNet.IndirectUI
{
    public abstract class ControlInfo
        : ICloneable<ControlInfo>
    {
        private PropertyCollection _controlProperties;
        private readonly List<ControlInfo> _childControls = new List<ControlInfo>();

        public PropertyCollection ControlProperties
        {
            get
            {
                return _controlProperties;
            }

            protected set
            {
                _controlProperties = value.Clone();
            }
        }

        public IList<ControlInfo> ChildControls
        {
            get
            {
                return new List<ControlInfo>(_childControls);
            }
        }

        private static PropertyControlInfo FindControlForPropertyName(object propertyName, ControlInfo control)
        {
            var asPCI = control as PropertyControlInfo;

            if (asPCI != null && asPCI.Property.Name == propertyName.ToString())
            {
                return asPCI;
            }
            return control.GetChildControlsCore().Select(childControl => FindControlForPropertyName(propertyName, childControl)).FirstOrDefault(pci => pci != null);
        }

        public PropertyControlInfo FindControlForPropertyName(object propertyName)
        {
            return FindControlForPropertyName(propertyName, this);
        }

        public bool SetPropertyControlValue(object propertyName, object controlPropertyName, object propertyValue)
        {
            PropertyControlInfo pci = FindControlForPropertyName(propertyName);

            if (pci == null)
            {
                return false;
            }

            Property prop = pci.ControlProperties[controlPropertyName];

            if (prop == null)
            {
                return false;
            }

            prop.Value = propertyValue;
            return true;
        }

        public bool SetPropertyControlType(object propertyName, PropertyControlType newControlType)
        {
            PropertyControlInfo pci = FindControlForPropertyName(propertyName);

            if (pci == null)
            {
                return false;
            }

            if (-1 == Array.IndexOf(pci.ControlType.ValueChoices, newControlType))
            {
                return false;
            }

            pci.ControlType.Value = newControlType;
            return true;
        }

        protected List<ControlInfo> GetChildControlsCore()
        {
            return _childControls;
        }

        internal ControlInfo()
        {
            _controlProperties = new PropertyCollection(new Property[0], new PropertyCollectionRule[0]);
        }

        internal ControlInfo(ICloneable<PropertyCollection> controlProperties)
        {
            _controlProperties = controlProperties.Clone();
        }

        internal ControlInfo(ControlInfo cloneMe)
        {
            _controlProperties = cloneMe._controlProperties.Clone();
            _childControls = new List<ControlInfo>(cloneMe._childControls.Count);

            foreach (ControlInfo ciClone in cloneMe._childControls.Select(ci => ci.Clone()))
            {
                _childControls.Add(ciClone);
            }
        }

        public object CreateConcreteControl(object uiContainer)
        {
            return CreateConcreteControl(uiContainer.GetType());
        }

        public object CreateConcreteControl(Type uiContainerType)
        {
            if (typeof(Control).IsAssignableFrom(uiContainerType))
            {
                return CreateWinFormsControl();
            }
            throw new ArgumentException("uiContainerType is not from a supported UI technology");
        }

        internal abstract Control CreateWinFormsControl();

        public abstract ControlInfo Clone();

        object ICloneable.Clone()
        {
            return Clone();
        }
    }
}
Back to Top