Fixes and improvements for dialogs
This commit is contained in:
parent
22bcee3318
commit
0411633bb2
3 changed files with 56 additions and 41 deletions
|
@ -38,11 +38,10 @@ char *convert_filters(const SDL_DialogFileFilter *filters, NameTransform ntf,
|
||||||
combined = SDL_strdup(prefix);
|
combined = SDL_strdup(prefix);
|
||||||
|
|
||||||
if (!combined) {
|
if (!combined) {
|
||||||
SDL_OutOfMemory();
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const SDL_DialogFileFilter *f = filters; f->name; f++) {
|
for (const SDL_DialogFileFilter *f = filters; f->name && f->pattern; f++) {
|
||||||
converted = convert_filter(*f, ntf, filt_prefix, filt_separator,
|
converted = convert_filter(*f, ntf, filt_prefix, filt_separator,
|
||||||
filt_suffix, ext_prefix, ext_separator,
|
filt_suffix, ext_prefix, ext_separator,
|
||||||
ext_suffix);
|
ext_suffix);
|
||||||
|
@ -61,7 +60,6 @@ char *convert_filters(const SDL_DialogFileFilter *filters, NameTransform ntf,
|
||||||
if (!new_combined) {
|
if (!new_combined) {
|
||||||
SDL_free(converted);
|
SDL_free(converted);
|
||||||
SDL_free(combined);
|
SDL_free(combined);
|
||||||
SDL_OutOfMemory();
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,6 +70,22 @@ char *convert_filters(const SDL_DialogFileFilter *filters, NameTransform ntf,
|
||||||
SDL_free(converted);
|
SDL_free(converted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* If the filter list is empty, put the suffix */
|
||||||
|
if (!filters->name || !filters->pattern) {
|
||||||
|
new_length = SDL_strlen(combined) + SDL_strlen(suffix) + 1;
|
||||||
|
|
||||||
|
new_combined = SDL_realloc(combined, new_length);
|
||||||
|
|
||||||
|
if (!new_combined) {
|
||||||
|
SDL_free(combined);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
combined = new_combined;
|
||||||
|
|
||||||
|
SDL_strlcat(combined, suffix, new_length);
|
||||||
|
}
|
||||||
|
|
||||||
return combined;
|
return combined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,7 +127,6 @@ char *convert_filter(const SDL_DialogFileFilter filter, NameTransform ntf,
|
||||||
if (!converted) {
|
if (!converted) {
|
||||||
SDL_free(list);
|
SDL_free(list);
|
||||||
SDL_free(name_filtered);
|
SDL_free(name_filtered);
|
||||||
SDL_OutOfMemory();
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,7 +161,6 @@ char *convert_ext_list(const char *list, const char *prefix,
|
||||||
converted = (char *) SDL_malloc(total_length);
|
converted = (char *) SDL_malloc(total_length);
|
||||||
|
|
||||||
if (!converted) {
|
if (!converted) {
|
||||||
SDL_OutOfMemory();
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,7 +211,7 @@ char *convert_ext_list(const char *list, const char *prefix,
|
||||||
const char *validate_filters(const SDL_DialogFileFilter *filters)
|
const char *validate_filters(const SDL_DialogFileFilter *filters)
|
||||||
{
|
{
|
||||||
if (filters) {
|
if (filters) {
|
||||||
for (const SDL_DialogFileFilter *f = filters; f->name; f++) {
|
for (const SDL_DialogFileFilter *f = filters; f->name && f->pattern; f++) {
|
||||||
const char *msg = validate_list(f->pattern);
|
const char *msg = validate_list(f->pattern);
|
||||||
|
|
||||||
if (msg) {
|
if (msg) {
|
||||||
|
|
|
@ -280,12 +280,14 @@ static void DBus_OpenDialog(const char *method, const char *method_title, SDL_Di
|
||||||
|
|
||||||
if (dbus == NULL) {
|
if (dbus == NULL) {
|
||||||
SDL_SetError("Failed to connect to DBus");
|
SDL_SetError("Failed to connect to DBus");
|
||||||
|
callback(userdata, NULL, -1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
msg = dbus->message_new_method_call(PORTAL_DESTINATION, PORTAL_PATH, PORTAL_INTERFACE, method);
|
msg = dbus->message_new_method_call(PORTAL_DESTINATION, PORTAL_PATH, PORTAL_INTERFACE, method);
|
||||||
if (msg == NULL) {
|
if (msg == NULL) {
|
||||||
SDL_SetError("Failed to send message to portal");
|
SDL_SetError("Failed to send message to portal");
|
||||||
|
callback(userdata, NULL, -1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -299,6 +301,7 @@ static void DBus_OpenDialog(const char *method, const char *method_title, SDL_Di
|
||||||
len += sizeof(WAYLAND_HANDLE_PREFIX) + 1;
|
len += sizeof(WAYLAND_HANDLE_PREFIX) + 1;
|
||||||
handle_str = SDL_malloc(len * sizeof(char));
|
handle_str = SDL_malloc(len * sizeof(char));
|
||||||
if (!handle_str) {
|
if (!handle_str) {
|
||||||
|
callback(userdata, NULL, -1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -309,6 +312,7 @@ static void DBus_OpenDialog(const char *method, const char *method_title, SDL_Di
|
||||||
const size_t len = sizeof(X11_HANDLE_PREFIX) + 24; /* A 64-bit number can be 20 characters max. */
|
const size_t len = sizeof(X11_HANDLE_PREFIX) + 24; /* A 64-bit number can be 20 characters max. */
|
||||||
handle_str = SDL_malloc(len * sizeof(char));
|
handle_str = SDL_malloc(len * sizeof(char));
|
||||||
if (!handle_str) {
|
if (!handle_str) {
|
||||||
|
callback(userdata, NULL, -1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -328,6 +332,7 @@ static void DBus_OpenDialog(const char *method, const char *method_title, SDL_Di
|
||||||
|
|
||||||
handle_str = SDL_malloc(sizeof(char) * (HANDLE_LEN + 1));
|
handle_str = SDL_malloc(sizeof(char) * (HANDLE_LEN + 1));
|
||||||
if (!handle_str) {
|
if (!handle_str) {
|
||||||
|
callback(userdata, NULL, -1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SDL_snprintf(handle_str, HANDLE_LEN, "%u", ++handle_id);
|
SDL_snprintf(handle_str, HANDLE_LEN, "%u", ++handle_id);
|
||||||
|
@ -361,6 +366,7 @@ static void DBus_OpenDialog(const char *method, const char *method_title, SDL_Di
|
||||||
|
|
||||||
if (!signal_id) {
|
if (!signal_id) {
|
||||||
SDL_SetError("Invalid response received by DBus");
|
SDL_SetError("Invalid response received by DBus");
|
||||||
|
callback(userdata, NULL, -1);
|
||||||
goto incorrect_type;
|
goto incorrect_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -369,6 +375,7 @@ static void DBus_OpenDialog(const char *method, const char *method_title, SDL_Di
|
||||||
filter_len = SDL_strlen(SIGNAL_FILTER) + SDL_strlen(signal_id) + 2;
|
filter_len = SDL_strlen(SIGNAL_FILTER) + SDL_strlen(signal_id) + 2;
|
||||||
filter = SDL_malloc(sizeof(char) * filter_len);
|
filter = SDL_malloc(sizeof(char) * filter_len);
|
||||||
if (!filter) {
|
if (!filter) {
|
||||||
|
callback(userdata, NULL, -1);
|
||||||
goto incorrect_type;
|
goto incorrect_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -378,6 +385,7 @@ static void DBus_OpenDialog(const char *method, const char *method_title, SDL_Di
|
||||||
|
|
||||||
SignalCallback *data = SDL_malloc(sizeof(SignalCallback));
|
SignalCallback *data = SDL_malloc(sizeof(SignalCallback));
|
||||||
if (!data) {
|
if (!data) {
|
||||||
|
callback(userdata, NULL, -1);
|
||||||
goto incorrect_type;
|
goto incorrect_type;
|
||||||
}
|
}
|
||||||
data->callback = callback;
|
data->callback = callback;
|
||||||
|
@ -385,6 +393,7 @@ static void DBus_OpenDialog(const char *method, const char *method_title, SDL_Di
|
||||||
data->path = SDL_strdup(signal_id);
|
data->path = SDL_strdup(signal_id);
|
||||||
if (!data->path) {
|
if (!data->path) {
|
||||||
SDL_free(data);
|
SDL_free(data);
|
||||||
|
callback(userdata, NULL, -1);
|
||||||
goto incorrect_type;
|
goto incorrect_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -154,39 +154,43 @@ void windows_ShowFileDialog(void *ptr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* '\x01' is used in place of a null byte */
|
wchar_t *filter_wchar = NULL;
|
||||||
char *filterlist = convert_filters(filters, NULL, "", "", "\x01", "",
|
|
||||||
"\x01", "\x01", "*.", ";*.", "");
|
|
||||||
|
|
||||||
if (!filterlist) {
|
if (filters) {
|
||||||
callback(userdata, NULL, -1);
|
/* '\x01' is used in place of a null byte */
|
||||||
SDL_free(filebuffer);
|
/* suffix needs two null bytes in case the filter list is empty */
|
||||||
return;
|
char *filterlist = convert_filters(filters, NULL, "", "", "\x01\x01", "",
|
||||||
}
|
"\x01", "\x01", "*.", ";*.", "");
|
||||||
|
|
||||||
int filter_len = (int)SDL_strlen(filterlist);
|
if (!filterlist) {
|
||||||
|
callback(userdata, NULL, -1);
|
||||||
for (char *c = filterlist; *c; c++) {
|
SDL_free(filebuffer);
|
||||||
if (*c == '\x01') {
|
return;
|
||||||
*c = '\0';
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int filter_wlen = MultiByteToWideChar(CP_UTF8, 0, filterlist, filter_len, NULL, 0);
|
int filter_len = (int)SDL_strlen(filterlist);
|
||||||
wchar_t *filter_wchar = SDL_malloc(filter_wlen * sizeof(wchar_t));
|
|
||||||
|
for (char *c = filterlist; *c; c++) {
|
||||||
|
if (*c == '\x01') {
|
||||||
|
*c = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int filter_wlen = MultiByteToWideChar(CP_UTF8, 0, filterlist, filter_len, NULL, 0);
|
||||||
|
filter_wchar = SDL_malloc(filter_wlen * sizeof(wchar_t));
|
||||||
|
|
||||||
|
if (!filter_wchar) {
|
||||||
|
SDL_free(filterlist);
|
||||||
|
callback(userdata, NULL, -1);
|
||||||
|
SDL_free(filebuffer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
MultiByteToWideChar(CP_UTF8, 0, filterlist, filter_len, filter_wchar, filter_wlen);
|
||||||
|
|
||||||
if (!filter_wchar) {
|
|
||||||
SDL_OutOfMemory();
|
|
||||||
SDL_free(filterlist);
|
SDL_free(filterlist);
|
||||||
callback(userdata, NULL, -1);
|
|
||||||
SDL_free(filebuffer);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MultiByteToWideChar(CP_UTF8, 0, filterlist, filter_len, filter_wchar, filter_wlen);
|
|
||||||
|
|
||||||
SDL_free(filterlist);
|
|
||||||
|
|
||||||
OPENFILENAMEW dialog;
|
OPENFILENAMEW dialog;
|
||||||
dialog.lStructSize = sizeof(OPENFILENAME);
|
dialog.lStructSize = sizeof(OPENFILENAME);
|
||||||
dialog.hwndOwner = window;
|
dialog.hwndOwner = window;
|
||||||
|
@ -237,7 +241,6 @@ void windows_ShowFileDialog(void *ptr)
|
||||||
char **chosen_files_list = (char **) SDL_malloc(sizeof(char *) * (nfiles + 1));
|
char **chosen_files_list = (char **) SDL_malloc(sizeof(char *) * (nfiles + 1));
|
||||||
|
|
||||||
if (!chosen_files_list) {
|
if (!chosen_files_list) {
|
||||||
SDL_OutOfMemory();
|
|
||||||
callback(userdata, NULL, -1);
|
callback(userdata, NULL, -1);
|
||||||
SDL_free(filebuffer);
|
SDL_free(filebuffer);
|
||||||
return;
|
return;
|
||||||
|
@ -264,8 +267,6 @@ void windows_ShowFileDialog(void *ptr)
|
||||||
char **new_cfl = (char **) SDL_realloc(chosen_files_list, sizeof(char*) * (nfiles + 1));
|
char **new_cfl = (char **) SDL_realloc(chosen_files_list, sizeof(char*) * (nfiles + 1));
|
||||||
|
|
||||||
if (!new_cfl) {
|
if (!new_cfl) {
|
||||||
SDL_OutOfMemory();
|
|
||||||
|
|
||||||
for (size_t i = 0; i < nfiles - 1; i++) {
|
for (size_t i = 0; i < nfiles - 1; i++) {
|
||||||
SDL_free(chosen_files_list[i]);
|
SDL_free(chosen_files_list[i]);
|
||||||
}
|
}
|
||||||
|
@ -299,8 +300,6 @@ void windows_ShowFileDialog(void *ptr)
|
||||||
chosen_files_list[nfiles - 1] = SDL_strdup(chosen_file);
|
chosen_files_list[nfiles - 1] = SDL_strdup(chosen_file);
|
||||||
|
|
||||||
if (!chosen_files_list[nfiles - 1]) {
|
if (!chosen_files_list[nfiles - 1]) {
|
||||||
SDL_OutOfMemory();
|
|
||||||
|
|
||||||
for (size_t i = 0; i < nfiles - 1; i++) {
|
for (size_t i = 0; i < nfiles - 1; i++) {
|
||||||
SDL_free(chosen_files_list[i]);
|
SDL_free(chosen_files_list[i]);
|
||||||
}
|
}
|
||||||
|
@ -318,7 +317,6 @@ void windows_ShowFileDialog(void *ptr)
|
||||||
char **new_cfl = (char **) SDL_realloc(chosen_files_list, sizeof(char*) * (nfiles + 1));
|
char **new_cfl = (char **) SDL_realloc(chosen_files_list, sizeof(char*) * (nfiles + 1));
|
||||||
|
|
||||||
if (!new_cfl) {
|
if (!new_cfl) {
|
||||||
SDL_OutOfMemory();
|
|
||||||
SDL_free(chosen_files_list);
|
SDL_free(chosen_files_list);
|
||||||
callback(userdata, NULL, -1);
|
callback(userdata, NULL, -1);
|
||||||
SDL_free(filebuffer);
|
SDL_free(filebuffer);
|
||||||
|
@ -330,7 +328,6 @@ void windows_ShowFileDialog(void *ptr)
|
||||||
chosen_files_list[nfiles - 1] = SDL_strdup(chosen_folder);
|
chosen_files_list[nfiles - 1] = SDL_strdup(chosen_folder);
|
||||||
|
|
||||||
if (!chosen_files_list[nfiles - 1]) {
|
if (!chosen_files_list[nfiles - 1]) {
|
||||||
SDL_OutOfMemory();
|
|
||||||
SDL_free(chosen_files_list);
|
SDL_free(chosen_files_list);
|
||||||
callback(userdata, NULL, -1);
|
callback(userdata, NULL, -1);
|
||||||
SDL_free(filebuffer);
|
SDL_free(filebuffer);
|
||||||
|
@ -457,7 +454,6 @@ void SDL_ShowOpenFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL
|
||||||
|
|
||||||
args = SDL_malloc(sizeof(winArgs));
|
args = SDL_malloc(sizeof(winArgs));
|
||||||
if (args == NULL) {
|
if (args == NULL) {
|
||||||
SDL_OutOfMemory();
|
|
||||||
callback(userdata, NULL, -1);
|
callback(userdata, NULL, -1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -494,7 +490,6 @@ void SDL_ShowSaveFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL
|
||||||
|
|
||||||
args = SDL_malloc(sizeof(winArgs));
|
args = SDL_malloc(sizeof(winArgs));
|
||||||
if (args == NULL) {
|
if (args == NULL) {
|
||||||
SDL_OutOfMemory();
|
|
||||||
callback(userdata, NULL, -1);
|
callback(userdata, NULL, -1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -531,7 +526,6 @@ void SDL_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void* userdata, S
|
||||||
|
|
||||||
args = SDL_malloc(sizeof(winFArgs));
|
args = SDL_malloc(sizeof(winFArgs));
|
||||||
if (args == NULL) {
|
if (args == NULL) {
|
||||||
SDL_OutOfMemory();
|
|
||||||
callback(userdata, NULL, -1);
|
callback(userdata, NULL, -1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue