Ejemplo n.º 1
0
 public BaseBlock(double sizeUnit)
 {
     Color    = ColorRandomizer.GetRandomColor();
     SizeUnit = sizeUnit;
 }
        public new void Refresh()
        {
            if (Properties != null)
            {
                SuspendLayout();
                // dispose existing controls

                // create controls for categories and items
                categoryLabels     = new List <Control>();
                categorySeperators = new List <Control>();
                itemLabels         = new List <Control>();
                itemEdits          = new List <Control>();

                var categoryOffsetX = Options.CategoryLeftMargin;

                var seperatorOffsetX = Options.SeperatorOffset;
                if (Options.SeperatorOffsetType == SeperatorOffsetType.Percent)
                {
                    seperatorOffsetX = MathHelper.RoundInt((double)Width * (double)seperatorOffsetX / (double)100);
                }

                var offsetY = 0;

                var categoryNames = Properties.Select(x => x.CategoryName).Distinct();
                foreach (var categoryName in categoryNames)
                {
                    var items = Properties.Where(d => d.CategoryName == categoryName && d.Browsable).ToList();

                    if (Options.ViewMode == ViewMode.CategoriesAndItems && items.Count > 0)
                    {
                        offsetY += Options.CategoryTopMargin;

                        // category title
                        var labelCategoryTitle = new Label()
                        {
                            Text     = Properties.Where(x => x.CategoryName == categoryName).First().CategoryDisplayName,
                            Font     = Options.CategoryTitleFont,
                            AutoSize = true,
                            Location = new Point(categoryOffsetX, offsetY),
                            Size     = new Size(Width - Options.CategoryLeftMargin - Options.CategoryRightMargin, Options.CategoryTitleHeight),
#if COLORIFY
                            BackColor = Color.Red
#endif
                        };
                        Controls.Add(labelCategoryTitle);
                        categoryLabels.Add(labelCategoryTitle);

                        offsetY += labelCategoryTitle.Size.Height;

                        // category seperator
                        var categorySeperatorWidth = Width - Options.CategoryLeftMargin - Options.CategoryRightMargin;
                        //if (VerticalScroll.Visible)
                        //    seperatorWidth -= SystemInformation.VerticalScrollBarWidth + SCROLLBAR_PADDING;

                        var panelCategorySeperator = new Panel
                        {
                            BackColor = Options.CategorySeperatorColor,
                            Location  = new Point(categoryOffsetX, offsetY),
                            Size      = new Size(categorySeperatorWidth, Options.CategorySeperatorHeight)
                        };
                        Controls.Add(panelCategorySeperator);
                        categorySeperators.Add(panelCategorySeperator);

                        offsetY += Options.CategoryBottomMargin;
                    }

                    // items
                    foreach (var item in items)
                    {
                        // label
                        var labelPropertyTitle = new Label()
                        {
                            Text         = item.DisplayName,
                            AutoSize     = true,
                            AutoEllipsis = true,
                            Location     = new Point(Options.ItemLeftMargin, offsetY),
                            Size         = new Size(seperatorOffsetX - Options.ItemLeftMargin - Options.SeperatorPadding, Options.ItemHeight),

#if COLORIFY
                            BackColor = ColorRandomizer.GetRandomColor()
#endif
                        };
                        Controls.Add(labelPropertyTitle);
                        itemLabels.Add(labelPropertyTitle);

                        // editor
                        var itemEditWidth = Width - seperatorOffsetX - Options.ItemRightMargin;
                        //if (VerticalScroll.Visible)
                        //    itemEditWidth -= SystemInformation.VerticalScrollBarWidth + SCROLLBAR_PADDING;

                        if (item.Type == GenericPropertyType.String)
                        {
                            var textBox = new TextBox()
                            {
                                Text     = Convert.ToString(item.Value),
                                Tag      = item,
                                Location = new Point(seperatorOffsetX, offsetY),
                                Size     = new Size(itemEditWidth, Options.ItemHeight)
                            };
                            textBox.TextChanged += TextBox_TextChanged;
                            Controls.Add(textBox);
                            itemEdits.Add(textBox);
                        }
                        else if (item.Type == GenericPropertyType.Integer)
                        {
                            var spinEdit = new NumericUpDown()
                            {
                                Value    = Convert.ToInt32(item.Value),
                                Tag      = item,
                                Location = new Point(seperatorOffsetX, offsetY),
                                Size     = new Size(itemEditWidth, Options.ItemHeight)
                            };
                            spinEdit.ValueChanged += SpinEdit_Integer_ValueChanged;

                            spinEdit.Minimum = Convert.ToInt32(item.MinimumValue);
                            spinEdit.Maximum = Convert.ToInt32(item.MaximumValue);

                            Controls.Add(spinEdit);
                            itemEdits.Add(spinEdit);
                        }
                        else if (item.Type == GenericPropertyType.Decimal)
                        {
                            var numericUpDown = new NumericUpDown()
                            {
                                Value    = Convert.ToDecimal(item.Value),
                                Tag      = item,
                                Location = new Point(seperatorOffsetX, offsetY),
                                Size     = new Size(itemEditWidth, Size.Height)
                            };
                            numericUpDown.ValueChanged += SpinEdit_Float_ValueChanged;

                            numericUpDown.Minimum = Convert.ToDecimal(item.MinimumValue);
                            numericUpDown.Maximum = Convert.ToDecimal(item.MaximumValue);

                            Controls.Add(numericUpDown);
                            itemEdits.Add(numericUpDown);
                        }
                        else if (item.Type == GenericPropertyType.Boolean)
                        {
                            var checkEdit = new CheckBox()
                            {
                                Checked  = Convert.ToBoolean(item.Value),
                                Text     = string.Empty,
                                Tag      = item,
                                Location = new Point(seperatorOffsetX, offsetY),
                                Size     = new Size(itemEditWidth, Size.Height)
                            };
                            checkEdit.CheckedChanged += CheckEdit_CheckedChanged;

                            Controls.Add(checkEdit);
                            itemEdits.Add(checkEdit);
                        }
                        else if (item.Type == GenericPropertyType.Enumeration)
                        {
                            var comboBox = new ComboBox
                            {
                                Tag      = item,
                                Location = new Point(seperatorOffsetX, offsetY),
                                Size     = new Size(itemEditWidth, Size.Height),
                            };
                            comboBox.SelectedValueChanged += ComboBoxEdit_SelectedValueChanged;

                            comboBox.DropDownStyle = ComboBoxStyle.DropDownList;

                            var selectedIndex = -1;
                            var index         = 0;

                            foreach (var enumItem in item.EnumItems)
                            {
                                comboBox.Items.Add(enumItem);

                                if (enumItem.Value.Equals(item.Value))
                                {
                                    selectedIndex = index;
                                }

                                index++;
                            }

                            comboBox.SelectedIndex = selectedIndex;

                            Controls.Add(comboBox);
                            itemEdits.Add(comboBox);
                        }
                        else if (item.Type == GenericPropertyType.Size)
                        {
                            // width
                            var spinEditWidth = new NumericUpDown()
                            {
                                Value    = Convert.ToInt32(item.Value),
                                Tag      = item,
                                Location = new Point(seperatorOffsetX, offsetY),
                                Size     = new Size(itemEditWidth, Size.Height),
                            };
                            spinEditWidth.ValueChanged += SpinEdit_Integer_ValueChanged;
                            //spinEditWidth.Properties.IsFloatValue = false;

                            Controls.Add(spinEditWidth);
                            itemEdits.Add(spinEditWidth);
                        }

                        /*
                         * else if (item.Type == GenericPropertyType.Color)
                         * {
                         *  var colorPickEdit = new ColorPickEdit()
                         *  {
                         *      Color = ColorHelper.FromHtml(Convert.ToString(item.Value)),
                         *      Text = string.Empty,
                         *      Tag = item,
                         *      Location = new Point(seperatorOffsetX, offsetY),
                         *      Size = new Size(itemEditWidth, Size.Height)
                         *  };
                         *
                         *  colorPickEdit.ColorChanged += ColorPickEdit_ColorChanged;
                         *
                         *  Controls.Add(colorPickEdit);
                         *  itemEdits.Add(colorPickEdit);
                         * }
                         */
                        else if (item.Type == GenericPropertyType.Path)
                        {
                            var buttonEdit = new ButtonEdit()
                            {
                                Text     = Convert.ToString(item.Value),
                                Tag      = item,
                                Location = new Point(seperatorOffsetX, offsetY),
                                Size     = new Size(itemEditWidth, Options.ItemHeight),
                            };

                            buttonEdit.TextChanged   += ButtonEdit_TextChanged;
                            buttonEdit.OnButtonClick += ButtonEdit_OnButtonClick;

                            if (item.CanUserResetToDefaultValue)
                            {
                                var buttonInfo = new ButtonInfo()
                                {
                                    Tag = "clear"
                                };

                                buttonEdit.Buttons.Insert(0, buttonInfo);
                            }

                            Controls.Add(buttonEdit);
                            itemEdits.Add(buttonEdit);
                        }

                        offsetY += Options.ItemHeight + Options.ItemGap;
                    }
                }

                ResumeLayout();
            }
        }