add alternative ui_dialog implementation for libadwaita newapi

Thu, 03 Oct 2024 11:42:14 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 03 Oct 2024 11:42:14 +0200
branch
newapi
changeset 320
be224cf1f50e
parent 319
050ceb8ecf44
child 321
8d63b7cd3d98

add alternative ui_dialog implementation for libadwaita

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/window.c file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Sep 29 20:24:46 2024 +0200
+++ b/application/main.c	Thu Oct 03 11:42:14 2024 +0200
@@ -68,7 +68,9 @@
 }
 
 void action_toolbar_button(UiEvent *event, void *userdata) {
+    printf("toolbar button\n");
     
+    ui_dialog(event->obj, .title = "Dialog Title", .content = "Content Label", .button1_label = "btn1", .button2_label = "btn2", .input = TRUE, .closebutton_label = "Cancel");
 }
 
 MyDocument* create_doc(void) {
--- a/ui/gtk/window.c	Sun Sep 29 20:24:46 2024 +0200
+++ b/ui/gtk/window.c	Thu Oct 03 11:42:14 2024 +0200
@@ -190,6 +190,74 @@
                 height);
 }
 
+#ifdef UI_LIBADWAITA
+
+static void dialog_response(AdwAlertDialog *self, gchar *response, UiEventData *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(!strcmp(response, "btn1")) {
+        evt.intval = 1;
+    } else if(!strcmp(response, "btn2")) {
+        evt.intval = 2;
+    }
+    
+    if(data->customdata) {
+        GtkWidget *entry = data->customdata;
+        evt.eventdata = (void*)ENTRY_GET_TEXT(GTK_ENTRY(entry));
+    }
+    
+    if(data->callback) {
+        data->callback(&evt, data->userdata);
+    }
+}
+
+void ui_dialog_create(UiObject *parent, UiDialogArgs args) {
+    AdwDialog *dialog = adw_alert_dialog_new(args.title, args.content);
+    UiEventData *event = malloc(sizeof(UiEventData));
+    event->callback = args.result;
+    event->userdata = args.resultdata;
+    event->customdata = NULL;
+    event->value = 0;
+    event->obj = parent;
+    
+    if(args.button1_label) {
+        adw_alert_dialog_add_response(ADW_ALERT_DIALOG(dialog), "btn1", args.button1_label);
+    }
+    if(args.button2_label) {
+        adw_alert_dialog_add_response(ADW_ALERT_DIALOG(dialog), "btn2", args.button2_label);
+    }
+    if(args.closebutton_label) {
+        adw_alert_dialog_add_response(ADW_ALERT_DIALOG(dialog), "close", args.closebutton_label);
+        adw_alert_dialog_set_close_response(ADW_ALERT_DIALOG(dialog), "close");
+    }
+    
+    GtkWidget *entry = NULL;
+    if(args.input) {
+        entry = gtk_entry_new();
+        adw_alert_dialog_set_extra_child(ADW_ALERT_DIALOG(dialog), entry);
+        event->customdata = entry;
+    }
+    
+    g_signal_connect(
+                dialog,
+                "destroy",
+                G_CALLBACK(ui_destroy_userdata),
+                event);
+    
+    g_signal_connect(dialog, "response", G_CALLBACK(dialog_response), event);
+    adw_dialog_present(dialog, parent->widget);
+    
+    if(entry) {
+        gtk_entry_grab_focus_without_selecting(GTK_ENTRY(entry));
+    }
+}
+#else
+
 static void ui_dialog_response (GtkDialog* self, gint response_id, gpointer user_data) {
     UiEventData *data = user_data;
     UiEvent evt;
@@ -219,6 +287,9 @@
 
 void ui_dialog_create(UiObject *parent, UiDialogArgs args) {
     GtkDialog *dialog = GTK_DIALOG(gtk_dialog_new());
+    gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent->widget));
+    gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
+    
     GtkWidget *dialog_w = GTK_WIDGET(dialog);
     if(args.title) {
         gtk_window_set_title(GTK_WINDOW(dialog), args.title);
@@ -259,6 +330,7 @@
     
     WINDOW_SHOW(GTK_WIDGET(dialog_w));
 }
+#endif
 
 
 #if GTK_MAJOR_VERSION >= 3

mercurial