CtrlLib: FileSel: Improved logic of adding extension

This commit is contained in:
Mirek Fidler 2023-03-24 09:24:06 +01:00
parent 81d06c1b25
commit 9dea38484b
4 changed files with 59 additions and 1 deletions

View file

@ -1037,7 +1037,12 @@ void FileSel::AddName(Vector<String>& fn, String& f) {
if(f[0] == '\"' && f.GetCount() > 2)
f = f.Mid(1, f.GetCount() - 2);
int q = f.ReverseFind('.');
if(q < 0 || Filter(f.Mid(q + 1), // "(file.xxx)" should add extension too, allow just some
String typed_ext; // typed by user
if(q >= 0)
typed_ext = f.Mid(q + 1);
if(q < 0 || // no extension
force_ext && allowed_ext.Find(typed_ext) < 0 ||
Filter(f.Mid(q + 1), // "(file.xxx)" should add extension too, allow just some
[](int c) { return IsAlNum(c) || findarg(c, '_', '-') >= 0 ? 0 : c; }
).GetCount()) {
String t = GetMask();
@ -1991,6 +1996,31 @@ bool FileSel::Execute(int _mode) {
}
if(default_name.GetCount() && mode == SAVEAS)
file <<= default_name;
force_ext = true;
allowed_ext.Clear();
for(String mm : mask) {
if(!force_ext)
break;
for(String m : Split(mm, ' ')) {
if(!force_ext)
break;
int q = m.Find('.');
if(q < 0)
force_ext = false;
else {
m = m.Mid(q + 1);
if(m.Find('*') >= 0 || m.Find('?') >= 0)
force_ext = false;
else
allowed_ext.FindAdd(m);
}
}
}
DDUMP(force_ext);
DDUMP(allowed_ext);
FileUpdate();
Update();
int c = TopWindow::Run(appmodal);

View file

@ -229,6 +229,9 @@ protected:
Ctrl *file_ctrl = NULL;
int file_ctrl_cx;
bool force_ext = true; // -> false if there is wildcard mask allowing set of exts
Index<String> allowed_ext; // allowed extensions typed by user if force_ext
static StaticMutex li_mutex;
static void (*li_current)(const String& path, Image& result);
static String li_path;

View file

@ -0,0 +1,9 @@
uses
CtrlLib;
file
main.cpp;
mainconfig
"" = "GUI";

View file

@ -0,0 +1,16 @@
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
GUI_APP_MAIN
{
FileSel sel;
sel.ActiveDir(GetExeFolder());
sel.Type("files", "*.txt *.cpp");
sel.Type("files", "*.c");
sel.ExecuteSaveAs();
DDUMP(~sel);
sel.Type("all", "*.*");
sel.ExecuteSaveAs();
DDUMP(~sel);
}