diff options
| author | Michael VanOverbeek <[email protected]> | 2016-07-25 12:57:52 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2016-07-25 12:57:52 -0400 |
| commit | 46c1c31302f111a1f3ec23a70e6f3986a9aa2a27 (patch) | |
| tree | f00af7ea3f6ad2641fb26fa1d310fd8b7179b39c /source/ShiftUI Designer/Program.cs | |
| parent | af48e774189596b8d7a058c564a7d6d75205ca03 (diff) | |
| parent | 6fa16209519896de09949a27425dff00ebf2970a (diff) | |
| download | shiftos-c--46c1c31302f111a1f3ec23a70e6f3986a9aa2a27.tar.gz shiftos-c--46c1c31302f111a1f3ec23a70e6f3986a9aa2a27.tar.bz2 shiftos-c--46c1c31302f111a1f3ec23a70e6f3986a9aa2a27.zip | |
Merge pull request #17 from MichaelTheShifter/shiftui_integration
Shiftui integration
Diffstat (limited to 'source/ShiftUI Designer/Program.cs')
| -rw-r--r-- | source/ShiftUI Designer/Program.cs | 1445 |
1 files changed, 1445 insertions, 0 deletions
diff --git a/source/ShiftUI Designer/Program.cs b/source/ShiftUI Designer/Program.cs new file mode 100644 index 0000000..4ca9e98 --- /dev/null +++ b/source/ShiftUI Designer/Program.cs @@ -0,0 +1,1445 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ShiftUI; +using System.Drawing; +using System.Diagnostics; +using System.Reflection; + +namespace ShiftUI_Designer +{ + public class MyAwesomeSkin : ShiftUI.ShiftOS.Skin + { + public MyAwesomeSkin() + { + base.ButtonBackColor_Pressed = Color.Red; + } + } + + class Program + { + static void Main(string[] args) + { + Application.LoadSkin(new MyAwesomeSkin()); + Application.EnableVisualStyles(); + Application.Run(new FormSelectionForm()); + Console.WriteLine("This executed after Run."); + } + } + + public static class Helpers + { + public static void Debug(this Form host, string message) + { + Console.WriteLine("<{0}> [ShiftUI/{1}] {2}", DateTime.Now.ToString(), host.Text, message); + } + } + + public class MainEditorForm : Form + { + private Panel resize_se = null; + private Panel resize_e = null; + private Panel resize_s = null; + private Panel resize_sw = null; + private Panel resize_w = null; + + public void LoadFormFromClass(Form form) + { + var t = form.GetType(); + var init_method = t.GetMethod("InitializeComponent"); + if (init_method != null) + { + init_method.Invoke(form, null); + } + TopLevels.Add(form); + current_form = form; + PopulateTree(); + DrawForm(); + this.Show(); + } + + public enum ResizeLocation + { + South = 0, + SouthEast = 1, + East = 2, + NorthEast = 3, + North = 4, + NorthWest = 5, + West = 6, + SouthWest = 7 + } + + public void Select(Widget widget) + { + if (widget != null) + { + if(widget is Form && current_form != widget as Form) + { + current_form = widget as Form; + DrawForm(); + Select(current_form); + } + if (widget != current_form.Body) + { + properties.SelectedObject = widget; + resizing_widget = widget; + resize_w?.Parent?.Widgets.Remove(resize_w); + resize_w = null; + resize_sw?.Parent?.Widgets.Remove(resize_sw); + resize_sw = null; + resize_se?.Parent?.Widgets.Remove(resize_se); + resize_se = null; + resize_s?.Parent?.Widgets.Remove(resize_s); + resize_s = null; + resize_e?.Parent?.Widgets.Remove(resize_e); + resize_e = null; + SetupResizeBorder(widget); + } + else + { + Select(current_form); + } + } + else + { + resize_w?.Hide(); + resize_sw?.Hide(); + resize_s?.Hide(); + resize_se?.Hide(); + resize_e?.Hide(); + + } + } + + public void SetupResizeBorder(Widget widget) + { + if (!(widget is Form)) + { + SetupMoveHandle(ref resize_sw, ResizeLocation.NorthWest); + } + SetupResizeHandle(ref resize_se, ResizeLocation.SouthEast); + SetupResizeHandle(ref resize_e, ResizeLocation.East); + SetupResizeHandle(ref resize_s, ResizeLocation.South); + + } + + + Point last_mouse_location = Point.Empty; + bool dragging = false; + private Widget resizing_widget = null; + + public void SetupMoveHandle(ref Panel handle, ResizeLocation loc) + { + bool updown = false; + bool leftright = false; + properties.SelectedObject = resizing_widget; + if (handle == null) + { + handle = new Panel(); + body.Widgets.Add(handle); + handle.BackColor = Color.Black; + handle.Size = new Size(16, 16); + handle.MouseDown += (o, a) => + { + if (a.Button == MouseButtons.Left) + { + last_mouse_location = a.Location; + dragging = true; + } + }; + handle.MouseUp += (o, a) => + { + dragging = false; + }; + handle.MouseMove += (o, a) => + { + if (dragging == true) + { + var mouse_loc = a.Location; + int y_distance = mouse_loc.Y - last_mouse_location.Y; + int x_distance = mouse_loc.X - last_mouse_location.X; + if (updown == true) + { + resizing_widget.Top += y_distance; + form_preview.Refresh(); + } + if (leftright == true) + { + resizing_widget.Left += x_distance; + form_preview.Refresh(); + } + SetupResizeBorder(resizing_widget); + } + }; + } + bool ex = false; + var wloc = Point.Empty; + try + { + wloc = resizing_widget.Parent.PointToScreen(resizing_widget.Location); + } + catch + { + ex = true; + wloc = body.PointToScreen(resizing_widget.Location); //Must be a top-level widget. + } + wloc = body.PointToClient(wloc); + switch (loc) + { + case ResizeLocation.NorthWest: + handle.Left = wloc.X - handle.Width; + handle.Top = wloc.Y - handle.Height; + updown = true; + leftright = true; + break; + } + + handle.BringToFront(); + handle.Show(); + handle.Refresh(); + body.Refresh(); + } + + + public void SetupResizeHandle(ref Panel handle, ResizeLocation loc) + { + bool updown = false; + bool leftright = false; + properties.SelectedObject = resizing_widget; + if (handle == null) + { + handle = new Panel(); + body.Widgets.Add(handle); + handle.BackColor = Color.Black; + handle.Size = new Size(16, 16); + handle.MouseDown += (o, a) => + { + if (a.Button == MouseButtons.Left) + { + last_mouse_location = a.Location; + dragging = true; + } + }; + handle.MouseUp += (o, a) => + { + dragging = false; + }; + handle.MouseMove += (o, a) => + { + if (dragging == true) + { + var mouse_loc = a.Location; + int y_distance = mouse_loc.Y - last_mouse_location.Y; + int x_distance = mouse_loc.X - last_mouse_location.X; + if (updown == true) + { + resizing_widget.Height += y_distance; + form_preview.Refresh(); + } + if (leftright == true) + { + resizing_widget.Width += x_distance; + form_preview.Refresh(); + } + SetupResizeBorder(resizing_widget); + } + }; + } + bool ex = false; + var wloc = Point.Empty; + try + { + wloc = resizing_widget.Parent.PointToScreen(resizing_widget.Location); + } + catch + { + ex = true; + wloc = body.PointToScreen(resizing_widget.Location); //Must be a top-level widget. + } + wloc = body.PointToClient(wloc); + switch ((int)loc) + { + case 0: + handle.Left = wloc.X + ((resizing_widget.Width - handle.Width) / 2); + handle.Top = wloc.Y + resizing_widget.Height; + updown = true; + break; + case 1: + handle.Left = wloc.X + resizing_widget.Width + 5; + handle.Top = wloc.Y + resizing_widget.Height + 5; + updown = true; + leftright = true; + break; + case 2: + handle.Left = wloc.X + resizing_widget.Width; + handle.Top = wloc.Y + ((resizing_widget.Height - handle.Height) / 2); + leftright = true; + break; + case 3: + handle.Left = wloc.X + resizing_widget.Width; + handle.Top = wloc.Y + (resizing_widget.Top - handle.Top); + leftright = true; + updown = true; + break; + } + + handle.BringToFront(); + handle.Show(); + handle.Refresh(); + body.Refresh(); + } + + private Panel form_preview; + private Form current_form; + + public void DrawForm() + { + if (form_preview == null) + { + form_preview = new Panel(); + body.Widgets.Add(form_preview); + } + + if (current_form != null) + { + current_form.FormClosing += (o, a) => + { + a.Cancel = true; + }; + current_form.SizeChanged += (o, a) => + { + form_preview.Size = current_form.Size; + }; + current_form.Body.WidgetAdded += (o, a) => + { + SetupWidget(a.Widget); + }; + form_preview.Widgets.Clear(); + form_preview.Location = new Point(5, 5); + form_preview.Size = current_form.Size; + Widget[] widgets = new Widget[current_form.Widgets.Count]; + current_form.Widgets.CopyTo(widgets, 0); + Widget[] borderwidgets = new Widget[current_form.BorderWidgets.Length]; + current_form.BorderWidgets.CopyTo(borderwidgets, 0); + foreach (var widget in borderwidgets) + { + form_preview.Widgets.Add(widget); + widget.Show(); + SetupWidget(widget); + + } + + foreach (var widget in widgets) + { + if (!borderwidgets.Contains(widget)) + { + form_preview.Widgets.Add(widget); + widget.Show(); + if (widget == current_form.Body) + widget.BringToFront(); + SetupWidget(widget); + } + } + form_preview.Show(); + } + } + + public void SetupWidget(Widget widget) + { + if (string.IsNullOrEmpty(widget.Name)) + { + widget.MouseDown += (o, a) => + { + Select(current_form); + current_parent = current_form; + }; + } + else + { + widget.MouseDown += (o, a) => + { + Select(widget); + current_parent = widget; + }; + } + //give the widget the Container context menu + widget.ContextMenuStrip = container_menu; + + foreach(Widget w in widget.Widgets) + { + SetupWidget(w); + } + + } + + public List<Form> TopLevels = new List<Form>(); + private Widget current_parent = null; + private ContextMenuStrip widget_modification_menu = null; + + public void PopulateTopLevelNode(TreeNode node, Widget widget) + { + document_outline.Nodes.Add(node); + node.ContextMenuStrip = widget_modification_menu; + if (widget.Widgets != null && widget.Widgets.Count > 0) + { + foreach(Widget w in widget.Widgets) + { + PopulateNode(node, w); + } + } + } + + public void PopulateNode(TreeNode parent, Widget widget) + { + var t = new TreeNode(); + if (!string.IsNullOrEmpty(widget.Name)) + { + t.Text = widget.Name + ": " + widget.GetType().Name; + t.Tag = widget; + t.ContextMenuStrip = widget_modification_menu; + parent.Nodes.Add(t); + if (widget.Widgets.Count > 0) + { + foreach (Widget w in widget.Widgets) + { + PopulateNode(t, w); + } + } + t.Expand(); + } + else + { + } + } + + public void PopulateTree() + { + document_outline.Nodes.Clear(); + foreach (var form in TopLevels) + { + var t = new TreeNode(); + t.Text = form.Name + ": " + form.GetType().Name; + t.Tag = form; + PopulateTopLevelNode(t, current_form.Body); + t.Expand(); + } + } + + public MainEditorForm() + { + Text = "ShiftUI"; + this.Debug("Starting designer..."); + this.MaximumSize = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height - 40); + this.SizeChanged += (o, a) => + { + if (this.WindowState == FormWindowState.Maximized) + this.Location = new Point(0, 0); + }; + this.Size = new Size(1280, 720); + this.Load += (o, a) => + { + ResetSkin(); + }; + + //create menu + menu = new MenuStrip(); + this.Widgets.Add(menu); + menu.Show(); + + //top-levels + file = new ToolStripMenuItem(); + edit = new ToolStripMenuItem(); + about = new ToolStripMenuItem(); + file.Text = "File"; + edit.Text = "Edit"; + about.Text = "About"; + menu.Items.Add(file); + menu.Items.Add(edit); + menu.Items.Add(about); + + //about + a_about_shiftui = new ToolStripMenuItem(); + a_about_shiftui_designer = new ToolStripMenuItem(); + a_shiftos = new ToolStripMenuItem(); + a_about_shiftui.Text = "About ShiftUI"; + a_about_shiftui_designer.Text = "About ShiftUI Designer"; + a_shiftos.Text = "ShiftOS"; + about.DropDownItems.Add(a_about_shiftui); + about.DropDownItems.Add(a_about_shiftui_designer); + about.DropDownItems.Add(a_shiftos); + a_about_shiftui.Click += (o, a) => + { + Process.Start("http://playshiftos.ml/wiki/index/shiftui"); + }; + a_about_shiftui_designer.Click += (o, a) => + { + Process.Start("http://playshiftos.ml/wiki/index/shiftui-designer"); + }; + a_shiftos.Click += (o, a) => + { + Process.Start("http://playshiftos.ml"); + }; + + + widget_modification_menu = new ContextMenuStrip(); + var rename_widget = new ToolStripMenuItem(); + rename_widget.Text = "Rename"; + widget_modification_menu.Items.Add(rename_widget); + rename_widget.Click += (o, a) => + { + if (document_outline.SelectedNode != null) + { + var renamer = new WidgetRenameForm(document_outline.SelectedNode.Tag as Widget); + renamer.ShowDialog(); + PopulateTree(); + } + }; + + e_newform = new ToolStripMenuItem(); + e_newform.Text = "Add new form"; + edit.DropDownItems.Add(e_newform); + e_newform.Click += (o, a) => + { + var nf = new Form(); + nf.Name = $"Form{TopLevels.Count + 1}"; + TopLevels.Add(nf); + current_form = nf; + PopulateTree(); + DrawForm(); + }; + + var delete_widget = new ToolStripMenuItem(); + delete_widget.Text = "Delete"; + widget_modification_menu.Items.Add(delete_widget); + delete_widget.Click += (o, a) => + { + if (document_outline.SelectedNode != null) + { + var w = document_outline.SelectedNode.Tag as Widget; + if (properties.SelectedObject == w) + Select(null); //deselect the widget IF it's the selected widget. + if (w.Parent != null) + w.Parent.Widgets.Remove(w); + + if (w is Form) + { + if (TopLevels.Contains(w as Form)) + { + bool should_redraw = current_form == w as Form; + Select(null); //have to deselect whatever is selected in case it's this form or + //it's a part of it's family (child, grandchild, etc) + TopLevels.Remove(w as Form); + if (TopLevels.Count > 0 && should_redraw == true) + { + current_form = TopLevels[0]; + DrawForm(); + } + else + { + current_form = null; + form_preview.Hide(); + } + } + } + + w.Hide(); + w.Dispose(); + + PopulateTree(); + + } + + }; + + + ///Main UI + + //Split panel + var splitter_parent = new Panel(); + this.Widgets.Add(splitter_parent); + splitter_parent.Dock = DockStyle.Fill; + splitter_parent.Show(); + //split panel - left panel + var splitter_left = new Panel(); + splitter_left.Dock = DockStyle.Left; + splitter_left.Width = splitter_parent.Width / 3; + splitter_parent.Widgets.Add(splitter_left); + splitter_left.Show(); + + //left panel - property grid + properties = new PropertyGrid(); + properties.Dock = DockStyle.Bottom; + properties.Height = splitter_left.Height / 2; + splitter_left.Widgets.Add(properties); + properties.Show(); + + //left panel - document tree + document_outline = new TreeView(); + document_outline.Dock = DockStyle.Fill; + splitter_left.Widgets.Add(document_outline); + document_outline.Show(); + + //splitter panel - body + body = new Panel(); + body.Dock = DockStyle.Fill; + splitter_parent.Widgets.Add(body); + body.Show(); + splitter_left.SendToBack(); + + //add a new form to the toplevel + var f = new Form(); + f.Name = "Form1"; + f.Text = "Form1"; + TopLevels.Add(f); + + //populate tree + current_form = TopLevels[0]; + PopulateTree(); + + menu.SendToBack(); + + //Create the container context menu + container_menu = new ContextMenuStrip(); + c_add_widget = new ToolStripMenuItem(); + c_add_widget.Text = "Add widget"; + container_menu.Items.Add(c_add_widget); + c_add_widget.Click += (o, a) => + { + var wselector = new WidgetSelectorForm(current_parent); + wselector.ShowDialog(); + PopulateTree(); + }; + + //draw the first top-level + DrawForm(); + Select(current_form); + document_outline.Click += (o, a) => + { + if(document_outline.SelectedNode != null) + { + Select(document_outline.SelectedNode.Tag as Widget); + } + }; + + f_export = new ToolStripMenuItem(); + f_export.Text = "Export as..."; + file.DropDownItems.Add(f_export); + + f_export_cs = new ToolStripMenuItem(); + f_export_cs.Text = "C# Code File"; + f_export.DropDownItems.Add(f_export_cs); + f_export_cs.Click += (o, a) => + { + MessageBox.Show("This is an experimental feature of the ShiftUI designer. Although we can handle most of the heavy lifting and you won't have to type lots of UI code, we may mess up a bit in some areas, and in that case, you may experience errors in the generated code.", "Warning: Experimental"); + ExportCS(TopLevels); + }; + } + + public string GenerateWidgetCSharp(Widget parent, Widget w) + { + string csharp = ""; + var wtype = w.GetType(); + string wName = w.Name; + if (w is Form && TopLevels.Contains(w as Form)) + wName = "this"; + string parentName = ""; + if (parent != null) + parentName = parent.Name; + if (parent is Form && TopLevels.Contains(parent as Form)) + parentName = "this"; + + + Type[] valid_types = { typeof(string), typeof(int), typeof(Color), typeof(bool), typeof(Size), typeof(Point), typeof(Font) }; + + //Instantiate widget. + if(wName != "this") + csharp += $"\t\t\t{wName} = new {wtype.Name}();\r\n"; + + //get all public properties of widget + var propertyinf = wtype.GetProperties(BindingFlags.Public | BindingFlags.Instance); + foreach (var p in propertyinf) + { + if (!p.CanRead || !p.CanWrite) + continue; + + var get = p.GetGetMethod(); + var set = p.GetSetMethod(); + + if (get == null) + continue; + + if (set == null) + continue; + + if (valid_types.Contains(p.PropertyType) || p.PropertyType.IsEnum) + { + if (p.Name != "TransparencyKey") + { + csharp += $"\t\t\t{wName}.{p.Name} = "; + + if (p.PropertyType == typeof(string)) + csharp += $"\"{p.GetValue(w)}\""; + else if (p.PropertyType == typeof(Cursor)) + csharp += $"{p.GetValue(w).ToString().Replace("[", "").Replace("]", "")}"; + else if (p.PropertyType.IsEnum) + { + try + { + int enum_val = int.Parse(p.GetValue(w).ToString()); + csharp += $"({p.PropertyType.Name}){enum_val}"; + } + catch + { + csharp += $"{p.PropertyType.Name}.{p.GetValue(w).ToString().Replace(", ", $" | {p.PropertyType.Name}.")}"; + } + } + else if (p.PropertyType == typeof(bool)) + csharp += $"{p.GetValue(w).ToString().ToLower()}"; //Booleans are always lowercase in C#. + else if (p.PropertyType == typeof(int)) + csharp += $"{p.GetValue(w)}"; + else if (p.PropertyType == typeof(Point)) + { + var point = (Point)p.GetValue(w); + csharp += $"new Point({point.X}, {point.Y})"; + } + else if (p.PropertyType == typeof(Size)) + { + var size = (Size)p.GetValue(w); + csharp += $"new Size({size.Width}, {size.Height})"; + } + else if (p.PropertyType == typeof(Color)) + { + //Bug: Form.TransparencyKey seems to always be set to black if the user doesn't explicitly + //set it. This causes the close, minimize and maximize buttons, as well as text to appear transparent. + // + //Workaround: Disallow TransparencyKey. + var color = (Color)p.GetValue(w); + csharp += $"Color.FromArgb({color.R}, {color.G}, {color.B})"; + } + else if (p.PropertyType == typeof(Font)) + { + var font = (Font)p.GetValue(w); + csharp += $"new Font(\"{font.FontFamily.Name}\", (float){font.Size}, (FontStyle){(int)font.Style})"; + } + csharp += ";\r\n"; + } + } + } + if (wName == "this") + { + csharp += "\r\n"; + } + else + { + csharp += $"\t\t\t{parentName}.Widgets.Add({wName});\r\n"; + csharp += $"\t\t\t{wName}.Show();\r\n"; + } + + foreach(Widget wid in w.Widgets) + { + csharp += GenerateWidgetCSharp(w, wid); + } + + return csharp; + } + + public string GenerateWidgetMembersCSharp(Widget widget) + { + var t = widget.GetType(); + string csharp = ""; + csharp += "public " + t.Name + " " + widget.Name + " = null;"; + foreach(Widget w in widget.Widgets) + { + csharp += GenerateWidgetMembersCSharp(w); + } + return csharp; + } + + public void ExportCS(List<Form> forms) + { + string csharp = ""; + csharp += @"/* <auto-generated> + * This piece of code was automatically + * generated by the ShiftUI designer. + * </auto-generated> + */ + +using System; +using System.IO; +using System.Collections.Generic; +using System.Linq; +using System.Drawing; +using ShiftUI; + +namespace ShiftUI_DesignerGenerated { +"; + foreach (var form in forms) + { + csharp += $@" public class {form.Name} : Form" + @" { + public " + form.Name + @"() { +"; + + csharp += GenerateWidgetCSharp(null, form); + + //Widget property setup. + foreach (Widget widget in form.Body.Widgets) + { + csharp += GenerateWidgetCSharp(form as Widget, widget); + } + + csharp += @" + } +"; + + foreach (Widget widget in form.Body.Widgets) + { + csharp += GenerateWidgetMembersCSharp(widget); + } + + csharp += @" + }"; + } + csharp += @" +}"; + var f = new Form(); + var rtb = new RichTextBox(); + f.Text = "C# Output"; + rtb.Dock = DockStyle.Fill; + f.Show(); + f.Widgets.Add(rtb); + rtb.Show(); + rtb.Text = csharp; + + } + + private Panel body = null; + private PropertyGrid properties = null; + private MenuStrip menu = null; + private TreeView document_outline = null; + private ContextMenuStrip container_menu = null; + + private ToolStripMenuItem c_add_widget = null; + + private ToolStripMenuItem file = null; + private ToolStripMenuItem f_new = null; + private ToolStripMenuItem f_open = null; + private ToolStripMenuItem f_save = null; + private ToolStripMenuItem f_export = null; + + private ToolStripMenuItem f_export_vb = null; + private ToolStripMenuItem f_export_cs = null; + private ToolStripMenuItem f_export_lua = null; + + private ToolStripMenuItem f_exit = null; + + private ToolStripMenuItem edit = null; + private ToolStripMenuItem e_newform = null; + + private ToolStripMenuItem about = null; + private ToolStripMenuItem a_about_shiftui = null; + private ToolStripMenuItem a_about_shiftui_designer = null; + private ToolStripMenuItem a_shiftos = null; + + + } + + public class WidgetSelectorForm : Form + { + public WidgetSelectorForm(Widget parent) + { + this.Text = "Add Widget"; + Size = new Size(400, 400); + var cbox = new ComboBox(); + foreach(var t in Widget.GetAllToolboxWidgets()) + { + cbox.Items.Add(t.Name); + } + this.Widgets.Add(cbox); + cbox.Location = new Point(10, 50); + cbox.Width = this.Width - (cbox.Left * 2); + var cbox_label = new Label(); + cbox_label.Text = "Select widget type"; + cbox_label.Top = cbox.Top - cbox_label.Height; + cbox_label.Left = 10; + this.Widgets.Add(cbox_label); + var widget_name = new TextBox(); + widget_name.Location = new Point(10, 100); + widget_name.Width = this.Width - (widget_name.Left * 2); + this.Widgets.Add(widget_name); + widget_name.Show(); + var widget_name_label = new Label(); + widget_name_label.Text = "Widget name"; + widget_name_label.Left = 10; + widget_name_label.Top = widget_name.Top - widget_name_label.Height; + this.Widgets.Add(widget_name_label); + widget_name_label.Show(); + cbox.Show(); + cbox_label.Show(); + DialogResult = DialogResult.Cancel; + Button confirm = new Button(); + confirm.AutoSize = true; + confirm.AutoSizeMode = AutoSizeMode.GrowAndShrink; + confirm.Text = "Add"; + confirm.Click += (o, a) => + { + if (string.IsNullOrEmpty(cbox.Text)) + { + MessageBox.Show("Error: Widget type not present. You must select a valid widget type.", "Add Widget"); return; + } + + if(string.IsNullOrEmpty(widget_name.Text)) + { + MessageBox.Show("Error: Widget name cannot be empty as it is required for the variable name when generating code.", "Add Widget"); return; + } + + //get the type of the selected widget + Type type = null; + foreach (var t in Widget.GetAllToolboxWidgets()) + { + if (t.Name == cbox.SelectedItem.ToString()) + { + type = t; + } + } + if(type == null) + { + MessageBox.Show("Error: Invalid widget type. The type could not be found.", "Add Widget"); return; + } + try + { + Widget w = Activator.CreateInstance(type) as Widget; + w.Name = widget_name.Text; + parent.Widgets.Add(w); + w.Show(); + } + catch(Exception ex) + { + MessageBox.Show($"Error: Couldn't create widget instance from type! {ex.Message}", "Add Widget"); return; + } + + this.DialogResult = DialogResult.OK; + this.Close(); + }; + var bottom_panel = new FlowLayoutPanel(); + bottom_panel.Dock = DockStyle.Bottom; + bottom_panel.Height = 25; + this.Widgets.Add(bottom_panel); + bottom_panel.Show(); + bottom_panel.BackColor = Application.CurrentSkin.MessageBox_BottomPanel; + bottom_panel.Widgets.Add(confirm); + confirm.Show(); + } + } + + public class WidgetRenameForm : Form + { + public WidgetRenameForm(Widget widget) + { + this.AllowTransparency = false; + this.AutoScale = true; + this.AutoScaleBaseSize = new Size(5, 13); + this.AutoScroll = false; + this.AutoSize = false; + this.AutoSizeMode = AutoSizeMode.GrowOnly; + this.AutoValidate = AutoValidate.Inherit; + this.BackColor = Color.FromArgb(240, 240, 240); + this.ClientSize = new Size(300, 132); + this.WidgetBox = true; + this.DesktopLocation = new Point(0, 0); + this.DialogResult = DialogResult.None; + this.FormBorderStyle = FormBorderStyle.FixedSingle; + this.HelpButton = false; + this.IsMdiContainer = false; + this.KeyPreview = false; + this.MaximizeBox = true; + this.MaximumSize = new Size(0, 0); + this.MinimizeBox = true; + this.MinimumSize = new Size(0, 0); + this.RightToLeftLayout = false; + this.ShowIcon = true; + this.ShowInTaskbar = true; + this.Size = new Size(300, 132); + this.SizeGripStyle = SizeGripStyle.Auto; + this.StartPosition = FormStartPosition.WindowsDefaultLocation; + this.TabIndex = 0; + this.TabStop = true; + this.TopLevel = true; + this.TopMost = false; + this.WindowState = FormWindowState.Normal; + this.Text = "Rename widget"; + this.Location = new Point(0, 0); + this.AutoScaleMode = AutoScaleMode.Inherit; + this.AutoScrollMargin = new Size(0, 0); + this.AutoScrollMinSize = new Size(0, 0); + this.AutoScrollPosition = new Point(0, 0); + this.Alignment = (ContentAlignment)0; + this.AccessibleDefaultActionDescription = ""; + this.AccessibleDescription = ""; + this.AccessibleName = ""; + this.AccessibleRole = AccessibleRole.Default; + this.AllowDrop = false; + this.Anchor = AnchorStyles.Top | AnchorStyles.Left; + this.AutoScrollOffset = new Point(0, 0); + this.BackgroundImageLayout = ImageLayout.Tile; + this.Capture = false; + this.CausesValidation = true; + this.Dock = DockStyle.None; + this.Enabled = true; + this.Font = new Font("Microsoft Sans Serif", (float)8.25, (FontStyle)0); + this.ForeColor = Color.FromArgb(0, 0, 0); + this.Height = 132; + this.ImeMode = ImeMode.NoControl; + this.IsAccessible = false; + this.Left = 0; + this.Name = "Form1"; + this.RightToLeft = RightToLeft.No; + this.Top = 0; + this.UseWaitCursor = false; + this.Visible = false; + this.Width = 300; + + btnok = new Button(); + btnok.AutoSizeMode = AutoSizeMode.GrowOnly; + btnok.DialogResult = DialogResult.None; + btnok.AutoEllipsis = false; + btnok.AutoSize = false; + btnok.BackColor = Color.FromArgb(255, 255, 0); + btnok.FlatStyle = FlatStyle.Standard; + btnok.ImageAlign = ContentAlignment.MiddleCenter; + btnok.ImageIndex = -1; + btnok.ImageKey = ""; + btnok.ImeMode = ImeMode.Disable; + btnok.Text = "Rename"; + btnok.TextAlign = ContentAlignment.MiddleCenter; + btnok.TextImageRelation = TextImageRelation.Overlay; + btnok.UseCompatibleTextRendering = true; + btnok.UseMnemonic = true; + btnok.UseVisualStyleBackColor = true; + btnok.Alignment = (ContentAlignment)0; + btnok.AccessibleDefaultActionDescription = ""; + btnok.AccessibleDescription = ""; + btnok.AccessibleName = ""; + btnok.AccessibleRole = AccessibleRole.Default; + btnok.AllowDrop = false; + btnok.Anchor = AnchorStyles.Top | AnchorStyles.Left; + btnok.AutoScrollOffset = new Point(0, 0); + btnok.MaximumSize = new Size(0, 0); + btnok.MinimumSize = new Size(0, 0); + btnok.BackgroundImageLayout = ImageLayout.Tile; + btnok.Capture = false; + btnok.CausesValidation = true; + btnok.ClientSize = new Size(75, 23); + btnok.Dock = DockStyle.None; + btnok.Enabled = true; + btnok.Font = new Font("Microsoft Sans Serif", (float)8.25, (FontStyle)0); + btnok.ForeColor = Color.FromArgb(0, 0, 0); + btnok.Height = 23; + btnok.IsAccessible = false; + btnok.Left = 210; + btnok.Location = new Point(210, 66); + btnok.Name = "btnok"; + btnok.RightToLeft = RightToLeft.No; + btnok.Size = new Size(75, 23); + btnok.TabIndex = 0; + btnok.TabStop = true; + btnok.Top = 66; + btnok.UseWaitCursor = false; + btnok.Visible = true; + btnok.Width = 75; + this.Widgets.Add(btnok); + btnok.Show(); + btnok.Click += (o, a) => + { + if(string.IsNullOrEmpty(txtnewname.Text)) + { + MessageBox.Show("Error: The new name must not be empty!", "Rename Widget"); return; + } + + widget.Name = txtnewname.Text.Replace(" ", ""); + this.Close(); + }; + + txtnewname = new TextBox(); + txtnewname.AutoCompleteMode = AutoCompleteMode.None; + txtnewname.AutoCompleteSource = AutoCompleteSource.None; + txtnewname.UseSystemPasswordChar = false; + txtnewname.AcceptsReturn = false; + txtnewname.CharacterCasing = CharacterCasing.Normal; + txtnewname.ScrollBars = ScrollBars.None; + txtnewname.Text = widget.Name; + txtnewname.TextAlign = HorizontalAlignment.Left; + txtnewname.Multiline = false; + txtnewname.AcceptsTab = false; + txtnewname.AutoSize = true; + txtnewname.BackColor = Color.FromArgb(240, 240, 240); + txtnewname.BorderStyle = BorderStyle.Fixed3D; + txtnewname.ForeColor = Color.FromArgb(0, 0, 0); + txtnewname.HideSelection = true; + txtnewname.MaxLength = 32767; + txtnewname.Modified = true; + txtnewname.ReadOnly = false; + txtnewname.SelectedText = ""; + txtnewname.SelectionLength = 0; + txtnewname.SelectionStart = 14; + txtnewname.ShortcutsEnabled = true; + txtnewname.WordWrap = true; + txtnewname.BackgroundImageLayout = ImageLayout.Tile; + txtnewname.Alignment = (ContentAlignment)0; + txtnewname.AccessibleDefaultActionDescription = ""; + txtnewname.AccessibleDescription = ""; + txtnewname.AccessibleName = ""; + txtnewname.AccessibleRole = AccessibleRole.Default; + txtnewname.AllowDrop = false; + txtnewname.Anchor = AnchorStyles.Top | AnchorStyles.Left; + txtnewname.AutoScrollOffset = new Point(0, 0); + txtnewname.MaximumSize = new Size(0, 0); + txtnewname.MinimumSize = new Size(0, 0); + txtnewname.Capture = false; + txtnewname.CausesValidation = true; + txtnewname.ClientSize = new Size(272, 20); + txtnewname.Dock = DockStyle.None; + txtnewname.Enabled = true; + txtnewname.Font = new Font("Microsoft Sans Serif", (float)8.25, (FontStyle)0); + txtnewname.Height = 24; + txtnewname.ImeMode = ImeMode.NoControl; + txtnewname.IsAccessible = false; + txtnewname.Left = 11; + txtnewname.Location = new Point(11, 35); + txtnewname.Name = "txtnewname"; + txtnewname.RightToLeft = RightToLeft.No; + txtnewname.Size = new Size(276, 24); + txtnewname.TabIndex = 1; + txtnewname.TabStop = true; + txtnewname.Top = 35; + txtnewname.UseWaitCursor = false; + txtnewname.Visible = true; + txtnewname.Width = 276; + this.Widgets.Add(txtnewname); + txtnewname.Show(); + lblnewnamelabel = new Label(); + lblnewnamelabel.AutoEllipsis = false; + lblnewnamelabel.AutoSize = false; + lblnewnamelabel.BackgroundImageLayout = ImageLayout.Tile; + lblnewnamelabel.BorderStyle = BorderStyle.None; + lblnewnamelabel.FlatStyle = FlatStyle.Standard; + lblnewnamelabel.ImageAlign = ContentAlignment.MiddleCenter; + lblnewnamelabel.ImageIndex = -1; + lblnewnamelabel.ImageKey = ""; + lblnewnamelabel.ImeMode = ImeMode.NoControl; + lblnewnamelabel.TabStop = false; + lblnewnamelabel.TextAlign = ContentAlignment.TopLeft; + lblnewnamelabel.UseMnemonic = true; + lblnewnamelabel.UseCompatibleTextRendering = true; + lblnewnamelabel.Text = "New name:"; + lblnewnamelabel.Alignment = (ContentAlignment)0; + lblnewnamelabel.AccessibleDefaultActionDescription = ""; + lblnewnamelabel.AccessibleDescription = ""; + lblnewnamelabel.AccessibleName = ""; + lblnewnamelabel.AccessibleRole = AccessibleRole.Default; + lblnewnamelabel.AllowDrop = false; + lblnewnamelabel.Anchor = AnchorStyles.Top | AnchorStyles.Left; + lblnewnamelabel.AutoScrollOffset = new Point(0, 0); + lblnewnamelabel.MaximumSize = new Size(0, 0); + lblnewnamelabel.MinimumSize = new Size(0, 0); + lblnewnamelabel.BackColor = Color.FromArgb(255, 255, 0); + lblnewnamelabel.Capture = false; + lblnewnamelabel.CausesValidation = true; + lblnewnamelabel.ClientSize = new Size(100, 23); + lblnewnamelabel.Dock = DockStyle.None; + lblnewnamelabel.Enabled = true; + lblnewnamelabel.Font = new Font("Microsoft Sans Serif", (float)8.25, (FontStyle)0); + lblnewnamelabel.ForeColor = Color.FromArgb(0, 0, 0); + lblnewnamelabel.Height = 23; + lblnewnamelabel.IsAccessible = false; + lblnewnamelabel.Left = 12; + lblnewnamelabel.Location = new Point(12, 18); + lblnewnamelabel.Name = "lblnewnamelabel"; + lblnewnamelabel.RightToLeft = RightToLeft.No; + lblnewnamelabel.Size = new Size(100, 23); + lblnewnamelabel.TabIndex = 2; + lblnewnamelabel.Top = 18; + lblnewnamelabel.UseWaitCursor = false; + lblnewnamelabel.Visible = true; + lblnewnamelabel.Width = 100; + this.Widgets.Add(lblnewnamelabel); + lblnewnamelabel.Show(); + + } + public Button btnok = null; public TextBox txtnewname = null; public Label lblnewnamelabel = null; + } + + public class FormSelectionForm : Form + { + public FormSelectionForm() + { + this.AllowTransparency = false; + this.AutoScale = true; + this.AutoScaleBaseSize = new Size(5, 13); + this.AutoScroll = false; + this.AutoSize = false; + this.AutoSizeMode = AutoSizeMode.GrowOnly; + this.AutoValidate = AutoValidate.Inherit; + this.BackColor = Color.FromArgb(240, 240, 240); + this.ClientSize = new Size(456, 395); + this.WidgetBox = true; + this.DesktopLocation = new Point(0, 0); + this.DialogResult = DialogResult.None; + this.FormBorderStyle = FormBorderStyle.Sizable; + this.HelpButton = false; + this.IsMdiContainer = false; + this.KeyPreview = false; + this.MaximizeBox = true; + this.MaximumSize = new Size(0, 0); + this.MinimizeBox = true; + this.MinimumSize = new Size(0, 0); + this.RightToLeftLayout = false; + this.ShowIcon = true; + this.ShowInTaskbar = true; + this.Size = new Size(456, 395); + this.SizeGripStyle = SizeGripStyle.Auto; + this.StartPosition = FormStartPosition.WindowsDefaultLocation; + this.TabIndex = 0; + this.TabStop = true; + this.TopLevel = true; + this.TopMost = false; + this.WindowState = FormWindowState.Normal; + this.Text = "Select form"; + this.Location = new Point(0, 0); + this.AutoScaleMode = AutoScaleMode.Inherit; + this.AutoScrollMargin = new Size(0, 0); + this.AutoScrollMinSize = new Size(0, 0); + this.AutoScrollPosition = new Point(0, 0); + this.Alignment = (ContentAlignment)0; + this.AccessibleDefaultActionDescription = ""; + this.AccessibleDescription = ""; + this.AccessibleName = ""; + this.AccessibleRole = AccessibleRole.Default; + this.AllowDrop = false; + this.Anchor = AnchorStyles.Top | AnchorStyles.Left; + this.AutoScrollOffset = new Point(0, 0); + this.BackgroundImageLayout = ImageLayout.Tile; + this.Capture = false; + this.CausesValidation = true; + this.Dock = DockStyle.None; + this.Enabled = true; + this.Font = new Font("Microsoft Sans Serif", (float)8.25, (FontStyle)0); + this.ForeColor = Color.FromArgb(0, 0, 0); + this.Height = 395; + this.ImeMode = ImeMode.NoControl; + this.IsAccessible = false; + this.Left = 0; + this.Name = "Form1"; + this.RightToLeft = RightToLeft.No; + this.Top = 0; + this.UseWaitCursor = false; + this.Visible = false; + this.Width = 456; + + lblabel = new Label(); + lblabel.AutoEllipsis = false; + lblabel.AutoSize = false; + lblabel.BackgroundImageLayout = ImageLayout.Tile; + lblabel.BorderStyle = BorderStyle.None; + lblabel.FlatStyle = FlatStyle.Standard; + lblabel.ImageAlign = ContentAlignment.MiddleCenter; + lblabel.ImageIndex = -1; + lblabel.ImageKey = ""; + lblabel.ImeMode = ImeMode.NoControl; + lblabel.TabStop = false; + lblabel.TextAlign = ContentAlignment.TopLeft; + lblabel.UseMnemonic = true; + lblabel.UseCompatibleTextRendering = true; + lblabel.Text = "Please select a form inside the ShiftOS code to load into the ShiftUI designer."; + lblabel.Alignment = (ContentAlignment)0; + lblabel.AccessibleDefaultActionDescription = ""; + lblabel.AccessibleDescription = ""; + lblabel.AccessibleName = ""; + lblabel.AccessibleRole = AccessibleRole.Default; + lblabel.AllowDrop = false; + lblabel.Anchor = AnchorStyles.Top | AnchorStyles.Left; + lblabel.AutoScrollOffset = new Point(0, 0); + lblabel.MaximumSize = new Size(0, 0); + lblabel.MinimumSize = new Size(0, 0); + lblabel.BackColor = Color.FromArgb(240, 240, 240); + lblabel.Capture = false; + lblabel.CausesValidation = true; + lblabel.ClientSize = new Size(390, 16); + lblabel.Dock = DockStyle.None; + lblabel.Enabled = true; + lblabel.Font = new Font("Microsoft Sans Serif", (float)8.25, (FontStyle)0); + lblabel.ForeColor = Color.FromArgb(0, 0, 0); + lblabel.Height = 16; + lblabel.IsAccessible = false; + lblabel.Left = 19; + lblabel.Location = new Point(19, 18); + lblabel.Name = "lblabel"; + lblabel.RightToLeft = RightToLeft.No; + lblabel.Size = new Size(390, 16); + lblabel.TabIndex = 0; + lblabel.Top = 18; + lblabel.UseWaitCursor = false; + lblabel.Visible = true; + lblabel.Width = 390; + this.Widgets.Add(lblabel); + lblabel.Show(); + lvforms = new ListView(); + lvforms.Activation = ItemActivation.Standard; + lvforms.Alignment = ListViewAlignment.Top; + lvforms.AllowColumnReorder = false; + lvforms.AutoArrange = true; + lvforms.BackColor = Color.FromArgb(255, 255, 255); + lvforms.BackgroundImageLayout = ImageLayout.Tile; + lvforms.BackgroundImageTiled = false; + lvforms.BorderStyle = BorderStyle.Fixed3D; + lvforms.CheckBoxes = false; + lvforms.ForeColor = Color.FromArgb(0, 0, 0); + lvforms.FullRowSelect = false; + lvforms.GridLines = false; + lvforms.HeaderStyle = ColumnHeaderStyle.Clickable; + lvforms.HideSelection = true; + lvforms.HotTracking = false; + lvforms.HoverSelection = false; + lvforms.LabelEdit = false; + lvforms.LabelWrap = true; + lvforms.MultiSelect = true; + lvforms.OwnerDraw = false; + lvforms.RightToLeftLayout = false; + lvforms.Scrollable = true; + lvforms.ShowGroups = true; + lvforms.ShowItemToolTips = false; + lvforms.Sorting = SortOrder.None; + lvforms.Text = ""; + lvforms.UseCompatibleStateImageBehavior = false; + lvforms.View = View.List; + lvforms.VirtualMode = false; + lvforms.VirtualListSize = 0; + lvforms.Alignment = (ListViewAlignment)0; + lvforms.AccessibleDefaultActionDescription = ""; + lvforms.AccessibleDescription = ""; + lvforms.AccessibleName = ""; + lvforms.AccessibleRole = AccessibleRole.Default; + lvforms.AllowDrop = false; + lvforms.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + lvforms.AutoScrollOffset = new Point(0, 0); + lvforms.AutoSize = false; + lvforms.MaximumSize = new Size(0, 0); + lvforms.MinimumSize = new Size(0, 0); + lvforms.Capture = false; + lvforms.CausesValidation = true; + lvforms.ClientSize = new Size(407, 280); + lvforms.Dock = DockStyle.None; + lvforms.Enabled = true; + lvforms.Font = new Font("Microsoft Sans Serif", (float)8.25, (FontStyle)0); + lvforms.Height = 284; + lvforms.ImeMode = ImeMode.NoControl; + lvforms.IsAccessible = false; + lvforms.Left = 16; + lvforms.Location = new Point(16, 36); + lvforms.Name = "lvforms"; + lvforms.RightToLeft = RightToLeft.No; + lvforms.Size = new Size(411, 284); + lvforms.TabIndex = 1; + lvforms.TabStop = true; + lvforms.Top = 36; + lvforms.UseWaitCursor = false; + lvforms.Visible = true; + lvforms.Width = 411; + this.Widgets.Add(lvforms); + lvforms.Show(); + btnstart = new Button(); + btnstart.AutoSizeMode = AutoSizeMode.GrowOnly; + btnstart.DialogResult = DialogResult.None; + btnstart.AutoEllipsis = false; + btnstart.AutoSize = false; + btnstart.BackColor = Color.FromArgb(240, 240, 240); + btnstart.FlatStyle = FlatStyle.Standard; + btnstart.ImageAlign = ContentAlignment.MiddleCenter; + btnstart.ImageIndex = -1; + btnstart.ImageKey = ""; + btnstart.ImeMode = ImeMode.Disable; + btnstart.Text = "Design"; + btnstart.TextAlign = ContentAlignment.MiddleCenter; + btnstart.TextImageRelation = TextImageRelation.Overlay; + btnstart.UseCompatibleTextRendering = true; + btnstart.UseMnemonic = true; + btnstart.UseVisualStyleBackColor = true; + btnstart.Alignment = (ContentAlignment)0; + btnstart.AccessibleDefaultActionDescription = ""; + btnstart.AccessibleDescription = ""; + btnstart.AccessibleName = ""; + btnstart.AccessibleRole = AccessibleRole.Default; + btnstart.AllowDrop = false; + btnstart.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + btnstart.AutoScrollOffset = new Point(0, 0); + btnstart.MaximumSize = new Size(0, 0); + btnstart.MinimumSize = new Size(0, 0); + btnstart.BackgroundImageLayout = ImageLayout.Tile; + btnstart.Capture = false; + btnstart.CausesValidation = true; + btnstart.ClientSize = new Size(75, 23); + btnstart.Dock = DockStyle.None; + btnstart.Enabled = true; + btnstart.Font = new Font("Microsoft Sans Serif", (float)8.25, (FontStyle)0); + btnstart.ForeColor = Color.FromArgb(0, 0, 0); + btnstart.Height = 23; + btnstart.IsAccessible = false; + btnstart.Left = 17; + btnstart.Location = new Point(17, 329); + btnstart.Name = "btnstart"; + btnstart.RightToLeft = RightToLeft.No; + btnstart.Size = new Size(75, 23); + btnstart.TabIndex = 2; + btnstart.TabStop = true; + btnstart.Top = 329; + btnstart.UseWaitCursor = false; + btnstart.Visible = true; + btnstart.Width = 75; + this.Widgets.Add(btnstart); + btnstart.Show(); + btnstart.Click += (o, a) => + { + if(lvforms.SelectedItems.Count > 0) + { + var form = lvforms.SelectedItems[0].Tag as Form; + var designer = new MainEditorForm(); + designer.LoadFormFromClass(form); + } + }; + ShiftOS.Program.Main(new[] { "nodisplay" }); + ShiftOS.API.ShouldLoadEngine = false; //so that things like bitnote wallet don't try to connect to server and stuff + Assembly asm = Assembly.Load("ShiftOS"); + Type[] types = asm.GetTypes(); + Dictionary<string, Form> forms = new Dictionary<string, Form>(); + foreach(var type in types) + { + try + { + if (type.BaseType == typeof(Form) && type != this.GetType()) + { + this.Debug($"Found form \"{type.Name}\" in assembly \"{asm.FullName}\"..."); + forms.Add(type.Name, Activator.CreateInstance(type) as Form); + } + } + catch (Exception catcherror) + { + Console.WriteLine(catcherror); + } + } + foreach(var item in forms) + { + var lvitem = new ListViewItem(); + lvitem.Text = item.Key; + lvitem.Tag = item.Value; + lvforms.Items.Add(lvitem); + } + + } + public Label lblabel = null; public ListView lvforms = null; public Button btnstart = null; + } + +} + + |
