using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.Primitives;
using Telerik.WinControls.RadControlSpy;
using Telerik.WinControls.UI;

namespace _1097868GridHeaderImage
{
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
            new RadControlSpyForm().Show();
            this.radGridView1.SortChanging += radGridView1_SortChanging;
        }

        private void radGridView1_SortChanging(object sender, GridViewCollectionChangingEventArgs e)
        {
            if (this.radGridView1.MasterView.TableHeaderRow.Tag == "cancel sorting")
            {
                this.radGridView1.MasterView.TableHeaderRow.Tag = null;
                e.Cancel = true;
            }
        }

        private void RadForm1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'nwindDataSet.Products' table. You can move, or remove it, as needed.
            this.productsTableAdapter.Fill(this.nwindDataSet.Products);
         
            this.radGridView1.AutoGenerateColumns = true;
            this.radGridView1.DataSource = this.productsBindingSource;  
            
            CustomCheckBoxColumn myColumn = new CustomCheckBoxColumn();
            myColumn.HeaderText = "ProductName";
            myColumn.FieldName = "ProductName";
            myColumn.HeaderImage = Properties.Resources.Alerts;
            myColumn.AllowSort = true;
            this.radGridView1.Columns.Add(myColumn);
            this.radGridView1.BestFitColumns(Telerik.WinControls.UI.BestFitColumnMode.AllCells); 
        }

        public class ImageHeaderCell : GridHeaderCellElement
        {
            private LightVisualElement m_ImageButton;

            public LightVisualElement ImageButton
            {
                get
                {
                    return m_ImageButton;
                }
                set
                {
                    m_ImageButton = value;
                }
            }

            protected override Type ThemeEffectiveType
            {
                get
                {
                    return typeof(GridHeaderCellElement);
                }
            }

            public ImageHeaderCell(GridViewColumn column, GridRowElement row) : base(column, row)
            { 
            }
 
            protected override void DisposeManagedResources()
            {
                m_ImageButton.Click -= imageButton_Click;
                base.DisposeManagedResources();
            }

            protected override void CreateChildElements()
            {
                base.CreateChildElements();

                m_ImageButton = new LightVisualElement();
                m_ImageButton.StretchHorizontally = false;
                m_ImageButton.MinSize = new Size(20, 20);
                m_ImageButton.NotifyParentOnMouseInput = false;
                m_ImageButton.ShouldHandleMouseInput = true;
                m_ImageButton.Click += imageButton_Click;  
                this.Children.Add(m_ImageButton);
            }

            public override bool IsCompatible(Telerik.WinControls.UI.GridViewColumn data, object context)
            {
                return data is GridViewTextBoxColumn && context is GridTableHeaderRowElement && base.IsCompatible(data, context);
            }

            private void imageButton_Click(object sender, EventArgs e)
            { 
                MessageBox.Show("ImageButton Clicked");
                this.RowInfo.Tag = "cancel sorting";
            }

            protected override void SetContentCore(object value)
            {
                base.SetContentCore(value); 
                this.ImageButton.Image = this.ColumnInfo.HeaderImage;
                this.ImageButton.ImageLayout = System.Windows.Forms.ImageLayout.Zoom;
                this.DrawImage = false; 
            }
        }

        public class CustomCheckBoxColumn : GridViewTextBoxColumn
        {
            public override Type GetCellType(GridViewRowInfo row)
            {
                if (row is GridViewTableHeaderRowInfo)
                {
                    return typeof(ImageHeaderCell);
                }
                return base.GetCellType(row);
            }
        }
    }
}