
Imports System.IO

Public Class SlideType

  Public Type As String
  Private FName As String
  Public ShortFileName As String
  Public Caption As String
  Public AlphanumericName As String
  Public PointSize As Integer
  'Public Image As Bitmap
  Private Thumbnail As Bitmap
  Public Index As Integer
  Public ImageWidth, ImageHeight As Integer
  Public ZfactorStart, ZfactorFinish As Double
  Public OriginStart, OriginFinish As Point
  Public Interval1, Interval2, Interval3 As Integer
  Public PanFlag As Boolean
  Public EnableFlag As Boolean
  Public SelectFlag As Boolean
  Public PauseFlag As Boolean
  Dim randomnumber As New Random

  Public Event SlideMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
  Public Event SlideClick(ByVal sender As Object, ByVal e As System.EventArgs)

  Public Property Filename() As String
    Get
      Return FName
    End Get
    Set(ByVal value As String)
      FName = value
      ShortFileName = IO.Path.GetFileNameWithoutExtension(FName).Trim
      Using TemporaryImage As Bitmap = GetImage()
        Select Case Type
          Case "BLANK"
            If FName = "New" Then Caption = "" Else Caption = ShortFileName ' needed for directories
            EnableFlag = True
            Interval1 = 0
            Interval2 = My.Settings.Interval2
            Interval3 = 0
            PointSize = 36
            PanFlag = False
            PauseFlag = False
            SelectFlag = False
            ShortFileName = ""
          Case "IMAGE", "MOVIE"
            PointSize = 24
            If FName = "New" Then
              ShortFileName = "" : Caption = ""
            Else
              If File.Exists(FName) Then
                If Type = "MOVIE" Then
                  mediaOpen(FName)
                  Interval2 = mediaGetLength()
                  mediaClose()
                End If
                EnableFlag = True
                Caption = ShortFileName
              Else
                Type = "NOT FOUND"
                EnableFlag = False
                Interval2 = 0
                PanFlag = False
                SelectFlag = False
              End If
            End If
            'remove initial number from Caption and create AlphanumericName
            AlphanumericName = ShortFileName
            Dim i As Integer = Caption.IndexOf("(")
            Dim j As Integer = Caption.IndexOf(")")
            If i < 5 And j > i + 1 And j < i + 5 Then
              Dim n As Integer = Val(Caption.Substring(i + 1))
              Select Case True
                Case n < 10
                  AlphanumericName = AlphanumericName.Substring(0, i + 1) & "000" & AlphanumericName.Substring(i + 1)
                  Caption = Caption.Substring(j + 1)
                Case n < 100
                  AlphanumericName = AlphanumericName.Substring(0, i + 1) & "00" & AlphanumericName.Substring(i + 1)
                  Caption = Caption.Substring(j + 1)
                Case n < 1000
                  AlphanumericName = AlphanumericName.Substring(0, i + 1) & "0" & AlphanumericName.Substring(i + 1)
                  Caption = Caption.Substring(j + 1)
              End Select
            End If
            Caption = Caption.TrimStart()
            Caption = Caption.TrimEnd()
            While Caption.IndexOf("&") > 0
              Caption = Caption.Substring(0, Caption.IndexOf("&")) & "and" & Caption.Substring(Caption.IndexOf("&") + 1)
            End While
        End Select
        ImageWidth = TemporaryImage.Width
        ImageHeight = TemporaryImage.Height
        SetDefaultFactors()
        CreateThumbnail(TemporaryImage)
        DrawSlide()
      End Using
    End Set
  End Property
  Public Function GetImage()
    If FName = "New" Then Return My.Resources.White_Slide
    Select Case Type
      Case "BLANK" : Return My.Resources.Blank_Slide
      Case "IMAGE"
        If File.Exists(FName) Then Return New Bitmap(FName)
      Case "MOVIE"
        If File.Exists(FName) Then Return My.Resources.Movie
    End Select
    Return My.Resources.Not_Found
  End Function
  Public Sub CreateCaptionSlide()
    Dim TemporaryImage As New Bitmap(200, 200)
    Dim g = Graphics.FromImage(TemporaryImage)
    g.Clear(Color.Black)
    Dim f As New Font(CaptionFont, PointSize \ 2, FontStyle.Italic)
    Dim textsize As SizeF = g.MeasureString(Caption, f)
    g.DrawString(Caption, f, Brushes.LightGray, 100 - textsize.Width \ 2, 100 - textsize.Height \ 2)
    CreateThumbnail(TemporaryImage)
  End Sub
  Private Sub CreateThumbnail(ByVal Image)
    Dim zfactor As Double = picSlide.Width / ImageWidth
    ' start by assuming that the photo fits horizontally
    ' check to see if height fits
    If ImageHeight * zfactor > picSlide.Height Then
      zfactor = picSlide.Height / ImageHeight
    End If
    picSlide.Image = Image.GetThumbnailImage(ImageWidth * zfactor, ImageHeight * zfactor, Nothing, IntPtr.Zero)
  End Sub
  Public Sub SetDefaultFactors()
    ' work out default zoom factors and position
    Interval1 = My.Settings.Interval1 * 1000
    Interval2 = My.Settings.Interval2 * 1000
    Interval3 = My.Settings.Interval3 * 1000
    Dim Pan As Integer = 0
    '0 = no pan needed; 1 = pan across; 2 = pan down; 3 zoom out ; 4 zoom in
    Select Case My.Settings.Aspect
      Case "Show all" : ZfactorStart = ZFactorMin(Pan)
      Case "Fill screen" : ZfactorStart = ZFactorMax(True, Pan)
    End Select
    ZfactorFinish = ZfactorStart
    Select Case Pan
      Case 0 ' no pan needed
        OriginStart = FindCentreStart()
        OriginFinish = FindCentreFinish()
      Case 1 'pan horizontally
        ZfactorStart = ZFactorMax(False)
        ZfactorFinish = ZfactorStart
        OriginStart = FindLeftStart()
        OriginFinish = FindRightFinish()
      Case 2 ' pan vertically
        ZfactorStart = ZFactorMax(False)
        ZfactorFinish = ZfactorStart
        OriginStart = FindTopStart()
        OriginFinish = FindBottomFinish()
      Case 3 ' zoom out
        ZfactorStart = ZFactorMax(False)
        ZfactorFinish = ZFactorMin()
        OriginStart = FindCentreStart()
        OriginFinish = FindCentreFinish()
      Case 4 ' zoom in
        ZfactorStart = ZFactorMin()
        ZfactorFinish = ZFactorMax(False)
        OriginStart = FindCentreStart()
        OriginFinish = FindCentreFinish()
    End Select
    SetPanFlag()
  End Sub
  Private Function FindLeftStart() As Point
    Return New Point(0, (ScreenSize.Height - ImageHeight * ZfactorStart) / 2)
  End Function
  Private Function FindRightFinish() As Point
    Dim x, y As Integer
    x = (ScreenSize.Width - ImageWidth / WsFactor * ZfactorFinish) * WsFactor
    y = (ScreenSize.Height - ImageHeight * ZfactorFinish) / 2
    Return New Point(x, y)
  End Function
  Private Function FindTopStart() As Point
    Return New Point((ScreenSize.Width - ImageWidth / WsFactor * ZfactorStart) * WsFactor / 2, 0)
  End Function
  Private Function FindBottomFinish() As Point
    Dim x, y As Integer
    x = (ScreenSize.Width - ImageWidth / WsFactor * ZfactorStart) * WsFactor / 2
    y = (ScreenSize.Height - ImageHeight * ZfactorFinish)
    Return New Point(x, y)
  End Function
  Public Function FindCentreStart() As Point
    Dim x, y As Integer
    x = (ScreenSize.Width - ImageWidth / WsFactor * ZfactorStart) * WsFactor / 2
    y = (ScreenSize.Height - ImageHeight * ZfactorStart) / 2
    Return New Point(x, y)
  End Function
  Public Function FindCentreFinish() As Point
    Dim x, y As Integer
    x = (ScreenSize.Width - ImageWidth / WsFactor * ZfactorFinish) * WsFactor / 2
    y = (ScreenSize.Height - ImageHeight * ZfactorFinish) / 2
    Return New Point(x, y)
  End Function
  Public Function ZFactorMin(Optional ByRef pan As Integer = 0) As Double
    'sets pan to recommended pan setting
    Dim Z As Double
    pan = 0
    ' start by assuming that the photo fits horizontally
    Z = ScreenSize.Width / (ImageWidth / WsFactor)
    ' check to see if height is too big
    If ImageHeight * Z > ScreenSize.Height Then
      Z = ScreenSize.Height / ImageHeight
    End If
    If ImageHeight * Z < ScreenSize.Height / 2 Then pan = 1 ' pan horizontally
    If ImageWidth * Z < ScreenSize.Width / 3 Then pan = 2 ' pan vertically
    Return Z
  End Function
  Public Function ZFactorMax(ByVal f As Boolean, Optional ByRef pan As Integer = 0) As Double
    ' if f = true then fit tall images to screen
    Dim Z As Double
    pan = 0
    ' start by assuming that the photo fits horizontally
    Z = ScreenSize.Width / (ImageWidth / WsFactor)
    ' check to see if height is too small
    If ImageHeight * Z < ScreenSize.Height Then
      Z = ScreenSize.Height / ImageHeight
    End If
    ' check to see if height is much too big
    If f And ImageHeight * Z > 2 * ScreenSize.Height Then
      Z = ScreenSize.Height / ImageHeight
    End If
    If ImageWidth * Z > ScreenSize.Width * 2 Then pan = 1 ' pan horizontally
    If ImageWidth * Z > ScreenSize.Width * 1.25 Then
      ' generate a random number 3 / 4
      pan = 3 + randomnumber.Next(2) ' zoom out/in
    End If
    Return Z
  End Function
  Public Sub SetPanFlag()
    If Type = ("BLANK") Then
      PanFlag = False
      Interval2 = 0
    Else
      SetInterval2()
    End If
  End Sub
  Public Sub SetInterval2()
    Dim z As Double = Math.Sqrt(ZfactorStart / ZfactorFinish)
    If z < 1 Then z = 1 / z
    Dim r As Integer = distancemoved()
    PanFlag = (r > 200 Or z > 1.1)
    Interval2 = My.Settings.Interval2 * 1000
    If PanFlag Then
      Interval2 += 4000 * z * 100 / My.Settings.ZoomSpeed
      Interval2 += 10 * r * 100 / My.Settings.PanSpeed
    End If
    Interval2 = 500 * (Interval2 \ 500)
  End Sub
  Private Function limit(ByVal a, ByVal b, ByVal c)
    If a < b Then Return b
    If a > c Then Return c
    Return a
  End Function
  Private Function distancemoved()
    Dim a, b, c, d As Double
    a = OriginStart.X + ImageWidth / 2 * ZfactorStart
    b = OriginFinish.X + ImageWidth / 2 * ZfactorFinish
    c = OriginStart.Y + ImageHeight / 2 * ZfactorStart
    d = OriginFinish.Y + ImageHeight / 2 * ZfactorFinish
    Return Math.Sqrt((b - a) ^ 2 + (d - c) ^ 2)
  End Function

  Public Sub New(ByVal Typ As String)
    MyBase.New()
    'This call is required by the Windows Form Designer.
    InitializeComponent()
    Type = Typ
    Filename = "New" 'Thi sets default values with white slide image
  End Sub

  Private Sub Control_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picSlide.MouseDown
    Dim eNew As System.Windows.Forms.MouseEventArgs
    ' Convert the sender to a Control so we can access its position
    Dim c As Control = DirectCast(sender, Control)
    'Create the new MouseEventArgs
    eNew = New System.Windows.Forms.MouseEventArgs(e.Button, e.Clicks, e.X + c.Top, e.Y + c.Left, e.Delta)
    RaiseEvent SlideMouseDown(Me, eNew)
  End Sub

  Public Sub SelectSlide(ByVal f)
    SelectFlag = f
    DrawSlide()
  End Sub

  Public Sub DrawSlide()
    lblFilename.Text = ShortFileName
    LblCaption.Text = Caption
    If EnableFlag Then
      Panel1.BackColor = Color.White
    Else
      Panel1.BackColor = Color.LightGray
    End If
    If SelectFlag Then
      Panel2.BackColor = Color.Red
    Else
      If EnableFlag Then
        Panel2.BackColor = Color.White
      Else
        Panel2.BackColor = Color.LightGray
      End If
    End If
  End Sub

End Class
