PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms 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
  1. Imports PowerSong.SongDatabase
  2. Imports PowerSong.SongDatabase.Items
  3. Public Class SongsListItem
  4. Friend Const ICON_HEIGHT As Integer = 40
  5. Private Shared FIconCache As New ImageCache
  6. Private FIcon As Image = Nothing
  7. Public ReadOnly Property Icon() As Image
  8. Get
  9. If FIcon Is Nothing Then FIcon = CreateIcon()
  10. Return FIcon
  11. End Get
  12. End Property
  13. Private FItem As BaseItem = Nothing
  14. Public Property Item() As BaseItem
  15. Get
  16. Return FItem
  17. End Get
  18. Set(ByVal value As BaseItem)
  19. FItem = value
  20. RaiseEvent NeedsRepainting()
  21. End Set
  22. End Property
  23. Private FStyle As Style = Nothing
  24. Public Property Style() As Style
  25. Get
  26. Return FStyle
  27. End Get
  28. Set(ByVal value As Style)
  29. FStyle = value
  30. RaiseEvent NeedsRepainting()
  31. End Set
  32. End Property
  33. Private FOverrideBackground As OverrideBackground = Nothing
  34. Public Property OverrideBackground() As OverrideBackground
  35. Get
  36. Return FOverrideBackground
  37. End Get
  38. Set(ByVal value As OverrideBackground)
  39. FOverrideBackground = value
  40. FIcon = Nothing
  41. RaiseEvent NeedsRepainting()
  42. End Set
  43. End Property
  44. Public Event NeedsRepainting()
  45. Public Sub New(ByVal item As BaseItem, _
  46. ByVal style As Style, _
  47. Optional ByVal overrideBackground As OverrideBackground = Nothing)
  48. FItem = item
  49. FStyle = style
  50. FOverrideBackground = overrideBackground
  51. End Sub
  52. Public Sub RefreshIcon()
  53. FIcon = Nothing
  54. RaiseEvent NeedsRepainting()
  55. End Sub
  56. Private Function CreateIcon() As Image
  57. Dim Icon As Image = Nothing
  58. If FOverrideBackground IsNot Nothing Then
  59. ' Override the background
  60. Select Case FOverrideBackground.Type
  61. Case Style.EBackgroundType.SolidColour
  62. Icon = New Bitmap(ICON_HEIGHT, ICON_HEIGHT)
  63. Graphics.FromImage(Icon).Clear(FOverrideBackground.Value)
  64. Case Style.EBackgroundType.Image
  65. Icon = GetImageFromCache(FOverrideBackground.Value)
  66. End Select
  67. Else
  68. ' Use the background set in the style
  69. Select Case FStyle.BackgroundType
  70. Case Style.EBackgroundType.SolidColour
  71. Icon = New Bitmap(ICON_HEIGHT, ICON_HEIGHT)
  72. Graphics.FromImage(Icon).Clear(FStyle.DefaultBackgroundColour)
  73. Case Style.EBackgroundType.Image
  74. Icon = GetImageFromCache(FStyle.DefaultBackgroundImage)
  75. End Select
  76. End If
  77. ' Return the icon
  78. Return Icon
  79. End Function
  80. Private Function GetImageFromCache(ByVal fileName As String) As Image
  81. ' Retrieve from the cache if possible otherwise load and resize the image
  82. Dim Result As Image = Nothing
  83. If FIconCache.ContainsImage(fileName) Then
  84. Result = FIconCache.GetImage(fileName)
  85. Else
  86. Try
  87. Result = GraphicRoutines.Resize(Image.FromFile(fileName), ICON_HEIGHT, ICON_HEIGHT)
  88. FIconCache.AddImage(fileName, Result)
  89. Catch
  90. ' Create an error image if the picture no longer exists on disk
  91. Result = New Bitmap(ICON_HEIGHT, ICON_HEIGHT, Imaging.PixelFormat.Format24bppRgb)
  92. Dim G As Graphics = Graphics.FromImage(Result)
  93. G.Clear(Color.Black)
  94. G.DrawString("IMAGE", SystemFonts.SmallCaptionFont, Brushes.White, 0, 0)
  95. G.DrawString("NOT", SystemFonts.SmallCaptionFont, Brushes.White, 0, 10)
  96. G.DrawString("FOUND", SystemFonts.SmallCaptionFont, Brushes.White, 0, 20)
  97. End Try
  98. Return Result
  99. End If
  100. Return Result
  101. End Function
  102. End Class