--- a/ui/gtk/window.c Mon Feb 12 21:13:23 2024 +0100 +++ b/ui/gtk/window.c Sun Jun 09 15:43:08 2024 +0200 @@ -126,6 +126,9 @@ gtk_box_pack_start(GTK_BOX(vbox), tb, FALSE, FALSE, 0); } } + + //GtkWidget *hb = ui_create_headerbar(obj); + //gtk_window_set_titlebar(GTK_WINDOW(obj->widget), hb); } // window content @@ -148,41 +151,171 @@ return create_window(title, window_data, FALSE); } -UiObject* ui_simplewindow(const char *title, void *window_data) { +UiObject* ui_simple_window(const char *title, void *window_data) { return create_window(title, window_data, TRUE); } -static char* ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action) { +void ui_window_size(UiObject *obj, int width, int height) { + gtk_window_set_default_size( + GTK_WINDOW(obj->widget), + width, + height); +} + +static void ui_dialog_response (GtkDialog* self, gint response_id, gpointer user_data) { + UiEventData *data = user_data; + UiEvent evt; + evt.obj = data->obj; + evt.document = evt.obj->ctx->document; + evt.window = evt.obj->window; + evt.eventdata = NULL; + evt.intval = 0; + + if(data->customdata) { + GtkWidget *entry = data->customdata; + evt.eventdata = (void*)gtk_entry_get_text(GTK_ENTRY(entry)); + + } + + if(response_id == 1 || response_id == 2) { + evt.intval = response_id; + } + + + if(data->callback) { + data->callback(&evt, data->userdata); + } + + gtk_widget_destroy(GTK_WIDGET(self)); +} + +void ui_dialog_create(UiObject *parent, UiDialogArgs args) { + GtkDialog *dialog = GTK_DIALOG(gtk_dialog_new()); + GtkWidget *dialog_w = GTK_WIDGET(dialog); + if(args.title) { + gtk_window_set_title(GTK_WINDOW(dialog), args.title); + } + if(args.button1_label) { + gtk_dialog_add_button(dialog, args.button1_label, 1); + } + if(args.button2_label) { + gtk_dialog_add_button(dialog, args.button2_label, 2); + } + if(args.closebutton_label) { + gtk_dialog_add_button(dialog, args.closebutton_label, 0); + } + + GtkWidget *content_area = gtk_dialog_get_content_area(dialog); + if(args.content) { + GtkWidget *label = gtk_label_new(args.content); + gtk_container_add(GTK_CONTAINER(content_area), label); + } + + GtkWidget *textfield = NULL; + if(args.input) { + textfield = gtk_entry_new(); + gtk_container_add(GTK_CONTAINER(content_area), textfield); + } + + UiEventData *event = malloc(sizeof(UiEventData)); + event->obj = parent; + event->callback = args.result; + event->userdata = args.resultdata; + event->value = 0; + event->customdata = textfield; + + g_signal_connect(dialog_w, + "response", + G_CALLBACK(ui_dialog_response), + event); + + gtk_widget_show_all(GTK_WIDGET(dialog_w)); +} + +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); +} +