using System.Drawing; namespace System.Windows.Forms { public abstract class ToolStripItem : Control { public bool Available { get => control.Visibility == Visibility.Visible; set => control.Visibility = value ? Visibility.Visible : Visibility.Collapsed; } public override bool Visible { get => control.Visibility == Visibility.Visible; set => control.Visibility = value ? Visibility.Visible : Visibility.Collapsed; } private ToolStripItemDisplayStyle displayStyle; public virtual ToolStripItemDisplayStyle DisplayStyle { get => displayStyle; set { displayStyle = value; UpdateText(); } } public override string Text { get => base.Text; set { base.Text = value; UpdateText(); ResetAutoSizeValue(false); } } private Image image; public virtual Image Image { get => image; set { image = value; ResetImage(); } } private int imageIndex; public int ImageIndex { get => imageIndex; set { imageIndex = value; // shouldn't take effect if Image is set if (Image == null) ResetImage(); } } private Media.ImageSource imageSource; internal Media.ImageSource ImageSource { get { if (imageSource == null) { if (Image == null) { var imageList = GetToolStrip()?.ImageList; if (imageList != null && imageIndex >= 0 && imageIndex < imageList.Images.Count) imageSource = imageList.Images.ImageSources[imageIndex]; } else { imageSource = Helper.GetImage(Image); } } return imageSource; } } public ToolStripItemImageScaling ImageScaling { get; set; } // TODO? // not in SWF public virtual System.Drawing.Size ImageSize { get; set; } public Color ImageTransparentColor { get; set; } // TODO? private ContentAlignment textAlign; public ContentAlignment TextAlign { get => textAlign; set { textAlign = value; Helper.GetContentAlignment(value, out var h, out var v); control.HorizontalContentAlignment = h; control.VerticalContentAlignment = v; } } public ContentAlignment ImageAlign { get; set; } // TODO? public string ToolTipText { get => control.ToolTip?.ToString(); set => control.ToolTip = value; } public Control Owner => base.Parent; public ToolStripItem OwnerItem { get; private set; } protected new ToolStrip Parent => GetToolStrip(); protected override void SetControlLeft(int value) { } protected override void SetControlTop(int value) { } protected internal override SizeF GetControlDesiredSize() { // we use margins here, unlike regular Control return new SizeF((float)control.DesiredSize.Width, (float)control.DesiredSize.Height); } protected override void ScaleCore(float dx, float dy) { } internal protected ToolStrip GetToolStrip() { Control parent = this; while (parent != null) { if (parent is ToolStrip ts) return ts; parent = parent.Parent; } return null; } public virtual void ApplyStyle(ToolStripProfessionalRenderer r) { } protected virtual void UpdateText() { } protected void SetItemControl(Control control) { SetControl(control.control); control.control.VerticalAlignment = VerticalAlignment.Stretch; control.Click += (s, e) => OnClick(e); } internal void SetOwnerItem(ToolStripItem ownerItem) { OwnerItem = ownerItem; } internal virtual void ResetImage() { imageSource = null; } internal void ApplyStyle() { var renderer = GetToolStrip()?.Renderer; if (renderer is ToolStripProfessionalRenderer pr) ApplyStyle(pr); } public virtual Drawing.Size GetPreferredSize(Drawing.Size constrainingSize) => constrainingSize; public void PerformClick() { OnClick(EventArgs.Empty); } public ToolStripItem() { AutoSize = true; imageIndex = -1; } } }