ui/gtk/window.c

Fri, 14 Jun 2024 18:25:43 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 14 Jun 2024 18:25:43 +0200
branch
newapi
changeset 291
302a150ff5e4
parent 281
2533cdebf6ef
child 296
a362c76dbf99
permissions
-rw-r--r--

implement ui_list_getselection (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"

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)); 
    
#ifndef 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(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);
    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);
    }
    
    // window content
    // the content has a (TODO: not yet) configurable frame
    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);
    
    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*)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;
    
    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);
}

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