2007-04-26 Everaldo Canuto <everaldo@simios.org>
[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 Public Properties
58                 [Localizable(true)]
59 #if NET_2_0
60                 [AmbientValue (RightToLeft.Inherit)]
61 #endif
62                 public virtual RightToLeft RightToLeft {
63                         get { return right_to_left;}
64                         set { right_to_left = value; }
65                 }
66
67                 #endregion Public Properties
68
69                 #region Public Methods
70                         
71                 public virtual MainMenu CloneMenu ()
72                 {
73                         MainMenu new_menu = new MainMenu ();
74                         new_menu.CloneMenu (this);
75                         return new_menu;
76                 }
77                 
78                 protected override IntPtr CreateMenuHandle ()
79                 {                       
80                         return IntPtr.Zero;
81                 }
82
83                 protected override void Dispose (bool disposing)
84                 {                       
85                         base.Dispose (disposing);                       
86                 }
87
88                 public Form GetForm ()
89                 {
90                         return form;
91                 }
92
93                 public override string ToString ()
94                 {
95                         return base.ToString () + ", GetForm: " + form;
96                 }
97
98                 #endregion Public Methods
99                 
100                 #region Private Methods
101
102                 internal void Draw () 
103                 {
104                         Draw (Rect);
105                 }
106
107                 internal void Draw (Rectangle rect) 
108                 {
109                         if (Wnd.IsHandleCreated) {
110                                 Point pt = XplatUI.GetMenuOrigin (Wnd.window.Handle);
111                                 PaintEventArgs pevent = XplatUI.PaintEventStart (Wnd.window.Handle, false);
112                                 pevent.Graphics.SetClip (new Rectangle (rect.X + pt.X, rect.Y + pt.Y, rect.Width, rect.Height));
113                                 Draw (pevent, Rect);
114                                 XplatUI.PaintEventEnd (Wnd.window.Handle, false);
115                         }
116                 }
117
118                 internal void Draw (PaintEventArgs pe)          
119                 {
120                         Draw (pe, Rect);
121                 }
122
123                 internal void Draw (PaintEventArgs pe, Rectangle rect)
124                 {
125                         if (!Wnd.IsHandleCreated)
126                                 return;
127
128                         X = rect.X;
129                         Y = rect.Y;
130                         Height = Rect.Height;
131
132                         ThemeEngine.Current.DrawMenuBar (pe.Graphics, this, rect);
133
134                         PaintEventHandler eh = (PaintEventHandler)(Events [PaintEvent]);
135                         if (eh != null)
136                                 eh (this, pe);
137                 }
138
139                 internal override void InvalidateItem (MenuItem item)
140                 {
141                         Draw (item.bounds);
142                 }
143                 
144                 internal void SetForm (Form form)
145                 {
146                         this.form = form;
147                         Wnd = form;
148                         
149                         if (tracker == null)
150                                 tracker = new MenuTracker (this); 
151                 }
152                 
153                 internal override void OnMenuChanged (EventArgs e)
154                 {
155                         base.OnMenuChanged (EventArgs.Empty);
156                         if (form == null)
157                                 return;
158
159                         Rectangle clip = Rect;
160                         Height = 0; /* need this so the theme code will re-layout the menu items
161                                        (why is the theme code doing the layout?  argh) */
162
163                         if (!Wnd.IsHandleCreated)
164                                 return;
165                         
166                         PaintEventArgs pevent = XplatUI.PaintEventStart (Wnd.window.Handle, false);
167                         pevent.Graphics.SetClip (clip);
168                         
169                         Draw (pevent, clip);
170                 }
171
172                 /* Mouse events from the form */
173                 internal void OnMouseDown (object window, MouseEventArgs args)
174                 {                       
175                         tracker.OnMouseDown (args);
176                 }
177                 
178                 internal void OnMouseMove (object window, MouseEventArgs e)
179                 {                       
180                         MouseEventArgs args = new MouseEventArgs (e.Button, e.Clicks, Control.MousePosition.X, Control.MousePosition.Y, e.Delta);
181                         tracker.OnMotion (args);
182                 }
183
184                 static object PaintEvent = new object ();
185
186                 internal event PaintEventHandler Paint {
187                         add { Events.AddHandler (PaintEvent, value); }
188                         remove { Events.RemoveHandler (PaintEvent, value); }
189                 }
190
191                 #endregion Private Methods
192         }
193 }
194
195