示例#1
0
文件: OptionPage.cs 项目: massreuy/3P
        private void GeneratePage()
        {
            var lastCategory   = "";
            var yPos           = 0;
            var configInstance = Config.Instance;

            ForEachConfigPropertyWithDisplayAttribute((property, attribute) => {
                var valObj = property.GetValue(configInstance);

                // new group
                if (!lastCategory.EqualsCi(attribute.GroupName))
                {
                    if (!string.IsNullOrEmpty(lastCategory))
                    {
                        // ReSharper disable once AccessToModifiedClosure
                        yPos += 10;
                    }
                    lastCategory = attribute.GroupName;
                    scrollPanel.ContentPanel.Controls.Add(new YamuiLabel {
                        AutoSize = true,
                        Function = FontFunction.Heading,
                        Location = new Point(0, yPos),
                        Text     = lastCategory.ToUpper()
                    });
                    yPos += 30;
                }

                // name of the field
                var label = new HtmlLabel {
                    AutoSizeHeightOnly = true,
                    BackColor          = Color.Transparent,
                    Location           = new Point(30, yPos),
                    Size = new Size(190, 10),
                    IsSelectionEnabled = false,
                    Text = attribute.Name
                };
                scrollPanel.ContentPanel.Controls.Add(label);

                var listRangeAttr = property.GetCustomAttributes(typeof(RangeAttribute), false);
                var rangeAttr     = (listRangeAttr.Any()) ? (RangeAttribute)listRangeAttr.FirstOrDefault() : null;

                if (valObj is string)
                {
                    // string
                    var strControl = new YamuiTextBox {
                        Location = new Point(240, yPos),
                        Text     = (string)property.GetValue(configInstance),
                        Size     = new Size(300, 20),
                        Tag      = property.Name
                    };

                    scrollPanel.ContentPanel.Controls.Add(strControl);
                    var strButton = new YamuiButtonImage {
                        Text          = @"save",
                        BackGrndImage = ImageResources.Save,
                        Size          = new Size(20, 20),
                        Location      = new Point(545, yPos),
                        Tag           = strControl,
                        TabStop       = false
                    };
                    strButton.ButtonPressed += SaveButtonOnButtonPressed;
                    scrollPanel.ContentPanel.Controls.Add(strButton);
                    tooltip.SetToolTip(strButton, "Click to <b>set the value</b> of this field<br>Otherwise, your modifications will not be saved");

                    var undoButton = new YamuiButtonImage {
                        BackGrndImage = ImageResources.UndoUserAction,
                        Size          = new Size(20, 20),
                        Location      = new Point(565, yPos),
                        Tag           = strControl,
                        TabStop       = false
                    };
                    undoButton.ButtonPressed += UndoButtonOnButtonPressed;
                    scrollPanel.ContentPanel.Controls.Add(undoButton);
                    tooltip.SetToolTip(undoButton, "Click to <b>reset this field</b> to its default value");

                    tooltip.SetToolTip(strControl, attribute.Description + "<br><div class='ToolTipBottomGoTo'>Click on the save button <img src='Save'> to set your modifications</div>");
                }
                if (valObj is int || valObj is double)
                {
                    // number
                    var numControl = new YamuiTextBox {
                        Location = new Point(240, yPos),
                        Text     = ((valObj is int) ? ((int)property.GetValue(configInstance)).ToString() : ((double)property.GetValue(configInstance)).ToString(CultureInfo.CurrentCulture)),
                        Size     = new Size(300, 20),
                        Tag      = property.Name
                    };

                    scrollPanel.ContentPanel.Controls.Add(numControl);
                    var numButton = new YamuiButtonImage {
                        Text          = @"save",
                        BackGrndImage = ImageResources.Save,
                        Size          = new Size(20, 20),
                        Location      = new Point(545, yPos),
                        Tag           = numControl,
                        TabStop       = false
                    };
                    numButton.ButtonPressed += SaveButtonOnButtonPressed;
                    scrollPanel.ContentPanel.Controls.Add(numButton);
                    tooltip.SetToolTip(numButton, "Click to <b>set the value</b> of this field<br>Otherwise, your modifications will not be saved");

                    var undoButton = new YamuiButtonImage {
                        BackGrndImage = ImageResources.UndoUserAction,
                        Size          = new Size(20, 20),
                        Location      = new Point(565, yPos),
                        Tag           = numControl,
                        TabStop       = false
                    };
                    undoButton.ButtonPressed += UndoButtonOnButtonPressed;
                    scrollPanel.ContentPanel.Controls.Add(undoButton);
                    tooltip.SetToolTip(undoButton, "Click to <b>reset this field</b> to its default value");

                    tooltip.SetToolTip(numControl, attribute.Description + "<br>" + (rangeAttr != null ? "<br><b><i>" + "Min value = " + rangeAttr.Minimum + "<br>Max value = " + rangeAttr.Maximum + "</i></b><br>" : "") + "<div class='ToolTipBottomGoTo'>Click on the save button <img src='Save'> to set your modifications</div>");
                }
                else if (valObj is bool)
                {
                    // bool
                    var toggleControl = new YamuiButtonToggle {
                        Location = new Point(240, yPos),
                        Size     = new Size(40, 16),
                        Text     = null,
                        Checked  = (bool)valObj,
                        Tag      = property.Name
                    };
                    toggleControl.ButtonPressed += ToggleControlOnCheckedChanged;
                    scrollPanel.ContentPanel.Controls.Add(toggleControl);

                    // tooltip
                    tooltip.SetToolTip(toggleControl, attribute.Description + "<br><div class='ToolTipBottomGoTo'>Click to <b>toggle on/off</b> this feature<br>Your choice is automatically applied</div>");
                }

                yPos += label.Height + 15;
            });

            yPos   += 15;
            _btSave = new YamuiButton {
                Location      = new Point(30, yPos),
                Size          = new Size(120, 24),
                Text          = @"Save everything",
                BackGrndImage = ImageResources.Save
            };
            _btSave.ButtonPressed += SaveAllButtonOnButtonPressed;
            tooltip.SetToolTip(_btSave, "Click to <b>save</b> all the options<br><i>This as the same effect than clicking save for each option</i>");
            scrollPanel.ContentPanel.Controls.Add(_btSave);

            var defaultButton = new YamuiButton {
                Location      = new Point(155, yPos),
                Size          = new Size(120, 24),
                Text          = @"Reset to default",
                BackGrndImage = ImageResources.UndoUserAction
            };

            defaultButton.ButtonPressed += DefaultButtonOnButtonPressed;
            tooltip.SetToolTip(defaultButton, "Click to <b>reset</b> all the options to default");
            scrollPanel.ContentPanel.Controls.Add(defaultButton);

            // add a button for the updates
            if (_allowedGroups.Contains("Updates"))
            {
                var updateButton = new YamuiButton {
                    Location      = new Point(280, yPos),
                    Size          = new Size(150, 24),
                    Text          = @"Check for updates",
                    BackGrndImage = ImageResources.Update
                };
                updateButton.ButtonPressed += (sender, args) => UpdateHandler.CheckForUpdate();
                tooltip.SetToolTip(updateButton, "Click to <b>check for updates</b>");
                scrollPanel.ContentPanel.Controls.Add(updateButton);
            }

            scrollPanel.ContentPanel.Height = yPos + 50;
        }
示例#2
0
        private Control InsertInputForItem(int i, ref int yPos)
        {
            var item     = _items[i];
            var itemType = GetItemType(item);

            // Get default text value
            object val;

            if (item == null)
            {
                val = DataObject;
            }
            else if (item is PropertyInfo)
            {
                val = ((PropertyInfo)item).GetValue(DataObject, null);
            }
            else
            {
                val = ((FieldInfo)item).GetValue(DataObject);
            }

            string strValue   = val.ConvertToStr();
            var    inputWidth = contentPanel.Width - _dataLabelWidth - InputPadding * 3;

            // Build control type
            Control retVal;

            if (itemType == typeof(bool))
            {
                retVal = new YamuiButtonToggle {
                    Location = new Point(_dataLabelWidth + InputPadding * 2, yPos),
                    Size     = new Size(40, 16),
                    Text     = null,
                    Checked  = (bool)val
                };

                // for enum or list of strings
            }
            else if (itemType.IsEnum || (itemType == typeof(string) && GetAttr(item) != null && GetAttr(item).AllowListedValuesOnly))
            {
                var cb = new YamuiComboBox {
                    Location = new Point(_dataLabelWidth + InputPadding * 2, yPos),
                    Size     = new Size(inputWidth, 20),
                    Anchor   = Anchor | AnchorStyles.Right
                };
                var dataSource = new List <string>();
                if (itemType.IsEnum)
                {
                    foreach (var name in Enum.GetNames(itemType))
                    {
                        var attribute = Attribute.GetCustomAttribute(itemType.GetField(name), typeof(DescriptionAttribute), true) as DescriptionAttribute;
                        dataSource.Add(attribute != null ? attribute.Description : name);
                    }
                }
                else
                {
                    dataSource = strValue.Split('|').ToList();
                    strValue   = dataSource[0];
                }
                cb.DataSource = dataSource;
                cb.Text       = strValue;
                retVal        = cb;

                // for everything else
            }
            else
            {
                var tb = new YamuiTextBox {
                    Location  = new Point(_dataLabelWidth + InputPadding * 2, yPos),
                    Size      = new Size(inputWidth, 20),
                    Text      = strValue,
                    Anchor    = Anchor | AnchorStyles.Right,
                    Multiline = false
                };
                tb.AcceptsTab       = false;
                tb.CausesValidation = true;
                tb.Enter           += (s, e) => tb.SelectAll();

                if (itemType == typeof(char))
                {
                    tb.KeyPress += (s, e) => e.Handled = !char.IsControl(e.KeyChar) && tb.TextLength > 0;
                }
                else
                {
                    tb.KeyPress += (s, e) => e.Handled = Utilities.IsInvalidKey(e.KeyChar, itemType);
                }

                tb.Validating += (s, e) => {
                    bool invalid = IsTextInvalid(tb, itemType);
                    e.Cancel = invalid;
                    _errorProvider.SetError(tb, invalid ? "The value has an invalid format for <" + itemType.Name + ">." : "");
                };

                tb.Validated += (s, e) => _errorProvider.SetError(tb, "");

                _errorProvider.SetIconPadding(tb, -18);
                _errorProvider.Icon = Resources.Resources.IcoError;
                retVal = tb;
            }

            // Set standard props
            retVal.Name = "input" + i;

            // add tooltip on the control
            if (_items[i] != null && !string.IsNullOrEmpty(GetAttr(_items[i]).Tooltip))
            {
                _tooltip.SetToolTip(retVal, GetAttr(_items[i]).Tooltip);
            }

            return(retVal);
        }
示例#3
0
        /// <summary>
        /// Insert the correct input for the option
        /// </summary>
        private Control InsertInputForItem(FieldInfo property, Config.ConfigAttribute attr, ref int yPos)
        {
            // Build control type
            Control retVal;

            if (property.FieldType == typeof(bool))
            {
                // for bool
                var tg = new YamuiButtonToggle {
                    Location = new Point(320, yPos),
                    Size     = new Size(40, 16),
                    Text     = null,
                    Checked  = (bool)property.GetValue(Config.Instance)
                };
                tg.ButtonPressed += (sender, args) => OnFieldModified();
                retVal            = tg;
            }
            else if (property.FieldType.IsEnum)
            {
                // for enum
                var dataSource = new List <string>();
                foreach (var name in Enum.GetNames(property.FieldType))
                {
                    var attribute = Attribute.GetCustomAttribute(property.FieldType.GetField(name), typeof(DescriptionAttribute), true) as DescriptionAttribute;
                    dataSource.Add(attribute != null ? attribute.Description : name);
                }
                dataSource = dataSource.Select(s => s.Replace("_", " ").Trim()).ToNonNullList();
                var cb = new YamuiComboBox {
                    Location   = new Point(320, yPos),
                    Size       = new Size(Math.Min(300, dataSource.Select(s => TextRenderer.MeasureText(s, FontManager.GetStandardFont()).Width).Max() + 25), 20),
                    DataSource = dataSource,
                };
                cb.SelectedIndex = Enum.GetNames(property.FieldType).IndexOf(property.GetValue(Config.Instance).ConvertToStr());
                cb.SelectedIndexChangedByUser += box => OnFieldModified();
                retVal = cb;
            }
            else
            {
                // for everything else
                var tb = new YamuiTextBox {
                    Location         = new Point(320, yPos),
                    Size             = new Size(300, 20),
                    Text             = property.GetValue(Config.Instance).ConvertToStr(),
                    Multiline        = false,
                    CausesValidation = true
                };
                tb.Enter += (s, e) => tb.SelectAll();
                if (property.FieldType == typeof(char))
                {
                    tb.KeyPress += (s, e) => {
                        e.Handled = !char.IsControl(e.KeyChar) && tb.TextLength > 0;
                    }
                }
                ;
                else
                {
                    tb.KeyPress += (s, e) => {
                        e.Handled = Utilities.IsInvalidKey(e.KeyChar, property.FieldType);
                    }
                };
                tb.Validating  += ValidateTextBox;
                tb.Validated   += (s, e) => errorProvider.SetError(tb, "");
                tb.TextChanged += (sender, args) => OnFieldModified();
                errorProvider.SetIconPadding(tb, -18);
                errorProvider.Icon = ImageResources.IcoError;
                retVal             = tb;
            }

            var undoButton = new YamuiButtonImage {
                BackGrndImage = ImageResources.UndoUserAction,
                Size          = new Size(20, 20),
                Location      = new Point(retVal.Left + retVal.Width + 5, yPos),
                Tag           = retVal,
                TabStop       = false
            };

            undoButton.ButtonPressed += OnUndoButton;
            scrollPanel.ContentPanel.Controls.Add(undoButton);
            tooltip.SetToolTip(undoButton, "Click to <b>reset this field</b> to its default value");

            // add tooltip on the control
            if (!string.IsNullOrEmpty(attr.Tooltip))
            {
                var rangeAttr = (RangeAttribute)property.GetCustomAttributes(typeof(RangeAttribute), false).FirstOrDefault();
                tooltip.SetToolTip(retVal, "<b>" + attr.Label + ":</b><br><br>" + attr.Tooltip + (rangeAttr != null ? "<br><b><i>" + "Min value = " + rangeAttr.Minimum + "<br>Max value = " + rangeAttr.Maximum + "</i></b>" : ""));
            }

            // Set standard props
            retVal.Name = "option_" + property.Name;
            retVal.Tag  = property;

            return(retVal);
        }