ui/gtk/display.c

Sun, 29 Sep 2024 18:51:03 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 29 Sep 2024 18:51:03 +0200
branch
newapi
changeset 315
144c2b4683cb
parent 298
c5e207d01ff2
child 319
050ceb8ecf44
permissions
-rw-r--r--

use GtkFileDialog on newer gtk4 versions

/*
 * 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 "display.h"
#include "container.h"
#include "../common/context.h"
#include "../common/object.h"
#include "../ui/display.h"

static void set_alignment(GtkWidget *widget, float xalign, float yalign) {
#if GTK_MAJOR_VERSION >= 4 || (GTK_MAJOR_VERSION >= 3 && GTK_MINOR_VERSION >= 16)
    gtk_label_set_xalign(GTK_LABEL(widget), xalign);
    gtk_label_set_yalign(GTK_LABEL(widget), yalign);
#else
    gtk_misc_set_alignment(GTK_MISC(widget), xalign, yalign);
#endif
}

UIWIDGET ui_label_create(UiObject *obj, UiLabelArgs args) {
    UiObject* current = uic_current_obj(obj);
    
    GtkWidget *widget = gtk_label_new(args.label);
    switch(args.align) {
        case UI_ALIGN_DEFAULT: break;
        case UI_ALIGN_LEFT: set_alignment(widget, 0, .5); break;
        case UI_ALIGN_RIGHT: set_alignment(widget, 1, .5); break;
        case UI_ALIGN_CENTER: break; // TODO
    }
    
    UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_STRING);
    if(var) {
        UiString* value = (UiString*)var->value;
        value->obj = widget;
        value->get = ui_label_get;
        value->set = ui_label_set;
    }
    
    UI_APPLY_LAYOUT1(current, args);
    current->container->add(current->container, widget, FALSE);
    
    return widget;
}

UIWIDGET ui_llabel_create(UiObject* obj, UiLabelArgs args) {
    args.align = UI_ALIGN_LEFT;
    return ui_label_create(obj, args);
}

UIWIDGET ui_rlabel_create(UiObject* obj, UiLabelArgs args) {
    args.align = UI_ALIGN_RIGHT;
    return ui_label_create(obj, args);
}

char* ui_label_get(UiString *s) {
    if(s->value.ptr) {
        s->value.free(s->value.ptr);
    }
    s->value.ptr = g_strdup(gtk_label_get_text(GTK_LABEL(s->obj)));
    s->value.free = (ui_freefunc)g_free;
    return s->value.ptr;
}

void ui_label_set(UiString *s, const char *value) {
    gtk_label_set_text(GTK_LABEL(s->obj), value);
    if(s->value.ptr) {
        s->value.free(s->value.ptr);
        s->value.ptr = NULL;
        s->value.free = NULL;
    }
}

UIWIDGET ui_space_deprecated(UiObject *obj) {
    GtkWidget *widget = gtk_label_new("");
    UiContainer *ct = uic_get_current_container(obj);
    ct->add(ct, widget, TRUE);
    
    return widget;
}

UIWIDGET ui_separator_deprecated(UiObject *obj) {
#if GTK_MAJOR_VERSION >= 3
    GtkWidget *widget = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
#else
    GtkWidget *widget = gtk_hseparator_new();
#endif
    UiContainer *ct = uic_get_current_container(obj);
    ct->add(ct, widget, FALSE);
    
    return widget;
}

/* ------------------------- progress bar ------------------------- */

UIWIDGET ui_progressbar_create(UiObject *obj, UiProgressbarArgs args) {
    UiObject* current = uic_current_obj(obj);
    
    GtkWidget *progressbar = gtk_progress_bar_new();
    
    UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_DOUBLE);
    if(var && var->value) {
        UiDouble *value = var->value;
        value->get = ui_progressbar_get;
        value->set = ui_progressbar_set;
        value->obj = progressbar;
        gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar), 0.5);
    }
    
    UI_APPLY_LAYOUT1(current, args);
    current->container->add(current->container, progressbar, FALSE);
    
    return progressbar;
}

double ui_progressbar_get(UiDouble *d) {
    d->value = gtk_progress_bar_get_fraction(GTK_PROGRESS_BAR(d->obj));
    return d->value;
}

void ui_progressbar_set(UiDouble *d, double value) {
    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(d->obj), value);
    d->value = value;
}


/* ------------------------- progress spinner ------------------------- */

UIWIDGET ui_progressspinner_create(UiObject* obj, UiProgressbarSpinnerArgs args) {
    UiObject* current = uic_current_obj(obj);
    
    GtkWidget *spinner = gtk_spinner_new();
    
    UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_INTEGER);
    if(var && var->value) {
        UiInteger *value = var->value;
        value->get = ui_spinner_get;
        value->set = ui_spinner_set;
        value->obj = spinner;
        ui_spinner_set(value, value->value);
    }
    
    UI_APPLY_LAYOUT1(current, args);
    current->container->add(current->container, spinner, FALSE);
    
    return spinner;
}

int64_t ui_spinner_get(UiInteger *i) {
    return i->value;
}

void ui_spinner_set(UiInteger *i, int64_t value) {
    i->value = value;
    if(i->obj) {
        GtkSpinner *spinner = GTK_SPINNER(i->obj);
        if(value != 0) {
            gtk_spinner_start(spinner);
        } else {
            gtk_spinner_stop(spinner);
        }
    }
}

mercurial