ToolStripDropDown.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. //
  2. // ToolStripDropDown.cs
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Copyright (c) 2006 Jonathan Pobst
  24. //
  25. // Authors:
  26. // Jonathan Pobst ([email protected])
  27. //
  28. #if NET_2_0
  29. using System.Drawing;
  30. using System.Runtime.InteropServices;
  31. using System.ComponentModel;
  32. namespace System.Windows.Forms
  33. {
  34. [ClassInterface (ClassInterfaceType.AutoDispatch)]
  35. [ComVisible (true)]
  36. public class ToolStripDropDown : ToolStrip
  37. {
  38. private bool allow_transparency;
  39. private bool auto_close;
  40. private bool drop_shadow_enabled = true;
  41. private double opacity = 1D;
  42. private ToolStripItem owner_item;
  43. #region Public Constructor
  44. public ToolStripDropDown () : base ()
  45. {
  46. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  47. SetStyle (ControlStyles.ResizeRedraw, true);
  48. this.auto_close = true;
  49. is_visible = false;
  50. Hwnd.ObjectFromHandle (this.Handle).no_activate = true;
  51. this.GripStyle = ToolStripGripStyle.Hidden;
  52. }
  53. #endregion
  54. #region Public Properties
  55. [Browsable (false)]
  56. [EditorBrowsable (EditorBrowsableState.Never)]
  57. public bool AllowTransparency {
  58. get { return allow_transparency; }
  59. set {
  60. if (value == allow_transparency)
  61. return;
  62. if ((XplatUI.SupportsTransparency () & TransparencySupport.Set) != 0) {
  63. allow_transparency = value;
  64. if (value)
  65. XplatUI.SetWindowTransparency (Handle, Opacity, Color.Empty);
  66. else
  67. UpdateStyles (); // Remove the WS_EX_LAYERED style
  68. }
  69. }
  70. }
  71. [Browsable (false)]
  72. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  73. public override AnchorStyles Anchor {
  74. get { return base.Anchor; }
  75. set { base.Anchor = value; }
  76. }
  77. [Browsable (false)]
  78. [EditorBrowsable (EditorBrowsableState.Never)]
  79. public bool AutoClose
  80. {
  81. get { return this.auto_close; }
  82. set { this.auto_close = value; }
  83. }
  84. [DefaultValue (true)]
  85. public override bool AutoSize {
  86. get { return base.AutoSize; }
  87. set { base.AutoSize = value; }
  88. }
  89. [Browsable (false)]
  90. [EditorBrowsable (EditorBrowsableState.Never)]
  91. public new ContextMenu ContextMenu {
  92. get { return null; }
  93. set { }
  94. }
  95. //[Browsable (false)]
  96. //[EditorBrowsable (EditorBrowsableState.Never)]
  97. //public ContextMenuStrip ContextMenuStrip {
  98. // get { return null; }
  99. // set { }
  100. //}
  101. [Browsable (false)]
  102. [EditorBrowsable (EditorBrowsableState.Never)]
  103. public override DockStyle Dock {
  104. get { return base.Dock; }
  105. set { base.Dock = value; }
  106. }
  107. public bool DropShadowEnabled {
  108. get { return this.drop_shadow_enabled; }
  109. set {
  110. if (this.drop_shadow_enabled == value)
  111. return;
  112. this.drop_shadow_enabled = value;
  113. UpdateStyles (); // Re-CreateParams
  114. }
  115. }
  116. public override Font Font {
  117. get { return base.Font; }
  118. set { base.Font = value; }
  119. }
  120. [Browsable (false)]
  121. [EditorBrowsable (EditorBrowsableState.Never)]
  122. public new ToolStripGripDisplayStyle GripDisplayStyle {
  123. get { return ToolStripGripDisplayStyle.Vertical; }
  124. set { }
  125. }
  126. [Browsable (false)]
  127. [EditorBrowsable (EditorBrowsableState.Never)]
  128. public new Padding GripMargin {
  129. get { return Padding.Empty; }
  130. set { }
  131. }
  132. [Browsable (false)]
  133. [EditorBrowsable (EditorBrowsableState.Never)]
  134. public new Rectangle GripRectangle {
  135. get { return Rectangle.Empty; }
  136. set { }
  137. }
  138. [Browsable (false)]
  139. [EditorBrowsable (EditorBrowsableState.Never)]
  140. public new ToolStripGripStyle GripStyle {
  141. get { return base.GripStyle; }
  142. set { base.GripStyle = value; }
  143. }
  144. [Browsable (false)]
  145. [EditorBrowsable (EditorBrowsableState.Never)]
  146. public new Point Location {
  147. get { return base.Location; }
  148. set { base.Location = value; }
  149. }
  150. public double Opacity {
  151. get { return this.opacity; }
  152. set {
  153. if (this.opacity == value)
  154. return;
  155. this.opacity = value;
  156. this.allow_transparency = true;
  157. UpdateStyles ();
  158. XplatUI.SetWindowTransparency (Handle, opacity, Color.Empty);
  159. }
  160. }
  161. public ToolStripItem OwnerItem {
  162. get { return this.owner_item; }
  163. set { this.owner_item = value;
  164. if (this.owner_item != null)
  165. if (this.owner_item.Owner != null)
  166. this.Renderer = this.owner_item.Owner.Renderer;
  167. }
  168. }
  169. public new Region Region {
  170. get { return base.Region; }
  171. set { base.Region = value; }
  172. }
  173. [Localizable (true)]
  174. public override RightToLeft RightToLeft {
  175. get { return base.RightToLeft; }
  176. set { base.RightToLeft = value; }
  177. }
  178. [Browsable (false)]
  179. [EditorBrowsable (EditorBrowsableState.Never)]
  180. public new bool Stretch {
  181. get { return false; }
  182. set { }
  183. }
  184. [Browsable (false)]
  185. [EditorBrowsable (EditorBrowsableState.Never)]
  186. public new int TabIndex {
  187. get { return 0; }
  188. set { }
  189. }
  190. public bool TopLevel {
  191. get { return this.TopMost; }
  192. set { }
  193. }
  194. [Localizable (true)]
  195. public new bool Visible {
  196. get { return base.Visible; }
  197. set { base.Visible = value; }
  198. }
  199. #endregion
  200. #region Protected Properties
  201. protected override CreateParams CreateParams {
  202. get {
  203. CreateParams cp = base.CreateParams;
  204. cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_CLIPCHILDREN));
  205. cp.ClassStyle = unchecked ((int)0x82000000);
  206. cp.ExStyle |= (int)(WindowExStyles.WS_EX_TOOLWINDOW | WindowExStyles.WS_EX_TOPMOST);
  207. if (Opacity < 1.0 && allow_transparency)
  208. cp.ExStyle |= (int)WindowExStyles.WS_EX_LAYERED;
  209. return cp;
  210. }
  211. }
  212. protected override DockStyle DefaultDock {
  213. get { return DockStyle.None; }
  214. }
  215. protected override Padding DefaultPadding {
  216. get { return new Padding (1, 2, 1, 2); }
  217. }
  218. protected override bool DefaultShowItemToolTips {
  219. get { return true; }
  220. }
  221. //protected internal override Size MaxItemSize {
  222. // get { return new Size (Screen.PrimaryScreen.Bounds.Width - 2, Screen.PrimaryScreen.Bounds.Height - 34); }
  223. //}
  224. protected virtual bool TopMost {
  225. get { return true; }
  226. }
  227. #endregion
  228. #region Public Methods
  229. public void Close ()
  230. {
  231. this.Close (ToolStripDropDownCloseReason.CloseCalled);
  232. }
  233. public void Close (ToolStripDropDownCloseReason reason)
  234. {
  235. // Give users a chance to cancel the close
  236. ToolStripDropDownClosingEventArgs e = new ToolStripDropDownClosingEventArgs (reason);
  237. this.OnClosing (e);
  238. if (e.Cancel)
  239. return;
  240. // Don't actually close if AutoClose == true unless explicitly called
  241. if (!this.auto_close && reason != ToolStripDropDownCloseReason.CloseCalled)
  242. return;
  243. // Detach from the tracker
  244. ToolStripManager.AppClicked -= new EventHandler (ToolStripMenuTracker_AppClicked); ;
  245. ToolStripManager.AppFocusChange -= new EventHandler (ToolStripMenuTracker_AppFocusChange);
  246. // Owner MenuItem needs to be told to redraw (it's no longer selected)
  247. if (owner_item != null)
  248. owner_item.Invalidate ();
  249. // Recursive hide all child dropdowns
  250. foreach (ToolStripItem tsi in this.Items)
  251. if (tsi is ToolStripMenuItem)
  252. (tsi as ToolStripMenuItem).HideDropDown (reason);
  253. // Hide this dropdown
  254. this.Hide ();
  255. this.OnClosed (new ToolStripDropDownClosedEventArgs (reason));
  256. }
  257. public new void Show ()
  258. {
  259. CancelEventArgs e = new CancelEventArgs ();
  260. this.OnOpening (e);
  261. if (e.Cancel)
  262. return;
  263. // The tracker lets us know when the form is clicked or loses focus
  264. ToolStripManager.AppClicked += new EventHandler (ToolStripMenuTracker_AppClicked);
  265. ToolStripManager.AppFocusChange += new EventHandler (ToolStripMenuTracker_AppFocusChange);
  266. base.Show ();
  267. this.OnOpened (EventArgs.Empty);
  268. }
  269. public void Show (Point screenLocation)
  270. {
  271. this.Location = screenLocation;
  272. Show ();
  273. }
  274. public void Show (Control control, Point position)
  275. {
  276. if (control == null)
  277. throw new ArgumentNullException ("control");
  278. this.Location = control.PointToScreen (position);
  279. }
  280. public void Show (int x, int y)
  281. {
  282. this.Location = new Point (x, y);
  283. Show ();
  284. }
  285. public void Show (Control control, int x, int y)
  286. {
  287. Show (control, new Point (x, y));
  288. }
  289. #endregion
  290. #region Protected Methods
  291. protected override void CreateHandle ()
  292. {
  293. base.CreateHandle ();
  294. }
  295. protected override void Dispose (bool disposing)
  296. {
  297. base.Dispose (disposing);
  298. }
  299. protected virtual void OnClosed (ToolStripDropDownClosedEventArgs e)
  300. {
  301. if (Closed != null) Closed (this, e);
  302. }
  303. protected virtual void OnClosing (ToolStripDropDownClosingEventArgs e)
  304. {
  305. if (Closing != null) Closing (this, e);
  306. }
  307. protected override void OnHandleCreated (EventArgs e)
  308. {
  309. base.OnHandleCreated (e);
  310. }
  311. protected override void OnItemClicked (ToolStripItemClickedEventArgs e)
  312. {
  313. base.OnItemClicked (e);
  314. }
  315. protected override void OnLayout (LayoutEventArgs e)
  316. {
  317. base.OnLayout (e);
  318. // Find the widest menu item
  319. int widest = 0;
  320. foreach (ToolStripItem tsi in this.Items)
  321. if (tsi.GetPreferredSize (Size.Empty).Width > widest)
  322. widest = tsi.GetPreferredSize (Size.Empty).Width;
  323. int x = this.Padding.Left;
  324. widest += 68 - this.Padding.Horizontal;
  325. int y = this.Padding.Top;
  326. foreach (ToolStripItem tsi in this.Items) {
  327. y += tsi.Margin.Top;
  328. int height = 0;
  329. if (tsi is ToolStripSeparator)
  330. height = 7;
  331. else
  332. height = 22;
  333. tsi.SetBounds (new Rectangle (x, y, widest, height));
  334. y += tsi.Height + tsi.Margin.Bottom;
  335. }
  336. this.Size = new Size (widest + this.Padding.Horizontal, y + this.Padding.Bottom);// + 2);
  337. }
  338. protected override void OnMouseUp (MouseEventArgs mea)
  339. {
  340. base.OnMouseUp (mea);
  341. }
  342. protected virtual void OnOpened (EventArgs e)
  343. {
  344. if (Opened != null) Opened (this, e);
  345. }
  346. protected virtual void OnOpening (CancelEventArgs e)
  347. {
  348. if (Opening != null) Opening (this, e);
  349. }
  350. protected override void OnParentChanged (EventArgs e)
  351. {
  352. base.OnParentChanged (e);
  353. if (parent is ToolStrip)
  354. this.Renderer = (parent as ToolStrip).Renderer;
  355. }
  356. protected override void OnVisibleChanged (EventArgs e)
  357. {
  358. base.OnVisibleChanged (e);
  359. }
  360. protected override bool ProcessDialogChar (char charCode)
  361. {
  362. return base.ProcessDialogChar (charCode);
  363. }
  364. protected override bool ProcessDialogKey (Keys keyData)
  365. {
  366. return base.ProcessDialogKey (keyData);
  367. }
  368. protected override bool ProcessMnemonic (char charCode)
  369. {
  370. return base.ProcessMnemonic (charCode);
  371. }
  372. protected override void ScaleCore (float dx, float dy)
  373. {
  374. base.ScaleCore (dx, dy);
  375. }
  376. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  377. {
  378. base.SetBoundsCore (x, y, width, height, specified);
  379. }
  380. protected override void SetVisibleCore (bool value)
  381. {
  382. base.SetVisibleCore (value);
  383. }
  384. protected override void WndProc (ref Message m)
  385. {
  386. const int MA_NOACTIVATE = 0x0003;
  387. // Don't activate when the WM tells us to
  388. if ((Msg)m.Msg == Msg.WM_MOUSEACTIVATE) {
  389. m.Result = (IntPtr)MA_NOACTIVATE;
  390. return;
  391. }
  392. base.WndProc (ref m);
  393. }
  394. #endregion
  395. #region Public Events
  396. public new event EventHandler BackgroundImageChanged;
  397. public event EventHandler BackgroundImageLayoutChanged;
  398. public new event EventHandler BindingContextChanged;
  399. public new event UICuesEventHandler ChangeUICues;
  400. public event ToolStripDropDownClosedEventHandler Closed;
  401. public event ToolStripDropDownClosingEventHandler Closing;
  402. [Browsable (false)]
  403. [EditorBrowsable (EditorBrowsableState.Never)]
  404. public new event EventHandler ContextMenuChanged;
  405. [Browsable (false)]
  406. [EditorBrowsable (EditorBrowsableState.Never)]
  407. public event EventHandler ContextMenuStripChanged;
  408. [Browsable (false)]
  409. [EditorBrowsable (EditorBrowsableState.Never)]
  410. public new event EventHandler DockChanged;
  411. public new event EventHandler Enter;
  412. public new event EventHandler FontChanged;
  413. public new event EventHandler ForeColorChanged;
  414. [Browsable (false)]
  415. [EditorBrowsable (EditorBrowsableState.Never)]
  416. public new event GiveFeedbackEventHandler GiveFeedback;
  417. public new event HelpEventHandler HelpRequested;
  418. public new event EventHandler ImeModeChanged;
  419. public new event KeyEventHandler KeyDown;
  420. public new event KeyPressEventHandler KeyPress;
  421. public new event KeyEventHandler KeyUp;
  422. public new event EventHandler Leave;
  423. public event EventHandler Opened;
  424. public event CancelEventHandler Opening;
  425. public event EventHandler RegionChanged;
  426. [Browsable (false)]
  427. [EditorBrowsable (EditorBrowsableState.Never)]
  428. public event ScrollEventHandler Scroll;
  429. public new event EventHandler StyleChanged;
  430. [Browsable (false)]
  431. [EditorBrowsable (EditorBrowsableState.Never)]
  432. public new event EventHandler TabIndexChanged;
  433. [Browsable (false)]
  434. [EditorBrowsable (EditorBrowsableState.Never)]
  435. public new event EventHandler TabStopChanged;
  436. [Browsable (false)]
  437. [EditorBrowsable (EditorBrowsableState.Never)]
  438. public new event EventHandler TextChanged;
  439. [Browsable (false)]
  440. [EditorBrowsable (EditorBrowsableState.Never)]
  441. public new event EventHandler Validated;
  442. [Browsable (false)]
  443. [EditorBrowsable (EditorBrowsableState.Never)]
  444. public new event CancelEventHandler Validating;
  445. #endregion
  446. #region Private Methods
  447. private void ToolStripMenuTracker_AppFocusChange (object sender, EventArgs e)
  448. {
  449. this.Close (ToolStripDropDownCloseReason.AppFocusChange);
  450. }
  451. private void ToolStripMenuTracker_AppClicked (object sender, EventArgs e)
  452. {
  453. this.Close (ToolStripDropDownCloseReason.AppClicked);
  454. }
  455. #endregion
  456. }
  457. }
  458. #endif