[melonDS] file picker dialog filters

This commit is contained in:
StapleButter 2017-09-11 15:56:52 +02:00 committed by Mike Sinkovsky
parent 5d5cb6eb08
commit 4768cad824
4 changed files with 81 additions and 14 deletions

View File

@ -32,7 +32,7 @@ static char *runSavePanel(NSWindow *parent, NSSavePanel *s)
return filename;
}
char *uiOpenFile(uiWindow *parent)
char *uiOpenFile(uiWindow *parent, char* filter)
{
NSOpenPanel *o;
@ -46,7 +46,7 @@ char *uiOpenFile(uiWindow *parent)
return runSavePanel(windowWindow(parent), o);
}
char *uiSaveFile(uiWindow *parent)
char *uiSaveFile(uiWindow *parent, char* filter)
{
NSSavePanel *s;

4
ui.h
View File

@ -289,8 +289,8 @@ _UI_EXTERN uiMenuItem *uiMenuAppendAboutItem(uiMenu *m);
_UI_EXTERN void uiMenuAppendSeparator(uiMenu *m);
_UI_EXTERN uiMenu *uiNewMenu(const char *name);
_UI_EXTERN char *uiOpenFile(uiWindow *parent);
_UI_EXTERN char *uiSaveFile(uiWindow *parent);
_UI_EXTERN char *uiOpenFile(uiWindow *parent, char* filter);
_UI_EXTERN char *uiSaveFile(uiWindow *parent, char* filter);
_UI_EXTERN void uiMsgBox(uiWindow *parent, const char *title, const char *description);
_UI_EXTERN void uiMsgBoxError(uiWindow *parent, const char *title, const char *description);

View File

@ -6,18 +6,56 @@
#define windowWindow(w) ((w)?(GTK_WINDOW(uiControlHandle(uiControl(w)))):NULL)
static char *filedialog(GtkWindow *parent, GtkFileChooserAction mode, const gchar *confirm)
static char *filedialog(GtkWindow *parent, GtkFileChooserAction mode, const gchar *confirm, char* filter)
{
GtkWidget *fcd;
GtkFileChooser *fc;
gint response;
char *filename;
GtkFileFilter *filter;
gchar _filter[256];
gchar* fp = &_filter[0];
gchar* fname;
gchar *j;
int i;
int s;
fcd = gtk_file_chooser_dialog_new(NULL, parent, mode,
"_Cancel", GTK_RESPONSE_CANCEL,
confirm, GTK_RESPONSE_ACCEPT,
NULL);
fc = GTK_FILE_CHOOSER(fcd);
for (i = s = 0; i < 255; i++) {
if (filter[i] == '|' || filter[i] == '\0') {
_filter[i] = '\0';
if (s & 1) {
filter = gtk_file_filter_new();
gtk_file_filter_set_name(filter, fname);
for (j = fp; ; j++) {
if (*j == ';') {
*j = '\0';
gtk_file_filter_add_pattern(filter, fp);
fp = j + 1;
} else if (*j == '\0') {
gtk_file_filter_add_pattern(filter, fp);
break;
}
}
gtk_file_chooser_add_filter(fc, filter);
} else {
fname = fp;
}
fp = &_filter[i + 1];
s++;
if (s >= 8) break;
if (filter[i] == '\0') break;
} else {
_filter[i] = filter[i];
}
}
gtk_file_chooser_set_local_only(fc, FALSE);
gtk_file_chooser_set_select_multiple(fc, FALSE);
gtk_file_chooser_set_show_hidden(fc, TRUE);
@ -33,14 +71,14 @@ static char *filedialog(GtkWindow *parent, GtkFileChooserAction mode, const gcha
return filename;
}
char *uiOpenFile(uiWindow *parent)
char *uiOpenFile(uiWindow *parent, char* filter)
{
return filedialog(windowWindow(parent), GTK_FILE_CHOOSER_ACTION_OPEN, "_Open");
return filedialog(windowWindow(parent), GTK_FILE_CHOOSER_ACTION_OPEN, "_Open", filter);
}
char *uiSaveFile(uiWindow *parent)
char *uiSaveFile(uiWindow *parent, char* filter)
{
return filedialog(windowWindow(parent), GTK_FILE_CHOOSER_ACTION_SAVE, "_Save");
return filedialog(windowWindow(parent), GTK_FILE_CHOOSER_ACTION_SAVE, "_Save", filter);
}
static void msgbox(GtkWindow *parent, const char *title, const char *description, GtkMessageType type, GtkButtonsType buttons)

View File

@ -16,7 +16,7 @@
#define windowHWND(w) (w ? (HWND) uiControlHandle(uiControl(w)) : NULL)
char *commonItemDialog(HWND parent, REFCLSID clsid, REFIID iid, FILEOPENDIALOGOPTIONS optsadd)
char *commonItemDialog(HWND parent, REFCLSID clsid, REFIID iid, char* filter, FILEOPENDIALOGOPTIONS optsadd)
{
IFileDialog *d = NULL;
FILEOPENDIALOGOPTIONS opts;
@ -25,6 +25,13 @@ char *commonItemDialog(HWND parent, REFCLSID clsid, REFIID iid, FILEOPENDIALOGOP
char *name = NULL;
HRESULT hr;
COMDLG_FILTERSPEC filterspec[8];
wchar_t _filter[256];
wchar_t* fp = &_filter[0];
wchar_t* fname;
int i;
int s = 0;
hr = CoCreateInstance(clsid,
NULL, CLSCTX_INPROC_SERVER,
iid, (LPVOID *) (&d));
@ -46,6 +53,26 @@ char *commonItemDialog(HWND parent, REFCLSID clsid, REFIID iid, FILEOPENDIALOGOP
logHRESULT(L"error setting options", hr);
goto out;
}
for (i = s = 0; i < 255; i++) {
if (filter[i] == '|' || filter[i] == '\0') {
_filter[i] = '\0';
if (s & 1) {
filterspec[s>>1].pszName = fname;
filterspec[s>>1].pszSpec = fp;
} else {
fname = fp;
}
fp = &_filter[i + 1];
s++;
if (s >= 8) break;
if (filter[i] == '\0') break;
} else {
_filter[i] = filter[i];
}
}
d->SetFileTypes(s >> 1, filterspec);
hr = d->Show(parent);
if (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED))
// cancelled; return NULL like we have ready
@ -76,26 +103,28 @@ out:
return name;
}
char *uiOpenFile(uiWindow *parent)
char *uiOpenFile(uiWindow *parent, char* filter)
{
char *res;
disableAllWindowsExcept(parent);
res = commonItemDialog(windowHWND(parent),
CLSID_FileOpenDialog, IID_IFileOpenDialog,
FOS_NOCHANGEDIR | FOS_ALLNONSTORAGEITEMS | FOS_NOVALIDATE | FOS_PATHMUSTEXIST | FOS_FILEMUSTEXIST | FOS_SHAREAWARE | FOS_NOTESTFILECREATE | FOS_NODEREFERENCELINKS | FOS_FORCESHOWHIDDEN | FOS_DEFAULTNOMINIMODE);
filter,
FOS_NOCHANGEDIR | FOS_FORCEFILESYSTEM | FOS_NOVALIDATE | FOS_PATHMUSTEXIST | FOS_FILEMUSTEXIST | FOS_SHAREAWARE | FOS_NOTESTFILECREATE | FOS_FORCESHOWHIDDEN | FOS_DEFAULTNOMINIMODE);
enableAllWindowsExcept(parent);
return res;
}
char *uiSaveFile(uiWindow *parent)
char *uiSaveFile(uiWindow *parent, char* filter)
{
char *res;
disableAllWindowsExcept(parent);
res = commonItemDialog(windowHWND(parent),
CLSID_FileSaveDialog, IID_IFileSaveDialog,
FOS_OVERWRITEPROMPT | FOS_NOCHANGEDIR | FOS_ALLNONSTORAGEITEMS | FOS_NOVALIDATE | FOS_SHAREAWARE | FOS_NOTESTFILECREATE | FOS_NODEREFERENCELINKS | FOS_FORCESHOWHIDDEN | FOS_DEFAULTNOMINIMODE);
filter,
FOS_OVERWRITEPROMPT | FOS_NOCHANGEDIR | FOS_FORCEFILESYSTEM | FOS_NOVALIDATE | FOS_SHAREAWARE | FOS_NOTESTFILECREATE | FOS_FORCESHOWHIDDEN | FOS_DEFAULTNOMINIMODE);
enableAllWindowsExcept(parent);
return res;
}