This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockSelector.vb
165 lines (144 loc) · 4.98 KB
/
BlockSelector.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports DevExpress.XtraTreeList
Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.XtraTreeList.Nodes.Operations
Imports DevExpress.XtraTreeList.Columns
Imports DevExpress.XtraTreeList.Nodes
Imports DevExpress.XtraEditors
Namespace WindowsApplication1
Friend Class BlockSelectionHelper
Private fList As TreeList
Private fBlock As Block
Public Sub New(ByVal list As TreeList)
If list Is Nothing Then
Throw New Exception("A valid TreeList instance must be passed to the constructor")
End If
fList = list
fBlock = New Block()
SubscribeEvents()
End Sub
Private Sub SubscribeEvents()
AddHandler fList.FocusedColumnChanged, AddressOf fList_FocusedColumnChanged
AddHandler fList.FocusedNodeChanged, AddressOf fList_FocusedNodeChanged
AddHandler fList.MouseMove, AddressOf fList_MouseMove
AddHandler fList.NodeCellStyle, AddressOf fList_NodeCellStyle
AddHandler fList.BeforeExpand, AddressOf fList_BeforeExpand
AddHandler fList.BeforeCollapse, AddressOf fList_BeforeCollapse
AddHandler fList.KeyDown, AddressOf fList_KeyDown
AddHandler fList.PopupMenuShowing, AddressOf fList_PopupMenuShowing
AddHandler fList.EditorKeyDown, AddressOf fList_EditorKeyDown
End Sub
Private Sub fList_EditorKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
If e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.C Then
Dim editor As TextEdit = TryCast(fList.ActiveEditor, TextEdit)
If editor Is Nothing Then
Return
End If
If editor.SelectionLength > 0 Then
Return
End If
CopyValuesToClipboard()
End If
End Sub
Private Sub fList_PopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs)
Dim hi As TreeListHitInfo = fList.CalcHitInfo(e.Point)
If hi.HitInfoType = HitInfoType.Cell Then
e.Menu.Items.Add(GetCopyMenuItem())
End If
End Sub
Private Function GetCopyMenuItem() As DevExpress.Utils.Menu.DXMenuItem
Dim item As New DevExpress.Utils.Menu.DXMenuItem()
item.Caption = "Copy selected values"
AddHandler item.Click, AddressOf item_Click
Return item
End Function
Private Sub item_Click(ByVal sender As Object, ByVal e As EventArgs)
CopyValuesToClipboard()
End Sub
Private Sub fList_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
If e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.C Then
CopyValuesToClipboard()
End If
End Sub
Private Sub fList_BeforeCollapse(ByVal sender As Object, ByVal e As BeforeCollapseEventArgs)
fBlock.Clear()
End Sub
Private Sub fList_BeforeExpand(ByVal sender As Object, ByVal e As BeforeExpandEventArgs)
fBlock.Clear()
End Sub
Public ReadOnly Property Block() As Block
Get
Return fBlock
End Get
End Property
Public ReadOnly Property List() As TreeList
Get
Return fList
End Get
End Property
Public Shared Function GetNodeIndex(ByVal node As TreeListNode) As Integer
Return node.TreeList.GetVisibleIndexByNode(node)
End Function
Private Sub fList_NodeCellStyle(ByVal sender As Object, ByVal e As GetCustomNodeCellStyleEventArgs)
If fBlock.Contains(e.Column.VisibleIndex, GetNodeIndex(e.Node)) Then
e.Appearance.BackColor = SystemColors.Highlight
End If
End Sub
Private Sub fList_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
If e.Button = MouseButtons.Left Then
Dim hInfo As TreeListHitInfo = fList.CalcHitInfo(e.Location)
If hInfo.HitInfoType = HitInfoType.Cell Then
fBlock.X2 = hInfo.Column.VisibleIndex
fBlock.Y2 = GetNodeIndex(hInfo.Node)
If fBlock.Modified AndAlso (Not fBlock.IsEmpty()) Then
InvalidateBlock()
End If
End If
End If
End Sub
Public Sub InvalidateBlock()
fList.LayoutChanged()
End Sub
Private Sub fList_FocusedNodeChanged(ByVal sender As Object, ByVal e As FocusedNodeChangedEventArgs)
If fList.FocusedColumn IsNot Nothing Then
UpdateBlock()
End If
End Sub
Private Sub UpdateBlock()
If Control.ModifierKeys = Keys.Shift Then
fBlock.X2 = fList.FocusedColumn.VisibleIndex
fBlock.Y2 = GetNodeIndex(fList.FocusedNode)
Else
fBlock.X1 = fList.FocusedColumn.VisibleIndex
fBlock.Y1 = GetNodeIndex(fList.FocusedNode)
fBlock.X2 = fBlock.X1
fBlock.Y2 = fBlock.Y1
End If
If fBlock.Modified Then
InvalidateBlock()
End If
End Sub
Private Sub fList_FocusedColumnChanged(ByVal sender As Object, ByVal e As FocusedColumnChangedEventArgs)
UpdateBlock()
End Sub
Private Sub CopyValuesToClipboard()
Dim value As String = GetSelectedValues()
If (Not String.IsNullOrEmpty(value)) Then
Clipboard.SetText(value)
End If
End Sub
Public Function GetSelectedValues() As String
Dim result As String = String.Empty
If (Not fBlock.IsEmpty()) Then
Dim operation As New SelectionOperation(fBlock)
fList.NodesIterator.DoOperation(operation)
result = operation.Result
End If
Return result
End Function
End Class
End Namespace