using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using C1.Win.C1Ribbon;

namespace ControlHostDemo
{
    public partial class DemoForm : C1RibbonForm
    {
        private bool _internalFontChange;

        public DemoForm()
        {
            InitializeComponent();

            Font font = label1.Font;
            _internalFontChange = true;
            fontEditor.SelectedFont = font;
            numericUpDownHost1.Value = Convert.ToDecimal(font.SizeInPoints);
            _internalFontChange = false;

            backColorEditor.SelectedColor = label1.BackColor;
            foreColorEditor.SelectedColor = label1.ForeColor;

            axWebBrowserHost1.Navigate(Application.StartupPath +
                @"\..\..\Html\A plea for C++.htm");
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void pictureBoxHost1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("You clicked the Earth.", "Congratulations", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void numericUpDownHost1_ValueChanged(object sender, EventArgs e)
        {
            if (!_internalFontChange)
            {
                Font font = label1.Font;
                font = new Font(font.Name, Convert.ToSingle(numericUpDownHost1.Value), font.Style, font.Unit);
                _internalFontChange = true;
                label1.Font = font;
                fontEditor.SelectedFont = font;
                _internalFontChange = false;
            }
            statusLabel.Text = String.Format("Number is {0}", numericUpDownHost1.Value);
        }

        private void checkedListBoxHost1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            CheckedListBox.ObjectCollection coll = checkedListBoxHost1.Items;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < coll.Count; i++)
            {
                bool isChecked = false;
                if (i == e.Index)
                    isChecked = e.NewValue == CheckState.Checked;
                else
                    isChecked = checkedListBoxHost1.GetItemChecked(i);
                if (isChecked)
                {
                    sb.Append(coll[i]).Append(", ");
                }
            }
            string res;
            if (sb.Length > 0)
            {
                res = sb.ToString(0, sb.Length - 2);
                label1.Text = res;
            }
            else
            {
                res = "<none>";
                label1.Text = "Demo";
            }
            checkStatusLabel.Text = "Checked: " + res;
        }

        private void backColorEditor_SelectedColorChanged(object sender, EventArgs e)
        {
            label1.BackColor = backColorEditor.SelectedColor;
        }

        private void foreColorEditor_SelectedColorChanged(object sender, EventArgs e)
        {
            label1.ForeColor = foreColorEditor.SelectedColor;
        }

        private void fontEditor_SelectedFontChanged(object sender, EventArgs e)
        {
            if (!_internalFontChange)
            {
                _internalFontChange = true;
                label1.Font = fontEditor.SelectedFont;
                numericUpDownHost1.Value = Convert.ToDecimal(fontEditor.SelectedFont.SizeInPoints);
                _internalFontChange = false;
            }
        }

        private void DemoForm_Resize(object sender, EventArgs e)
        {
            axWebBrowserHost1.Width = Width - 40;
        }
    }
}