// TODO: get rid of this.

        protected override void OnLayout(LayoutEventArgs levent)
        {
            base.OnLayout(levent);

            // write file CustomSwatches.xml
            //////////if (File.Exists(m_customSwatchesFile))
            //////////{
            //////////    m_swatches = ColorSwatchXml.ReadSwatches(m_customSwatchesFile, false);
            //////////}
            //////////else
            //////////{

            //////////CreateCustomSwatchesFile();
            m_swatches = ColorSwatchXml.ReadSwatches("ColorSwatches.xml", true);

            ////}

            m_swatchOuterRegionWidth  = this.Width - ((this.Width - (2 * OUTER_PADDING))) % (SWATCH_WIDTH + PADDING);
            m_swatchOuterRegionHeight = this.Height - ((this.Height - (2 * OUTER_PADDING))) % (SWATCH_HEIGHT + PADDING);

            int horizSwatches = (m_swatchOuterRegionWidth - (2 * OUTER_PADDING)) / (SWATCH_WIDTH + PADDING);
            int vertSwatches  = (m_swatchOuterRegionHeight - (2 * OUTER_PADDING)) / (SWATCH_HEIGHT + PADDING);

            m_swatchArray = new ColorSwatch[horizSwatches, vertSwatches];
        }
        private void AddColor(Color c)
        {
            bool  writeSwatchesToFile  = false;
            Point nextEmptySwatchPoint = m_nextEmptySwatchPoint;

            if (DoesColorAlreadyExist(c))
            {
                MessageBox.Show(this.Parent, "Color already exists.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (m_numberOfEmptySwatches <= 0)
            {
                MessageBox.Show(this.Parent, "There are no empty swatches available.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                using (AddNewColorSwatchForm colorForm = new AddNewColorSwatchForm(c))
                {
                    colorForm.StartPosition = FormStartPosition.CenterParent;
                    colorForm.ShowInTaskbar = false;

                    if (colorForm.ShowDialog() == DialogResult.OK)
                    {
                        if (m_swatchBitmap != null)
                        {
                            int x = m_nextEmptySwatchPoint.X;
                            int y = m_nextEmptySwatchPoint.Y;

                            using (Graphics g = Graphics.FromImage(m_swatchBitmap))
                            {
                                using (SolidBrush sb = new SolidBrush(c))
                                {
                                    g.FillRectangle(sb, x, y, 10, 10);
                                }

                                g.DrawRectangle(Pens.Black, x, y, 10, 10);
                            }

                            m_numberOfEmptySwatches--;

                            ColorSwatch cs = new ColorSwatch(
                                c,
                                colorForm.ColorDescription,
                                new Point(x, y),
                                new Size(SWATCH_WIDTH, SWATCH_HEIGHT));

                            m_swatchArray[m_swatchColumnNum++, m_swatchRowNum] = cs;
                            m_swatches.Add(cs);
                            writeSwatchesToFile = true;

                            UpdatePositions(ref x, ref y, ref m_swatchColumnNum, ref m_swatchRowNum);
                            m_nextEmptySwatchPoint = new Point(x, y);
                        }
                    }
                }
            }

            if (writeSwatchesToFile)
            {
                ColorSwatchXml.WriteSwatches(m_customSwatchesFile, m_swatches);
            }

            m_paintActiveEmptySwatch = false;
            this.InvalidateEmptySwatch(nextEmptySwatchPoint);
        }