* UnsafeNativeMethods.cs: added IEnumVariant interface,
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / DomainUpDown.cs
index ce1aba4057234ea534f7d3bf45a30e548aa26af0..e308706fcc7520c456d422558fc0301564e6de01 100644 (file)
@@ -4,11 +4,13 @@
 // Author:
 //   stubbed out by Richard Baumann (biochem333@nyc.rr.com)
 //   Dennis Hayes (dennish@Raytek.com)
-//
+//   implemented by Aleksey Ryabchuk (ryabchuk@yahoo.com)
 // (C) Ximian, Inc., 2002
 //
 using System.Collections;
 using System.ComponentModel;
+using System.Globalization;
+
 namespace System.Windows.Forms {
 
        // <summary>
@@ -17,108 +19,173 @@ namespace System.Windows.Forms {
        public class DomainUpDown : UpDownBase {
 
                
-               //  --- Constructors/Destructors
-               
+               DomainUpDownItemCollection items;
+               int selectedIndex;
+               bool sorted;
+               bool wrap;
+
                [MonoTODO]
                public DomainUpDown() : base()
                {
+                       selectedIndex = -1;
+                       sorted = false;
+                       wrap = false;
+                       TextChanged += new EventHandler ( this.BuddyTextChanged );
                }
 
                
-               //  --- Public Methods
-               
-               [MonoTODO]
                public override void DownButton()
                {
-                       throw new NotImplementedException ();
+                       int newIndex = SelectedIndex + 1;
+                       if (  newIndex < Items.Count )
+                               SelectedIndex = newIndex;
+                       else if ( Wrap && Items.Count > 0)
+                               SelectedIndex = 0;
+                               
                }
-               [MonoTODO]
+
                public override string ToString()
                {
-                       //FIXME:
-                       return base.ToString();
+                       return GetType( ).FullName.ToString( ) + ", Items.Count: " + Items.Count.ToString ( ) + 
+                               ", SelectedIndex: " + SelectedIndex;
                }
-               [MonoTODO]
+
                public override void UpButton()
                {
-                       throw new NotImplementedException ();
+                       int newIndex = SelectedIndex - 1;
+                       if ( newIndex > -1 && newIndex < Items.Count )
+                               SelectedIndex = newIndex;
+                       else if ( Wrap && Items.Count > 0 )
+                               SelectedIndex = Items.Count - 1;
                }
 
-               
-               //  --- Protected Methods
-               
                [MonoTODO]
                protected override AccessibleObject CreateAccessibilityInstance()
                {
                        //FIXME:
                        return base.CreateAccessibilityInstance();
                }
-               //[MonoTODO]
-               //protected override void OnChanged(object source, EventArgs e)
-               //{
-               //      //This method is internal to the .NET framework.
-               //      if (Changed != null) {
-               //
-               //              Changed(this, e);
-               //      }
-               //}
-               [MonoTODO]
+
                protected void OnSelectedItemChanged(object source, EventArgs e)
                {
-                       if (SelectedItemChanged != null) {
-
+                       if (SelectedItemChanged != null) 
                                SelectedItemChanged(this, e);
-                       }
                }
+
+               [MonoTODO]
+               protected override void OnChanged(object source, EventArgs e) {
+                       base.OnChanged(source, e);
+               }
+
                [MonoTODO]
                protected override void OnTextBoxKeyDown(object source, KeyEventArgs e)
                {
-                       throw new NotImplementedException ();
-                       //if (TextBoxKeyDown != null) {
-                       //      TextBoxKeyDown(this, e);
-                       //}
+                       if ( ReadOnly ) {
+                               char symbol = System.Convert.ToChar( (int)e.KeyCode );
+                               
+                               if ( Char.IsLetterOrDigit ( symbol ) ) {
+                                       string lower = Char.ToLower ( symbol ).ToString ( );
+                                       string upper = Char.ToUpper ( symbol ).ToString ( );
+
+                                       foreach ( object item in Items ) {
+                                               string sitem = item.ToString ( );
+                                               if ( sitem.StartsWith ( upper ) || sitem.StartsWith ( lower ) ) {
+                                                       SelectedItem = item;
+                                                       break;
+                                               }
+                                       }
+                                       e.Handled = true;
+                               }
+                       }
+                       base.OnTextBoxKeyDown ( source, e );
                }
-               [MonoTODO]
-               protected override void UpdateEditText()
+
+               protected override void UpdateEditText ( )
                {
-                       throw new NotImplementedException ();
+                       if ( SelectedIndex != -1 )
+                               Text = Items [ SelectedIndex ].ToString ( );
+                       else
+                               Text = String.Empty;
                }
 
-               //  --- Public Events
-               
                public event EventHandler SelectedItemChanged;
 
-               
-               //  --- Public Properties
-               
-               [MonoTODO]
                public DomainUpDown.DomainUpDownItemCollection Items {
-
-                       get { throw new NotImplementedException (); }
+                       get {
+                               if ( items == null )
+                                       items = new DomainUpDownItemCollection ( this );
+                               return items; 
+                       }
                }
+
                [MonoTODO]
-               public int SelectedIndex{// default -1 {
+               public int SelectedIndex {
+                       get { return selectedIndex; }
+                       set {
+                               if ( value < -1 || value >= Items.Count )
+                                       throw new ArgumentException ( ); // FIXME: message
+
+                               if ( selectedIndex != value ) {
+                                       selectedIndex = value;
+                                       UpdateEditText ( );
+                               }
+                       }
+               }
 
-                       get { throw new NotImplementedException (); }
-                       set { throw new NotImplementedException (); }
+               [MonoTODO]
+               public object SelectedItem {
+                       get { 
+                               if ( SelectedIndex == -1 )
+                                       return null;
+                               return Items[ SelectedIndex ];
+                       }
+                       set {
+                               SelectedIndex = Items.IndexOf ( value );
+                       }
                }
+
                [MonoTODO]
-               public object SelectedItem{ // default null {
+               public bool Sorted {
+                       get { return sorted; }
+                       set { 
+                               if ( sorted != value ) {
+                                       object selectedItem = SelectedItem;
+                                       Items.Sort ( );
+                                       SelectedItem = selectedItem;
+                               }
+                       }
+               }
 
-                       get { throw new NotImplementedException (); }
-                       set { throw new NotImplementedException (); }
+               public bool Wrap { 
+                       get { return wrap; }
+                       set { wrap = value; }
+               }
+
+               private void itemAdded ( object item )
+               {
                }
-               [MonoTODO]
-               public bool Sorted{ // default false {
 
-                       get { throw new NotImplementedException (); }
-                       set { throw new NotImplementedException (); }
+               private void itemInserted ( int index, object item )
+               {
                }
-               [MonoTODO]
-               public bool Wrap{ // default false {
 
-                       get { throw new NotImplementedException (); }
-                       set { throw new NotImplementedException (); }
+               private void itemRemoved ( object item )
+               {
+               }
+
+               private void itemRemoved ( int index )
+               {
+               }
+
+               private void itemChanged ( int index )
+               {
+                       if ( index == SelectedIndex )
+                               UpdateEditText ( );
+               }
+
+               private void BuddyTextChanged ( object sender, EventArgs e )
+               {
+                       OnSelectedItemChanged ( this, EventArgs.Empty );
                }
 
                //System.Windows.Forms.DomainUpDown.DomainUpDownItemCollection
@@ -133,53 +200,46 @@ namespace System.Windows.Forms {
                //</summary>
                public class DomainUpDownItemCollection : ArrayList {
 
-                       //
-                       //  --- Constructors/Destructors
-                       //
-                       [MonoTODO]
-                       internal DomainUpDownItemCollection(DomainUpDown owner) : base()
+                       DomainUpDown owner;
+
+                       internal DomainUpDownItemCollection( DomainUpDown owner )
                        {
-                               
+                               this.owner = owner;
                        }
 
-                       
-                       //  --- Public Methods
-                       
-                       [MonoTODO]
-                       public override int Add(object value)
+                       public override int Add( object value )
                        {
-                               //FIXME:
-                               return base.Add(value);
+                               int index =  base.Add ( value );
+                               owner.itemAdded ( value );
+                               return index;
                        }
-                       [MonoTODO]
-                       public override void Insert(int index, object value)
+
+                       public override void Insert( int index, object value )
                        {
-                               //FIXME:
-                               base.Insert(index, value);
+                               base.Insert ( index, value );
+                               owner.itemInserted ( index, value );
                        }
-                       [MonoTODO]
-                       public override void Remove(object obj)
+
+                       public override void Remove( object obj )
                        {
-                               //FIXME:
-                               base.Remove(obj);
+                               base.Remove ( obj );
+                               owner.itemRemoved ( obj );
                        }
-                       [MonoTODO]
-                       public override void RemoveAt(int index)
+
+                       public override void RemoveAt( int index )
                        {
-                               //FIXME:
-                               base.RemoveAt(index);
+                               base.RemoveAt ( index );
+                               owner.itemRemoved ( index );
                        }
 
-                       
-                       //  --- Public Properties
-                                       
-                       public override object this[int index] {
-
+                       public override object this[ int index ]
+                       {
                                get {
-                                       throw new NotImplementedException ();
+                                       return base[index];
                                }
                                set {
-                                       //FIXME:
+                                       base[index] = value;
+                                       owner.itemChanged ( index );
                                }
                        }
                }