From d40fed5ce2bc806a91245adb18039634eac13ed0 Mon Sep 17 00:00:00 2001 From: MichaelTheShifter Date: Wed, 20 Jul 2016 09:40:36 -0400 Subject: Move ShiftUI source code to ShiftOS This'll be a lot easier to work on. --- .../Controls/WinFormsHost.cs | 86 ++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 source/WindowsFormsApplication1/Controls/WinFormsHost.cs (limited to 'source/WindowsFormsApplication1/Controls') diff --git a/source/WindowsFormsApplication1/Controls/WinFormsHost.cs b/source/WindowsFormsApplication1/Controls/WinFormsHost.cs new file mode 100644 index 0000000..e2be505 --- /dev/null +++ b/source/WindowsFormsApplication1/Controls/WinFormsHost.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ShiftUI; +using System.Reflection; +using System.Drawing; + +namespace ShiftOS.Controls +{ + class WinFormsHost : Widget + { + private System.Windows.Forms.Control guest = null; + + /// + /// Creates a new Windows Forms host that will host the given control. + /// + /// The control to use. + public WinFormsHost(System.Windows.Forms.Control ctrl) + { + guest = ctrl; + ScanProperties(); + } + + /// + /// Scan the guest for any properties that this widget contains, and set our properties to the ShiftUI equivalent. + /// + private void ScanProperties() + { + var guest_properties = guest.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); + var host_properties = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); + + var valid_types = new Type[] { typeof(string), typeof(int), typeof(Size), typeof(Point), typeof(Font), typeof(Color), typeof(bool) }; + + var guest_names = new Dictionary(); + var host_names = new Dictionary(); + + foreach(var property in guest_properties) + { + if (!property.CanWrite || !property.CanRead) + continue; //We want to be able to read and write from and to the property... + + if (!valid_types.Contains(property.PropertyType) || !property.PropertyType.IsEnum) + continue; //Need to be valid propertytype... + + guest_names.Add(property.Name, property); //Add to the guest dictionary for easy access. + } + + //Now let's do the same for our host. + foreach (var property in host_properties) + { + if (!property.CanWrite || !property.CanRead) + continue; //We want to be able to read and write from and to the property... + + if (!valid_types.Contains(property.PropertyType) || !property.PropertyType.IsEnum) + continue; //Need to be valid propertytype... + + host_names.Add(property.Name, property); //Add to the guest dictionary for easy access. + } + + //Cool. Properties scanned. Let's set our properties to the guest's! + foreach(var property in host_names) + { + if(guest_names.ContainsKey(property.Key)) + { + var p = guest_names[property.Key]; + if(p.PropertyType.IsEnum) + { + //If it's an enum, convert it to an integer value + //so that Reflection won't yell at us. + + var value = p.GetValue(guest); //Get the guest's value of this property + int value_int = (int)value; + property.Value.SetValue(this, value_int); + } + else + { + var value = p.GetValue(guest); + property.Value.SetValue(this, value); + } + } + } + } + } +} -- cgit v1.2.3