1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-2005 Novell, Inc.
23 // Jordi Mas i Hernandez, jordi@ximian.com
27 using System.ComponentModel;
28 using System.ComponentModel.Design;
31 namespace System.Windows.Forms
33 [DefaultEvent("Popup")]
34 public class ContextMenu : Menu
36 private RightToLeft right_to_left;
37 private Control src_control;
41 static object CollapseEvent = new object ();
43 static object PopupEvent = new object ();
46 public event EventHandler Collapse {
47 add { Events.AddHandler (CollapseEvent, value); }
48 remove { Events.RemoveHandler (CollapseEvent, value); }
52 public event EventHandler Popup {
53 add { Events.AddHandler (PopupEvent, value); }
54 remove { Events.RemoveHandler (PopupEvent, value); }
59 public ContextMenu () : base (null)
61 tracker = new MenuTracker (this);
62 right_to_left = RightToLeft.Inherit;
65 public ContextMenu (MenuItem [] menuItems) : base (menuItems)
67 tracker = new MenuTracker (this);
68 right_to_left = RightToLeft.Inherit;
71 #region Public Properties
75 [DefaultValue (RightToLeft.No)]
77 public virtual RightToLeft RightToLeft {
78 get { return right_to_left; }
79 set { right_to_left = value; }
83 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
84 public Control SourceControl {
85 get { return src_control; }
88 #endregion Public Properties
90 #region Public Methods
93 protected internal virtual bool ProcessCmdKey (ref Message msg, Keys keyData, Control control)
95 src_control = control;
96 return ProcessCmdKey (ref msg, keyData);
99 protected internal virtual void OnCollapse (EventArgs e)
101 EventHandler eh = (EventHandler) (Events [CollapseEvent]);
107 protected internal virtual void OnPopup (EventArgs e)
109 EventHandler eh = (EventHandler) (Events [PopupEvent]);
114 public void Show (Control control, Point pos)
117 throw new ArgumentException ();
119 src_control = control;
121 OnPopup (EventArgs.Empty);
122 pos = control.PointToScreen (pos);
123 MenuTracker.TrackPopupMenu (this, pos);
125 OnCollapse (EventArgs.Empty);
130 public void Show (Control control, Point pos, LeftRightAlignment alignment)
134 if (alignment == LeftRightAlignment.Left)
135 point = new Point ((pos.X - control.Width), pos.Y);
139 Show (control, point);
142 #endregion Public Methods