|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Drawing; |
| 5 | +using System.Linq; |
| 6 | +using System.Reflection; |
| 7 | +using System.Text; |
| 8 | + |
| 9 | +namespace System.Windows.Forms.Design |
| 10 | +{ |
| 11 | + public class ControlDesigner : IControlDesigner |
| 12 | + { |
| 13 | + private objectEditor editor; |
| 14 | + private bool toggleEditor = true; |
| 15 | + |
| 16 | + public Control Control { get; set; } |
| 17 | + |
| 18 | + public ControlDesigner(Control c) |
| 19 | + { |
| 20 | + Control = c; |
| 21 | + |
| 22 | + editor = new objectEditor(c, Control.GetType().Name); |
| 23 | + editor.toggleEditor = true; |
| 24 | + } |
| 25 | + |
| 26 | + public virtual object Draw(int width, int height) |
| 27 | + { |
| 28 | + if (Control == null) return null; |
| 29 | + |
| 30 | + Control controlToSet = null; |
| 31 | + |
| 32 | + Editor.BeginGroup(width - 24, ""); |
| 33 | + |
| 34 | + controlToSet = editor.Draw(); |
| 35 | + |
| 36 | + Editor.EndGroup(); |
| 37 | + |
| 38 | + return controlToSet; |
| 39 | + } |
| 40 | + |
| 41 | + private class controlProperty |
| 42 | + { |
| 43 | + public objectEditor editor; |
| 44 | + public bool expanded; |
| 45 | + public PropertyInfo info; |
| 46 | + } |
| 47 | + private class objectEditor |
| 48 | + { |
| 49 | + private readonly List<FieldInfo> fields; |
| 50 | + private readonly List<MethodInfo> methods; |
| 51 | + private readonly object obj; |
| 52 | + private readonly List<controlProperty> props; |
| 53 | + private readonly string name; |
| 54 | + |
| 55 | + public bool toggleEditor; |
| 56 | + |
| 57 | + public objectEditor(object o, string objName) |
| 58 | + { |
| 59 | + obj = o; |
| 60 | + name = objName; |
| 61 | + |
| 62 | + var objType = obj.GetType(); |
| 63 | + var pList = objType.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList(); |
| 64 | + pList.Sort((x, y) => string.Compare(x.Name, y.Name, StringComparison.Ordinal)); |
| 65 | + |
| 66 | + props = new List<controlProperty>(); |
| 67 | + for (int i = 0; i < pList.Count; i++) |
| 68 | + { |
| 69 | + var p = pList[i]; |
| 70 | + var cp = new controlProperty() |
| 71 | + { |
| 72 | + info = p, |
| 73 | + }; |
| 74 | + |
| 75 | + props.Add(cp); |
| 76 | + } |
| 77 | + |
| 78 | + fields = objType.GetFields(BindingFlags.Public | BindingFlags.Instance).ToList(); |
| 79 | + fields.Sort((x, y) => x.Name.CompareTo(y.Name)); |
| 80 | + |
| 81 | + methods = objType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public).ToList(); |
| 82 | + methods.Sort((x, y) => String.Compare(x.Name, y.Name, StringComparison.Ordinal)); |
| 83 | + for (int i = 0; i < methods.Count; i++) |
| 84 | + { |
| 85 | + var m = methods[i]; |
| 86 | + if (m.GetParameters().Length == 0 && m.ReturnType == typeof(void)) continue; |
| 87 | + |
| 88 | + methods.RemoveAt(i); |
| 89 | + i--; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public Control Draw() |
| 94 | + { |
| 95 | + Control control = null; |
| 96 | + if (props.Count == 0 && methods.Count == 0) |
| 97 | + { |
| 98 | + Editor.Label(name); |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + Editor.BeginVertical("Box"); |
| 103 | + toggleEditor = Editor.Foldout(name, toggleEditor); |
| 104 | + if (toggleEditor) |
| 105 | + { |
| 106 | + // Fields. |
| 107 | + if (fields.Count > 0) |
| 108 | + { |
| 109 | + Editor.BeginVertical("Box"); |
| 110 | + for (int i = 0; i < fields.Count; i++) |
| 111 | + { |
| 112 | + var tc = Draw(fields[i]); |
| 113 | + if (tc != null) |
| 114 | + control = tc; |
| 115 | + } |
| 116 | + Editor.EndVertical(); |
| 117 | + |
| 118 | + Editor.NewLine(1); |
| 119 | + } |
| 120 | + |
| 121 | + // Properties. |
| 122 | + for (int i = 0; i < props.Count; i++) |
| 123 | + { |
| 124 | + var tc = Draw(props[i]); |
| 125 | + if (tc != null) |
| 126 | + control = tc; |
| 127 | + } |
| 128 | + |
| 129 | + // Methods. |
| 130 | + if (methods.Count > 0) |
| 131 | + { |
| 132 | + Editor.NewLine(1); |
| 133 | + |
| 134 | + for (int i = 0; i < methods.Count; i++) |
| 135 | + { |
| 136 | + var tc = Draw(methods[i]); |
| 137 | + if (tc != null) |
| 138 | + control = tc; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + Editor.EndVertical(); |
| 143 | + return control; |
| 144 | + } |
| 145 | + public Control Draw(controlProperty p) |
| 146 | + { |
| 147 | + Control controlToSet = null; |
| 148 | + var val = p.info.GetValue(obj, null); |
| 149 | + Type type = null; |
| 150 | + if (val != null) |
| 151 | + type = val.GetType(); |
| 152 | + |
| 153 | + // Array & List. |
| 154 | + if (val is string == false) |
| 155 | + if ((type != null && type.IsArray) || val is IEnumerable) |
| 156 | + { |
| 157 | + Editor.BeginVertical("Box"); |
| 158 | + p.expanded = Editor.Foldout(p.info.Name, p.expanded); |
| 159 | + if (p.expanded) |
| 160 | + { |
| 161 | + var vEnum = val as IEnumerable; |
| 162 | + var arrayIndex = 0; |
| 163 | + foreach (var e in vEnum) |
| 164 | + { |
| 165 | + var ec = e as Control; |
| 166 | + if (ec != null) |
| 167 | + { |
| 168 | + if (Editor.Button(ec.ToString())) |
| 169 | + controlToSet = ec; |
| 170 | + } |
| 171 | + else |
| 172 | + { |
| 173 | + if (p.editor == null) |
| 174 | + p.editor = new objectEditor(e, arrayIndex.ToString()); |
| 175 | + |
| 176 | + p.editor.Draw(); |
| 177 | + } |
| 178 | + arrayIndex++; |
| 179 | + } |
| 180 | + } |
| 181 | + Editor.EndVertical(); |
| 182 | + return controlToSet; |
| 183 | + } |
| 184 | + |
| 185 | + // If there is no Set() method then skip. |
| 186 | + var pSetMethod = p.info.GetSetMethod(true); |
| 187 | + if (pSetMethod == null || pSetMethod.IsPrivate) |
| 188 | + { |
| 189 | + Editor.Label(p.info.Name, val); |
| 190 | + return null; |
| 191 | + } |
| 192 | + |
| 193 | + // Other editors. |
| 194 | + if (val is bool) |
| 195 | + { |
| 196 | + var bVal = (bool)val; |
| 197 | + var ebVal = Editor.BooleanField(p.info.Name, bVal); |
| 198 | + if (ebVal.Changed) |
| 199 | + p.info.SetValue(obj, ebVal.Value, null); |
| 200 | + } |
| 201 | + else if (val is Control) |
| 202 | + { |
| 203 | + var cVal = val as Control; |
| 204 | + if (Editor.Button(p.info.Name, cVal.GetType().Name)) |
| 205 | + controlToSet = cVal; |
| 206 | + } |
| 207 | + else if (val is Color) |
| 208 | + { |
| 209 | + var colorVal = (Color)val; |
| 210 | + Editor.ColorField(p.info.Name, colorVal, c => p.info.SetValue(obj, c, null)); |
| 211 | + } |
| 212 | + else if (val is string) |
| 213 | + { |
| 214 | + var stringtVal = (string)val; |
| 215 | + var esVal = Editor.TextField(p.info.Name, stringtVal); |
| 216 | + if (esVal.Changed) |
| 217 | + p.info.SetValue(obj, esVal.Value, null); |
| 218 | + } |
| 219 | + else if (val is int) |
| 220 | + { |
| 221 | + var eiVal = Editor.IntField(p.info.Name, (int) val); |
| 222 | + if (eiVal.Changed) |
| 223 | + p.info.SetValue(obj, eiVal.Value[0], null); |
| 224 | + } |
| 225 | + else if (val is byte || val is sbyte || val is short || val is ushort || val is uint || val is long || val is ulong || val is float || val is double) |
| 226 | + { |
| 227 | + // TODO: editors for common types (like for int ^up there). |
| 228 | + Editor.Label(p.info.Name, val); |
| 229 | + } |
| 230 | + else if (val is Enum) |
| 231 | + { |
| 232 | + var eeVal = Editor.EnumField(p.info.Name, (Enum)val); |
| 233 | + if (eeVal.Changed) |
| 234 | + p.info.SetValue(obj, eeVal.Value, null); |
| 235 | + } |
| 236 | + else if (val != null) |
| 237 | + { |
| 238 | + if (p.editor == null) |
| 239 | + p.editor = new objectEditor(val, p.info.Name); |
| 240 | + |
| 241 | + p.editor.Draw(); |
| 242 | + } |
| 243 | + |
| 244 | + return controlToSet; |
| 245 | + } |
| 246 | + public Control Draw(FieldInfo f) |
| 247 | + { |
| 248 | + // TODO: editors for fields. |
| 249 | + |
| 250 | + var val = f.GetValue(obj); |
| 251 | + Editor.Label(f.Name, val); |
| 252 | + |
| 253 | + return null; |
| 254 | + } |
| 255 | + public Control Draw(MethodInfo m) |
| 256 | + { |
| 257 | + if (Editor.Button(m.Name)) |
| 258 | + { |
| 259 | + m.Invoke(obj, null); |
| 260 | + } |
| 261 | + |
| 262 | + return null; |
| 263 | + } |
| 264 | + } |
| 265 | + } |
| 266 | +} |
0 commit comments