2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ContextMenu.cs
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:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
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.
19 //
20 // Copyright (c) 2004-2005 Novell, Inc.
21 //
22 // Authors:
23 //      Jordi Mas i Hernandez, jordi@ximian.com
24 //
25
26 using System;
27 using System.ComponentModel;
28 using System.ComponentModel.Design;
29 using System.Drawing;
30
31 namespace System.Windows.Forms
32 {
33         [DefaultEvent("Popup")]
34         public class ContextMenu : Menu
35         {
36                 private RightToLeft right_to_left;
37                 private Control src_control;
38
39                 #region Events
40 #if NET_2_0
41                 static object CollapseEvent = new object ();
42 #endif
43                 static object PopupEvent = new object ();
44
45 #if NET_2_0
46                 public event EventHandler Collapse {
47                         add { Events.AddHandler (CollapseEvent, value); }
48                         remove { Events.RemoveHandler (CollapseEvent, value); }
49                 }
50 #endif
51
52                 public event EventHandler Popup {
53                         add { Events.AddHandler (PopupEvent, value); }
54                         remove { Events.RemoveHandler (PopupEvent, value); }
55                 }
56                 
57                 #endregion Events
58
59                 public ContextMenu () : base (null)
60                 {
61                         tracker = new MenuTracker (this);
62                         right_to_left = RightToLeft.Inherit;
63                 }
64
65                 public ContextMenu (MenuItem [] menuItems) : base (menuItems)
66                 {
67                         tracker = new MenuTracker (this);
68                         right_to_left = RightToLeft.Inherit;
69                 }
70                 
71                 #region Public Properties
72                 
73                 [Localizable(true)]
74 #if NET_2_0
75                 [DefaultValue (RightToLeft.No)]
76 #endif
77                 public virtual RightToLeft RightToLeft {
78                         get { return right_to_left; }
79                         set { right_to_left = value; }
80                 }
81
82                 [Browsable(false)]
83                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
84                 public Control SourceControl {
85                         get { return src_control; }
86                 }
87
88                 #endregion Public Properties
89
90                 #region Public Methods
91
92 #if NET_2_0
93                 protected internal virtual bool ProcessCmdKey (ref Message msg, Keys keyData, Control control)
94                 {
95                         src_control = control;
96                         return ProcessCmdKey (ref msg, keyData);
97                 }
98
99                 protected internal virtual void OnCollapse (EventArgs e)
100                 {
101                         EventHandler eh = (EventHandler) (Events [CollapseEvent]);
102                         if (eh != null)
103                                 eh (this, e);
104                 }
105 #endif
106
107                 protected internal virtual void OnPopup (EventArgs e)
108                 {
109                         EventHandler eh = (EventHandler) (Events [PopupEvent]);
110                         if (eh != null)
111                                 eh (this, e);
112                 }
113                 
114                 public void Show (Control control, Point pos)
115                 {
116                         if (control == null)
117                                 throw new ArgumentException ();
118
119                         src_control = control;
120
121                         OnPopup (EventArgs.Empty);
122                         pos = control.PointToScreen (pos);
123                         MenuTracker.TrackPopupMenu (this, pos);
124 #if NET_2_0
125                         OnCollapse (EventArgs.Empty);
126 #endif
127                 }
128
129 #if NET_2_0
130                 public void Show (Control control, Point pos, LeftRightAlignment alignment)
131                 {
132                         Point point;
133                         
134                         if (alignment == LeftRightAlignment.Left)
135                                 point = new Point ((pos.X - control.Width), pos.Y);
136                         else
137                                 point = pos;
138
139                         Show (control, point);
140                 }
141 #endif
142                 #endregion Public Methods
143         }
144 }