2 // System.Windows.Forms.Menu.cs
5 // stubbed out by Paul Osman (paul.osman@sympatico.ca)
6 // Dennis Hayes (dennish@raytek.com)
7 // Alexandre Pigolkine (pigolkine@gmx.de)
9 // (C) 2002 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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
34 using System.Reflection;
35 using System.Globalization;
36 //using System.Windows.Forms.AccessibleObject.IAccessible;
38 using System.Collections;
39 using System.Runtime.Remoting;
41 namespace System.Windows.Forms {
46 using System.ComponentModel;
47 public abstract class Menu : Component {
49 // Static Constant Fields
50 public const int FindHandle = 0;
51 public const int FindShortcut = 1;
59 public MenuItem FindMenuItem(int type, IntPtr value) {
60 throw new NotImplementedException();
64 public ContextMenu GetContextMenu() {
65 throw new NotImplementedException();
69 public MainMenu GetMainMenu() {
70 if ( parent_ == null )
71 return this as MainMenu;
73 Menu parent = parent_;
75 while ( parent != null ) {
76 if ( parent.parent_ != null )
77 parent = parent.parent_;
82 return parent as MainMenu;
86 public virtual void MergeMenu(Menu menuSrc) {
91 public override string ToString() {
92 //do our own ToString here.
93 // this is overrridden
94 return base.ToString();
98 // -- Protected Methods
103 protected void CloneMenu(Menu menuSrc) {
108 protected override void Dispose(bool disposing) {
110 base.Dispose(disposing);
114 protected int FindMergePosition(int mergeOrder) {
115 throw new NotImplementedException();
118 protected Menu( MenuItem[] items) {
119 MenuItems.AddRange ( items);
123 // -- Public Properties
126 private bool menuStructureModified_ = true;
128 internal bool MenuStructureModified {
130 menuStructureModified_ = value;
135 internal void BuildMenuStructure () {
136 if( menuStructureModified_) {
137 Win32.SetMenuDefaultItem(menuHandle_, -1, 0);
138 while( Win32.RemoveMenu( menuHandle_, 0, (uint)MF_.MF_BYPOSITION) != 0);
139 foreach(MenuItem mi in MenuItems) {
140 //System.Console.WriteLine("MenuItem {0} Parent {1}", mi.Text, mi.IsParent);
142 Win32.AppendMenuA( menuHandle_, (int)MF_.MF_ENABLED | (int)MF_.MF_STRING | (int)MF_.MF_POPUP,
146 Win32.AppendMenuA( menuHandle_, mi.MenuItemFlags,
147 (IntPtr) mi.GetID(), mi.Text);
149 Win32.SetMenuDefaultItem(menuHandle_, mi.GetID(), 0);
153 menuStructureModified_ = false;
157 internal Menu parent_ = null;
159 internal IntPtr menuHandle_ = IntPtr.Zero;
160 internal bool isPopupMenu = false;
163 protected virtual IntPtr CreateMenuHandle() {
164 //changed from internal void to protected intptr for signture reasons.
165 //Also had to add return menuitem_ at same time.
166 if( menuHandle_ == IntPtr.Zero) {
168 menuHandle_ = Win32.CreateMenu();
170 menuHandle_ = Win32.CreatePopupMenu ( );
171 //System.Console.WriteLine("Create menu {0}", menuHandle_);
172 BuildMenuStructure();
173 allMenus_[menuHandle_] = this;
178 public IntPtr Handle {
185 public virtual bool IsParent {
188 return MenuItems.Count != 0;
192 public MenuItem MdiListItem {
194 MenuItem mdiListItem = null;
195 foreach( MenuItem mi in MenuItems) {
199 mdiListItem = mi.MdiListItem;
200 if ( mdiListItem != null ) break;
206 private Menu.MenuItemCollection menuCollection_ = null;
208 public Menu.MenuItemCollection MenuItems {
210 if( menuCollection_ == null) {
211 menuCollection_ = new Menu.MenuItemCollection( this);
213 return menuCollection_;
220 // Recursively searches for specified item in menu.
221 // Goes immediately into child, when mets one.
222 internal MenuItem GetMenuItemByID (uint id) {
223 foreach( MenuItem mi in MenuItems) {
225 MenuItem submi = mi.GetMenuItemByID(id);
226 if( submi != null) return submi;
229 if( mi.GetID() == id){
237 private static Hashtable allMenus_ = new Hashtable();
239 internal static Menu GetMenuByHandle (IntPtr hMenu) {
242 result = allMenus_[hMenu] as Menu;
244 catch(ArgumentNullException) {
246 catch(NotSupportedException) {
251 internal void OnNewMenuItemAdd (MenuItem mi){
252 menuStructureModified_ = true;
256 internal void OnRemoveMenuItem (MenuItem mi)
258 if(menuHandle_ != IntPtr.Zero) {
259 menuStructureModified_ = true;
264 internal void OnLastSubItemRemoved ()
266 if( menuHandle_ != IntPtr.Zero) {
267 //System.Console.WriteLine("Delete menu {0}", menuHandle_);
268 Win32.DestroyMenu(menuHandle_);
269 allMenus_.Remove(menuHandle_);
270 menuHandle_ = IntPtr.Zero;
272 if( parent_ != null) {
273 parent_.MenuStructureModified = true;
278 internal void OnWmInitMenu ()
282 internal void OnWmInitMenuPopup ()
284 BuildMenuStructure();
288 // System.Windows.Forms.Menu.MenuItemCollection.cs
291 // stubbed out by Paul Osman (paul.osman@sympatico.ca)
293 // (C) 2002 Ximian, Inc
298 public class MenuItemCollection : IList, ICollection, IEnumerable {
299 private ArrayList items_ = new ArrayList();
300 private Menu parentMenu_ = null;
305 public MenuItemCollection (Menu m) {
309 internal void MoveItemToIndex( int index, MenuItem mi) {
310 if( index >= items_.Count){
311 // FIXME: Set exception parameters
312 throw new ArgumentException();
314 else if( items_.Count != 1){
316 items_.Insert (index, mi);
325 public virtual int Add (MenuItem mi) {
327 if( mi != null && parentMenu_ != null){
328 parentMenu_.OnNewMenuItemAdd(mi);
330 result = items_.Count - 1;
336 private MenuItem AddMenuItemCommon (MenuItem mi) {
337 return ( -1 != Add (mi)) ? mi : null;
340 public virtual MenuItem Add ( string s) {
341 return AddMenuItemCommon( new MenuItem (s));
344 public virtual int Add ( int i, MenuItem mi) {
345 if( i > items_.Count){
346 // FIXME: Set exception details
347 throw new System.ArgumentException();
350 if( mi != null && parentMenu_ != null){
351 parentMenu_.OnNewMenuItemAdd(mi);
352 items_.Insert(i, mi);
359 public virtual MenuItem Add (string s, EventHandler e) {
360 return AddMenuItemCommon(new MenuItem ( s, e));
363 public virtual MenuItem Add (string s, MenuItem[] items) {
364 return AddMenuItemCommon(new MenuItem ( s, items));
367 public virtual void AddRange(MenuItem[] items) {
369 foreach( MenuItem mi in items) {
375 private void DoClear() {
376 if( parentMenu_ != null) {
377 foreach( MenuItem mi in items_) {
378 parentMenu_.OnRemoveMenuItem( mi);
382 if( parentMenu_ != null) {
383 parentMenu_.OnLastSubItemRemoved();
387 public virtual void Clear() {
391 public bool Contains(MenuItem m) {
392 return items_.Contains(m);
395 public void CopyTo(Array a, int i) {
397 foreach( MenuItem mi in items_) {
398 MenuItem newMi = mi.CloneMenu();
399 a.SetValue(newMi,targetIdx++);
403 public override bool Equals(object o) {
404 return base.Equals(o);
408 public override int GetHashCode() {
409 //FIXME add our proprities
410 return base.GetHashCode();
413 public IEnumerator GetEnumerator() {
414 return items_.GetEnumerator();
417 public int IndexOf(MenuItem m) {
418 return items_.IndexOf(m);
421 public virtual void Remove(MenuItem m) {
422 if( m != null && parentMenu_ != null){
424 parentMenu_.OnRemoveMenuItem(m);
426 if( items_.Count == 0){
427 parentMenu_.OnLastSubItemRemoved();
433 public virtual void RemoveAt(int i) {
434 Remove(items_[i] as MenuItem);
437 public override string ToString() {
438 throw new NotImplementedException ();
442 // -- Protected Methods
445 ~MenuItemCollection() {
450 //protected object MemberwiseClone() {
451 // throw new NotImplementedException ();
455 // -- Public Properties
465 // public virtual MenuItem this(int i)
469 // throw new NotImplementedException ();
473 /// IList Interface implmentation.
475 bool IList.IsReadOnly {
477 // We allow addition, removeal, and editing of items after creation of the list.
482 bool IList.IsFixedSize {
484 // We allow addition and removeal of items after creation of the list.
489 public MenuItem this[int index] {
491 return items_[index] as MenuItem;
496 object IList.this[int index] {
498 return items_[index];
501 // FIXME: Set exception members
502 throw new System.NotSupportedException();
511 private MenuItem Object2MenuItem( object value) {
512 MenuItem result = value as MenuItem;
513 if( result == null) {
514 // FIXME: Set exception parameters
515 throw new System.ArgumentException();
521 int IList.Add( object value) {
522 return Add( Object2MenuItem(value));
526 bool IList.Contains( object value) {
527 return Contains(Object2MenuItem(value));
531 int IList.IndexOf( object value) {
532 return IndexOf(Object2MenuItem(value));
536 void IList.Insert(int index, object value) {
537 Add( index, Object2MenuItem(value));
541 void IList.Remove( object value) {
542 Remove( Object2MenuItem(value));
546 void IList.RemoveAt( int index){
549 // End of IList interface
552 /// ICollection Interface implmentation.
554 int ICollection.Count {
559 bool ICollection.IsSynchronized {
561 throw new NotImplementedException ();
564 object ICollection.SyncRoot {
566 throw new NotImplementedException ();
569 void ICollection.CopyTo(Array array, int index){
570 CopyTo(array, index);
572 // End Of ICollection