Imports System.Threading
Imports System.Runtime.InteropServices
Imports System.Drawing.Drawing2D

Imports Stax.Win32
Imports Stax.AppFramework

Namespace UI
    Public Class PopUpForm
        Inherits Form

#Region " Windows Form Designer generated code "

        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.SuspendLayout()
            '
            'PopUp
            '
            Me.ClientSize = New System.Drawing.Size(198, 190)
            Me.ControlBox = False
            Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
            Me.KeyPreview = True
            Me.Name = "PopUp"
            Me.ShowInTaskbar = False
            Me.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide
            Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
            Me.ResumeLayout(False)

        End Sub

#End Region

        Private WordWrapValue As Integer = 300
        Private WordWrap As Boolean = True
        Private CenterOwner As Form
        Private Image As Image
        Private Title As String
        Private TextValue As String
        Private MarginValue As Integer = 4
        Private SizeName As SizeF
        Private SizeText As SizeF
        Private HeaderHeight As Integer
        Private Middle As Integer = 2
        Private CenterLocation As Point

        Public Sub New(ByVal title As String, ByVal text As String, ByVal image As Image, ByVal centerOwner As Form)
            MyBase.New()
            InitializeComponent()
            Me.Title = title
            TextValue = text
            Me.Image = image
            Me.CenterOwner = centerOwner
        End Sub

        Private HeaderFontValue As Font

        Public Property HeaderFont() As Font
            Get
                If HeaderFontValue Is Nothing Then
                    HeaderFontValue = New Font(Font.FontFamily, 9, FontStyle.Bold)
                End If

                Return HeaderFontValue
            End Get
            Set(ByVal Value As Font)
                HeaderFontValue = Value
            End Set
        End Property

        Protected Overrides Sub OnLoad(ByVal e As EventArgs)
            MyBase.OnLoad(e)

            'set size values
            Dim g As Graphics = CreateGraphics()

            If Not Title Is Nothing AndAlso Title <> "" Then
                SizeName = g.MeasureString(Title, HeaderFont)
            End If

            If WordWrap Then
                SizeText = g.MeasureString(TextValue, Font, WordWrapValue)
            Else
                SizeText = g.MeasureString(TextValue, Font)
            End If

            g.Dispose()

            ' set the width
            Width = CInt(SizeName.Width)

            If Image IsNot Nothing Then
                Width += Image.Width + MarginValue
            End If

            If SizeText.Width > Width Then
                Width = CInt(SizeText.Width)
            End If

            Width += MarginValue + MarginValue

            'set the height
            Dim tempHeight As Integer = CInt(SizeName.Height)

            If Image IsNot Nothing Then
                If Image.Height > tempHeight Then
                    tempHeight = Image.Height
                End If
            End If

            HeaderHeight = tempHeight

            If SizeName.Height = 0 Then
                Height = tempHeight + CInt(SizeText.Height) + MarginValue + MarginValue
            Else
                Height = tempHeight + CInt(SizeText.Height) + MarginValue + Middle + MarginValue
            End If

            If CenterOwner Is Nothing Then
                'set left
                Left = Control.MousePosition.X - Width \ 2

                If Left < 5 Then
                    Left = 5
                End If

                If Left + Width + MarginValue + MarginValue > SystemInformation.VirtualScreen.Width Then
                    Left = SystemInformation.VirtualScreen.Width - Width - MarginValue - MarginValue
                End If

                'set top
                If Control.MousePosition.Y < SystemInformation.VirtualScreen.Height \ 2 Then
                    Top = Control.MousePosition.Y - MarginValue - MarginValue
                Else
                    Top = Control.MousePosition.Y - Height + MarginValue + MarginValue
                End If
            Else
                Dim p As Point = CenterOwner.Location
                p.X += CenterOwner.Width \ 2
                p.Y += CenterOwner.Height \ 2
                p.X -= Width \ 2
                p.Y -= Height \ 2
                Location = p
            End If
        End Sub

        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)

            Dim g As Graphics = e.Graphics

            Dim gradientBrush As New LinearGradientBrush(ClientRectangle, _
                Color.White, Appearance.GradientColorLight, LinearGradientMode.ForwardDiagonal)

            g.FillRectangle(gradientBrush, ClientRectangle)
            gradientBrush.Dispose()

            'draw name

            If Not Title Is Nothing AndAlso Title <> "" Then
                If Image Is Nothing Then
                    g.DrawString(Title, HeaderFont, Brushes.Black, MarginValue, MarginValue)
                Else
                    g.DrawString(Title, HeaderFont, Brushes.Black, MarginValue + Image.Width + Middle, ((HeaderHeight - HeaderFont.Height) \ 2) + MarginValue)
                End If
            End If

            Dim textStart As Point
            textStart.X = MarginValue

            'draw text
            If HeaderHeight = 0 Then
                textStart.Y = MarginValue
            Else
                textStart.Y = MarginValue + HeaderHeight + Middle
            End If

            g.DrawString(TextValue, Font, Brushes.Black, New RectangleF(textStart, SizeText))

            'draw image
            If Image IsNot Nothing Then
                g.DrawImage(Image, MarginValue, MarginValue)
            End If

            ControlPaint.DrawBorder(g, ClientRectangle, _
                Appearance.PopUpBorderColor, ButtonBorderStyle.Solid)
        End Sub

        Private Sub PopUpTipForm_LostFocus(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.LostFocus
            Close()
            Dispose()
        End Sub

        Private Sub PopUpTipForm_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
            Close()
            Dispose()
        End Sub

        Private Sub PopUpTipForm_Click(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Click
            Close()
            Dispose()
        End Sub
    End Class
End Namespace