Skip to content

Commit

Permalink
FileFiltersDlg:
Browse files Browse the repository at this point in the history
- Embed the specified file name after "name:" in the filter file when creating a new file filter
- Select the file filter added to the list view after pressing the New or Install button.
  • Loading branch information
sdottaka committed Mar 17, 2022
1 parent 32a2be4 commit 6b85d42
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Filters/FileFilter.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## This is a directory/file filter template for WinMerge
name: Name of filter
name: ${name}
desc: Longer description

## Select if filter is inclusive or exclusive
Expand Down
30 changes: 29 additions & 1 deletion Src/FileFiltersDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "SharedFilterDlg.h"
#include "TestFilterDlg.h"
#include "FileOrFolderSelect.h"
#include "UniFile.h"

using std::vector;

Expand Down Expand Up @@ -146,6 +147,22 @@ void FileFiltersDlg::SelectFilterByIndex(int index)
m_listFilters.EnsureVisible(index, bPartialOk);
}

/**
* @brief Select filter by file path in the listview.
* @param [in] path file path
*/
void FileFiltersDlg::SelectFilterByFilePath(const String& path)
{
for (size_t i = 0; i < m_Filters.size(); ++i)
{
if (m_Filters[i].fullpath == path)
{
SelectFilterByIndex(static_cast<int>(i + 1));
break;
}
}
}

/**
* @brief Called before dialog is shown.
* @return Always TRUE.
Expand Down Expand Up @@ -437,14 +454,23 @@ void FileFiltersDlg::OnBnClickedFilterfileNewbutton()

// Open-dialog asks about overwriting, so we can overwrite filter file
// user has already allowed it.
if (!CopyFile(templatePath.c_str(), s.c_str(), FALSE))
UniMemFile fileIn;
UniStdioFile fileOut;
if (!fileIn.OpenReadOnly(templatePath) || !fileOut.OpenCreate(s))
{
String msg = strutils::format_string1(
_( "Cannot copy filter template file to filter folder:\n%1\n\nPlease make sure the folder exists and is writable."),
templatePath);
AfxMessageBox(msg.c_str(), MB_ICONERROR);
return;
}
String lines;
fileIn.ReadStringAll(lines);
strutils::replace(lines, _T("${name}"), file);
fileOut.WriteString(lines);
fileIn.Close();
fileOut.Close();

EditFileFilter(s);
FileFilterMgr *pMgr = pGlobalFileFilter->GetManager();
int retval = pMgr->AddFilter(s);
Expand All @@ -456,6 +482,7 @@ void FileFiltersDlg::OnBnClickedFilterfileNewbutton()
m_Filters = pGlobalFileFilter->GetFileFilters(selected);

UpdateFiltersList();
SelectFilterByFilePath(s);
}
}
}
Expand Down Expand Up @@ -580,6 +607,7 @@ void FileFiltersDlg::OnBnClickedFilterfileInstall()
m_Filters = pGlobalFileFilter->GetFileFilters(selected);

UpdateFiltersList();
SelectFilterByFilePath(userPath);
}
}
}
1 change: 1 addition & 0 deletions Src/FileFiltersDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class FileFiltersDlg : public CTrPropertyPage
protected:
void InitList();
void SelectFilterByIndex(int index);
void SelectFilterByFilePath(const String& path);
void AddToGrid(int filterIndex);
bool IsFilterItemNone(int item) const;
void UpdateFiltersList();
Expand Down

0 comments on commit 6b85d42

Please sign in to comment.