aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.WinForms/GUIFunctions.cs
blob: ac058a67afbb589dfb4d844647929dc5e50f2399 (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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiftOS.Engine.Scripting;
using System.Drawing;
using ShiftOS.Engine;
using System.Windows.Forms;
using ShiftOS.WinForms.Tools;

namespace ShiftOS.WinForms
{
    [Exposed("gui")]
    public class GUIFunctions
    {
        public dynamic color(int r, int g, int b)
        {
            return Color.FromArgb(r, g, b);
        }

        public dynamic color(string name)
        {
            return Color.FromName(name);
        }

        public dynamic point(int x, int y)
        {
            return new Point(x, y);
        }

        public dynamic size(int w, int h)
        {
            return new Size(w, h);
        }

        public dynamic font(string name, float size, bool bold, bool italic, bool strikethrough, bool underline)
        {
            FontStyle fs = FontStyle.Regular;
            if (bold)
                fs = fs | FontStyle.Bold;
            if (italic)
                fs = fs | FontStyle.Italic;
            if (underline)
                fs = fs | FontStyle.Underline;
            if (strikethrough)
                fs = fs | FontStyle.Strikeout;

            return new Font(name, size, fs);
        }

        public dynamic createWindow(string name, dynamic size)
        {
            var win = new Window();
            win.Size = size;
            AppearanceManager.SetupWindow(win);
            return win;
        }

        public dynamic panel(Control parent, Point loc, Size size)
        {
            var pnl = new Panel();
            pnl.Location = loc;
            pnl.Size = size;
            parent.Controls.Add(pnl);
            pnl.Show();
            return pnl;
        }

        public DockStyle dock(string type)
        {
            DockStyle d = DockStyle.None;
            switch (type.ToLower())
            {
                case "top":
                    d = DockStyle.Top;
                    break;
                case "bottom":
                    d = DockStyle.Bottom;
                    break;
                case "left":
                    d = DockStyle.Left;
                    break;
                case "right":
                    d = DockStyle.Right;
                    break;
                case "fill":
                    d = DockStyle.Fill;
                    break;
            }
            return d;
        }

        public AnchorStyles anchor(bool top, bool left, bool bottom, bool right)
        {
            AnchorStyles a = AnchorStyles.None;
            if (top)
                a = a | AnchorStyles.Top;
            if (left)
                a = a | AnchorStyles.Left;
            if (bottom)
                a = a | AnchorStyles.Bottom;
            if (right)
                a = a | AnchorStyles.Right;
            return a;
        }

        public dynamic flow(Control parent, Point loc, Size size)
        {
            var pnl = new FlowLayoutPanel();
            pnl.Size = size;
            pnl.Location = loc;
            parent.Controls.Add(pnl);
            pnl.Show();
            return pnl;
        }

        private Control addToParent(Control parent, Point loc, Size size, Control newControl)
        {
            newControl.Size = size;
            newControl.Location = loc;
            parent.Controls.Add(newControl);
            newControl.Show();
            ControlManager.SetupControls(parent);
            return newControl;
        }

        public dynamic button(Control parent, Point loc, Size size, string text)
        {
            var btn = new Button();
            btn.Text = text;
            btn.AutoSize = false;
            return addToParent(parent, loc, size, btn);
        }

        public dynamic label(Control parent, Point loc, Size size, string text)
        {
            var lbl = new Label();
            lbl.Text = text;
            lbl.AutoSize = false;
            return addToParent(parent, loc, size, lbl);
        }

        public dynamic menu(Control parent)
        {
            var mnu = new MenuStrip();
            parent.Controls.Add(mnu);
            mnu.Dock = DockStyle.Top;
            mnu.Show();
            return mnu;
        }

        public dynamic menuitem(MenuStrip parent, string text, Action onClick = null)
        {
            var mitem = new ToolStripMenuItem();
            parent.Items.Add(mitem);
            mitem.Text = text;
            mitem.Click += (o, a) =>
            {
                onClick?.Invoke();
            };
            return mitem;
        }

        public dynamic menuitem(ToolStripMenuItem parent, string text, Action onClick = null)
        {
            var mitem = new ToolStripMenuItem();
            parent.DropDownItems.Add(mitem);
            mitem.Text = text;
            mitem.Click += (o, a) =>
            {
                onClick?.Invoke();
            };
            return mitem;
        }

        public dynamic textbox(Control parent, Point loc, int width)
        {
            var txt = new TextBox();
            txt.Location = loc;
            txt.Width = width;
            parent.Controls.Add(txt);
            txt.Show();
            ControlManager.SetupControls(parent);
            return txt;
        }

        public dynamic timer(int interval, Action elapsed)
        {
            var tmr = new System.Windows.Forms.Timer();
            tmr.Interval = interval;
            tmr.Tick += (o, a) =>
            {
                elapsed?.Invoke();
            };
            return tmr;
        }
    }

    [Exposed("clr")]
    public class CommonLanguageRuntimeInterop
    {
        public void tryCode(Action code, Action<Exception> error)
        {
            try
            {
                code?.Invoke();
            }
            catch (Exception ex)
            {
                error?.Invoke(ex);
            }
        }

        public void throwError(string error)
        {
            throw new Exception(error);
        }


        public dynamic construct(Type type, dynamic[] ctorParams)
        {
            return Activator.CreateInstance(type, ctorParams);
        }

        public dynamic typeOf(string typeName)
        {
            return Type.GetType(typeName);
        }

        public void throwException(string message)
        {
            throw new UserException(message);
        }
    }

    public class UserException : Exception
    {
        public UserException(string message) :base("User threw exception using clr:throwException().\r\n\r\n" + message)
        {

        }
    }
}