示例#1
0
        private void editPath()
        {
            var path = (HCPath) lstPaths.SelectedItem;
            if (path == null)
                return;

            using (var dlg = new ManagedForm(_settings.EditDialogSettings)
            {
                Text = "Edit path",
                FormBorderStyle = FormBorderStyle.FixedDialog,
                AutoSize = true,
                AutoSizeMode = AutoSizeMode.GrowAndShrink,
                MinimizeBox = false,
                MaximizeBox = false
            })
            {
                var layout = new TableLayoutPanel { Dock = DockStyle.Fill, AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink };

                layout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
                layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 1));

                int row = 0;

                var addControl = Ut.Lambda((string label, Control control) =>
                {
                    layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                    var lbl = new Label { Text = label, AutoSize = true, Anchor = AnchorStyles.Right };
                    layout.Controls.Add(lbl, 0, row);
                    control.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
                    control.Tag = lbl;
                    layout.Controls.Add(control, 1, row);
                    row++;
                    return control;
                });

                var addCheckbox = Ut.Lambda((string label) =>
                {
                    layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                    var chk = new CheckBox { Text = label, Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top, AutoSize = true };
                    layout.Controls.Add(chk, 1, row);
                    row++;
                    return chk;
                });

                var txtName = addControl("&Name:", new TextBox());
                var layoutColor = (TableLayoutPanel) addControl("Colo&r:", new TableLayoutPanel { AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink });
                var chkDrawStart = addCheckbox("Draw &start");
                var chkDrawEnd = addCheckbox("Draw &end");
                var ddStartDir = (ComboBox) addControl("IP start &direction:", new ComboBox { DropDownStyle = ComboBoxStyle.DropDownList });
                var chkMaxInstr = addCheckbox("&Limit number of instructions");
                var txtMaxInstr = (NumericUpDown) addControl("&Max instructions:", new NumericUpDown { Minimum = 1, DecimalPlaces = 0 });
                var ddBranch = (ComboBox) addControl("Take &branches:", new ComboBox { DropDownStyle = ComboBoxStyle.DropDownList });
                var chkStop = addCheckbox("S&top at [, ], #");

                // Color
                layoutColor.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 1));
                layoutColor.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
                layoutColor.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));

                var pnlColor = new Panel
                {
                    AutoSize = true,
                    AutoSizeMode = AutoSizeMode.GrowAndShrink,
                    Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right,
                    MinimumSize = new Size(25, 10)
                };
                var ddColor = new ComboBox { Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top, Tag = path.Color, DropDownStyle = ComboBoxStyle.DropDownList };
                foreach (var kvp in PredefinedColors)
                    ddColor.Items.Add(kvp.Key);
                ddColor.Items.Add("Custom");

                var btnColor = new Button { Text = "...", Width = 30 };
                layoutColor.Controls.Add(pnlColor, 0, 0);
                layoutColor.Controls.Add(ddColor, 1, 0);
                layoutColor.Controls.Add(btnColor, 2, 0);

                // OK/Cancel buttons
                var flowLayout = new FlowLayoutPanel { Dock = DockStyle.Fill, FlowDirection = FlowDirection.RightToLeft, AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink };
                var btnOk = new Button { Text = "&OK", DialogResult = DialogResult.OK };
                var btnCancel = new Button { Text = "&Cancel", DialogResult = DialogResult.Cancel };
                flowLayout.Controls.Add(btnCancel);
                flowLayout.Controls.Add(btnOk);
                layout.Controls.Add(flowLayout, 0, row);
                layout.SetColumnSpan(flowLayout, 2);
                dlg.AcceptButton = btnOk;
                dlg.CancelButton = btnCancel;
                row++;

                // “IP start direction” drop-down box
                ddStartDir.Items.Add(Direction.NorthEast);
                ddStartDir.Items.Add(Direction.East);
                ddStartDir.Items.Add(Direction.SouthEast);
                ddStartDir.Items.Add(Direction.SouthWest);
                ddStartDir.Items.Add(Direction.West);
                ddStartDir.Items.Add(Direction.NorthWest);

                // “Take branches” drop-down box
                var ddBranchMap = new[] { Tuple.Create((bool?) null, "Don’t"), Tuple.Create((bool?) true, "True"), Tuple.Create((bool?) false, "False") };
                foreach (var tup in ddBranchMap)
                    ddBranch.Items.Add(tup.Item2);

                // Populate all controls with the data
                var populateColor = Ut.Lambda((Color color) =>
                {
                    pnlColor.BackColor = path.Color;
                    var colorKey = PredefinedColors.FirstOrDefault(kvp => kvp.Value.R == path.Color.R && kvp.Value.G == path.Color.G && kvp.Value.B == path.Color.B).Key;
                    if (colorKey == null)
                    {
                        ddColor.SelectedItem = "Custom";
                        ddColor.Tag = path.Color;
                        btnColor.Visible = true;
                    }
                    else
                    {
                        ddColor.SelectedItem = colorKey;
                        btnColor.Visible = false;
                    }
                });

                txtName.Text = path.Name;
                populateColor(path.Color);
                chkDrawStart.Checked = path.DrawStart;
                chkDrawEnd.Checked = path.DrawEnd;
                ddStartDir.SelectedItem = path.IpStartDirection;
                chkMaxInstr.Checked = path.NumInstructions.HasValue;
                txtMaxInstr.Value = path.NumInstructions ?? 10;
                ddBranch.SelectedItem = ddBranchMap.First(tup => object.Equals(path.TakeBranch, tup.Item1)).Item2;
                chkStop.Checked = path.StopAtIpChanger;

                var updateDlgUi = Ut.Lambda(() =>
                {
                    txtMaxInstr.Visible = chkMaxInstr.Checked;
                    ((Control) txtMaxInstr.Tag).Visible = chkMaxInstr.Checked;
                });
                updateDlgUi();

                // Set events
                chkMaxInstr.Click += delegate { updateDlgUi(); };
                btnColor.Click += delegate
                {
                    using (var colorDlg = new ColorDialog { Color = pnlColor.BackColor, FullOpen = true, AnyColor = true })
                    {
                        if (_settings.CustomColorsData != null)
                            colorDlg.CustomColors = _settings.CustomColorsData;
                        if (colorDlg.ShowDialog() == DialogResult.OK)
                        {
                            _settings.CustomColorsData = colorDlg.CustomColors;
                            populateColor(colorDlg.Color);
                        }
                    }
                };

                ddColor.SelectedIndexChanged += delegate
                {
                    if (ddColor.SelectedItem.Equals("Custom"))
                    {
                        pnlColor.BackColor = (Color) ddColor.Tag;
                        btnColor.Visible = true;
                    }
                    else
                    {
                        pnlColor.BackColor = PredefinedColors[(string) ddColor.SelectedItem];
                        btnColor.Visible = false;
                    }
                };

                btnOk.Click += delegate
                {
                    // Put all the values from the controls back into the object
                    path.Name = txtName.Text;
                    path.Color = pnlColor.BackColor;
                    path.DrawStart = chkDrawStart.Checked;
                    path.DrawEnd = chkDrawEnd.Checked;
                    path.IpStartDirection = (Direction) ddStartDir.SelectedItem;
                    path.NumInstructions = chkMaxInstr.Checked ? (int) txtMaxInstr.Value : (int?) null;
                    path.TakeBranch = ddBranchMap.First(tup => tup.Item2 == (string) ddBranch.SelectedItem).Item1;
                    path.StopAtIpChanger = chkStop.Checked;
                    rerender();
                    updateList();
                    _anyChanges = true;
                };

                dlg.Controls.Add(layout);
                dlg.ShowDialog();
            }
        }
示例#2
0
        private void editPath()
        {
            var path = (HCPath)lstPaths.SelectedItem;

            if (path == null)
            {
                return;
            }

            using (var dlg = new ManagedForm(_settings.EditDialogSettings)
            {
                Text = "Edit path",
                FormBorderStyle = FormBorderStyle.FixedDialog,
                AutoSize = true,
                AutoSizeMode = AutoSizeMode.GrowAndShrink,
                MinimizeBox = false,
                MaximizeBox = false
            }) {
                var layout = new TableLayoutPanel {
                    Dock         = DockStyle.Fill,
                    AutoSize     = true,
                    AutoSizeMode = AutoSizeMode.GrowAndShrink
                };

                layout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
                layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 1));

                int row = 0;

                var addControl = Ut.Lambda((string label, Control control) => {
                    layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                    var lbl = new Label {
                        Text     = label,
                        AutoSize = true,
                        Anchor   = AnchorStyles.Right
                    };
                    layout.Controls.Add(lbl, 0, row);
                    control.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
                    control.Tag    = lbl;
                    layout.Controls.Add(control, 1, row);
                    row++;
                    return(control);
                });

                var addCheckbox = Ut.Lambda((string label) => {
                    layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                    var chk = new CheckBox {
                        Text     = label,
                        Anchor   = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top,
                        AutoSize = true
                    };
                    layout.Controls.Add(chk, 1, row);
                    row++;
                    return(chk);
                });

                var txtName     = addControl("&Name:", new TextBox());
                var layoutColor = (TableLayoutPanel)addControl("Colo&r:", new TableLayoutPanel {
                    AutoSize     = true,
                    AutoSizeMode = AutoSizeMode.GrowAndShrink
                });
                var chkDrawStart = addCheckbox("Draw &start");
                var chkDrawEnd   = addCheckbox("Draw &end");
                var ddStartDir   = (ComboBox)addControl("IP start &direction:", new ComboBox {
                    DropDownStyle = ComboBoxStyle.DropDownList
                });
                var chkMaxInstr = addCheckbox("&Limit number of instructions");
                var txtMaxInstr = (NumericUpDown)addControl("&Max instructions:", new NumericUpDown {
                    Minimum       = 1,
                    Maximum       = decimal.MaxValue,
                    DecimalPlaces = 0
                });
                var ddBranch = (ComboBox)addControl("Take &branches:", new ComboBox {
                    DropDownStyle = ComboBoxStyle.DropDownList
                });
                var chkStop = addCheckbox("S&top at [, ], #");

                // Color
                layoutColor.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 1));
                layoutColor.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
                layoutColor.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));

                var pnlColor = new Panel {
                    AutoSize     = true,
                    AutoSizeMode = AutoSizeMode.GrowAndShrink,
                    Anchor       = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right,
                    MinimumSize  = new Size(25, 10)
                };
                var ddColor = new ComboBox {
                    Anchor        = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top,
                    Tag           = path.Color,
                    DropDownStyle = ComboBoxStyle.DropDownList
                };
                foreach (var kvp in PredefinedColors)
                {
                    ddColor.Items.Add(kvp.Key);
                }
                ddColor.Items.Add("Custom");

                var btnColor = new Button {
                    Text = "...", Width = 30
                };
                layoutColor.Controls.Add(pnlColor, 0, 0);
                layoutColor.Controls.Add(ddColor, 1, 0);
                layoutColor.Controls.Add(btnColor, 2, 0);

                // OK/Cancel buttons
                var flowLayout = new FlowLayoutPanel {
                    Dock          = DockStyle.Fill,
                    FlowDirection = FlowDirection.RightToLeft,
                    AutoSize      = true,
                    AutoSizeMode  = AutoSizeMode.GrowAndShrink
                };
                var btnOk = new Button {
                    Text = "&OK", DialogResult = DialogResult.OK
                };
                var btnCancel = new Button {
                    Text         = "&Cancel",
                    DialogResult = DialogResult.Cancel
                };
                flowLayout.Controls.Add(btnCancel);
                flowLayout.Controls.Add(btnOk);
                layout.Controls.Add(flowLayout, 0, row);
                layout.SetColumnSpan(flowLayout, 2);
                dlg.AcceptButton = btnOk;
                dlg.CancelButton = btnCancel;
                row++;

                // “IP start direction” drop-down box
                ddStartDir.Items.Add(Direction.NorthEast);
                ddStartDir.Items.Add(Direction.East);
                ddStartDir.Items.Add(Direction.SouthEast);
                ddStartDir.Items.Add(Direction.SouthWest);
                ddStartDir.Items.Add(Direction.West);
                ddStartDir.Items.Add(Direction.NorthWest);

                // “Take branches” drop-down box
                var ddBranchMap = new[] {
                    Tuple.Create((bool?)null, "Don’t"),
                    Tuple.Create((bool?)true, "True"),
                    Tuple.Create((bool?)false, "False")
                };
                foreach (var tup in ddBranchMap)
                {
                    ddBranch.Items.Add(tup.Item2);
                }

                // Populate all controls with the data
                var populateColor = Ut.Lambda((Color color) => {
                    pnlColor.BackColor = path.Color;
                    var colorKey       = PredefinedColors.FirstOrDefault(kvp => kvp.Value.R == path.Color.R && kvp.Value.G == path.Color.G && kvp.Value.B == path.Color.B).Key;
                    if (colorKey == null)
                    {
                        ddColor.SelectedItem = "Custom";
                        ddColor.Tag          = path.Color;
                        btnColor.Visible     = true;
                    }
                    else
                    {
                        ddColor.SelectedItem = colorKey;
                        btnColor.Visible     = false;
                    }
                });

                txtName.Text = path.Name;
                populateColor(path.Color);
                chkDrawStart.Checked    = path.DrawStart;
                chkDrawEnd.Checked      = path.DrawEnd;
                ddStartDir.SelectedItem = path.IpStartDirection;
                chkMaxInstr.Checked     = path.NumInstructions.HasValue;
                txtMaxInstr.Value       = path.NumInstructions ?? 10;
                ddBranch.SelectedItem   = ddBranchMap.First(tup => object.Equals(path.TakeBranch, tup.Item1)).Item2;
                chkStop.Checked         = path.StopAtIpChanger;

                var updateDlgUi = Ut.Lambda(() => {
                    txtMaxInstr.Visible = chkMaxInstr.Checked;
                    ((Control)txtMaxInstr.Tag).Visible = chkMaxInstr.Checked;
                });
                updateDlgUi();

                // Set events
                chkMaxInstr.Click += delegate {
                    updateDlgUi();
                };
                btnColor.Click += delegate {
                    using (var colorDlg = new ColorDialog {
                        Color = pnlColor.BackColor,
                        FullOpen = true,
                        AnyColor = true
                    }) {
                        if (_settings.CustomColorsData != null)
                        {
                            colorDlg.CustomColors = _settings.CustomColorsData;
                        }
                        if (colorDlg.ShowDialog() == DialogResult.OK)
                        {
                            _settings.CustomColorsData = colorDlg.CustomColors;
                            populateColor(colorDlg.Color);
                        }
                    }
                };

                ddColor.SelectedIndexChanged += delegate {
                    if (ddColor.SelectedItem.Equals("Custom"))
                    {
                        pnlColor.BackColor = (Color)ddColor.Tag;
                        btnColor.Visible   = true;
                    }
                    else
                    {
                        pnlColor.BackColor = PredefinedColors[(string)ddColor.SelectedItem];
                        btnColor.Visible   = false;
                    }
                };

                btnOk.Click += delegate {
                    // Put all the values from the controls back into the object
                    path.Name             = txtName.Text;
                    path.Color            = pnlColor.BackColor;
                    path.DrawStart        = chkDrawStart.Checked;
                    path.DrawEnd          = chkDrawEnd.Checked;
                    path.IpStartDirection = (Direction)ddStartDir.SelectedItem;
                    path.NumInstructions  = chkMaxInstr.Checked ? (int)txtMaxInstr.Value : (int?)null;
                    path.TakeBranch       = ddBranchMap.First(tup => tup.Item2 == (string)ddBranch.SelectedItem).Item1;
                    path.StopAtIpChanger  = chkStop.Checked;
                    rerender();
                    updateList();
                    _anyChanges = true;
                };

                dlg.Controls.Add(layout);
                dlg.ShowDialog();
            }
        }