Skip to content

Commit a5d724f

Browse files
committed
Refactor ListView word wrap and summary row handling
- Standardize AddItem parameter naming (WrapValue) in ListViewExtensions and improve text wrapping logic for newlines and width. - Remove redundant InsertItem extension method. - Rewrite StringExtensions.WordWrap to support MaxWidth/TargetWidth and adaptive line reduction. - Fix FloppyDB.GetPublisherList to use vbNewLine as separator. - Add AutoResize property to SummaryRow; update SummaryPanel to use it for improved ListView wrapping and display.
1 parent 7e26780 commit a5d724f

File tree

4 files changed

+85
-109
lines changed

4 files changed

+85
-109
lines changed

DiskImageTool/Extensions/ListViewExtensions.vb

Lines changed: 17 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Module ListViewExtensions
2727
End Sub
2828

2929
<Extension()>
30-
Public Function AddItem(listViewControl As ListView, Text As String, Value As String, WrapText As Boolean) As ListViewItem
31-
Return AddItem(listViewControl, Nothing, "", Text, Value, Color.Empty, WrapText, Nothing)
30+
Public Function AddItem(listViewControl As ListView, Text As String, Value As String, WrapValue As Boolean) As ListViewItem
31+
Return AddItem(listViewControl, Nothing, "", Text, Value, Color.Empty, WrapValue, Nothing)
3232
End Function
3333

3434
<Extension()>
@@ -42,8 +42,8 @@ Module ListViewExtensions
4242
End Function
4343

4444
<Extension()>
45-
Public Function AddItem(listViewControl As ListView, Group As ListViewGroup, Text As String, Value As String, WrapText As Boolean) As ListViewItem
46-
Return AddItem(listViewControl, Group, "", Text, Value, Color.Empty, WrapText, Nothing)
45+
Public Function AddItem(listViewControl As ListView, Group As ListViewGroup, Text As String, Value As String, WrapValue As Boolean) As ListViewItem
46+
Return AddItem(listViewControl, Group, "", Text, Value, Color.Empty, WrapValue, Nothing)
4747
End Function
4848

4949
<Extension()>
@@ -52,31 +52,31 @@ Module ListViewExtensions
5252
End Function
5353

5454
<Extension()>
55-
Public Function AddItem(listViewControl As ListView, Group As ListViewGroup, Text As String, Value As String, ForeColor As Color, WrapText As Boolean) As ListViewItem
56-
Return AddItem(listViewControl, Group, "", Text, Value, ForeColor, WrapText, Nothing)
55+
Public Function AddItem(listViewControl As ListView, Group As ListViewGroup, Text As String, Value As String, ForeColor As Color, WrapValue As Boolean) As ListViewItem
56+
Return AddItem(listViewControl, Group, "", Text, Value, ForeColor, WrapValue, Nothing)
5757
End Function
5858

5959
<Extension()>
60-
Public Function AddItem(listViewControl As ListView, Group As ListViewGroup, Text As String, Value As String, ForeColor As Color, WrapText As Boolean, Width? As Integer) As ListViewItem
61-
Return AddItem(listViewControl, Group, "", Text, Value, ForeColor, WrapText, Width)
60+
Public Function AddItem(listViewControl As ListView, Group As ListViewGroup, Text As String, Value As String, ForeColor As Color, WrapValue As Boolean, Width? As Integer) As ListViewItem
61+
Return AddItem(listViewControl, Group, "", Text, Value, ForeColor, WrapValue, Width)
6262
End Function
6363

6464
<Extension()>
65-
Public Function AddItem(listViewControl As ListView, Group As ListViewGroup, Name As String, Text As String, Value As String, ForeColor As Color, WrapText As Boolean, Width? As Integer) As ListViewItem
65+
Public Function AddItem(listViewControl As ListView, Group As ListViewGroup, Name As String, Text As String, Value As String, ForeColor As Color, WrapValue As Boolean, Width? As Integer) As ListViewItem
6666
Dim Item As ListViewItem = Nothing
6767
Dim SubItem As ListViewItem.ListViewSubItem
6868
Dim StringList As List(Of String) = Nothing
6969
Dim AddTags As Boolean = False
7070

71-
If WrapText Then
72-
Dim CalcWidth As Integer
73-
If Width.HasValue Then
74-
CalcWidth = Width.Value
75-
Else
76-
CalcWidth = listViewControl.Columns.Item(1).Width - 6
71+
If WrapValue Then
72+
Dim HasNewLine = Value.Contains(vbCrLf) OrElse Value.Contains(vbCr) OrElse Value.Contains(vbLf)
73+
Dim TargetWidth As Integer = listViewControl.Columns.Item(1).Width - 6
74+
Dim MaxWidth As Integer = If(Width.HasValue, Width.Value, TargetWidth)
75+
If HasNewLine Then
76+
TargetWidth = MaxWidth
7777
End If
78-
If TextRenderer.MeasureText(Value, listViewControl.Font).Width > CalcWidth OrElse Value.Contains(vbNewLine) Then
79-
StringList = Value.WordWrap(CalcWidth, listViewControl.Font)
78+
If TextRenderer.MeasureText(Value, listViewControl.Font).Width > TargetWidth OrElse HasNewLine Then
79+
StringList = Value.WordWrap(MaxWidth, TargetWidth, listViewControl.Font)
8080
AddTags = True
8181
End If
8282
End If
@@ -118,55 +118,6 @@ Module ListViewExtensions
118118
Return Item
119119
End Function
120120

121-
<Extension()>
122-
Public Function InsertItem(listViewControl As ListView, Index As Integer, Group As ListViewGroup, Text As String, Value As String, ForeColor As Color, WrapText As Boolean) As ListViewItem
123-
Dim Item As ListViewItem = Nothing
124-
Dim SubItem As ListViewItem.ListViewSubItem
125-
Dim StringList As List(Of String) = Nothing
126-
Dim AddTags As Boolean = False
127-
128-
If WrapText Then
129-
Dim Width = listViewControl.Columns.Item(1).Width - 5
130-
If TextRenderer.MeasureText(Value, listViewControl.Font).Width > Width Then
131-
StringList = Value.WordWrap(Width, listViewControl.Font)
132-
AddTags = True
133-
End If
134-
End If
135-
136-
If StringList Is Nothing Then
137-
StringList = New List(Of String) From {
138-
Value
139-
}
140-
End If
141-
142-
For Counter = 0 To StringList.Count - 1
143-
Dim ItemText As String
144-
If Counter = 0 Then
145-
ItemText = Text
146-
Else
147-
ItemText = ""
148-
End If
149-
Dim NewItem As New ListViewItem(ItemText, Group) With {
150-
.UseItemStyleForSubItems = False
151-
}
152-
If AddTags Then
153-
NewItem.Tag = Text
154-
End If
155-
SubItem = NewItem.SubItems.Add(StringList(Counter))
156-
SubItem.ForeColor = ForeColor
157-
If AddTags Then
158-
SubItem.Tag = Value
159-
End If
160-
listViewControl.Items.Insert(Index, NewItem)
161-
162-
If Item Is Nothing Then
163-
Item = NewItem
164-
End If
165-
Next
166-
167-
Return Item
168-
End Function
169-
170121

171122
<Extension()>
172123
Public Sub AutoResizeColumnsContstrained(listViewControl As ListView, headerAutoResize As ColumnHeaderAutoResizeStyle)

DiskImageTool/Extensions/StringExtensions.vb

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,69 @@
22

33
Module StringExtensions
44
<Extension()>
5-
Public Function WordWrap(Text As String, Width As Integer, Font As Font) As List(Of String)
6-
Dim Result As New List(Of String)
7-
8-
If String.IsNullOrEmpty(Text) OrElse Width <= 0 Then
9-
Result.Add(Text)
10-
Return Result
5+
Public Function WordWrap(Text As String, MaxWidth As Integer, TargetWidth As Integer, Font As Font) As List(Of String)
6+
If String.IsNullOrEmpty(Text) OrElse MaxWidth <= 0 Then
7+
Return New List(Of String) From {Text}
118
End If
129

1310
' Normalize newlines and split into logical lines
1411
Dim Lines = Text.Replace(vbCrLf, vbLf).Replace(vbCr, vbLf).Split(vbLf)
1512

16-
For Each LogicalLine In Lines
17-
' Preserve blank lines
18-
If LogicalLine = "" Then
19-
Result.Add("")
20-
Continue For
21-
End If
22-
23-
Dim Words = LogicalLine.Split(" ")
24-
Dim Line As String = ""
13+
Dim DoReduceLines As Boolean = (Lines.Count = 1)
2514

26-
For Each Word In Words
27-
Dim NewLine = If(Line = "", Word, Line & " " & Word)
28-
Dim Size = TextRenderer.MeasureText(NewLine, Font)
15+
Dim Result As List(Of String)
16+
Dim Words() As String = {}
17+
Dim TryReduceLines As Boolean
2918

30-
If Size.Width <= Width Then
31-
Line = NewLine
32-
Else
33-
If Line <> "" Then
34-
Result.Add(Line)
35-
End If
36-
Line = Word
19+
Do
20+
Result = New List(Of String)
21+
For Each Line In Lines
22+
' Preserve blank lines
23+
If Line = "" Then
24+
Result.Add("")
25+
Continue For
3726
End If
27+
28+
Words = Line.Split(" ")
29+
30+
Result.AddRange(WrapLine(Words, TargetWidth, Font))
3831
Next
3932

40-
If Line <> "" Then
41-
Result.Add(Line)
33+
TryReduceLines = DoReduceLines AndAlso Result.Count > 2 AndAlso TargetWidth < MaxWidth AndAlso Words.Length > 0
34+
35+
If TryReduceLines Then
36+
TargetWidth += 4
37+
If TargetWidth > MaxWidth Then
38+
TargetWidth = MaxWidth
39+
End If
4240
End If
43-
Next
41+
Loop Until Not TryReduceLines
4442

4543
Return Result
4644
End Function
45+
46+
Private Function WrapLine(Words() As String, MaxWidth As Integer, Font As Font) As List(Of String)
47+
Dim Lines As New List(Of String)()
48+
Dim CurrentLine As String = ""
49+
50+
For Each Word In Words
51+
Dim NewLine = If(CurrentLine = "", Word, CurrentLine & " " & Word)
52+
Dim Size = TextRenderer.MeasureText(NewLine, Font)
53+
54+
If Size.Width <= MaxWidth Then
55+
CurrentLine = NewLine
56+
Else
57+
If CurrentLine <> "" Then
58+
Lines.Add(CurrentLine)
59+
End If
60+
CurrentLine = Word
61+
End If
62+
Next
63+
64+
If CurrentLine <> "" Then
65+
Lines.Add(CurrentLine)
66+
End If
67+
68+
Return Lines
69+
End Function
4770
End Module

DiskImageTool/Modules/FloppyDB.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ Public Class FloppyDB
812812
End Function
813813

814814
Public Function GetPublisherList() As String
815-
Return JoinDistinctStrings(Function(d) d.GetPublisher())
815+
Return JoinDistinctStrings(Function(d) d.GetPublisher(), vbNewLine)
816816
End Function
817817

818818
Public Function GetRegionList() As String

DiskImageTool/Modules/SummaryPanel.vb

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,18 @@ Public Class SummaryPanel
273273

274274
TitleRows.Clear()
275275

276-
TitleRows.Add("Name", New SummaryRow(ListViewSummary.Font, My.Resources.SummaryPanel_Title, True))
277-
TitleRows.Add("Variation", New SummaryRow(ListViewSummary.Font, My.Resources.SummaryPanel_Variant, False))
278-
TitleRows.Add("Compilation", New SummaryRow(ListViewSummary.Font, My.Resources.SummaryPanel_Compilation, True))
279-
TitleRows.Add("Publisher", New SummaryRow(ListViewSummary.Font, My.Resources.SummaryPanel_Publisher, True))
280-
TitleRows.Add("Year", New SummaryRow(ListViewSummary.Font, My.Resources.SummaryPanel_Year, False))
281-
TitleRows.Add("OperatingSystem", New SummaryRow(ListViewSummary.Font, My.Resources.SummaryPanel_OperatingSystem, False))
282-
TitleRows.Add("Region", New SummaryRow(ListViewSummary.Font, My.Resources.Label_Region, False))
283-
TitleRows.Add("Language", New SummaryRow(ListViewSummary.Font, My.Resources.Label_Language, True))
284-
TitleRows.Add("Version", New SummaryRow(ListViewSummary.Font, My.Resources.Label_Version, True))
285-
TitleRows.Add("Disk", New SummaryRow(ListViewSummary.Font, My.Resources.Label_Disk, False))
286-
TitleRows.Add("CopyProtection", New SummaryRow(ListViewSummary.Font, My.Resources.SummaryPanel_CopyProtection, True))
287-
TitleRows.Add("TDC", New SummaryRow(ListViewSummary.Font, My.Resources.Label_TDC, True))
276+
TitleRows.Add("Name", New SummaryRow(ListViewSummary.Font, My.Resources.SummaryPanel_Title, True, True))
277+
TitleRows.Add("Variation", New SummaryRow(ListViewSummary.Font, My.Resources.SummaryPanel_Variant, False, False))
278+
TitleRows.Add("Compilation", New SummaryRow(ListViewSummary.Font, My.Resources.SummaryPanel_Compilation, True, False))
279+
TitleRows.Add("Publisher", New SummaryRow(ListViewSummary.Font, My.Resources.SummaryPanel_Publisher, True, False))
280+
TitleRows.Add("Year", New SummaryRow(ListViewSummary.Font, My.Resources.SummaryPanel_Year, False, False))
281+
TitleRows.Add("OperatingSystem", New SummaryRow(ListViewSummary.Font, My.Resources.SummaryPanel_OperatingSystem, False, False))
282+
TitleRows.Add("Region", New SummaryRow(ListViewSummary.Font, My.Resources.Label_Region, False, False))
283+
TitleRows.Add("Language", New SummaryRow(ListViewSummary.Font, My.Resources.Label_Language, True, False))
284+
TitleRows.Add("Version", New SummaryRow(ListViewSummary.Font, My.Resources.Label_Version, True, False))
285+
TitleRows.Add("Disk", New SummaryRow(ListViewSummary.Font, My.Resources.Label_Disk, True, False))
286+
TitleRows.Add("CopyProtection", New SummaryRow(ListViewSummary.Font, My.Resources.SummaryPanel_CopyProtection, True, True))
287+
TitleRows.Add("TDC", New SummaryRow(ListViewSummary.Font, My.Resources.Label_TDC, True, False))
288288
End Sub
289289

290290
Private Sub PopulateError(InvalidImage As Boolean)
@@ -323,7 +323,7 @@ Public Class SummaryPanel
323323

324324
For Each SummaryRow As SummaryRow In Rows.Values
325325
If SummaryRow.Value <> "" Then
326-
If SummaryRow.WrapText Then
326+
If SummaryRow.AutoResize Then
327327
.AddItem(Group, SummaryRow.Text, SummaryRow.Value, SummaryRow.ForeColor, SummaryRow.WrapText, MaxWidth)
328328
Else
329329
.AddItem(Group, SummaryRow.Text, SummaryRow.Value, SummaryRow.ForeColor, SummaryRow.WrapText)
@@ -892,12 +892,14 @@ Public Class SummaryPanel
892892
End Structure
893893

894894
Private Class SummaryRow
895-
Public Sub New(Font As Font, Text As String, WrapText As Boolean)
895+
Public Sub New(Font As Font, Text As String, WrapText As Boolean, AutoResize As Boolean)
896896
_Text = Text
897897
_TextWidth = TextRenderer.MeasureText(Text, Font).Width
898898
_WrapText = WrapText
899+
_AutoResize = AutoResize
899900
End Sub
900901

902+
Public Property AutoResize As Boolean
901903
Public Property ForeColor As Color = SystemColors.WindowText
902904
Public Property Text As String
903905
Public Property TextWidth As Integer

0 commit comments

Comments
 (0)