Merge pull request #268 from pcc/menudeactivate
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ColorDialog.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2006 Alexander Olk
21 //
22 // Authors:
23 //      Alexander Olk   alex.olk@googlemail.com
24 //
25
26 // COMPLETE
27
28 using System.ComponentModel;
29 using System.Drawing;
30 using System.Globalization;
31 using System.Resources;
32 using System;
33
34 namespace System.Windows.Forms {
35         [DefaultProperty ("Color")]
36         public class ColorDialog : CommonDialog {
37                 #region Local Variables
38                 private bool allowFullOpen = true;
39                 private bool anyColor = false;
40                 private Color color;
41                 private int[] customColors = null;
42                 private bool fullOpen = false;
43                 private bool showHelp = false;
44                 private bool solidColorOnly = false;
45                 
46                 private Panel selectedColorPanel;
47                 private BaseColorControl baseColorControl;
48                 private ColorMatrixControl colorMatrixControl;
49                 private BrightnessControl brightnessControl;
50                 private TriangleControl triangleControl;
51                 
52                 private Button okButton;
53                 private Button cancelButton;
54                 private Button helpButton;
55                 private Button addColoursButton;
56                 private Button defineColoursButton;
57                 
58                 private TextBox hueTextBox;
59                 private TextBox satTextBox;
60                 private TextBox briTextBox;
61                 private TextBox redTextBox;
62                 private TextBox greenTextBox;
63                 private TextBox blueTextBox;
64                 
65                 private Label briLabel;
66                 private Label satLabel;
67                 private Label hueLabel;
68                 private Label colorBaseLabel;
69                 private Label greenLabel;
70                 private Label blueLabel;
71                 private Label redLabel;
72                 #endregion      // Local Variables
73                 
74                 #region Public Constructors
75                 public ColorDialog () : base()
76                 {
77                         form = new DialogForm (this);
78                         form.SuspendLayout ();
79                         
80                         form.Text = "Color";
81                         
82                         form.FormBorderStyle = FormBorderStyle.FixedDialog;
83                         form.MaximizeBox = false;
84                         
85                         satTextBox = new TextBox ();
86                         briTextBox = new TextBox ();
87                         blueTextBox = new TextBox ();
88                         greenTextBox = new TextBox ();
89                         redTextBox = new TextBox ();
90                         hueTextBox = new TextBox ();
91                         
92                         redLabel = new Label ();
93                         blueLabel = new Label ();
94                         greenLabel = new Label ();
95                         colorBaseLabel = new Label ();
96                         hueLabel = new Label ();
97                         satLabel = new Label ();
98                         briLabel = new Label ();
99                         
100                         okButton = new Button ();
101                         cancelButton = new Button ();
102                         form.CancelButton = cancelButton;
103                         helpButton = new Button ();
104                         defineColoursButton = new Button ();
105                         addColoursButton = new Button ();
106                         
107                         baseColorControl = new BaseColorControl (this);
108                         colorMatrixControl = new ColorMatrixControl (this);
109                         brightnessControl = new BrightnessControl (this);
110                         triangleControl = new TriangleControl (this);
111                         
112                         selectedColorPanel = new Panel ();
113                         
114                         // hueTextBox
115                         hueTextBox.Location = new Point (324, 203);
116                         hueTextBox.Size = new Size (27, 21);
117                         hueTextBox.TabIndex = 11;
118                         hueTextBox.MaxLength = 3;
119                         // satTextBox
120                         satTextBox.Location = new Point (324, 225);
121                         satTextBox.Size = new Size (27, 21);
122                         satTextBox.TabIndex = 15;
123                         satTextBox.MaxLength = 3;
124                         // greenTextBox
125                         greenTextBox.Location = new Point (404, 225);
126                         greenTextBox.Size = new Size (27, 21);
127                         greenTextBox.TabIndex = 18;
128                         greenTextBox.MaxLength = 3;
129                         // briTextBox
130                         briTextBox.Location = new Point (324, 247);
131                         briTextBox.Size = new Size (27, 21);
132                         briTextBox.TabIndex = 16;
133                         briTextBox.MaxLength = 3;
134                         // blueTextBox
135                         blueTextBox.Location = new Point (404, 247);
136                         blueTextBox.Size = new Size (27, 21);
137                         blueTextBox.TabIndex = 19;
138                         blueTextBox.MaxLength = 3;
139                         // redTextBox
140                         redTextBox.Location = new Point (404, 203);
141                         redTextBox.Size = new Size (27, 21);
142                         redTextBox.TabIndex = 17;
143                         redTextBox.MaxLength = 3;
144                         
145                         // redLabel
146                         redLabel.FlatStyle = FlatStyle.System;
147                         redLabel.Location = new Point (361, 206);
148                         redLabel.Size = new Size (40, 16);
149                         redLabel.TabIndex = 25;
150                         redLabel.Text = Locale.GetText ("Red") + ":";
151                         redLabel.TextAlign = ContentAlignment.MiddleRight;
152                         // blueLabel
153                         blueLabel.FlatStyle = FlatStyle.System;
154                         blueLabel.Location = new Point (361, 250);
155                         blueLabel.Size = new Size (40, 16);
156                         blueLabel.TabIndex = 26;
157                         blueLabel.Text = Locale.GetText ("Blue") + ":";
158                         blueLabel.TextAlign = ContentAlignment.MiddleRight;
159                         // greenLabel
160                         greenLabel.FlatStyle = FlatStyle.System;
161                         greenLabel.Location = new Point (361, 228);
162                         greenLabel.Size = new Size (40, 16);
163                         greenLabel.TabIndex = 27;
164                         greenLabel.Text = Locale.GetText ("Green") + ":";
165                         greenLabel.TextAlign = ContentAlignment.MiddleRight;
166                         // colorBaseLabel
167                         colorBaseLabel.Location = new Point (228, 247);
168                         colorBaseLabel.Size = new Size (60, 25);
169                         colorBaseLabel.TabIndex = 28;
170                         colorBaseLabel.Text = Locale.GetText ("Color");
171                         colorBaseLabel.TextAlign = ContentAlignment.MiddleCenter;
172                         // hueLabel
173                         hueLabel.FlatStyle = FlatStyle.System;
174                         hueLabel.Location = new Point (287, 206);
175                         hueLabel.Size = new Size (36, 16);
176                         hueLabel.TabIndex = 23;
177                         hueLabel.Text = Locale.GetText ("Hue") + ":";
178                         hueLabel.TextAlign = ContentAlignment.MiddleRight;
179                         // satLabel
180                         satLabel.FlatStyle = FlatStyle.System;
181                         satLabel.Location = new Point (287, 228);
182                         satLabel.Size = new Size (36, 16);
183                         satLabel.TabIndex = 22;
184                         satLabel.Text = Locale.GetText ("Sat") + ":";
185                         satLabel.TextAlign = ContentAlignment.MiddleRight;
186                         // briLabel
187                         briLabel.FlatStyle = FlatStyle.System;
188                         briLabel.Location = new Point (287, 250);
189                         briLabel.Size = new Size (36, 16);
190                         briLabel.TabIndex = 24;
191                         briLabel.Text = Locale.GetText ("Bri") + ":";
192                         briLabel.TextAlign = ContentAlignment.MiddleRight;
193                         
194                         // defineColoursButton
195                         defineColoursButton.FlatStyle = FlatStyle.System;
196                         defineColoursButton.Location = new Point (5, 244);
197                         defineColoursButton.Size = new Size (210, 22);
198                         defineColoursButton.TabIndex = 6;
199                         // FIXME: update resource files
200                         defineColoursButton.Text = "Define Custom Colors >>";
201                         //defineColoursButton.Text = Locale.GetText ("Define Custom Colors >>");
202                         // okButton
203                         okButton.FlatStyle = FlatStyle.System;
204                         okButton.Location = new Point (5, 271);
205                         okButton.Size = new Size (66, 22);
206                         okButton.TabIndex = 0;
207                         okButton.Text = Locale.GetText ("OK");
208                         // cancelButton
209                         cancelButton.FlatStyle = FlatStyle.System;
210                         cancelButton.Location = new Point (78, 271);
211                         cancelButton.Size = new Size (66, 22);
212                         cancelButton.TabIndex = 1;
213                         cancelButton.Text = Locale.GetText ("Cancel");
214                         // helpButton
215                         helpButton.FlatStyle = FlatStyle.System;
216                         helpButton.Location = new Point (149, 271);
217                         helpButton.Size = new Size (66, 22);
218                         helpButton.TabIndex = 5;
219                         helpButton.Text = Locale.GetText ("Help");
220                         helpButton.Hide ();
221                         
222                         // addColoursButton
223                         addColoursButton.FlatStyle = FlatStyle.System;
224                         addColoursButton.Location = new Point (227, 271);
225                         addColoursButton.Size = new Size (213, 22);
226                         addColoursButton.TabIndex = 7;
227                         // FIXME: update resource files
228                         addColoursButton.Text =  "Add To Custom Colors";
229                         //addColoursButton.Text =  Locale.GetText ("Add To Custom Colors");
230                         
231                         // baseColorControl
232                         baseColorControl.Location = new Point (3, 6);
233                         baseColorControl.Size = new Size (212, 231);
234                         baseColorControl.TabIndex = 13;
235                         // colorMatrixControl
236                         colorMatrixControl.Location = new Point (227, 7);
237                         colorMatrixControl.Size = new Size (179, 190);
238                         colorMatrixControl.TabIndex = 14;
239                         // triangleControl
240                         triangleControl.Location = new Point (432, 0);
241                         triangleControl.Size = new Size (16, 204);
242                         triangleControl.TabIndex = 12;
243                         // brightnessControl
244                         brightnessControl.Location = new Point (415, 7);
245                         brightnessControl.Size = new Size (14, 190);
246                         brightnessControl.TabIndex = 20;
247                         
248                         // selectedColorPanel
249                         selectedColorPanel.BackColor = SystemColors.Desktop;
250                         selectedColorPanel.BorderStyle = BorderStyle.Fixed3D;
251                         selectedColorPanel.Location = new Point (227, 202);
252                         selectedColorPanel.Size = new Size (60, 42);
253                         selectedColorPanel.TabIndex = 10;
254                         
255                         form.Controls.Add (hueTextBox);
256                         form.Controls.Add (satTextBox);
257                         form.Controls.Add (briTextBox);
258                         form.Controls.Add (redTextBox);
259                         form.Controls.Add (greenTextBox);
260                         form.Controls.Add (blueTextBox);
261                         
262                         form.Controls.Add (defineColoursButton);
263                         form.Controls.Add (okButton);
264                         form.Controls.Add (cancelButton);
265                         form.Controls.Add (addColoursButton);
266                         form.Controls.Add (helpButton);
267                         
268                         form.Controls.Add (baseColorControl);
269                         form.Controls.Add (colorMatrixControl);
270                         form.Controls.Add (brightnessControl);
271                         form.Controls.Add (triangleControl);
272                         
273                         form.Controls.Add (colorBaseLabel);
274                         form.Controls.Add (greenLabel);
275                         form.Controls.Add (blueLabel);
276                         form.Controls.Add (redLabel);
277                         form.Controls.Add (briLabel);
278                         form.Controls.Add (hueLabel);
279                         form.Controls.Add (satLabel);
280                         
281                         form.Controls.Add (selectedColorPanel);
282                         
283                         form.ResumeLayout (false);
284                         
285                         Color = Color.Black;
286                         
287                         defineColoursButton.Click += new EventHandler (OnClickButtonDefineColours);
288                         addColoursButton.Click += new EventHandler (OnClickButtonAddColours);
289                         helpButton.Click += new EventHandler (OnClickHelpButton);
290                         cancelButton.Click += new EventHandler (OnClickCancelButton);
291                         okButton.Click += new EventHandler (OnClickOkButton);
292                         
293                         hueTextBox.KeyPress += new KeyPressEventHandler (OnKeyPressTextBoxes);
294                         satTextBox.KeyPress += new KeyPressEventHandler (OnKeyPressTextBoxes);
295                         briTextBox.KeyPress += new KeyPressEventHandler (OnKeyPressTextBoxes);
296                         redTextBox.KeyPress += new KeyPressEventHandler (OnKeyPressTextBoxes);
297                         greenTextBox.KeyPress += new KeyPressEventHandler (OnKeyPressTextBoxes);
298                         blueTextBox.KeyPress += new KeyPressEventHandler (OnKeyPressTextBoxes);
299                         
300                         hueTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
301                         satTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
302                         briTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
303                         redTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
304                         greenTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
305                         blueTextBox.TextChanged += new EventHandler (OnTextChangedTextBoxes);
306                         
307                         hueTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
308                         satTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
309                         briTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
310                         redTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
311                         greenTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
312                         blueTextBox.GotFocus += new EventHandler (OnGotFocusTextBoxes);
313                         
314                         hueTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
315                         satTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
316                         briTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
317                         redTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
318                         greenTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
319                         blueTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
320                         
321                         ResetCustomColors ();
322                 }
323                 #endregion      // Public Constructors
324                 
325                 #region Public Instance Properties
326                 public Color Color {
327                         get {
328                                 return selectedColorPanel.BackColor;
329                         }
330                         
331                         set {
332                                 if (value.IsEmpty) {
333                                         color = Color.Black;
334                                         baseColorControl.SetColor (color);
335                                 } else if (color != value) {
336                                         color = value;
337                                         baseColorControl.SetColor (color);
338                                 }
339                         }
340                 }
341                 
342                 [DefaultValue(true)]
343                 public virtual bool AllowFullOpen {
344                         get {
345                                 return allowFullOpen;
346                         }
347                         
348                         set {
349                                 if (allowFullOpen != value) {
350                                         allowFullOpen = value;
351                                         
352                                         if (!allowFullOpen)
353                                                 defineColoursButton.Enabled = false;
354                                         else
355                                                 defineColoursButton.Enabled = true;
356                                 }
357                         }
358                 }
359                 
360                 // Currently AnyColor internally is always true
361                 // Does really anybody still use 256 or less colors ???
362                 // Naw, cairo only supports 24bit anyway - pdb
363                 [DefaultValue(false)]
364                 public virtual bool AnyColor {
365                         get {
366                                 return anyColor;
367                         }
368                         
369                         set {
370                                 anyColor = value;
371                         }
372                 }
373                 
374                 [DefaultValue(false)]
375                 public virtual bool FullOpen {
376                         get {
377                                 return fullOpen;
378                         }
379                         
380                         set {
381                                 if (fullOpen != value) {
382                                         fullOpen = value;
383                                         
384                                         if (fullOpen && allowFullOpen) {
385                                                 defineColoursButton.Enabled = false;
386                                                 colorMatrixControl.ColorToShow = baseColorControl.ColorToShow;
387                                                 form.Size = GetFormSize (true);
388                                         } else {
389                                                 if (allowFullOpen)
390                                                         defineColoursButton.Enabled = true;
391                                                 form.Size = GetFormSize (false);
392                                         }
393                                 }
394                         }
395                 }
396                 
397                 [Browsable(false)]
398                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
399                 public int[] CustomColors {
400                         get {
401                                 return customColors;
402                         }
403                         
404                         set {
405                                 if (value == null)
406                                         ResetCustomColors ();
407                                 else {
408                                         int[] tmp_colors = value;
409                                         
410                                         Array.Copy (tmp_colors, customColors, tmp_colors.Length);
411                                 }
412                                         
413                                 baseColorControl.SetCustomColors ();
414                         }
415                 }
416                 
417                 [DefaultValue(false)]
418                 public virtual bool ShowHelp {
419                         get {
420                                 return showHelp;
421                         }
422                         
423                         set {
424                                 if (showHelp != value) {
425                                         showHelp = value;
426                                         if (showHelp)
427                                                 helpButton.Show ();
428                                         else
429                                                 helpButton.Hide ();
430                                 }
431                         }
432                 }
433                 
434                 [DefaultValue(false)]
435                 public virtual bool SolidColorOnly {
436                         get {
437                                 return solidColorOnly;
438                         }
439                         
440                         set {
441                                 solidColorOnly = value;
442                         }
443                 }
444                 #endregion      // Public Instance Properties
445                 
446                 #region Public Instance Methods
447                 public override void Reset ()
448                 {
449                         AllowFullOpen = true;
450                         anyColor = false;
451                         Color = Color.Black;
452                         CustomColors = null;
453                         FullOpen = false;
454                         ShowHelp = false;
455                         solidColorOnly = false;
456                 }
457                 
458                 public override string ToString ()
459                 {
460                         return base.ToString () + ",  Color: " + Color.ToString ();
461                 }
462                 #endregion      // Public Instance Methods
463                 
464                 #region Protected Instance Properties
465                 protected virtual IntPtr Instance {
466                         get {
467                                 // MS Internal
468                                 return (IntPtr)GetHashCode ();
469                         }
470                 }
471                 
472                 protected virtual int Options {
473                         get {
474                                 // MS Internal
475                                 return 0;
476                         }
477                 }
478                 #endregion      // Protected Instance Properties
479                 
480                 #region Protected Instance Methods
481                 protected override bool RunDialog (IntPtr hwndOwner)
482                 {
483                         defineColoursButton.Enabled = (AllowFullOpen && !FullOpen);
484                         defineColoursButton.Refresh ();
485
486                         form.Size = GetFormSize (FullOpen && AllowFullOpen);
487                         // currently needed, otherwise there are a lot of drawing artefacts/transparent controls if the same dialog gets opened again
488                         form.Refresh ();
489                         
490                         return true;
491                 }
492                 #endregion      // Protected Instance Methods
493                 
494                 #region Private Methods
495
496                 Size GetFormSize (bool fullOpen)
497                 {
498                         if (fullOpen)
499                                 return new Size (448, 332);
500                         else
501                                 return new Size (221, 332); // 300
502                 }
503
504                 void OnClickCancelButton (object sender, EventArgs e)
505                 {
506                         form.DialogResult = DialogResult.Cancel;
507                 }
508                 
509                 void OnClickOkButton (object sender, EventArgs e)
510                 {
511                         form.DialogResult = DialogResult.OK;
512                 }
513                 
514                 void OnClickButtonAddColours (object sender, EventArgs e)
515                 {
516                         baseColorControl.SetUserColor (selectedColorPanel.BackColor);
517                 }
518                 
519                 void OnClickButtonDefineColours (object sender, EventArgs e)
520                 {
521                         if (allowFullOpen) {
522                                 defineColoursButton.Enabled = false;
523                                 colorMatrixControl.ColorToShow = baseColorControl.ColorToShow;
524                                 form.Size = GetFormSize (true);
525                         }
526                 }
527                 
528                 // FIXME: Is this correct ?
529                 void OnClickHelpButton (object sender, EventArgs e)
530                 {
531                         OnHelpRequest (e);
532                 }
533                 
534                 string textBox_text_old = "";
535                 
536                 void OnGotFocusTextBoxes (object sender, EventArgs e)
537                 {
538                         TextBox textbox = sender as TextBox;
539                         textBox_text_old = textbox.Text;
540                 }
541                 
542                 void OnLostFocusTextBoxes (object sender, EventArgs e)
543                 {
544                         TextBox textbox = sender as TextBox;
545                         
546                         if (textbox.Text.Length == 0)
547                                 textbox.Text = textBox_text_old;
548                 }
549                 
550                 void OnKeyPressTextBoxes (object sender, KeyPressEventArgs e)
551                 {
552                         if (Char.IsLetter (e.KeyChar) || Char.IsWhiteSpace (e.KeyChar) || Char.IsPunctuation (e.KeyChar) || e.KeyChar == ',') {
553                                 e.Handled = true;
554                                 return; 
555                         }
556                         
557                         internal_textbox_change = true;
558                 }
559                 
560                 internal TextBox edit_textbox = null;
561                 bool internal_textbox_change = false;
562                 
563                 void OnTextChangedTextBoxes (object sender, EventArgs e)
564                 {
565                         if (!internal_textbox_change)
566                                 return;
567                         
568                         internal_textbox_change = false;
569                         
570                         TextBox tmp_box = sender as TextBox;
571                         
572                         if (tmp_box.Text.Length == 0)
573                                 return;
574                         
575                         string text = tmp_box.Text;
576                         
577                         int val = 0;
578                         
579                         try {
580                                 val = System.Convert.ToInt32 (text);
581                         } catch (Exception) {
582                                 // bla
583                         }
584                         
585                         if (sender == hueTextBox) {
586                                 if (val > 239) {
587                                         val = 239;
588                                         hueTextBox.Text = val.ToString ();
589                                 } else
590                                 if (val < 0) {
591                                         val = 0;
592                                         hueTextBox.Text = val.ToString ();
593                                 }
594                                 
595                                 edit_textbox = hueTextBox;
596                                 
597                                 UpdateFromHSBTextBoxes ();
598                                 
599                                 UpdateControls (selectedColorPanel.BackColor);
600                         } else
601                         if (sender == satTextBox) {
602                                 if (val > 240) {
603                                         val = 240;
604                                         satTextBox.Text = val.ToString ();
605                                 } else
606                                 if (val < 0) {
607                                         val = 0;
608                                         satTextBox.Text = val.ToString ();
609                                 }
610                                 
611                                 edit_textbox = satTextBox;
612                                 
613                                 UpdateFromHSBTextBoxes ();
614                                 
615                                 UpdateControls (selectedColorPanel.BackColor);
616                         } else
617                         if (sender == briTextBox) {
618                                 if (val > 240) {
619                                         val = 240;
620                                         briTextBox.Text = val.ToString ();
621                                 } else
622                                 if (val < 0) {
623                                         val = 0;
624                                         briTextBox.Text = val.ToString ();
625                                 }
626                                 
627                                 edit_textbox = briTextBox;
628                                 
629                                 UpdateFromHSBTextBoxes ();
630                                 
631                                 UpdateControls (selectedColorPanel.BackColor);
632                         } else
633                         if (sender == redTextBox) {
634                                 if (val > 255) {
635                                         val = 255;
636                                         redTextBox.Text = val.ToString ();
637                                 } else
638                                 if (val < 0) {
639                                         val = 0;
640                                         redTextBox.Text = val.ToString ();
641                                 }
642                                 
643                                 edit_textbox = redTextBox;
644                                 
645                                 UpdateFromRGBTextBoxes ();
646                         } else
647                         if (sender == greenTextBox) {
648                                 if (val > 255) {
649                                         val = 255;
650                                         greenTextBox.Text = val.ToString ();
651                                 } else
652                                 if (val < 0) {
653                                         val = 0;
654                                         greenTextBox.Text = val.ToString ();
655                                 }
656                                 
657                                 edit_textbox = greenTextBox;
658                                 
659                                 UpdateFromRGBTextBoxes ();
660                         } else
661                         if (sender == blueTextBox) {
662                                 if (val > 255) {
663                                         val = 255;
664                                         blueTextBox.Text = val.ToString ();
665                                 } else
666                                 if (val < 0) {
667                                         val = 0;
668                                         blueTextBox.Text = val.ToString ();
669                                 }
670                                 
671                                 edit_textbox = blueTextBox;
672                                 
673                                 UpdateFromRGBTextBoxes ();
674                         }
675                         
676                         textBox_text_old = edit_textbox.Text;
677                         
678                         edit_textbox = null;
679                 }
680                 
681                 internal void UpdateControls (Color acolor)
682                 {
683                         selectedColorPanel.BackColor = acolor;
684                         colorMatrixControl.ColorToShow = acolor;
685                         brightnessControl.ColorToShow = acolor;
686                         triangleControl.ColorToShow = acolor;
687                 }
688                 
689                 internal void UpdateRGBTextBoxes (Color acolor)
690                 {
691                         if (edit_textbox != redTextBox)
692                                 redTextBox.Text = acolor.R.ToString ();
693                         if (edit_textbox != greenTextBox)
694                                 greenTextBox.Text = acolor.G.ToString ();
695                         if (edit_textbox != blueTextBox)
696                                 blueTextBox.Text = acolor.B.ToString ();
697                 }
698                 
699                 internal void UpdateHSBTextBoxes (Color acolor)
700                 {
701                         HSB hsb = HSB.RGB2HSB (acolor);
702                         
703                         if (edit_textbox != hueTextBox)
704                                 hueTextBox.Text = hsb.hue.ToString ();
705                         if (edit_textbox != satTextBox)
706                                 satTextBox.Text = hsb.sat.ToString ();
707                         if (edit_textbox != briTextBox)
708                                 briTextBox.Text = hsb.bri.ToString ();
709                 }
710                 
711                 internal void UpdateFromHSBTextBoxes ()
712                 {
713                         Color col = HSB.HSB2RGB (System.Convert.ToInt32 (hueTextBox.Text),
714                                                  System.Convert.ToInt32 (satTextBox.Text),
715                                                  System.Convert.ToInt32 (briTextBox.Text));
716                         
717                         selectedColorPanel.BackColor = col;
718                         UpdateRGBTextBoxes (col);
719                 }
720                 
721                 internal void UpdateFromRGBTextBoxes ()
722                 {
723                         Color col = Color.FromArgb (System.Convert.ToInt32 (redTextBox.Text),
724                                                     System.Convert.ToInt32 (greenTextBox.Text),
725                                                     System.Convert.ToInt32 (blueTextBox.Text));
726                         
727                         selectedColorPanel.BackColor = col;
728                         
729                         UpdateHSBTextBoxes (col);
730                         
731                         UpdateControls (col);
732                 }
733                 
734                 void ResetCustomColors ()
735                 {
736                         // check if this.customColors already exists
737                         if (customColors == null)
738                                 customColors = new int [16];
739                         
740                         int default_color = Color.FromArgb(0, 255, 255, 255).ToArgb ();
741                                 
742                         for (int i = 0; i < customColors.Length; i++)
743                                 customColors [i] = default_color;
744                 }
745                 #endregion
746                 
747                 #region Internal structs and classes
748                 internal struct HSB {
749                         public int hue;
750                         public int sat;
751                         public int bri;
752                         
753                         public static HSB RGB2HSB (Color color)
754                         {
755                                 HSB hsb = new HSB ();
756                                 
757                                 hsb.hue = (int)((color.GetHue () / 360.0f) * 240);
758                                 hsb.sat = (int)(color.GetSaturation () * 241);
759                                 hsb.bri = (int)(color.GetBrightness () * 241);
760                                 
761                                 if (hsb.hue > 239) hsb.hue = 239;
762                                 if (hsb.sat > 240) hsb.sat = 240;
763                                 if (hsb.bri > 240) hsb.bri = 240;
764                                 
765                                 return hsb;
766                         }
767                         
768                         // not using ControlPaint HBS2Color, this algo is more precise
769                         public static Color HSB2RGB (int hue, int saturation, int brightness)
770                         {
771                                 if (hue > 239)
772                                         hue = 239;
773                                 else
774                                 if (hue < 0)
775                                         hue = 0;
776                                 
777                                 if (saturation > 240)
778                                         saturation = 240;
779                                 else
780                                 if (saturation < 0)
781                                         saturation = 0;
782                                 
783                                 if (brightness > 240)
784                                         brightness = 240;
785                                 else
786                                 if (brightness < 0)
787                                         brightness = 0;
788                                 
789                                 float H = hue / 239.0f;
790                                 float S = saturation / 240.0f;
791                                 float L = brightness / 240.0f;
792                                 
793                                 float r = 0, g = 0, b = 0;
794                                 float d1, d2;
795                                 
796                                 if (L == 0) {
797                                         r = g =  b = 0;
798                                 } else {
799                                         if (S == 0) {
800                                                 r = g = b = L;
801                                         } else {
802                                                 d2 = (L <= 0.5f) ? L * (1.0f + S) : L + S - (L * S);
803                                                 d1 = 2.0f * L - d2;
804                                                 
805                                                 float[] d3 = new float [] { H + 1.0f / 3.0f , H, H - 1.0f / 3.0f };
806                                                 float[] rgb = new float [] { 0,0,0 };
807                                                 
808                                                 for (int i = 0; i < 3; i++) {
809                                                         if (d3 [i] < 0)
810                                                                 d3 [i] += 1.0f;
811                                                         if (d3 [i] > 1.0f)
812                                                                 d3 [i] -= 1.0f;
813                                                         
814                                                         if (6.0f * d3 [i] < 1.0f)
815                                                                 rgb [i] = d1 + (d2 - d1) * d3 [i] * 6.0f;
816                                                         else
817                                                         if (2.0f * d3 [i] < 1.0f)
818                                                                 rgb [i] = d2;
819                                                         else
820                                                         if (3.0f * d3 [i] < 2.0f)
821                                                                 rgb [i] = (d1 + (d2 - d1) * ((2.0f / 3.0f) - d3 [i]) * 6.0f);
822                                                         else
823                                                                 rgb [i] = d1;
824                                                 }
825                                                 
826                                                 r = rgb [0];
827                                                 g = rgb [1];
828                                                 b = rgb [2];
829                                         }
830                                 }
831                                 
832                                 r = 255.0f * r;
833                                 g = 255.0f * g;
834                                 b = 255.0f * b;
835                                 
836                                 if (r < 1)
837                                         r = 0.0f;
838                                 else
839                                 if (r > 255.0f)
840                                         r = 255.0f;
841                                 
842                                 if (g < 1)
843                                         g = 0.0f;
844                                 else
845                                 if (g > 255.0f)
846                                         g = 255.0f;
847                                 
848                                 if (b < 1)
849                                         b = 0.0f;
850                                 else
851                                 if (b > 255.0f)
852                                         b = 255.0f;
853                                 
854                                 return Color.FromArgb ((int)r, (int)g, (int)b);
855                         }
856                         
857                         public static int Brightness (Color color)
858                         {
859                                 return (int)(color.GetBrightness () * 241);
860                         }
861                         
862                         public static void GetHueSaturation (Color color, out int hue, out int sat)
863                         {
864                                 hue = (int)((color.GetHue () / 360.0f) * 240);
865                                 sat = (int)(color.GetSaturation () * 241);
866                         }
867                         
868                         // only for testing
869                         // there are some small glitches, but it is still better than ControlPaint implementation
870                         public static void TestColor (Color color)
871                         {
872                                 Console.WriteLine ("Color: " + color);
873                                 HSB hsb = HSB.RGB2HSB (color);
874                                 Console.WriteLine ("RGB2HSB: " + hsb.hue + ", " + hsb.sat + ", " + hsb.bri);
875                                 Console.WriteLine ("HSB2RGB: " + HSB.HSB2RGB (hsb.hue, hsb.sat, hsb.bri));
876                                 Console.WriteLine ();
877                         }
878                 }
879                 
880                 internal class BaseColorControl : Control {
881                         internal class SmallColorControl : Control {
882                                 private Color internalcolor;
883                                 
884                                 private bool isSelected = false;
885                                 
886                                 public SmallColorControl (Color color)
887                                 {
888                                         SuspendLayout ();
889                                         
890                                         this.internalcolor = color;
891                                         
892                                         Size = new Size (25, 23);
893                                         
894                                         ResumeLayout (false);
895                                         
896                                         SetStyle (ControlStyles.DoubleBuffer, true);
897                                         SetStyle (ControlStyles.AllPaintingInWmPaint, true);
898                                         SetStyle (ControlStyles.UserPaint, true);
899                                         SetStyle (ControlStyles.Selectable, true);
900                                 }
901                                 
902                                 public bool IsSelected {
903                                         set {
904                                                 isSelected = value;
905                                                 Invalidate ();
906                                         }
907
908                                         get {
909                                                 return isSelected;
910                                         }
911                                 }
912                                 
913                                 public Color InternalColor {
914                                         set {
915                                                 internalcolor = value;
916                                                 Invalidate ();
917                                         }
918                                         
919                                         get {
920                                                 return internalcolor;
921                                         }
922                                 }
923                                 
924                                 protected override void OnPaint (PaintEventArgs pe)
925                                 {
926                                         base.OnPaint (pe);
927                                         
928                                         pe.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (internalcolor),
929                                                                    new Rectangle (4, 4, 17, 15));
930                                         
931                                         ControlPaint.DrawBorder3D (pe.Graphics, 3, 3, 19, 17, Border3DStyle.Sunken);
932                                         
933                                         if (isSelected) {
934                                                 pe.Graphics.DrawRectangle (ThemeEngine.Current.ResPool.GetPen (Color.Black),
935                                                                            new Rectangle (2, 2, 20, 18));
936                                         }
937                                         
938                                         if (Focused) {
939                                                 ControlPaint.DrawFocusRectangle (
940                                                         pe.Graphics, new Rectangle (0, 0, 25, 23)
941                                                 );
942                                         }
943                                 }
944                                 
945                                 protected override void OnClick (EventArgs e)
946                                 {
947                                         Focus ();
948                                         IsSelected = true;
949                                         
950                                         base.OnClick (e);
951                                 }
952                                 
953                                 
954                                 protected override void OnLostFocus (EventArgs e)
955                                 {
956                                         Invalidate ();
957                                         
958                                         base.OnLostFocus (e);
959                                 }
960                         }
961                         
962                         private SmallColorControl[] smallColorControl;
963                         
964                         private SmallColorControl[] userSmallColorControl;
965                         
966                         private Label userColorLabel;
967                         private Label baseColorLabel;
968                         
969                         private SmallColorControl selectedSmallColorControl;
970
971                         #region UIA Framework Property
972                         public SmallColorControl UIASelectedSmallColorControl {
973                                 get {
974                                         for (int i = 0; i < smallColorControl.Length - 1; ++i)
975                                                 if (smallColorControl [i].IsSelected)
976                                                         return smallColorControl [i];
977                                         for (int j = 0; j < userSmallColorControl.Length - 1; ++j)
978                                                 if (userSmallColorControl [j].IsSelected)
979                                                         return userSmallColorControl [j];
980                                         return null;
981                                 }
982                         }
983                         #endregion
984
985                         private int currentlyUsedUserSmallColorControl = 0;
986                         
987                         private ColorDialog colorDialog = null;
988                         
989                         public BaseColorControl (ColorDialog colorDialog)
990                         {
991                                 this.colorDialog = colorDialog;
992                                 
993                                 userSmallColorControl = new SmallColorControl [16];
994                                 userSmallColorControl [0] = new SmallColorControl (Color.White);
995                                 userSmallColorControl [1] = new SmallColorControl (Color.White);
996                                 userSmallColorControl [2] = new SmallColorControl (Color.White);
997                                 userSmallColorControl [3] = new SmallColorControl (Color.White);
998                                 userSmallColorControl [4] = new SmallColorControl (Color.White);
999                                 userSmallColorControl [5] = new SmallColorControl (Color.White);
1000                                 userSmallColorControl [6] = new SmallColorControl (Color.White);
1001                                 userSmallColorControl [7] = new SmallColorControl (Color.White);
1002                                 userSmallColorControl [8] = new SmallColorControl (Color.White);
1003                                 userSmallColorControl [9] = new SmallColorControl (Color.White);
1004                                 userSmallColorControl [10] = new SmallColorControl (Color.White);
1005                                 userSmallColorControl [11] = new SmallColorControl (Color.White);
1006                                 userSmallColorControl [12] = new SmallColorControl (Color.White);
1007                                 userSmallColorControl [13] = new SmallColorControl (Color.White);
1008                                 userSmallColorControl [14] = new SmallColorControl (Color.White);
1009                                 userSmallColorControl [15] = new SmallColorControl (Color.White);
1010                                 
1011                                 smallColorControl = new SmallColorControl [48];
1012                                 smallColorControl [0] = new SmallColorControl (Color.FromArgb (((Byte)(255)), ((Byte)(128)), ((Byte)(128))));
1013                                 smallColorControl [1] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(128)), ((Byte)(64))));
1014                                 smallColorControl [2] = new SmallColorControl (Color.Gray);
1015                                 smallColorControl [3] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(0)), ((Byte)(255))));
1016                                 smallColorControl [4] = new SmallColorControl (Color.Silver);
1017                                 smallColorControl [5] = new SmallColorControl (Color.FromArgb (((Byte)(64)), ((Byte)(128)), ((Byte)(128))));
1018                                 smallColorControl [6] = new SmallColorControl (Color.White);
1019                                 smallColorControl [7] = new SmallColorControl (Color.FromArgb (((Byte)(64)), ((Byte)(0)), ((Byte)(64))));
1020                                 smallColorControl [8] = new SmallColorControl (Color.FromArgb (((Byte)(255)), ((Byte)(128)), ((Byte)(64))));
1021                                 smallColorControl [9] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(64)), ((Byte)(64))));
1022                                 smallColorControl [10] = new SmallColorControl (Color.Teal);
1023                                 smallColorControl [11] = new SmallColorControl (Color.Lime);
1024                                 smallColorControl [12] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(128)), ((Byte)(255))));
1025                                 smallColorControl [13] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(64)), ((Byte)(128))));
1026                                 smallColorControl [14] = new SmallColorControl (Color.FromArgb (((Byte)(255)), ((Byte)(0)), ((Byte)(128))));
1027                                 smallColorControl [15] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(255)), ((Byte)(0))));
1028                                 smallColorControl [16] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(255)), ((Byte)(64))));
1029                                 smallColorControl [17] = new SmallColorControl (Color.Red);
1030                                 smallColorControl [18] = new SmallColorControl (Color.FromArgb (((Byte)(255)), ((Byte)(128)), ((Byte)(0))));
1031                                 smallColorControl [19] = new SmallColorControl (Color.FromArgb (((Byte)(255)), ((Byte)(128)), ((Byte)(255))));
1032                                 smallColorControl [20] = new SmallColorControl (Color.Fuchsia);
1033                                 smallColorControl [21] = new SmallColorControl (Color.Aqua);
1034                                 smallColorControl [22] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(255)), ((Byte)(128))));
1035                                 smallColorControl [23] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(255)), ((Byte)(255))));
1036                                 smallColorControl [24] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(128)), ((Byte)(255))));
1037                                 smallColorControl [25] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(64)), ((Byte)(0))));
1038                                 smallColorControl [26] = new SmallColorControl (Color.FromArgb (((Byte)(64)), ((Byte)(0)), ((Byte)(0))));
1039                                 smallColorControl [27] = new SmallColorControl (Color.Maroon);
1040                                 smallColorControl [28] = new SmallColorControl (Color.Purple);
1041                                 smallColorControl [29] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(0)), ((Byte)(160))));
1042                                 smallColorControl [30] = new SmallColorControl (Color.Blue);
1043                                 smallColorControl [31] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(128)), ((Byte)(64))));
1044                                 smallColorControl [32] = new SmallColorControl (Color.Green);
1045                                 smallColorControl [33] = new SmallColorControl (Color.Yellow);
1046                                 smallColorControl [34] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(128)), ((Byte)(192))));
1047                                 smallColorControl [35] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(128)), ((Byte)(192))));
1048                                 smallColorControl [36] = new SmallColorControl (Color.FromArgb (((Byte)(128)), ((Byte)(0)), ((Byte)(64))));
1049                                 smallColorControl [37] = new SmallColorControl (Color.FromArgb (((Byte)(255)), ((Byte)(128)), ((Byte)(192))));
1050                                 smallColorControl [38] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(255)), ((Byte)(128))));
1051                                 smallColorControl [39] = new SmallColorControl (Color.FromArgb (((Byte)(255)), ((Byte)(255)), ((Byte)(128))));
1052                                 smallColorControl [40] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(64)), ((Byte)(0))));
1053                                 smallColorControl [41] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(64)), ((Byte)(64))));
1054                                 smallColorControl [42] = new SmallColorControl (Color.Navy);
1055                                 smallColorControl [43] = new SmallColorControl (Color.FromArgb (((Byte)(0)), ((Byte)(0)), ((Byte)(64))));
1056                                 smallColorControl [44] = new SmallColorControl (Color.FromArgb (((Byte)(64)), ((Byte)(0)), ((Byte)(64))));
1057                                 smallColorControl [45] = new SmallColorControl (Color.FromArgb (((Byte)(64)), ((Byte)(0)), ((Byte)(128))));
1058                                 smallColorControl [46] = new SmallColorControl (Color.Black); //Black
1059                                 smallColorControl [47] = new SmallColorControl (Color.Olive);
1060                                 
1061                                 baseColorLabel = new Label ();
1062                                 userColorLabel = new Label ();
1063                                 
1064                                 SuspendLayout ();
1065                                 
1066                                 // colorPanel1
1067                                 smallColorControl [0].Location = new Point (0, 15);
1068                                 smallColorControl [0].TabIndex = 51;
1069                                 smallColorControl [0].Click += new EventHandler (OnSmallColorControlClick);
1070                                 // colorPanel2
1071                                 smallColorControl [1].Location = new Point (50, 130);
1072                                 smallColorControl [1].TabIndex = 92;
1073                                 smallColorControl [1].Click += new EventHandler (OnSmallColorControlClick);
1074                                 // colorPanel3
1075                                 smallColorControl [2].Location = new Point (75, 130);
1076                                 smallColorControl [2].TabIndex = 93;
1077                                 smallColorControl [2].Click += new EventHandler (OnSmallColorControlClick);
1078                                 // colorPanel4
1079                                 smallColorControl [3].Location = new Point (175, 84);
1080                                 smallColorControl [3].TabIndex = 98;
1081                                 smallColorControl [3].Click += new EventHandler (OnSmallColorControlClick);
1082                                 // colorPanel5
1083                                 smallColorControl [4].Location = new Point (125, 130);
1084                                 smallColorControl [4].TabIndex = 95;
1085                                 smallColorControl [4].Click += new EventHandler (OnSmallColorControlClick);
1086                                 // colorPanel6
1087                                 smallColorControl [5].Location = new Point (100, 130);
1088                                 smallColorControl [5].TabIndex = 94;
1089                                 smallColorControl [5].Click += new EventHandler (OnSmallColorControlClick);
1090                                 // colorPanel7
1091                                 smallColorControl [6].Location = new Point (175, 130);
1092                                 smallColorControl [6].TabIndex = 97;
1093                                 smallColorControl [6].Click += new EventHandler (OnSmallColorControlClick);
1094                                 // colorPanel8
1095                                 smallColorControl [7].Location = new Point (150, 130);
1096                                 smallColorControl [7].TabIndex = 96;
1097                                 smallColorControl [7].Click += new EventHandler (OnSmallColorControlClick);
1098                                 // colorPanel9
1099                                 smallColorControl [8].Location = new Point (25, 61);
1100                                 smallColorControl [8].TabIndex = 68;
1101                                 smallColorControl [8].Click += new EventHandler (OnSmallColorControlClick);
1102                                 // colorPanel10
1103                                 smallColorControl [9].Location = new Point (0, 61);
1104                                 smallColorControl [9].TabIndex = 67;
1105                                 smallColorControl [9].Click += new EventHandler (OnSmallColorControlClick);
1106                                 // colorPanel11
1107                                 smallColorControl [10].Location = new Point (75, 61);
1108                                 smallColorControl [10].TabIndex = 70;
1109                                 smallColorControl [10].Click += new EventHandler (OnSmallColorControlClick);
1110                                 // colorPanel12
1111                                 smallColorControl [11].Location = new Point (50, 61);
1112                                 smallColorControl [11].TabIndex = 69;
1113                                 smallColorControl [11].Click += new EventHandler (OnSmallColorControlClick);
1114                                 // colorPanel13
1115                                 smallColorControl [12].Location = new Point (125, 61);
1116                                 smallColorControl [12].TabIndex = 72;
1117                                 smallColorControl [12].Click += new EventHandler (OnSmallColorControlClick);
1118                                 // colorPanel14
1119                                 smallColorControl [13].Location = new Point (100, 61);
1120                                 smallColorControl [13].TabIndex = 71;
1121                                 smallColorControl [13].Click += new EventHandler (OnSmallColorControlClick);
1122                                 // colorPanel15
1123                                 smallColorControl [14].Location = new Point (175, 61);
1124                                 smallColorControl [14].TabIndex = 74;
1125                                 smallColorControl [14].Click += new EventHandler (OnSmallColorControlClick);
1126                                 // colorPanel16
1127                                 smallColorControl [15].Location = new Point (50, 38);
1128                                 smallColorControl [15].TabIndex = 61;
1129                                 smallColorControl [15].Click += new EventHandler (OnSmallColorControlClick);
1130                                 // colorPanel17
1131                                 smallColorControl [16].Location = new Point (75, 38);
1132                                 smallColorControl [16].TabIndex = 62;
1133                                 smallColorControl [16].Click += new EventHandler (OnSmallColorControlClick);
1134                                 // colorPanel18
1135                                 smallColorControl [17].Location = new Point (0, 38);
1136                                 smallColorControl [17].TabIndex = 59;
1137                                 smallColorControl [17].Click += new EventHandler (OnSmallColorControlClick);
1138                                 // colorPanel19
1139                                 smallColorControl [18].Location = new Point (25, 84);
1140                                 smallColorControl [18].TabIndex = 75;
1141                                 smallColorControl [18].Click += new EventHandler (OnSmallColorControlClick);
1142                                 // colorPanel20
1143                                 smallColorControl [19].Location = new Point (175, 15);
1144                                 smallColorControl [19].TabIndex = 58;
1145                                 smallColorControl [19].Click += new EventHandler (OnSmallColorControlClick);
1146                                 // colorPanel21
1147                                 smallColorControl [20].Location = new Point (175, 38);
1148                                 smallColorControl [20].TabIndex = 66;
1149                                 smallColorControl [20].Click += new EventHandler (OnSmallColorControlClick);
1150                                 // colorPanel22
1151                                 smallColorControl [21].Location = new Point (100, 38);
1152                                 smallColorControl [21].TabIndex = 63;
1153                                 smallColorControl [21].Click += new EventHandler (OnSmallColorControlClick);
1154                                 // colorPanel23
1155                                 smallColorControl [22].Location = new Point (50, 15);
1156                                 smallColorControl [22].TabIndex = 53;
1157                                 smallColorControl [22].Click += new EventHandler (OnSmallColorControlClick);
1158                                 // colorPanel24
1159                                 smallColorControl [23].Location = new Point (100, 15);
1160                                 smallColorControl [23].TabIndex = 55;
1161                                 smallColorControl [23].Click += new EventHandler (OnSmallColorControlClick);
1162                                 // colorPanel25
1163                                 smallColorControl [24].Location = new Point (125, 15);
1164                                 smallColorControl [24].TabIndex = 56;
1165                                 smallColorControl [24].Click += new EventHandler (OnSmallColorControlClick);
1166                                 // colorPanel26
1167                                 smallColorControl [25].Location = new Point (25, 107);
1168                                 smallColorControl [25].TabIndex = 83;
1169                                 smallColorControl [25].Click += new EventHandler (OnSmallColorControlClick);
1170                                 // colorPanel27
1171                                 smallColorControl [26].Location = new Point (0, 107);
1172                                 smallColorControl [26].TabIndex = 82;
1173                                 smallColorControl [26].Click += new EventHandler (OnSmallColorControlClick);
1174                                 // colorPanel28
1175                                 smallColorControl [27].Location = new Point (0, 84);
1176                                 smallColorControl [27].TabIndex = 81;
1177                                 smallColorControl [27].Click += new EventHandler (OnSmallColorControlClick);
1178                                 // colorPanel29
1179                                 smallColorControl [28].Location = new Point (150, 84);
1180                                 smallColorControl [28].TabIndex = 80;
1181                                 smallColorControl [28].Click += new EventHandler (OnSmallColorControlClick);
1182                                 // colorPanel30
1183                                 smallColorControl [29].Location = new Point (125, 84);
1184                                 smallColorControl [29].TabIndex = 79;
1185                                 smallColorControl [29].Click += new EventHandler (OnSmallColorControlClick);
1186                                 // colorPanel31
1187                                 smallColorControl [30].Location  = new Point (100, 84);
1188                                 smallColorControl [30].TabIndex = 78;
1189                                 smallColorControl [30].Click += new EventHandler (OnSmallColorControlClick);
1190                                 // colorPanel32
1191                                 smallColorControl [31].Location = new Point (75, 84);
1192                                 smallColorControl [31].TabIndex = 77;
1193                                 smallColorControl [31].Click += new EventHandler (OnSmallColorControlClick);
1194                                 // colorPanel33
1195                                 smallColorControl [32].Location = new Point (50, 84);
1196                                 smallColorControl [32].TabIndex = 76;
1197                                 smallColorControl [32].Click += new EventHandler (OnSmallColorControlClick);
1198                                 // colorPanel34
1199                                 smallColorControl [33].Location = new Point (25, 38);
1200                                 smallColorControl [33].TabIndex = 60;
1201                                 smallColorControl [33].Click += new EventHandler (OnSmallColorControlClick);
1202                                 // colorPanel35
1203                                 smallColorControl [34].Location = new Point (150, 38);
1204                                 smallColorControl [34].TabIndex = 65;
1205                                 smallColorControl [34].Click += new EventHandler (OnSmallColorControlClick);
1206                                 // colorPanel36
1207                                 smallColorControl [35].Location = new Point (125, 38);
1208                                 smallColorControl [35].TabIndex = 64;
1209                                 smallColorControl [35].Click += new EventHandler (OnSmallColorControlClick);
1210                                 // colorPanel37
1211                                 smallColorControl [36].Location = new Point (150, 61);
1212                                 smallColorControl [36].TabIndex = 73;
1213                                 smallColorControl [36].Click += new EventHandler (OnSmallColorControlClick);
1214                                 // colorPanel38
1215                                 smallColorControl [37].Location = new Point (150, 15);
1216                                 smallColorControl [37].TabIndex = 57;
1217                                 smallColorControl [37].Click += new EventHandler (OnSmallColorControlClick);
1218                                 // colorPanel39
1219                                 smallColorControl [38].Location = new Point (75, 15);
1220                                 smallColorControl [38].TabIndex = 54;
1221                                 smallColorControl [38].Click += new EventHandler (OnSmallColorControlClick);
1222                                 // colorPanel40
1223                                 smallColorControl [39].Location = new Point (25, 15);
1224                                 smallColorControl [39].TabIndex = 52;
1225                                 smallColorControl [39].Click += new EventHandler (OnSmallColorControlClick);
1226                                 // colorPanel41
1227                                 smallColorControl [40].Location = new Point (50, 107);
1228                                 smallColorControl [40].TabIndex = 84;
1229                                 smallColorControl [40].Click += new EventHandler (OnSmallColorControlClick);
1230                                 // colorPanel42
1231                                 smallColorControl [41].Location = new Point (75, 107);
1232                                 smallColorControl [41].TabIndex = 85;
1233                                 smallColorControl [41].Click += new EventHandler (OnSmallColorControlClick);
1234                                 // colorPanel43
1235                                 smallColorControl [42].Location = new Point (100, 107);
1236                                 smallColorControl [42].TabIndex = 86;
1237                                 smallColorControl [42].Click += new EventHandler (OnSmallColorControlClick);
1238                                 // colorPanel44
1239                                 smallColorControl [43].Location = new Point (125, 107);
1240                                 smallColorControl [43].TabIndex = 87;
1241                                 smallColorControl [43].Click += new EventHandler (OnSmallColorControlClick);
1242                                 // colorPanel45
1243                                 smallColorControl [44].Location = new Point (150, 107);
1244                                 smallColorControl [44].TabIndex = 88;
1245                                 smallColorControl [44].Click += new EventHandler (OnSmallColorControlClick);
1246                                 // colorPanel46
1247                                 smallColorControl [45].Location = new Point (175, 107);
1248                                 smallColorControl [45].TabIndex = 89;
1249                                 smallColorControl [45].Click += new EventHandler (OnSmallColorControlClick);
1250                                 // colorPanel47
1251                                 smallColorControl [46].Location = new Point (0, 130);
1252                                 smallColorControl [46].TabIndex = 90;
1253                                 smallColorControl [46].Click += new EventHandler (OnSmallColorControlClick);
1254                                 // colorPanel48
1255                                 smallColorControl [47].Location = new Point (25, 130);
1256                                 smallColorControl [47].TabIndex = 91;
1257                                 smallColorControl [47].Click += new EventHandler (OnSmallColorControlClick);
1258                                 
1259                                 // userColorPane1
1260                                 userSmallColorControl [0].Location = new Point (0, 180);
1261                                 userSmallColorControl [0].TabIndex = 99;
1262                                 userSmallColorControl [0].Click += new EventHandler (OnSmallColorControlClick);
1263                                 // userColorPanel2
1264                                 userSmallColorControl [1].Location = new Point (0, 203);
1265                                 userSmallColorControl [1].TabIndex = 108;
1266                                 userSmallColorControl [1].Click += new EventHandler (OnSmallColorControlClick);
1267                                 // userColorPanel13
1268                                 userSmallColorControl [2].Location = new Point (25, 180);
1269                                 userSmallColorControl [2].TabIndex = 100;
1270                                 userSmallColorControl [2].Click += new EventHandler (OnSmallColorControlClick);
1271                                 // userColorPanel4
1272                                 userSmallColorControl [3].Location = new Point (25, 203);
1273                                 userSmallColorControl [3].TabIndex = 109;
1274                                 userSmallColorControl [3].Click += new EventHandler (OnSmallColorControlClick);
1275                                 // userColorPanel5
1276                                 userSmallColorControl [4].Location = new Point (50, 180);
1277                                 userSmallColorControl [4].TabIndex = 101;
1278                                 userSmallColorControl [4].Click += new EventHandler (OnSmallColorControlClick);
1279                                 // userColorPanel6
1280                                 userSmallColorControl [5].Location = new Point (50, 203);
1281                                 userSmallColorControl [5].TabIndex = 110;
1282                                 userSmallColorControl [5].Click += new EventHandler (OnSmallColorControlClick);
1283                                 // userColorPanel7
1284                                 userSmallColorControl [6].Location = new Point (75, 180);
1285                                 userSmallColorControl [6].TabIndex = 102;
1286                                 userSmallColorControl [6].Click += new EventHandler (OnSmallColorControlClick);
1287                                 // userColorPanel8
1288                                 userSmallColorControl [7].Location = new Point (75, 203);
1289                                 userSmallColorControl [7].TabIndex = 111;
1290                                 userSmallColorControl [7].Click += new EventHandler (OnSmallColorControlClick);
1291                                 // userColorPanel9
1292                                 userSmallColorControl [8].Location = new Point (100, 180);
1293                                 userSmallColorControl [8].TabIndex = 103;
1294                                 userSmallColorControl [8].Click += new EventHandler (OnSmallColorControlClick);
1295                                 // userColorPanel10
1296                                 userSmallColorControl [9].Location = new Point (100, 203);
1297                                 userSmallColorControl [9].TabIndex = 112;
1298                                 userSmallColorControl [9].Click += new EventHandler (OnSmallColorControlClick);
1299                                 // userColorPanel11
1300                                 userSmallColorControl [10].Location = new Point (125, 180);
1301                                 userSmallColorControl [10].TabIndex = 105;
1302                                 userSmallColorControl [10].Click += new EventHandler (OnSmallColorControlClick);
1303                                 // userColorPanel2
1304                                 userSmallColorControl [11].Location = new Point (125, 203);
1305                                 userSmallColorControl [11].TabIndex = 113;
1306                                 userSmallColorControl [11].Click += new EventHandler (OnSmallColorControlClick);
1307                                 // userColorPanel13
1308                                 userSmallColorControl [12].Location = new Point (150, 180);
1309                                 userSmallColorControl [12].TabIndex = 106;
1310                                 userSmallColorControl [12].Click += new EventHandler (OnSmallColorControlClick);
1311                                 // userColorPanel4
1312                                 userSmallColorControl [13].Location = new Point (150, 203);
1313                                 userSmallColorControl [13].TabIndex = 114;
1314                                 userSmallColorControl [13].Click += new EventHandler (OnSmallColorControlClick);
1315                                 // userColorPanel15
1316                                 userSmallColorControl [14].Location = new Point (175, 180);
1317                                 userSmallColorControl [14].TabIndex = 107;
1318                                 userSmallColorControl [14].Click += new EventHandler (OnSmallColorControlClick);
1319                                 // userColorPanel16
1320                                 userSmallColorControl [15].Location = new Point (175, 203);
1321                                 userSmallColorControl [15].TabIndex = 115;
1322                                 userSmallColorControl [15].Click += new EventHandler (OnSmallColorControlClick);
1323                                 
1324                                 // baseColorLabel
1325                                 baseColorLabel.Location = new Point (2, 0);
1326                                 baseColorLabel.Size = new Size (200, 12);
1327                                 baseColorLabel.TabIndex = 5;
1328                                 baseColorLabel.Text = Locale.GetText ("Base Colors") + ":";
1329                                 // userColorLabel
1330                                 userColorLabel.FlatStyle = FlatStyle.System;
1331                                 userColorLabel.Location = new Point (2, 164);
1332                                 userColorLabel.Size = new Size (200, 14);
1333                                 userColorLabel.TabIndex = 104;
1334                                 userColorLabel.Text = Locale.GetText ("User Colors") + ":";
1335                                 
1336                                 Controls.Add (userSmallColorControl [7]);
1337                                 Controls.Add (userSmallColorControl [6]);
1338                                 Controls.Add (userSmallColorControl [5]);
1339                                 Controls.Add (userSmallColorControl [4]);
1340                                 Controls.Add (userSmallColorControl [3]);
1341                                 Controls.Add (userSmallColorControl [2]);
1342                                 Controls.Add (userSmallColorControl [1]);
1343                                 Controls.Add (userSmallColorControl [0]);
1344                                 Controls.Add (userSmallColorControl [15]);
1345                                 Controls.Add (userSmallColorControl [14]);
1346                                 Controls.Add (userSmallColorControl [13]);
1347                                 Controls.Add (userSmallColorControl [12]);
1348                                 Controls.Add (userSmallColorControl [11]);
1349                                 Controls.Add (userSmallColorControl [10]);
1350                                 Controls.Add (userSmallColorControl [9]);
1351                                 Controls.Add (userSmallColorControl [8]);
1352                                 
1353                                 Controls.Add (smallColorControl [0]);
1354                                 Controls.Add (smallColorControl [3]);
1355                                 Controls.Add (smallColorControl [6]);
1356                                 Controls.Add (smallColorControl [7]);
1357                                 Controls.Add (smallColorControl [4]);
1358                                 Controls.Add (smallColorControl [5]);
1359                                 Controls.Add (smallColorControl [2]);
1360                                 Controls.Add (smallColorControl [1]);
1361                                 Controls.Add (smallColorControl [47]);
1362                                 Controls.Add (smallColorControl [46]);
1363                                 Controls.Add (smallColorControl [45]);
1364                                 Controls.Add (smallColorControl [44]);
1365                                 Controls.Add (smallColorControl [43]);
1366                                 Controls.Add (smallColorControl [42]);
1367                                 Controls.Add (smallColorControl [41]);
1368                                 Controls.Add (smallColorControl [40]);
1369                                 Controls.Add (smallColorControl [25]);
1370                                 Controls.Add (smallColorControl [26]);
1371                                 Controls.Add (smallColorControl [27]);
1372                                 Controls.Add (smallColorControl [28]);
1373                                 Controls.Add (smallColorControl [29]);
1374                                 Controls.Add (smallColorControl [30]);
1375                                 Controls.Add (smallColorControl [31]);
1376                                 Controls.Add (smallColorControl [32]);
1377                                 Controls.Add (smallColorControl [18]);
1378                                 Controls.Add (smallColorControl [14]);
1379                                 Controls.Add (smallColorControl [36]);
1380                                 Controls.Add (smallColorControl [12]);
1381                                 Controls.Add (smallColorControl [13]);
1382                                 Controls.Add (smallColorControl [10]);
1383                                 Controls.Add (smallColorControl [11]);
1384                                 Controls.Add (smallColorControl [8]);
1385                                 Controls.Add (smallColorControl [9]);
1386                                 Controls.Add (smallColorControl [20]);
1387                                 Controls.Add (smallColorControl [34]);
1388                                 Controls.Add (smallColorControl [35]);
1389                                 Controls.Add (smallColorControl [21]);
1390                                 Controls.Add (smallColorControl [16]);
1391                                 Controls.Add (smallColorControl [15]);
1392                                 Controls.Add (smallColorControl [33]);
1393                                 Controls.Add (smallColorControl [17]);
1394                                 Controls.Add (smallColorControl [19]);
1395                                 Controls.Add (smallColorControl [37]);
1396                                 Controls.Add (smallColorControl [24]);
1397                                 Controls.Add (smallColorControl [23]);
1398                                 Controls.Add (smallColorControl [38]);
1399                                 Controls.Add (smallColorControl [22]);
1400                                 Controls.Add (smallColorControl [39]);
1401                                 
1402                                 Controls.Add (userColorLabel);
1403                                 Controls.Add (baseColorLabel);
1404                                 
1405                                 Size = new Size (212, 238);
1406                                 
1407                                 ResumeLayout (false);
1408                         }
1409                         
1410                         private void CheckIfColorIsInPanel (Color color)
1411                         {
1412                                 // check if we have a panel with a BackColor = color...
1413                                 for (int i = 0; i < smallColorControl.Length; i++) {
1414                                         if (smallColorControl [i].InternalColor == color) {
1415                                                 selectedSmallColorControl = smallColorControl [i];
1416                                                 selectedSmallColorControl.IsSelected = true;
1417                                                 break;
1418                                         }
1419                                 }
1420                         }
1421                         
1422                         void OnSmallColorControlClick (object sender, EventArgs e)
1423                         {
1424                                 // previous selected smallcolorcontrol
1425                                 if (selectedSmallColorControl != (SmallColorControl)sender)
1426                                         selectedSmallColorControl.IsSelected = false;
1427                                 
1428                                 selectedSmallColorControl = (SmallColorControl)sender;
1429                                 
1430                                 TriangleControl.CurrentBrightness = HSB.Brightness (selectedSmallColorControl.InternalColor);
1431                                 
1432                                 colorDialog.UpdateControls (selectedSmallColorControl.InternalColor);
1433                                 colorDialog.UpdateRGBTextBoxes (selectedSmallColorControl.InternalColor);
1434                                 colorDialog.UpdateHSBTextBoxes (selectedSmallColorControl.InternalColor);
1435                         }
1436                         
1437                         public Color ColorToShow {
1438                                 get {
1439                                         return selectedSmallColorControl.InternalColor;
1440                                 }
1441                         }
1442                         
1443                         
1444                         public void SetColor (Color acolor)
1445                         {
1446                                 if (selectedSmallColorControl != null)
1447                                         selectedSmallColorControl.IsSelected = false;
1448                                 
1449                                 CheckIfColorIsInPanel (acolor);
1450                                 
1451                                 TriangleControl.CurrentBrightness = HSB.Brightness (acolor);
1452                                 
1453                                 colorDialog.UpdateControls (acolor);
1454                                 colorDialog.UpdateRGBTextBoxes (acolor);
1455                                 colorDialog.UpdateHSBTextBoxes (acolor);
1456                         }
1457                         
1458                         public void SetUserColor (Color col)
1459                         {
1460                                 userSmallColorControl [currentlyUsedUserSmallColorControl].InternalColor = col;
1461                                 
1462                                 colorDialog.customColors [currentlyUsedUserSmallColorControl] = col.ToArgb ();
1463                                 
1464                                 currentlyUsedUserSmallColorControl++;
1465                                 if (currentlyUsedUserSmallColorControl > 15)
1466                                         currentlyUsedUserSmallColorControl = 0;
1467                         }
1468                         
1469                         public void SetCustomColors ()
1470                         {
1471                                 for (int i = 0; i < colorDialog.customColors.Length; i++) {
1472                                         userSmallColorControl [i].InternalColor = Color.FromArgb (colorDialog.customColors [i]);
1473                                 }
1474                         }
1475                 }
1476                 
1477                 internal class ColorMatrixControl : Panel {
1478                         internal class DrawingBitmap {
1479                                 private Bitmap bitmap;
1480                                 
1481                                 public DrawingBitmap (Size size)
1482                                 {
1483                                         bitmap = new Bitmap (size.Width, size.Height);
1484                                         
1485                                         float hueadd = 240.0f / (size.Width - 1);
1486                                         float satsub = 241.0f / (size.Height - 1);
1487                                         float satpos = 240.0f;
1488                                         
1489                                         // paint the matrix to the bitmap
1490                                         for (int height = 0; height < size.Height; height++) {
1491                                                 float huepos = 0.0f;
1492                                                 
1493                                                 for (int width = 0; width < size.Width; width++) {
1494                                                         HSB hsb = new HSB ();
1495                                                         
1496                                                         hsb.hue = (int)huepos;
1497                                                         hsb.sat = (int)satpos;
1498                                                         hsb.bri = 120; // paint it with 120 to get a nice bitmap
1499                                                         
1500                                                         bitmap.SetPixel (width, height, HSB.HSB2RGB (hsb.hue, hsb.sat, hsb.bri));
1501                                                         
1502                                                         huepos += hueadd;
1503                                                 }
1504                                                 
1505                                                 satpos -= satsub;
1506                                         }
1507                                 }
1508                                 
1509                                 public Bitmap Bitmap {
1510                                         set {
1511                                                 bitmap = value;
1512                                         }
1513                                         
1514                                         get {
1515                                                 return bitmap;
1516                                         }
1517                                 }
1518                         }
1519                         
1520                         internal class CrossCursor {
1521                                 private Bitmap bitmap;
1522                                 
1523                                 private Color cursorColor;
1524                                 
1525                                 public CrossCursor ()
1526                                 {
1527                                         bitmap = new Bitmap (22, 22);
1528                                         
1529                                         cursorColor = Color.Black;
1530                                         
1531                                         Draw ();
1532                                 }
1533                                 
1534                                 public void Draw ()
1535                                 {
1536                                         using (Pen pen = new Pen (ThemeEngine.Current.ResPool.GetSolidBrush (cursorColor), 3)) {
1537                                                 using (Graphics graphics = Graphics.FromImage (bitmap)) {
1538                                                         graphics.DrawLine (pen, 11, 0, 11, 7);
1539                                                         graphics.DrawLine (pen, 11, 14, 11, 21);
1540                                                         graphics.DrawLine (pen, 0, 11, 7, 11);
1541                                                         graphics.DrawLine (pen, 14, 11, 21, 11);
1542                                                 }
1543                                         }
1544                                 }
1545                                 
1546                                 public Bitmap Bitmap {
1547                                         set {
1548                                                 bitmap = value;
1549                                         }
1550                                         
1551                                         get {
1552                                                 return bitmap;
1553                                         }
1554                                 }
1555                                 
1556                                 public Color CursorColor {
1557                                         set {
1558                                                 cursorColor = value;
1559                                         }
1560                                         
1561                                         get {
1562                                                 return cursorColor;
1563                                         }
1564                                 }
1565                         }
1566                         
1567                         private DrawingBitmap drawingBitmap = null;
1568                         
1569                         private CrossCursor crossCursor = new CrossCursor();
1570                         
1571                         private bool mouseButtonDown = false;
1572                         
1573                         private bool drawCross = true;
1574                         
1575                         private Color color;
1576                         
1577                         private int currentXPos;
1578                         private int currentYPos;
1579                         
1580                         private float xstep;
1581                         private float ystep;
1582                         
1583                         private ColorDialog colorDialog;
1584                         
1585                         public ColorMatrixControl (ColorDialog colorDialog)
1586                         {
1587                                 this.colorDialog = colorDialog;
1588                                 
1589                                 SuspendLayout ();
1590                                 
1591                                 BorderStyle = BorderStyle.Fixed3D;
1592                                 Location = new Point (0, 0);
1593                                 Size = new Size (179, 190);
1594                                 TabIndex = 0;
1595                                 TabStop = false;
1596                                 
1597                                 ResumeLayout (false);
1598                                 
1599                                 xstep = 240.0f / (ClientSize.Width - 1);
1600                                 ystep = 241.0f / (ClientSize.Height - 1);
1601                                 
1602                                 SetStyle (ControlStyles.DoubleBuffer, true);
1603                                 SetStyle (ControlStyles.AllPaintingInWmPaint, true);
1604                                 SetStyle (ControlStyles.UserPaint, true);
1605                         }
1606                         
1607                         protected override void OnPaint (PaintEventArgs e)
1608                         {
1609                                 if (drawingBitmap == null)
1610                                         drawingBitmap = new DrawingBitmap (ClientSize);
1611                                 
1612                                 Draw (e);
1613                                 
1614                                 base.OnPaint (e);
1615                         }
1616                         
1617                         private void Draw (PaintEventArgs e)
1618                         {
1619                                 e.Graphics.DrawImage (drawingBitmap.Bitmap, ClientRectangle.X, ClientRectangle.Y);
1620                                 
1621                                 // drawCross is false if the mouse gets moved...
1622                                 if (drawCross) {
1623                                         e.Graphics.DrawImage (crossCursor.Bitmap, currentXPos - 11 , currentYPos - 11);
1624                                 }
1625                         }
1626                         
1627                         protected override void OnMouseDown (MouseEventArgs e)
1628                         {
1629                                 mouseButtonDown = true;
1630                                 currentXPos = e.X;
1631                                 currentYPos = e.Y;
1632                                 if (drawCross) {
1633                                         drawCross = false;
1634                                         Invalidate ();
1635                                         Update ();
1636                                 }
1637                                 
1638                                 UpdateControls ();
1639                                 XplatUI.GrabWindow (Handle, Handle);
1640                                 
1641                                 base.OnMouseDown (e);
1642                         }
1643                         
1644                         protected override void OnMouseMove (MouseEventArgs e)
1645                         {
1646                                 if (mouseButtonDown)
1647                                         if ((e.X < ClientSize.Width && e.X >= 0) && (e.Y < ClientSize.Height && e.Y >= 0)) {
1648                                                 currentXPos = e.X;
1649                                                 currentYPos = e.Y;
1650                                                 UpdateControls ();
1651                                         }
1652                                 
1653                                 base.OnMouseMove (e);
1654                         }
1655                         
1656                         protected override void OnMouseUp (MouseEventArgs e)
1657                         {
1658                                 XplatUI.UngrabWindow (Handle);
1659                                 mouseButtonDown = false;
1660                                 drawCross = true;
1661                                 Invalidate ();
1662                                 Update ();
1663                         }
1664                         
1665                         public Color ColorToShow {
1666                                 set {
1667                                         ComputePos (value);
1668                                 }
1669                         }
1670                         
1671                         private void ComputePos (Color acolor)
1672                         {
1673                                 if (acolor != color) {
1674                                         color = acolor;
1675                                         
1676                                         HSB hsb = HSB.RGB2HSB (color);
1677                                         
1678                                         currentXPos = (int)((float)hsb.hue / xstep);
1679                                         currentYPos = ClientSize.Height - 1 - (int)((float)hsb.sat / ystep);
1680                                         
1681                                         if (currentXPos < 0)
1682                                                 currentXPos = 0;
1683                                         if (currentYPos < 0)
1684                                                 currentYPos = 0;
1685                                         
1686                                         Invalidate ();
1687                                         Update ();
1688                                 }
1689                         }
1690                         
1691                         private Color GetColorFromHSB ()
1692                         {
1693                                 int hue = (int)((float)currentXPos * xstep);
1694                                 int sat = 240 - ((int)((float)currentYPos * ystep));
1695                                 int bri = TriangleControl.CurrentBrightness;
1696                                 
1697                                 return HSB.HSB2RGB (hue, sat, bri);
1698                         }
1699                         
1700                         private void UpdateControls ()
1701                         {
1702                                 Color tmpColor = GetColorFromHSB ();
1703                                 
1704                                 // update the brightness control
1705                                 colorDialog.brightnessControl.ShowColor ((int)((float)currentXPos * xstep), 240 - ((int)((float)currentYPos * ystep)));
1706                                 
1707                                 // update saturation text box
1708                                 int satvalue = (240 - ((int)((float)currentYPos * ystep)));
1709                                 colorDialog.satTextBox.Text = satvalue.ToString ();
1710                                 
1711                                 // update hue text box
1712                                 int huevalue = (int)((float)currentXPos * xstep);
1713                                 if (huevalue > 239)
1714                                         huevalue = 239;
1715                                 colorDialog.hueTextBox.Text = huevalue.ToString ();
1716                                 
1717                                 // update the main selected color panel
1718                                 colorDialog.selectedColorPanel.BackColor = tmpColor;
1719                                 
1720                                 // and finally the rgb text boxes
1721                                 colorDialog.UpdateRGBTextBoxes (tmpColor);
1722                         }
1723                 }
1724                 
1725                 
1726                 internal class BrightnessControl : Panel {
1727                         internal class DrawingBitmap {
1728                                 private Bitmap bitmap;
1729                                 
1730                                 public DrawingBitmap ()
1731                                 {
1732                                         bitmap = new Bitmap (14, 190);
1733                                 }
1734                                 
1735                                 public Bitmap Bitmap {
1736                                         set {
1737                                                 bitmap = value;
1738                                         }
1739                                         
1740                                         get {
1741                                                 return bitmap;
1742                                         }
1743                                 }
1744                                 
1745                                 // only hue and saturation are needed.
1746                                 // color will be computed with an iteration
1747                                 public void Draw (int hue, int sat)
1748                                 {
1749                                         float brisub = 241.0f / 190.0f;
1750                                         float bri = 241.0f;
1751                                         
1752                                         for (int height = 0; height < 190; height++) {
1753                                                 for (int width = 0; width < 14; width++) {
1754                                                         Color pixcolor = HSB.HSB2RGB (hue, sat, (int)bri);
1755                                                         bitmap.SetPixel (width, height, pixcolor);
1756                                                 }
1757                                                 bri = bri - brisub;
1758                                         }
1759                                 }
1760                         }
1761                         
1762                         private const float step = 241.0f/189.0f;
1763                         
1764                         private DrawingBitmap bitmap;
1765                         
1766                         private ColorDialog colorDialog = null;
1767                         
1768                         public BrightnessControl (ColorDialog colorDialog)
1769                         {
1770                                 this.colorDialog = colorDialog;
1771                                 
1772                                 bitmap = new DrawingBitmap ();
1773                                 
1774                                 SuspendLayout ();
1775                                 
1776                                 BorderStyle = BorderStyle.Fixed3D;
1777                                 Location = new Point (0, 0);
1778                                 Size = new Size (14, 190);
1779                                 TabIndex = 0;
1780                                 TabStop = false;
1781                                 Size = new Size (14, 190);
1782                                 
1783                                 ResumeLayout (false);
1784                                 
1785                                 SetStyle (ControlStyles.DoubleBuffer, true);
1786                                 SetStyle (ControlStyles.AllPaintingInWmPaint, true);
1787                                 SetStyle (ControlStyles.UserPaint, true);
1788                         }
1789                         
1790                         
1791                         protected override void OnPaint (PaintEventArgs e)
1792                         {
1793                                 e.Graphics.DrawImage (bitmap.Bitmap, 0, 0);
1794                                 
1795                                 base.OnPaint (e);
1796                         }
1797                         
1798                         protected override void OnMouseDown (MouseEventArgs e)
1799                         {
1800                                 colorDialog.triangleControl.TrianglePosition = (int)((float)(189 - e.Y) * step);
1801                                 
1802                                 base.OnMouseDown (e);
1803                         }
1804                         
1805                         // this one is for ColorMatrixControl
1806                         public void ShowColor (int hue, int sat)
1807                         {
1808                                 bitmap.Draw (hue, sat);
1809                                 Invalidate ();
1810                                 Update ();
1811                         }
1812                         
1813                         // this one for the other controls
1814                         public Color ColorToShow {
1815                                 set {
1816                                         int hue, sat;
1817                                         HSB.GetHueSaturation (value, out hue, out sat);
1818                                         bitmap.Draw (hue, sat);
1819                                         Invalidate ();
1820                                         Update ();
1821                                 }
1822                         }
1823                 }
1824                 
1825                 
1826                 internal class TriangleControl : Panel {
1827                         private bool mouseButtonDown = false;
1828                         
1829                         private int currentTrianglePosition = 195;
1830 //                      private Rectangle clipRectangle;
1831                         
1832                         private const float briStep = 241.0f/186.0f;
1833                         
1834                         private static int currentBrightness = 0;
1835                         
1836                         private ColorDialog colorDialog;
1837                         
1838                         public TriangleControl (ColorDialog colorDialog)
1839                         {
1840                                 this.colorDialog = colorDialog;
1841                                 
1842                                 SuspendLayout ();
1843                                 
1844                                 Size = new Size (16, 203);
1845                                 
1846                                 ResumeLayout (false);
1847                                 
1848                                 SetStyle (ControlStyles.DoubleBuffer, true);
1849                                 SetStyle (ControlStyles.AllPaintingInWmPaint, true);
1850                                 SetStyle (ControlStyles.UserPaint, true);
1851                         }
1852                         
1853                         public static int CurrentBrightness {
1854                                 set {
1855                                         currentBrightness = value;
1856                                 }
1857                                 
1858                                 get {
1859                                         return currentBrightness;
1860                                 }
1861                         }
1862                         
1863                         protected override void OnPaint (PaintEventArgs e)
1864                         {
1865                                 Draw (e);
1866                                 
1867                                 base.OnPaint (e);
1868                         }
1869                         
1870                         private void Draw (PaintEventArgs e)
1871                         {
1872                                 e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), new Rectangle (0, 0, 16, 203));
1873                                 
1874                                 Point[] trianglePoints = new Point [3]
1875                                 {
1876                                         new Point (0, currentTrianglePosition),
1877                                         new Point (8, currentTrianglePosition - 8),
1878                                         new Point (8, currentTrianglePosition + 8)
1879                                 };
1880                                 
1881                                 e.Graphics.FillPolygon (ThemeEngine.Current.ResPool.GetSolidBrush (Color.Black), trianglePoints);
1882                         }
1883                         
1884                         protected override void OnMouseDown (MouseEventArgs e)
1885                         {
1886                                 if (e.Y > 195 || e.Y < 9) return; // helper until Cursor.Clip works
1887                                 
1888                                 mouseButtonDown = true;
1889                                 currentTrianglePosition = e.Y;
1890                                 
1891                                 // Cursor.Clip doesn't yet work in Managed.Windows.Forms
1892 //                              clipRectangle = Cursor.Clip;
1893 //                              Point p = Location;
1894 //                              p.Y += 8;
1895 //                              Size s = Size;
1896 //                              s.Width -= 5;
1897 //                              s.Height -= 16;
1898 //                              Cursor.Clip = new Rectangle( Parent.PointToScreen( p ), s );
1899                                 
1900                                 colorDialog.briTextBox.Text = TrianglePosition.ToString ();
1901                                 colorDialog.UpdateFromHSBTextBoxes ();
1902                                 
1903                                 Invalidate ();
1904                                 Update ();
1905                                 
1906                                 base.OnMouseDown (e);
1907                         }
1908                         
1909                         protected override void OnMouseMove (MouseEventArgs e)
1910                         {
1911                                 if (mouseButtonDown)
1912                                         if (e.Y < 196 && e.Y > 8) {
1913                                                 currentTrianglePosition = e.Y;
1914                                                 
1915                                                 colorDialog.briTextBox.Text = TrianglePosition.ToString ();
1916                                                 colorDialog.UpdateFromHSBTextBoxes ();
1917                                                 
1918                                                 Invalidate ();
1919                                                 Update ();
1920                                         }
1921                                 
1922                                 base.OnMouseMove (e);
1923                         }
1924                         
1925                         protected override void OnMouseUp (MouseEventArgs e)
1926                         {
1927                                 mouseButtonDown = false;
1928 //                              Cursor.Clip = clipRectangle;
1929                                 
1930                                 base.OnMouseUp (e);
1931                         }
1932                         
1933                         public int TrianglePosition {
1934                                 get {
1935                                         float tmp = (float)(currentTrianglePosition - 9);
1936                                         tmp = tmp * briStep;
1937                                         
1938                                         int retval = 240 - (int)tmp;
1939                                         
1940                                         TriangleControl.CurrentBrightness = retval;
1941                                         
1942                                         return retval;
1943                                 }
1944                                 
1945                                 set {
1946                                         float tmp = (float)value / briStep;
1947                                         currentTrianglePosition = 186 - (int)tmp + 9;
1948                                         
1949                                         colorDialog.briTextBox.Text = TrianglePosition.ToString ();
1950                                         
1951                                         colorDialog.UpdateFromHSBTextBoxes ();
1952                                         
1953                                         Invalidate ();
1954                                         Update ();
1955                                 }
1956                         }
1957                         
1958                         public Color ColorToShow {
1959                                 set {
1960                                         SetColor (value);
1961                                 }
1962                         }
1963                         
1964                         public void SetColor (Color color)
1965                         {
1966                                 int pos_raw = HSB.Brightness (color);
1967                                 
1968                                 float tmp = (float)pos_raw / briStep;
1969                                 currentTrianglePosition = 186 - (int)tmp + 9;
1970                                 
1971                                 if (colorDialog.edit_textbox == null)
1972                                         colorDialog.briTextBox.Text = TrianglePosition.ToString ();
1973                                 
1974                                 Invalidate ();
1975                         }
1976                 }
1977                 #endregion
1978         }
1979 }
1980