Forráskód Böngészése

Fixes and improvements for dialogs

Semphris 11 hónapja
szülő
commit
0411633bb2

+ 18 - 6
src/dialog/SDL_dialog_utils.c

@@ -38,11 +38,10 @@ char *convert_filters(const SDL_DialogFileFilter *filters, NameTransform ntf,
     combined = SDL_strdup(prefix);
 
     if (!combined) {
-        SDL_OutOfMemory();
         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,
                                    filt_suffix, ext_prefix, ext_separator,
                                    ext_suffix);
@@ -61,7 +60,6 @@ char *convert_filters(const SDL_DialogFileFilter *filters, NameTransform ntf,
         if (!new_combined) {
             SDL_free(converted);
             SDL_free(combined);
-            SDL_OutOfMemory();
             return NULL;
         }
 
@@ -72,6 +70,22 @@ char *convert_filters(const SDL_DialogFileFilter *filters, NameTransform ntf,
         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;
 }
 
@@ -113,7 +127,6 @@ char *convert_filter(const SDL_DialogFileFilter filter, NameTransform ntf,
     if (!converted) {
         SDL_free(list);
         SDL_free(name_filtered);
-        SDL_OutOfMemory();
         return NULL;
     }
 
@@ -148,7 +161,6 @@ char *convert_ext_list(const char *list, const char *prefix,
     converted = (char *) SDL_malloc(total_length);
 
     if (!converted) {
-        SDL_OutOfMemory();
         return NULL;
     }
 
@@ -199,7 +211,7 @@ char *convert_ext_list(const char *list, const char *prefix,
 const char *validate_filters(const SDL_DialogFileFilter *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);
 
              if (msg) {

+ 9 - 0
src/dialog/unix/SDL_portaldialog.c

@@ -280,12 +280,14 @@ static void DBus_OpenDialog(const char *method, const char *method_title, SDL_Di
 
     if (dbus == NULL) {
         SDL_SetError("Failed to connect to DBus");
+        callback(userdata, NULL, -1);
         return;
     }
 
     msg = dbus->message_new_method_call(PORTAL_DESTINATION, PORTAL_PATH, PORTAL_INTERFACE, method);
     if (msg == NULL) {
         SDL_SetError("Failed to send message to portal");
+        callback(userdata, NULL, -1);
         return;
     }
 
@@ -299,6 +301,7 @@ static void DBus_OpenDialog(const char *method, const char *method_title, SDL_Di
             len += sizeof(WAYLAND_HANDLE_PREFIX) + 1;
             handle_str = SDL_malloc(len * sizeof(char));
             if (!handle_str) {
+                callback(userdata, NULL, -1);
                 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. */
                 handle_str = SDL_malloc(len * sizeof(char));
                 if (!handle_str) {
+                    callback(userdata, NULL, -1);
                     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));
     if (!handle_str) {
+        callback(userdata, NULL, -1);
         return;
     }
     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) {
         SDL_SetError("Invalid response received by DBus");
+        callback(userdata, NULL, -1);
         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 = SDL_malloc(sizeof(char) * filter_len);
     if (!filter) {
+        callback(userdata, NULL, -1);
         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));
     if (!data) {
+        callback(userdata, NULL, -1);
         goto incorrect_type;
     }
     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);
     if (!data->path) {
         SDL_free(data);
+        callback(userdata, NULL, -1);
         goto incorrect_type;
     }
 

+ 28 - 34
src/dialog/windows/SDL_windowsdialog.c

@@ -154,38 +154,42 @@ void windows_ShowFileDialog(void *ptr)
         }
     }
 
-    /* '\x01' is used in place of a null byte */
-    char *filterlist = convert_filters(filters, NULL, "", "", "\x01", "",
-                                       "\x01", "\x01", "*.", ";*.", "");
+    wchar_t *filter_wchar = NULL;
 
-    if (!filterlist) {
-        callback(userdata, NULL, -1);
-        SDL_free(filebuffer);
-        return;
-    }
+    if (filters) {
+        /* '\x01' is used in place of a null byte */
+        /* suffix needs two null bytes in case the filter list is empty */
+        char *filterlist = convert_filters(filters, NULL, "", "", "\x01\x01", "",
+                                           "\x01", "\x01", "*.", ";*.", "");
 
-    int filter_len = (int)SDL_strlen(filterlist);
+        if (!filterlist) {
+            callback(userdata, NULL, -1);
+            SDL_free(filebuffer);
+            return;
+        }
 
-    for (char *c = filterlist; *c; c++) {
-        if (*c == '\x01') {
-            *c = '\0';
+        int filter_len = (int)SDL_strlen(filterlist);
+
+        for (char *c = filterlist; *c; c++) {
+            if (*c == '\x01') {
+                *c = '\0';
+            }
         }
-    }
 
-    int filter_wlen = MultiByteToWideChar(CP_UTF8, 0, filterlist, filter_len, NULL, 0);
-    wchar_t *filter_wchar = SDL_malloc(filter_wlen * sizeof(wchar_t));
+        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_OutOfMemory();
-        SDL_free(filterlist);
-        callback(userdata, NULL, -1);
-        SDL_free(filebuffer);
-        return;
-    }
+        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);
+        MultiByteToWideChar(CP_UTF8, 0, filterlist, filter_len, filter_wchar, filter_wlen);
 
-    SDL_free(filterlist);
+        SDL_free(filterlist);
+    }
 
     OPENFILENAMEW dialog;
     dialog.lStructSize = sizeof(OPENFILENAME);
@@ -237,7 +241,6 @@ void windows_ShowFileDialog(void *ptr)
             char **chosen_files_list = (char **) SDL_malloc(sizeof(char *) * (nfiles + 1));
 
             if (!chosen_files_list) {
-                SDL_OutOfMemory();
                 callback(userdata, NULL, -1);
                 SDL_free(filebuffer);
                 return;
@@ -264,8 +267,6 @@ void windows_ShowFileDialog(void *ptr)
                 char **new_cfl = (char **) SDL_realloc(chosen_files_list, sizeof(char*) * (nfiles + 1));
 
                 if (!new_cfl) {
-                    SDL_OutOfMemory();
-
                     for (size_t i = 0; i < nfiles - 1; 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);
 
                 if (!chosen_files_list[nfiles - 1]) {
-                    SDL_OutOfMemory();
-
                     for (size_t i = 0; i < nfiles - 1; 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));
 
                 if (!new_cfl) {
-                    SDL_OutOfMemory();
                     SDL_free(chosen_files_list);
                     callback(userdata, NULL, -1);
                     SDL_free(filebuffer);
@@ -330,7 +328,6 @@ void windows_ShowFileDialog(void *ptr)
                 chosen_files_list[nfiles - 1] = SDL_strdup(chosen_folder);
 
                 if (!chosen_files_list[nfiles - 1]) {
-                    SDL_OutOfMemory();
                     SDL_free(chosen_files_list);
                     callback(userdata, NULL, -1);
                     SDL_free(filebuffer);
@@ -457,7 +454,6 @@ void SDL_ShowOpenFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL
 
     args = SDL_malloc(sizeof(winArgs));
     if (args == NULL) {
-        SDL_OutOfMemory();
         callback(userdata, NULL, -1);
         return;
     }
@@ -494,7 +490,6 @@ void SDL_ShowSaveFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL
 
     args = SDL_malloc(sizeof(winArgs));
     if (args == NULL) {
-        SDL_OutOfMemory();
         callback(userdata, NULL, -1);
         return;
     }
@@ -531,7 +526,6 @@ void SDL_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void* userdata, S
 
     args = SDL_malloc(sizeof(winFArgs));
     if (args == NULL) {
-        SDL_OutOfMemory();
         callback(userdata, NULL, -1);
         return;
     }