PageRenderTime 27ms CodeModel.GetById 16ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Source Code/PowerSong/Custom Controls/SongsListItem.vb

#
Visual Basic | 141 lines | 97 code | 39 blank | 5 comment | 0 complexity | 6a7a5725a4ddce658c76cf698d586b5a MD5 | raw file
  1Imports PowerSong.SongDatabase
  2Imports PowerSong.SongDatabase.Items
  3
  4Public Class SongsListItem
  5
  6    Friend Const ICON_HEIGHT As Integer = 40
  7
  8    Private Shared FIconCache As New ImageCache
  9
 10    Private FIcon As Image = Nothing
 11
 12    Public ReadOnly Property Icon() As Image
 13        Get
 14            If FIcon Is Nothing Then FIcon = CreateIcon()
 15            Return FIcon
 16        End Get
 17    End Property
 18
 19    Private FItem As BaseItem = Nothing
 20
 21    Public Property Item() As BaseItem
 22        Get
 23            Return FItem
 24        End Get
 25        Set(ByVal value As BaseItem)
 26            FItem = value
 27            RaiseEvent NeedsRepainting()
 28        End Set
 29    End Property
 30
 31    Private FStyle As Style = Nothing
 32
 33    Public Property Style() As Style
 34        Get
 35            Return FStyle
 36        End Get
 37        Set(ByVal value As Style)
 38            FStyle = value
 39            RaiseEvent NeedsRepainting()
 40        End Set
 41    End Property
 42
 43    Private FOverrideBackground As OverrideBackground = Nothing
 44
 45    Public Property OverrideBackground() As OverrideBackground
 46        Get
 47            Return FOverrideBackground
 48        End Get
 49        Set(ByVal value As OverrideBackground)
 50            FOverrideBackground = value
 51            FIcon = Nothing
 52            RaiseEvent NeedsRepainting()
 53        End Set
 54    End Property
 55
 56    Public Event NeedsRepainting()
 57
 58    Public Sub New(ByVal item As BaseItem, _
 59                   ByVal style As Style, _
 60                   Optional ByVal overrideBackground As OverrideBackground = Nothing)
 61
 62        FItem = item
 63        FStyle = style
 64        FOverrideBackground = overrideBackground
 65
 66    End Sub
 67
 68    Public Sub RefreshIcon()
 69        FIcon = Nothing
 70        RaiseEvent NeedsRepainting()
 71    End Sub
 72
 73    Private Function CreateIcon() As Image
 74
 75        Dim Icon As Image = Nothing
 76
 77        If FOverrideBackground IsNot Nothing Then
 78
 79            ' Override the background
 80            Select Case FOverrideBackground.Type
 81
 82                Case Style.EBackgroundType.SolidColour
 83                    Icon = New Bitmap(ICON_HEIGHT, ICON_HEIGHT)
 84                    Graphics.FromImage(Icon).Clear(FOverrideBackground.Value)
 85
 86                Case Style.EBackgroundType.Image
 87                    Icon = GetImageFromCache(FOverrideBackground.Value)
 88
 89            End Select
 90
 91        Else
 92
 93            ' Use the background set in the style
 94            Select Case FStyle.BackgroundType
 95
 96                Case Style.EBackgroundType.SolidColour
 97                    Icon = New Bitmap(ICON_HEIGHT, ICON_HEIGHT)
 98                    Graphics.FromImage(Icon).Clear(FStyle.DefaultBackgroundColour)
 99
100                Case Style.EBackgroundType.Image
101                    Icon = GetImageFromCache(FStyle.DefaultBackgroundImage)
102
103            End Select
104
105        End If
106
107        ' Return the icon
108        Return Icon
109
110    End Function
111
112    Private Function GetImageFromCache(ByVal fileName As String) As Image
113
114        ' Retrieve from the cache if possible otherwise load and resize the image
115        Dim Result As Image = Nothing
116        If FIconCache.ContainsImage(fileName) Then
117            Result = FIconCache.GetImage(fileName)
118        Else
119
120            Try
121                Result = GraphicRoutines.Resize(Image.FromFile(fileName), ICON_HEIGHT, ICON_HEIGHT)
122                FIconCache.AddImage(fileName, Result)
123            Catch
124
125                ' Create an error image if the picture no longer exists on disk
126                Result = New Bitmap(ICON_HEIGHT, ICON_HEIGHT, Imaging.PixelFormat.Format24bppRgb)
127                Dim G As Graphics = Graphics.FromImage(Result)
128                G.Clear(Color.Black)
129                G.DrawString("IMAGE", SystemFonts.SmallCaptionFont, Brushes.White, 0, 0)
130                G.DrawString("NOT", SystemFonts.SmallCaptionFont, Brushes.White, 0, 10)
131                G.DrawString("FOUND", SystemFonts.SmallCaptionFont, Brushes.White, 0, 20)
132
133            End Try
134            Return Result
135
136        End If
137        Return Result
138
139    End Function
140
141End Class