ToolStripItem.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System.Drawing;
  2. namespace System.Windows.Forms
  3. {
  4. public abstract class ToolStripItem : Control
  5. {
  6. public bool Available
  7. {
  8. get => control.Visibility == Visibility.Visible;
  9. set => control.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
  10. }
  11. public override bool Visible
  12. {
  13. get => control.Visibility == Visibility.Visible;
  14. set => control.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
  15. }
  16. private ToolStripItemDisplayStyle displayStyle;
  17. public virtual ToolStripItemDisplayStyle DisplayStyle
  18. {
  19. get => displayStyle;
  20. set
  21. {
  22. displayStyle = value;
  23. UpdateText();
  24. }
  25. }
  26. public override string Text
  27. {
  28. get => base.Text;
  29. set
  30. {
  31. base.Text = value;
  32. UpdateText();
  33. ResetAutoSizeValue(false);
  34. }
  35. }
  36. private Image image;
  37. public virtual Image Image
  38. {
  39. get => image;
  40. set
  41. {
  42. image = value;
  43. ResetImage();
  44. }
  45. }
  46. private int imageIndex;
  47. public int ImageIndex
  48. {
  49. get => imageIndex;
  50. set
  51. {
  52. imageIndex = value;
  53. // shouldn't take effect if Image is set
  54. if (Image == null)
  55. ResetImage();
  56. }
  57. }
  58. private Media.ImageSource imageSource;
  59. internal Media.ImageSource ImageSource
  60. {
  61. get
  62. {
  63. if (imageSource == null)
  64. {
  65. if (Image == null)
  66. {
  67. var imageList = GetToolStrip()?.ImageList;
  68. if (imageList != null && imageIndex >= 0 && imageIndex < imageList.Images.Count)
  69. imageSource = imageList.Images.ImageSources[imageIndex];
  70. }
  71. else
  72. {
  73. imageSource = Helper.GetImage(Image);
  74. }
  75. }
  76. return imageSource;
  77. }
  78. }
  79. public ToolStripItemImageScaling ImageScaling { get; set; } // TODO?
  80. // not in SWF
  81. public virtual System.Drawing.Size ImageSize { get; set; }
  82. public Color ImageTransparentColor { get; set; } // TODO?
  83. private ContentAlignment textAlign;
  84. public ContentAlignment TextAlign
  85. {
  86. get => textAlign;
  87. set
  88. {
  89. textAlign = value;
  90. Helper.GetContentAlignment(value, out var h, out var v);
  91. control.HorizontalContentAlignment = h;
  92. control.VerticalContentAlignment = v;
  93. }
  94. }
  95. public ContentAlignment ImageAlign { get; set; } // TODO?
  96. public string ToolTipText
  97. {
  98. get => control.ToolTip?.ToString();
  99. set => control.ToolTip = value;
  100. }
  101. public Control Owner => base.Parent;
  102. public ToolStripItem OwnerItem { get; private set; }
  103. protected new ToolStrip Parent => GetToolStrip();
  104. protected override void SetControlLeft(int value) { }
  105. protected override void SetControlTop(int value) { }
  106. protected internal override SizeF GetControlDesiredSize()
  107. {
  108. // we use margins here, unlike regular Control
  109. return new SizeF((float)control.DesiredSize.Width, (float)control.DesiredSize.Height);
  110. }
  111. protected override void ScaleCore(float dx, float dy) { }
  112. internal protected ToolStrip GetToolStrip()
  113. {
  114. Control parent = this;
  115. while (parent != null)
  116. {
  117. if (parent is ToolStrip ts)
  118. return ts;
  119. parent = parent.Parent;
  120. }
  121. return null;
  122. }
  123. public virtual void ApplyStyle(ToolStripProfessionalRenderer r) { }
  124. protected virtual void UpdateText() { }
  125. protected void SetItemControl(Control control)
  126. {
  127. SetControl(control.control);
  128. control.control.VerticalAlignment = VerticalAlignment.Stretch;
  129. control.Click += (s, e) => OnClick(e);
  130. }
  131. internal void SetOwnerItem(ToolStripItem ownerItem)
  132. {
  133. OwnerItem = ownerItem;
  134. }
  135. internal virtual void ResetImage()
  136. {
  137. imageSource = null;
  138. }
  139. internal void ApplyStyle()
  140. {
  141. var renderer = GetToolStrip()?.Renderer;
  142. if (renderer is ToolStripProfessionalRenderer pr)
  143. ApplyStyle(pr);
  144. }
  145. public virtual Drawing.Size GetPreferredSize(Drawing.Size constrainingSize) => constrainingSize;
  146. public void PerformClick()
  147. {
  148. OnClick(EventArgs.Empty);
  149. }
  150. public ToolStripItem()
  151. {
  152. AutoSize = true;
  153. imageIndex = -1;
  154. }
  155. }
  156. }