-
Notifications
You must be signed in to change notification settings - Fork 4
/
Class_LV_Rows.ahk
1314 lines (1287 loc) · 57.3 KB
/
Class_LV_Rows.ahk
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;=======================================================================================
;
; Class LV_Rows
;
; Author: Pulover [Rodolfo U. Batista]
; AHK version: 1.1.23.01
;
; Additional functions for ListView controls
;=======================================================================================
;
; This class provides an easy way to add functionalities to ListViews that are not
; supported by AutoHotkey built-in functions such as Copy, Cut, Paste, Drag and more.
;
;=======================================================================================
;
; Edit Functions:
; Copy([CopyData])
; Cut([CopyData])
; Paste([Row, Multiline])
; Duplicate()
; Delete()
; Move([Up])
; Drag([DragButton, AutoScroll, ScrollDelay, LineThick, Color])
;
; History Functions:
; Add([Data])
; Undo()
; Redo()
; ClearHistory()
;
; Group Functions:
; EnableGroups([Enable, FirstName, Collapsible, StartCollapsed])
; InsertGroup([Row, GroupName])
; RemoveGroup([Row])
; InsertAtGroup([Count, Rows*])
; RemoveAtGroup([Count, Rows*])
; SetGroups(Groups)
; GetGroups([AsObject])
; SetGroupCollapisable([Collapsible])
; RemoveAllGroups([FirstName])
; CollapseAll([Collapse])
; RefreshGroups([Collapsed])
;
; Management Functions:
; InsertHwnd(Hwnd)
; RemoveHwnd(Hwnd)
; SetHwnd(Hwnd [, NewData])
; GetData([Hwnd])
; SetData([Hwnd])
; SetCallback(Func)
;
;=======================================================================================
;
; Usage:
;
; You can call the function by preceding them with LV_Rows. For example:
; LV_Rows.Copy() <-- Calls function on active ListView.
;
; Or with a handle initialized via New meta-function. For example:
; MyListHandle := New LV_Rows() <-- Creates a new handle.
; MyListHandle.Add() <-- Calls function for that Handle.
;
; Like AutoHotkey built-in functions, these functions operate on the default gui,
; and active ListView control. When usingle handles, SetHwnd will attempt to set
; the selected ListView as active, but it's recommend to call Gui, Listview on
; your script too.
;
; Initializing is required for History and Group functions and in case your ListView
; has icons to be moved during Drag().
; Gui, Add, ListView, hwndhLV, Columns
; MyListHandle := New LV_Rows(hLV) <-- Creates a new handle with Hwnd.
;
; Gui, Add, ListView, hwndhLV1, Columns
; Gui, Add, ListView, hwndhLV2, Columns
; MyListHandle := New LV_Rows(hLV1, hLV2)
;
; You can also use the same handle for the Edit functions.
; You can create more handles or pass the ListView's Hwnd to operate on different
; lists with the same handle.
;
;=======================================================================================
Class LV_Rows extends LV_Rows.LV_EX
{
;=======================================================================================
; Meta-Functions Pass the Hwnd of one or more Listiviews when initializing it to
; manage history, groups and keep row's icons during Drag().
; Use InsertHwnd() to add more lists and SetHwnd() to switch between them.
;
; Properties:
; ActiveSlot: Contains the current entry position in the ListView History.
; HasChanged: The HasChanged property may optionally be used to check if the
; ListView has been changed. For this you must use a handle
; for all functions.
; Every time a function (except Copy) is called it will be set
; to true. The user may consult Handle.HasChanged to show
; a save dialog and set it to False after saving.
;=======================================================================================
__New(Hwnd*)
{
this.hArray := {}
If (Hwnd.Length())
{
For e, h in Hwnd
{
this.hArray[h] := { Hwnd: h
, GroupsArray: []
, Slot: []
, ActiveSlot: 1}
, this.hArray[h].GroupsArray.Push({Name: "Start", Row: 1, ID: this.GenerateRandomID()})
}
this.LVHwnd := this.hArray[Hwnd[1]].Hwnd
, this.Handle := this.hArray[Hwnd[1]]
}
Else
{
this.hArray["default"] := { Hwnd: ""
, GroupsArray: []
, Slot: []
, ActiveSlot: 1}
, this.Handle := this.hArray["default"]
, this.Handle.GroupsArray.Push({Name: "Start", Row: 1, ID: this.GenerateRandomID()})
}
}
__Call(Func)
{
Callback := this.Callback
If (IsFunc(Callback))
{
If (!%Callback%(Func, this.LVHwnd))
return
}
If Func in Cut,Paste,Duplicate,Delete,Move,Drag,Undo,Redo
this.HasChanged := true
}
__Delete()
{
this.RemoveAt(1, this.Length())
, this.SetCapacity(0)
, this.base := ""
}
;=======================================================================================
; Management Functions: Set, Insert and Remove ListView Hwnd's.
;=======================================================================================
; Method: Handle.InsertHwnd()
; Description: Inserts one or more ListView Hwnd's to be managed.
; Parameters:
; Hwnd: One or more ListView Hwnd's.
; Return: No return value.
;=======================================================================================
InsertHwnd(Hwnd*)
{
If (Hwnd.Length())
{
For e, h in Hwnd
{
If (this.hArray.HasKey(h))
continue
this.hArray[h] := { Hwnd: h
, GroupsArray: []
, Slot: []
, ActiveSlot: 1}
, this.hArray[h].GroupsArray.Push({Name: "Start", Row: 1, ID: this.GenerateRandomID()})
}
}
}
;=======================================================================================
; Method: Handle.RemoveHwnd()
; Description: Removes a ListView Hwnd.
; Parameters:
; Hwnd: Hwnd of the ListView to be removed.
; Return: No return value.
;=======================================================================================
RemoveHwnd(Hwnd)
{
If (this.hArray.HasKey(Hwnd))
{
this.hArray[Hwnd].RemoveAt(1, this.Length())
, this.hArray[Hwnd].SetCapacity(0)
, this.hArray[Hwnd].base := ""
}
}
;=======================================================================================
; Method: Handle.SetHwnd()
; Description: Selects a previously inserted ListView or adds it to the handle
; and selects it, optionally copying the data, history and
; groups from another Hwnd.
; Parameters:
; Hwnd: Hwnd of a ListView to be selected. If the hwnd is not found, it
; will be added to the handle and selected.
; NewData: Hwnd of a previously inserted ListView whose data, history and groups
; will be copied to the one being selected.
; Return: No return value.
;=======================================================================================
SetHwnd(Hwnd, NewData := "")
{
If (this.hArray.HasKey(Hwnd))
{
this.LVHwnd := this.hArray[Hwnd].Hwnd
, this.Handle := this.hArray[Hwnd]
Gui, Listview, % this.LVHwnd
If (NewData != "")
{
If (IsObject(NewData))
{
this.hArray[Hwnd].GroupsArray := NewData.GroupsArray
, this.hArray[Hwnd].Slot := NewData.Slot
, this.hArray[Hwnd].ActiveSlot := NewData.ActiveSlot
, this.Load()
}
Else If (this.hArray.HasKey(NewData))
{
this.hArray[Hwnd].GroupsArray := this.hArray[NewData].GroupsArray.Clone()
, this.hArray[Hwnd].Slot := this.DeepClone(this.hArray[NewData].Slot)
, this.hArray[Hwnd].ActiveSlot := this.hArray[NewData].ActiveSlot
, this.Load()
}
}
return
}
this.hArray[Hwnd] := { Hwnd: Hwnd
, GroupsArray: []
, Slot: []
, ActiveSlot: 1}
this.LVHwnd := Hwnd
, this.Handle := this.hArray[Hwnd]
, this.Handle.GroupsArray.Push({Name: "Start", Row: 1, ID: this.GenerateRandomID()})
Gui, Listview, % this.LVHwnd
If (NewData != "")
{
If (IsObject(NewData))
{
this.hArray[Hwnd].GroupsArray := NewData.GroupsArray
, this.hArray[Hwnd].Slot := NewData.Slot
, this.hArray[Hwnd].ActiveSlot := NewData.ActiveSlot
, this.Load()
}
Else If (this.hArray.HasKey(NewData))
{
this.hArray[Hwnd].GroupsArray := this.hArray[NewData].GroupsArray.Clone()
, this.hArray[Hwnd].Slot := this.DeepClone(this.hArray[NewData].Slot)
, this.hArray[Hwnd].ActiveSlot := this.hArray[NewData].ActiveSlot
, this.Load()
}
}
}
;=======================================================================================
; Method: Handle.GetData()
; Description: Retrieves the data, history and groups of a previously inserted
; ListView.
; Parameters:
; Hwnd: Hwnd of a previously inserted ListView. If left blank, the
; current active ListView will be returned.
; Return: An object with data, history and groups of a ListView.
;=======================================================================================
GetData(Hwnd := "")
{
If (Hwnd = "")
Hwnd := this.LVHwnd
If (this.hArray.HasKey(Hwnd))
return this.DeepClone(this.hArray[Hwnd])
}
;=======================================================================================
; Method: Handle.SetData()
; Description: Sets the CopyData, data, history and groups for specific or current
; ListView.
; Parameters:
; Hwnd: Hwnd of a previously inserted ListView. If left blank, the
; current active ListView will be used.
; Return: No return value.
;=======================================================================================
SetData(Hwnd := "", Data := "", CopyData := "")
{
If (Hwnd = "")
Hwnd := this.LVHwnd
this.LVHwnd := this.hArray[Hwnd].Hwnd
, this.Handle := this.hArray[Hwnd]
Gui, Listview, % this.LVHwnd
If (Data != "")
{
If (IsObject(Data))
{
this.hArray[Hwnd].GroupsArray := Data.GroupsArray
, this.hArray[Hwnd].Slot := Data.Slot
, this.hArray[Hwnd].ActiveSlot := Data.ActiveSlot
, this.Load()
}
Else If (this.hArray.HasKey(Data))
{
this.hArray[Hwnd].GroupsArray := this.hArray[Data].GroupsArray.Clone()
, this.hArray[Hwnd].Slot := this.DeepClone(this.hArray[Data].Slot)
, this.hArray[Hwnd].ActiveSlot := this.hArray[Data].ActiveSlot
, this.Load()
}
}
If (CopyData != "")
this.CopyData := CopyData
}
;=======================================================================================
; Method: LV_Rows.SetCallback()
; Description: Sets a callback function where the user can take actions based
; on the function being called called. The Callback function
; must return true for the operation to be completed.
; Parameters:
; Func: Name of a user-defined function that should receive 2 parameters:
; The name of the function being called and the Hwnd of the
; current set ListView.
; Return: No return value.
;=======================================================================================
SetCallback(Func)
{
this.Callback := Func
}
;=======================================================================================
; Edit Functions: Edit ListView rows.
;=======================================================================================
; Method: LV_Rows.Copy()
; Description: Copy selected rows to memory.
; Parameters:
; CopyData: Optional output variable to store the copied data.
; Return: Number of copied rows.
;=======================================================================================
Copy(ByRef CopyData := "")
{
this.CopyData := [], LV_Row := 0
Loop
{
LV_Row := LV_GetNext(LV_Row)
If (!LV_Row)
break
RowData := this.RowText(LV_Row)
, Row := [RowData*]
, this.CopyData.Push(Row)
, CopiedLines := A_Index
}
CopyData := this.CopyData
return CopiedLines
}
;=======================================================================================
; Method: LV_Rows.Cut()
; Description: Copy selected rows to memory and delete them.
; Parameters:
; CopyData: Optional output variable to store the copied data.
; Return: Number of copied rows.
;=======================================================================================
Cut(ByRef CopyData := "")
{
this.CopyData := [], LV_Row := 0
Loop
{
LV_Row := LV_GetNext(LV_Row)
If (!LV_Row)
break
RowData := this.RowText(LV_Row)
, Row := [RowData*]
, this.CopyData.Push(Row)
, CopiedLines := A_Index
}
CopyData := this.CopyData
this.Delete()
this.RefreshGroups()
return CopiedLines
}
;=======================================================================================
; Method: LV_Rows.Paste()
; Description: Paste copied rows at selected position.
; Parameters:
; Row: If non-zero pastes memory contents at the specified row.
; Multiline: If true pastes the contents at every selected row.
; Return: True if memory contains data or false if not.
;=======================================================================================
Paste(Row := 0, Multiline := false)
{
If (!this.CopyData.Length())
return false
TargetRow := Row ? Row : LV_GetNext()
If (!TargetRow)
{
For each, Row in this.CopyData
LV_Add(Row*)
}
Else
{
If ((!Row) && (Multiline))
{
LV_Row := 0
Loop
{
LV_Row := LV_GetNext(LV_Row - 1)
If (!LV_Row)
break
For e, g in this.Handle.GroupsArray
{
If ((g.Row > 1) && (LV_Row < g.Row))
g.Row += this.CopyData.Length()
}
For each, Row in this.CopyData
LV_Insert(LV_Row, Row*), LV_Row += 1
LV_Row += 1
}
}
Else
{
LV_Row := TargetRow - 1
For e, g in this.Handle.GroupsArray
{
If ((g.Row > 1) && ((LV_Row+1) < g.Row))
g.Row += this.CopyData.Length()
}
For each, Row in this.CopyData
LV_Insert(LV_Row+A_Index, Row*)
}
}
this.RefreshGroups()
return true
}
;=======================================================================================
; Method: LV_Rows.Duplicate()
; Description: Duplicates selected rows.
; Return: Number of duplicated rows.
;=======================================================================================
Duplicate()
{
CopyData := this.CopyData.Clone()
, DupLines := this.Copy()
, this.Paste()
, this.CopyData := CopyData.Clone()
, CopyData.RemoveAt(1, CopyData.Length())
, CopyData := ""
, this.RefreshGroups()
return DupLines
}
;=======================================================================================
; Method: LV_Rows.Delete()
; Description: Delete selected rows.
; Return: Number of removed rows.
;=======================================================================================
Delete()
{
If (LV_GetCount("Selected") = 0)
return false
LV_Row := 0
Loop
{
LV_Row := LV_GetNext(LV_Row - 1)
If (!LV_Row)
break
LV_Delete(LV_Row)
, DeletedLines := A_Index
For e, g in this.Handle.GroupsArray
{
If ((g.Row > 1) && (LV_Row < g.Row))
g.Row--
}
}
this.RefreshGroups()
return DeletedLines
}
;=======================================================================================
; Method: LV_Rows.Move()
; Description: Move selected rows down or up.
; Parameters:
; Up: If false or omitted moves rows down. If true moves rows up.
; Return: Number of rows moved.
;=======================================================================================
Move(Up := false)
{
Selections := [], LV_Row := 0
Critical
If (Up)
{
Loop
{
LV_Row := LV_GetNext(LV_Row)
If (!LV_Row)
break
If (LV_Row = 1)
return
Selections.Push(LV_Row)
}
For each, Row in Selections
{
RowData := this.RowText(Row)
, LV_Insert(Row-1, RowData*)
, LV_Delete(Row+1)
, LV_Modify(Row-1, "Select")
If (A_Index = 1)
LV_Modify(Row-1, "Focus Vis")
}
this.RefreshGroups()
return Selections.Length()
}
Else
{
Loop
{
LV_Row := LV_GetNext(LV_Row)
If (!LV_Row)
break
If (LV_Row = LV_GetCount())
return
Selections.InsertAt(1, LV_Row)
}
For each, Row in Selections
{
RowData := this.RowText(Row+1)
, LV_Insert(Row, RowData*)
, LV_Delete(Row+2)
If (A_Index = 1)
LV_Modify(Row+1, "Focus Vis")
}
this.RefreshGroups()
return Selections.Length()
}
}
;=======================================================================================
; Method: LV_Rows.Drag()
; Description: Drag-and-Drop selected rows showing a destination bar.
; Must be called in the ListView G-Label subroutine when
; A_GuiEvent returns "D" or "d".
; Parameters:
; DragButton: If it is a lower case "d" it will be recognized as a Right-Click
; drag and won't actually move any row, only return the
; destination, otherwise it will be recognized as a Left-Click
; drag. You may pass A_GuiEvent as the parameter.
; AutoScroll: If true or omitted the ListView will automatically scroll
; up or down when the cursor is above or below the control.
; ScrollDelay: Delay in miliseconds for AutoScroll. Default is 100ms.
; LineThick: Thickness of the destination bar in pixels. Default is 2px.
; Color: Color of destination bar. Default is "Black".
; Drop: Set to False to disable automatically dropping selected rows.
; Return: The destination row number.
;=======================================================================================
Drag(DragButton := "D", AutoScroll := true, ScrollDelay := 100, LineThick := 2, Color := "Black", Drop := true)
{
Static LVIR_LABEL := 0x0002
Static LVM_GETITEMCOUNT := 0x1004
Static LVM_SCROLL := 0x1014
Static LVM_GETTOPINDEX := 0x1027
Static LVM_GETCOUNTPERPAGE := 0x1028
Static LVM_GETSUBITEMRECT := 0x1038
Static LV_currColHeight := 0
RestoreGroups := false
If (this.IsGroupViewEnabled())
{
RestoreGroups := true
, this.EnableGroupView(false)
}
SysGet, SM_CXVSCROLL, 2
If (InStr(DragButton, "d", true))
DragButton := "RButton"
Else
DragButton := "LButton"
CoordMode, Mouse, Window
MouseGetPos,,, LV_Win, LV_LView, 2
WinGetPos, Win_X, Win_Y, Win_W, Win_H, ahk_id %LV_Win%
ControlGetPos, LV_lx, LV_ly, LV_lw, LV_lh, , ahk_id %LV_LView%
LV_lw := LV_lw * 96 // A_ScreenDPI
, VarSetCapacity(LV_XYstruct, 16, 0)
While (GetKeyState(DragButton, "P"))
{
MouseGetPos, LV_mx, LV_my,, CurrCtrl, 2
LV_mx -= LV_lx, LV_my -= LV_ly
If (AutoScroll)
{
If (LV_my < 0)
{
SendMessage, LVM_SCROLL, 0, -LV_currColHeight,, ahk_id %LV_LView%
Sleep, %ScrollDelay%
}
If (LV_my > LV_lh)
{
SendMessage, LVM_SCROLL, 0, LV_currColHeight,, ahk_id %LV_LView%
Sleep, %ScrollDelay%
}
}
If (CurrCtrl != LV_LView)
{
LV_currRow := ""
Gui, MarkLine:Cancel
continue
}
SendMessage, LVM_GETITEMCOUNT, 0, 0,, ahk_id %LV_LView%
LV_TotalNumOfRows := ErrorLevel
SendMessage, LVM_GETCOUNTPERPAGE, 0, 0,, ahk_id %LV_LView%
LV_NumOfRows := ErrorLevel
SendMessage, LVM_GETTOPINDEX, 0, 0,, ahk_id %LV_LView%
LV_topIndex := ErrorLevel
, Line_W := (LV_TotalNumOfRows > LV_NumOfRows) ? LV_lw - SM_CXVSCROLL : LV_lw
Loop, % LV_NumOfRows + 1
{
LV_which := LV_topIndex + A_Index - 1
NumPut(LVIR_LABEL, LV_XYstruct, 0, "UInt")
, NumPut(A_Index - 1, LV_XYstruct, 4, "UInt")
SendMessage, LVM_GETSUBITEMRECT, LV_which, &LV_XYstruct,, ahk_id %LV_LView%
LV_RowY := NumGet(LV_XYstruct, 4, "UInt")
, LV_RowY2 := NumGet(LV_XYstruct, 12, "UInt")
, LV_currColHeight := LV_RowY2 - LV_RowY
If (LV_my <= LV_RowY + LV_currColHeight)
{
LV_currRow := LV_which + 1
, LV_currRow0 := LV_which
, Line_Y := Win_Y + LV_ly + LV_RowY
, Line_X := Win_X + LV_lx
If (LV_currRow > (LV_TotalNumOfRows+1))
{
Gui, MarkLine:Cancel
LV_currRow := ""
}
Break
}
}
If (LV_currRow)
{
Gui, MarkLine:Color, %Color%
Gui, MarkLine:+LastFound +AlwaysOnTop +Toolwindow -Caption +HwndLineMark
Gui, MarkLine:Show, W%Line_W% H%LineThick% Y%Line_Y% X%Line_X% NoActivate
}
}
Gui, MarkLine:Cancel
If (Drop && DragButton = "LButton" && LV_currRow)
{
CopyData := this.CopyData.Clone()
Lines := this.Copy()
, this.Paste(LV_currRow)
If (LV_GetNext() < LV_currRow)
o := Lines+1, FocusedRow := LV_currRow-1
Else
o := 1, FocusedRow := LV_currRow
this.Delete()
Loop, %Lines%
{
i := A_Index-o
, LV_Modify(LV_currRow+i, "Select")
}
LV_Modify(FocusedRow, "Focus")
, this.CopyData := CopyData.Clone()
, CopyData.RemoveAt(1, CopyData.Length())
, CopyData := ""
}
If (RestoreGroups)
this.EnableGroupView()
return LV_currRow
}
;=======================================================================================
; History Functions: Keep a history of ListView changes and allow Undo and Redo.
; These functions operate on the currently selected ListView.
;=======================================================================================
; Method: Handle.Add()
; Description: Adds an entry on History. This function requires
; initializing: MyListHandle := New LV_Rows()
; Parameters:
; Data: An optional object containing an array of RowText Data. The
; object MUST be an array of objects retrieved using RowText().
; Return: The total number of entries in history.
;=======================================================================================
Add(Data := "")
{
Rows := Data != "" ? Data : []
If (Data = "")
{
Loop, % LV_GetCount()
{
RowData := this.RowText(A_Index)
, Rows[A_Index] := [RowData*]
}
}
Groups := []
For e, g in this.Handle.GroupsArray
Groups[e] := {Name: g.Name, Row: g.Row, ID: this.GenerateRandomID()}
SlotsAhead := this.Handle.Slot.Length() - this.Handle.ActiveSlot
, SlotsAhead > 0 ? this.Handle.Slot.RemoveAt(this.Handle.ActiveSlot + 1, SlotsAhead) : ""
, this.Handle.Slot.Push({Rows: Rows, Groups: Groups})
, this.Handle.ActiveSlot := this.Handle.Slot.Length()
return this.Handle.Slot.Length()
}
;=======================================================================================
; Method: Handle.Undo()
; Description: Replaces ListView contents with previous entry state, if any.
; Return: New entry position or false if it's already the first entry.
;=======================================================================================
Undo()
{
If (this.Handle.ActiveSlot = 1)
return false
this.Handle.ActiveSlot -= 1
, this.Load()
return this.Handle.ActiveSlot
}
;=======================================================================================
; Method: Handle.Redo()
; Description: Replaces ListView contents with next entry state, if any.
; Return: New entry position or false if it's already the last entry.
;=======================================================================================
Redo()
{
If (this.Handle.ActiveSlot = (this.Handle.Slot.Length()))
return false
this.Handle.ActiveSlot += 1
, this.Load()
return this.Handle.ActiveSlot
}
;=======================================================================================
; Method: Handle.ClearHistory()
; Description: Removes all history entries from the ListView.
; Return: New entry position or false if it's already the last entry.
;=======================================================================================
ClearHistory()
{
this.Handle.Slot.RemoveAt(1, this.Handle.Slot.Length())
, this.Handle.ActiveSlot := 1
}
;=======================================================================================
; Group Functions: Set, add and remove Listview Groups.
; These functions are based on just me's LV_EX library:
; http://autohotkey.com/boards/viewtopic.php?t=1256
;=======================================================================================
; Method: Handle.EnableGroups()
; Description: Enables or disables Groups in the currently selected ListView
; initializing: MyListHandle := New LV_Rows()
; Parameters:
; Enable: If true enables GroupView in the selected ListView. If false
; disables it.
; FirstName: Name for the first (mandatory) group at row 1.
; Collapsible: If true makes the groups collapsible.
; StartCollapsed: If true starts all groups collapsed. If false starts all groups
; expanded.
; Return: No return value.
;=======================================================================================
EnableGroups(Enable := true, FirstName := "New Group", Collapsible := true, StartCollapsed := "")
{
Gui, Listview, % this.LVHwnd
ListCount := LV_GetCount()
, this.Collapsible := Collapsible
, this.EnableGroupView(Enable)
If (Enable)
{
If (!this.Handle.GroupsArray.Length())
this.Handle.GroupsArray := [{Name: FirstName, Row: 1, ID: this.GenerateRandomID()}]
this.RefreshGroups(StartCollapsed)
}
}
;=======================================================================================
; Method: Handle.InsertGroup()
; Description: Inserts or renames a group on top of the specified row.
; Parameters:
; Row: Number of the row for the group to be inserted. If left blank
; the first selected row will be used.
; GroupName: Name of the new group or new name for an existing group.
; Return: True if Row is bigger than 0 or false otherwise.
;=======================================================================================
InsertGroup(Row := "", GroupName := "New Group")
{
If (Row = "")
Row := LV_GetNext()
If ((!Row) || (Row =< 0))
return false
For e, g in this.Handle.GroupsArray
{
If (Row = g.Row)
{
this.Handle.GroupsArray[e] := {Name: GroupName, Row: Row, ID: this.GenerateRandomID()}
break
}
If (Row < g.Row)
{
this.Handle.GroupsArray.InsertAt(e, {Name: GroupName, Row: Row, ID: this.GenerateRandomID()})
break
}
If (e = this.Handle.GroupsArray.Length())
{
this.Handle.GroupsArray.Push({Name: GroupName, Row: Row, ID: this.GenerateRandomID()})
break
}
}
If (!this.Handle.GroupsArray.Length())
{
this.Handle.GroupsArray.Push({Name: "New Group", Row: 1, ID: this.GenerateRandomID()})
If (Row > 1)
this.Handle.GroupsArray.Push({Name: GroupName, Row: Row, ID: this.GenerateRandomID()})
}
this.RefreshGroups()
return true
}
;=======================================================================================
; Method: Handle.RemoveGroup()
; Description: Removes the group the indicated row belongs to.
; Parameters:
; Row: Number of a row the group belongs to. If left blank the first
; selected row will be used.
; Return: True if Row is bigger than 0 or false otherwise.
;=======================================================================================
RemoveGroup(Row := "")
{
If (Row = "")
Row := LV_GetNext()
If (Row =< 0)
return false
For e, g in this.Handle.GroupsArray
{
If (Row = g.Row)
{
If (g.Row = 1)
return
this.Handle.GroupsArray.RemoveAt(e)
break
}
If (Row < g.Row)
{
If (e = 2)
return
this.Handle.GroupsArray.RemoveAt(e - 1)
break
}
If (e = this.Handle.GroupsArray.Length())
{
If (g.Row = 1)
return
this.Handle.GroupsArray.RemoveAt(e)
break
}
}
this.RefreshGroups()
return true
}
;=======================================================================================
; Method: Handle.InsertAtGroup()
; Description: Inserts a row at indicated position, moving groups after it one
; row down.
; Parameters:
; Row: Number of the row where to insert.
; Return: No return value.
;=======================================================================================
InsertAtGroup(Row)
{
For e, g in this.Handle.GroupsArray
{
If ((Row > 0) && (Row < g.Row))
g.Row++
}
}
;=======================================================================================
; Method: Handle.RemoveAtGroup()
; Description: Removes a row from indicated position, moving groups after it one
; row up.
; Parameters:
; Row: Number of the row where to insert.
; Return: No return value.
;=======================================================================================
RemoveAtGroup(Row)
{
For e, g in this.Handle.GroupsArray
{
If ((Row > 0) && (Row < g.Row))
g.Row--
}
}
;=======================================================================================
; Method: Handle.SetGroups()
; Description: Sets one or more groups in the selected ListView.
; Parameters:
; Groups: A list of groups in the format "GroupName:RowNumber" separated
; by comma. You can use GetGroups() to save a valid String or
; Object to be used with this function.
; Return: No return value.
;=======================================================================================
SetGroups(Groups)
{
this.Handle.GroupsArray := []
If (!Groups.Length())
{
Loop, Parse, Groups, `,, %A_Space%
{
Pars := StrSplit(A_LoopField, ":", A_Space)
, this.Handle.GroupsArray.Push({Name: Pars[1], Row: Pars[2], ID: this.GenerateRandomID()})
}
}
Else
this.Handle.GroupsArray := Groups
this.Handle.Slot[this.Handle.ActiveSlot].Groups := this.Handle.GroupsArray
, this.RefreshGroups()
}
;=======================================================================================
; Method: Handle.GetGroups()
; Description: Returns a string or object representing the current groups in
; the selected ListView.
; Parameters:
; AsObject: If true returns an object with the groups, otherwise an string.
; Both can be used with SetGroups().
; Return: No return value.
;=======================================================================================
GetGroups(AsObject := false)
{
If (AsObject)
return this.Handle.GroupsArray.Clone()
For e, g in this.Handle.GroupsArray
GroupsString .= g.Name ":" g.Row ","
return RTrim(GroupsString, ",")
}
;=======================================================================================
; Method: Handle.SetGroupCollapisable()
; Description: Enables or disables Groups Collapsible style.
; Parameters:
; Collapsible: If true enables Collapsible style in the selected ListView.
; If false disables it.
; Return: No return value.
;=======================================================================================
SetGroupCollapisable(Collapsible := true)
{
this.Collapsible := Collapsible
, this.RefreshGroups()
}
;=======================================================================================
; Method: Handle.RemoveAllGroups()
; Description: Removes all groups in the selected ListView.
; Parameters:
; FirstName: Name for the first (mandatory) group at row 1.
; Return: No return value.
;=======================================================================================
RemoveAllGroups(FirstName := "New Group")
{
If (!this.Handle.GroupsArray.Length())
this.Handle.GroupsArray.Push({Name: FirstName, Row: 1, ID: this.GenerateRandomID()})
Else
{
this.Handle.GroupsArray[1] := {Name: FirstName, Row: 1, ID: this.GenerateRandomID()}
, this.Handle.GroupsArray.RemoveAt(2, this.Handle.GroupsArray.Length())
}
this.RefreshGroups()
}
;=======================================================================================
; Method: Handle.CollapseAll()
; Description: Collapses or expands all groups.
; Parameters:
; Collapse: If true collapses all groups in the selected ListView. If false
; expands all groups in the selected ListView.
; Return: No return value.
;=======================================================================================
CollapseAll(Collapse := true)
{
this.RefreshGroups(Collapse)
}
;=======================================================================================
; Method: Handle.RefreshGroups()
; Description: Reloads the ListView to update groups. This function is called
; automatically in from other functions, usually it's not
; necessary to use it in your script.
; Parameters:
; Collapsed: If true collapses all groups in the selected ListView. If false
; expands all groups in selected ListView.
; Return: No return value.
;=======================================================================================
RefreshGroups(Collapsed := "")
{
GroupsEnabled := this.IsGroupViewEnabled()
, this.EnableGroupView(false)
, GroupStates := []
Gui, Listview, % this.LVHwnd
For e, g in this.Handle.GroupsArray
{
this.GroupGetState(g.ID, IsCollapsed)
, GroupStates.Push(IsCollapsed ? "Collapsed" : "")
}
ListCount := LV_GetCount()
, this.GroupRemoveAll(), GrNum := 1
Loop, %ListCount%
{
If (this.Handle.GroupsArray[GrNum].Row = A_Index)
{
Group := this.Handle.GroupsArray[GrNum]
, this.GroupInsert(Group.ID, Group.Name)
, Styles := !this.Collapsible ? []
: Collapsed = "" ? ["Collapsible", GroupStates[GrNum]]
: Collapsed ? ["Collapsible", "Collapsed"]
: ["Collapsible", ""]