Esempio n. 1
0
        private void TreeView_Click(object sender, EventArgs e)
        {
            if (!(e is MouseEventArgs m) || m.Button != MouseButtons.Right)
            {
                return;
            }

            NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));

            treeView.SelectedNode = info.Node;
            if (info.Node != null)
            {
                if (info.Node.Tag is SensorNode node && node.Sensor != null)
                {
                    treeContextMenu.MenuItems.Clear();
                    if (node.Sensor.Parameters.Count > 0)
                    {
                        MenuItem item = new MenuItem("Parameters...");
                        item.Click += delegate
                        {
                            ShowParameterForm(node.Sensor);
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }
                    if (nodeTextBoxText.EditEnabled)
                    {
                        MenuItem item = new MenuItem("Rename");
                        item.Click += delegate
                        {
                            nodeTextBoxText.BeginEdit();
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }
                    if (node.IsVisible)
                    {
                        MenuItem item = new MenuItem("Hide");
                        item.Click += delegate
                        {
                            node.IsVisible = false;
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }
                    else
                    {
                        MenuItem item = new MenuItem("Unhide");
                        item.Click += delegate
                        {
                            node.IsVisible = true;
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }
                    treeContextMenu.MenuItems.Add(new MenuItem("-"));
                    {
                        MenuItem item = new MenuItem("Pen Color...");
                        item.Click += delegate
                        {
                            ColorDialog dialog = new ColorDialog {
                                Color = node.PenColor.GetValueOrDefault()
                            };
                            if (dialog.ShowDialog() == DialogResult.OK)
                            {
                                node.PenColor = dialog.Color;
                            }
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }
                    {
                        MenuItem item = new MenuItem("Reset Pen Color");
                        item.Click += delegate
                        {
                            node.PenColor = null;
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }
                    treeContextMenu.MenuItems.Add(new MenuItem("-"));
                    {
                        MenuItem item = new MenuItem("Show in Tray")
                        {
                            Checked = _systemTray.Contains(node.Sensor)
                        };
                        item.Click += delegate
                        {
                            if (item.Checked)
                            {
                                _systemTray.Remove(node.Sensor);
                            }
                            else
                            {
                                _systemTray.Add(node.Sensor, true);
                            }
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }
                    if (_gadget != null)
                    {
                        MenuItem item = new MenuItem("Show in Gadget")
                        {
                            Checked = _gadget.Contains(node.Sensor)
                        };
                        item.Click += delegate
                        {
                            if (item.Checked)
                            {
                                _gadget.Remove(node.Sensor);
                            }
                            else
                            {
                                _gadget.Add(node.Sensor);
                            }
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }
                    if (node.Sensor.Control != null)
                    {
                        treeContextMenu.MenuItems.Add(new MenuItem("-"));
                        IControl control     = node.Sensor.Control;
                        MenuItem controlItem = new MenuItem("Control");
                        MenuItem defaultItem = new MenuItem("Default")
                        {
                            Checked = control.ControlMode == ControlMode.Default
                        };
                        controlItem.MenuItems.Add(defaultItem);
                        defaultItem.Click += delegate
                        {
                            control.SetDefault();
                        };
                        MenuItem manualItem = new MenuItem("Manual");
                        controlItem.MenuItems.Add(manualItem);
                        manualItem.Checked = control.ControlMode == ControlMode.Software;
                        for (int i = 0; i <= 100; i += 5)
                        {
                            if (i <= control.MaxSoftwareValue &&
                                i >= control.MinSoftwareValue)
                            {
                                MenuItem item = new MenuItem(i + " %")
                                {
                                    RadioCheck = true
                                };
                                manualItem.MenuItems.Add(item);
                                item.Checked = control.ControlMode == ControlMode.Software && Math.Round(control.SoftwareValue) == i;
                                int softwareValue = i;
                                item.Click += delegate
                                {
                                    control.SetSoftware(softwareValue);
                                };
                            }
                        }
                        treeContextMenu.MenuItems.Add(controlItem);
                    }

                    treeContextMenu.Show(treeView, new Point(m.X, m.Y));
                }

                if (info.Node.Tag is HardwareNode hardwareNode && hardwareNode.Hardware != null)
                {
                    treeContextMenu.MenuItems.Clear();

                    if (nodeTextBoxText.EditEnabled)
                    {
                        MenuItem item = new MenuItem("Rename");
                        item.Click += delegate
                        {
                            nodeTextBoxText.BeginEdit();
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }

                    treeContextMenu.Show(treeView, new Point(m.X, m.Y));
                }
            }
        }
Esempio n. 2
0
        private void TreeView_Click(object sender, EventArgs e)
        {
            if (!(e is MouseEventArgs m) || (m.Button != MouseButtons.Left && m.Button != MouseButtons.Right))
            {
                return;
            }

            NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));

            if (m.Button == MouseButtons.Left && info.Node != null)
            {
                if (info.Node.Tag is IExpandPersistNode expandPersistNode)
                {
                    expandPersistNode.Expanded = info.Node.IsExpanded;
                }

                return;
            }

            treeView.SelectedNode = info.Node;
            if (info.Node != null)
            {
                if (info.Node.Tag is SensorNode node && node.Sensor != null)
                {
                    treeContextMenu.MenuItems.Clear();
                    if (node.Sensor.Parameters.Count > 0)
                    {
                        MenuItem item = new MenuItem("Parameters...");
                        item.Click += delegate
                        {
                            ShowParameterForm(node.Sensor);
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }
                    if (nodeTextBoxText.EditEnabled)
                    {
                        MenuItem item = new MenuItem("Rename");
                        item.Click += delegate
                        {
                            nodeTextBoxText.BeginEdit();
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }
                    if (node.IsVisible)
                    {
                        MenuItem item = new MenuItem("Hide");
                        item.Click += delegate
                        {
                            node.IsVisible = false;
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }
                    else
                    {
                        MenuItem item = new MenuItem("Unhide");
                        item.Click += delegate
                        {
                            node.IsVisible = true;
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }
                    treeContextMenu.MenuItems.Add(new MenuItem("-"));
                    {
                        MenuItem item = new MenuItem("Pen Color...");
                        item.Click += delegate
                        {
                            ColorDialog dialog = new ColorDialog {
                                Color = node.PenColor.GetValueOrDefault()
                            };
                            if (dialog.ShowDialog() == DialogResult.OK)
                            {
                                node.PenColor = dialog.Color;
                            }
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }
                    {
                        MenuItem item = new MenuItem("Reset Pen Color");
                        item.Click += delegate
                        {
                            node.PenColor = null;
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }
                    treeContextMenu.MenuItems.Add(new MenuItem("-"));
                    {
                        MenuItem item = new MenuItem("Show in Tray")
                        {
                            Checked = _systemTray.Contains(node.Sensor)
                        };
                        item.Click += delegate
                        {
                            if (item.Checked)
                            {
                                _systemTray.Remove(node.Sensor);
                            }
                            else
                            {
                                _systemTray.Add(node.Sensor, true);
                            }
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }
                    if (_gadget != null)
                    {
                        MenuItem item = new MenuItem("Show in Gadget")
                        {
                            Checked = _gadget.Contains(node.Sensor)
                        };
                        item.Click += delegate
                        {
                            if (item.Checked)
                            {
                                _gadget.Remove(node.Sensor);
                            }
                            else
                            {
                                _gadget.Add(node.Sensor);
                            }
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }
                    if (node.Sensor.Control != null)
                    {
                        treeContextMenu.MenuItems.Add(new MenuItem("-"));
                        IControl control     = node.Sensor.Control;
                        MenuItem controlItem = new MenuItem("Control");
                        MenuItem defaultItem = new MenuItem("Default")
                        {
                            Checked = control.ActualControlMode == ControlMode.Default
                        };
                        controlItem.MenuItems.Add(defaultItem);
                        defaultItem.Click += delegate
                        {
                            control.SetDefault();
                        };
                        MenuItem manualItem = new MenuItem("Value");
                        controlItem.MenuItems.Add(manualItem);
                        manualItem.Checked = control.ActualControlMode == ControlMode.Software;
                        for (int i = 0; i <= 100; i += 5)
                        {
                            if (i <= control.MaxSoftwareValue &&
                                i >= control.MinSoftwareValue)
                            {
                                MenuItem item = new MenuItem(i + " %")
                                {
                                    RadioCheck = true
                                };
                                manualItem.MenuItems.Add(item);
                                item.Checked = control.ActualControlMode == ControlMode.Software && Math.Round(control.SoftwareValue) == i;
                                int softwareValue = i;
                                item.Click += delegate
                                {
                                    control.SetSoftware(softwareValue);
                                };
                            }
                        }
                        MenuItem curveItem = new MenuItem("Curve");
                        controlItem.MenuItems.Add(curveItem);
                        curveItem.Checked = control.ActualControlMode == ControlMode.SoftwareCurve;
                        MenuItem newCurveItem = new MenuItem("New");
                        newCurveItem.Click += delegate(object obj, EventArgs args) {
                            var confirmSensorselect = MessageBox.Show("Select the other sensor after clicking OK.", "New Manual Curve", MessageBoxButtons.OKCancel);
                            if (confirmSensorselect == DialogResult.OK)
                            {
                                // Listen for user click on sensor
                                EventHandler selectorListener = null;
                                selectorListener = (curveselect_sender, curveselect_e) => {
                                    MouseEventArgs curveselect_m = curveselect_e as MouseEventArgs;
                                    if (curveselect_m == null || curveselect_m.Button != MouseButtons.Left)
                                    {
                                        this.treeView.Click -= selectorListener;
                                        return;
                                    }
                                    // Try find sensor user clicked
                                    NodeControlInfo curveselect_info = treeView.GetNodeControlInfoAt(new Point(curveselect_m.X, curveselect_m.Y));
                                    if (curveselect_info.Node != null)
                                    {
                                        SensorNode curveselect_node = curveselect_info.Node.Tag as SensorNode;
                                        if (curveselect_node != null && curveselect_node.Sensor != null)
                                        {
                                            new SensorControlForm(node.Sensor, curveselect_node.Sensor, null).ShowDialog();
                                            return;
                                        }
                                    }

                                    var tryagainSensorselect = MessageBox.Show("Could not find sensor. Try again?", "Manual Curve", MessageBoxButtons.RetryCancel);
                                    if (tryagainSensorselect != DialogResult.Retry)
                                    {
                                        this.treeView.Click -= selectorListener;
                                    }
                                };
                                treeView.Click += selectorListener;
                            }
                        };
                        curveItem.MenuItems.Add(newCurveItem);
                        var softwareCurve = node.Sensor.Control.GetSoftwareCurve();
                        if (softwareCurve != null)
                        {
                            // edit curve
                            MenuItem editCurveItem = new MenuItem("Edit");
                            curveItem.MenuItems.Add(editCurveItem);
                            editCurveItem.Click += delegate(object obj, EventArgs args)
                            {
                                new SensorControlForm(node.Sensor, softwareCurve.Sensor, softwareCurve.Points).ShowDialog();
                            };

                            if (control.ActualControlMode != ControlMode.SoftwareCurve)
                            {
                                // enable curve
                                MenuItem enableCurveItem = new MenuItem("Enable");
                                curveItem.MenuItems.Add(enableCurveItem);
                                enableCurveItem.Click += delegate
                                {
                                    node.Sensor.Control.SetSoftwareCurve(softwareCurve.Points, softwareCurve.Sensor);
                                };
                            }
                        }
                        treeContextMenu.MenuItems.Add(controlItem);
                    }

                    treeContextMenu.Show(treeView, new Point(m.X, m.Y));
                }

                if (info.Node.Tag is HardwareNode hardwareNode && hardwareNode.Hardware != null)
                {
                    treeContextMenu.MenuItems.Clear();

                    if (nodeTextBoxText.EditEnabled)
                    {
                        MenuItem item = new MenuItem("Rename");
                        item.Click += delegate
                        {
                            nodeTextBoxText.BeginEdit();
                        };
                        treeContextMenu.MenuItems.Add(item);
                    }

                    treeContextMenu.Show(treeView, new Point(m.X, m.Y));
                }
            }
        }