implement file dialog (GTK) newapi

Sun, 07 Apr 2024 13:24:26 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 07 Apr 2024 13:24:26 +0200
branch
newapi
changeset 279
2ad83650d797
parent 278
a8faf8757450
child 280
e3565cf7c831

implement file dialog (GTK)

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/window.c file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Apr 07 12:36:07 2024 +0200
+++ b/application/main.c	Sun Apr 07 13:24:26 2024 +0200
@@ -49,14 +49,16 @@
     
 }
 
+void action_file_selected(UiEvent *event, void *userdata) {
+    UiFileList *files = event->eventdata;
+    printf("files: %d\n", (int)files->nfiles);
+    if(files->nfiles > 0) {
+        printf("selected file: %s\n", files->files[0]);
+    }
+}
+
 void action_button(UiEvent *event, void *userdata) {
-    ui_dialog(event->obj,
-            .title = "My Title",
-            .button1_label = "OK",
-            .button2_label = "Test",
-            .closebutton_label = "Cancel",
-            .content = "Hello World!",
-            .input = true);
+    ui_openfiledialog(event->obj, UI_FILEDIALOG_SELECT_SINGLE, action_file_selected, NULL);
 }
 
 void action_switch(UiEvent *event, void *userdata) {
--- a/ui/gtk/window.c	Sun Apr 07 12:36:07 2024 +0200
+++ b/ui/gtk/window.c	Sun Apr 07 13:24:26 2024 +0200
@@ -225,37 +225,90 @@
     gtk_widget_show_all(GTK_WIDGET(dialog_w));
 }
 
-static char* ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action) {
+static void ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action, unsigned int mode, ui_callback file_selected_callback, void *cbdata) {
     char *button;
     char *title;
     
-    if(action == GTK_FILE_CHOOSER_ACTION_OPEN) {
-        button = GTK_STOCK_OPEN;
-        title = "Datei öffnen...";
+    GtkWidget *dialog;
+    if((mode & UI_FILEDIALOG_SELECT_FOLDER) == UI_FILEDIALOG_SELECT_FOLDER) {
+        dialog = gtk_file_chooser_dialog_new (
+                "Open Folder",
+                GTK_WINDOW(obj->widget),
+                GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
+                "Cancel",
+                GTK_RESPONSE_CANCEL,
+                "Select Folder",
+                GTK_RESPONSE_ACCEPT,
+                NULL);
+    } else if(action == GTK_FILE_CHOOSER_ACTION_OPEN) {
+        dialog = gtk_file_chooser_dialog_new (
+                "Select Folder",
+                GTK_WINDOW(obj->widget),
+                action,
+                "Cancel",
+                GTK_RESPONSE_CANCEL,
+                "Open File",
+                GTK_RESPONSE_ACCEPT,
+                NULL);
     } else {
-        button = GTK_STOCK_SAVE;
-        title = "Datei speichern...";
+        dialog = gtk_file_chooser_dialog_new (
+                "Save File",
+                GTK_WINDOW(obj->widget),
+                action,
+                "Cancel",
+                GTK_RESPONSE_CANCEL,
+                "Save File",
+                GTK_RESPONSE_ACCEPT,
+                NULL);
     }
     
-    GtkWidget *dialog = gtk_file_chooser_dialog_new(
-                                title,
-                                GTK_WINDOW(obj->widget),
-                                action,
-                                GTK_STOCK_CANCEL,
-                                GTK_RESPONSE_CANCEL,
-                                button,
-                                GTK_RESPONSE_ACCEPT,
-                                NULL);
-    if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
-        char *file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
-        gtk_widget_destroy(dialog);
-        char *copy = strdup(file);
-        g_free(file);
-        return copy;
-    } else {
-        gtk_widget_destroy(dialog);
-        return NULL;
+    if((mode & UI_FILEDIALOG_SELECT_MULTI) == UI_FILEDIALOG_SELECT_MULTI) {
+        gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
     }
+       
+    UiEvent evt;
+    evt.obj = obj;
+    evt.document = evt.obj->ctx->document;
+    evt.window = evt.obj->window;
+    evt.intval = 0;
+    
+    UiFileList flist;
+    flist.files = NULL;
+    flist.nfiles = 0;
+    evt.eventdata = &flist;
+    
+    int result = gtk_dialog_run(GTK_DIALOG (dialog));
+    GSList *selection = NULL;
+    if(result == GTK_RESPONSE_ACCEPT) {
+        selection = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
+        flist.nfiles = g_slist_length(selection);
+        flist.files = calloc(flist.nfiles, sizeof(char*));
+        int i = 0;
+        while(selection) {
+            flist.files[i] = selection->data;
+            selection = selection->next;
+            i++;
+        }
+    }
+    
+    if(file_selected_callback) {
+        file_selected_callback(&evt, cbdata);
+    }
+    
+    for(int i=0;i<flist.nfiles;i++) {
+        g_free(flist.files[i]);
+    } 
+    free(flist.files);
+    g_slist_free(selection);
+    
+    gtk_widget_destroy(dialog);
 }
 
+void ui_openfiledialog(UiObject *obj, unsigned int mode, ui_callback file_selected_callback, void *cbdata) {
+    ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_OPEN, mode, file_selected_callback, cbdata);
+}
 
+void ui_savefiledialog(UiObject *obj, const char *name, ui_callback file_selected_callback, void *cbdata) {
+    ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_SAVE, 0, file_selected_callback, cbdata);
+}
+

mercurial