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

namespace _1426971ListViewItemPadding
{
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
            new RadControlSpyForm().Show();

            this.radCheckedDropDownList1.CreatingVisualListItem += radCheckedDropDownList1_CreatingVisualListItem;
            this.radCheckedDropDownList1.DataSource = this.productsBindingSource;
            this.radCheckedDropDownList1.DisplayMember = "UnitPrice";
        }

        private void radCheckedDropDownList1_CreatingVisualListItem(object sender, CreatingVisualListItemEventArgs args)
        {
            args.VisualItem = new CustomVisualItem();
        }

        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);
        }

        public class CustomVisualItem : RadCheckedListVisualItem
        {
            StackLayoutPanel mainContainer;
            LightVisualElement titleElement;
            LightVisualElement photoElement;

            private ImagePrimitive image;

            public ImagePrimitive Image
            {
                get
                {
                    return this.image;
                }
            }

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

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

                mainContainer = new StackLayoutPanel();

                photoElement = new LightVisualElement();
                titleElement = new LightVisualElement();

                this.Children.Add(mainContainer);

                mainContainer.Orientation = Orientation.Horizontal;
                mainContainer.Children.Add(photoElement);
                photoElement.DrawBorder = true;
                photoElement.Shape = new CircleShape();
                photoElement.ImageLayout = System.Windows.Forms.ImageLayout.Zoom;
                photoElement.BackColor = Color.Transparent;
                photoElement.BackColor2 = Color.Transparent;
                photoElement.BackColor3 = Color.Transparent;
                photoElement.BackColor4 = Color.Transparent;
                photoElement.AutoSize = false;
                photoElement.Size = new System.Drawing.Size(20, 20);

                mainContainer.Children.Add(titleElement);
                titleElement.Padding = new Padding(30, 0, 0, 0);
                titleElement.AutoSize = true;
                titleElement.DrawBorder = true;
                titleElement.BorderBoxStyle = Telerik.WinControls.BorderBoxStyle.FourBorders;
                titleElement.BorderLeftWidth = 0;
                titleElement.BorderTopWidth = 1;
                titleElement.BorderRightWidth = 0;
                titleElement.BorderBottomWidth = 0;

                titleElement.NotifyParentOnMouseInput = true;
                photoElement.NotifyParentOnMouseInput = true;
            }

            private bool HasOleContainerHeader(byte[] imageByteArray)
            {
                const byte OleByte0 = 21;
                const byte OleByte1 = 28;
                return (imageByteArray[0] == OleByte0) && (imageByteArray[1] == OleByte1);
            }

            private Image GetImageFromData(byte[] imageData)
            {
                const int OleHeaderLength = 78;
                MemoryStream memoryStream = new MemoryStream();
                if (HasOleContainerHeader(imageData))
                {
                    memoryStream.Write(imageData, OleHeaderLength, imageData.Length - OleHeaderLength);
                }
                else
                {
                    memoryStream.Write(imageData, 0, imageData.Length);
                }
                Bitmap bitmap = new Bitmap(memoryStream);
                return bitmap.GetThumbnailImage(55, 65, null, new IntPtr());
            }

            public override void Synchronize()
            {
                base.Synchronize();
                this.StackLayout.Visibility = ElementVisibility.Hidden;
                DataRowView rowView = this.Data.DataBoundItem as DataRowView;
                if (rowView == null)
                {
                    return;
                }
                this.titleElement.Text = rowView.Row["ProductName"].ToString();
                this.photoElement.Image = GetImageFromData(rowView.Row["Picture"] as byte[]);
            }
        }
    }
}