ui/gtk/window.c

Sun, 29 Sep 2024 15:01:14 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 29 Sep 2024 15:01:14 +0200
branch
newapi
changeset 313
b679cc6059ab
parent 311
450a813dc2a5
child 314
d96ba820083a
permissions
-rw-r--r--

add option to name widgets and add css classes (GTK)

/*
 * 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"

static int nwindows = 0;

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

void ui_exit_event(GtkWidget *widget, gpointer 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);
    }
    // TODO: free UiObject
    
    nwindows--;
#ifdef UI_GTK2
    if(nwindows == 0) {
        gtk_main_quit();
    }
#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);
    
    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);
    
    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(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);
}

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());
    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) {
        textfield = gtk_entry_new();
        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));
}


#if GTK_MAJOR_VERSION >= 4
static void ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action, unsigned int mode, ui_callback file_selected_callback, void *cbdata) {
    // TODO
}
#else
static void ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action, unsigned int mode, 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);
    }
    
    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);
}
#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, 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