# HG changeset patch # User Olaf Wintermann # Date 1712489066 -7200 # Node ID 2ad83650d797f6c42a15890e77b59c478dd7bafa # Parent a8faf87574507bb8f151bdd8e907bc7c62b3d46e implement file dialog (GTK) diff -r a8faf8757450 -r 2ad83650d797 application/main.c --- 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) { diff -r a8faf8757450 -r 2ad83650d797 ui/gtk/window.c --- 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