-
Notifications
You must be signed in to change notification settings - Fork 836
Expand file tree
/
Copy pathdlistview.lua
More file actions
595 lines (380 loc) · 12 KB
/
dlistview.lua
File metadata and controls
595 lines (380 loc) · 12 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
local PANEL = {}
AccessorFunc( PANEL, "m_bDirty", "Dirty", FORCE_BOOL )
AccessorFunc( PANEL, "m_bSortable", "Sortable", FORCE_BOOL )
AccessorFunc( PANEL, "m_iHeaderHeight", "HeaderHeight" )
AccessorFunc( PANEL, "m_iDataHeight", "DataHeight" )
AccessorFunc( PANEL, "m_bMultiSelect", "MultiSelect" )
AccessorFunc( PANEL, "m_bHideHeaders", "HideHeaders" )
Derma_Hook( PANEL, "Paint", "Paint", "ListView" )
function PANEL:Init()
self:SetSortable( true )
self:SetMouseInputEnabled( true )
self:SetMultiSelect( true )
self:SetHideHeaders( false )
self:SetPaintBackground( true )
self:SetHeaderHeight( 16 )
self:SetDataHeight( 17 )
self.Columns = {}
self.Lines = {}
self.Sorted = {}
self:SetDirty( true )
self.pnlCanvas = vgui.Create( "Panel", self )
self.VBar = vgui.Create( "DVScrollBar", self )
self.VBar:SetZPos( 20 )
end
function PANEL:DisableScrollbar()
if ( IsValid( self.VBar ) ) then
self.VBar:Remove()
end
self.VBar = nil
end
function PANEL:GetLines()
return self.Lines
end
function PANEL:GetInnerTall()
return self:GetCanvas():GetTall()
end
function PANEL:GetCanvas()
return self.pnlCanvas
end
function PANEL:AddColumn( strName, iPosition )
if ( iPosition ) then
if ( iPosition <= 0 ) then
ErrorNoHaltWithStack( "Attempted to insert column at invalid position ", iPosition )
return
end
if ( IsValid( self.Columns[ iPosition ] ) ) then
ErrorNoHaltWithStack( "Attempted to insert duplicate column." )
return
end
end
local pColumn = nil
if ( self.m_bSortable ) then
pColumn = vgui.Create( "DListView_Column", self )
else
pColumn = vgui.Create( "DListView_ColumnPlain", self )
end
pColumn:SetName( strName )
pColumn:SetZPos( 10 )
if ( iPosition ) then
table.insert( self.Columns, iPosition, pColumn )
local i = 1
for id, pnl in pairs( self.Columns ) do
pnl:SetColumnID( i )
i = i + 1
end
else
local ID = table.insert( self.Columns, pColumn )
pColumn:SetColumnID( ID )
end
self:InvalidateLayout()
return pColumn
end
function PANEL:RemoveLine( LineID )
local Line = self:GetLine( LineID )
local SelectedID = self:GetSortedID( LineID )
self.Lines[ LineID ] = nil
table.remove( self.Sorted, SelectedID )
self:SetDirty( true )
self:InvalidateLayout()
Line:Remove()
end
function PANEL:ColumnWidth( i )
local ctrl = self.Columns[ i ]
if ( !ctrl ) then return 0 end
return ctrl:GetWide()
end
function PANEL:FixColumnsLayout()
local NumColumns = table.Count( self.Columns )
if ( NumColumns == 0 ) then return end
local AllWidth = 0
for k, Column in pairs( self.Columns ) do
AllWidth = AllWidth + math.ceil( Column:GetWide() )
end
local ChangeRequired = self.pnlCanvas:GetWide() - AllWidth
local ChangePerColumn = math.floor( ChangeRequired / NumColumns )
local Remainder = ChangeRequired - ( ChangePerColumn * NumColumns )
for k, Column in pairs( self.Columns ) do
local TargetWidth = math.ceil( Column:GetWide() ) + ChangePerColumn
Remainder = Remainder + ( TargetWidth - Column:SetWidth( TargetWidth ) )
end
local TotalMaxWidth = 0
-- If there's a remainder, try to palm it off on the other panels, equally
while ( Remainder != 0 ) do
local PerPanel = math.floor( Remainder / NumColumns )
for k, Column in pairs( self.Columns ) do
Remainder = math.Approach( Remainder, 0, PerPanel )
local TargetWidth = math.ceil( Column:GetWide() ) + PerPanel
Remainder = Remainder + ( TargetWidth - Column:SetWidth( TargetWidth ) )
if ( Remainder == 0 ) then break end
TotalMaxWidth = TotalMaxWidth + math.ceil( Column:GetMaxWidth() )
end
-- Total max width of all the columns is less than the width of the DListView, abort!
if ( TotalMaxWidth < self.pnlCanvas:GetWide() ) then break end
Remainder = math.Approach( Remainder, 0, 1 )
end
-- Set the positions of the resized columns
local x = 0
for k, Column in pairs( self.Columns ) do
Column.x = x
x = x + math.ceil( Column:GetWide() )
Column:SetTall( math.ceil( self:GetHeaderHeight() ) )
Column:SetVisible( !self:GetHideHeaders() )
end
end
function PANEL:PerformLayout()
-- Do Scrollbar
local Wide = self:GetWide()
local YPos = 0
if ( IsValid( self.VBar ) ) then
self.VBar:SetPos( self:GetWide() - 16, 0 )
self.VBar:SetSize( 16, self:GetTall() )
self.VBar:SetUp( self.VBar:GetTall() - self:GetHeaderHeight(), self.pnlCanvas:GetTall() )
YPos = self.VBar:GetOffset()
if ( self.VBar.Enabled ) then Wide = Wide - 16 end
end
if ( self.m_bHideHeaders ) then
self.pnlCanvas:SetPos( 0, YPos )
else
self.pnlCanvas:SetPos( 0, YPos + self:GetHeaderHeight() )
end
self.pnlCanvas:SetSize( Wide, self.pnlCanvas:GetTall() )
self:FixColumnsLayout()
--
-- If the data is dirty, re-layout
--
if ( self:GetDirty() ) then
self:SetDirty( false )
local y = self:DataLayout()
self.pnlCanvas:SetTall( y )
-- Layout again, since stuff has changed..
self:InvalidateLayout( true )
end
end
function PANEL:OnScrollbarAppear()
self:SetDirty( true )
self:InvalidateLayout()
end
function PANEL:OnRequestResize( SizingColumn, iSize )
-- Find the column to the right of this one
local Passed = false
local RightColumn = nil
for k, Column in pairs( self.Columns ) do
if ( Passed ) then
RightColumn = Column
break
end
if ( SizingColumn == Column ) then Passed = true end
end
-- Alter the size of the column on the right too, slightly
if ( RightColumn ) then
local SizeChange = SizingColumn:GetWide() - iSize
RightColumn:SetWide( RightColumn:GetWide() + SizeChange )
end
SizingColumn:SetWide( iSize )
self:SetDirty( true )
-- Invalidating will munge all the columns about and make it right
self:InvalidateLayout()
end
function PANEL:DataLayout()
local y = 0
local h = self.m_iDataHeight
local alt = false
for k, Line in ipairs( self.Sorted ) do
if ( !Line:IsVisible() ) then continue end
Line:SetPos( 1, y )
Line:SetSize( self:GetWide() - 2, h )
Line:DataLayout( self )
Line:SetAltLine( alt )
alt = !alt
y = y + Line:GetTall()
end
return y
end
function PANEL:AddLine( ... )
self:SetDirty( true )
self:InvalidateLayout()
local Line = vgui.Create( "DListView_Line", self.pnlCanvas )
local ID = table.insert( self.Lines, Line )
Line:SetListView( self )
Line:SetID( ID )
-- This assures that there will be an entry for every column
for k, v in pairs( self.Columns ) do
Line:SetColumnText( k, "" )
end
for k, v in pairs( {...} ) do
Line:SetColumnText( k, v )
end
-- Make appear at the bottom of the sorted list
local SortID = table.insert( self.Sorted, Line )
if ( SortID % 2 == 1 ) then
Line:SetAltLine( true )
end
return Line
end
function PANEL:OnMouseWheeled( dlta )
if ( !IsValid( self.VBar ) ) then return end
return self.VBar:OnMouseWheeled( dlta )
end
function PANEL:ClearSelection( dlta )
for k, Line in pairs( self.Lines ) do
Line:SetSelected( false )
end
end
function PANEL:GetSelectedLine()
for k, Line in pairs( self.Lines ) do
if ( Line:IsSelected() ) then return k, Line end
end
end
function PANEL:GetLine( id )
return self.Lines[ id ]
end
function PANEL:GetSortedID( line )
for k, v in pairs( self.Sorted ) do
if ( v:GetID() == line ) then return k end
end
end
function PANEL:OnClickLine( Line, bClear )
local bMultiSelect = self:GetMultiSelect()
if ( !bMultiSelect && !bClear ) then return end
--
-- Control, multi select
--
if ( bMultiSelect && input.IsKeyDown( KEY_LCONTROL ) ) then
bClear = false
end
--
-- Shift block select
--
if ( bMultiSelect && input.IsKeyDown( KEY_LSHIFT ) ) then
local Selected = self:GetSortedID( self:GetSelectedLine() )
if ( Selected ) then
local LineID = self:GetSortedID( Line:GetID() )
local First = math.min( Selected, LineID )
local Last = math.max( Selected, LineID )
-- Fire off OnRowSelected for each non selected row
for id = First, Last do
local line = self.Sorted[ id ]
if ( !line:IsLineSelected() ) then self:OnRowSelected( line:GetID(), line ) end
line:SetSelected( true )
end
-- Clear the selection and select only the required rows
if ( bClear ) then self:ClearSelection() end
for id = First, Last do
local line = self.Sorted[ id ]
line:SetSelected( true )
end
return
end
end
--
-- Check for double click
--
if ( Line:IsSelected() && Line.m_fClickTime && ( !bMultiSelect || bClear ) ) then
local fTimeDistance = SysTime() - Line.m_fClickTime
if ( fTimeDistance < 0.3 ) then
self:DoDoubleClick( Line:GetID(), Line )
return
end
end
--
-- If it's a new mouse click, or this isn't
-- multiselect we clear the selection
--
if ( !bMultiSelect || bClear ) then
self:ClearSelection()
end
if ( Line:IsSelected() ) then return end
Line:SetSelected( true )
Line.m_fClickTime = SysTime()
self:OnRowSelected( Line:GetID(), Line )
end
function PANEL:SortByColumns( c1, d1, c2, d2, c3, d3, c4, d4 )
table.sort( self.Sorted, function( a, b )
if ( !IsValid( a ) ) then return true end
if ( !IsValid( b ) ) then return false end
if ( c1 && a:GetColumnText( c1 ) != b:GetColumnText( c1 ) ) then
if ( d1 ) then a, b = b, a end
return a:GetColumnText( c1 ) < b:GetColumnText( c1 )
end
if ( c2 && a:GetColumnText( c2 ) != b:GetColumnText( c2 ) ) then
if ( d2 ) then a, b = b, a end
return a:GetColumnText( c2 ) < b:GetColumnText( c2 )
end
if ( c3 && a:GetColumnText( c3 ) != b:GetColumnText( c3 ) ) then
if ( d3 ) then a, b = b, a end
return a:GetColumnText( c3 ) < b:GetColumnText( c3 )
end
if ( c4 && a:GetColumnText( c4 ) != b:GetColumnText( c4 ) ) then
if ( d4 ) then a, b = b, a end
return a:GetColumnText( c4 ) < b:GetColumnText( c4 )
end
return true
end )
self:SetDirty( true )
self:InvalidateLayout()
end
function PANEL:SortByColumn( ColumnID, Desc )
table.sort( self.Sorted, function( a, b )
if ( Desc ) then
a, b = b, a
end
local aval = a:GetSortValue( ColumnID ) || a:GetColumnText( ColumnID )
local bval = b:GetSortValue( ColumnID ) || b:GetColumnText( ColumnID )
-- Maintain nicer sorting for numbers
if ( isnumber( aval ) && isnumber( bval ) ) then return aval < bval end
return tostring( aval ) < tostring( bval )
end )
self:SetDirty( true )
self:InvalidateLayout()
end
function PANEL:SelectItem( Item )
if ( !Item ) then return end
Item:SetSelected( true )
self:OnRowSelected( Item:GetID(), Item )
end
function PANEL:SelectFirstItem()
self:ClearSelection()
self:SelectItem( self.Sorted[ 1 ] )
end
function PANEL:DoDoubleClick( LineID, Line )
-- For Override
end
function PANEL:OnRowSelected( LineID, Line )
-- For Override
end
function PANEL:OnRowRightClick( LineID, Line )
-- For Override
end
function PANEL:Clear()
for k, v in pairs( self.Lines ) do
v:Remove()
end
self.Lines = {}
self.Sorted = {}
self:SetDirty( true )
end
function PANEL:GetSelected()
local ret = {}
for k, v in pairs( self.Lines ) do
if ( v:IsLineSelected() ) then
table.insert( ret, v )
end
end
return ret
end
function PANEL:SizeToContents()
self:SetHeight( self.pnlCanvas:GetTall() + self:GetHeaderHeight() )
end
function PANEL:GenerateExample( ClassName, PropertySheet, Width, Height )
local ctrl = vgui.Create( ClassName )
ctrl:AddColumn( "Address" )
local Col2 = ctrl:AddColumn( "Port" )
Col2:SetFixedWidth( 30 )
for i = 1, 128 do
ctrl:AddLine( "192.168.0." .. i, "80" )
end
ctrl:SetSize( 300, 200 )
PropertySheet:AddSheet( ClassName, ctrl, nil, true, true )
end
derma.DefineControl( "DListView", "Data View", PANEL, "DPanel" )