Fix bug #395
[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                 static object CollapseEvent = new object ();
41                 static object PopupEvent = new object ();
42
43                 public event EventHandler Collapse {
44                         add { Events.AddHandler (CollapseEvent, value); }
45                         remove { Events.RemoveHandler (CollapseEvent, value); }
46                 }
47
48                 public event EventHandler Popup {
49                         add { Events.AddHandler (PopupEvent, value); }
50                         remove { Events.RemoveHandler (PopupEvent, value); }
51                 }
52                 
53                 #endregion Events
54
55                 public ContextMenu () : base (null)
56                 {
57                         tracker = new MenuTracker (this);
58                         right_to_left = RightToLeft.Inherit;
59                 }
60
61                 public ContextMenu (MenuItem [] menuItems) : base (menuItems)
62                 {
63                         tracker = new MenuTracker (this);
64                         right_to_left = RightToLeft.Inherit;
65                 }
66                 
67                 #region Public Properties
68                 
69                 [Localizable(true)]
70                 [DefaultValue (RightToLeft.No)]
71                 public virtual RightToLeft RightToLeft {
72                         get { return right_to_left; }
73                         set { right_to_left = value; }
74                 }
75
76                 [Browsable(false)]
77                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
78                 public Control SourceControl {
79                         get { return src_control; }
80                 }
81
82                 #endregion Public Properties
83
84                 #region Public Methods
85
86                 protected internal virtual bool ProcessCmdKey (ref Message msg, Keys keyData, Control control)
87                 {
88                         src_control = control;
89                         return ProcessCmdKey (ref msg, keyData);
90                 }
91
92                 protected internal virtual void OnCollapse (EventArgs e)
93                 {
94                         EventHandler eh = (EventHandler) (Events [CollapseEvent]);
95                         if (eh != null)
96                                 eh (this, e);
97                 }
98
99                 protected internal virtual void OnPopup (EventArgs e)
100                 {
101                         EventHandler eh = (EventHandler) (Events [PopupEvent]);
102                         if (eh != null)
103                                 eh (this, e);
104                 }
105                 
106                 public void Show (Control control, Point pos)
107                 {
108                         if (control == null)
109                                 throw new ArgumentException ();
110
111                         src_control = control;
112
113                         OnPopup (EventArgs.Empty);
114                         pos = control.PointToScreen (pos);
115                         MenuTracker.TrackPopupMenu (this, pos);
116                         OnCollapse (EventArgs.Empty);
117                 }
118
119                 public void Show (Control control, Point pos, LeftRightAlignment alignment)
120                 {
121                         Point point;
122                         
123                         if (alignment == LeftRightAlignment.Left)
124                                 point = new Point ((pos.X - control.Width), pos.Y);
125                         else
126                                 point = pos;
127
128                         Show (control, point);
129                 }
130                 #endregion Public Methods
131                 internal void Hide ()
132                 {
133                         tracker.Deactivate ();
134                 }
135         }
136 }