/
CheckMarkSelection.vb
296 lines (253 loc) · 11.3 KB
/
CheckMarkSelection.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
' Multiple row selection using custom checkbox column (web style)
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors.Controls
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
Imports DevExpress.Utils.Drawing
Imports DevExpress.XtraEditors.ViewInfo
Imports DevExpress.XtraEditors.Drawing
Namespace DevExpress.XtraGrid.Selection
Public Class GridCheckMarksSelection
Protected _view As GridView
Protected selection As ArrayList
Private column As GridColumn
Private edit As RepositoryItemCheckEdit
Const CheckboxIndent As Integer = 4
Public Sub New()
selection = New ArrayList()
End Sub
Public Sub New(ByVal view As GridView)
Me.New()
Me.View = view
End Sub
Public Property View As GridView
Get
Return _view
End Get
Set(ByVal value As GridView)
If _view IsNot value Then
Detach()
Attach(value)
End If
End Set
End Property
Public ReadOnly Property CheckMarkColumn As GridColumn
Get
Return column
End Get
End Property
Public ReadOnly Property SelectedCount As Integer
Get
Return selection.Count
End Get
End Property
Public Function GetSelectedRow(ByVal index As Integer) As Object
Return selection(index)
End Function
Public Function GetSelectedIndex(ByVal row As Object) As Integer
Return selection.IndexOf(row)
End Function
Public Sub ClearSelection()
selection.Clear()
Invalidate()
End Sub
Public Sub SelectAll()
selection.Clear()
' fast (won't work if the grid is filtered)
'if(_view.DataSource is ICollection)
' selection.AddRange(((ICollection)_view.DataSource));
'else
' slow:
For i As Integer = 0 To _view.DataRowCount - 1
selection.Add(_view.GetRow(i))
Next
Invalidate()
End Sub
Public Sub SelectGroup(ByVal rowHandle As Integer, ByVal [select] As Boolean)
If IsGroupRowSelected(rowHandle) AndAlso [select] Then Return
For i As Integer = 0 To _view.GetChildRowCount(rowHandle) - 1
Dim childRowHandle As Integer = _view.GetChildRowHandle(rowHandle, i)
If _view.IsGroupRow(childRowHandle) Then
SelectGroup(childRowHandle, [select])
Else
SelectRow(childRowHandle, [select], False)
End If
Next
Invalidate()
End Sub
Public Sub SelectRow(ByVal rowHandle As Integer, ByVal [select] As Boolean)
SelectRow(rowHandle, [select], True)
End Sub
Public Sub InvertRowSelection(ByVal rowHandle As Integer)
If View.IsDataRow(rowHandle) Then
SelectRow(rowHandle, Not IsRowSelected(rowHandle))
End If
If View.IsGroupRow(rowHandle) Then
SelectGroup(rowHandle, Not IsGroupRowSelected(rowHandle))
End If
End Sub
Public Function IsGroupRowSelected(ByVal rowHandle As Integer) As Boolean
For i As Integer = 0 To _view.GetChildRowCount(rowHandle) - 1
Dim row As Integer = _view.GetChildRowHandle(rowHandle, i)
If _view.IsGroupRow(row) Then
If Not IsGroupRowSelected(row) Then Return False
ElseIf Not IsRowSelected(row) Then
Return False
End If
Next
Return True
End Function
Public Function IsRowSelected(ByVal rowHandle As Integer) As Boolean
If _view.IsGroupRow(rowHandle) Then Return IsGroupRowSelected(rowHandle)
Dim row As Object = _view.GetRow(rowHandle)
Return GetSelectedIndex(row) <> -1
End Function
Protected Overridable Sub Attach(ByVal view As GridView)
If view Is Nothing Then Return
selection.Clear()
_view = view
view.BeginUpdate()
Try
edit = TryCast(view.GridControl.RepositoryItems.Add("CheckEdit"), RepositoryItemCheckEdit)
column = view.Columns.Add()
column.OptionsColumn.AllowSort = Utils.DefaultBoolean.False
column.Visible = True
column.VisibleIndex = 0
column.FieldName = "CheckMarkSelection"
column.Caption = "Mark"
column.OptionsColumn.ShowCaption = False
column.OptionsColumn.AllowEdit = False
column.OptionsColumn.AllowSize = False
column.UnboundType = Data.UnboundColumnType.Boolean
column.Width = GetCheckBoxWidth()
column.ColumnEdit = edit
AddHandler view.Click, New EventHandler(AddressOf View_Click)
AddHandler view.CustomDrawColumnHeader, New ColumnHeaderCustomDrawEventHandler(AddressOf View_CustomDrawColumnHeader)
AddHandler view.CustomDrawGroupRow, New RowObjectCustomDrawEventHandler(AddressOf View_CustomDrawGroupRow)
AddHandler view.CustomUnboundColumnData, New CustomColumnDataEventHandler(AddressOf view_CustomUnboundColumnData)
AddHandler view.KeyDown, New KeyEventHandler(AddressOf view_KeyDown)
AddHandler view.RowStyle, New RowStyleEventHandler(AddressOf view_RowStyle)
Finally
view.EndUpdate()
End Try
End Sub
Protected Overridable Sub Detach()
If _view Is Nothing Then Return
If column IsNot Nothing Then column.Dispose()
If edit IsNot Nothing Then
_view.GridControl.RepositoryItems.Remove(edit)
edit.Dispose()
End If
RemoveHandler _view.Click, New EventHandler(AddressOf View_Click)
RemoveHandler _view.CustomDrawColumnHeader, New ColumnHeaderCustomDrawEventHandler(AddressOf View_CustomDrawColumnHeader)
RemoveHandler _view.CustomDrawGroupRow, New RowObjectCustomDrawEventHandler(AddressOf View_CustomDrawGroupRow)
RemoveHandler _view.CustomUnboundColumnData, New CustomColumnDataEventHandler(AddressOf view_CustomUnboundColumnData)
RemoveHandler _view.KeyDown, New KeyEventHandler(AddressOf view_KeyDown)
RemoveHandler _view.RowStyle, New RowStyleEventHandler(AddressOf view_RowStyle)
_view = Nothing
End Sub
Protected Function GetCheckBoxWidth() As Integer
Dim info As CheckEditViewInfo = TryCast(edit.CreateViewInfo(), CheckEditViewInfo)
Dim width As Integer = 0
Call GraphicsInfo.Default.AddGraphics(Nothing)
Try
width = info.CalcBestFit(GraphicsInfo.Default.Graphics).Width
Finally
Call GraphicsInfo.Default.ReleaseGraphics()
End Try
Return width + CheckboxIndent * 2
End Function
Protected Sub DrawCheckBox(ByVal cache As GraphicsCache, ByVal r As Rectangle, ByVal Checked As Boolean)
Dim info As CheckEditViewInfo
Dim painter As CheckEditPainter
Dim args As ControlGraphicsInfoArgs
info = TryCast(edit.CreateViewInfo(), CheckEditViewInfo)
painter = TryCast(edit.CreatePainter(), CheckEditPainter)
info.EditValue = Checked
info.Bounds = r
info.CalcViewInfo(Nothing)
args = New ControlGraphicsInfoArgs(info, cache, r)
painter.Draw(args)
End Sub
Private Sub Invalidate()
_view.CloseEditor()
_view.BeginUpdate()
_view.EndUpdate()
End Sub
Private Sub SelectRow(ByVal rowHandle As Integer, ByVal [select] As Boolean, ByVal invalidate As Boolean)
If IsRowSelected(rowHandle) = [select] Then Return
Dim row As Object = _view.GetRow(rowHandle)
If [select] Then
selection.Add(row)
Else
selection.Remove(row)
End If
If invalidate Then
Me.Invalidate()
End If
End Sub
Private Sub view_CustomUnboundColumnData(ByVal sender As Object, ByVal e As CustomColumnDataEventArgs)
If e.Column Is CheckMarkColumn Then
If e.IsGetData Then
e.Value = IsRowSelected(View.GetRowHandle(e.ListSourceRowIndex))
Else
SelectRow(View.GetRowHandle(e.ListSourceRowIndex), CBool(e.Value))
End If
End If
End Sub
Private Sub view_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
If View.FocusedColumn IsNot column OrElse e.KeyCode <> Keys.Space Then Return
InvertRowSelection(View.FocusedRowHandle)
End Sub
Private Sub View_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim info As GridHitInfo
Dim pt As Point = _view.GridControl.PointToClient(Control.MousePosition)
info = _view.CalcHitInfo(pt)
If info.Column Is column Then
If info.InColumn Then
If SelectedCount = _view.DataRowCount Then
ClearSelection()
Else
SelectAll()
End If
End If
If info.InRowCell Then
InvertRowSelection(info.RowHandle)
End If
End If
If info.InRow AndAlso _view.IsGroupRow(info.RowHandle) AndAlso info.HitTest <> GridHitTest.RowGroupButton Then
InvertRowSelection(info.RowHandle)
End If
End Sub
Private Sub View_CustomDrawColumnHeader(ByVal sender As Object, ByVal e As ColumnHeaderCustomDrawEventArgs)
If e.Column Is column Then
e.Info.InnerElements.Clear()
e.Painter.DrawObject(e.Info)
DrawCheckBox(e.Cache, e.Bounds, SelectedCount = _view.DataRowCount)
e.Handled = True
End If
End Sub
Private Sub View_CustomDrawGroupRow(ByVal sender As Object, ByVal e As RowObjectCustomDrawEventArgs)
Dim info As GridGroupRowInfo = TryCast(e.Info, GridGroupRowInfo)
info.GroupText = " " & info.GroupText.TrimStart()
e.Appearance.FillRectangle(e.Cache, e.Bounds)
e.Painter.DrawObject(e.Info)
Dim r As Rectangle = info.ButtonBounds
r.Offset(r.Width + CheckboxIndent * 2 - 1, 0)
DrawCheckBox(e.Cache, r, IsGroupRowSelected(e.RowHandle))
e.Handled = True
End Sub
Private Sub view_RowStyle(ByVal sender As Object, ByVal e As RowStyleEventArgs)
If IsRowSelected(e.RowHandle) Then
e.Appearance.BackColor = SystemColors.Highlight
e.Appearance.ForeColor = SystemColors.HighlightText
End If
End Sub
End Class
End Namespace