* Control.cs: ControlCollection.Count must be public. Fixed build of
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ColorDialog.cs
index 9c9c417e58511abd7bd67964cc804094733471f5..1c478bce6e4b8ebade3ae08ed3231cd219fba8de 100644 (file)
@@ -78,8 +78,6 @@ namespace System.Windows.Forms {
                        
                        form.Text = "Color";
                        
-                       form.Size = new Size (221, 332); // 300
-                       
                        form.FormBorderStyle = FormBorderStyle.FixedDialog;
                        form.MaximizeBox = false;
                        
@@ -196,7 +194,9 @@ namespace System.Windows.Forms {
                        defineColoursButton.Location = new Point (5, 244);
                        defineColoursButton.Size = new Size (210, 22);
                        defineColoursButton.TabIndex = 6;
-                       defineColoursButton.Text = Locale.GetText ("Define Colours >>");
+                       // FIXME: update resource files
+                       defineColoursButton.Text = "Define Custom Colors >>";
+                       //defineColoursButton.Text = Locale.GetText ("Define Custom Colors >>");
                        // okButton
                        okButton.FlatStyle = FlatStyle.System;
                        okButton.Location = new Point (5, 271);
@@ -222,7 +222,9 @@ namespace System.Windows.Forms {
                        addColoursButton.Location = new Point (227, 271);
                        addColoursButton.Size = new Size (213, 22);
                        addColoursButton.TabIndex = 7;
-                       addColoursButton.Text =  Locale.GetText ("Add Colours");
+                       // FIXME: update resource files
+                       addColoursButton.Text =  "Add To Custom Colors";
+                       //addColoursButton.Text =  Locale.GetText ("Add To Custom Colors");
                        
                        // baseColorControl
                        baseColorControl.Location = new Point (3, 6);
@@ -291,6 +293,29 @@ namespace System.Windows.Forms {
                        redTextBox.KeyPress += new KeyPressEventHandler (OnKeyPressTextBoxes);
                        greenTextBox.KeyPress += new KeyPressEventHandler (OnKeyPressTextBoxes);
                        blueTextBox.KeyPress += new KeyPressEventHandler (OnKeyPressTextBoxes);
+                       
+                       hueTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
+                       satTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
+                       briTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
+                       redTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
+                       greenTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
+                       blueTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
+                       
+                       hueTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
+                       satTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
+                       briTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
+                       redTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
+                       greenTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
+                       blueTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
+                       
+                       hueTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
+                       satTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
+                       briTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
+                       redTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
+                       greenTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
+                       blueTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
+                       
+                       ResetCustomColors ();
                }
                #endregion      // Public Constructors
                
@@ -301,9 +326,11 @@ namespace System.Windows.Forms {
                        }
                        
                        set {
-                               if (color != value) {
+                               if (value.IsEmpty) {
+                                       color = Color.Black;
+                                       baseColorControl.SetColor (color);
+                               } else if (color != value) {
                                        color = value;
-                                       
                                        baseColorControl.SetColor (color);
                                }
                        }
@@ -353,15 +380,12 @@ namespace System.Windows.Forms {
                                        
                                        if (fullOpen && allowFullOpen) {
                                                defineColoursButton.Enabled = false;
-                                               
                                                colorMatrixControl.ColorToShow = baseColorControl.ColorToShow;
-                                               
-                                               form.Size = new Size (448, 332);
+                                               form.Size = GetFormSize (true);
                                        } else {
                                                if (allowFullOpen)
                                                        defineColoursButton.Enabled = true;
-                                               
-                                               form.Size = new Size (221, 332); // 300
+                                               form.Size = GetFormSize (false);
                                        }
                                }
                        }
@@ -375,11 +399,15 @@ namespace System.Windows.Forms {
                        }
                        
                        set {
-                               if (allowFullOpen) {
-                                       customColors = value;
+                               if (value == null)
+                                       ResetCustomColors ();
+                               else {
+                                       int[] tmp_colors = value;
                                        
-                                       baseColorControl.SetCustomColors ();
+                                       Array.Copy (tmp_colors, customColors, tmp_colors.Length);
                                }
+                                       
+                               baseColorControl.SetCustomColors ();
                        }
                }
                
@@ -426,7 +454,7 @@ namespace System.Windows.Forms {
                
                public override string ToString ()
                {
-                       return base.ToString () + ", Color: " + Color.ToString ();
+                       return base.ToString () + ",  Color: " + Color.ToString ();
                }
                #endregion      // Public Instance Methods
                
@@ -449,6 +477,10 @@ namespace System.Windows.Forms {
                #region Protected Instance Methods
                protected override bool RunDialog (IntPtr hwndOwner)
                {
+                       defineColoursButton.Enabled = (AllowFullOpen && !FullOpen);
+                       defineColoursButton.Refresh ();
+
+                       form.Size = GetFormSize (FullOpen && AllowFullOpen);
                        // currently needed, otherwise there are a lot of drawing artefacts/transparent controls if the same dialog gets opened again
                        form.Refresh ();
                        
@@ -456,7 +488,16 @@ namespace System.Windows.Forms {
                }
                #endregion      // Protected Instance Methods
                
-               #region Private Methods         
+               #region Private Methods
+
+               Size GetFormSize (bool fullOpen)
+               {
+                       if (fullOpen)
+                               return new Size (448, 332);
+                       else
+                               return new Size (221, 332); // 300
+               }
+
                void OnClickCancelButton (object sender, EventArgs e)
                {
                        form.DialogResult = DialogResult.Cancel;
@@ -476,10 +517,8 @@ namespace System.Windows.Forms {
                {
                        if (allowFullOpen) {
                                defineColoursButton.Enabled = false;
-                               
                                colorMatrixControl.ColorToShow = baseColorControl.ColorToShow;
-                               
-                               form.Size = new Size (448, 332);
+                               form.Size = GetFormSize (true);
                        }
                }
                
@@ -489,33 +528,60 @@ namespace System.Windows.Forms {
                        OnHelpRequest (e);
                }
                
-               // not working 100 %, S.W.F.TextBox isn't finished yet
+               string textBox_text_old = "";
+               
+               void OnGotFocusTextBoxes (object sender, EventArgs e)
+               {
+                       TextBox textbox = sender as TextBox;
+                       textBox_text_old = textbox.Text;
+               }
+               
+               void OnLostFocusTextBoxes (object sender, EventArgs e)
+               {
+                       TextBox textbox = sender as TextBox;
+                       
+                       if (textbox.Text.Length == 0)
+                               textbox.Text = textBox_text_old;
+               }
+               
                void OnKeyPressTextBoxes (object sender, KeyPressEventArgs e)
                {
-                       if (Char.IsLetter (e.KeyChar) || Char.IsWhiteSpace (e.KeyChar) || Char.IsPunctuation (e.KeyChar)) {
+                       if (Char.IsLetter (e.KeyChar) || Char.IsWhiteSpace (e.KeyChar) || Char.IsPunctuation (e.KeyChar) || e.KeyChar == ',') {
                                e.Handled = true;
                                return; 
                        }
                        
-                       TextChangedTextBoxes (sender);
+                       internal_textbox_change = true;
                }
                
-               // not working 100 %, S.W.F.TextBox isn't finished yet
-               // setting TextBox.Maxlength gives us weird results
-               void TextChangedTextBoxes (object sender)
+               internal TextBox edit_textbox = null;
+               bool internal_textbox_change = false;
+               
+               void OnTextChangedTextBoxes (object sender, EventArgs e)
                {
+                       if (!internal_textbox_change)
+                               return;
+                       
+                       internal_textbox_change = false;
+                       
                        TextBox tmp_box = sender as TextBox;
                        
                        if (tmp_box.Text.Length == 0)
                                return;
                        
-                       int val;
+                       string text = tmp_box.Text;
+                       
+                       int val = 0;
+                       
+                       try {
+                               val = System.Convert.ToInt32 (text);
+                       } catch (Exception) {
+                               // bla
+                       }
                        
                        if (sender == hueTextBox) {
-                               val = System.Convert.ToInt32 (hueTextBox.Text);
-                               
-                               if (val > 240) {
-                                       val = 240;
+                               if (val > 239) {
+                                       val = 239;
                                        hueTextBox.Text = val.ToString ();
                                } else
                                if (val < 0) {
@@ -523,15 +589,15 @@ namespace System.Windows.Forms {
                                        hueTextBox.Text = val.ToString ();
                                }
                                
+                               edit_textbox = hueTextBox;
+                               
                                UpdateFromHSBTextBoxes ();
                                
                                UpdateControls (selectedColorPanel.BackColor);
                        } else
                        if (sender == satTextBox) {
-                               val = System.Convert.ToInt32 (satTextBox.Text);
-                               
-                               if (val > 239) {
-                                       val = 239;
+                               if (val > 240) {
+                                       val = 240;
                                        satTextBox.Text = val.ToString ();
                                } else
                                if (val < 0) {
@@ -539,15 +605,15 @@ namespace System.Windows.Forms {
                                        satTextBox.Text = val.ToString ();
                                }
                                
+                               edit_textbox = satTextBox;
+                               
                                UpdateFromHSBTextBoxes ();
                                
                                UpdateControls (selectedColorPanel.BackColor);
                        } else
                        if (sender == briTextBox) {
-                               val = System.Convert.ToInt32 (briTextBox.Text);
-                               
-                               if (val > 239) {
-                                       val = 239;
+                               if (val > 240) {
+                                       val = 240;
                                        briTextBox.Text = val.ToString ();
                                } else
                                if (val < 0) {
@@ -555,13 +621,13 @@ namespace System.Windows.Forms {
                                        briTextBox.Text = val.ToString ();
                                }
                                
+                               edit_textbox = briTextBox;
+                               
                                UpdateFromHSBTextBoxes ();
                                
                                UpdateControls (selectedColorPanel.BackColor);
                        } else
                        if (sender == redTextBox) {
-                               val = System.Convert.ToInt32 (redTextBox.Text);
-                               
                                if (val > 255) {
                                        val = 255;
                                        redTextBox.Text = val.ToString ();
@@ -571,11 +637,11 @@ namespace System.Windows.Forms {
                                        redTextBox.Text = val.ToString ();
                                }
                                
+                               edit_textbox = redTextBox;
+                               
                                UpdateFromRGBTextBoxes ();
                        } else
                        if (sender == greenTextBox) {
-                               val = System.Convert.ToInt32 (greenTextBox.Text);
-                               
                                if (val > 255) {
                                        val = 255;
                                        greenTextBox.Text = val.ToString ();
@@ -585,11 +651,11 @@ namespace System.Windows.Forms {
                                        greenTextBox.Text = val.ToString ();
                                }
                                
+                               edit_textbox = greenTextBox;
+                               
                                UpdateFromRGBTextBoxes ();
                        } else
                        if (sender == blueTextBox) {
-                               val = System.Convert.ToInt32 (blueTextBox.Text);
-                               
                                if (val > 255) {
                                        val = 255;
                                        blueTextBox.Text = val.ToString ();
@@ -599,8 +665,14 @@ namespace System.Windows.Forms {
                                        blueTextBox.Text = val.ToString ();
                                }
                                
+                               edit_textbox = blueTextBox;
+                               
                                UpdateFromRGBTextBoxes ();
                        }
+                       
+                       textBox_text_old = edit_textbox.Text;
+                       
+                       edit_textbox = null;
                }
                
                internal void UpdateControls (Color acolor)
@@ -613,18 +685,24 @@ namespace System.Windows.Forms {
                
                internal void UpdateRGBTextBoxes (Color acolor)
                {
-                       redTextBox.Text = acolor.R.ToString ();
-                       greenTextBox.Text = acolor.G.ToString ();
-                       blueTextBox.Text = acolor.B.ToString ();
+                       if (edit_textbox != redTextBox)
+                               redTextBox.Text = acolor.R.ToString ();
+                       if (edit_textbox != greenTextBox)
+                               greenTextBox.Text = acolor.G.ToString ();
+                       if (edit_textbox != blueTextBox)
+                               blueTextBox.Text = acolor.B.ToString ();
                }
                
                internal void UpdateHSBTextBoxes (Color acolor)
                {
                        HSB hsb = HSB.RGB2HSB (acolor);
                        
-                       hueTextBox.Text = hsb.hue.ToString ();
-                       satTextBox.Text = hsb.sat.ToString ();
-                       briTextBox.Text = hsb.bri.ToString ();
+                       if (edit_textbox != hueTextBox)
+                               hueTextBox.Text = hsb.hue.ToString ();
+                       if (edit_textbox != satTextBox)
+                               satTextBox.Text = hsb.sat.ToString ();
+                       if (edit_textbox != briTextBox)
+                               briTextBox.Text = hsb.bri.ToString ();
                }
                
                internal void UpdateFromHSBTextBoxes ()
@@ -647,10 +725,20 @@ namespace System.Windows.Forms {
                        
                        UpdateHSBTextBoxes (col);
                        
-                       UpdateFromHSBTextBoxes ();
-                       
                        UpdateControls (col);
                }
+               
+               void ResetCustomColors ()
+               {
+                       // check if this.customColors already exists
+                       if (customColors == null)
+                               customColors = new int [16];
+                       
+                       int default_color = Color.FromArgb(0, 255, 255, 255).ToArgb ();
+                               
+                       for (int i = 0; i < customColors.Length; i++)
+                               customColors [i] = default_color;
+               }
                #endregion
                
                #region Internal structs and classes
@@ -663,13 +751,13 @@ namespace System.Windows.Forms {
                        {
                                HSB hsb = new HSB ();
                                
-                               hsb.hue = (int)((color.GetHue () / 360.0f) * 241);
+                               hsb.hue = (int)((color.GetHue () / 360.0f) * 240);
                                hsb.sat = (int)(color.GetSaturation () * 241);
-                               hsb.bri = (int)(color.GetBrightness () * 240);
+                               hsb.bri = (int)(color.GetBrightness () * 241);
                                
-                               if (hsb.hue > 240) hsb.hue = 240;
+                               if (hsb.hue > 239) hsb.hue = 239;
                                if (hsb.sat > 240) hsb.sat = 240;
-                               if (hsb.bri > 239) hsb.bri = 239;
+                               if (hsb.bri > 240) hsb.bri = 240;
                                
                                return hsb;
                        }
@@ -677,8 +765,8 @@ namespace System.Windows.Forms {
                        // not using ControlPaint HBS2Color, this algo is more precise
                        public static Color HSB2RGB (int hue, int saturation, int brightness)
                        {
-                               if (hue > 240)
-                                       hue = 240;
+                               if (hue > 239)
+                                       hue = 239;
                                else
                                if (hue < 0)
                                        hue = 0;
@@ -689,15 +777,15 @@ namespace System.Windows.Forms {
                                if (saturation < 0)
                                        saturation = 0;
                                
-                               if (brightness > 239)
-                                       brightness = 239;
+                               if (brightness > 240)
+                                       brightness = 240;
                                else
                                if (brightness < 0)
                                        brightness = 0;
                                
-                               float H = hue / 240.0f;
+                               float H = hue / 239.0f;
                                float S = saturation / 240.0f;
-                               float L = brightness / 239.0f;
+                               float L = brightness / 240.0f;
                                
                                float r = 0, g = 0, b = 0;
                                float d1, d2;
@@ -765,12 +853,12 @@ namespace System.Windows.Forms {
                        
                        public static int Brightness (Color color)
                        {
-                               return (int)(color.GetBrightness () * 240);
+                               return (int)(color.GetBrightness () * 241);
                        }
                        
                        public static void GetHueSaturation (Color color, out int hue, out int sat)
                        {
-                               hue = (int)((color.GetHue () / 360.0f) * 241);
+                               hue = (int)((color.GetHue () / 360.0f) * 240);
                                sat = (int)(color.GetSaturation () * 241);
                        }
                        
@@ -874,7 +962,6 @@ namespace System.Windows.Forms {
                        private SmallColorControl selectedSmallColorControl;
                        
                        private int currentlyUsedUserSmallColorControl = 0;
-                       private int[] customColors = null;
                        
                        private ColorDialog colorDialog = null;
                        
@@ -1217,7 +1304,7 @@ namespace System.Windows.Forms {
                                baseColorLabel.Location = new Point (2, 0);
                                baseColorLabel.Size = new Size (200, 12);
                                baseColorLabel.TabIndex = 5;
-                               baseColorLabel.Text = Locale.GetText ("Base Colours") + ":";
+                               baseColorLabel.Text = Locale.GetText ("Base Colors") + ":";
                                // userColorLabel
                                userColorLabel.FlatStyle = FlatStyle.System;
                                userColorLabel.Location = new Point (2, 164);
@@ -1351,19 +1438,7 @@ namespace System.Windows.Forms {
                        {
                                userSmallColorControl [currentlyUsedUserSmallColorControl].InternalColor = col;
                                
-                               // check if this.customColors already exists
-                               if (customColors == null) {
-                                       customColors = new int [16];
-                                       int white = Color.White.ToArgb ();
-                                       
-                                       for (int i = 0; i < customColors.Length; i++)
-                                               customColors [i] = white;
-                               }
-                               
-                               customColors [currentlyUsedUserSmallColorControl] = col.ToArgb ();
-                               
-                               // update ColorDialog dialog property
-                               colorDialog.CustomColors = customColors;
+                               colorDialog.customColors [currentlyUsedUserSmallColorControl] = col.ToArgb ();
                                
                                currentlyUsedUserSmallColorControl++;
                                if (currentlyUsedUserSmallColorControl > 15)
@@ -1372,16 +1447,8 @@ namespace System.Windows.Forms {
                        
                        public void SetCustomColors ()
                        {
-                               int[] customColors = colorDialog.CustomColors;
-                               
-                               if (customColors != null) {
-                                       for (int i = 0; i < customColors.Length; i++) {
-                                               userSmallColorControl [i].InternalColor = Color.FromArgb (customColors [i]);
-                                       }
-                               } else {
-                                       for (int i = 0; i < userSmallColorControl.Length; i++) {
-                                               userSmallColorControl [i].InternalColor = Color.White;
-                                       }
+                               for (int i = 0; i < colorDialog.customColors.Length; i++) {
+                                       userSmallColorControl [i].InternalColor = Color.FromArgb (colorDialog.customColors [i]);
                                }
                        }
                }
@@ -1394,7 +1461,7 @@ namespace System.Windows.Forms {
                                {
                                        bitmap = new Bitmap (size.Width, size.Height);
                                        
-                                       float hueadd = 241.0f / (size.Width - 1);
+                                       float hueadd = 240.0f / (size.Width - 1);
                                        float satsub = 241.0f / (size.Height - 1);
                                        float satpos = 240.0f;
                                        
@@ -1508,7 +1575,7 @@ namespace System.Windows.Forms {
                                
                                ResumeLayout (false);
                                
-                               xstep = 241.0f / (ClientSize.Width - 1);
+                               xstep = 240.0f / (ClientSize.Width - 1);
                                ystep = 241.0f / (ClientSize.Height - 1);
                                
                                SetStyle (ControlStyles.DoubleBuffer, true);
@@ -1548,6 +1615,7 @@ namespace System.Windows.Forms {
                                }
                                
                                UpdateControls ();
+                               XplatUI.GrabWindow (Handle, Handle);
                                
                                base.OnMouseDown (e);
                        }
@@ -1566,6 +1634,7 @@ namespace System.Windows.Forms {
                        
                        protected override void OnMouseUp (MouseEventArgs e)
                        {
+                               XplatUI.UngrabWindow (Handle);
                                mouseButtonDown = false;
                                drawCross = true;
                                Invalidate ();
@@ -1619,7 +1688,10 @@ namespace System.Windows.Forms {
                                colorDialog.satTextBox.Text = satvalue.ToString ();
                                
                                // update hue text box
-                               colorDialog.hueTextBox.Text = ((int)((float)currentXPos * xstep)).ToString ();
+                               int huevalue = (int)((float)currentXPos * xstep);
+                               if (huevalue > 239)
+                                       huevalue = 239;
+                               colorDialog.hueTextBox.Text = huevalue.ToString ();
                                
                                // update the main selected color panel
                                colorDialog.selectedColorPanel.BackColor = tmpColor;
@@ -1653,8 +1725,8 @@ namespace System.Windows.Forms {
                                // color will be computed with an iteration
                                public void Draw (int hue, int sat)
                                {
-                                       float brisub = 240.0f / 190.0f;
-                                       float bri = 240.0f;
+                                       float brisub = 241.0f / 190.0f;
+                                       float bri = 241.0f;
                                        
                                        for (int height = 0; height < 190; height++) {
                                                for (int width = 0; width < 14; width++) {
@@ -1666,7 +1738,7 @@ namespace System.Windows.Forms {
                                }
                        }
                        
-                       private const float step = 240.0f/189.0f;
+                       private const float step = 241.0f/189.0f;
                        
                        private DrawingBitmap bitmap;
                        
@@ -1736,7 +1808,7 @@ namespace System.Windows.Forms {
                        private int currentTrianglePosition = 195;
 //                     private Rectangle clipRectangle;
                        
-                       private const float briStep = 239.0f/186.0f;
+                       private const float briStep = 241.0f/186.0f;
                        
                        private static int currentBrightness = 0;
                        
@@ -1842,7 +1914,7 @@ namespace System.Windows.Forms {
                                        float tmp = (float)(currentTrianglePosition - 9);
                                        tmp = tmp * briStep;
                                        
-                                       int retval = 239 - (int)tmp;
+                                       int retval = 240 - (int)tmp;
                                        
                                        TriangleControl.CurrentBrightness = retval;
                                        
@@ -1875,7 +1947,8 @@ namespace System.Windows.Forms {
                                float tmp = (float)pos_raw / briStep;
                                currentTrianglePosition = 186 - (int)tmp + 9;
                                
-                               colorDialog.briTextBox.Text = TrianglePosition.ToString ();
+                               if (colorDialog.edit_textbox == null)
+                                       colorDialog.briTextBox.Text = TrianglePosition.ToString ();
                                
                                Invalidate ();
                        }