// ALL RIGHTS RESERVED:
// ====================
// The contents of this file, and associated files in this directory, are 
// Copyright (C) SPT , all rights reserved, 2009.
// All software Source Code, Images, Database-Design and code, Graphics Design 
// and source files, and related content (collectively referred to as SOURCE) are 
// Copyright (c) SPT, 2009, All Rights Reserved.
// http://SerialPortTool.com

// PTZ Controller is a Registered Trademark of SerialPortTool.com.
using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace PTZController
{
    class SkinListBox : ListBox
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct SIZE
        {
            public int cx;
            public int cy;
        }

        [DllImport("user32.dll")]
        public static extern int GetSystemMetrics(int index);

        private const int SM_CXVSCROLL = 20; // vertical scroll bar width

        public SkinListBox()
        {
            m_toolTip.InitialDelay = 500;
            m_toolTip.ReshowDelay = 500;
            m_toolTip.AutoPopDelay = 3000;
            m_toolTip.Active = true;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            int index = IndexFromPoint(e.X, e.Y);

            if (index != ListBox.NoMatches)
            {
                if (m_lastIndex != index)
                {
                    string s = Items[index].ToString();
                    Graphics g = this.CreateGraphics();
                    SizeF sz = g.MeasureString(s, this.Font);

                    int w = this.Width;

                    if (this.ScrollAlwaysVisible)
                    {
                        w -= GetSystemMetrics(SM_CXVSCROLL);
                    }

                    if (w <= sz.Width)
                    {
                        m_toolTip.Active = true;
                        m_toolTip.SetToolTip(this, s);
                    }
                    else
                    {
                        m_toolTip.Active = false;
                    }

                    m_lastIndex = index;
                }
            }
        }

        private ToolTip m_toolTip = new ToolTip();
        private int m_lastIndex = -1;

        private Image m_background;

        public Image Background
        {
            get { return m_background; }
            set { m_background = value; }
        }
    }
}
