ToolStripEx.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.Threading;
  3. using System.Windows.Forms;
  4. namespace PaintDotNet.SystemLayer
  5. {
  6. /// <summary>
  7. /// This class adds on to the functionality provided in System.Windows.Forms.ToolStrip.
  8. /// </summary>
  9. public class ToolStripEx : ToolStrip
  10. {
  11. private bool clickThrough = true;
  12. private bool managedFocus = true;
  13. private static int lockFocusCount = 0;
  14. public ToolStripEx()
  15. {
  16. ToolStripProfessionalRenderer tspr = this.Renderer as ToolStripProfessionalRenderer;
  17. if (tspr != null)
  18. {
  19. tspr.ColorTable.UseSystemColors = true;
  20. tspr.RoundedEdges = false;
  21. }
  22. this.ImageScalingSize = new System.Drawing.Size(UI.ScaleWidth(16), UI.ScaleHeight(16));
  23. }
  24. protected override bool ProcessCmdKey(ref Message m, Keys keyData)
  25. {
  26. bool processed = false;
  27. Form form = this.FindForm();
  28. FormEx formEx = FormEx.FindFormEx(form);
  29. if (formEx != null)
  30. {
  31. processed = formEx.RelayProcessCmdKey(keyData);
  32. }
  33. return processed;
  34. }
  35. /// <summary>
  36. /// Gets or sets whether the ToolStripEx honors item clicks when its containing form does
  37. /// not have input focus.
  38. /// </summary>
  39. /// <remarks>
  40. /// Default value is true, which is the opposite of the behavior provided by the base
  41. /// ToolStrip class.
  42. /// </remarks>
  43. public bool ClickThrough
  44. {
  45. get
  46. {
  47. return this.clickThrough;
  48. }
  49. set
  50. {
  51. this.clickThrough = value;
  52. }
  53. }
  54. protected override void WndProc(ref Message m)
  55. {
  56. base.WndProc(ref m);
  57. if (this.clickThrough)
  58. {
  59. UI.ClickThroughWndProc(ref m);
  60. }
  61. }
  62. /// <summary>
  63. /// This event is raised when this toolstrip instance wishes to relinquish focus.
  64. /// </summary>
  65. public event EventHandler RelinquishFocus;
  66. private void OnRelinquishFocus()
  67. {
  68. if (RelinquishFocus != null)
  69. {
  70. RelinquishFocus(this, EventArgs.Empty);
  71. }
  72. }
  73. /// <summary>
  74. /// Gets or sets whether the toolstrip manages focus.
  75. /// </summary>
  76. /// <remarks>
  77. /// If this is true, the toolstrip will capture focus when the mouse enters its client area. It will then
  78. /// relinquish focus (via the RelinquishFocus event) when the mouse leaves. It will not capture or
  79. /// attempt to relinquish focus if MenuStripEx.IsAnyMenuActive returns true.
  80. /// </remarks>
  81. public bool ManagedFocus
  82. {
  83. get
  84. {
  85. return this.managedFocus;
  86. }
  87. set
  88. {
  89. this.managedFocus = value;
  90. }
  91. }
  92. protected override void OnItemAdded(ToolStripItemEventArgs e)
  93. {
  94. ToolStripComboBox tscb = e.Item as ToolStripComboBox;
  95. ToolStripTextBox tstb = e.Item as ToolStripTextBox;
  96. if (tscb != null)
  97. {
  98. tscb.DropDownClosed += ComboBox_DropDownClosed;
  99. tscb.Enter += ComboBox_Enter;
  100. tscb.Leave += ComboBox_Leave;
  101. }
  102. else if (tstb != null)
  103. {
  104. tstb.Enter += TextBox_Enter;
  105. tstb.Leave += TextBox_Enter;
  106. }
  107. else
  108. {
  109. e.Item.MouseEnter += OnItemMouseEnter;
  110. }
  111. base.OnItemAdded(e);
  112. }
  113. private static void PushLockFocus()
  114. {
  115. Interlocked.Increment(ref lockFocusCount);
  116. }
  117. private static void PopLockFocus()
  118. {
  119. Interlocked.Decrement(ref lockFocusCount);
  120. }
  121. private static bool IsFocusLocked
  122. {
  123. get
  124. {
  125. return lockFocusCount > 0;
  126. }
  127. }
  128. private void ComboBox_Enter(object sender, EventArgs e)
  129. {
  130. PushLockFocus();
  131. }
  132. private void ComboBox_Leave(object sender, EventArgs e)
  133. {
  134. PopLockFocus();
  135. }
  136. private void TextBox_Enter(object sender, EventArgs e)
  137. {
  138. PushLockFocus();
  139. }
  140. private void TextBox_Leave(object sender, EventArgs e)
  141. {
  142. PopLockFocus();
  143. }
  144. private void ComboBox_DropDownClosed(object sender, EventArgs e)
  145. {
  146. OnRelinquishFocus();
  147. }
  148. protected override void OnItemRemoved(ToolStripItemEventArgs e)
  149. {
  150. ToolStripComboBox tscb = e.Item as ToolStripComboBox;
  151. ToolStripTextBox tstb = e.Item as ToolStripTextBox;
  152. if (tscb != null)
  153. {
  154. tscb.DropDownClosed -= ComboBox_DropDownClosed;
  155. tscb.Enter -= ComboBox_Enter;
  156. tscb.Leave -= ComboBox_Leave;
  157. }
  158. else if (tstb != null)
  159. {
  160. tstb.Enter -= TextBox_Enter;
  161. tstb.Leave -= TextBox_Enter;
  162. }
  163. else
  164. {
  165. e.Item.MouseEnter -= OnItemMouseEnter;
  166. }
  167. base.OnItemRemoved(e);
  168. }
  169. private void OnItemMouseEnter(object sender, EventArgs e)
  170. {
  171. if (this.managedFocus && !MenuStripEx.IsAnyMenuActive && UI.IsOurAppActive && !IsFocusLocked)
  172. {
  173. Tracing.Ping("stealing focus");
  174. this.Focus();
  175. }
  176. }
  177. protected override void OnMouseLeave(EventArgs e)
  178. {
  179. if (this.managedFocus && !MenuStripEx.IsAnyMenuActive && UI.IsOurAppActive && !IsFocusLocked)
  180. {
  181. Tracing.Ping("relinquishing focus");
  182. OnRelinquishFocus();
  183. }
  184. base.OnMouseLeave(e);
  185. }
  186. }
  187. }