forked from microsoft/mfcmapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHierarchyTableTreeCtrl.cpp
1255 lines (1077 loc) · 35.7 KB
/
HierarchyTableTreeCtrl.cpp
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
#include "stdafx.h"
#include "HierarchyTableTreeCtrl.h"
#include <Dialogs/BaseDialog.h>
#include <Dialogs/HierarchyTable/HierarchyTableDlg.h>
#include "MapiObjects.h"
#include "MAPIFunctions.h"
#include "UIFunctions.h"
#include "AdviseSink.h"
#include "SingleMAPIPropListCtrl.h"
#include "SortList/NodeData.h"
enum
{
htPR_DISPLAY_NAME,
htPR_ENTRYID,
htPR_INSTANCE_KEY,
htPR_SUBFOLDERS,
htPR_CONTAINER_FLAGS,
htNUMCOLS
};
static const SizedSPropTagArray(htNUMCOLS, sptHTCols) =
{
htNUMCOLS,
PR_DISPLAY_NAME,
PR_ENTRYID,
PR_INSTANCE_KEY,
PR_SUBFOLDERS,
PR_CONTAINER_FLAGS
};
enum
{
htcPR_CONTENT_COUNT,
htcPR_ASSOC_CONTENT_COUNT,
htcPR_DELETED_FOLDER_COUNT,
htcPR_DELETED_MSG_COUNT,
htcPR_DELETED_ASSOC_MSG_COUNT,
htcNUMCOLS
};
static const SizedSPropTagArray(htNUMCOLS, sptHTCountCols) =
{
htcNUMCOLS,
PR_CONTENT_COUNT,
PR_ASSOC_CONTENT_COUNT,
PR_DELETED_FOLDER_COUNT,
PR_DELETED_MSG_COUNT,
PR_DELETED_ASSOC_MSG_COUNT
};
static wstring CLASS = L"CHierarchyTableTreeCtrl";
CHierarchyTableTreeCtrl::CHierarchyTableTreeCtrl(
_In_ CWnd* pCreateParent,
_In_ CMapiObjects* lpMapiObjects,
_In_ CHierarchyTableDlg *lpHostDlg,
ULONG ulDisplayFlags,
UINT nIDContextMenu)
{
TRACE_CONSTRUCTOR(CLASS);
CRect pRect;
m_cRef = 1;
m_bShuttingDown = false;
// We borrow our parent's Mapi objects
m_lpMapiObjects = lpMapiObjects;
if (m_lpMapiObjects) m_lpMapiObjects->AddRef();
m_lpHostDlg = lpHostDlg;
if (m_lpHostDlg)
{
m_lpHostDlg->AddRef();
m_lpHostDlg->GetClientRect(pRect);
}
m_lpContainer = nullptr;
m_ulContainerType = NULL;
m_ulDisplayFlags = ulDisplayFlags;
m_bItemSelected = false;
m_nIDContextMenu = nIDContextMenu;
m_hItemCurHover = nullptr;
m_HoverButton = false;
Create(
TVS_HASBUTTONS
| TVS_LINESATROOT
| TVS_EDITLABELS
| TVS_DISABLEDRAGDROP
| TVS_SHOWSELALWAYS
| TVS_FULLROWSELECT
| WS_CHILD
| WS_TABSTOP
//| WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
| WS_VISIBLE,
pRect,
pCreateParent,
IDC_FOLDER_TREE);
TreeView_SetBkColor(m_hWnd, MyGetSysColor(cBackground));
TreeView_SetTextColor(m_hWnd, MyGetSysColor(cText));
::SendMessageA(m_hWnd, WM_SETFONT, reinterpret_cast<WPARAM>(GetSegoeFont()), false);
}
CHierarchyTableTreeCtrl::~CHierarchyTableTreeCtrl()
{
TRACE_DESTRUCTOR(CLASS);
m_bShuttingDown = true;
CWnd::DestroyWindow();
if (m_lpHostDlg) m_lpHostDlg->Release();
if (m_lpMapiObjects) m_lpMapiObjects->Release();
}
STDMETHODIMP_(ULONG) CHierarchyTableTreeCtrl::AddRef()
{
auto lCount = InterlockedIncrement(&m_cRef);
TRACE_ADDREF(CLASS, lCount);
return lCount;
}
STDMETHODIMP_(ULONG) CHierarchyTableTreeCtrl::Release()
{
auto lCount = InterlockedDecrement(&m_cRef);
TRACE_RELEASE(CLASS, lCount);
if (!lCount) delete this;
return lCount;
}
BEGIN_MESSAGE_MAP(CHierarchyTableTreeCtrl, CTreeCtrl)
ON_NOTIFY_REFLECT(TVN_GETDISPINFO, OnGetDispInfo)
ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelChanged)
ON_NOTIFY_REFLECT(TVN_ENDLABELEDIT, OnEndLabelEdit)
ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, OnItemExpanding)
ON_NOTIFY_REFLECT(TVN_DELETEITEM, OnDeleteItem)
ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
ON_NOTIFY_REFLECT(NM_RCLICK, OnRightClick)
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
ON_WM_KEYDOWN()
ON_WM_GETDLGCODE()
ON_WM_CONTEXTMENU()
ON_MESSAGE(WM_MFCMAPI_ADDITEM, msgOnAddItem)
ON_MESSAGE(WM_MFCMAPI_DELETEITEM, msgOnDeleteItem)
ON_MESSAGE(WM_MFCMAPI_MODIFYITEM, msgOnModifyItem)
ON_MESSAGE(WM_MFCMAPI_REFRESHTABLE, msgOnRefreshTable)
END_MESSAGE_MAP()
LRESULT CHierarchyTableTreeCtrl::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// Read the current hover local, since we need to clear it before we do any drawing
auto hItemCurHover = m_hItemCurHover;
switch (message)
{
case WM_MOUSEMOVE:
{
auto hRes = S_OK;
TVHITTESTINFO tvHitTestInfo = { 0 };
tvHitTestInfo.pt.x = GET_X_LPARAM(lParam);
tvHitTestInfo.pt.y = GET_Y_LPARAM(lParam);
WC_B(::SendMessage(m_hWnd, TVM_HITTEST, 0, reinterpret_cast<LPARAM>(&tvHitTestInfo)));
if (tvHitTestInfo.hItem)
{
if (tvHitTestInfo.flags & TVHT_ONITEMBUTTON)
{
m_HoverButton = true;
}
else
{
m_HoverButton = false;
}
// If this is a new glow, clean up the old glow and track for leaving the control
if (hItemCurHover != tvHitTestInfo.hItem)
{
DrawTreeItemGlow(m_hWnd, tvHitTestInfo.hItem);
if (hItemCurHover)
{
m_hItemCurHover = nullptr;
DrawTreeItemGlow(m_hWnd, hItemCurHover);
}
m_hItemCurHover = tvHitTestInfo.hItem;
TRACKMOUSEEVENT tmEvent = { 0 };
tmEvent.cbSize = sizeof(TRACKMOUSEEVENT);
tmEvent.dwFlags = TME_LEAVE;
tmEvent.hwndTrack = m_hWnd;
WC_B(TrackMouseEvent(&tmEvent));
}
}
else
{
if (hItemCurHover)
{
m_hItemCurHover = nullptr;
DrawTreeItemGlow(m_hWnd, hItemCurHover);
}
}
break;
}
case WM_MOUSELEAVE:
if (hItemCurHover)
{
m_hItemCurHover = nullptr;
DrawTreeItemGlow(m_hWnd, hItemCurHover);
}
return NULL;
}
return CTreeCtrl::WindowProc(message, wParam, lParam);
}
_Check_return_ HRESULT CHierarchyTableTreeCtrl::RefreshHierarchyTable()
{
auto hRes = S_OK;
// Turn off redraw while we work on the window
SetRedraw(false);
m_bItemSelected = false; // clear this just in case
EC_B(DeleteItem(GetRootItem()));
if (m_lpContainer)
EC_H(AddRootNode(m_lpContainer));
if (m_lpHostDlg) m_lpHostDlg->OnUpdateSingleMAPIPropListCtrl(nullptr, nullptr);
// Turn redraw back on to update our view
SetRedraw(true);
return hRes;
}
_Check_return_ HRESULT CHierarchyTableTreeCtrl::LoadHierarchyTable(_In_ LPMAPICONTAINER lpMAPIContainer)
{
auto hRes = S_OK;
CWaitCursor Wait; // Change the mouse to an hourglass while we work.
if (m_lpContainer) m_lpContainer->Release();
m_lpContainer = lpMAPIContainer;
m_ulContainerType = NULL;
// If we weren't passed a container to load, give up
if (!m_lpContainer) return hRes;
m_lpContainer->AddRef();
m_ulContainerType = GetMAPIObjectType(lpMAPIContainer);
WC_H(RefreshHierarchyTable());
if (MAPI_E_NOT_FOUND == hRes)
{
WARNHRESMSG(hRes, IDS_HIERARCHNOTFOUND);
}
else CHECKHRESMSG(hRes, IDS_REFRESHHIERARCHYFAILED);
return hRes;
}
// Removes any existing node data and replaces it with lpData
void SetNodeData(HWND hWnd, HTREEITEM hItem, SortListData* lpData)
{
if (lpData)
{
TVITEM tvItem = { 0 };
tvItem.hItem = hItem;
tvItem.mask = TVIF_PARAM;
if (TreeView_GetItem(hWnd, &tvItem) && tvItem.lParam)
{
DebugPrintEx(DBGHierarchy, CLASS, L"SetNodeData", L"Node %p, replacing data\n", hItem);
delete reinterpret_cast<SortListData*>(tvItem.lParam);
}
else
{
DebugPrintEx(DBGHierarchy, CLASS, L"SetNodeData", L"Node %p, first data\n", hItem);
}
tvItem.lParam = reinterpret_cast<LPARAM>(lpData);
TreeView_SetItem(hWnd, &tvItem);
// The tree now owns our lpData
}
}
_Check_return_ HRESULT CHierarchyTableTreeCtrl::AddRootNode(_In_ LPMAPICONTAINER lpMAPIContainer) const
{
auto hRes = S_OK;
LPSPropValue lpProps = nullptr;
LPSPropValue lpRootName = nullptr; // don't free
LPSBinary lpEIDBin = nullptr; // don't free
if (!m_hWnd) return S_OK;
if (!lpMAPIContainer) return MAPI_E_INVALID_PARAMETER;
CWaitCursor Wait; // Change the mouse to an hourglass while we work.
ULONG cVals = 0;
WC_H_GETPROPS(lpMAPIContainer->GetProps(
LPSPropTagArray(&sptHTCols),
fMapiUnicode,
&cVals,
&lpProps));
hRes = S_OK;
// Get the entry ID for the Root Container
if (!lpProps || PT_ERROR == PROP_TYPE(lpProps[htPR_ENTRYID].ulPropTag))
{
DebugPrint(DBGHierarchy, L"Could not find EntryID for Root Container. This is benign. Assuming NULL.\n");
}
else lpEIDBin = &lpProps[htPR_ENTRYID].Value.bin;
// Get the Display Name for the Root Container
if (!lpProps || PT_ERROR == PROP_TYPE(lpProps[htPR_DISPLAY_NAME].ulPropTag))
{
DebugPrint(DBGHierarchy, L"Could not find Display Name for Root Container. This is benign. Assuming NULL.\n");
}
else lpRootName = &lpProps[htPR_DISPLAY_NAME];
wstring szName;
// Shouldn't have to check lpRootName for non-NULL since CheckString does it, but prefast is complaining
if (lpRootName && CheckStringProp(lpRootName, PT_TSTRING))
{
szName = LPCTSTRToWstring(lpRootName->Value.LPSZ);
}
else
{
szName = loadstring(IDS_ROOTCONTAINER);
}
auto lpData = new SortListData();
if (lpData)
{
lpData->InitializeNode(
cVals,
lpProps, // pass our lpProps to be archived
lpEIDBin,
nullptr,
true, // Always assume root nodes have children so we always paint an expanding icon
lpProps ? lpProps[htPR_CONTAINER_FLAGS].Value.ul : MAPI_E_NOT_FOUND);
AddNode(
szName,
TVI_ROOT,
lpData,
true);
}
// Node owns the lpProps memory now, so we don't free it
return hRes;
}
void CHierarchyTableTreeCtrl::AddNode(
_In_ wstring szName,
HTREEITEM hParent,
SortListData* lpData,
bool bGetTable) const
{
DebugPrintEx(DBGHierarchy, CLASS, L"AddNode", L"Adding Node \"%ws\" under node %p, bGetTable = 0x%X\n", szName.c_str(), hParent, bGetTable);
TVINSERTSTRUCTW tvInsert = { nullptr };
tvInsert.hParent = hParent;
tvInsert.hInsertAfter = TVI_SORT;
tvInsert.item.mask = TVIF_CHILDREN | TVIF_TEXT;
tvInsert.item.cChildren = I_CHILDRENCALLBACK;
tvInsert.item.pszText = const_cast<LPWSTR>(szName.c_str());
auto hItem = reinterpret_cast<HTREEITEM>(::SendMessage(m_hWnd, TVM_INSERTITEMW, 0, reinterpret_cast<LPARAM>(&tvInsert)));
SetNodeData(m_hWnd, hItem, lpData);
if (bGetTable &&
(RegKeys[regkeyHIER_ROOT_NOTIFS].ulCurDWORD || hParent != TVI_ROOT))
{
(void)GetHierarchyTable(hItem, nullptr, true);
}
}
void CHierarchyTableTreeCtrl::AddNode(_In_ LPSRow lpsRow, HTREEITEM hParent, bool bGetTable) const
{
if (!lpsRow) return;
wstring szName;
auto lpName = PpropFindProp(
lpsRow->lpProps,
lpsRow->cValues,
PR_DISPLAY_NAME);
if (CheckStringProp(lpName, PT_TSTRING))
{
szName = LPCTSTRToWstring(lpName->Value.LPSZ);
}
else
{
szName = loadstring(IDS_UNKNOWNNAME);
}
DebugPrintEx(DBGHierarchy, CLASS, L"AddNode", L"Adding to %p: %ws\n", hParent, szName.c_str());
auto lpData = new SortListData();
if (lpData)
{
lpData->InitializeNode(lpsRow);
AddNode(
szName,
hParent,
lpData,
bGetTable);
}
}
_Check_return_ LPMAPITABLE CHierarchyTableTreeCtrl::GetHierarchyTable(HTREEITEM hItem, _In_opt_ LPMAPICONTAINER lpMAPIContainer, bool bRegNotifs) const
{
auto hRes = S_OK;
auto lpData = GetSortListData(hItem);
if (!lpData || !lpData->Node()) return nullptr;
if (!lpData->Node()->m_lpHierarchyTable)
{
if (lpMAPIContainer)
{
lpMAPIContainer->AddRef();
}
else
{
GetContainer(hItem, mfcmapiDO_NOT_REQUEST_MODIFY, &lpMAPIContainer);
}
if (lpMAPIContainer)
{
// Get the hierarchy table for the node and shove it into the data
LPMAPITABLE lpHierarchyTable = nullptr;
// on the AB, something about this call triggers table reloads on the parent hierarchy table
// no idea why they're triggered - doesn't happen for all AB providers
WC_MAPI(lpMAPIContainer->GetHierarchyTable(
(m_ulDisplayFlags & dfDeleted ? SHOW_SOFT_DELETES : NULL) | fMapiUnicode,
&lpHierarchyTable));
if (lpHierarchyTable)
{
EC_MAPI(lpHierarchyTable->SetColumns(
LPSPropTagArray(&sptHTCols),
TBL_BATCH));
}
lpData->Node()->m_lpHierarchyTable = lpHierarchyTable;
lpMAPIContainer->Release();
}
}
if (lpData->Node()->m_lpHierarchyTable && !lpData->Node()->m_lpAdviseSink)
{
// set up our advise sink
if (bRegNotifs &&
(RegKeys[regkeyHIER_ROOT_NOTIFS].ulCurDWORD || GetRootItem() != hItem))
{
DebugPrintEx(DBGNotify, CLASS, L"GetHierarchyTable", L"Advise sink for \"%ws\" = %p\n", LPCTSTRToWstring(GetItemText(hItem)).c_str(), hItem);
lpData->Node()->m_lpAdviseSink = new CAdviseSink(m_hWnd, hItem);
if (lpData->Node()->m_lpAdviseSink)
{
WC_MAPI(lpData->Node()->m_lpHierarchyTable->Advise(
fnevTableModified,
static_cast<IMAPIAdviseSink *>(lpData->Node()->m_lpAdviseSink),
&lpData->Node()->m_ulAdviseConnection));
if (MAPI_E_NO_SUPPORT == hRes) // Some tables don't support this!
{
if (lpData->Node()->m_lpAdviseSink) lpData->Node()->m_lpAdviseSink->Release();
lpData->Node()->m_lpAdviseSink = nullptr;
DebugPrint(DBGNotify, L"This table doesn't support notifications\n");
}
else if (S_OK == hRes)
{
auto lpMDB = m_lpMapiObjects->GetMDB(); // do not release
if (lpMDB)
{
lpData->Node()->m_lpAdviseSink->SetAdviseTarget(lpMDB);
LPSPropValue lpProp = nullptr;
// Try to trigger some RPC to get the notifications going
WC_MAPI(HrGetOneProp(
lpMDB,
PR_TEST_LINE_SPEED,
&lpProp));
// No need to check the error - we're not going to do anything with it
MAPIFreeBuffer(lpProp);
}
}
DebugPrintEx(DBGNotify, CLASS, L"GetHierarchyTable", L"Advise sink %p, ulAdviseConnection = 0x%08X\n", lpData->Node()->m_lpAdviseSink, static_cast<int>(lpData->Node()->m_ulAdviseConnection));
}
}
}
return lpData->Node()->m_lpHierarchyTable;
}
// Add the first level contents of lpMAPIContainer under the Parent node
_Check_return_ HRESULT CHierarchyTableTreeCtrl::ExpandNode(HTREEITEM hParent) const
{
LPSRowSet pRows = nullptr;
auto hRes = S_OK;
CWaitCursor Wait; // Change the mouse to an hourglass while we work.
if (!m_hWnd) return S_OK;
if (!hParent) return MAPI_E_INVALID_PARAMETER;
DebugPrintEx(DBGHierarchy, CLASS, L"ExpandNode", L"Expanding %p\n", hParent);
auto lpHierarchyTable = GetHierarchyTable(hParent, nullptr, 0 != RegKeys[regkeyHIER_EXPAND_NOTIFS].ulCurDWORD);
if (lpHierarchyTable)
{
// go to the first row
EC_MAPI(lpHierarchyTable->SeekRow(
BOOKMARK_BEGINNING,
0,
NULL));
hRes = S_OK; // don't let failure here fail the whole load
ULONG i = 0;
// get each row in turn and add it to the list
// TODO: Query several rows at once
if (!FAILED(hRes)) for (;;)
{
hRes = S_OK;
// Note - we're saving the rows off in AddNode, so we don't FreeProws this...we just MAPIFreeBuffer the array
if (pRows) MAPIFreeBuffer(pRows);
pRows = nullptr;
EC_MAPI(lpHierarchyTable->QueryRows(
1,
NULL,
&pRows));
if (FAILED(hRes) || !pRows || !pRows->cRows) break;
// Now we can process the row!
AddNode(
pRows->aRow,
hParent,
false);
i++;
}
}
// Note - we're saving the props off in AddNode, so we don't FreeProws this...we just MAPIFreeBuffer the array
if (pRows) MAPIFreeBuffer(pRows);
return hRes;
}
void CHierarchyTableTreeCtrl::OnGetDispInfo(_In_ NMHDR* pNMHDR, _In_ LRESULT* pResult)
{
NMTVDISPINFO* lpDispInfo = reinterpret_cast<LPNMTVDISPINFO>(pNMHDR);
if (lpDispInfo &&
lpDispInfo->item.mask & TVIF_CHILDREN)
{
auto lpData = reinterpret_cast<SortListData*>(lpDispInfo->item.lParam);
if (lpData && lpData->Node())
{
if (m_ulDisplayFlags & dfDeleted)
{
lpDispInfo->item.cChildren = 1;
}
else if (lpData->Node()->m_cSubfolders >= 0)
{
lpDispInfo->item.cChildren = lpData->Node()->m_cSubfolders ? 1 : 0;
}
else
{
LPCTSTR szName = nullptr;
if (PROP_TYPE(lpData->lpSourceProps[0].ulPropTag) == PT_TSTRING) szName = lpData->lpSourceProps[0].Value.LPSZ;
DebugPrintEx(DBGHierarchy, CLASS, L"OnGetDispInfo", L"Using Hierarchy table %d %p %ws\n", lpData->Node()->m_cSubfolders, lpData->Node()->m_lpHierarchyTable, LPCTSTRToWstring(szName).c_str());
// won't force the hierarchy table - just get it if we've already got it
auto lpHierarchyTable = lpData->Node()->m_lpHierarchyTable;
if (lpHierarchyTable)
{
lpDispInfo->item.cChildren = 1;
auto hRes = S_OK;
ULONG ulRowCount = NULL;
WC_MAPI(lpHierarchyTable->GetRowCount(
NULL,
&ulRowCount));
if (S_OK == hRes && !ulRowCount)
{
lpDispInfo->item.cChildren = 0;
}
}
}
}
}
*pResult = 0;
}
void CHierarchyTableTreeCtrl::UpdateSelectionUI(HTREEITEM hItem) const
{
auto hRes = S_OK;
LPMAPICONTAINER lpMAPIContainer = nullptr;
LPSPropValue lpProps = nullptr;
ULONG cVals = 0;
UINT uiMsg = IDS_STATUSTEXTNOFOLDER;
wstring szParam1;
wstring szParam2;
wstring szParam3;
DebugPrintEx(DBGHierarchy, CLASS, L"UpdateSelectionUI", L"%p\n", hItem);
if (!m_lpHostDlg) return;
// Have to request modify or this object is read only in the single prop control.
GetContainer(hItem, mfcmapiREQUEST_MODIFY, &lpMAPIContainer);
// make sure we've gotten the hierarchy table for this node
(void)GetHierarchyTable(hItem, lpMAPIContainer, 0 != RegKeys[regkeyHIER_EXPAND_NOTIFS].ulCurDWORD);
if (SUCCEEDED(hRes) && lpMAPIContainer)
{
// Get some props for status bar
WC_H_GETPROPS(lpMAPIContainer->GetProps(
LPSPropTagArray(&sptHTCountCols),
fMapiUnicode,
&cVals,
&lpProps));
if (lpProps)
{
if (!(m_ulDisplayFlags & dfDeleted))
{
uiMsg = IDS_STATUSTEXTCONTENTCOUNTS;
if (PT_ERROR == PROP_TYPE(lpProps[htcPR_CONTENT_COUNT].ulPropTag))
{
WARNHRESMSG(lpProps[htcPR_CONTENT_COUNT].Value.err, IDS_NODELACKSCONTENTCOUNT);
szParam1 = L"?"; // STRING_OK
}
else szParam1 = format(L"%u", lpProps[htcPR_CONTENT_COUNT].Value.ul);
if (PT_ERROR == PROP_TYPE(lpProps[htcPR_ASSOC_CONTENT_COUNT].ulPropTag))
{
WARNHRESMSG(lpProps[htcPR_ASSOC_CONTENT_COUNT].Value.err, IDS_NODELACKSASSOCCONTENTCOUNT);
szParam2 = L"?"; // STRING_OK
}
else szParam2 = format(L"%u", lpProps[htcPR_ASSOC_CONTENT_COUNT].Value.ul);
}
else
{
uiMsg = IDS_STATUSTEXTDELETEDCOUNTS;
if (PT_ERROR == PROP_TYPE(lpProps[htcPR_DELETED_MSG_COUNT].ulPropTag))
{
WARNHRESMSG(lpProps[htcPR_DELETED_MSG_COUNT].Value.err, IDS_NODELACKSDELETEDMESSAGECOUNT);
szParam1 = L"?"; // STRING_OK
}
else szParam1 = format(L"%u", lpProps[htcPR_DELETED_MSG_COUNT].Value.ul);
if (PT_ERROR == PROP_TYPE(lpProps[htcPR_DELETED_ASSOC_MSG_COUNT].ulPropTag))
{
WARNHRESMSG(lpProps[htcPR_DELETED_ASSOC_MSG_COUNT].Value.err, IDS_NODELACKSDELETEDASSOCMESSAGECOUNT);
szParam2 = L"?"; // STRING_OK
}
else szParam2 = format(L"%u", lpProps[htcPR_DELETED_ASSOC_MSG_COUNT].Value.ul);
if (PT_ERROR == PROP_TYPE(lpProps[htcPR_DELETED_FOLDER_COUNT].ulPropTag))
{
WARNHRESMSG(lpProps[htcPR_DELETED_FOLDER_COUNT].Value.err, IDS_NODELACKSDELETEDSUBFOLDERCOUNT);
szParam3 = L"?"; // STRING_OK
}
else szParam3 = format(L"%u", lpProps[htcPR_DELETED_FOLDER_COUNT].Value.ul);
}
MAPIFreeBuffer(lpProps);
}
}
m_lpHostDlg->UpdateStatusBarText(
STATUSDATA1,
uiMsg,
szParam1,
szParam2,
szParam3);
m_lpHostDlg->OnUpdateSingleMAPIPropListCtrl(lpMAPIContainer, GetSortListData(hItem));
auto selectedItem = LPCTSTRToWstring(GetItemText(GetSelectedItem()));
m_lpHostDlg->UpdateTitleBarText(selectedItem);
if (lpMAPIContainer) lpMAPIContainer->Release();
}
_Check_return_ bool CHierarchyTableTreeCtrl::IsItemSelected() const
{
return m_bItemSelected;
}
void CHierarchyTableTreeCtrl::OnSelChanged(_In_ NMHDR* pNMHDR, _In_ LRESULT* pResult)
{
LPNMTREEVIEW pNMTV = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
if (pNMTV && pNMTV->itemNew.hItem)
{
m_bItemSelected = true;
UpdateSelectionUI(pNMTV->itemNew.hItem);
}
else
{
m_bItemSelected = false;
}
*pResult = 0;
}
// This function will be called when we edit a node so we can attempt to commit the changes
void CHierarchyTableTreeCtrl::OnEndLabelEdit(_In_ NMHDR* pNMHDR, _In_ LRESULT* pResult)
{
auto hRes = S_OK;
LPMAPICONTAINER lpMAPIContainer = nullptr;
SPropValue sDisplayName;
TV_DISPINFO* pTVDispInfo = reinterpret_cast<TV_DISPINFO*>(pNMHDR);
*pResult = 0;
if (!pTVDispInfo || !pTVDispInfo->item.pszText) return;
GetContainer(pTVDispInfo->item.hItem, mfcmapiREQUEST_MODIFY, &lpMAPIContainer);
if (!lpMAPIContainer) return;
sDisplayName.ulPropTag = PR_DISPLAY_NAME;
sDisplayName.Value.LPSZ = pTVDispInfo->item.pszText;
EC_MAPI(HrSetOneProp(lpMAPIContainer, &sDisplayName));
lpMAPIContainer->Release();
}
void CHierarchyTableTreeCtrl::OnDblclk(_In_ NMHDR* /*pNMHDR*/, _In_ LRESULT* pResult)
{
// Due to problems with focus...we have to post instead of calling OnDisplayItem directly.
// Post the message to display the item
if (m_lpHostDlg)
m_lpHostDlg->PostMessage(WM_COMMAND, ID_DISPLAYSELECTEDITEM, NULL);
// Don't do default behavior for double-click (We only want '+' sign expansion.
// Double click should display the item, not expand the tree.)
*pResult = 1;
}
void CHierarchyTableTreeCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
DebugPrintEx(DBGMenu, CLASS, L"OnKeyDown", L"0x%X\n", nChar);
auto bCtrlPressed = GetKeyState(VK_CONTROL) < 0;
auto bShiftPressed = GetKeyState(VK_SHIFT) < 0;
auto bMenuPressed = GetKeyState(VK_MENU) < 0;
if (!bMenuPressed)
{
if (VK_RETURN == nChar && bCtrlPressed)
{
DebugPrintEx(DBGGeneric, CLASS, L"OnKeyDown", L"calling Display Associated Contents\n");
if (m_lpHostDlg)
m_lpHostDlg->PostMessage(WM_COMMAND, ID_DISPLAYASSOCIATEDCONTENTS, NULL);
}
else if (!m_lpHostDlg || !m_lpHostDlg->HandleKeyDown(nChar, bShiftPressed, bCtrlPressed, bMenuPressed))
{
CTreeCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
}
}
}
// Assert that we want all keyboard input (including ENTER!)
_Check_return_ UINT CHierarchyTableTreeCtrl::OnGetDlgCode()
{
auto iDlgCode = CTreeCtrl::OnGetDlgCode() | DLGC_WANTMESSAGE;
// to make sure that the control key is not pressed
if (GetKeyState(VK_CONTROL) >= 0 && m_hWnd == ::GetFocus())
{
// to make sure that the Tab key is pressed
if (GetKeyState(VK_TAB) < 0)
iDlgCode &= ~(DLGC_WANTALLKEYS | DLGC_WANTMESSAGE | DLGC_WANTTAB);
}
return iDlgCode;
}
void CHierarchyTableTreeCtrl::OnRightClick(_In_ NMHDR* /*pNMHDR*/, _In_ LRESULT* pResult)
{
// Send WM_CONTEXTMENU to self
(void)SendMessage(WM_CONTEXTMENU, reinterpret_cast<WPARAM>(m_hWnd), GetMessagePos());
// Mark message as handled and suppress default handling
*pResult = 1;
}
void CHierarchyTableTreeCtrl::OnContextMenu(_In_ CWnd* pWnd, CPoint pos)
{
if (pWnd && -1 == pos.x && -1 == pos.y)
{
// Find the highlighted item
auto item = GetSelectedItem();
if (item)
{
RECT rc = { 0 };
GetItemRect(item, &rc, true);
pos.x = rc.left;
pos.y = rc.top;
::ClientToScreen(pWnd->m_hWnd, &pos);
}
}
else
{
// Select the item that is at the point pos.
UINT uFlags = NULL;
auto ptTree = pos;
ScreenToClient(&ptTree);
auto hClickedItem = HitTest(ptTree, &uFlags);
if (hClickedItem != nullptr && TVHT_ONITEM & uFlags)
{
Select(hClickedItem, TVGN_CARET);
}
}
DisplayContextMenu(m_nIDContextMenu, IDR_MENU_HIERARCHY_TABLE, m_lpHostDlg->m_hWnd, pos.x, pos.y);
}
_Check_return_ SortListData* CHierarchyTableTreeCtrl::GetSelectedItemData() const
{
// Find the highlighted item
auto Item = GetSelectedItem();
if (Item)
{
return GetSortListData(Item);
}
return nullptr;
}
_Check_return_ SortListData* CHierarchyTableTreeCtrl::GetSortListData(HTREEITEM iItem) const
{
return reinterpret_cast<SortListData*>(GetItemData(iItem));
}
_Check_return_ LPSBinary CHierarchyTableTreeCtrl::GetSelectedItemEID() const
{
// Find the highlighted item
auto Item = GetSelectedItem();
// get the EID associated with it
if (Item)
{
auto lpData = GetSortListData(Item);
if (lpData && lpData->Node())
return lpData->Node()->m_lpEntryID;
}
return nullptr;
}
_Check_return_ LPMAPICONTAINER CHierarchyTableTreeCtrl::GetSelectedContainer(__mfcmapiModifyEnum bModify) const
{
LPMAPICONTAINER lpSelectedContainer = nullptr;
GetContainer(GetSelectedItem(), bModify, &lpSelectedContainer);
return lpSelectedContainer;
}
void CHierarchyTableTreeCtrl::GetContainer(
HTREEITEM Item,
__mfcmapiModifyEnum bModify,
_In_ LPMAPICONTAINER* lppContainer) const
{
auto hRes = S_OK;
ULONG ulObjType = 0;
SBinary NullBin = { 0 };
LPMAPICONTAINER lpContainer = nullptr;
*lppContainer = nullptr;
if (!Item) return;
DebugPrintEx(DBGGeneric, CLASS, L"GetContainer", L"HTREEITEM = %p, bModify = %d, m_ulContainerType = 0x%X\n", Item, bModify, m_ulContainerType);
auto lpData = GetSortListData(Item);
if (!lpData || !lpData->Node())
{
// We didn't get an entryID, so log it and get out of here
DebugPrintEx(DBGGeneric, CLASS, L"GetContainer", L"GetSortListData returned NULL or lpEntryID is NULL\n");
return;
}
auto ulFlags = mfcmapiREQUEST_MODIFY == bModify ? MAPI_MODIFY : NULL;
auto lpCurBin = lpData->Node()->m_lpEntryID;
if (!lpCurBin) lpCurBin = &NullBin;
// Check the type of the root container to know whether the MDB or AddrBook object is valid
// This also allows NULL EIDs to return the root container itself.
// Use the Root container if we can't decide and log an error
if (m_lpMapiObjects)
{
if (MAPI_ABCONT == m_ulContainerType)
{
auto lpAddrBook = m_lpMapiObjects->GetAddrBook(false); // do not release
if (lpAddrBook)
{
DebugPrint(DBGGeneric, L"\tCalling OpenEntry on address book with ulFlags = 0x%X\n", ulFlags);
WC_H(CallOpenEntry(
NULL,
lpAddrBook,
NULL,
NULL,
lpCurBin->cb,
reinterpret_cast<LPENTRYID>(lpCurBin->lpb),
NULL,
ulFlags,
&ulObjType,
reinterpret_cast<LPUNKNOWN*>(&lpContainer)));
}
}
else if (MAPI_FOLDER == m_ulContainerType)
{
auto lpMDB = m_lpMapiObjects->GetMDB(); // do not release
if (lpMDB)
{
ulFlags = (mfcmapiREQUEST_MODIFY == bModify ? MAPI_MODIFY : NULL) |
(m_ulDisplayFlags & dfDeleted ? SHOW_SOFT_DELETES | MAPI_NO_CACHE : NULL);
WC_H(CallOpenEntry(
lpMDB,
NULL,
NULL,
NULL,
lpCurBin->cb,
reinterpret_cast<LPENTRYID>(lpCurBin->lpb),
NULL,
ulFlags,
&ulObjType,
reinterpret_cast<LPUNKNOWN*>(&lpContainer)));
}
}
}
// If we didn't get a container above, try to open from m_lpContainer
if (!lpContainer && m_lpContainer)
{
WARNHRESMSG(MAPI_E_CALL_FAILED, IDS_UNKNOWNCONTAINERTYPE);
hRes = S_OK;
WC_H(CallOpenEntry(
NULL,
NULL,
m_lpContainer,
NULL,
lpCurBin->cb,
reinterpret_cast<LPENTRYID>(lpCurBin->lpb),
NULL,
ulFlags,
&ulObjType,
reinterpret_cast<LPUNKNOWN*>(&lpContainer)));
}
// if we failed because write access was denied, try again if acceptable
if (!lpContainer && FAILED(hRes) && mfcmapiREQUEST_MODIFY == bModify)
{
DebugPrint(DBGGeneric, L"\tOpenEntry failed: 0x%X. Will try again without MAPI_MODIFY\n", hRes);
// We failed to open the item with MAPI_MODIFY.
// Let's try to open it with NULL
GetContainer(
Item,
mfcmapiDO_NOT_REQUEST_MODIFY,
&lpContainer);
}
// Ok - we're just out of luck
if (!lpContainer)
{
WARNHRESMSG(hRes, IDS_NOCONTAINER);
hRes = MAPI_E_NOT_FOUND;
}
if (lpContainer) *lppContainer = lpContainer;
DebugPrintEx(DBGGeneric, CLASS, L"GetContainer", L"returning lpContainer = %p, ulObjType = 0x%X and hRes = 0x%X\n", lpContainer, ulObjType, hRes);
}
// When + is clicked, add all entries in the table as children
void CHierarchyTableTreeCtrl::OnItemExpanding(_In_ NMHDR* pNMHDR, _In_ LRESULT* pResult)
{
auto hRes = S_OK;
*pResult = 0;
NM_TREEVIEW* pNMTreeView = reinterpret_cast<NM_TREEVIEW*>(pNMHDR);
if (pNMTreeView)
{
DebugPrintEx(DBGHierarchy, CLASS, L"OnItemExpanding", L"Expanding item %p \"%ws\" action = 0x%08X state = 0x%08X\n", pNMTreeView->itemNew.hItem, LPCTSTRToWstring(GetItemText(pNMTreeView->itemOld.hItem)).c_str(), pNMTreeView->action, pNMTreeView->itemNew.state);
if (pNMTreeView->action & TVE_EXPAND)
{
if (!(pNMTreeView->itemNew.state & TVIS_EXPANDEDONCE))
{
EC_H(ExpandNode(pNMTreeView->itemNew.hItem));