From cb8fd0c4381fb486de1f79daa3eb9515fbb41748 Mon Sep 17 00:00:00 2001 From: Don HO Date: Fri, 27 Mar 2020 13:05:54 +0100 Subject: [PATCH] TreeView refactoring Shift the allocation of TreeView item's "lParam" member from TreeView class to the derived class, so "lParam" could be the pointer of anything, not necessary anymore the string pointer. --- .../WinControls/FileBrowser/fileBrowser.cpp | 11 ++++- .../src/WinControls/FileBrowser/fileBrowser.h | 2 + .../FunctionList/functionListPanel.cpp | 46 ++++++++++++++++--- .../FunctionList/functionListPanel.h | 3 ++ .../WinControls/ProjectPanel/ProjectPanel.cpp | 28 +++++++++-- .../WinControls/ProjectPanel/ProjectPanel.h | 3 +- .../src/WinControls/ProjectPanel/TreeView.cpp | 43 +++-------------- .../src/WinControls/ProjectPanel/TreeView.h | 4 +- 8 files changed, 90 insertions(+), 50 deletions(-) diff --git a/PowerEditor/src/WinControls/FileBrowser/fileBrowser.cpp b/PowerEditor/src/WinControls/FileBrowser/fileBrowser.cpp index 2e72cb72..cdddcbbc 100644 --- a/PowerEditor/src/WinControls/FileBrowser/fileBrowser.cpp +++ b/PowerEditor/src/WinControls/FileBrowser/fileBrowser.cpp @@ -64,6 +64,11 @@ FileBrowser::~FileBrowser() folder->stopWatcher(); delete folder; } + + for (const auto s : rootPaths) + { + delete s; + } } vector split(const generic_string & string2split, TCHAR sep) @@ -1043,7 +1048,11 @@ HTREEITEM FileBrowser::createFolderItemsFromDirStruct(HTREEITEM hParentItem, con size_t len = lstrlen(rootPath); if (rootPath[len - 1] == '\\') rootPath[len - 1] = '\0'; - hFolderItem = _treeView.addItem(directoryStructure._name.c_str(), TVI_ROOT, INDEX_CLOSE_ROOT, rootPath); + + generic_string* rootPathStr = new generic_string(rootPath); + rootPaths.push_back(rootPathStr); + LPARAM lParamRootPath = reinterpret_cast(rootPathStr); + hFolderItem = _treeView.addItem(directoryStructure._name.c_str(), TVI_ROOT, INDEX_CLOSE_ROOT, lParamRootPath); } else { diff --git a/PowerEditor/src/WinControls/FileBrowser/fileBrowser.h b/PowerEditor/src/WinControls/FileBrowser/fileBrowser.h index d60cb60a..1010b6c9 100644 --- a/PowerEditor/src/WinControls/FileBrowser/fileBrowser.h +++ b/PowerEditor/src/WinControls/FileBrowser/fileBrowser.h @@ -168,6 +168,8 @@ protected: HMENU _hFileMenu = NULL; std::vector _folderUpdaters; + std::vector rootPaths; + void initPopupMenus(); void destroyMenus(); BOOL setImageList(int root_open_id, int root_close_id, int open_node_id, int closed_node_id, int leaf_id); diff --git a/PowerEditor/src/WinControls/FunctionList/functionListPanel.cpp b/PowerEditor/src/WinControls/FunctionList/functionListPanel.cpp index 32ea4781..56a2f748 100644 --- a/PowerEditor/src/WinControls/FunctionList/functionListPanel.cpp +++ b/PowerEditor/src/WinControls/FunctionList/functionListPanel.cpp @@ -41,6 +41,13 @@ using namespace std; #define INDEX_NODE 1 #define INDEX_LEAF 2 +FunctionListPanel::~FunctionListPanel() +{ + for (const auto s : posStrs) + { + delete s; + } +} void FunctionListPanel::addEntry(const TCHAR *nodeName, const TCHAR *displayText, size_t pos) { @@ -54,13 +61,21 @@ void FunctionListPanel::addEntry(const TCHAR *nodeName, const TCHAR *displayText itemParent = _treeView.searchSubItemByName(nodeName, root); if (!itemParent) { - itemParent = _treeView.addItem(nodeName, root, INDEX_NODE, TEXT("-1")); + generic_string* invalidValueStr = new generic_string(TEXT("-1")); + posStrs.push_back(invalidValueStr); + LPARAM lParamInvalidPosStr = reinterpret_cast(invalidValueStr); + + itemParent = _treeView.addItem(nodeName, root, INDEX_NODE, lParamInvalidPosStr); } } else itemParent = root; - _treeView.addItem(displayText, itemParent, INDEX_LEAF, posStr); + generic_string* posString = new generic_string(posStr); + posStrs.push_back(posString); + LPARAM lParamPosStr = reinterpret_cast(posString); + + _treeView.addItem(displayText, itemParent, INDEX_LEAF, lParamPosStr); } void FunctionListPanel::removeAllEntries() @@ -217,7 +232,12 @@ void FunctionListPanel::sortOrUnsort() _treeViewSearchResult.removeAllItems(); const TCHAR *fn = ((*_ppEditView)->getCurrentBuffer())->getFileName(); - _treeViewSearchResult.addItem(fn, NULL, INDEX_ROOT, TEXT("-1")); + + generic_string* invalidValueStr = new generic_string(TEXT("-1")); + posStrs.push_back(invalidValueStr); + LPARAM lParamInvalidPosStr = reinterpret_cast(invalidValueStr); + _treeViewSearchResult.addItem(fn, NULL, INDEX_ROOT, lParamInvalidPosStr); + _treeView.searchLeafAndBuildTree(_treeViewSearchResult, text2search, INDEX_LEAF); _treeViewSearchResult.display(true); _treeViewSearchResult.expand(_treeViewSearchResult.getRoot()); @@ -338,7 +358,11 @@ void FunctionListPanel::reload() bool parsedOK = _funcParserMgr.parse(_foundFuncInfos, AssociationInfo(-1, langID, ext, udln)); if (parsedOK) { - _treeView.addItem(fn, NULL, INDEX_ROOT, TEXT("-1")); + generic_string* invalidValueStr = new generic_string(TEXT("-1")); + posStrs.push_back(invalidValueStr); + LPARAM lParamInvalidPosStr = reinterpret_cast(invalidValueStr); + + _treeView.addItem(fn, NULL, INDEX_ROOT, lParamInvalidPosStr); } for (size_t i = 0, len = _foundFuncInfos.size(); i < len; ++i) @@ -352,7 +376,12 @@ void FunctionListPanel::reload() { currentBuf = (*_ppEditView)->getCurrentBuffer(); const TCHAR *fullFilePath = currentBuf->getFullPathName(); - _treeView.setItemParam(root, fullFilePath); + + generic_string* fullPathStr = new generic_string(fullFilePath); + posStrs.push_back(fullPathStr); + LPARAM lParamFullPathStr = reinterpret_cast(fullPathStr); + + _treeView.setItemParam(root, lParamFullPathStr); TreeParams *previousParams = getFromStateArray(fullFilePath); if (!previousParams) { @@ -615,7 +644,12 @@ void FunctionListPanel::searchFuncAndSwitchView() _treeViewSearchResult.removeAllItems(); const TCHAR *fn = ((*_ppEditView)->getCurrentBuffer())->getFileName(); - _treeViewSearchResult.addItem(fn, NULL, INDEX_ROOT, TEXT("-1")); + + generic_string* invalidValueStr = new generic_string(TEXT("-1")); + posStrs.push_back(invalidValueStr); + LPARAM lParamInvalidPosStr = reinterpret_cast(invalidValueStr); + _treeViewSearchResult.addItem(fn, NULL, INDEX_ROOT, lParamInvalidPosStr); + _treeView.searchLeafAndBuildTree(_treeViewSearchResult, text2search, INDEX_LEAF); _treeViewSearchResult.display(true); _treeViewSearchResult.expand(_treeViewSearchResult.getRoot()); diff --git a/PowerEditor/src/WinControls/FunctionList/functionListPanel.h b/PowerEditor/src/WinControls/FunctionList/functionListPanel.h index e5d852a2..4a340e5e 100644 --- a/PowerEditor/src/WinControls/FunctionList/functionListPanel.h +++ b/PowerEditor/src/WinControls/FunctionList/functionListPanel.h @@ -85,6 +85,7 @@ class FunctionListPanel : public DockingDlgInterface { public: FunctionListPanel(): DockingDlgInterface(IDD_FUNCLIST_PANEL), _ppEditView(NULL), _pTreeView(&_treeView), _reloadTipStr(TEXT("Reload")), _sortTipStr(TEXT("Sort")) {}; + ~FunctionListPanel(); void init(HINSTANCE hInst, HWND hPere, ScintillaEditView **ppEditView); @@ -134,6 +135,8 @@ private: std::vector _foundFuncInfos; + std::vector posStrs; + ScintillaEditView **_ppEditView; FunctionParsersManager _funcParserMgr; std::vector< std::pair > _skipZones; diff --git a/PowerEditor/src/WinControls/ProjectPanel/ProjectPanel.cpp b/PowerEditor/src/WinControls/ProjectPanel/ProjectPanel.cpp index edce6eb2..4baff2fb 100644 --- a/PowerEditor/src/WinControls/ProjectPanel/ProjectPanel.cpp +++ b/PowerEditor/src/WinControls/ProjectPanel/ProjectPanel.cpp @@ -45,6 +45,14 @@ #define INDEX_LEAF 5 #define INDEX_LEAF_INVALID 6 +ProjectPanel::~ProjectPanel() +{ + for (const auto s : fullPathStrs) + { + delete s; + } +} + INT_PTR CALLBACK ProjectPanel::run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam) { switch (message) @@ -530,7 +538,12 @@ bool ProjectPanel::buildTreeFrom(TiXmlNode *projectRoot, HTREEITEM hParentItem) generic_string fullPath = getAbsoluteFilePath(strValue); TCHAR *strValueLabel = ::PathFindFileName(strValue); int iImage = ::PathFileExists(fullPath.c_str())?INDEX_LEAF:INDEX_LEAF_INVALID; - _treeView.addItem(strValueLabel, hParentItem, iImage, fullPath.c_str()); + + generic_string* fullPathStr = new generic_string(fullPath); + fullPathStrs.push_back(fullPathStr); + LPARAM lParamFullPathStr = reinterpret_cast(fullPathStr); + + _treeView.addItem(strValueLabel, hParentItem, iImage, lParamFullPathStr); } } return true; @@ -1190,7 +1203,12 @@ void ProjectPanel::addFiles(HTREEITEM hTreeItem) for (size_t i = 0 ; i < sz ; ++i) { TCHAR *strValueLabel = ::PathFindFileName(pfns->at(i).c_str()); - _treeView.addItem(strValueLabel, hTreeItem, INDEX_LEAF, pfns->at(i).c_str()); + + generic_string* pathFileStr = new generic_string(pfns->at(i)); + fullPathStrs.push_back(pathFileStr); + LPARAM lParamPathFileStr = reinterpret_cast(pathFileStr); + + _treeView.addItem(strValueLabel, hTreeItem, INDEX_LEAF, lParamPathFileStr); } _treeView.expand(hTreeItem); setWorkSpaceDirty(true); @@ -1247,7 +1265,11 @@ void ProjectPanel::recursiveAddFilesFrom(const TCHAR *folderPath, HTREEITEM hTre if (folderPath[lstrlen(folderPath)-1] != '\\') pathFile += TEXT("\\"); pathFile += files[i]; - _treeView.addItem(files[i].c_str(), hTreeItem, INDEX_LEAF, pathFile.c_str()); + + generic_string* pathFileStr = new generic_string(pathFile); + fullPathStrs.push_back(pathFileStr); + LPARAM lParamPathFileStr = reinterpret_cast(pathFileStr); + _treeView.addItem(files[i].c_str(), hTreeItem, INDEX_LEAF, lParamPathFileStr); } ::FindClose(hFile); diff --git a/PowerEditor/src/WinControls/ProjectPanel/ProjectPanel.h b/PowerEditor/src/WinControls/ProjectPanel/ProjectPanel.h index 930759f9..a31b998d 100644 --- a/PowerEditor/src/WinControls/ProjectPanel/ProjectPanel.h +++ b/PowerEditor/src/WinControls/ProjectPanel/ProjectPanel.h @@ -68,7 +68,7 @@ class FileDialog; class ProjectPanel : public DockingDlgInterface { public: ProjectPanel(): DockingDlgInterface(IDD_PROJECTPANEL) {}; - + ~ProjectPanel(); void init(HINSTANCE hInst, HWND hPere) { DockingDlgInterface::init(hInst, hPere); @@ -140,6 +140,7 @@ protected: generic_string getAbsoluteFilePath(const TCHAR * relativePath); void openSelectFile(); void setFileExtFilter(FileDialog & fDlg); + std::vector fullPathStrs; }; class FileRelocalizerDlg : public StaticDialog diff --git a/PowerEditor/src/WinControls/ProjectPanel/TreeView.cpp b/PowerEditor/src/WinControls/ProjectPanel/TreeView.cpp index dfb343af..55470a61 100644 --- a/PowerEditor/src/WinControls/ProjectPanel/TreeView.cpp +++ b/PowerEditor/src/WinControls/ProjectPanel/TreeView.cpp @@ -80,7 +80,7 @@ void TreeView::makeLabelEditable(bool toBeEnabled) } -bool TreeView::setItemParam(HTREEITEM Item2Set, const TCHAR *paramStr) +bool TreeView::setItemParam(HTREEITEM Item2Set, LPARAM param) { if (!Item2Set) return false; @@ -88,15 +88,8 @@ bool TreeView::setItemParam(HTREEITEM Item2Set, const TCHAR *paramStr) TVITEM tvItem; tvItem.hItem = Item2Set; tvItem.mask = TVIF_PARAM; - - SendMessage(_hSelf, TVM_GETITEM, 0, reinterpret_cast(&tvItem)); - - if (!tvItem.lParam) - tvItem.lParam = reinterpret_cast(new generic_string(paramStr)); - else - { - *((generic_string *)tvItem.lParam) = paramStr; - } + tvItem.lParam = param; + SendMessage(_hSelf, TVM_SETITEM, 0, reinterpret_cast(&tvItem)); return true; } @@ -143,7 +136,7 @@ bool TreeView::renameItem(HTREEITEM Item2Set, const TCHAR *newName) return true; } -HTREEITEM TreeView::addItem(const TCHAR *itemName, HTREEITEM hParentItem, int iImage, const TCHAR *filePath) +HTREEITEM TreeView::addItem(const TCHAR *itemName, HTREEITEM hParentItem, int iImage, LPARAM lParam) { TVITEM tvi; tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM; @@ -156,8 +149,7 @@ HTREEITEM TreeView::addItem(const TCHAR *itemName, HTREEITEM hParentItem, int iI tvi.iImage = iImage;//isNode?INDEX_CLOSED_NODE:INDEX_LEAF; tvi.iSelectedImage = iImage;//isNode?INDEX_OPEN_NODE:INDEX_LEAF; - // Save the full path of file in the item's application-defined data area. - tvi.lParam = (filePath == NULL ? 0 : reinterpret_cast(new generic_string(filePath))); + tvi.lParam = lParam; TVINSERTSTRUCT tvInsertStruct; tvInsertStruct.item = tvi; @@ -177,8 +169,6 @@ void TreeView::removeItem(HTREEITEM hTreeItem) tvItem.hItem = hTreeItem; tvItem.mask = TVIF_PARAM; SendMessage(_hSelf, TVM_GETITEM, 0, reinterpret_cast(&tvItem)); - if (tvItem.lParam) - delete (generic_string *)(tvItem.lParam); // Remove the node TreeView_DeleteItem(_hSelf, hTreeItem); @@ -207,10 +197,6 @@ void TreeView::dupTree(HTREEITEM hTree2Dup, HTREEITEM hParentItem) tvItem.cchTextMax = MAX_PATH; tvItem.mask = TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE; SendMessage(_hSelf, TVM_GETITEM, 0, reinterpret_cast(&tvItem)); - if (tvItem.lParam) - { - tvItem.lParam = reinterpret_cast(new generic_string(*(reinterpret_cast(tvItem.lParam)))); - } TVINSERTSTRUCT tvInsertStruct; tvInsertStruct.item = tvItem; @@ -255,10 +241,7 @@ void TreeView::cleanSubEntries(HTREEITEM hTreeItem) tvItem.hItem = hItem; tvItem.mask = TVIF_PARAM; SendMessage(_hSelf, TVM_GETITEM, 0, reinterpret_cast(&tvItem)); - if (tvItem.lParam) - { - delete reinterpret_cast(tvItem.lParam); - } + cleanSubEntries(hItem); } } @@ -448,9 +431,6 @@ void TreeView::moveTreeViewItem(HTREEITEM draggedItem, HTREEITEM targetItem) tvDraggingItem.hItem = draggedItem; SendMessage(_hSelf, TVM_GETITEM, 0, reinterpret_cast(&tvDraggingItem)); - if (tvDraggingItem.lParam) - tvDraggingItem.lParam = reinterpret_cast(new generic_string(*(reinterpret_cast(tvDraggingItem.lParam)))); - TVINSERTSTRUCT tvInsertStruct; tvInsertStruct.item = tvDraggingItem; tvInsertStruct.hInsertAfter = (HTREEITEM)TVI_LAST; @@ -507,13 +487,6 @@ bool TreeView::swapTreeViewItem(HTREEITEM itemGoDown, HTREEITEM itemGoUp) SendMessage(_hSelf, TVM_GETITEM, 0, reinterpret_cast(&tvUpItem)); SendMessage(_hSelf, TVM_GETITEM, 0, reinterpret_cast(&tvDownItem)); - // make copy recursively for both items - - if (tvUpItem.lParam) - tvUpItem.lParam = reinterpret_cast(new generic_string(*(reinterpret_cast(tvUpItem.lParam)))); - if (tvDownItem.lParam) - tvDownItem.lParam = reinterpret_cast(new generic_string(*(reinterpret_cast(tvDownItem.lParam)))); - // add 2 new items TVINSERTSTRUCT tvInsertUp; tvInsertUp.item = tvUpItem; @@ -611,10 +584,6 @@ bool TreeView::searchLeafRecusivelyAndBuildTree(HTREEITEM tree2Build, const gene size_t res = itemNameUpperCase.find(text2SearchUpperCase); if (res != generic_string::npos) { - if (tvItem.lParam) - { - tvItem.lParam = reinterpret_cast(new generic_string(*(reinterpret_cast(tvItem.lParam)))); - } TVINSERTSTRUCT tvInsertStruct; tvInsertStruct.item = tvItem; tvInsertStruct.hInsertAfter = TVI_LAST; diff --git a/PowerEditor/src/WinControls/ProjectPanel/TreeView.h b/PowerEditor/src/WinControls/ProjectPanel/TreeView.h index e352a448..9561ed95 100644 --- a/PowerEditor/src/WinControls/ProjectPanel/TreeView.h +++ b/PowerEditor/src/WinControls/ProjectPanel/TreeView.h @@ -48,8 +48,8 @@ public: virtual void init(HINSTANCE hInst, HWND parent, int treeViewID); virtual void destroy(); - HTREEITEM addItem(const TCHAR *itemName, HTREEITEM hParentItem, int iImage, const TCHAR *filePath = NULL); - bool setItemParam(HTREEITEM Item2Set, const TCHAR *paramStr); + HTREEITEM addItem(const TCHAR *itemName, HTREEITEM hParentItem, int iImage, LPARAM lParam = NULL); + bool setItemParam(HTREEITEM Item2Set, LPARAM param); LPARAM getItemParam(HTREEITEM Item2Get) const; generic_string getItemDisplayName(HTREEITEM Item2Set) const; HTREEITEM searchSubItemByName(const TCHAR *itemName, HTREEITEM hParentItem);