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

namespace _1337504_RadTrackBarRanges
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.radTrackBar1.Minimum = 0;
            this.radTrackBar1.Maximum = 60;

            this.radTrackBar1.TrackBarMode = Telerik.WinControls.UI.TrackBarRangeMode.Range;

            this.radTrackBar1.Ranges[0].Start = 4;
            this.radTrackBar1.Ranges[0].End = 15;
        }

        TrackBarIndicatorElement indicator;
        CustomTrackBarThumbElement thumb;

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            indicator = this.radTrackBar1.TrackBarElement.FindDescendant<TrackBarIndicatorElement>();
            thumb = new CustomTrackBarThumbElement(indicator, false);
            var range = indicator.RangeElement;
            thumb.Alignment = ContentAlignment.MiddleLeft;
            indicator.RangeElement.Children.Add(thumb);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int step = 5;
            float offset = thumb.PositionOffset.Width + step;
            thumb.PositionOffset = new SizeF(Math.Min(indicator.RangeElement.DesiredSize.Width - thumb.DesiredSize.Width, offset), 0);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int step = -5;
            float offset = thumb.PositionOffset.Width + step;
            thumb.PositionOffset = new SizeF(Math.Max(0, offset), 0);
        }
    }

    public class CustomTrackBarThumbElement : TrackBarThumbElement
    {
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(TrackBarThumbElement);
            }
        }

        public CustomTrackBarThumbElement(TrackBarIndicatorElement owner, bool isFirst)
            : base(owner, isFirst)
        {
        }

        protected override void OnLoaded()
        {
            if ((this.Parent as TrackBarIndicatorElement) == null)
            {
                return;
            }

            base.OnLoaded();
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            //The base implementation should not be called as it will try to select the thumb and work with its range info which does not exist for the custom thumb.
        }
    }
}
