**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / MenuItem.cs
blob03cc2b56f2cdfcf8c4e436de9df324f4a429cb2e
1 //
2 // System.Windows.Forms.Menu.MenuItem.cs
3 //
4 // Author:
5 // stubbed out by Paul Osman (paul.osman@sympatico.ca)
6 // Dennis Hayes (dennish@raytek.com)
7 // Alexandre Pigolkine (pigolkine@gmx.de)
8 //
9 // (C) 2002/3 Ximian, Inc
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System;
34 using System.Reflection;
35 using System.Globalization;
36 //using System.Windows.Forms.AccessibleObject.IAccessible;
37 using System.Drawing;
38 using System.Runtime.Remoting;
39 using System.ComponentModel;
40 using System.Text;
42 namespace System.Windows.Forms {
44 /// <summary>
45 /// ToDo note:
46 /// </summary>
48 public class MenuItem : Menu {
49 int mergeOrder;
50 MenuMerge mergeType;
51 Shortcut shortcut;
52 bool mdiList;
53 bool showShortcut;
55 // - Constructor
57 public MenuItem() : base(null) {
58 shortcut = Shortcut.None;
59 mergeType = MenuMerge.Add; // FIXME: what is default
60 mdiList = false;
61 showShortcut = true;
64 public MenuItem(string s) : this(){
65 Text = s;
68 public MenuItem(string s, EventHandler e) : this( s ) {
69 Click += e;
72 public MenuItem(string s, MenuItem[] items) : base(items) {
73 Text = s;
74 shortcut = Shortcut.None;
75 mergeType = MenuMerge.Add; // FIXME: what is default
76 mdiList = false;
79 public MenuItem(string s, EventHandler e, Shortcut sc) : this(s, e) {
80 shortcut = sc;
83 public MenuItem(MenuMerge mm, int i, Shortcut sc, string s, EventHandler e, EventHandler e1, EventHandler e2, MenuItem[] items) : base(items){
84 throw new NotImplementedException ();
88 // -- Public Methods
91 public virtual MenuItem CloneMenu() {
92 MenuItem result = new MenuItem();
93 result.CloneMenu(this);
94 return result;
97 //public override ObjRef CreateObjRef(Type t) {
98 // throw new NotImplementedException ();
99 //}
101 //public override bool Equals(object o) {
102 // return base.Equals(o);
105 //[MonoTODO]
106 //public override int GetHashCode() {
107 // //FIXME add our proprities
108 // return base.GetHashCode();
111 public virtual MenuItem MergeMenu() {
112 throw new NotImplementedException ();
115 public void MergeMenu(MenuItem m) {
116 throw new NotImplementedException ();
119 public void PerformClick() {
120 OnClick( new EventArgs());
123 public virtual void PerformSelect() {
124 throw new NotImplementedException ();
127 public override string ToString() {
128 StringBuilder sb = new StringBuilder();
129 sb.Append(base.ToString());
130 sb.AppendFormat(", Items.Count: {0}, Text: {1}", MenuItems.Count, Text);
131 return sb.ToString();
135 // -- Protected Methods
138 protected void CloneMenu(MenuItem m) {
139 Text = m.Text;
140 Click += m.Click;
141 if( m.MenuItems.Count != 0){
142 MenuItem[] all_items = new MenuItem[m.MenuItems.Count];
143 m.MenuItems.CopyTo(all_items, 0);
144 MenuItems.AddRange(all_items);
147 [MonoTODO]
148 protected override void Dispose(bool disposing){
149 base.Dispose(disposing);
152 // ~MenuItem() {
153 // //FIXME: free resources
154 // //throw new NotImplementedException ();
155 // }
157 protected virtual void OnClick(EventArgs e) {
158 if( Click != null){
159 Click(this,e);
163 protected virtual void OnDrawItem(DrawItemEventArgs e) {
164 throw new NotImplementedException ();
167 protected virtual void OnMeasureItem(MeasureItemEventArgs e) {
168 throw new NotImplementedException ();
171 protected virtual void OnPopUp(EventArgs e) {
172 throw new NotImplementedException ();
175 protected virtual void OnSelect(EventArgs e) {
176 throw new NotImplementedException ();
180 // -- Public Properties
182 private void ModifyParent()
184 if( Parent != null)
186 Parent.MenuStructureModified = true;
190 private uint MenuItemFlags_ = (uint)MF_.MF_ENABLED | (uint)MF_.MF_STRING;
191 internal uint MenuItemFlags
195 return MenuItemFlags_;
199 private bool GetPropertyByFlag( MF_ flag)
201 return (MenuItemFlags_ & (uint)flag) != 0 ? true : false;
204 private void SetPropertyByFlag( MF_ flag, bool SetOrClear)
206 uint PrevState = MenuItemFlags_;
207 if( SetOrClear)
209 MenuItemFlags_ |= (uint)flag;
211 else
213 MenuItemFlags_ &= ~(uint)flag;
215 if( PrevState != MenuItemFlags_)
216 ModifyParent();
219 public bool BarBreak {
220 get {
221 return GetPropertyByFlag(MF_.MF_MENUBARBREAK);
223 set {
224 SetPropertyByFlag(MF_.MF_MENUBARBREAK, value);
228 public bool Break
230 get {
231 return GetPropertyByFlag(MF_.MF_MENUBREAK);
233 set {
234 SetPropertyByFlag(MF_.MF_MENUBREAK, value);
238 public bool Checked
240 get
242 return GetPropertyByFlag(MF_.MF_CHECKED);
244 set
246 SetPropertyByFlag(MF_.MF_CHECKED, value);
250 //public IContainer Container {
251 // get {
252 // throw new NotImplementedException ();
253 // }
256 public bool DefaultItem {
257 get
259 return GetPropertyByFlag(MF_.MF_DEFAULT);
261 set
263 SetPropertyByFlag(MF_.MF_DEFAULT, value);
267 public bool Enabled {
268 get
270 return !GetPropertyByFlag(MF_.MF_DISABLED | MF_.MF_GRAYED);
272 set
274 SetPropertyByFlag(MF_.MF_DISABLED | MF_.MF_GRAYED, !value);
278 // completely inherit from base class
279 public new IntPtr Handle {
280 get {
281 throw new NotImplementedException ();
285 protected int index_ = -1;
287 internal void SetIndex( int value)
289 index_ = value;
292 public int Index {
294 get {
295 return index_;
297 set {
298 if( index_ != value){
299 if(Parent != null) {
300 Parent.MenuItems.MoveItemToIndex(value, this);
301 Parent.MenuStructureModified = true;
307 public override bool IsParent {
309 get {
310 return base.IsParent;
314 public bool MdiList {
315 get { return mdiList; }
316 set {
317 if ( mdiList != value ) {
318 MainMenu mainMenu = GetMainMenu();
319 if ( mainMenu != null ) {
320 Form form = mainMenu.GetForm ( );
321 if ( form != null )
322 form.replaceMdiWindowMenu ( value ? Handle : IntPtr.Zero );
324 mdiList = value;
329 public int MergeOrder {
330 get {
331 return mergeOrder;
333 set {
334 mergeOrder = value;;
338 public MenuMerge MergeType {
339 get {
340 return mergeType;
342 set {
343 mergeType = value;
347 public char Mnemonic {
349 get {
350 throw new NotImplementedException ();
354 public bool OwnerDraw {
355 get {
356 return GetPropertyByFlag(MF_.MF_OWNERDRAW);
358 set {
359 SetPropertyByFlag(MF_.MF_OWNERDRAW, value);
363 internal void SetParent( Menu parent) {
364 if( parent == null)
365 throw new System.ArgumentNullException ("parent");
366 parent_ = parent;
369 public Menu Parent {
371 get {
372 return parent_;
376 private bool RadioCheck_ = false;
377 public bool RadioCheck {
379 get {
380 return RadioCheck_;
382 set {
383 RadioCheck_ = value;
384 ModifyParent();
388 public Shortcut Shortcut {
389 get {
390 return shortcut;
392 set {
393 shortcut = value;
397 public bool ShowShortcut {
398 get {
399 return showShortcut;
401 set {
402 showShortcut = value;
406 private string text_ = String.Empty;
408 public string Text {
409 get {
410 return text_;
412 set {
413 text_ = value;
414 if( text_ == "-") {
415 SetPropertyByFlag(MF_.MF_SEPARATOR, true);
417 else {
418 SetPropertyByFlag(MF_.MF_SEPARATOR, false);
419 //SetPropertyByFlag(MF_.MF_STRING, true);
422 ModifyParent();
426 private bool Visible_ = true;
427 public bool Visible {
429 get {
430 return Visible_;
432 set {
433 Visible_ = value;
434 ModifyParent();
439 // -- Protected Properties
442 internal const int INVALID_MENU_ID = -1; //0xffffffff;
443 protected int MenuID_ = INVALID_MENU_ID;
445 // Provides unique id to all items in all menus, hopefully space is enougth.
446 // Possible to use array to keep ids from deleted menu items
447 // and reuse them.
448 protected static int MenuIDs_ = 1;
450 protected int GetNewMenuID() {
451 return MenuIDs_++;
454 protected int MenuID {
456 get {
457 if( MenuID_ == INVALID_MENU_ID) {
458 MenuID_ = GetNewMenuID();
460 return (int)MenuID_;
465 // Btw, this function is funky, it is being used by routines that are supposed
466 // to be passing an IntPtr to the AppendMenu function
468 internal int GetID() {
469 return MenuID;
473 // -- Public Events
476 public event EventHandler Click;
477 //inherited
478 //public event EventHandler Disposed;
479 public event DrawItemEventHandler DrawItem;
480 public event MeasureItemEventHandler MeasureItem;
481 public event EventHandler PopUp;
482 public event EventHandler Select;