2008-03-27 Carlos Alberto Cortez <calberto.cortez@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                         right_to_left = RightToLeft.Inherit;
62                 }
63
64                 public ContextMenu (MenuItem [] menuItems) : base (menuItems)
65                 {
66                         right_to_left = RightToLeft.Inherit;
67                 }
68
69                 #region Public Properties
70                 
71                 [Localizable(true)]
72 #if NET_2_0
73                 [DefaultValue (RightToLeft.No)]
74 #endif
75                 public virtual RightToLeft RightToLeft {
76                         get { return right_to_left; }
77                         set { right_to_left = value; }
78                 }
79
80                 [Browsable(false)]
81                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
82                 public Control SourceControl {
83                         get { return src_control; }
84                 }
85
86                 #endregion Public Properties
87
88                 #region Public Methods
89
90 #if NET_2_0
91                 protected internal virtual bool ProcessCmdKey (ref Message msg, Keys keyData, Control control)
92                 {
93                         src_control = control;
94                         return ProcessCmdKey (ref msg, keyData);
95                 }
96
97                 protected internal virtual void OnCollapse (EventArgs e)
98                 {
99                         EventHandler eh = (EventHandler) (Events [CollapseEvent]);
100                         if (eh != null)
101                                 eh (this, e);
102                 }
103 #endif
104
105                 protected internal virtual void OnPopup (EventArgs e)
106                 {
107                         EventHandler eh = (EventHandler) (Events [PopupEvent]);
108                         if (eh != null)
109                                 eh (this, e);
110                 }
111                 
112                 public void Show (Control control, Point pos)
113                 {
114                         if (control == null)
115                                 throw new ArgumentException ();
116
117                         src_control = control;
118
119                         OnPopup (EventArgs.Empty);
120                         pos = control.PointToScreen (pos);
121                         MenuTracker.TrackPopupMenu (this, pos);
122 #if NET_2_0
123                         OnCollapse (EventArgs.Empty);
124 #endif
125                 }
126
127 #if NET_2_0
128                 public void Show (Control control, Point pos, LeftRightAlignment alignment)
129                 {
130                         Point point;
131                         
132                         if (alignment == LeftRightAlignment.Left)
133                                 point = new Point ((pos.X - control.Width), pos.Y);
134                         else
135                                 point = pos;
136
137                         Show (control, point);
138                 }
139 #endif
140                 #endregion Public Methods
141         }
142 }