forked from keizi666/charu3
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Charu3Tree.cpp
2245 lines (2007 loc) · 75.1 KB
/
Charu3Tree.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
/*----------------------------------------------------------
Charu3Treeクラス
2002/11/16 (c)Keizi
----------------------------------------------------------*/
#include "stdafx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <list>
#include <vector>
#include "Charu3Tree.h"
#include "search.h"
#include "text.h"
#include "Charu3.h"
#include "log.h"
#define ICON_FOLDER 0
#define ICON_RIREKI_FOLDER 2
#define ICON_ONETIME 4
#define ICON_KEY 6
#define ICON_DATE 8
#define ICON_EXE 10
#define ICON_RELATE 12
#define ICON_SELECT 14
#define ICON_CLIP 16
#define ICON_PLUG 18
#define ICON_KEYM 20
namespace {
//---------------------------------------------------
//関数名 getIconNumber(char cKind,char cIcon)
//機能 アイコン番号を取得
//---------------------------------------------------
int getIconNumber(char cKind, char cIcon)
{
int nRet = 0;
if (cKind & KIND_FOLDER) {
nRet = ICON_FOLDER;
}
else if (cKind & KIND_RIREKI) {
nRet = ICON_RIREKI_FOLDER;
}
else if (cKind & KIND_LOCK) {//ロック項目の場合
if (cIcon > 0) {
int nIcon = cIcon * 2;
nRet = ICON_KEY + nIcon;
}
else {
nRet = ICON_KEY;
}
}
else {//一時項目の場合
nRet = ICON_ONETIME;
}
return nRet;
}
//---------------------------------------------------
//関数名 data2TreeStruct(TV_INSERTSTRUCT& pTreeCtrlItem, list<STRING_DATA>::iterator pit)
//機能 ツリー構造体にデータを格納
//---------------------------------------------------
void data2TreeStruct(TV_INSERTSTRUCT& treeCtrlItem, std::list<STRING_DATA>::iterator it)
{
//ツリーデータ作成
treeCtrlItem.hInsertAfter = TVI_LAST;
treeCtrlItem.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
STRING_DATA data;
data.m_strTitle = it->m_strTitle;
//アイコンを設定
int nIcon = getIconNumber(it->m_cKind, it->m_cIcon);
treeCtrlItem.item.iImage = nIcon;
treeCtrlItem.item.iSelectedImage = nIcon + 1;
//名前を設定
treeCtrlItem.item.pszText = (TCHAR*)LPCTSTR(it->m_strTitle);
//データのアドレスを設定
treeCtrlItem.item.lParam = (LPARAM) & *it;
}
bool findKeywords(CString text, std::vector<CString> words)
{
if (theApp.m_ini.m_bSearchCaseInsensitive) {
text.MakeLower();
}
bool found = (theApp.m_ini.m_nSearchLogic == SEARCH_LOGIC_AND);
for (std::vector<CString>::iterator it = words.begin(); it != words.end(); it++) {
bool matched = (text.Find(*it) != -1);
if (!matched && theApp.m_ini.m_nSearchLogic == SEARCH_LOGIC_AND) {
found = false;
break;
}
if (matched && theApp.m_ini.m_nSearchLogic == SEARCH_LOGIC_OR) {
found = true;
break;
}
}
return found;
}
bool loadBitmapFile(CString strFileName, CBitmap* BitMap)
{
HBITMAP hBitmap;
BitMap->Detach();
hBitmap = (HBITMAP)::LoadImage(NULL, strFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (hBitmap == NULL) {
return false;
}
BitMap->Attach(hBitmap);
return true;
}
} // anonymous namespace
//---------------------------------------------------
//関数名 CCharu3Tree
//機能 コンストラクタ
//---------------------------------------------------
CCharu3Tree::CCharu3Tree()
: m_dragState(DragState::NOT_DRAGGING)
, m_nHoverTick(0)
, m_hDragItem(nullptr)
, m_hDragTarget(nullptr)
, m_hPrevTarget(nullptr)
, m_pDragImage(nullptr)
, m_ImageList(nullptr)
, m_pMaxID(nullptr)
, m_pSelectID(nullptr)
, m_pRecNumber(nullptr)
{
m_MyStringList.clear();
}
//---------------------------------------------------
//関数名 ~CCharu3Tree
//機能 デストラクタ
//---------------------------------------------------
CCharu3Tree::~CCharu3Tree()
{
if (m_ImageList) delete m_ImageList;
}
BEGIN_MESSAGE_MAP(CCharu3Tree, CTreeCtrl)
//{{AFX_MSG_MAP(CCharu3Tree)
ON_WM_MOUSEMOVE()
ON_NOTIFY_REFLECT(TVN_BEGINDRAG, OnBegindrag)
ON_WM_LBUTTONUP()
ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, OnItemexpanding)
ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
ON_WM_MOUSEWHEEL()
ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, OnItemexpanded)
ON_WM_WINDOWPOSCHANGING()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
HTREEITEM CCharu3Tree::AddFolder(HTREEITEM hTreeItem, CString strName)
{
if (theApp.m_ini.m_bDebug) {
LOG(_T("AddFolder %s"), strName.GetString());
}
STRING_DATA data;
data.m_cKind = KIND_FOLDER;
data.m_strTitle = strName;
HTREEITEM hNewItem = AddData(hTreeItem, data);
SelectItem(hNewItem);
return hNewItem;
}
HTREEITEM CCharu3Tree::AddData(HTREEITEM hTreeItem, STRING_DATA data, bool asChild /* = false */)
{
if (theApp.m_ini.m_bDebug) {
LOG(_T("AddData %s %s %d"), data.m_strTitle.GetString(), data.m_strData.GetString(), data.m_cKind);
}
data.m_nMyID = makeNewID();
data.m_nParentID = dataTree::ROOT;
if (hTreeItem) {
HTREEITEM hParentItem = asChild ? hTreeItem : GetParentItem(hTreeItem);
if (hParentItem) {
data.m_nParentID = getDataPtr(hParentItem)->m_nMyID;
}
}
if (data.m_strTitle == "") data.m_strTitle = makeTitle(data.m_strData);
time(&data.m_timeCreate);
time(&data.m_timeEdit);
std::list<STRING_DATA>::iterator it = m_MyStringList.insert(m_MyStringList.end(), data);//リストに追加
TV_INSERTSTRUCT treeCtrlItem;
data2TreeStruct(treeCtrlItem, it);
if (hTreeItem) {
if (asChild) {
treeCtrlItem.hParent = hTreeItem;
treeCtrlItem.hInsertAfter = TVI_FIRST;
}
else {
treeCtrlItem.hParent = GetParentItem(hTreeItem);
treeCtrlItem.hInsertAfter = hTreeItem;
}
}
else {
treeCtrlItem.hParent = TVI_ROOT;
treeCtrlItem.hInsertAfter = TVI_FIRST;
}
return InsertItem(&treeCtrlItem);
}
void CCharu3Tree::AddDataToHistoryFolders(STRING_DATA data)
{
int nSize = m_MyStringList.size(), nRirekiCount, nIsLock, i;
int nNumber = *m_pRecNumber;
STRING_DATA* parentData;
HTREEITEM hTreeItem, hStart = nullptr;
if (theApp.m_ini.m_bDebug) {
LOG(_T("addDataToRecordFolder"));
LOG(_T("hRootItem:%p TreeSize:%d"), GetRootItem(), nSize);
}
// hTreeItem = (HTREEITEM)::SendMessage(m_hWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
for (hTreeItem = GetRootItem(), i = 0; i < nSize && hTreeItem && hTreeItem != hStart; i++, hTreeItem = GetTrueNextItem(hTreeItem)) {
parentData = getDataPtr(hTreeItem);
hStart = GetRootItem();
if ((parentData->m_cKind & KIND_RIREKI)
&& (
(data.m_strData != m_strPreviousRecordedToHistory)
|| !(getDataOption(parentData->m_strMacro, _T("duplicationcheck")))
)
) {
if (*m_pRecNumber == nNumber) nNumber++;
if (theApp.m_ini.m_bDebug) {
LOG(_T("number:%d handle:%p title:%s ID:%d"), i, hTreeItem, parentData->m_strTitle.GetString(), parentData->m_nMyID);
}
nIsLock = getDataOption(parentData->m_strMacro, _T("lock"));//履歴種別を取得
if (!nIsLock) data.m_cKind = KIND_ONETIME;
else data.m_cKind = KIND_LOCK;
nRirekiCount = getDataOption(parentData->m_strMacro, _T("count"));//履歴数を取得
if (nRirekiCount <= 0) nRirekiCount = 2;
int nDeleteCount = nRirekiCount;
deleteExcessChildren(GetChildItem(hTreeItem), &nDeleteCount);//余計な子を消す
int nTitleLength = getDataOption(parentData->m_strMacro, _T("titlelen"));//タイトルの文字数
CString strTitle;
if (nTitleLength > 0 && nTitleLength < 256) {
strTitle = makeTitle(data.m_strData, nTitleLength);
}
else
strTitle = makeTitle(data.m_strData);
data.m_strTitle = strTitle;
if (getDataOption(parentData->m_strMacro, _T("number"))) {//番号をつけるかどうか
data.m_strTitle.Format(_T("%02d : %s"), *m_pRecNumber, strTitle.GetString());
if (nNumber > nRirekiCount) nNumber = 0;
}
// archiving
{
int archiveBy;
if (hasDataOption(parentData->m_strMacro, _T("archiveunit"))) {
archiveBy = getDataOption(parentData->m_strMacro, _T("archiveunit"));
}
else {
archiveBy = getDataOption(parentData->m_strMacro, _T("classhistory")); // for historical compatibility
}
if (archiveBy > 0) {
archiveHistory(hTreeItem, archiveBy);
}
}
if (theApp.m_ini.m_bDebug) {
LOG(_T("AddDataToHistoryFolders %s %s %d"), data.m_strTitle.GetString(), data.m_strData.GetString(), nNumber);
}
AddData(hTreeItem, data, true);//追加
nSize = m_MyStringList.size();
}
}
*m_pRecNumber = nNumber;
m_strPreviousRecordedToHistory = data.m_strData;
}
void CCharu3Tree::DeleteData(HTREEITEM hTreeItem)
{
STRING_DATA* dataPtr = getDataPtr(hTreeItem);
if (theApp.m_ini.m_bDebug) {
LOG(_T("DeleteData %s %d"), dataPtr->m_strTitle.GetString(), dataPtr->m_cKind);
}
DeleteChildren(hTreeItem);
std::list<STRING_DATA>::iterator it = findData(dataPtr);
m_MyStringList.erase(it);
DeleteItem(hTreeItem);
}
void CCharu3Tree::DeleteChildren(HTREEITEM hTreeItem)
{
HTREEITEM child = GetChildItem(hTreeItem);
while (child) {
HTREEITEM next= GetNextItem(child, TVGN_NEXT);
DeleteData(child);
child = next;
}
}
void CCharu3Tree::DeleteItem(HTREEITEM hTreeItem)
{
UncheckItem(hTreeItem);
CTreeCtrl::DeleteItem(hTreeItem);
}
//---------------------------------------------------
//関数名 GetTrueNextItem(HTREEITEM hTreeItem)
//機能 次のアイテムを取得(色々処理してます)
//---------------------------------------------------
HTREEITEM CCharu3Tree::GetTrueNextItem(HTREEITEM hTreeItem)
{
HTREEITEM hRetTreeItem, hParentItem;
if (!hTreeItem) { //ルートを設定
hRetTreeItem = GetRootItem();
}
else if (!GetChildItem(hTreeItem)) {//子供がいるか調べる
hRetTreeItem = GetNextSiblingItem(hTreeItem);//兄弟ハンドルを取得
if (!hRetTreeItem) {//兄弟がもういない時は親の兄弟
hParentItem = hTreeItem;
do {
hParentItem = GetParentItem(hParentItem);
if (!hParentItem) {
hRetTreeItem = nullptr;
break;
}
hRetTreeItem = GetNextSiblingItem(hParentItem);//兄弟ハンドルを取得
} while (hParentItem != GetRootItem() && !hRetTreeItem);
}
}
else {
hRetTreeItem = GetChildItem(hTreeItem);//子供のハンドルを取得
}
if (!hRetTreeItem) {
hRetTreeItem = GetRootItem();//再ループで必要なので戻る
}
return hRetTreeItem;
}
HTREEITEM CCharu3Tree::GetTruePrevItem(HTREEITEM hTreeItem)
{
HTREEITEM hRetTreeItem = nullptr;
if (hTreeItem) {
hRetTreeItem = GetPrevSiblingItem(hTreeItem);
if (!hRetTreeItem) {
HTREEITEM hParentItem = hTreeItem;
do {
hParentItem = GetParentItem(hParentItem);
if (!hParentItem) {
break;
}
hRetTreeItem = GetPrevSiblingItem(hParentItem);
} while (hParentItem != GetRootItem() && !hRetTreeItem);
}
}
if (!hRetTreeItem) {
hRetTreeItem = GetLastSiblingItem(GetRootItem());
}
if (hRetTreeItem) {
if (GetChildItem(hRetTreeItem)) {
hRetTreeItem = GetLastDescendantItem(hRetTreeItem);
}
}
return hRetTreeItem;
}
//---------------------------------------------------
//関数名 GetLastVisibleItem()
//機能 最後の可視アイテムを取得
//---------------------------------------------------
HTREEITEM CCharu3Tree::GetLastVisibleItem()
{
HTREEITEM hRetTreeItem;
HTREEITEM hTreeItemTmp = nullptr;
hRetTreeItem = GetRootItem();
while (hRetTreeItem) {
hTreeItemTmp = hRetTreeItem;
hRetTreeItem = GetNextVisibleItem(hRetTreeItem);//最後の可視アイテムを取得
}
if (hTreeItemTmp) hRetTreeItem = hTreeItemTmp;
return hRetTreeItem;
}
HTREEITEM CCharu3Tree::GetLastSiblingItem(HTREEITEM hTreeItem)
{
HTREEITEM next;
if (!hTreeItem) {
return nullptr;
}
while (next = GetNextSiblingItem(hTreeItem)) {
hTreeItem = next;
}
return hTreeItem;
}
HTREEITEM CCharu3Tree::GetLastDescendantItem(HTREEITEM hTreeItem)
{
HTREEITEM next;
if (!hTreeItem) {
return nullptr;
}
while (next = GetChildItem(hTreeItem)) {
hTreeItem = GetLastSiblingItem(next);
}
return hTreeItem;
}
//---------------------------------------------------
//関数名 HTREEITEM getFirstFolder(HTREEITEM hStartItem)
//機能 最初のフォルダを探す
//---------------------------------------------------
HTREEITEM CCharu3Tree::getFirstFolder(HTREEITEM hStartItem)
{
if (!hStartItem) return nullptr;
HTREEITEM hItem = hStartItem;
hItem = GetChildItem(hItem);
do { // TODO: Is it safe to use 'do'?
STRING_DATA* dataPtr = getDataPtr(hItem);
if (dataPtr->m_cKind & KIND_FOLDER_ALL) break;
hItem = GetNextItem(hItem, TVGN_NEXT);
} while (hItem);
return hItem;
}
//---------------------------------------------------
//関数名 HTREEITEM getLastChild(HTREEITEM hStartItem)
//機能 子アイテムの最後を探す
//---------------------------------------------------
HTREEITEM CCharu3Tree::getLastChild(HTREEITEM hStartItem)
{
if (!hStartItem) return nullptr;
HTREEITEM hItem = hStartItem, hPrevItem = nullptr;
hItem = GetChildItem(hItem);
do { // TODO: Is it safe to use 'do'?
STRING_DATA* dataPtr = getDataPtr(hItem);
if (dataPtr->m_cKind & KIND_DATA_ALL) hPrevItem = hItem;
hItem = GetNextItem(hItem, TVGN_NEXT);
} while (hItem);
return hPrevItem;
}
//---------------------------------------------------
//関数名 setPlugin(CString strPath)
//機能 プラグイン情報を設定する
//---------------------------------------------------
void CCharu3Tree::setPlugin(CString strPath)
{
m_rwPlugin.reserve(20);//とりあえず20個分確保
m_rwPlugin.clear();
CFileFind filefind;
BOOL more = filefind.FindFile(strPath + _T("\\*.dll"));
while (more) {
more = filefind.FindNextFile();
if (!filefind.IsDirectory()) {
HMODULE hDLL = LoadLibrary(filefind.GetFilePath());//DLLをロード
if (hDLL) {
//関数ポインタを取得
isUnicode pIsUnicode = (isUnicode)GetProcAddress(hDLL, "isUnicode");
#ifdef _UNICODE
if (pIsUnicode && pIsUnicode())
#else
if (pIsUnicode && !pIsUnicode())
#endif
{
readDataFile pReadData = (readDataFile)GetProcAddress(hDLL, "readDataFile");
initDLL pInitDLL = (initDLL)GetProcAddress(hDLL, "initDLL");
endDLL pEndDLL = (endDLL)GetProcAddress(hDLL, "endDLL");
writeDataFile pWriteData = (writeDataFile)GetProcAddress(hDLL, "writeDataFile");
getFormatName pGetFormatName = (getFormatName)GetProcAddress(hDLL, "getFormatName");
getExtension pGetExtension = (getExtension)GetProcAddress(hDLL, "getExtension");
if (pInitDLL && pEndDLL && pReadData && pWriteData && pGetFormatName && pGetExtension) {
TCHAR tcFormat[256], tcExt[256];
pGetExtension(tcExt, sizeof tcExt);
pGetFormatName(tcFormat, sizeof tcFormat);
RW_PLUGIN plugin(filefind.GetFilePath(), tcFormat, tcExt);
m_rwPlugin.push_back(plugin);
if (theApp.m_ini.m_bDebug) {
LOG(_T("plugin setting \"%s\""), plugin.m_strPluginFilePath.GetString());
}
}
}
FreeLibrary(hDLL);
}
}
}
}
//---------------------------------------------------
//関数名 setImageList(POINT posSize)
//機能 イメージリストを作る
//---------------------------------------------------
void CCharu3Tree::setImageList(POINT posSize, CString strFileName, CString strPath)
{
//アイコンのイメージリストを作る
if (m_ImageList == nullptr) {
::SetCurrentDirectory(strPath);
CBitmap Bitmap;
m_ImageList = new CImageList;
if (strFileName == "" || !loadBitmapFile(strFileName, &Bitmap))
Bitmap.LoadBitmap(IDB_ICON_BITMAP);//ビットマップをロード
BITMAP bitMap;
Bitmap.GetBitmap(&bitMap);
m_ImageList->Create(bitMap.bmHeight, bitMap.bmHeight, ILC_COLOR16 | ILC_MASK, 22, 2);
m_ImageList->Add(&Bitmap, RGB(0, 255, 0));
Bitmap.DeleteObject();
CTreeCtrl::SetImageList(m_ImageList, TVSIL_NORMAL);
}
}
//---------------------------------------------------
//関数名 checkMyChild(HTREEITEM hChildItem,int nMyID)
//機能 自分の子孫フォルダか調べる
//---------------------------------------------------
bool CCharu3Tree::checkMyChild(HTREEITEM hMeItem, HTREEITEM hChildItem)
{
bool isRet = false;
if (!hChildItem || !hMeItem) return isRet;
while (hChildItem) {
hChildItem = GetParentItem(hChildItem);//親を取得
if (hMeItem == hChildItem) {//自分の子孫でした
isRet = true;
break;
}
}
return isRet;
}
//---------------------------------------------------
//関数名 searchMyRoots(HTREEITEM hStartItem)
//機能 一番上の親を探す
//---------------------------------------------------
HTREEITEM CCharu3Tree::searchMyRoots(HTREEITEM hStartItem)
{
HTREEITEM hRet = nullptr, hTmp;
hTmp = hStartItem;
while (hTmp) {
hRet = hTmp;
hTmp = GetParentItem(hTmp);
if (hTmp) Expand(hTmp, TVE_COLLAPSE);
}
return hRet;
}
//---------------------------------------------------
//関数名 searchParentOption(HTREEITEM hStartItem,CString strOption)
//機能 親のオプションを捜していく
//---------------------------------------------------
HTREEITEM CCharu3Tree::searchParentOption(HTREEITEM hStartItem, CString strOption)
{
strOption.MakeLower();
HTREEITEM hRet = hStartItem;
while (hRet) {
if (hRet) {
STRING_DATA data = getData(hRet);
data.m_strMacro.MakeLower();
int nFound = data.m_strMacro.Find(strOption);
if (nFound >= 0) {
nFound = data.m_strMacro.Find(_T("="), nFound);
if (nFound >= 0) {
break;
}
}
}
hRet = GetParentItem(hRet);
}
return hRet;
}
//---------------------------------------------------
//関数名 saveDataToFile(CString strFileName,CString strPlugin)
//機能 データを保存する
//---------------------------------------------------
bool CCharu3Tree::saveDataToFile(CString strFileName, CString strPlugin, HTREEITEM hStartItem)
{
std::list<STRING_DATA> tmplist;
//全データを出力
if (!hStartItem) {
hStartItem = GetRootItem();
//保存データをリストに変換(データの順番はツリーで持っているため)
tree2List(hStartItem, &tmplist);
}
//エクスポートの場合
else {
STRING_DATA data = getData(hStartItem);
data.m_nParentID = dataTree::ROOT;
tmplist.insert(tmplist.end(), data);
if (ItemHasChildren(hStartItem)) tree2List(GetChildItem(hStartItem), &tmplist);
}
if (strPlugin != DAT_FORMAT && strPlugin != DAT_FORMAT2) {
return SaveDataWithPlugin(strFileName, strPlugin, &tmplist);
}
FILE* fFile = nullptr;
if (_tfopen_s(&fFile, strFileName, _T("wb")) != 0) {
return false;
}
if (nullptr == fFile) {
return false;
}
#ifdef _UNICODE
fwrite(DAT_FORMAT2, strlen(DAT_FORMAT2), 1, fFile);//データ識別子
#else
fwrite(DAT_FORMAT, strlen(DAT_FORMAT), 1, fFile);//データ識別子
#endif
for (std::list<STRING_DATA>::iterator it = tmplist.begin(); it != tmplist.end(); it++) {
if (it->m_cKind & KIND_ONETIME) {//ノーマル項目以外を保存
continue;
}
fwrite(&it->m_cKind, sizeof(it->m_cKind), 1, fFile);//データの種類
fwrite(&it->m_cIcon, sizeof(it->m_cIcon), 1, fFile);//アイコン種別
fwrite(&it->m_nMyID, sizeof(it->m_nMyID), 1, fFile);//自分のID
fwrite(&it->m_nParentID, sizeof(it->m_nParentID), 1, fFile);//親のID
fwrite(&it->m_timeCreate, sizeof(it->m_timeCreate), 1, fFile);//作成日時
fwrite(&it->m_timeEdit, sizeof(it->m_timeEdit), 1, fFile);//変更日時
//タイトル
int nDataSize = it->m_strTitle.GetLength();
#ifdef _UNICODE
nDataSize = nDataSize * sizeof(WCHAR);
#endif
fwrite(&nDataSize, sizeof(nDataSize), 1, fFile);
CString strTmp = it->m_strTitle;
fwrite(LPCTSTR(strTmp), nDataSize, 1, fFile);
//文章
nDataSize = it->m_strData.GetLength();
#ifdef _UNICODE
nDataSize = nDataSize * sizeof(WCHAR);
#endif
fwrite(&nDataSize, sizeof(nDataSize), 1, fFile);
fwrite(LPCTSTR(it->m_strData), nDataSize, 1, fFile);
//拡張用文字列データ
nDataSize = it->m_strMacro.GetLength();
#ifdef _UNICODE
nDataSize = nDataSize * sizeof(WCHAR);
#endif
fwrite(&nDataSize, sizeof(nDataSize), 1, fFile);
fwrite(LPCTSTR(it->m_strMacro), nDataSize, 1, fFile);
}
if (theApp.m_ini.m_bDebug) {
LOG(_T("wrriten charu3 data \"%s\""), strFileName.GetString());
}
fclose(fFile);
return true;
}
//---------------------------------------------------
//関数名 saveDataWithPlugin(CString strFileName,CString strPlugin)
//機能 プラグインを使ってデータを保存する
//---------------------------------------------------
bool CCharu3Tree::SaveDataWithPlugin(CString strFileName, CString strPlugin, std::list<STRING_DATA>* tmplist)
{
bool isRet = false;
RW_PLUGIN plugin;
if (!getPlugin(strPlugin, &plugin)) {
CString strRes;
(void)strRes.LoadString(APP_MES_NOT_FOUND_WRITEPLUGIN);
AfxMessageBox(strRes, MB_ICONEXCLAMATION, 0);
return isRet;
}
HMODULE hDLL;
writeDataFile pWriteData;
hDLL = LoadLibrary(plugin.m_strPluginFilePath);//DLLをロード
if (hDLL) {
//関数ポインタを取得
pWriteData = (writeDataFile)GetProcAddress(hDLL, "writeDataFile");
if (pWriteData) {
isRet = pWriteData(strFileName, tmplist);
}
FreeLibrary(hDLL);
}
if (theApp.m_ini.m_bDebug) {
LOG(_T("wrriten plugin data \"%s\" %s %d"), strFileName.GetString(), strPlugin.GetString(), isRet);
}
return isRet;
}
//---------------------------------------------------
//関数名 loadDataFileDef(CString strFileName,CString strPlugin)
//機能 デフォルトリストにデータを読み込む
//---------------------------------------------------
bool CCharu3Tree::loadDataFileDef(CString strFileName, CString strPlugin) {
DeleteAllItems();
bool isRet = loadDataFile(strFileName, strPlugin, &m_MyStringList);
CWnd::LockWindowUpdate();
copyData(dataTree::ROOT, TVI_ROOT, &m_MyStringList);//ツリーにデータをセット
CWnd::UnlockWindowUpdate();
return isRet;
}
//---------------------------------------------------
//関数名 loadDataFile(CString strFileName,CString strPlugin)
//機能 データを読み込む
//---------------------------------------------------
bool CCharu3Tree::loadDataFile(CString strFileName, CString strPlugin, std::list<STRING_DATA>* tmplist)
{
bool isRet = false;
if (strPlugin != DAT_FORMAT) {
//プラグインを使ってファイルから読み込む
return LoadDataWithPlugin(strFileName, strPlugin, tmplist);
}
STRING_DATA data;
int nDataSize;
FILE* fFile;
/*
識別子
データ種別 1byte
アイコン種別 1byte
ID 4byte
親のID 4byte
作成日時 4byte
タイトルサイズ 4byte
タイトル
本文サイズ 4byte
本文
マクロサイズ 4byte
マクロ
*/
//ファイルの拡張子を検査
char* szReadBuff;
char strMBCS[256];
wchar_t* szUniReadBuff;
int nVersion = 0;
CString strExtention;
strExtention = strFileName.Right(3);
strExtention.MakeLower();
std::list<STRING_DATA> readList;//データリスト
if (strExtention != DAT_EXT) return isRet;
if (_tfopen_s(&fFile, strFileName, _T("rb")) != 0) return isRet;//ファイルを開く
if (!fread(strMBCS, strlen(DAT_FORMAT), 1, fFile)) {
;//データ識別子を取得
fclose(fFile);
return isRet;
}
strMBCS[strlen(DAT_FORMAT)] = '\0';
if (strcmp(strMBCS, DAT_FORMAT) != 0) {
nVersion = 1;
if (strcmp(strMBCS, DAT_FORMAT2) != 0) {
fclose(fFile);
return isRet;
}
}
if (feof(fFile)) {
fclose(fFile);
return isRet;
}
readList.clear();
//仮リストに読み込み
while (!feof(fFile)) {
if (!fread(&data.m_cKind, sizeof(data.m_cKind), 1, fFile)) break;//データの種類を読み込み
if (!fread(&data.m_cIcon, sizeof(data.m_cIcon), 1, fFile)) break;//アイコン種別
if (!fread(&data.m_nMyID, sizeof(data.m_nMyID), 1, fFile)) break;//自分のIDを読み込み
if (!data.m_nMyID) data.m_nMyID = makeNewID();
if (!fread(&data.m_nParentID, sizeof(data.m_nParentID), 1, fFile)) break;//親のIDを読み込み
if (!fread(&data.m_timeCreate, sizeof(data.m_timeCreate), 1, fFile)) break;//作成日時を読み込み
if (!fread(&data.m_timeEdit, sizeof(data.m_timeEdit), 1, fFile)) break;//変更日時を読み込み
//文字列データ読み込み MBCS
if (nVersion == 0) {
//タイトルを読み込み
if (!fread(&nDataSize, sizeof(nDataSize), 1, fFile)) break;
szReadBuff = new char[nDataSize + 1];
fread(szReadBuff, nDataSize, 1, fFile);
szReadBuff[nDataSize] = '\0';
data.m_strTitle = szReadBuff;
delete[] szReadBuff;
//文章を読み込み
if (!fread(&nDataSize, sizeof(nDataSize), 1, fFile)) break;
szReadBuff = new char[nDataSize + 1];
fread(szReadBuff, nDataSize, 1, fFile);
szReadBuff[nDataSize] = '\0';
data.m_strData = szReadBuff;
delete[] szReadBuff;
//拡張用文字列データ
if (!fread(&nDataSize, sizeof(nDataSize), 1, fFile)) break;
szReadBuff = new char[nDataSize + 1];
fread(szReadBuff, nDataSize, 1, fFile);
szReadBuff[nDataSize] = '\0';
data.m_strMacro = szReadBuff;
delete[] szReadBuff;
}
//文字列データ読み込み Unicode
else if (nVersion == 1) {
//タイトルを読み込み
if (!fread(&nDataSize, sizeof(nDataSize), 1, fFile)) break;
szUniReadBuff = new wchar_t[nDataSize + 1];
fread(szUniReadBuff, nDataSize, 1, fFile);
szUniReadBuff[nDataSize / sizeof(WCHAR)] = '\0';
#ifdef _UNICODE
data.m_strTitle = szUniReadBuff;
#else
data.m_strTitle = Text::ConvertWcharToCString(szUniReadBuff);
#endif
delete[] szUniReadBuff;
//文章を読み込み
if (!fread(&nDataSize, sizeof(nDataSize), 1, fFile)) break;
szUniReadBuff = new wchar_t[nDataSize + 1];
fread(szUniReadBuff, nDataSize, 1, fFile);
szUniReadBuff[nDataSize / sizeof(WCHAR)] = '\0';
#ifdef _UNICODE
data.m_strData = szUniReadBuff;
#else
data.m_strData = Text::ConvertWcharToCString(szUniReadBuff);
#endif
delete[] szUniReadBuff;
//拡張用文字列データ
if (!fread(&nDataSize, sizeof(nDataSize), 1, fFile)) break;
szUniReadBuff = new wchar_t[nDataSize + 1];
fread(szUniReadBuff, nDataSize, 1, fFile);
szUniReadBuff[nDataSize / sizeof(WCHAR)] = '\0';
#ifdef _UNICODE
data.m_strMacro = szUniReadBuff;
#else
data.m_strMacro = Text::ConvertWcharToCString(szUniReadBuff);
#endif
delete[] szUniReadBuff;
}
if (data.m_cIcon < KIND_DEFAULT) data.m_cIcon = KIND_DEFAULT;
if (data.m_timeCreate < 0) data.m_timeCreate = 0;
if (data.m_timeEdit < data.m_timeCreate) data.m_timeEdit = data.m_timeCreate;
readList.insert(readList.end(), data);//リストに追加
}
//本リストに登録
tmplist->clear();
std::list<STRING_DATA>::iterator stit;
for (stit = readList.begin(); stit != readList.end(); stit++) {
data = *stit;
if (checkRedundancyID(data.m_nMyID)) {
int nOldID = data.m_nMyID;
data.m_nMyID = makeNewID();
std::list<STRING_DATA>::iterator newit;
for (newit = readList.begin(); newit != readList.end(); newit++) {
if (newit->m_nParentID == nOldID) newit->m_nParentID = data.m_nMyID;
}
}
tmplist->insert(tmplist->end(), data);//リストに追加
}
fclose(fFile);
if (theApp.m_ini.m_bDebug) {
LOG(_T("read charu3 data \"%s\" %d"), strFileName.GetString(), tmplist->size());
}
isRet = true;
return isRet;
}
//---------------------------------------------------
//関数名 getPlugin(CString strName,RW_PLUGIN* pPlugin)
//機能 プラグインを検索
//---------------------------------------------------
bool CCharu3Tree::getPlugin(CString strName, RW_PLUGIN* pPlugin)
{
for (std::vector<RW_PLUGIN>::iterator it = m_rwPlugin.begin(); it != m_rwPlugin.end(); it++) {
if (it->m_strFormatName == strName) {
*pPlugin = *it;
return true;
}
}
return false;
}
//---------------------------------------------------
//関数名 loadDataFilePlugin(CString strFileName,CString strPlugin)
//機能 プラグインでデータを読み込み
//---------------------------------------------------
bool CCharu3Tree::LoadDataWithPlugin(CString strFileName, CString strPlugin, std::list<STRING_DATA>* tmplist)
{
bool isRet = false;
RW_PLUGIN plugin;
if (!getPlugin(strPlugin, &plugin)) {
CString strRes;
(void)strRes.LoadString(APP_MES_NOT_FOUND_READPLUGIN);
AfxMessageBox(strRes, MB_ICONEXCLAMATION, 0);
return false;
}
HMODULE hDLL;
readDataFile pReadData;
initDLL pInitDLL;
endDLL pEndDLL;
time_t timeCreate; //作成日時
time(&timeCreate);
std::list<STRING_DATA>* pData;//読み込みリスト
hDLL = LoadLibrary(plugin.m_strPluginFilePath);//DLLをロード
if (hDLL) {
//関数ポインタを取得
pReadData = (readDataFile)GetProcAddress(hDLL, "readDataFile");
pInitDLL = (initDLL)GetProcAddress(hDLL, "initDLL");
pEndDLL = (endDLL)GetProcAddress(hDLL, "endDLL");
if (pInitDLL && pEndDLL && pReadData) {
pInitDLL();
pData = pReadData(strFileName, false);//読み込み関数を呼び出し
if (!pData) {
pEndDLL();
FreeLibrary(hDLL);
return isRet;
}
tmplist->clear();
TCHAR* szBuff;
STRING_DATA data, data2;
std::list<STRING_DATA> readList;//データリスト
std::list<STRING_DATA>::iterator stit;
//仮リストに登録
for (stit = pData->begin(); stit != pData->end(); stit++) {
if (stit->m_strTitle != "") {
size_t size = stit->m_strTitle.GetLength() + 1;
szBuff = new TCHAR[size];
_tcscpy_s(szBuff, size, LPCTSTR(stit->m_strTitle));
data.m_strTitle = szBuff;
delete[] szBuff;
}
else data.m_strTitle = "";
if (stit->m_strData != "") {
size_t size = stit->m_strData.GetLength() + 1;
szBuff = new TCHAR[size];
_tcscpy_s(szBuff, size, LPCTSTR(stit->m_strData));
data.m_strData = szBuff;
delete[] szBuff;
}
else data.m_strData = "";
if (stit->m_strMacro != "") {
size_t size = stit->m_strMacro.GetLength() + 1;
szBuff = new TCHAR[size];
_tcscpy_s(szBuff, size, LPCTSTR(stit->m_strMacro));
data.m_strMacro = szBuff;
delete[] szBuff;
}
else data.m_strMacro = "";