/// <summary> /// Overriding DrawButtons to add the Type selector buttons /// </summary> protected override void DrawButtons() { base.DrawButtons(); if (BottomHeight == 0) { return; } int maxWidthForTypeButtons = Width - BottomPadding * 3 - _itemsNbLabelWidth - 10; _nbDisplayableTypeButton = (int)Math.Floor((decimal)maxWidthForTypeButtons / TypeButtonWidth); _nbDisplayableTypeButton = _nbDisplayableTypeButton.ClampMax(_typeList.Count); var buttonsToDisplay = _typeList.Count; // for each distinct type of items, create the buttons int xPos = BottomPadding; int nBut = 0; foreach (var type in _typeList) { // new type, add a button for it if (!_typeButtons.ContainsKey(type)) { _typeButtons.Add(type, new SelectorButton { Size = new Size(TypeButtonWidth, DefaultBottomHeight), TabStop = false, AcceptsAnyClick = true, HideFocusedIndicator = true, Activated = true, BackGrndImage = TypeImages.ContainsKey(type) ? TypeImages[type] : null }); _typeButtons[type].ButtonPressed += HandleTypeClick; _tooltip.SetToolTip(_typeButtons[type], (TypeText.ContainsKey(type) && TypeText[type] != null ? TypeText[type] + "<br>" : "") + TypeButtonTooltipText); } _typeButtons[type].Anchor = AnchorStyles.Left | AnchorStyles.Bottom; _typeButtons[type].Type = type; _typeButtons[type].Activated = _typeButtons[type].Activated; _typeButtons[type].Location = new Point(xPos, Height - BottomHeight / 2 - _typeButtons[type].Height / 2); nBut++; // show as many button as we can show - 1 if (nBut < _nbDisplayableTypeButton || buttonsToDisplay == _nbDisplayableTypeButton) { xPos += TypeButtonWidth; if (!Controls.Contains(_typeButtons[type])) { Controls.Add(_typeButtons[type]); } } else { if (Controls.Contains(_typeButtons[type])) { Controls.Remove(_typeButtons[type]); } } } // remove buttons that are no longer used var tmpDic = new Dictionary <int, SelectorButton>(); foreach (int key in _typeButtons.Keys) { if (!_typeList.Contains(key)) { if (Controls.Contains(_typeButtons[key])) { Controls.Remove(_typeButtons[key]); } } else { tmpDic.Add(key, _typeButtons[key]); } } _typeButtons = tmpDic; if (nBut > 0 && buttonsToDisplay > _nbDisplayableTypeButton) { // we display a "more button" that opens a small interface to show the extra buttons if (_moreButton == null) { _moreButton = new SelectorButton { BackGrndImage = MoreTypesImage ?? Resources.Resources.More, Activated = true, Size = new Size(TypeButtonWidth, DefaultBottomHeight), TabStop = false, Anchor = AnchorStyles.Left | AnchorStyles.Bottom, HideFocusedIndicator = true }; _moreButton.ButtonPressed += HandleMoreTypeClick; _tooltip.SetToolTip(_moreButton, MoreButtonTooltipText); } _moreButton.Location = new Point(xPos, Height - BottomHeight / 2 - _moreButton.Height / 2); if (!Controls.Contains(_moreButton)) { Controls.Add(_moreButton); } } else { // if we have enough space to display the last button, hide the more button // remove the more button if it exists if (_moreButton != null) { if (Controls.Contains(_moreButton)) { Controls.Remove(_moreButton); } } } }
public void Build(Point location, List <int> typeList, Action <object, EventArgs> clickHandler, YamuiFilteredTypeList parentList) { _parentFilteredList = parentList; var buttonSize = new Size(YamuiFilteredTypeList.TypeButtonWidth, parentList.BottomHeight); // for each distinct type of items, create the buttons int xPos = -buttonSize.Width; int yPos = 0; int nBut = 0; int maxNbButPerRow = (int)Math.Ceiling(Math.Sqrt(typeList.Count)); foreach (var type in typeList) { xPos += buttonSize.Width; if (nBut >= maxNbButPerRow) { yPos += buttonSize.Height; xPos = 0; nBut = 0; } var button = new SelectorButton { Size = buttonSize, TabStop = false, Anchor = AnchorStyles.Left | AnchorStyles.Top, AcceptsAnyClick = true, HideFocusedIndicator = true, Activated = true, BackGrndImage = parentList.TypeImages.ContainsKey(type) ? parentList.TypeImages[type] : null, Type = type, Location = new Point(xPos, yPos) }; button.ButtonPressed += (sender, args) => { clickHandler(sender, args); foreach (SelectorButton control in _panel.Controls) { control.Activated = _parentFilteredList.IsTypeActivated(control.Type); } }; button.LostFocus += (sender, args) => CloseIfMouseOut(); button.Activated = _parentFilteredList.IsTypeActivated(type); _tooltip.SetToolTip(button, (parentList.TypeText.ContainsKey(type) && parentList.TypeText[type] != null ? parentList.TypeText[type] + "<br>" : "") + YamuiFilteredTypeList.TypeButtonTooltipText); if (!_panel.Controls.Contains(button)) { _panel.Controls.Add(button); } nBut++; } // Size the form Size = new Size(maxNbButPerRow * buttonSize.Width + BorderWidth * 2, (int)Math.Ceiling((double)typeList.Count / maxNbButPerRow) * buttonSize.Height + BorderWidth * 2); MinimumSize = Size; MaximumSize = Size; // menu position var screen = Screen.FromPoint(location); if (location.X - MousePaddingInsideForm > screen.WorkingArea.X + screen.WorkingArea.Width / 2) { location.X = location.X - Width + MousePaddingInsideForm; } else { location.X -= MousePaddingInsideForm; } if (location.Y - MousePaddingInsideForm > screen.WorkingArea.Y + screen.WorkingArea.Height / 2) { location.Y = location.Y - Height + MousePaddingInsideForm; } else { location.Y -= MousePaddingInsideForm; } Location = location; }