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.
This commit is contained in:
Don HO 2020-03-27 13:05:54 +01:00
parent 13f0717a1b
commit cb8fd0c438
No known key found for this signature in database
GPG Key ID: 6C429F1D8D84F46E
8 changed files with 90 additions and 50 deletions

View File

@ -64,6 +64,11 @@ FileBrowser::~FileBrowser()
folder->stopWatcher();
delete folder;
}
for (const auto s : rootPaths)
{
delete s;
}
}
vector<generic_string> 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<LPARAM>(rootPathStr);
hFolderItem = _treeView.addItem(directoryStructure._name.c_str(), TVI_ROOT, INDEX_CLOSE_ROOT, lParamRootPath);
}
else
{

View File

@ -168,6 +168,8 @@ protected:
HMENU _hFileMenu = NULL;
std::vector<FolderUpdater *> _folderUpdaters;
std::vector<generic_string*> 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);

View File

@ -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<LPARAM>(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<LPARAM>(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<LPARAM>(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<LPARAM>(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<LPARAM>(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<LPARAM>(invalidValueStr);
_treeViewSearchResult.addItem(fn, NULL, INDEX_ROOT, lParamInvalidPosStr);
_treeView.searchLeafAndBuildTree(_treeViewSearchResult, text2search, INDEX_LEAF);
_treeViewSearchResult.display(true);
_treeViewSearchResult.expand(_treeViewSearchResult.getRoot());

View File

@ -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<foundInfo> _foundFuncInfos;
std::vector<generic_string*> posStrs;
ScintillaEditView **_ppEditView;
FunctionParsersManager _funcParserMgr;
std::vector< std::pair<int, int> > _skipZones;

View File

@ -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<LPARAM>(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<LPARAM>(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<LPARAM>(pathFileStr);
_treeView.addItem(files[i].c_str(), hTreeItem, INDEX_LEAF, lParamPathFileStr);
}
::FindClose(hFile);

View File

@ -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<generic_string*> fullPathStrs;
};
class FileRelocalizerDlg : public StaticDialog

View File

@ -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;
tvItem.lParam = param;
SendMessage(_hSelf, TVM_GETITEM, 0, reinterpret_cast<LPARAM>(&tvItem));
if (!tvItem.lParam)
tvItem.lParam = reinterpret_cast<LPARAM>(new generic_string(paramStr));
else
{
*((generic_string *)tvItem.lParam) = paramStr;
}
SendMessage(_hSelf, TVM_SETITEM, 0, reinterpret_cast<LPARAM>(&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<LPARAM>(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<LPARAM>(&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<LPARAM>(&tvItem));
if (tvItem.lParam)
{
tvItem.lParam = reinterpret_cast<LPARAM>(new generic_string(*(reinterpret_cast<generic_string *>(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<LPARAM>(&tvItem));
if (tvItem.lParam)
{
delete reinterpret_cast<generic_string *>(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<LPARAM>(&tvDraggingItem));
if (tvDraggingItem.lParam)
tvDraggingItem.lParam = reinterpret_cast<LPARAM>(new generic_string(*(reinterpret_cast<generic_string *>(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<LPARAM>(&tvUpItem));
SendMessage(_hSelf, TVM_GETITEM, 0, reinterpret_cast<LPARAM>(&tvDownItem));
// make copy recursively for both items
if (tvUpItem.lParam)
tvUpItem.lParam = reinterpret_cast<LPARAM>(new generic_string(*(reinterpret_cast<generic_string *>(tvUpItem.lParam))));
if (tvDownItem.lParam)
tvDownItem.lParam = reinterpret_cast<LPARAM>(new generic_string(*(reinterpret_cast<generic_string *>(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<LPARAM>(new generic_string(*(reinterpret_cast<generic_string *>(tvItem.lParam))));
}
TVINSERTSTRUCT tvInsertStruct;
tvInsertStruct.item = tvItem;
tvInsertStruct.hInsertAfter = TVI_LAST;

View File

@ -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);