/Source Code/PowerSong/Custom Controls/SongsListItem.vb
Visual Basic | 141 lines | 97 code | 39 blank | 5 comment | 0 complexity | 6a7a5725a4ddce658c76cf698d586b5a MD5 | raw file
- Imports PowerSong.SongDatabase
- Imports PowerSong.SongDatabase.Items
-
- Public Class SongsListItem
-
- Friend Const ICON_HEIGHT As Integer = 40
-
- Private Shared FIconCache As New ImageCache
-
- Private FIcon As Image = Nothing
-
- Public ReadOnly Property Icon() As Image
- Get
- If FIcon Is Nothing Then FIcon = CreateIcon()
- Return FIcon
- End Get
- End Property
-
- Private FItem As BaseItem = Nothing
-
- Public Property Item() As BaseItem
- Get
- Return FItem
- End Get
- Set(ByVal value As BaseItem)
- FItem = value
- RaiseEvent NeedsRepainting()
- End Set
- End Property
-
- Private FStyle As Style = Nothing
-
- Public Property Style() As Style
- Get
- Return FStyle
- End Get
- Set(ByVal value As Style)
- FStyle = value
- RaiseEvent NeedsRepainting()
- End Set
- End Property
-
- Private FOverrideBackground As OverrideBackground = Nothing
-
- Public Property OverrideBackground() As OverrideBackground
- Get
- Return FOverrideBackground
- End Get
- Set(ByVal value As OverrideBackground)
- FOverrideBackground = value
- FIcon = Nothing
- RaiseEvent NeedsRepainting()
- End Set
- End Property
-
- Public Event NeedsRepainting()
-
- Public Sub New(ByVal item As BaseItem, _
- ByVal style As Style, _
- Optional ByVal overrideBackground As OverrideBackground = Nothing)
-
- FItem = item
- FStyle = style
- FOverrideBackground = overrideBackground
-
- End Sub
-
- Public Sub RefreshIcon()
- FIcon = Nothing
- RaiseEvent NeedsRepainting()
- End Sub
-
- Private Function CreateIcon() As Image
-
- Dim Icon As Image = Nothing
-
- If FOverrideBackground IsNot Nothing Then
-
- ' Override the background
- Select Case FOverrideBackground.Type
-
- Case Style.EBackgroundType.SolidColour
- Icon = New Bitmap(ICON_HEIGHT, ICON_HEIGHT)
- Graphics.FromImage(Icon).Clear(FOverrideBackground.Value)
-
- Case Style.EBackgroundType.Image
- Icon = GetImageFromCache(FOverrideBackground.Value)
-
- End Select
-
- Else
-
- ' Use the background set in the style
- Select Case FStyle.BackgroundType
-
- Case Style.EBackgroundType.SolidColour
- Icon = New Bitmap(ICON_HEIGHT, ICON_HEIGHT)
- Graphics.FromImage(Icon).Clear(FStyle.DefaultBackgroundColour)
-
- Case Style.EBackgroundType.Image
- Icon = GetImageFromCache(FStyle.DefaultBackgroundImage)
-
- End Select
-
- End If
-
- ' Return the icon
- Return Icon
-
- End Function
-
- Private Function GetImageFromCache(ByVal fileName As String) As Image
-
- ' Retrieve from the cache if possible otherwise load and resize the image
- Dim Result As Image = Nothing
- If FIconCache.ContainsImage(fileName) Then
- Result = FIconCache.GetImage(fileName)
- Else
-
- Try
- Result = GraphicRoutines.Resize(Image.FromFile(fileName), ICON_HEIGHT, ICON_HEIGHT)
- FIconCache.AddImage(fileName, Result)
- Catch
-
- ' Create an error image if the picture no longer exists on disk
- Result = New Bitmap(ICON_HEIGHT, ICON_HEIGHT, Imaging.PixelFormat.Format24bppRgb)
- Dim G As Graphics = Graphics.FromImage(Result)
- G.Clear(Color.Black)
- G.DrawString("IMAGE", SystemFonts.SmallCaptionFont, Brushes.White, 0, 0)
- G.DrawString("NOT", SystemFonts.SmallCaptionFont, Brushes.White, 0, 10)
- G.DrawString("FOUND", SystemFonts.SmallCaptionFont, Brushes.White, 0, 20)
-
- End Try
- Return Result
-
- End If
- Return Result
-
- End Function
-
- End Class