* Control.cs: ControlCollection.Count must be public. Fixed build of
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ColorDialog.cs
index e2bcd2c4f9208407b2ac7dc633cb23e45ca39b5c..1c478bce6e4b8ebade3ae08ed3231cd219fba8de 100644 (file)
@@ -37,7 +37,7 @@ namespace System.Windows.Forms {
                #region Local Variables
                private bool allowFullOpen = true;
                private bool anyColor = false;
-               private Color color = Color.Black;
+               private Color color;
                private int[] customColors = null;
                private bool fullOpen = false;
                private bool showHelp = false;
@@ -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;
                        
@@ -115,37 +113,31 @@ namespace System.Windows.Forms {
                        hueTextBox.Location = new Point (324, 203);
                        hueTextBox.Size = new Size (27, 21);
                        hueTextBox.TabIndex = 11;
-                       hueTextBox.TextAlign = HorizontalAlignment.Right;
                        hueTextBox.MaxLength = 3;
                        // satTextBox
                        satTextBox.Location = new Point (324, 225);
                        satTextBox.Size = new Size (27, 21);
                        satTextBox.TabIndex = 15;
-                       satTextBox.TextAlign = HorizontalAlignment.Right;
                        satTextBox.MaxLength = 3;
                        // greenTextBox
                        greenTextBox.Location = new Point (404, 225);
                        greenTextBox.Size = new Size (27, 21);
                        greenTextBox.TabIndex = 18;
-                       greenTextBox.TextAlign = HorizontalAlignment.Right;
                        greenTextBox.MaxLength = 3;
                        // briTextBox
                        briTextBox.Location = new Point (324, 247);
                        briTextBox.Size = new Size (27, 21);
                        briTextBox.TabIndex = 16;
-                       briTextBox.TextAlign = HorizontalAlignment.Right;
                        briTextBox.MaxLength = 3;
                        // blueTextBox
                        blueTextBox.Location = new Point (404, 247);
                        blueTextBox.Size = new Size (27, 21);
                        blueTextBox.TabIndex = 19;
-                       blueTextBox.TextAlign = HorizontalAlignment.Right;
                        blueTextBox.MaxLength = 3;
                        // redTextBox
                        redTextBox.Location = new Point (404, 203);
                        redTextBox.Size = new Size (27, 21);
                        redTextBox.TabIndex = 17;
-                       redTextBox.TextAlign = HorizontalAlignment.Right;
                        redTextBox.MaxLength = 3;
                        
                        // redLabel
@@ -202,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);
@@ -228,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);
@@ -283,27 +279,11 @@ namespace System.Windows.Forms {
                        
                        form.ResumeLayout (false);
                        
-                       brightnessControl.ColorToShow = selectedColorPanel.BackColor;
-                       
-                       redTextBox.Text = selectedColorPanel.BackColor.R.ToString ();
-                       greenTextBox.Text = selectedColorPanel.BackColor.G.ToString ();
-                       blueTextBox.Text = selectedColorPanel.BackColor.B.ToString ();
-                       
-                       HSB hsb = HSB.RGB2HSB (selectedColorPanel.BackColor);
-                       hueTextBox.Text = hsb.hue.ToString ();
-                       satTextBox.Text = hsb.sat.ToString ();
-                       briTextBox.Text = hsb.bri.ToString ();
-                       
-                       if (!allowFullOpen)
-                               defineColoursButton.Enabled = false;
-                       
-                       if (fullOpen)
-                               DoButtonDefineColours ();
+                       Color = Color.Black;
                        
                        defineColoursButton.Click += new EventHandler (OnClickButtonDefineColours);
                        addColoursButton.Click += new EventHandler (OnClickButtonAddColours);
-                       if (showHelp)
-                               helpButton.Click += new EventHandler (OnClickHelpButton);
+                       helpButton.Click += new EventHandler (OnClickHelpButton);
                        cancelButton.Click += new EventHandler (OnClickCancelButton);
                        okButton.Click += new EventHandler (OnClickOkButton);
                        
@@ -313,17 +293,46 @@ 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
                
                #region Public Instance Properties
                public Color Color {
                        get {
-                               return color;
+                               return selectedColorPanel.BackColor;
                        }
                        
                        set {
-                               color = value;
+                               if (value.IsEmpty) {
+                                       color = Color.Black;
+                                       baseColorControl.SetColor (color);
+                               } else if (color != value) {
+                                       color = value;
+                                       baseColorControl.SetColor (color);
+                               }
                        }
                }
                
@@ -334,7 +343,14 @@ namespace System.Windows.Forms {
                        }
                        
                        set {
-                               allowFullOpen = value;
+                               if (allowFullOpen != value) {
+                                       allowFullOpen = value;
+                                       
+                                       if (!allowFullOpen)
+                                               defineColoursButton.Enabled = false;
+                                       else
+                                               defineColoursButton.Enabled = true;
+                               }
                        }
                }
                
@@ -359,7 +375,19 @@ namespace System.Windows.Forms {
                        }
                        
                        set {
-                               fullOpen = value;
+                               if (fullOpen != value) {
+                                       fullOpen = value;
+                                       
+                                       if (fullOpen && allowFullOpen) {
+                                               defineColoursButton.Enabled = false;
+                                               colorMatrixControl.ColorToShow = baseColorControl.ColorToShow;
+                                               form.Size = GetFormSize (true);
+                                       } else {
+                                               if (allowFullOpen)
+                                                       defineColoursButton.Enabled = true;
+                                               form.Size = GetFormSize (false);
+                                       }
+                               }
                        }
                }
                
@@ -371,7 +399,15 @@ namespace System.Windows.Forms {
                        }
                        
                        set {
-                               customColors = value;
+                               if (value == null)
+                                       ResetCustomColors ();
+                               else {
+                                       int[] tmp_colors = value;
+                                       
+                                       Array.Copy (tmp_colors, customColors, tmp_colors.Length);
+                               }
+                                       
+                               baseColorControl.SetCustomColors ();
                        }
                }
                
@@ -382,11 +418,13 @@ namespace System.Windows.Forms {
                        }
                        
                        set {
-                               showHelp = value;
-                               if (showHelp)
-                                       helpButton.Show ();
-                               else
-                                       helpButton.Hide ();
+                               if (showHelp != value) {
+                                       showHelp = value;
+                                       if (showHelp)
+                                               helpButton.Show ();
+                                       else
+                                               helpButton.Hide ();
+                               }
                        }
                }
                
@@ -405,18 +443,18 @@ namespace System.Windows.Forms {
                #region Public Instance Methods
                public override void Reset ()
                {
-                       allowFullOpen = true;
+                       AllowFullOpen = true;
                        anyColor = false;
-                       color = Color.Black;
-                       customColors = null;
-                       fullOpen = false;
-                       showHelp = false;
+                       Color = Color.Black;
+                       CustomColors = null;
+                       FullOpen = false;
+                       ShowHelp = false;
                        solidColorOnly = false;
                }
                
                public override string ToString ()
                {
-                       return base.ToString () + ", Color: " + Color.ToString ();
+                       return base.ToString () + ",  Color: " + Color.ToString ();
                }
                #endregion      // Public Instance Methods
                
@@ -439,17 +477,27 @@ 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 ();
                        
-                       if (customColors != null)
-                               baseColorControl.SetCustomColors ();
-                       
                        return true;
                }
                #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;
@@ -460,57 +508,80 @@ namespace System.Windows.Forms {
                        form.DialogResult = DialogResult.OK;
                }
                
+               void OnClickButtonAddColours (object sender, EventArgs e)
+               {
+                       baseColorControl.SetUserColor (selectedColorPanel.BackColor);
+               }
+               
                void OnClickButtonDefineColours (object sender, EventArgs e)
                {
-                       DoButtonDefineColours ();
+                       if (allowFullOpen) {
+                               defineColoursButton.Enabled = false;
+                               colorMatrixControl.ColorToShow = baseColorControl.ColorToShow;
+                               form.Size = GetFormSize (true);
+                       }
                }
                
-               void DoButtonDefineColours ()
+               // FIXME: Is this correct ?
+               void OnClickHelpButton (object sender, EventArgs e)
                {
-                       defineColoursButton.Enabled = false;
-                       
-                       fullOpen = true;
-                       
-                       colorMatrixControl.ColorToShow = baseColorControl.ColorToShow;
-                       
-                       form.ClientSize = new Size (448, 332);
+                       OnHelpRequest (e);
                }
                
-               void OnClickButtonAddColours (object sender, EventArgs e)
+               string textBox_text_old = "";
+               
+               void OnGotFocusTextBoxes (object sender, EventArgs e)
                {
-                       baseColorControl.SetUserColor (selectedColorPanel.BackColor);
+                       TextBox textbox = sender as TextBox;
+                       textBox_text_old = textbox.Text;
                }
                
-               // FIXME: Is this correct ?
-               void OnClickHelpButton (object sender, EventArgs e)
+               void OnLostFocusTextBoxes (object sender, EventArgs e)
                {
-                       OnHelpRequest (e);
+                       TextBox textbox = sender as TextBox;
+                       
+                       if (textbox.Text.Length == 0)
+                               textbox.Text = textBox_text_old;
                }
                
-               // not working 100 %, S.W.F.TextBox isn't finished yet
                void OnKeyPressTextBoxes (object sender, KeyPressEventArgs e)
                {
-                       // accept only '0', '1', ... , '9'
-                       // 48 = '0', 57 = '9'
-                       if (e.KeyChar < (char)48 || e.KeyChar > (char)57)
+                       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
-               void TextChangedTextBoxes (object sender)
+               internal TextBox edit_textbox = null;
+               bool internal_textbox_change = false;
+               
+               void OnTextChangedTextBoxes (object sender, EventArgs e)
                {
-                       if (((TextBox)sender).Text.Length == 0)
+                       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) {
@@ -518,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) {
@@ -534,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) {
@@ -550,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 ();
@@ -566,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 ();
@@ -580,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 ();
@@ -594,36 +665,47 @@ namespace System.Windows.Forms {
                                        blueTextBox.Text = val.ToString ();
                                }
                                
+                               edit_textbox = blueTextBox;
+                               
                                UpdateFromRGBTextBoxes ();
                        }
+                       
+                       textBox_text_old = edit_textbox.Text;
+                       
+                       edit_textbox = null;
                }
                
-               private void UpdateControls (Color acolor)
+               internal void UpdateControls (Color acolor)
                {
-                       color = acolor;
                        selectedColorPanel.BackColor = acolor;
                        colorMatrixControl.ColorToShow = acolor;
                        brightnessControl.ColorToShow = acolor;
                        triangleControl.ColorToShow = acolor;
                }
                
-               private void UpdateRGBTextBoxes (Color color)
+               internal void UpdateRGBTextBoxes (Color acolor)
                {
-                       redTextBox.Text = color.R.ToString ();
-                       greenTextBox.Text = color.G.ToString ();
-                       blueTextBox.Text = color.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 ();
                }
                
-               private void UpdateHSBTextBoxes (Color color)
+               internal void UpdateHSBTextBoxes (Color acolor)
                {
-                       HSB hsb = HSB.RGB2HSB (color);
-                       
-                       hueTextBox.Text = hsb.hue.ToString ();
-                       satTextBox.Text = hsb.sat.ToString ();
-                       briTextBox.Text = hsb.bri.ToString ();
+                       HSB hsb = HSB.RGB2HSB (acolor);
+                       
+                       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 ();
                }
                
-               private void UpdateFromHSBTextBoxes ()
+               internal void UpdateFromHSBTextBoxes ()
                {
                        Color col = HSB.HSB2RGB (System.Convert.ToInt32 (hueTextBox.Text),
                                                 System.Convert.ToInt32 (satTextBox.Text),
@@ -633,7 +715,7 @@ namespace System.Windows.Forms {
                        UpdateRGBTextBoxes (col);
                }
                
-               private void UpdateFromRGBTextBoxes ()
+               internal void UpdateFromRGBTextBoxes ()
                {
                        Color col = Color.FromArgb (System.Convert.ToInt32 (redTextBox.Text),
                                                    System.Convert.ToInt32 (greenTextBox.Text),
@@ -643,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
@@ -659,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;
                        }
@@ -673,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;
@@ -685,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;
@@ -707,8 +799,8 @@ namespace System.Windows.Forms {
                                                d2 = (L <= 0.5f) ? L * (1.0f + S) : L + S - (L * S);
                                                d1 = 2.0f * L - d2;
                                                
-                                               float[] d3 = new float    [] { H + 1.0f / 3.0f , H, H - 1.0f / 3.0f };
-                                               float[] rgb = new float    [] { 0,0,0 };
+                                               float[] d3 = new float [] { H + 1.0f / 3.0f , H, H - 1.0f / 3.0f };
+                                               float[] rgb = new float [] { 0,0,0 };
                                                
                                                for (int i = 0; i < 3; i++) {
                                                        if (d3 [i] < 0)
@@ -761,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);
                        }
                        
@@ -784,7 +876,7 @@ namespace System.Windows.Forms {
                
                internal class BaseColorControl : Control {
                        internal class SmallColorControl : Control {
-                               private Color color;
+                               private Color internalcolor;
                                
                                private bool isSelected = false;
                                
@@ -792,7 +884,7 @@ namespace System.Windows.Forms {
                                {
                                        SuspendLayout ();
                                        
-                                       this.color = color;
+                                       this.internalcolor = color;
                                        
                                        Size = new Size (25, 23);
                                        
@@ -811,14 +903,14 @@ namespace System.Windows.Forms {
                                        }
                                }
                                
-                               public Color Color {
+                               public Color InternalColor {
                                        set {
-                                               color = value;
+                                               internalcolor = value;
                                                Invalidate ();
                                        }
                                        
                                        get {
-                                               return color;
+                                               return internalcolor;
                                        }
                                }
                                
@@ -826,12 +918,10 @@ namespace System.Windows.Forms {
                                {
                                        base.OnPaint (pe);
                                        
-                                       pe.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), 0, 0, 26, 23);
-                                       
-                                       pe.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (color),
+                                       pe.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (internalcolor),
                                                                   new Rectangle (4, 4, 17, 15));
                                        
-                                       DrawBorder (pe.Graphics, new Rectangle (4, 4, 17, 15));
+                                       ControlPaint.DrawBorder3D (pe.Graphics, 3, 3, 19, 17, Border3DStyle.Sunken);
                                        
                                        if (isSelected) {
                                                pe.Graphics.DrawRectangle (ThemeEngine.Current.ResPool.GetPen (Color.Black),
@@ -860,13 +950,6 @@ namespace System.Windows.Forms {
                                        
                                        base.OnLostFocus (e);
                                }
-                               
-                               private void DrawBorder (Graphics dc, Rectangle rect)
-                               {
-                                       Pen pen = ThemeEngine.Current.ResPool.GetPen (Color.Black);
-                                       dc.DrawLine (pen, rect.X, rect.Y, rect.X, rect.Bottom - 1);
-                                       dc.DrawLine (pen, rect.X + 1, rect.Y, rect.Right - 1, rect.Y);
-                               }
                        }
                        
                        private SmallColorControl[] smallColorControl;
@@ -879,7 +962,6 @@ namespace System.Windows.Forms {
                        private SmallColorControl selectedSmallColorControl;
                        
                        private int currentlyUsedUserSmallColorControl = 0;
-                       private int[] customColors = null;
                        
                        private ColorDialog colorDialog = null;
                        
@@ -906,7 +988,7 @@ namespace System.Windows.Forms {
                                userSmallColorControl [15] = new SmallColorControl (Color.White);
                                
                                smallColorControl = new SmallColorControl [48];
-                               smallColorControl [0] = new SmallColorControl (Color.FromArgb (((Byte)(255)), ((Byte)(128)), ((Byte)(138))));
+                               smallColorControl [0] = new SmallColorControl (Color.FromArgb (((Byte)(255)), ((Byte)(128)), ((Byte)(128))));
                                smallColorControl [1] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(128)), ((Byte)(64))));
                                smallColorControl [2] = new SmallColorControl (Color.Gray);
                                smallColorControl [3] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(0)), ((Byte)(255))));
@@ -1222,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);
@@ -1302,22 +1384,16 @@ namespace System.Windows.Forms {
                                Size = new Size (212, 238);
                                
                                ResumeLayout (false);
-                               
-                               selectedSmallColorControl = smallColorControl [46];  // default, Black
-                               selectedSmallColorControl.IsSelected = true;
-                               
-                               CheckIfColorIsInPanel ();
                        }
                        
-                       private void CheckIfColorIsInPanel ()
+                       private void CheckIfColorIsInPanel (Color color)
                        {
-                               if (colorDialog.Color != Color.Black) {
-                                       // check if we have a panel with a BackColor = ColorDialog.Color...
-                                       for (int i = 0; i < smallColorControl.Length; i++) {
-                                               if (smallColorControl [i].BackColor == colorDialog.Color) {
-                                                       selectedSmallColorControl = smallColorControl [i];
-                                                       break;
-                                               }
+                               // check if we have a panel with a BackColor = color...
+                               for (int i = 0; i < smallColorControl.Length; i++) {
+                                       if (smallColorControl [i].InternalColor == color) {
+                                               selectedSmallColorControl = smallColorControl [i];
+                                               selectedSmallColorControl.IsSelected = true;
+                                               break;
                                        }
                                }
                        }
@@ -1330,36 +1406,39 @@ namespace System.Windows.Forms {
                                
                                selectedSmallColorControl = (SmallColorControl)sender;
                                
-                               TriangleControl.CurrentBrightness = HSB.Brightness (selectedSmallColorControl.Color);
+                               TriangleControl.CurrentBrightness = HSB.Brightness (selectedSmallColorControl.InternalColor);
                                
-                               colorDialog.UpdateControls (selectedSmallColorControl.Color);
-                               colorDialog.UpdateRGBTextBoxes (selectedSmallColorControl.Color);
-                               colorDialog.UpdateHSBTextBoxes (selectedSmallColorControl.Color);
+                               colorDialog.UpdateControls (selectedSmallColorControl.InternalColor);
+                               colorDialog.UpdateRGBTextBoxes (selectedSmallColorControl.InternalColor);
+                               colorDialog.UpdateHSBTextBoxes (selectedSmallColorControl.InternalColor);
                        }
                        
                        public Color ColorToShow {
                                get {
-                                       return selectedSmallColorControl.Color;
+                                       return selectedSmallColorControl.InternalColor;
                                }
                        }
                        
-                       public void SetUserColor (Color col)
+                       
+                       public void SetColor (Color acolor)
                        {
-                               userSmallColorControl [currentlyUsedUserSmallColorControl].Color = col;
+                               if (selectedSmallColorControl != null)
+                                       selectedSmallColorControl.IsSelected = false;
                                
-                               // 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;
-                               }
+                               CheckIfColorIsInPanel (acolor);
                                
-                               customColors [currentlyUsedUserSmallColorControl] = col.ToArgb ();
+                               TriangleControl.CurrentBrightness = HSB.Brightness (acolor);
                                
-                               // update ColorDialog dialog property
-                               colorDialog.CustomColors = customColors;
+                               colorDialog.UpdateControls (acolor);
+                               colorDialog.UpdateRGBTextBoxes (acolor);
+                               colorDialog.UpdateHSBTextBoxes (acolor);
+                       }
+                       
+                       public void SetUserColor (Color col)
+                       {
+                               userSmallColorControl [currentlyUsedUserSmallColorControl].InternalColor = col;
+                               
+                               colorDialog.customColors [currentlyUsedUserSmallColorControl] = col.ToArgb ();
                                
                                currentlyUsedUserSmallColorControl++;
                                if (currentlyUsedUserSmallColorControl > 15)
@@ -1368,10 +1447,8 @@ namespace System.Windows.Forms {
                        
                        public void SetCustomColors ()
                        {
-                               int[] customColors = colorDialog.CustomColors;
-                               
-                               for (int i = 0; i < customColors.Length; i++) {
-                                       userSmallColorControl [i].Color = Color.FromArgb (customColors [i]);
+                               for (int i = 0; i < colorDialog.customColors.Length; i++) {
+                                       userSmallColorControl [i].InternalColor = Color.FromArgb (colorDialog.customColors [i]);
                                }
                        }
                }
@@ -1380,19 +1457,19 @@ namespace System.Windows.Forms {
                        internal class DrawingBitmap {
                                private Bitmap bitmap;
                                
-                               public DrawingBitmap ()
+                               public DrawingBitmap (Size size)
                                {
-                                       bitmap = new Bitmap (180, 191);
+                                       bitmap = new Bitmap (size.Width, size.Height);
                                        
-                                       float hueadd = 241.0f / 178.0f;
-                                       float satsub = 241.0f / 189.0f;
+                                       float hueadd = 240.0f / (size.Width - 1);
+                                       float satsub = 241.0f / (size.Height - 1);
                                        float satpos = 240.0f;
                                        
                                        // paint the matrix to the bitmap
-                                       for (int height = 0; height < 191; height++) {
+                                       for (int height = 0; height < size.Height; height++) {
                                                float huepos = 0.0f;
                                                
-                                               for (int width = 0; width < 180; width++) {
+                                               for (int width = 0; width < size.Width; width++) {
                                                        HSB hsb = new HSB ();
                                                        
                                                        hsb.hue = (int)huepos;
@@ -1466,7 +1543,7 @@ namespace System.Windows.Forms {
                                }
                        }
                        
-                       private DrawingBitmap drawingBitmap = new DrawingBitmap( );
+                       private DrawingBitmap drawingBitmap = null;
                        
                        private CrossCursor crossCursor = new CrossCursor();
                        
@@ -1479,8 +1556,8 @@ namespace System.Windows.Forms {
                        private int currentXPos;
                        private int currentYPos;
                        
-                       private const float xstep = 240.0f/178.0f;
-                       private const float ystep = 240.0f/189.0f;
+                       private float xstep;
+                       private float ystep;
                        
                        private ColorDialog colorDialog;
                        
@@ -1495,11 +1572,12 @@ namespace System.Windows.Forms {
                                Size = new Size (179, 190);
                                TabIndex = 0;
                                TabStop = false;
-                               //BackColor = SystemColors.Control;
-                               Size = new Size (179, 190);
                                
                                ResumeLayout (false);
                                
+                               xstep = 240.0f / (ClientSize.Width - 1);
+                               ystep = 241.0f / (ClientSize.Height - 1);
+                               
                                SetStyle (ControlStyles.DoubleBuffer, true);
                                SetStyle (ControlStyles.AllPaintingInWmPaint, true);
                                SetStyle (ControlStyles.UserPaint, true);
@@ -1507,6 +1585,9 @@ namespace System.Windows.Forms {
                        
                        protected override void OnPaint (PaintEventArgs e)
                        {
+                               if (drawingBitmap == null)
+                                       drawingBitmap = new DrawingBitmap (ClientSize);
+                               
                                Draw (e);
                                
                                base.OnPaint (e);
@@ -1514,7 +1595,7 @@ namespace System.Windows.Forms {
                        
                        private void Draw (PaintEventArgs e)
                        {
-                               e.Graphics.DrawImage (drawingBitmap.Bitmap, 0, 0);
+                               e.Graphics.DrawImage (drawingBitmap.Bitmap, ClientRectangle.X, ClientRectangle.Y);
                                
                                // drawCross is false if the mouse gets moved...
                                if (drawCross) {
@@ -1534,6 +1615,7 @@ namespace System.Windows.Forms {
                                }
                                
                                UpdateControls ();
+                               XplatUI.GrabWindow (Handle, Handle);
                                
                                base.OnMouseDown (e);
                        }
@@ -1541,7 +1623,7 @@ namespace System.Windows.Forms {
                        protected override void OnMouseMove (MouseEventArgs e)
                        {
                                if (mouseButtonDown)
-                                       if ((e.X < 178 && e.X >= 0) && (e.Y < 189 && e.Y >= 0)) {
+                                       if ((e.X < ClientSize.Width && e.X >= 0) && (e.Y < ClientSize.Height && e.Y >= 0)) {
                                                currentXPos = e.X;
                                                currentYPos = e.Y;
                                                UpdateControls ();
@@ -1552,6 +1634,7 @@ namespace System.Windows.Forms {
                        
                        protected override void OnMouseUp (MouseEventArgs e)
                        {
+                               XplatUI.UngrabWindow (Handle);
                                mouseButtonDown = false;
                                drawCross = true;
                                Invalidate ();
@@ -1560,12 +1643,19 @@ namespace System.Windows.Forms {
                        
                        public Color ColorToShow {
                                set {
-                                       color = value;
+                                       ComputePos (value);
+                               }
+                       }
+                       
+                       private void ComputePos (Color acolor)
+                       {
+                               if (acolor != color) {
+                                       color = acolor;
                                        
                                        HSB hsb = HSB.RGB2HSB (color);
                                        
                                        currentXPos = (int)((float)hsb.hue / xstep);
-                                       currentYPos = 189 - (int)((float)hsb.sat / ystep);
+                                       currentYPos = ClientSize.Height - 1 - (int)((float)hsb.sat / ystep);
                                        
                                        if (currentXPos < 0)
                                                currentXPos = 0;
@@ -1574,8 +1664,6 @@ namespace System.Windows.Forms {
                                        
                                        Invalidate ();
                                        Update ();
-                                       
-                                       UpdateControls ();
                                }
                        }
                        
@@ -1597,11 +1685,13 @@ namespace System.Windows.Forms {
                                
                                // update saturation text box
                                int satvalue = (240 - ((int)((float)currentYPos * ystep)));
-                               satvalue = satvalue == 240 ? 239 : satvalue;
                                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;
@@ -1635,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++) {
@@ -1648,17 +1738,15 @@ namespace System.Windows.Forms {
                                }
                        }
                        
-                       private const float step = 240.0f/189.0f;
+                       private const float step = 241.0f/189.0f;
                        
                        private DrawingBitmap bitmap;
                        
-                       private Color color;
-                       
-                       private ColorDialog colorDialog;
+                       private ColorDialog colorDialog = null;
                        
-                       public BrightnessControl (ColorDialog colorDialogPanel)
+                       public BrightnessControl (ColorDialog colorDialog)
                        {
-                               this.colorDialog = colorDialogPanel;
+                               this.colorDialog = colorDialog;
                                
                                bitmap = new DrawingBitmap ();
                                
@@ -1670,6 +1758,7 @@ namespace System.Windows.Forms {
                                TabIndex = 0;
                                TabStop = false;
                                Size = new Size (14, 190);
+                               
                                ResumeLayout (false);
                                
                                SetStyle (ControlStyles.DoubleBuffer, true);
@@ -1719,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;
                        
@@ -1825,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;
                                        
@@ -1837,6 +1926,7 @@ namespace System.Windows.Forms {
                                        currentTrianglePosition = 186 - (int)tmp + 9;
                                        
                                        colorDialog.briTextBox.Text = TrianglePosition.ToString ();
+                                       
                                        colorDialog.UpdateFromHSBTextBoxes ();
                                        
                                        Invalidate ();
@@ -1846,9 +1936,22 @@ namespace System.Windows.Forms {
                        
                        public Color ColorToShow {
                                set {
-                                       TrianglePosition = HSB.Brightness (value);
+                                       SetColor (value);
                                }
                        }
+                       
+                       public void SetColor (Color color)
+                       {
+                               int pos_raw = HSB.Brightness (color);
+                               
+                               float tmp = (float)pos_raw / briStep;
+                               currentTrianglePosition = 186 - (int)tmp + 9;
+                               
+                               if (colorDialog.edit_textbox == null)
+                                       colorDialog.briTextBox.Text = TrianglePosition.ToString ();
+                               
+                               Invalidate ();
+                       }
                }
                #endregion
        }