123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- 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;
- }
- }
- }
|