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