/// <summary> /// Paints the specified item. /// </summary> /// <param name="g">A <see cref="Graphics"/> reference.</param> /// <param name="index">The index of the item.</param> /// <param name="bounds">The bounds in which to paint the item.</param> protected override void PaintItem(System.Drawing.Graphics g, int index, Rectangle bounds) { ButtonListItem li = BaseItems[index] as ButtonListItem; System.Diagnostics.Debug.WriteLine($"PaintItem: {Name}[{index}], Bounds=({bounds.X},{bounds.Y},{bounds.Width},{bounds.Height}), " + $"GlPos=({li.GlyphPosition.X},{li.GlyphPosition.Y}), TPos=({li.TextRect.X},{li.TextRect.Y},{li.TextRect.Width},{li.TextRect.Height})"); // Draw glyph PaintButton(g, index, bounds); // Draw text Rectangle tr = li.TextRect; tr.Offset(bounds.Location); TextRenderer.DrawText(g, li.Text, Font, tr, li.Enabled ? ForeColor : SystemColors.GrayText, TextFormatFlags); Rectangle str = li.SubtextRect; bool hasSubtext = !string.IsNullOrEmpty(li.Subtext); if (hasSubtext) { str.Offset(bounds.Location); TextRenderer.DrawText(g, li.Subtext, SubtextFont, str, li.Enabled ? SubtextForeColor : SystemColors.GrayText, TextFormatFlags); } // Draw focus rectangle if (index == FocusedIndex && Focused) { Rectangle fr = li.FocusRect; fr.Offset(bounds.Location); ControlPaint.DrawFocusRectangle(g, fr); } }
/// <summary> /// Determines whether the specified <see cref="ButtonListItem{T}"/> is equal to the current <see cref="ButtonListItem{T}"/>. /// </summary> /// <param name="other">The <see cref="ButtonListItem{T}"/> to compare with the current <see cref="ButtonListItem{T}"/>.</param> /// <returns> /// true if the specified <see cref="ButtonListItem{T}"/> is equal to the current <see cref="ButtonListItem{T}"/>; otherwise, false. /// </returns> public bool Equals(ButtonListItem <T> other) { if (other == null) { return(false); } return((Checked == other.Checked) && (Enabled == other.Enabled) && (Subtext == other.Subtext) && (Text == other.Text) && (ToolTipText == other.ToolTipText)); }
/// <summary> /// Focuses the next item. /// </summary> /// <param name="item">The current item.</param> /// <param name="forward">if set to <c>true</c>, moves to the next item, otherwise moves to the previous item.</param> /// <returns><c>true</c> on success, <c>false</c> otherwise.</returns> protected bool FocusNextItem(ButtonListItem <T> item, bool forward) { if (BaseItems.Count > 0) { var idx = -1; if (item != null && (idx = BaseItems.IndexOf(item)) == -1) { throw new ArgumentOutOfRangeException(nameof(item)); } idx = GetNextEnabledItemIndex(idx, forward); if (idx != -1) { SetFocused(idx); return(true); } } return(false); }
/// <summary> /// Measures the specified item. /// </summary> /// <param name="g">A <see cref="Graphics"/> reference.</param> /// <param name="index">The index of the item.</param> /// <param name="maxSize">Maximum size of the item. Usually only constrains the width.</param> /// <returns>Minimum size for the item.</returns> protected internal override Size MeasureItem(System.Drawing.Graphics g, int index, Size maxSize) { var chkAlign = new EnumFlagIndexer <ContentAlignment>(CheckAlign, false); // base.RtlTranslateContent(this.CheckAlign); var txtAlign = new EnumFlagIndexer <ContentAlignment>(TextAlign, false); // base.RtlTranslateContent(this.TextAlign); ButtonListItem item = BaseItems[index] as ButtonListItem; if (item == null) { return(Size.Empty); } // Get glyph size if (imageSize == Size.Empty) { imageSize = GetButtonSize(g); } int glyphWithPadding = imageSize.Width + (lrPadding * 2); // Calculate text size Size textSize = new Size(maxSize.Width, Int32.MaxValue); if (!chkAlign[anyCenterAlignment]) { textSize.Width -= glyphWithPadding; } Size tsz = TextRenderer.MeasureText(g, item.Text, Font, textSize, TextFormatFlags); item.TextRect = new Rectangle(0, 0, textSize.Width, tsz.Height); // NEW: item.TextRect = new Rectangle(Point.Empty, tsz); Size stsz = Size.Empty; item.SubtextRect = Rectangle.Empty; if (!string.IsNullOrEmpty(item.Subtext)) { stsz = TextRenderer.MeasureText(g, item.Subtext, SubtextFont, textSize, TextFormatFlags); item.SubtextRect = new Rectangle(0, tsz.Height + subtextSeparatorHeight, textSize.Width, stsz.Height); // NEW: item.SubtextRect = new Rectangle(0, tsz.Height + subtextSeparatorHeight, stsz.Width, stsz.Height); } // Calculate minimum item height int minHeight = item.TextRect.Height; if (!item.SubtextRect.IsEmpty) { minHeight += (item.SubtextRect.Height + subtextSeparatorHeight); } int textHeight = minHeight; if (chkAlign[ContentAlignment.TopCenter] || chkAlign[ContentAlignment.BottomCenter]) { minHeight += (imageSize.Height + tPadding); } // Calculate minimum item width int minWidth = Math.Max(tsz.Width, stsz.Width); if (imageSize.Width > 0 && !chkAlign[anyCenterAlignment]) { minWidth += (imageSize.Width + lrPadding); } Size itemSize = new Size(maxSize.Width, minHeight); // NEW: Size itemSize = new Size(minWidth, minHeight); // Set relative position of glyph item.GlyphPosition = Point.Empty; if (chkAlign[anyBottomAlignment]) { item.GlyphPosition.Y = itemSize.Height - imageSize.Height; } else if (chkAlign[anyMiddleAlignment]) { item.GlyphPosition.Y = (itemSize.Height - imageSize.Height) / 2; } else if (chkAlign == ContentAlignment.TopCenter) { item.OffsetText(0, imageSize.Height + tPadding); } if (chkAlign[anyRightAlignment]) { item.GlyphPosition.X = itemSize.Width - imageSize.Width; item.OffsetText(lrPadding, 0); } else if (chkAlign[anyCenterAlignment]) { item.GlyphPosition.X = (itemSize.Width - imageSize.Width) / 2; if (item.TextRect.Width < item.SubtextRect.Width) { item.TextRect.Offset((item.SubtextRect.Width - item.TextRect.Width) / 2, 0); } else { item.SubtextRect.Offset((item.TextRect.Width - item.SubtextRect.Width) / 2, 0); } } else { item.OffsetText(imageSize.Width + lrPadding, 0); } // Set text focus rectangle item.FocusRect = Rectangle.Union(item.TextRect, item.SubtextRect); /*item.FocusRect = new Rectangle(item.TextRect.Location, new Size(Math.Min(Math.Max(tsz.Width, stsz.Width), maxSize.Width), textHeight)); * if ((txtAlign & anyCenterAlignment) != (ContentAlignment)0) * item.FocusRect.X += (itemSize.Width - item.FocusRect.Width) / 2; * else if ((txtAlign & anyRightAlignment) != (ContentAlignment)0) * item.FocusRect.X = itemSize.Width - item.FocusRect.Width - imageSize.Width - lrPadding;*/ // Adjust text position for bottom or middle if (txtAlign[anyBottomAlignment]) { item.OffsetText(0, itemSize.Height - item.TextRect.Height); } else if (txtAlign[anyMiddleAlignment]) { item.OffsetText(0, (itemSize.Height - item.TextRect.Height) / 2); } return(itemSize); }
/// <summary> /// Determines whether the specified item is enabled. /// </summary> /// <param name="index">The item index.</param> /// <returns> /// <c>true</c> if item is enabled; otherwise, <c>false</c>. /// </returns> protected override bool IsItemEnabled(int index) { ButtonListItem item = BaseItems[index] as ButtonListItem; return(item == null ? true : item.Enabled); }
/// <summary> /// Gets the specified item's tooltip text. /// </summary> /// <param name="index">The index of the item.</param> /// <returns> /// Tooltip text to display. <c>null</c> or <see cref="String.Empty"/> to display no tooltip. /// </returns> protected override string GetItemToolTipText(int index) { ButtonListItem item = BaseItems[index] as ButtonListItem; return(item == null ? string.Empty : item.ToolTipText); }