ui/gtk/window.c

Wed, 30 Oct 2024 11:07:52 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 30 Oct 2024 11:07:52 +0100
branch
newapi
changeset 373
2eede3d98aba
parent 371
8c028965410a
permissions
-rw-r--r--

implement windows condvar

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2017 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../ui/window.h"
#include "../ui/properties.h"
#include "../common/context.h"
#include "../common/menu.h"
#include "../common/toolbar.h"

#include <cx/mempool.h>

#include "menu.h"
#include "toolbar.h"
#include "container.h"
#include "headerbar.h"
#include "button.h"

static int nwindows = 0;

static int window_default_width = 650;
static int window_default_height = 550;

static gboolean ui_window_destroy(void *data)  {
    UiObject *obj = data;
    UiEvent ev;
    ev.window = obj->window;
    ev.document = obj->ctx->document;
    ev.obj = obj;
    ev.eventdata = NULL;
    ev.intval = 0;
    
    if(obj->ctx->close_callback) {
        obj->ctx->close_callback(&ev, obj->ctx->close_data);
    }
    
    cxMempoolDestroy(obj->ctx->mp);
    
    nwindows--;
#ifdef UI_GTK2
    if(nwindows == 0) {
        gtk_main_quit();
    }
#endif
    
    return FALSE;
}

void ui_exit_event(GtkWidget *widget, gpointer data) {
    // delay exit handler  
    UiObject *obj = data;
    g_idle_add(ui_window_destroy, data);
}

#if GTK_MAJOR_VERSION >= 4
static gboolean close_request(GtkWindow* self, UiContext *ctx) {
    uic_context_prepare_close(ctx);
    return FALSE;
}
#else
static gboolean close_request(GtkWidget* self, GdkEvent* event, UiContext *ctx) {
    uic_context_prepare_close(ctx);
    return FALSE;
}
#endif

static UiObject* create_window(const char *title, void *window_data, UiBool simple) {
    CxMempool *mp = cxBasicMempoolCreate(256);
    UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject)); 
   
#ifdef UI_LIBADWAITA
    obj->widget = adw_application_window_new(ui_get_application());
#elif !defined(UI_GTK2)
    obj->widget = gtk_application_window_new(ui_get_application());
#else
    obj->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
#endif
    
    
    obj->ctx = uic_context(obj, mp);
    obj->window = window_data;
    
#if GTK_CHECK_VERSION(4, 0, 0)
    obj->ctx->action_map = G_ACTION_MAP(obj->widget);
#endif
    
    if(title != NULL) {
        gtk_window_set_title(GTK_WINDOW(obj->widget), title);
    }
    
    char *width = ui_get_property("ui.window.width");
    char *height = ui_get_property("ui.window.height");
    if(width && height) {
        gtk_window_set_default_size(
                GTK_WINDOW(obj->widget),
                atoi(width),
                atoi(height));
    } else {
        gtk_window_set_default_size(
                GTK_WINDOW(obj->widget),
                window_default_width,
                window_default_height);
    }
    
    g_signal_connect(
            obj->widget,
            "destroy",
            G_CALLBACK(ui_exit_event),
            obj);
#if GTK_MAJOR_VERSION >= 4
    g_signal_connect(
            obj->widget,
            "close-request",
            G_CALLBACK(close_request),
            obj->ctx);
#else
    g_signal_connect(
            obj->widget,
            "delete-event",
            G_CALLBACK(close_request),
            obj->ctx);
#endif
    
    GtkWidget *vbox = ui_gtk_vbox_new(0);
#ifdef UI_LIBADWAITA
    GtkWidget *toolbar_view = adw_toolbar_view_new();
    adw_application_window_set_content(ADW_APPLICATION_WINDOW(obj->widget), toolbar_view);
    adw_toolbar_view_set_content(ADW_TOOLBAR_VIEW(toolbar_view), vbox);

    GtkWidget *headerbar = adw_header_bar_new();
    adw_toolbar_view_add_top_bar(ADW_TOOLBAR_VIEW(toolbar_view), headerbar);
    g_object_set_data(G_OBJECT(obj->widget), "ui_headerbar", headerbar);
    
    if(!simple) {
        ui_fill_headerbar(obj, headerbar);
    }
#elif GTK_MAJOR_VERSION >= 4
    WINDOW_SET_CONTENT(obj->widget, vbox);
#else
    gtk_container_add(GTK_CONTAINER(obj->widget), vbox);
    
    if(!simple) {
        // menu
        if(uic_get_menu_list()) {
            GtkWidget *mb = ui_create_menubar(obj);
            if(mb) {
                gtk_box_pack_start(GTK_BOX(vbox), mb, FALSE, FALSE, 0);
            }
        }

        // toolbar
        if(uic_toolbar_isenabled()) {
            GtkWidget *tb = ui_create_toolbar(obj);
            if(tb) {
                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);
    }
#endif
    
    // window content
    // the content has a (TODO: not yet) configurable frame
    // TODO: really? why
    /*
    GtkWidget *frame = gtk_frame_new(NULL);
    gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
    gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0);
    
    // content vbox
    GtkWidget *content_box = ui_gtk_vbox_new(0);
    gtk_container_add(GTK_CONTAINER(frame), content_box);
    obj->container = ui_box_container(obj, content_box);
    */
    GtkWidget *content_box = ui_gtk_vbox_new(0);
    BOX_ADD_EXPAND(GTK_BOX(vbox), content_box);
    obj->container = ui_box_container(obj, content_box, UI_CONTAINER_VBOX);
    
    nwindows++;
    return obj;
}


UiObject* ui_window(const char *title, void *window_data) {
    return create_window(title, window_data, FALSE);
}

UiObject* ui_simple_window(const char *title, void *window_data) {
    return create_window(title, window_data, TRUE);
}

void ui_window_size(UiObject *obj, int width, int height) {
    gtk_window_set_default_size(
                GTK_WINDOW(obj->widget),
                width,
                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 || args.password) {
        entry = gtk_entry_new();
        if(args.password) {
            gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
        }
        if(args.input_value) {
            ENTRY_SET_TEXT(entry, args.input_value);
        }
        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;
    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*)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);
    }
    
    WINDOW_DESTROY(GTK_WIDGET(self));
}

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);
    }
    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);
        BOX_ADD(content_area, label);
    }
    
    GtkWidget *textfield = NULL;
    if(args.input || args.password) {
        textfield = gtk_entry_new();
        if(args.password) {
            gtk_entry_set_visibility(GTK_ENTRY(textfield), FALSE);
        }
        if(args.input_value) {
            ENTRY_SET_TEXT(textfield, args.input_value);
        }
        BOX_ADD(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);
    
    WINDOW_SHOW(GTK_WIDGET(dialog_w));
}
#endif


#if GTK_MAJOR_VERSION >= 3
UiFileList listmodel2filelist(GListModel *selection) {
    UiFileList flist;
    flist.files = NULL;
    flist.nfiles = 0;
    flist.nfiles = g_list_model_get_n_items(selection);
    flist.files = calloc(flist.nfiles, sizeof(char*));
    for(int i=0;i<flist.nfiles;i++) {
        GFile *file = g_list_model_get_item(selection, i);
        char *path = g_file_get_path(file);
        flist.files[i] = path ? strdup(path) : NULL;
        g_object_unref(file);
    }
    return flist;
}
#endif


#if GTK_CHECK_VERSION(4, 10, 0)

#define UI_GTK_FILEDIALOG_OPEN 16
#define UI_GTK_FILEDIALOG_SAVE 32

static void filechooser_opened(GObject *source, GAsyncResult *result, void *data) {
    UiEventData *event = data;
    
    GFile *file = NULL;
    GListModel *selection = NULL;
    GError *error = NULL;
    
    int mode = event->value;
    int multi = mode & UI_FILEDIALOG_SELECT_MULTI;
    if((mode & UI_FILEDIALOG_SELECT_FOLDER) == UI_FILEDIALOG_SELECT_FOLDER) {
        if(multi) {
            selection = gtk_file_dialog_select_multiple_folders_finish(GTK_FILE_DIALOG(source), result, &error);
        } else {
            file = gtk_file_dialog_select_folder_finish(GTK_FILE_DIALOG(source), result, &error);
        }
    } else if((mode & UI_GTK_FILEDIALOG_OPEN) == UI_GTK_FILEDIALOG_OPEN) {
        if(multi) {
            selection = gtk_file_dialog_open_multiple_finish(GTK_FILE_DIALOG(source), result, &error);
        } else {
            file = gtk_file_dialog_open_finish(GTK_FILE_DIALOG(source), result, &error);
        }
    } else {
        file = gtk_file_dialog_save_finish(GTK_FILE_DIALOG(source), result, &error);
    }
    
    UiEvent evt;
    evt.obj = event->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;
    
    if(selection) {
        flist = listmodel2filelist(selection);
        g_object_unref(selection);
    } else if(file) {
        char *path = g_file_get_path(file);
        if(path) {
            flist.nfiles = 1;
            flist.files = calloc(flist.nfiles, sizeof(char*));
            flist.files[0] = strdup(path);
        }
        g_object_unref(file); 
    }
    
    if(event->callback) {
        event->callback(&evt, event->userdata);
    }
    
    for(int i=0;i<flist.nfiles;i++) {
        free(flist.files[i]);
    }
}

static void ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action, unsigned int mode, const char *name, ui_callback file_selected_callback, void *cbdata) {
    if(action == GTK_FILE_CHOOSER_ACTION_OPEN) {
        mode |= UI_GTK_FILEDIALOG_OPEN;
    } else {
        mode |= UI_GTK_FILEDIALOG_SAVE;
    }
    
    UiEventData *event = malloc(sizeof(UiEventData));
    event->callback = file_selected_callback;
    event->userdata = cbdata;
    event->customdata = NULL;
    event->value = mode;
    event->obj = obj;
    
    GtkWindow *parent = GTK_WINDOW(gtk_widget_get_root(obj->widget));
    GtkFileDialog *dialog = gtk_file_dialog_new();
    if(name) {
        gtk_file_dialog_set_initial_name(dialog, name);
    }
    
    int multi = mode & UI_FILEDIALOG_SELECT_MULTI;
    if((mode & UI_FILEDIALOG_SELECT_FOLDER) == UI_FILEDIALOG_SELECT_FOLDER) {
        if(multi) {
            gtk_file_dialog_select_multiple_folders(dialog, parent, NULL, filechooser_opened, event);
        } else {
            gtk_file_dialog_select_folder(dialog, parent, NULL, filechooser_opened, event);
        }
    } else if(action == GTK_FILE_CHOOSER_ACTION_OPEN) {
        if(multi) {
            gtk_file_dialog_open_multiple(dialog, parent, NULL, filechooser_opened, event);
        } else {
            gtk_file_dialog_open(dialog, parent, NULL, filechooser_opened, event);
        }
    } else {
        gtk_file_dialog_save(dialog, parent, NULL, filechooser_opened, event);
    }
    
    g_object_unref(dialog);
}
#else



static void filechooser_response(GtkDialog* self, gint response_id, UiEventData *data) {
    UiEvent evt;
    evt.obj = data->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;
    
    if(response_id == GTK_RESPONSE_ACCEPT) {
#if GTK_CHECK_VERSION(4, 0, 0)
        GListModel *selection = gtk_file_chooser_get_files(GTK_FILE_CHOOSER(self));
        flist = flist = listmodel2filelist(selection);
        g_object_unref(selection);
#else
        GSList *selection = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(self));
        flist.nfiles = g_slist_length(selection);
        flist.files = calloc(flist.nfiles, sizeof(char*));
        int i = 0;
        while(selection) {
            char *file = selection->data;
            flist.files[i] = strdup(file);
            g_free(file);
            selection = selection->next;
            i++;
        }
        g_slist_free(selection);
#endif
    }
    
    
    if(data->callback) {
        data->callback(&evt, data->userdata);
    }
    
    for(int i=0;i<flist.nfiles;i++) {
        free(flist.files[i]);
    }
    
    WINDOW_DESTROY(GTK_WIDGET(self));
}

static void ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action, unsigned int mode, const char *name, ui_callback file_selected_callback, void *cbdata) {
    char *button;
    char *title;
    
    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 {
        dialog = gtk_file_chooser_dialog_new (
                "Save File",
                GTK_WINDOW(obj->widget),
                action,
                "Cancel",
                GTK_RESPONSE_CANCEL,
                "Save File",
                GTK_RESPONSE_ACCEPT,
                NULL);
    }
    
    if((mode & UI_FILEDIALOG_SELECT_MULTI) == UI_FILEDIALOG_SELECT_MULTI) {
        gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
    }
    
    UiEventData *event = malloc(sizeof(UiEventData));
    event->obj = obj;
    event->userdata = cbdata;
    event->callback = file_selected_callback;
    event->value = 0;
    event->customdata = NULL;
    
    g_signal_connect(
                dialog,
                "response",
                G_CALLBACK(filechooser_response),
                event);
    g_signal_connect(
                dialog,
                "destroy",
                G_CALLBACK(ui_destroy_userdata),
                event);
    
    
    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;
    
    gtk_widget_show(dialog);
}
#endif

void ui_openfiledialog(UiObject *obj, unsigned int mode, ui_callback file_selected_callback, void *cbdata) {
    ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_OPEN, mode, NULL, 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, name, file_selected_callback, cbdata);
}

#if GTK_CHECK_VERSION(4, 10, 0)
#define DIALOG_NEW() gtk_window_new()
#else
#define DIALOG_NEW() gtk_dialog_new()

static void ui_dialogwindow_response(GtkDialog* self, gint response_id, gpointer user_data) {
    UiEventData *event = user_data;
    // TODO: do we need to check if response_id == GTK_RESPONSE_DELETE_EVENT?
    if(event->callback) {
        UiEvent e;
        e.obj = event->obj;
        e.window = event->obj->window;
        e.document = event->obj->ctx->document;
        e.eventdata = NULL;
        e.intval = event->value;
        event->callback(&e, event->userdata);
    }
}

#endif

#if GTK_CHECK_VERSION(4, 0, 0)
#define HEADERBAR_SHOW_CLOSEBUTTON(headerbar, set) gtk_header_bar_set_show_title_buttons(GTK_HEADER_BAR(headerbar), set)
#define DEFAULT_BUTTON(window, button) gtk_window_set_default_widget(GTK_WINDOW(window), button)
#else
#define HEADERBAR_SHOW_CLOSEBUTTON(headerbar, set) gtk_header_bar_set_show_close_button(GTK_HEADER_BAR(headerbar), set)
#define DEFAULT_BUTTON(window, button) gtk_widget_set_can_default(button, TRUE); gtk_window_set_default(GTK_WINDOW(window), button)
#endif



UiObject* ui_dialog_window_create(UiObject *parent, UiDialogWindowArgs args) {
    GtkWidget *dialog = DIALOG_NEW();
    if(args.width > 0 || args.height > 0) {
        gtk_window_set_default_size(
                GTK_WINDOW(dialog),
                args.width,
                args.height);
    }
    
    
    gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent->widget));
    if(args.modal != UI_OFF) {
        gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
    }
    
    CxMempool *mp = cxBasicMempoolCreate(256);
    UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject)); 
    obj->ctx = uic_context(obj, mp);
    obj->widget = dialog;
    nwindows++;
    
    if(args.title != NULL) {
        gtk_window_set_title(GTK_WINDOW(dialog), args.title);
    }
    
#if ! GTK_CHECK_VERSION(4, 10, 0)
    UiEventData *event = malloc(sizeof(UiEventData));
    event->obj = obj;
    event->userdata = args.onclickdata;
    event->callback = args.onclick;
    event->value = 0;
    event->customdata = NULL;

    g_signal_connect(dialog, "response", G_CALLBACK(ui_dialogwindow_response), event);
    g_signal_connect(
            dialog,
            "destroy",
            G_CALLBACK(ui_destroy_userdata),
            event);
#endif
    
    g_signal_connect(
            dialog,
            "destroy",
            G_CALLBACK(ui_exit_event),
            obj);
    
#if GTK_MAJOR_VERSION < 4
    GtkWidget *c = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
    gtk_container_remove(GTK_CONTAINER(dialog), c);
#endif
    
    GtkWidget *content_vbox = ui_gtk_vbox_new(0);
    obj->container = ui_box_container(obj, content_vbox, UI_CONTAINER_VBOX);
    if(args.lbutton1 || args.lbutton2 || args.rbutton3 || args.rbutton4) {
#if GTK_CHECK_VERSION(3, 10, 0)
        if(args.titlebar_buttons != UI_OFF) {
            GtkWidget *headerbar = gtk_header_bar_new();
            gtk_window_set_titlebar(GTK_WINDOW(dialog), headerbar);
            if(args.show_closebutton == UI_OFF) {
                HEADERBAR_SHOW_CLOSEBUTTON(headerbar, FALSE);
            }
            
            if(args.lbutton1) {
                GtkWidget *button = ui_create_button(obj, args.lbutton1, NULL, args.onclick, args.onclickdata, 1, args.default_button == 1);
                gtk_header_bar_pack_start(GTK_HEADER_BAR(headerbar), button);
                if(args.default_button == 1) {
                    WIDGET_ADD_CSS_CLASS(button, "suggested-action");
                    DEFAULT_BUTTON(dialog, button);
                }
            }
            if(args.lbutton2) {
                GtkWidget *button = ui_create_button(obj, args.lbutton2, NULL, args.onclick, args.onclickdata, 2, args.default_button == 2);
                gtk_header_bar_pack_start(GTK_HEADER_BAR(headerbar), button);
                if(args.default_button == 2) {
                    WIDGET_ADD_CSS_CLASS(button, "suggested-action");
                    DEFAULT_BUTTON(dialog, button);
                }
            }
            
            if(args.rbutton4) {
                GtkWidget *button = ui_create_button(obj, args.rbutton4, NULL, args.onclick, args.onclickdata, 4, args.default_button == 4);
                gtk_header_bar_pack_end(GTK_HEADER_BAR(headerbar), button);
                if(args.default_button == 4) {
                    WIDGET_ADD_CSS_CLASS(button, "suggested-action");
                    DEFAULT_BUTTON(dialog, button);
                }
            }
            if(args.rbutton3) {
                GtkWidget *button = ui_create_button(obj, args.rbutton3, NULL, args.onclick, args.onclickdata, 3, args.default_button == 3);
                gtk_header_bar_pack_end(GTK_HEADER_BAR(headerbar), button);
                if(args.default_button == 3) {
                    WIDGET_ADD_CSS_CLASS(button, "suggested-action");
                    DEFAULT_BUTTON(dialog, button);
                }
            }
            WINDOW_SET_CONTENT(obj->widget, content_vbox);
            return obj;
        }
#endif
        GtkWidget *vbox = ui_gtk_vbox_new(0);
        WINDOW_SET_CONTENT(obj->widget, vbox);
        
        GtkWidget *separator = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
        
        GtkWidget *grid = ui_create_grid_widget(10, 10);
        GtkWidget *widget = ui_box_set_margin(grid, 16);
        gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE); 
        
        if(args.lbutton1) {
            GtkWidget *button = ui_create_button(obj, args.lbutton1, NULL, args.onclick, args.onclickdata, 1, args.default_button == 1);
            gtk_grid_attach(GTK_GRID(grid), button, 0, 0, 1, 1);
            if(args.default_button == 1) {
                WIDGET_ADD_CSS_CLASS(button, "suggested-action");
                DEFAULT_BUTTON(dialog, button);
            }
        }
        if(args.lbutton2) {
            GtkWidget *button = ui_create_button(obj, args.lbutton2, NULL, args.onclick, args.onclickdata, 2, args.default_button == 2);
            gtk_grid_attach(GTK_GRID(grid), button, 1, 0, 1, 1);
            if(args.default_button == 2) {
                WIDGET_ADD_CSS_CLASS(button, "suggested-action");
                DEFAULT_BUTTON(dialog, button);
            }
        }
        GtkWidget *space = gtk_label_new(NULL);
        gtk_widget_set_hexpand(space, TRUE);
        gtk_grid_attach(GTK_GRID(grid), space, 2, 0, 1, 1);
        if(args.rbutton3) {
            GtkWidget *button = ui_create_button(obj, args.rbutton3, NULL, args.onclick, args.onclickdata, 3, args.default_button == 3);
            gtk_grid_attach(GTK_GRID(grid), button, 3, 0, 1, 1);
            if(args.default_button == 3) {
                WIDGET_ADD_CSS_CLASS(button, "suggested-action");
                DEFAULT_BUTTON(dialog, button);
            }
        }
        if(args.rbutton4) {
            GtkWidget *button = ui_create_button(obj, args.rbutton4, NULL, args.onclick, args.onclickdata, 4, args.default_button == 4);
            gtk_grid_attach(GTK_GRID(grid), button, 4, 0, 1, 1);
            if(args.default_button == 4) {
                WIDGET_ADD_CSS_CLASS(button, "suggested-action");
                DEFAULT_BUTTON(dialog, button);
            }
        }
        
        BOX_ADD_EXPAND(vbox, content_vbox);   
        BOX_ADD_NO_EXPAND(vbox, separator);
        BOX_ADD_NO_EXPAND(vbox, widget);
    } else {
        WINDOW_SET_CONTENT(obj->widget, content_vbox);
    }
    
    return obj;
}

mercurial