-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormListView.cpp
More file actions
1422 lines (1292 loc) · 36.8 KB
/
Copy pathFormListView.cpp
File metadata and controls
1422 lines (1292 loc) · 36.8 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
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
// Copyleft 2004 Chris Korda
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or any later version.
/*
chris korda
revision history:
rev date comments
00 21nov03 initial version
01 30nov03 CreateItem must maximize pos slider
02 01dec03 MoveItems must call ResizeToFit
03 02dec03 bring back MaximizeItems
04 10dec03 don't call ResizeParentToFit
05 10dec03 add default column widths
06 11dec03 in Reset, close item first to avoid slow painting
07 30dec03 add sort items, add set transport
08 31dec03 add current position
09 05jan04 sort was moving items needlessly
10 08jan04 add sort items
11 19jan04 convert to MDI
12 23jan04 create item was trashing siblings, must set Z-order
13 25jan04 make item visible, then set parent, then set Z-order
14 25jan04 deselect before inserting, otherwise selection scrolls
15 31jan04 OnActivate now calls SetCurPos
16 31jan04 create item: set Z-pos, make it visible, then move
17 11feb04 make item visible with ShowWindow, not ModifyStyle
18 13feb04 header control is a member of view now
19 13feb04 improve modularity of clipboard support
20 17feb04 go back to creating view in OnInitialUpdate
21 22feb04 add undo
22 07mar04 undo sort must save indices, not pointers
23 08mar04 undo sort must restore sort key and direction
24 09mar04 set wait cursor in RestoreUndoState
25 25sep04 override CalcWindowRect to make room for header ctrl
26 25sep04 header's OnViewSize doesn't take args anymore
27 15nov04 add item dragging
28 15dec04 in SetItemCount, if doc was empty, set current pos
29 30dec04 in CreateItem, if OnNewItem fails, return failure
30 10jan05 add OnPaste handler
31 02feb05 add undo column width
32 07feb05 add EnsureVisible; call it from SortItems
form list view
*/
// FormListView.cpp : implementation of the CFormListView class
//
#include "stdafx.h"
#include "Resource.h"
#include "ChildFrm.h"
#include "FormListView.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFormListView
const int CFormListView::m_UndoTitleID[UNDO_CODES] = { // must match undo code enum
IDS_FLU_SELECT,
IDS_FLU_SORT,
IDS_FLU_CUT,
IDS_FLU_PASTE,
IDS_FLU_INSERT,
IDS_FLU_DELETE,
IDS_FLU_RAISE_ITEM_COUNT,
IDS_FLU_LOWER_ITEM_COUNT,
IDS_FLU_DRAG,
IDS_FLU_COLUMN_WIDTH
};
const int CFormListView::SCROLL_DELAY = 50; // milliseconds
CFormListView *CFormListView::m_SortThis;
IMPLEMENT_DYNCREATE(CFormListView, CFormView)
/////////////////////////////////////////////////////////////////////////////
// CFormListView construction/destruction
CFormListView::CFormListView() :
CFormView(CFormListView::IDD),
m_Clipboard(m_hWnd, NULL), // don't register a format
#pragma warning(disable : 4355) // 'this' used in base member initializer list
// these ctors don't access 'this', they only copy it to a member var
m_UndoManager(this),
m_Resizer(this),
m_Hdr(this)
#pragma warning(default : 4355)
{
//{{AFX_DATA_INIT(CFormListView)
//}}AFX_DATA_INIT
m_ViewCreated = FALSE;
m_ItemInfoSize = sizeof(CFormListInfo);
m_ColDef = NULL;
m_Columns = 0;
m_SortKey = -1;
m_SortDir = 0;
m_CurPos = 0;
m_Selections = 0;
m_BlockBegin = 0;
m_BlockEnd = 0;
m_SelCury = -1;
m_SelOrgy = -1;
m_ScrollDelta = 0;
m_ScrollTimer = 0;
m_DragPos = -1;
m_Dragging = FALSE;
m_NewItemCount = 0;
m_PrevSortKey = 0;
m_PrevSortDir = 0;
m_Modified = FALSE;
m_PasteInfo = NULL;
m_PasteLen = 0;
SetUndoManager(&m_UndoManager);
CoCreateGuid(&m_ClipboardOwner);
m_Clipboard.SetOwner(&m_ClipboardOwner);
}
CFormListView::~CFormListView()
{
SetItemCount(0);
}
/////////////////////////////////////////////////////////////////////////////
// CFormListView initialization
void CFormListView::InitView(const CColumnResizer::COLDEF *ColumnInfo, int Columns,
int InfoItemSize, LPCSTR ClipboardFormat)
{
m_ColDef = ColumnInfo;
m_Columns = Columns;
m_ItemInfoSize = InfoItemSize;
m_Clipboard.SetFormat(ClipboardFormat);
}
bool CFormListView::Create()
{
m_Resizer.Create(m_ColDef, m_Columns); // pass resizer our column info
m_Hdr.Create(); // create header control at top of our frame
PopulateHeader();
OnCreateView(); // create derived class
m_ViewCreated = TRUE;
return(TRUE);
}
/////////////////////////////////////////////////////////////////////////////
// CFormListView item array
int CFormListView::GetFullItemCount() const
{
int FullItems = 0;
for (int i = 0; i < GetItemCount(); i++) {
if (!IsEmpty(i))
FullItems++;
}
return(FullItems);
}
int CFormListView::SetItemCount(int Count)
{
int Prev = GetItemCount();
if (Count == Prev)
return(Prev);
CWaitCursor wc;
if (Count > Prev) {
m_Item.SetSize(Count);
for (int i = Prev; i < Count; i++) {
m_Item[i] = CreateItem(i);
// if create failed, stop creating items
if (m_Item[i] == NULL) {
Count = i; // number of items actually created
m_Item.SetSize(Count); // resize the array
break;
}
}
} else {
// close items before deleting them to avoid slow painting
int i;
for (i = Count; i < Prev; i++)
m_Item[i]->CloseItem();
// now items can be deleted quickly
for (i = Count; i < Prev; i++)
DestroyItem(i);
m_Item.SetSize(Count);
}
// current position could be invalid
if (m_CurPos >= Count || !Prev)
SetCurPos(0);
// calculate the minimum size required to fit all items
ResizeToFit(); // otherwise TFU
return(Count); // number of items actually created
}
int CFormListView::SetItemCountUndoable(int Count)
{
int Prev = GetItemCount();
if (Count == Prev) // nothing to do
return(Prev);
if (Count < Prev) { // deleting items, undo handler must save them
CWaitCursor wc;
m_NewItemCount = Count; // pass new item count to undo handler
NotifyUndoableEdit(UCODE_LOWER_ITEM_COUNT);
// close items before deleting them to avoid slow painting
for (int i = Count; i < Prev; i++)
m_Item[i]->CloseItem();
Delete(Count, NULL, Prev - Count);
} else { // adding empty items, undo handler only saves item count
NotifyUndoableEdit(UCODE_RAISE_ITEM_COUNT);
Count = SetItemCount(Count);
}
return(Count);
}
CFormListItemDlg *CFormListView::CreateItem(int ItemIdx)
{
CFormListItemDlg *Item = OnNewItem();
if (Item == NULL) // if item creation failed, bail out
return(NULL);
// make item a child of view, so it scrolls with view
Item->SetParent(this);
// set its index
Item->SetIndex(ItemIdx);
// set its column widths
m_Resizer.UpdateRow(Item->GetWndList());
// move it to bottom of Z-order, to avoid trashing siblings
Item->SetWindowPos(&CWnd::wndBottom, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
// make it visible
Item->ShowWindow(SW_NORMAL);
// move it into place; must be visible, or results are unpredictable
MoveItem(Item, ItemIdx);
return(Item);
}
void CFormListView::DestroyItem(int ItemIdx)
{
CFormListItemDlg *Item = m_Item[ItemIdx];
// mark as deleted first, in case destructor iterates over items
m_Item[ItemIdx] = NULL;
delete Item;
}
void CFormListView::MoveItem(CFormListItemDlg *Item, int ItemIdx)
{
CRect cr;
Item->GetClientRect(cr);
cr.OffsetRect(-GetScrollPosition());
cr.OffsetRect(0, ItemIdx * cr.Height());
Item->MoveWindow(cr);
}
void CFormListView::ResizeToFit()
{
CRect cr, BestFit;
int Items = GetItemCount();
if (Items) {
m_Item[Items - 1]->GetWindowRect(cr);
ScreenToClient(cr);
GetClientRect(BestFit);
BestFit.BottomRight() = cr.BottomRight() + GetScrollPosition();
} else
BestFit.SetRectEmpty();
SetScrollSizes(MM_TEXT, BestFit.Size());
}
void CFormListView::MoveItems()
{
for (int i = 0; i < GetItemCount(); i++)
MoveItem(m_Item[i], i);
ResizeToFit();
}
void CFormListView::IndexItems()
{
for (int i = 0; i < GetItemCount(); i++)
m_Item[i]->SetIndex(i);
}
/////////////////////////////////////////////////////////////////////////////
// CFormListView item sorting
void CFormListView::SortItems(int SortKey, bool Descending)
{
Deselect();
m_PrevSortKey = m_SortKey; // save current sort parameters for undo
m_PrevSortDir = m_SortDir;
m_SortKey = SortKey;
m_SortDir = Descending ? -1 : 1;
CWaitCursor wc;
m_SortThis = this; // pass sort function our this
qsort(m_Item.GetData(), GetItemCount(), sizeof(void *), SortCompare);
NotifyUndoableEdit(UCODE_SORT);
MoveSortedItems();
EnsureVisible(m_CurPos);
}
void CFormListView::MoveSortedItems()
{
// item pointers are in order now, but their windows haven't been moved;
// move windows in a chain pattern so that no window ever gets overwritten
CByteArray Moved;
Moved.SetSize(GetItemCount());
int i;
for (i = 0; i < GetItemCount(); i++) {
// if window hasn't been moved yet, and needs to move
if (!Moved[i] && m_Item[i]->GetIndex() != i) {
// first window in the chain; move it out of harm's way
MoveItem(m_Item[i], -1);
Moved[i] = TRUE;
int Next = i;
do {
Next = m_Item[Next]->GetIndex(); // find next window in chain
MoveItem(m_Item[Next], Next); // move it into place
Moved[Next] = TRUE;
} while (Next != i); // until we're back where we started
}
}
// re-index the items and fix current position
for (i = 0; i < GetItemCount(); i++) {
m_Item[i]->SetIndex(i);
if (m_Item[i]->IsActive())
m_CurPos = i;
}
}
void CFormListView::EnsureVisible(int Pos)
{
CRect vr, ir;
GetClientRect(vr); // get view's rect
m_Item[Pos]->GetWindowRect(ir); // get item's rect
ScreenToClient(ir); // in view coordinates
if (ir.top < 0 || ir.bottom > vr.bottom) { // if item isn't entirely visible
CPoint sp = GetScrollPosition();
sp.y += ir.top;
ScrollToPosition(sp); // scroll so that it's at the top of the view
}
}
/////////////////////////////////////////////////////////////////////////////
// CFormListView position/selection
void CFormListView::SetCurPos(int Pos)
{
if (m_CurPos < GetItemCount())
m_Item[m_CurPos]->ActivateItem(FALSE);
if (Pos < GetItemCount())
m_Item[Pos]->ActivateItem(TRUE);
m_CurPos = Pos;
}
void CFormListView::OnActivate(CFormListItemDlg *Item)
{
int Pos = Item->GetIndex();
if (Pos == m_CurPos) // item already active, nothing to do
return;
SetCurPos(Pos);
}
bool CFormListView::IsEmpty(int Pos) const
{
if (!IsValidPos(Pos))
return(TRUE);
return(m_Item[Pos]->IsEmpty());
}
bool CFormListView::IsCurEmpty() const
{
if (!IsValidPos(m_CurPos))
return(TRUE);
return(m_Item[m_CurPos]->IsEmpty());
}
bool CFormListView::IsSelected(int Pos) const
{
if (!IsValidPos(Pos))
return(FALSE);
return(m_Item[Pos]->IsSelected());
}
bool CFormListView::IsSelOrCur(int Pos) const
{
if (!IsValidPos(Pos))
return(FALSE);
return(m_Selections ? m_Item[Pos]->IsSelected() : (Pos == m_CurPos));
}
bool CFormListView::IsSelOrCurAndFull(int Pos) const
{
return(IsSelOrCur(Pos) && !IsEmpty(Pos));
}
int CFormListView::GetSingleSel() const
{
if (!m_Selections)
return(m_CurPos);
if (m_Selections == 1) {
for (int i = 0; i < GetItemCount(); i++) {
if (IsSelected(i))
return(i);
}
}
return(-1);
}
void CFormListView::SetSelect(int Pos, bool State)
{
if (!IsUndoInProgress())
NotifyUndoableEdit(UCODE_SELECT, UE_COALESCE | UE_INSIGNIFICANT);
if (m_Item[Pos]->IsSelected() != State)
m_Selections += State ? 1 : -1;
m_Item[Pos]->SelectItem(State);
}
void CFormListView::ToggleSelect(int Pos)
{
SetSelect(Pos, !m_Item[Pos]->IsSelected());
}
void CFormListView::SelectAll()
{
for (int i = 0; i < GetItemCount(); i++)
SetSelect(i, TRUE);
}
void CFormListView::Deselect()
{
if (!m_Selections)
return; // nothing to do
if (!IsUndoInProgress())
NotifyUndoableEdit(UCODE_SELECT, UE_COALESCE | UE_INSIGNIFICANT);
ClearSelection(); // doesn't send undo notifications
}
void CFormListView::ClearSelection()
{
for (int i = 0; i < GetItemCount(); i++)
m_Item[i]->SelectItem(FALSE);
m_Selections = 0;
}
void CFormListView::SaveSelection()
{
for (int i = 0; i < GetItemCount(); i++)
m_Item[i]->SetWasSel(m_Item[i]->IsSelected());
if (!m_Selections && IsValidPos(m_CurPos))
m_Item[m_CurPos]->SetWasSel(TRUE);
}
int CFormListView::PointToItemIdx(CPoint Point)
{
if (!GetItemCount())
return(-1);
CRect wr, cr;
GetClientRect(wr);
m_Item[0]->GetWindowRect(cr);
ScreenToClient(cr);
Point += GetScrollPosition();
return(Point.y / cr.Height());
}
inline void CFormListView::Sort(int& a, int& b)
{
if (a > b) {
int t = a;
a = b;
b = t;
}
}
void CFormListView::OnSelect(CFormListItemDlg *Item, int Flags)
{
if (Flags & MK_SHIFT && m_Selections) {
// sort previous block boundaries
int PrevBeg = m_BlockBegin;
int PrevEnd = m_BlockEnd;
Sort(PrevBeg, PrevEnd);
// sort new block boundaries
int NewBeg = m_BlockBegin;
int NewEnd = Item->GetIndex();
Sort(NewBeg, NewEnd);
// unselect any portion of previous block that's outside new block
int i;
for (i = PrevBeg; i <= PrevEnd; i++) {
if (i < NewBeg || i > NewEnd)
SetSelect(i, FALSE);
}
// select new block
for (i = NewBeg; i <= NewEnd; i++)
SetSelect(i, TRUE);
// store new end of block position
m_BlockEnd = Item->GetIndex();
} else {
int Pos = Item->GetIndex();
SetSelect(Pos, !Item->IsSelected());
m_BlockBegin = m_BlockEnd = Pos;
// capture mouse for drag select
SetCapture(); // released by OnLButtonUp
m_SelCury = m_SelOrgy = Pos;
}
}
void CFormListView::DragSelect(const CPoint& Cursor)
{
if (m_SelOrgy < 0)
return;
AutoScroll(Cursor); // if cursor is outside of view, scroll view
// extend selection to include item cursor is over
int y = min(max(PointToItemIdx(Cursor), 0), GetItemCount() - 1);
if (y < m_SelOrgy)
y--;
while (y > m_SelCury) {
m_SelCury++;
if (m_SelCury != m_SelOrgy)
ToggleSelect(m_SelCury);
}
while (y < m_SelCury) {
if (m_SelCury != m_SelOrgy)
ToggleSelect(m_SelCury);
m_SelCury--;
}
}
void CFormListView::AutoScroll(const CPoint& Cursor)
{
CRect r;
GetClientRect(r);
CSize vs = GetTotalSize();
// if cursor is outside of view, enable auto-scroll
if (vs.cy > r.Height()) { // if view is scrollable
if (Cursor.y < r.top)
m_ScrollDelta = Cursor.y - r.top;
else if (Cursor.y > r.bottom)
m_ScrollDelta = Cursor.y - r.bottom;
else
m_ScrollDelta = 0;
} else
m_ScrollDelta = 0;
if (m_ScrollDelta && !m_ScrollTimer)
m_ScrollTimer = SetTimer(1, SCROLL_DELAY, NULL);
}
/////////////////////////////////////////////////////////////////////////////
// CFormListView item dragging
void CFormListView::BeginDrag(CFormListItemDlg *Item, const CRect& Bounds)
{
m_DragPos = Item->GetIndex();
m_DragRect = Bounds;
Item->MapWindowPoints(this, m_DragRect);
SetCapture();
}
void CFormListView::ContinueDrag(const CPoint& Cursor)
{
if (m_DragPos < 0)
return;
if (!m_Dragging) {
// if cursor moved outside drag detection bounds, start dragging
if (Cursor.y < m_DragRect.top || Cursor.y > m_DragRect.bottom)
m_Dragging = TRUE;
}
if (m_Dragging) {
CRect r;
GetClientRect(r);
int ResID;
if (r.PtInRect(Cursor)) // if within view, show drag cursor
ResID = GetSelCount() ? IDC_DRAG_MULTI : IDC_DRAG_SINGLE;
else
ResID = AFX_IDC_NODROPCRSR; // show no drop cursor
SetCursor(AfxGetApp()->LoadCursor(ResID));
AutoScroll(Cursor); // if cursor is outside of view, scroll view
}
}
void CFormListView::EndDrag(const CPoint *Cursor)
{
if (m_Dragging) { // if dragging items
m_Dragging = FALSE;
ReleaseCapture(); // release mouse
if (Cursor != NULL) { // if drag wasn't aborted
CRect r;
GetClientRect(r);
if (r.PtInRect(*Cursor)) { // if cursor still within view
int DropPos = PointToItemIdx(*Cursor);
if (DropPos != m_DragPos) // if moved from starting position
DropItems(DropPos);
}
}
}
m_DragPos = -1; // reset drag detect state
}
void CFormListView::DropItems(int Pos)
{
Pos = min(max(Pos, 0), GetItemCount()); // clamp insert position
int sels = max(GetSelCount(), 1);
CFormListItemDlg **ip = new CFormListItemDlg *[sels];
if (GetSelCount()) { // if selection, drop selected items
int j = 0;
for (int i = 0; i < GetItemCount(); i++) {
if (IsSelected(i)) {
ip[j++] = m_Item[i]; // copy item pointer to list
m_Item[i] = NULL; // mark element for deletion
}
}
} else { // drop current item
ip[0] = m_Item[m_CurPos]; // copy item pointer to list
m_Item[m_CurPos] = NULL; // mark element for deletion
}
int i;
for (i = 0; i < sels; i++) // insert copied pointers
m_Item.InsertAt(Pos + i, ip[i]);
i = 0;
while (i < GetItemCount()) { // remove deleted elements
if (m_Item[i] == NULL)
m_Item.RemoveAt(i);
else
i++;
}
m_PrevSortKey = m_SortKey; // save current sort parameters for undo
m_PrevSortDir = m_SortDir;
m_SortKey = -1; // reset sort parameters
m_SortDir = 0;
m_Hdr.SetSort(m_SortKey, m_SortDir != 0);
NotifyUndoableEdit(UCODE_DRAG);
MoveSortedItems();
}
/////////////////////////////////////////////////////////////////////////////
// CFormListView clipboard
int CFormListView::Insert(int Pos, const void *Info, int Count, bool Move)
{
ASSERT(Pos >= 0 && Pos <= GetItemCount()); // pos can equal item count
CFormListItemDlg *CurItem;
if (IsValidPos(m_CurPos))
CurItem = m_Item[m_CurPos]; // save current position
else
CurItem = NULL;
// insert items and create them
m_Item.InsertAt(Pos, NULL, Count);
int i;
for (i = 0; i < Count; i++) {
m_Item[Pos + i] = CreateItem(Pos + i);
// if create failed, abort entire insert
if (m_Item[Pos + i] == NULL) {
// must clean up before displaying message, because AfxMessageBox
// allows timer hook to run, and null item pointer will crash it
for (int j = 0; j < i; j++)
DestroyItem(Pos + j);
m_Item.RemoveAt(Pos, Count);
Count = 0;
AfxMessageBox(LDS(FL_CANT_INSERT_ITEM));
break;
}
if (Info != NULL)
m_Item[Pos + i]->OpenItem(GetItemInfo(Info, i));
}
// set item info
if (Info != NULL) {
for (i = 0; i < Count; i++)
m_Item[Pos + i]->SetInfo(GetItemInfo(Info, i));
}
// clean up
if (Move)
MoveItems();
IndexItems();
SetCurPos(CurItem != NULL ? CurItem->GetIndex() : 0); // fix current position
return(Count); // number of items actually inserted
}
void CFormListView::Delete(int Pos, void *Info, int Count, bool Move)
{
ASSERT(IsValidPos(Pos));
int EndPos = Pos + Count;
ASSERT(Count >= 0 && EndPos <= GetItemCount());
// if current position will be deleted, try to find a safe position
int SafePos = m_CurPos;
if (m_CurPos >= Pos && m_CurPos < EndPos) {
if (EndPos < GetItemCount())
SafePos = EndPos; // after deletion
else {
if (Pos)
SafePos = Pos - 1; // before deletion
else
SafePos = -1; // no safe position exists
}
}
CFormListItemDlg *SafeItem = NULL; // pointer to safe item
if (SafePos >= 0)
SafeItem = m_Item[SafePos];
// destroy the items and remove them
for (int i = 0; i < Count; i++) {
// if item info was requested, retrieve it
if (Info != NULL)
m_Item[Pos + i]->GetInfo(GetItemInfo(Info, i));
DestroyItem(Pos + i);
}
m_Item.RemoveAt(Pos, Count);
// clean up
if (Move)
MoveItems();
IndexItems();
SetCurPos(SafeItem != NULL ? SafeItem->GetIndex() : 0); // fix current position
}
void CFormListView::Delete(bool Cut)
{
CWaitCursor wc;
if (!GetItemCount())
return; // nothing to do
NotifyUndoableEdit(Cut ? UCODE_CUT : UCODE_DELETE);
BYTE *cip;
int Items = max(m_Selections, 1); // either selections, or current item
if (Cut)
cip = new BYTE[Items * m_ItemInfoSize]; // allocate memory for items
// close items before deleting them to avoid slow painting
int j = 0;
for (int i = 0; i < GetItemCount(); i++) {
if (IsSelOrCur(i)) {
if (Cut)
m_Item[i]->GetInfo(GetItemInfo(cip, j++)); // save info before closing
m_Item[i]->CloseItem();
}
}
// save and clear selection
SaveSelection();
ClearSelection();
// delete selected items
int Pos = 0;
while (Pos < GetItemCount()) {
int i = Pos; // assume a block of contiguous selected items
while (i < GetItemCount() && m_Item[i]->GetWasSel())
i++;
int Len = i - Pos; // length of block
if (Len > 0)
Delete(Pos, NULL, Len, FALSE); // don't move items yet
else
Pos++;
}
// if cutting, write deleted items to clipboard
if (Cut) {
m_Clipboard.Write(cip, Items * m_ItemInfoSize);
delete [] cip; // free item memory
}
MoveItems(); // move items all at once, looks better
}
void CFormListView::Copy()
{
CWaitCursor wc;
if (!GetItemCount())
return; // nothing to do
BYTE *cip;
int Items = max(m_Selections, 1); // either selections, or current item
cip = new BYTE[Items * m_ItemInfoSize]; // allocate memory for items
// save selected items
int j = 0;
for (int i = 0; i < GetItemCount(); i++) {
if (IsSelOrCur(i))
m_Item[i]->GetInfo(GetItemInfo(cip, j++));
}
// write saved items to clipboard
m_Clipboard.Write(cip, Items * m_ItemInfoSize);
delete [] cip; // free item memory
ClearSelection();
}
void CFormListView::Cut()
{
Delete(TRUE); // copy deleted items to clipboard
}
void CFormListView::Paste()
{
GUID Owner;
m_PasteInfo = m_Clipboard.Read(m_PasteLen, &Owner); // read paste items
if (m_PasteInfo == NULL)
return;
int Pos = GetSingleSel();
if (Pos < 0)
return;
CWaitCursor wc;
ClearSelection();
SetCurPos(Pos); // undo assumes paste at current position
int Count = m_PasteLen / m_ItemInfoSize; // get item count from length
// determine if paste items came from this instance
bool Local = !memcmp(&Owner, &m_ClipboardOwner, sizeof(GUID));
OnPaste(m_PasteInfo, Count, Local);
NotifyUndoableEdit(UCODE_PASTE);
Insert(Pos, m_PasteInfo, Count); // and insert them
delete [] m_PasteInfo; // free paste items
}
void CFormListView::Insert()
{
if (!GetItemCount())
return; // nothing to do
NotifyUndoableEdit(UCODE_INSERT);
SaveSelection();
ClearSelection();
int Pos = 0;
while (Pos < GetItemCount()) {
int i = Pos; // assume a block of contiguous selected items
while (i < GetItemCount() && m_Item[i]->GetWasSel())
i++;
int Len = i - Pos; // length of block
if (Len > 0) {
Insert(Pos, NULL, Len);
Pos += Len * 2; // skip block and inserted items
} else
Pos++;
}
}
void CFormListView::Delete()
{
Delete(FALSE); // don't copy deleted items to clipboard
}
bool CFormListView::ClipboardHasData() const
{
return(m_Clipboard.HasData());
}
/////////////////////////////////////////////////////////////////////////////
// CFormListView header control
bool CFormListView::ResizeColumn(int Col, int Width)
{
ASSERT(Col >= 0 && Col < m_Columns);
if (!IsUndoInProgress())
NotifyUndoableEdit(UCODE_COLUMN_WIDTH, UE_COALESCE);
bool retc = m_Resizer.SetColumnWidth(Col, Width);
if (retc)
ResizeToFit();
return(retc);
}
void CFormListView::PopulateHeader()
{
if (m_ColDef != NULL) {
HDITEM hdi;
hdi.mask = HDI_TEXT | HDI_WIDTH | HDI_FORMAT;
hdi.fmt = HDF_STRING | HDF_CENTER;
CString Name;
for (int i = 0; i < m_Columns; i++) {
Name.LoadString(m_ColDef[i].NameID);
hdi.pszText = Name.GetBuffer(0);
hdi.cchTextMax = Name.GetLength();
hdi.cxy = m_ColDef[i].DefaultWidth;
m_Hdr.InsertItem(i, &hdi);
}
}
}
int CFormListView::GetColumnWidth(int Col) const
{
ASSERT(Col >= 0 && Col < m_Columns);
return(m_Resizer.GetColumnWidth(Col));
}
bool CFormListView::SetColumnWidth(int Col, int Width)
{
ASSERT(Col >= 0 && Col < m_Columns);
m_Hdr.SetWidth(Col, Width);
return(TRUE);
}
void CFormListView::ResizeHeader()
{
m_Hdr.OnViewSize();
}
/////////////////////////////////////////////////////////////////////////////
// CFormListView undo/redo
void *CFormListView::AllocUndoItemInfo(CUndoState& State, int Items) const
{
BYTE *uap = new BYTE[Items * m_ItemInfoSize];
State.SetDataPtr(uap, Items * m_ItemInfoSize);
UValItems(State) = Items;
return(uap);
}
CFormListInfo *CFormListView::GetUndoItemInfo(const CUndoState& State, int Item) const
{
return(GetItemInfo(State.GetData(), Item));
}
void CFormListView::SaveUndoState(CUndoState& State)
{
int Items = GetItemCount();
UValCurPos(State) = m_CurPos; // do first so handlers can override
switch (State.GetCode()) {
case UCODE_SELECT:
{
bool *uap;
AllocUndoArray(uap, State, Items);
for (int i = 0; i < Items; i++)
uap[i] = m_Item[i]->IsSelected();
}
break;
case UCODE_SORT:
case UCODE_DRAG:
{
if (State.IsEmpty()) { // if first time
// copy sorted indices
WORD *uap;
AllocUndoArray(uap, State, Items);
for (int i = 0; i < Items; i++)
uap[i] = m_Item[i]->GetIndex();
// save previous sort parameters
UValSortKey(State) = m_PrevSortKey;
UValSortDesc(State) = m_PrevSortDir < 0;
} else {
// invert sortedness of indices
WORD *Temp = new WORD[Items];
WORD *uap;
GetUndoArray(uap, State);
memcpy(Temp, uap, Items * sizeof(WORD));
for (int i = 0; i < Items; i++)
uap[Temp[i]] = i;
delete [] Temp;
// save current sort parameters
UValSortKey(State) = m_Hdr.GetSortCol();
UValSortDesc(State) = m_Hdr.GetSortDesc();
}
}
break;
case UCODE_CUT:
case UCODE_DELETE:
if (!UValFlags(State)) {
void *uap = AllocUndoItemInfo(State, max(m_Selections, 1));
int j = 0;
for (int i = 0; i < Items; i++) {
if (IsSelOrCur(i))
m_Item[i]->GetInfo(GetItemInfo(uap, j++));
}
UValFlags(State) = UFLAG_INS; // first restore will insert
if (m_Selections)
UValFlags(State) |= UFLAG_HADSEL;
} else
UValFlags(State) ^= UFLAG_DIR; // reverse direction
break;
case UCODE_PASTE:
if (!UValFlags(State)) {
int Count = m_PasteLen / m_ItemInfoSize;
void *uap = AllocUndoItemInfo(State, Count);
memcpy(uap, m_PasteInfo, m_PasteLen); // copy paste items
// assign new item indices, starting from insert position
for (int i = 0; i < Count; i++)
GetItemInfo(uap, i)->m_Index = m_CurPos + i;
UValFlags(State) = UFLAG_DEL; // first restore will delete
} else
UValFlags(State) ^= UFLAG_DIR; // reverse direction
break;
case UCODE_INSERT:
if (!UValFlags(State)) {
void *uap = AllocUndoItemInfo(State, max(m_Selections, 1));
int j = 0, Len = 0, Offset = 0;
for (int i = 0; i < Items; i++) {
if (IsSelOrCur(i)) {
// index must compensate for previous insertions
GetItemInfo(uap, j++)->m_Index = i + Offset;
Len++; // bump block length
} else { // end of block
Offset += Len; // increase compensation by block length
Len = 0; // reset block length
}
}
// specify empty flag; tells restore to insert empty items
UValFlags(State) = UFLAG_EMPTY | UFLAG_DEL; // first restore will delete
if (m_Selections)
UValFlags(State) |= UFLAG_HADSEL;
} else
UValFlags(State) ^= UFLAG_DIR; // reverse direction
break;
case UCODE_RAISE_ITEM_COUNT:
UValItems(State) = Items;
break;
case UCODE_LOWER_ITEM_COUNT:
if (!UValFlags(State)) {
int DeleteCount = Items - m_NewItemCount;
void *uap = AllocUndoItemInfo(State, DeleteCount);
for (int i = 0; i < DeleteCount; i++)
m_Item[m_NewItemCount + i]->GetInfo(GetItemInfo(uap, i));
UValFlags(State) = UFLAG_INS; // first restore will insert
} else
UValFlags(State) ^= UFLAG_DIR; // reverse direction
break;
case UCODE_COLUMN_WIDTH: