Esempio n. 1
0
        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog();

            ofd.DefaultExt = "xml";
            ofd.Filter     = "OSHGui File (*.xml)|*.xml";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    var control = ControlSerializer.Deserialize(XElement.Load(ofd.FileName));
                    if (control is GuiControls.Form)
                    {
                        form          = control as GuiControls.Form;
                        form.DragEnd += control_DragEnd;

                        ControlManager.Instance().Clear();
                        ControlManager.Instance().RegisterControl(control);

                        form.RegisterInternalControls();

                        controlComboBox.Items.Clear();
                        controlComboBox.Items.AddRange(ControlManager.Instance().Controls.ToArray());
                        controlComboBox.SelectedItem = control;

                        foreach (var scalableControl in ControlManager.Instance().Controls.OfType <ScalableControl>())
                        {
                            if (scalableControl != form)
                            {
                                scalableControl.MouseDown += control_MouseDown;
                                scalableControl.MouseMove += control_MouseMove;
                                scalableControl.MouseUp   += control_MouseUp;
                                scalableControl.DragEnd   += control_DragEnd;
                            }
                        }

                        canvasPictureBox.Invalidate();
                    }
                    else
                    {
                        MessageBox.Show("Invalid file!");
                    }
                }
                catch (Exception serializeError)
                {
                    MessageBox.Show("Error while parsing " + Path.GetFileName(ofd.FileName) + "\n\n" + serializeError.Message, "OSHGui Parse Error");
                }
            }
        }
Esempio n. 2
0
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var sfd = new SaveFileDialog {
                DefaultExt = "xml"
            };

            sfd.Filter   = "OSHGui File (*." + sfd.DefaultExt + ")|*." + sfd.DefaultExt;
            sfd.FileName = form.Name + "." + sfd.DefaultExt;

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                ControlSerializer.Serialize(form).Save(sfd.FileName);
            }
        }
Esempio n. 3
0
        private void canvasPictureBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (GuiControls.Control.FocusedControl != null)
            {
                if (e.KeyCode == Keys.Delete)
                {
                    var removeControl = GuiControls.Control.FocusedControl;
                    var parent        = removeControl.RealParent ?? form;

                    RecursiveRemove(removeControl);

                    parent.Focus();

                    controlComboBox.SelectedIndex = 0;
                }
                else if (e.Control && e.KeyCode == Keys.C)
                {
                    if (GuiControls.Control.FocusedControl == form)
                    {
                        return;
                    }

                    var copiedControl = GuiControls.Control.FocusedControl.Copy();
                    Clipboard.SetData(
                        "OSHVisualGuiControl",
                        ControlSerializer.Serialize(copiedControl).ToString()
                        );
                }
                else if (e.Control && e.KeyCode == Keys.X)
                {
                    if (GuiControls.Control.FocusedControl == form)
                    {
                        return;
                    }

                    Clipboard.SetData(
                        "OSHVisualGuiControl",
                        ControlSerializer.Serialize(GuiControls.Control.FocusedControl).ToString()
                        );

                    RecursiveRemove(GuiControls.Control.FocusedControl);

                    controlComboBox.SelectedIndex = 0;
                }
                else if (e.Control && e.KeyCode == Keys.V)
                {
                    if (!Clipboard.ContainsData("OSHVisualGuiControl"))
                    {
                        return;
                    }

                    if (Clipboard.GetData("OSHVisualGuiControl") is string serializedControl)
                    {
                        var copiedControl = ControlSerializer.Deserialize(XElement.Parse(serializedControl));
                        if (copiedControl != null)
                        {
                            ContainerControl parent = null;
                            if (copiedControl is GuiControls.TabPage && !(GuiControls.Control.FocusedControl is GuiControls.TabControl))
                            {
                                MessageBox.Show("A TabPage needs to be inserted into a TabControl.");
                                return;
                            }
                            if (GuiControls.Control.FocusedControl is ContainerControl)
                            {
                                parent = GuiControls.Control.FocusedControl as ContainerControl;
                            }
                            else
                            {
                                parent = GuiControls.Control.FocusedControl.RealParent;
                            }

                            //check if name already exists
                            if (ControlManager.Instance().FindByName(copiedControl.Name, null) != null)
                            {
                                //then change the name
                                copiedControl.Name = copiedControl.DefaultName + ControlManager.Instance().GetControlCountPlusOne(copiedControl.GetType());
                                //and change the location
                                copiedControl.Location = copiedControl.Location.Add(new Point(10, 10));
                            }

                            parent.AddControl(copiedControl);

                            AddControlToList(copiedControl);
                            if (copiedControl is ContainerControl)
                            {
                                foreach (var control in (copiedControl as ContainerControl).PreOrderVisit())
                                {
                                    if (control is ScalableControl)
                                    {
                                        RegisterEvents(control);

                                        AddControlToList(control);
                                    }
                                }
                                controlComboBox.SelectedItem = copiedControl;
                            }

                            RegisterEvents(copiedControl);
                        }
                    }
                }
            }
        }