libui: primp file picker dialogs

* support for filters, for Windows (tested) and GTK (theoretical)
* add extra parameter for setting the start directory, but not supported yet
This commit is contained in:
StapleButter 2017-09-11 15:56:52 +02:00
parent 08ed0f3229
commit 62a26977c7
5 changed files with 130 additions and 17 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, char* initpath)
{
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, char* initpath)
{
NSSavePanel *s;

View File

@ -265,8 +265,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, char* initpath);
_UI_EXTERN char *uiSaveFile(uiWindow *parent, char* filter, char* initpath);
_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,7 +6,7 @@
#define windowWindow(w) (GTK_WINDOW(uiControlHandle(uiControl(w))))
static char *filedialog(GtkWindow *parent, GtkFileChooserAction mode, const gchar *confirm)
static char *filedialog(GtkWindow *parent, GtkFileChooserAction mode, const gchar *confirm, char* filter, char* initpath)
{
GtkWidget *fcd;
GtkFileChooser *fc;
@ -18,6 +18,52 @@ static char *filedialog(GtkWindow *parent, GtkFileChooserAction mode, const gcha
confirm, GTK_RESPONSE_ACCEPT,
NULL);
fc = GTK_FILE_CHOOSER(fcd);
// filters
{
gchar _filter[256];
gchar* fp = &_filter[0]; int s = 0;
gchar* fname;
for (int i = 0; i < 255; i++)
{
if (filter[i] == '|' || filter[i] == '\0')
{
_filter[i] = '\0';
if (s & 1)
{
GtkFileFilter* filter = gtk_file_filter_new();
gtk_file_filter_set_name(filter, fname);
for (gchar* j = fp; ; j++)
{
if (*j == ';')
{
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 +79,14 @@ static char *filedialog(GtkWindow *parent, GtkFileChooserAction mode, const gcha
return filename;
}
char *uiOpenFile(uiWindow *parent)
char *uiOpenFile(uiWindow *parent, char* filter, char* initpath)
{
return filedialog(windowWindow(parent), GTK_FILE_CHOOSER_ACTION_OPEN, "_Open");
return filedialog(windowWindow(parent), GTK_FILE_CHOOSER_ACTION_OPEN, "_Open", filter, initpath);
}
char *uiSaveFile(uiWindow *parent)
char *uiSaveFile(uiWindow *parent, char* filter, char* initpath)
{
return filedialog(windowWindow(parent), GTK_FILE_CHOOSER_ACTION_SAVE, "_Save");
return filedialog(windowWindow(parent), GTK_FILE_CHOOSER_ACTION_SAVE, "_Save", filter, initpath);
}
static void msgbox(GtkWindow *parent, const char *title, const char *description, GtkMessageType type, GtkButtonsType buttons)

View File

@ -16,7 +16,7 @@
#define windowHWND(w) ((HWND) uiControlHandle(uiControl(w)))
char *commonItemDialog(HWND parent, REFCLSID clsid, REFIID iid, FILEOPENDIALOGOPTIONS optsadd)
char *commonItemDialog(HWND parent, REFCLSID clsid, REFIID iid, char* filter, char* initpath, FILEOPENDIALOGOPTIONS optsadd)
{
IFileDialog *d = NULL;
FILEOPENDIALOGOPTIONS opts;
@ -46,6 +46,38 @@ char *commonItemDialog(HWND parent, REFCLSID clsid, REFIID iid, FILEOPENDIALOGOP
logHRESULT(L"error setting options", hr);
goto out;
}
// filters
{
COMDLG_FILTERSPEC filterspec[8];
wchar_t _filter[256];
wchar_t* fp = &_filter[0]; int s = 0;
wchar_t* fname;
for (int i = 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 +108,28 @@ out:
return name;
}
char *uiOpenFile(uiWindow *parent)
char *uiOpenFile(uiWindow *parent, char* filter, char* initpath)
{
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, initpath,
FOS_NOCHANGEDIR | FOS_FORCEFILESYSTEM | FOS_NOVALIDATE | FOS_PATHMUSTEXIST | FOS_FILEMUSTEXIST | FOS_SHAREAWARE | FOS_NOTESTFILECREATE | FOS_NODEREFERENCELINKS | FOS_FORCESHOWHIDDEN | FOS_DEFAULTNOMINIMODE);
enableAllWindowsExcept(parent);
return res;
}
char *uiSaveFile(uiWindow *parent)
char *uiSaveFile(uiWindow *parent, char* filter, char* initpath)
{
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, initpath,
FOS_OVERWRITEPROMPT | FOS_NOCHANGEDIR | FOS_FORCEFILESYSTEM | FOS_NOVALIDATE | FOS_SHAREAWARE | FOS_NOTESTFILECREATE | FOS_NODEREFERENCELINKS | FOS_FORCESHOWHIDDEN | FOS_DEFAULTNOMINIMODE);
enableAllWindowsExcept(parent);
return res;
}

View File

@ -21,8 +21,7 @@
#include <stdio.h>
#include <SDL2/SDL.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include "libui/ui.h"
#include "../types.h"
#include "../version.h"
@ -34,6 +33,14 @@ SDL_GLContext MainGL;
void RunMainWindow();
void OnOpenFile(uiMenuItem* item, uiWindow* window, void* blarg)
{
char* file = uiOpenFile(window, "DS ROM (*.nds)|*.nds;*.srl|Any file|*.*", NULL);
if (!file) return;
printf("file opened: %s\n", file);
}
int main(int argc, char** argv)
{
srand(time(NULL));
@ -47,8 +54,34 @@ int main(int argc, char** argv)
return 1;
}
RunMainWindow();
uiInitOptions ui_opt;
memset(&ui_opt, 0, sizeof(uiInitOptions));
const char* ui_err = uiInit(&ui_opt);
if (ui_err != NULL)
{
printf("libui shat itself :( %s\n", ui_err);
uiFreeInitError(ui_err);
return 1;
}
//RunMainWindow();
uiMenu* menu;
uiMenuItem* menuitem;
menu = uiNewMenu("File");
menuitem = uiMenuAppendItem(menu, "Open...");
uiMenuItemOnClicked(menuitem, OnOpenFile, NULL);
uiMenuAppendSeparator(menu);
uiMenuAppendItem(menu, "Quit");
uiWindow* win;
win = uiNewWindow("melonDS " MELONDS_VERSION, 256, 384, 1);
uiControlShow(uiControl(win));
uiMain();
uiUninit();
SDL_Quit();
return 0;
}