Imports System.Drawing

''' <summary>
''' EventArgs for the DrawItem event.
''' </summary>
''' <remarks></remarks>
Public Class DrawItemEventArgs
    Inherits System.EventArgs

    Private _backColor As Color
    Private _foreColor As Color
    Private _font As Font
    Private _index As Integer = -1
    Private _graphics As Graphics
    Private _rect As Rectangle
    Private _state As DrawItemState

#Region "Constructors"

    '' Initializes a new instance
    Public Sub New(ByVal g As System.Drawing.Graphics, ByVal itemFont As System.Drawing.Font, _
                   ByVal itemBounds As Rectangle, ByVal itemIndex As Integer, _
                   ByVal itemState As DrawItemState, ByVal fore As Color, ByVal back As Color)
        Me._graphics = g
        Me._font = itemFont
        Me._rect = itemBounds
        Me._index = itemIndex
        Me._state = itemState
        Me._foreColor = fore
        Me._backColor = back
    End Sub

    '' Initializes a new instance
    Public Sub New(ByVal g As System.Drawing.Graphics, ByVal itemFont As System.Drawing.Font, _
                   ByVal itemBounds As Rectangle, ByVal itemIndex As Integer, _
                   ByVal itemState As DrawItemState)
        Me._graphics = g
        Me._font = itemFont
        Me._rect = itemBounds
        Me._index = itemIndex
        Me._state = itemState
    End Sub

#End Region

#Region "Properties"

    ''' <summary>
    ''' Gets the rectangle that represents the bounds of the item being drawn.
    ''' </summary>
    ''' <value>Rectangle bounds.</value>
    ''' <returns>Rectangle bounds.</returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Bounds() As Rectangle
        Get
            Return Me._rect
        End Get
    End Property

    ''' <summary>
    ''' Gets the DrawItemState specified for the item.
    ''' </summary>
    ''' <value>DrawItemState.</value>
    ''' <returns>DrawItemState.</returns>
    ''' <remarks></remarks>
    Public ReadOnly Property State() As DrawItemState
        Get
            Return Me._state
        End Get
    End Property

    ''' <summary>
    ''' Gets the specified graphics item.
    ''' </summary>
    ''' <value>Graphics.</value>
    ''' <returns>Graphics.</returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Graphics() As Graphics
        Get
            Return Me._graphics
        End Get
    End Property

    ''' <summary>
    ''' Gets the item index.
    ''' </summary>
    ''' <value>Integer index.</value>
    ''' <returns>Integer index.</returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Index() As Integer
        Get
            Return Me._index
        End Get
    End Property

    ''' <summary>
    ''' Gets the specified item font.
    ''' </summary>
    ''' <value>System.Drawing.Font.</value>
    ''' <returns>System.Drawing.Font.</returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Font() As Font
        Get
            Return Me._font
        End Get
    End Property

    ''' <summary>
    ''' Gets the specified background color.
    ''' </summary>
    ''' <value>Color.</value>
    ''' <returns>Color.</returns>
    ''' <remarks></remarks>
    Public ReadOnly Property BackColor() As Color
        Get
            Return Me._backColor
        End Get
    End Property

    ''' <summary>
    ''' Gets the specified foreground color.
    ''' </summary>
    ''' <value>Color.</value>
    ''' <returns>Color.</returns>
    ''' <remarks></remarks>
    Public ReadOnly Property ForeColor() As Color
        Get
            Return Me._foreColor
        End Get
    End Property

#End Region

#Region "Methods"

    ''' <summary>
    ''' Draws the background within the bounds specified in the DrawItemEventArgs 
    ''' constructor and with the appropriate color.
    ''' </summary>
    ''' <remarks></remarks>
    Public Overridable Sub DrawBackground()
        Dim bgBrush As Brush
        ' If state is 'selected'
        If Me._state = DrawItemState.Selected Then
            bgBrush = New SolidBrush(SystemColors.Highlight)
        Else
            bgBrush = New SolidBrush(Me._backColor)
        End If
        ' Get area rectangle
        Dim rc As Rectangle = GetBackgroundDrawArea()
        ' Fill background
        Me._graphics.FillRectangle(bgBrush, rc)
    End Sub

    ''' <summary>
    ''' Draws the background within the bounds specified in the DrawItemEventArgs 
    ''' constructor and with the specified color.
    ''' </summary>
    ''' <param name="withColor">Background color to paint.</param>
    ''' <remarks></remarks>
    Public Overridable Sub DrawBackground(ByVal withColor As Color)
        Dim bgBrush As Brush = New SolidBrush(withColor)
        ' Get area rectangle
        Dim rc As Rectangle = GetBackgroundDrawArea()
        ' Fill background
        Me._graphics.FillRectangle(bgBrush, rc)
    End Sub

    ''' <summary>
    ''' Draws the background within the specified bounds and with the specified color.
    ''' </summary>
    ''' <param name="withColor">Background color to paint.</param>
    ''' <param name="withBounds">Background paint boundaries.</param>
    ''' <remarks></remarks>
    Public Overridable Sub DrawBackground(ByVal withColor As Color, ByVal withBounds As Rectangle)
        Dim bgBrush As Brush = New SolidBrush(withColor)
        ' Get area rectangle
        Dim rc As Rectangle = withBounds
        ' Fill background
        Me._graphics.FillRectangle(bgBrush, rc)
    End Sub

    ''' <summary>
    ''' Draws a rectangle focus for the item.
    ''' </summary>
    ''' <remarks></remarks>
    Public Overridable Sub DrawFocusRectangle()
        Dim focusRect As Rectangle = Me._rect
        focusRect.Width -= 1
        focusRect.Inflate(-1, 0)
        Me._graphics.DrawRectangle(New Pen(SystemColors.Highlight), focusRect)
    End Sub

    ''' <summary>
    ''' Gets the actual drawing area for the item background.  
    ''' This may differ from the item bounds.
    ''' </summary>
    ''' <returns>Background drawing area Rectangle.</returns>
    ''' <remarks></remarks>
    Public Overridable Function GetBackgroundDrawArea() As Rectangle
        ' Get area rectangle and transform
        Dim rc As Rectangle = New Rectangle(Me._rect.X + 1, Me._rect.Y, Me._rect.Width, Me._rect.Height - 1)
        rc.Y += 1
        ' Return
        Return rc
    End Function

#End Region

End Class
