ui/gtk/display.c

Thu, 14 Nov 2024 17:25:23 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 14 Nov 2024 17:25:23 +0100
changeset 83
a612adaee43d
parent 60
ee4e4742391e
permissions
-rw-r--r--

fix pathbar elmfunc creating an element for an empty string

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

#include <cx/printf.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);
    
    const char *css_class = NULL;
    char *markup = NULL;
    if(args.label) {
        #if GTK_MAJOR_VERSION < 3
            switch(args.style) {
                case UI_LABEL_STYLE_DEFAULT: break;
                case UI_LABEL_STYLE_TITLE: {
                    cxmutstr m = cx_asprintf("<b>%s</b>", args.label);
                    markup = m.ptr;
                    args.label = NULL;
                }
                case UI_LABEL_STYLE_SUBTITLE: {
                    break;
                }
                case UI_LABEL_STYLE_DIM: {
                    break;
                }
            }
#       else
            switch(args.style) {
                case UI_LABEL_STYLE_DEFAULT: break;
                case UI_LABEL_STYLE_TITLE: {
                    css_class = "ui_label_title";
                    break;
                }
                case UI_LABEL_STYLE_SUBTITLE: {
                    css_class = "subtitle";
                    break;
                }
                case UI_LABEL_STYLE_DIM: {
                    css_class = "dim-label";
                    break;
                }
            }
#       endif
    }

    
    GtkWidget *widget = gtk_label_new(args.label);
    if(markup) {
        gtk_label_set_markup(GTK_LABEL(widget), markup);
        free(markup);
    }
    
    if(css_class) {
        WIDGET_ADD_CSS_CLASS(widget, css_class);
    }
    
    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 ------------------------- */

typedef struct UiProgressBarRange {
    double min;
    double max;
} UiProgressBarRange;

UIWIDGET ui_progressbar_create(UiObject *obj, UiProgressbarArgs args) {
    UiObject* current = uic_current_obj(obj);
    
    GtkWidget *progressbar = gtk_progress_bar_new();
    if(args.max > args.min) {
        UiProgressBarRange *range = malloc(sizeof(UiProgressBarRange));
        range->min = args.min;
        range->max = args.max;
        g_signal_connect(
                progressbar,
                "destroy",
                G_CALLBACK(ui_destroy_userdata),
                range);
        g_object_set_data(G_OBJECT(progressbar), "ui_range", range);
    }
    
    
    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;
        ui_progressbar_set(value, value->value);
    }
    
    UI_APPLY_LAYOUT1(current, args);
    current->container->add(current->container, progressbar, FALSE);
    
    return progressbar;
}

double ui_progressbar_get(UiDouble *d) {
    UiProgressBarRange *range = g_object_get_data(d->obj, "ui_range");
    double fraction = gtk_progress_bar_get_fraction(GTK_PROGRESS_BAR(d->obj));
    if(range) {
        fraction = range->min + (range->max - range->min) * fraction;
    }
    d->value = fraction;
    return d->value;
}

void ui_progressbar_set(UiDouble *d, double value) {
    d->value = value;
    UiProgressBarRange *range = g_object_get_data(d->obj, "ui_range");
    if(range) {
        value = (value - range->min) / (range->max - range->min);
    }
    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(d->obj), 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