aboutsummaryrefslogtreecommitdiff
path: root/source/WindowsFormsApplication1/Controls/WinFormsHost.cs
blob: e2be505d8907692ac32d89e91f5b3209a32a5214 (plain) (blame)
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
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;

        /// <summary>
        /// Creates a new Windows Forms host that will host the given control.
        /// </summary>
        /// <param name="ctrl">The control to use.</param>
        public WinFormsHost(System.Windows.Forms.Control ctrl)
        {
            guest = ctrl;
            ScanProperties();
        }

        /// <summary>
        /// Scan the guest for any properties that this widget contains, and set our properties to the ShiftUI equivalent.
        /// </summary>
        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<string, PropertyInfo>();
            var host_names = new Dictionary<string, PropertyInfo>();

            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);
                    }
                }
            }
        }
    }
}