//----------------------------------------------------------------------------
// ColorPickerForm.cs
//
// Defines drop-down form used by the ColorPicketControl.
//
// This is a part of the C1ReportDesigner package.
// Copyright (C) GrapeCity, Inc
// All rights reserved.
//
// This source code is intended as a supplement to the
// C1ReportDesigner control and related documentation 
// provided with the product.
// See these sources for detailed information regarding the
// C1ReportDesigner product.
//----------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using System.Globalization;

namespace C1.Design
{
	/// <summary>
	/// Defines drop-down form used by the ColorPicketControl.
	/// </summary>
    internal class ColorPickerForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.ListBox lbWebColors;
		// <summary>
		// Required designer variable.
		// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.TabPage tpWeb;
		private System.Windows.Forms.TabPage tpSystem;
		private System.Windows.Forms.ListBox lbSysColors;
		private System.Windows.Forms.TabPage tpCustom;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.NumericUpDown udR;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.NumericUpDown udB;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.NumericUpDown udG;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.NumericUpDown udA;
		private System.Windows.Forms.Button btnCustColor;
		private System.Windows.Forms.Panel pnlPal;

        //
		private Color _clr = Color.Red;
		private static List<Color> webClrs = new List<Color>();
        private static List<Color> sysClrs = new List<Color>();
        private Color[] _webColors = null;
        private Color[] _sysColors = null;
        private bool _canClose = false;
        private bool _initCust = false;

		public Color Color
		{
			get { return _clr; }
			set { _clr = value; }
		}

        public ColorPickerForm()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();


            //
            Size = new Size(Size.Width, Size.Height - FontHeight);
            InitWebColors();
            Color[,] clrs = new Color[7, 7];
            for (int i = 0; i < 7; i++)
                clrs[0, i] = Color.FromArgb((i * 255) / 6, (i * 255) / 6, (i * 255) / 6);
            for (int i = 0; i < 7; i++)
                clrs[1, i] = Color.FromArgb(96 + (i * 159) / 6, 0, 0);
            for (int i = 0; i < 7; i++)
                clrs[2, i] = Color.FromArgb(0, 96 + (i * 159) / 6, 0);
            for (int i = 0; i < 7; i++)
                clrs[3, i] = Color.FromArgb(0, 0, 96 + (i * 159) / 6);
            for (int i = 0; i < 7; i++)
                clrs[4, i] = Color.FromArgb(0, 96 + (i * 159) / 6, 96 + (i * 159) / 6);
            for (int i = 0; i < 7; i++)
                clrs[5, i] = Color.FromArgb(96 + (i * 159) / 6, 0, 96 + (i * 159) / 6);
            for (int i = 0; i < 7; i++)
                clrs[6, i] = Color.FromArgb(96 + (i * 159) / 6, 96 + (i * 159) / 6, 0);

            GenerateButtons(pnlPal, 7, 7, clrs);
        }

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        private Color[] WebColors
        {
            get
            {
                if (this._webColors == null)
                    this._webColors = ColorPickerControl.GetConstants((typeof(Color)));
                return this._webColors;
            }
        }

        private Color[] SystemColors
        {
            get
            {
                if (this._sysColors == null)
                    this._sysColors = ColorPickerControl.GetConstants(typeof(SystemColors));
                return this._sysColors;
            }
        }

        private void InitWebColors()
		{
            Array.Sort<Color>(this.WebColors, ColorPickerControl.ColorComparer.Instance);
            Array.Sort<Color>(this.SystemColors, ColorPickerControl.SystemColorComparer.Instance);

            for (int i = 0; i < this.SystemColors.Length; i++)
                lbSysColors.Items.Add(this.SystemColors[i]);

            for (int i = 0; i < this.WebColors.Length; i++)
                lbWebColors.Items.Add(this.WebColors[i]);
		}

		#region Windows Form Designer generated code
		// <summary>
		// Required method for Designer support - do not modify
		// the contents of this method with the code editor.
		// </summary>
		private void InitializeComponent()
		{
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tpWeb = new System.Windows.Forms.TabPage();
            this.lbWebColors = new System.Windows.Forms.ListBox();
            this.tpSystem = new System.Windows.Forms.TabPage();
            this.lbSysColors = new System.Windows.Forms.ListBox();
            this.tpCustom = new System.Windows.Forms.TabPage();
            this.udA = new System.Windows.Forms.NumericUpDown();
            this.label5 = new System.Windows.Forms.Label();
            this.pnlPal = new System.Windows.Forms.Panel();
            this.btnCustColor = new System.Windows.Forms.Button();
            this.label4 = new System.Windows.Forms.Label();
            this.udG = new System.Windows.Forms.NumericUpDown();
            this.label3 = new System.Windows.Forms.Label();
            this.udB = new System.Windows.Forms.NumericUpDown();
            this.label2 = new System.Windows.Forms.Label();
            this.udR = new System.Windows.Forms.NumericUpDown();
            this.label1 = new System.Windows.Forms.Label();
            this.tabControl1.SuspendLayout();
            this.tpWeb.SuspendLayout();
            this.tpSystem.SuspendLayout();
            this.tpCustom.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.udA)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.udG)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.udB)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.udR)).BeginInit();
            this.SuspendLayout();
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tpWeb);
            this.tabControl1.Controls.Add(this.tpSystem);
            this.tabControl1.Controls.Add(this.tpCustom);
            this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControl1.Location = new System.Drawing.Point(0, 0);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(197, 234);
            this.tabControl1.TabIndex = 0;
            this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
            this.tabControl1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ColorPickerForm_KeyDown);
            // 
            // tpWeb
            // 
            this.tpWeb.Controls.Add(this.lbWebColors);
            this.tpWeb.Location = new System.Drawing.Point(4, 22);
            this.tpWeb.Name = "tpWeb";
            this.tpWeb.Size = new System.Drawing.Size(186, 208);
            this.tpWeb.TabIndex = 0;
            this.tpWeb.Text = "Web";
            // 
            // lbWebColors
            // 
            this.lbWebColors.BackColor = System.Drawing.SystemColors.Control;
            this.lbWebColors.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.lbWebColors.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbWebColors.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
            this.lbWebColors.IntegralHeight = false;
            this.lbWebColors.Location = new System.Drawing.Point(0, 0);
            this.lbWebColors.Name = "lbWebColors";
            this.lbWebColors.Size = new System.Drawing.Size(186, 208);
            this.lbWebColors.TabIndex = 0;
            this.lbWebColors.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.lbWebColors_DrawItem);
            this.lbWebColors.SelectedIndexChanged += new System.EventHandler(this.lbWebColors_SelectedIndexChanged);
            this.lbWebColors.KeyDown += new System.Windows.Forms.KeyEventHandler(this.lbWebColors_KeyDown);
            // 
            // tpSystem
            // 
            this.tpSystem.Controls.Add(this.lbSysColors);
            this.tpSystem.Location = new System.Drawing.Point(4, 22);
            this.tpSystem.Name = "tpSystem";
            this.tpSystem.Size = new System.Drawing.Size(186, 208);
            this.tpSystem.TabIndex = 1;
            this.tpSystem.Text = "System";
            // 
            // lbSysColors
            // 
            this.lbSysColors.BackColor = System.Drawing.SystemColors.Control;
            this.lbSysColors.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lbSysColors.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbSysColors.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
            this.lbSysColors.IntegralHeight = false;
            this.lbSysColors.Location = new System.Drawing.Point(0, 0);
            this.lbSysColors.Name = "lbSysColors";
            this.lbSysColors.Size = new System.Drawing.Size(186, 208);
            this.lbSysColors.TabIndex = 1;
            this.lbSysColors.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.lbWebColors_DrawItem);
            this.lbSysColors.SelectedIndexChanged += new System.EventHandler(this.lbWebColors_SelectedIndexChanged);
            // 
            // tpCustom
            // 
            this.tpCustom.Controls.Add(this.udA);
            this.tpCustom.Controls.Add(this.label5);
            this.tpCustom.Controls.Add(this.pnlPal);
            this.tpCustom.Controls.Add(this.btnCustColor);
            this.tpCustom.Controls.Add(this.label4);
            this.tpCustom.Controls.Add(this.udG);
            this.tpCustom.Controls.Add(this.label3);
            this.tpCustom.Controls.Add(this.udB);
            this.tpCustom.Controls.Add(this.label2);
            this.tpCustom.Controls.Add(this.udR);
            this.tpCustom.Controls.Add(this.label1);
            this.tpCustom.Location = new System.Drawing.Point(4, 22);
            this.tpCustom.Name = "tpCustom";
            this.tpCustom.Size = new System.Drawing.Size(189, 208);
            this.tpCustom.TabIndex = 2;
            this.tpCustom.Text = "Custom";
            // 
            // udA
            // 
            this.udA.Location = new System.Drawing.Point(4, 48);
            this.udA.Maximum = new decimal(new int[] {
            255,
            0,
            0,
            0});
            this.udA.Name = "udA";
            this.udA.Size = new System.Drawing.Size(40, 21);
            this.udA.TabIndex = 7;
            this.udA.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.udA.ValueChanged += new System.EventHandler(this.udR_ValueChanged);
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(11, 34);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(18, 13);
            this.label5.TabIndex = 8;
            this.label5.Text = "A:";
            // 
            // pnlPal
            // 
            this.pnlPal.Location = new System.Drawing.Point(4, 72);
            this.pnlPal.Name = "pnlPal";
            this.pnlPal.Size = new System.Drawing.Size(184, 136);
            this.pnlPal.TabIndex = 17;
            // 
            // btnCustColor
            // 
            this.btnCustColor.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            this.btnCustColor.Location = new System.Drawing.Point(100, 6);
            this.btnCustColor.Name = "btnCustColor";
            this.btnCustColor.Size = new System.Drawing.Size(78, 25);
            this.btnCustColor.TabIndex = 9;
            this.btnCustColor.Click += new System.EventHandler(this.btnCustColor_Click);
            this.btnCustColor.BackColorChanged += new System.EventHandler(this.btnCustColor_BackColorChanged);
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.ForeColor = System.Drawing.Color.Green;
            this.label4.Location = new System.Drawing.Point(107, 34);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(18, 13);
            this.label4.TabIndex = 6;
            this.label4.Text = "G:";
            // 
            // udG
            // 
            this.udG.Location = new System.Drawing.Point(100, 48);
            this.udG.Maximum = new decimal(new int[] {
            255,
            0,
            0,
            0});
            this.udG.Name = "udG";
            this.udG.Size = new System.Drawing.Size(40, 21);
            this.udG.TabIndex = 5;
            this.udG.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.udG.ValueChanged += new System.EventHandler(this.udR_ValueChanged);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.ForeColor = System.Drawing.Color.Blue;
            this.label3.Location = new System.Drawing.Point(155, 34);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(17, 13);
            this.label3.TabIndex = 4;
            this.label3.Text = "B:";
            // 
            // udB
            // 
            this.udB.Location = new System.Drawing.Point(148, 48);
            this.udB.Maximum = new decimal(new int[] {
            255,
            0,
            0,
            0});
            this.udB.Name = "udB";
            this.udB.Size = new System.Drawing.Size(40, 21);
            this.udB.TabIndex = 3;
            this.udB.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.udB.ValueChanged += new System.EventHandler(this.udR_ValueChanged);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.ForeColor = System.Drawing.Color.Red;
            this.label2.Location = new System.Drawing.Point(59, 34);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(18, 13);
            this.label2.TabIndex = 2;
            this.label2.Text = "R:";
            // 
            // udR
            // 
            this.udR.Location = new System.Drawing.Point(52, 48);
            this.udR.Maximum = new decimal(new int[] {
            255,
            0,
            0,
            0});
            this.udR.Name = "udR";
            this.udR.Size = new System.Drawing.Size(40, 21);
            this.udR.TabIndex = 1;
            this.udR.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.udR.ValueChanged += new System.EventHandler(this.udR_ValueChanged);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(11, 12);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(78, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Selected color:";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // ColorPickerForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(194, 234);
            this.ControlBox = false;
            this.Controls.Add(this.tabControl1);
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.MinimumSize = new System.Drawing.Size(200, 240);
            this.Name = "ColorPickerForm";
            this.ShowInTaskbar = false;
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Deactivate += new System.EventHandler(this.ColorPickerForm_Deactivate);
            this.Shown += new System.EventHandler(this.ColorPickerForm_Shown);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ColorPickerForm_KeyDown);
            this.tabControl1.ResumeLayout(false);
            this.tpWeb.ResumeLayout(false);
            this.tpSystem.ResumeLayout(false);
            this.tpCustom.ResumeLayout(false);
            this.tpCustom.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.udA)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.udG)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.udB)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.udR)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion

        private void ColorPickerForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                Close();
        }

        private void lbWebColors_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            ListBox lb = sender as ListBox;
            e.DrawBackground();

            Color clr = (Color)lb.Items[e.Index];

            SolidBrush sb = new SolidBrush(clr);
            Pen pen = new Pen(e.ForeColor);
            e.Graphics.FillRectangle(sb, e.Bounds.X + 2, e.Bounds.Y + 2, 20, e.Bounds.Height - 4);
            e.Graphics.DrawRectangle(pen, e.Bounds.X + 2, e.Bounds.Y + 2, 20, e.Bounds.Height - 4);

            sb.Color = e.ForeColor;
            e.Graphics.DrawString(clr.Name, e.Font, sb, e.Bounds.X + 24, e.Bounds.Y);

            sb.Dispose();
            pen.Dispose();

            if (e.State == DrawItemState.Selected)
                e.DrawFocusRectangle();
        }

        private void lbWebColors_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                Close();
        }

		private void ColorPickerForm_Deactivate(object sender, System.EventArgs e)
		{
			Close();
		}

        private void lbWebColors_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            ListBox lb = sender as ListBox;
            if (lb != null)
            {
                _clr = (Color)lb.SelectedItem;
                this.DialogResult = DialogResult.OK;
                if (_canClose)
                    Close();
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (tabControl1.SelectedTab == tpCustom)
            {
                _initCust = true;
                udR.Value = _clr.R;
                udG.Value = _clr.G;
                udB.Value = _clr.B;
                udA.Value = _clr.A;
                btnCustColor.BackColor = _clr;
                _initCust = false;
            }
        }

        private void udR_ValueChanged(object sender, System.EventArgs e)
        {
            if (!_initCust)
            {
                _clr = Color.FromArgb((int)udA.Value, (int)udR.Value, (int)udG.Value, (int)udB.Value);
                btnCustColor.BackColor = _clr;
                btnCustColor.Text = _clr.Name;
            }
        }

		private void btnCustColor_Click(object sender, System.EventArgs e)
		{
			Close();
		}

        private void btnPalette_Click(object sender, System.EventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                btnCustColor.BackColor = btn.BackColor;
                Close();
            }
        }

        private void GenerateButtons(Panel pnl, int xn, int yn, Color[,] clrs)
        {
            int margin = 4;
            int btnW = pnl.Width / xn - margin;
            int btnH = pnl.Height / yn - margin;

            Button[,] btns = new Button[xn, yn];

            int locx = margin / 2;
            for (int ix = 0; ix < xn; ix++)
            {
                int locy = margin / 2;
                for (int iy = 0; iy < yn; iy++)
                {
                    btns[ix, iy] = new Button();
                    btns[ix, iy].Size = new Size(btnW, btnH);
                    btns[ix, iy].Location = new Point(locx, locy);
                    btns[ix, iy].FlatStyle = FlatStyle.Popup;

                    if (clrs != null)
                        btns[ix, iy].BackColor = clrs[ix, iy];

                    btns[ix, iy].Click += new System.EventHandler(btnPalette_Click);

                    btns[ix, iy].DialogResult = DialogResult.OK;
                    pnl.Controls.Add(btns[ix, iy]);

                    locy += btnH + margin;
                }

                locx += btnW + margin;
            }
        }

		private void btnCustColor_BackColorChanged(object sender, System.EventArgs e)
		{
			_clr = btnCustColor.BackColor;
		}

        private void ColorPickerForm_Shown(object sender, EventArgs e)
        {
            // sync the color with the UI
            int index = lbWebColors.Items.IndexOf(_clr);
            if (index != -1)
            {
                tabControl1.SelectedIndex = 0;
                lbWebColors.SelectedIndex = index;
            }
            else
            {
                index = lbSysColors.Items.IndexOf(_clr);
                if (index != -1)
                {
                    tabControl1.SelectedIndex = 1;
                    lbSysColors.SelectedIndex = index;
                }
                else
                    tabControl1.SelectedIndex = 2;
            }
            _canClose = true;
        }
	}
}
