Typo error
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / MainMenu.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
27 // COMPLETE
28
29 using System.ComponentModel;
30 using System.Drawing;
31
32 namespace System.Windows.Forms
33 {
34         [ToolboxItemFilter("System.Windows.Forms.MainMenu", ToolboxItemFilterType.Allow)]
35         public class MainMenu : Menu
36         {
37                 private RightToLeft right_to_left = RightToLeft.Inherit;
38                 private Form form = null;
39
40                 public MainMenu () : base (null)
41                 {
42                         
43                 }
44
45                 public MainMenu (MenuItem[] items) : base (items)
46                 {
47                         
48                 }
49
50                 public MainMenu (IContainer container) : this ()
51                 {
52                         container.Add (this);
53                 }
54
55                 #region Events
56
57                 static object CollapseEvent = new object ();
58
59                 public event EventHandler Collapse {
60                         add { Events.AddHandler (CollapseEvent, value); }
61                         remove { Events.RemoveHandler (CollapseEvent, value); }
62                 }
63                 
64                 #endregion Events
65
66                 #region Public Properties
67                 [Localizable(true)]
68                 [AmbientValue (RightToLeft.Inherit)]
69                 public virtual RightToLeft RightToLeft {
70                         get { return right_to_left;}
71                         set { right_to_left = value; }
72                 }
73
74                 #endregion Public Properties
75
76                 #region Public Methods
77                         
78                 public virtual MainMenu CloneMenu ()
79                 {
80                         MainMenu new_menu = new MainMenu ();
81                         new_menu.CloneMenu (this);
82                         return new_menu;
83                 }
84                 
85                 protected override IntPtr CreateMenuHandle ()
86                 {                       
87                         return IntPtr.Zero;
88                 }
89
90                 protected override void Dispose (bool disposing)
91                 {                       
92                         base.Dispose (disposing);                       
93                 }
94
95                 public Form GetForm ()
96                 {
97                         return form;
98                 }
99
100                 public override string ToString ()
101                 {
102                         return base.ToString () + ", GetForm: " + form;
103                 }
104
105                 protected internal virtual void OnCollapse (EventArgs e)
106                 {
107                         EventHandler eh = (EventHandler) (Events [CollapseEvent]);
108                         if (eh != null)
109                                 eh (this, e);
110                 }
111
112                 #endregion Public Methods
113                 
114                 #region Private Methods
115
116                 internal void Draw () 
117                 {
118                         Message m = Message.Create (Wnd.window.Handle, (int) Msg.WM_PAINT, IntPtr.Zero, IntPtr.Zero);
119                         PaintEventArgs pe = XplatUI.PaintEventStart (ref m, Wnd.window.Handle, false);
120                         Draw (pe, Rect);
121                 }
122
123                 internal void Draw (Rectangle rect) 
124                 {
125                         if (Wnd.IsHandleCreated) {
126                                 Point pt = XplatUI.GetMenuOrigin (Wnd.window.Handle);
127                                 Message m = Message.Create (Wnd.window.Handle, (int)Msg.WM_PAINT, IntPtr.Zero, IntPtr.Zero);
128                                 PaintEventArgs pevent = XplatUI.PaintEventStart (ref m, Wnd.window.Handle, false);
129                                 pevent.Graphics.SetClip (new Rectangle (rect.X + pt.X, rect.Y + pt.Y, rect.Width, rect.Height));
130                                 Draw (pevent, Rect);
131                                 XplatUI.PaintEventEnd (ref m, Wnd.window.Handle, false);
132                         }
133                 }
134
135                 internal void Draw (PaintEventArgs pe)          
136                 {
137                         Draw (pe, Rect);
138                 }
139
140                 internal void Draw (PaintEventArgs pe, Rectangle rect)
141                 {
142                         if (!Wnd.IsHandleCreated)
143                                 return;
144
145                         X = rect.X;
146                         Y = rect.Y;
147                         Height = Rect.Height;
148
149                         ThemeEngine.Current.DrawMenuBar (pe.Graphics, this, rect);
150
151                         PaintEventHandler eh = (PaintEventHandler)(Events [PaintEvent]);
152                         if (eh != null)
153                                 eh (this, pe);
154                 }
155
156                 internal override void InvalidateItem (MenuItem item)
157                 {
158                         Draw (item.bounds);
159                 }
160                 
161                 internal void SetForm (Form form)
162                 {
163                         this.form = form;
164                         Wnd = form;
165                         
166                         if (tracker == null) {
167                                 tracker = new MenuTracker (this);
168                                 tracker.GrabControl = form;
169                         }
170                 }
171                 
172                 internal override void OnMenuChanged (EventArgs e)
173                 {
174                         base.OnMenuChanged (EventArgs.Empty);
175                         if (form == null)
176                                 return;
177
178                         Rectangle clip = Rect;
179                         Height = 0; /* need this so the theme code will re-layout the menu items
180                                        (why is the theme code doing the layout?  argh) */
181
182                         if (!Wnd.IsHandleCreated)
183                                 return;
184
185                         Message m = Message.Create (Wnd.window.Handle, (int) Msg.WM_PAINT, IntPtr.Zero, IntPtr.Zero);
186                         PaintEventArgs pevent = XplatUI.PaintEventStart (ref m, Wnd.window.Handle, false);
187                         pevent.Graphics.SetClip (clip);
188                         
189                         Draw (pevent, clip);
190                 }
191
192                 /* Mouse events from the form */
193                 internal void OnMouseDown (object window, MouseEventArgs args)
194                 {                       
195                         tracker.OnMouseDown (args);
196                 }
197                 
198                 internal void OnMouseMove (object window, MouseEventArgs e)
199                 {                       
200                         MouseEventArgs args = new MouseEventArgs (e.Button, e.Clicks, Control.MousePosition.X, Control.MousePosition.Y, e.Delta);
201                         tracker.OnMotion (args);
202                 }
203
204                 static object PaintEvent = new object ();
205
206                 internal event PaintEventHandler Paint {
207                         add { Events.AddHandler (PaintEvent, value); }
208                         remove { Events.RemoveHandler (PaintEvent, value); }
209                 }
210
211                 #endregion Private Methods
212         }
213 }
214
215