ui/gtk/entry.c

Mon, 29 Jan 2024 12:15:19 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 29 Jan 2024 12:15:19 +0100
branch
newapi
changeset 236
ee794586f51b
parent 174
0358f1d9c506
child 264
24d9a92fd048
permissions
-rw-r--r--

add getvalue func for menu item lists (WinUI3)

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


UIWIDGET ui_spinner(UiObject *obj, int step, UiInteger *i) {
    UiVar *var = malloc(sizeof(UiVar));
    var->value = i;
    var->type = UI_VAR_SPECIAL;
    return ui_spinner_var(obj, step, 0, var, UI_VAR_INTEGER);
}

UIWIDGET ui_spinnerf(UiObject *obj, double step, int digits, UiDouble *d) {
    UiVar *var = malloc(sizeof(UiVar));
    var->value = d;
    var->type = UI_VAR_SPECIAL;
    return ui_spinner_var(obj, step, digits, var, UI_VAR_DOUBLE);
}

UIWIDGET ui_spinnerr(UiObject *obj, UiRange *r) {
    UiVar *var = malloc(sizeof(UiVar));
    var->value = r;
    var->type = UI_VAR_SPECIAL;
    return ui_spinner_var(obj, r->extent, 1, var, UI_VAR_RANGE);
}

UIWIDGET ui_spinner_nv(UiObject *obj, int step, char *varname) {
    UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_INTEGER);
    return ui_spinner_var(obj, step, 0, var, UI_VAR_INTEGER);
}

UIWIDGET ui_spinnerf_nv(UiObject *obj, double step, int digits, char *varname) {
    UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_DOUBLE);
    return ui_spinner_var(obj, step, digits, var, UI_VAR_DOUBLE);
}

UIWIDGET ui_spinnerr_nv(UiObject *obj, char *varname) {
    UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_RANGE);
    UiRange *r = var->value;
    return ui_spinner_var(obj, r->extent, 1, var, UI_VAR_RANGE);
}

UIWIDGET ui_spinner_var(UiObject *obj, double step, int digits, UiVar *var, UiVarType type) {
    double min = 0;
    double max = 1000;
    if(type == UI_VAR_RANGE) {
        UiRange *r = var->value;
        min = r->min;
        max = r->max;
    }
    if(step == 0) {
        step = 1;
    }
#ifdef UI_GTK2LEGACY
    if(min == max) {
        max = min + 1;
    }
#endif
    GtkWidget *spin = gtk_spin_button_new_with_range(min, max, step);
    gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
    if(var) {
        double value = 0;
        UiObserver **obs = NULL;
        switch(type) {
            default: break;
            case UI_VAR_INTEGER: {
                UiInteger *i = var->value;
                i->get = ui_spinbutton_getint;
                i->set = ui_spinbutton_setint;
                i->obj = spin;
                value = (double)i->value;
                obs = &i->observers;
                break;
            }
            case UI_VAR_DOUBLE: {
                UiDouble *d = var->value;
                d->get = ui_spinbutton_getdouble;
                d->set = ui_spinbutton_setdouble;
                d->obj = spin;
                value = d->value;
                obs = &d->observers;
                break;
            }
            case UI_VAR_RANGE: {
                UiRange *r = var->value;
                r->get = ui_spinbutton_getrangeval;
                r->set = ui_spinbutton_setrangeval;
                r->setrange = ui_spinbutton_setrange;
                r->setextent = ui_spinbutton_setextent;
                r->obj = spin;
                value = r->value;
                obs = &r->observers;
                break;
            }
        }
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
        
        UiVarEventData *event = malloc(sizeof(UiVarEventData));
        event->obj = obj;
        event->var = var;
        event->observers = obs;
        
        g_signal_connect(
                spin,
                "value-changed",
                G_CALLBACK(ui_spinner_changed),
                event);
        g_signal_connect(
                spin,
                "destroy",
                G_CALLBACK(ui_destroy_vardata),
                event);
    }
    
    UiContainer *ct = uic_get_current_container(obj);
    ct->add(ct, spin, FALSE);
    
    return spin;
}

void ui_spinner_setrange(UIWIDGET spinner, double min, double max) {
    gtk_spin_button_set_range(GTK_SPIN_BUTTON(spinner), min, max);
}

void ui_spinner_setdigits(UIWIDGET spinner, int digits) {
    gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spinner), digits);
}


void ui_spinner_changed(GtkSpinButton *spinner, UiVarEventData *event) {
    UiEvent e;
    e.obj = event->obj;
    e.window = event->obj->window;
    e.document = event->obj->ctx->document;
    e.eventdata = event->var->value;
    e.intval = 0;
    
    UiObserver *observer = *event->observers;
    ui_notify_evt(observer, &e);
}


int64_t ui_spinbutton_getint(UiInteger *i) {
    i->value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(i->obj));
    return i->value;
}

void ui_spinbutton_setint(UiInteger *i, int64_t val) {
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(i->obj), (double)val);
    i->value = val;
}

double ui_spinbutton_getdouble(UiDouble *d) {
    d->value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(d->obj));
    return d->value;
}

void ui_spinbutton_setdouble(UiDouble *d, double val) {
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(d->obj), val);
    d->value = val;
}

double ui_spinbutton_getrangeval(UiRange *r) {
    r->value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(r->obj));
    return r->value;
}

void ui_spinbutton_setrangeval(UiRange *r, double val) {
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(r->obj), val);
    r->value = val;
}
void ui_spinbutton_setrange(UiRange *r, double min, double max) {
    gtk_spin_button_set_range(GTK_SPIN_BUTTON(r->obj), min, max);
    r->min = min;
    r->max = max;
}

void ui_spinbutton_setextent(UiRange *r, double extent) {
    gtk_spin_button_set_increments(GTK_SPIN_BUTTON(r->obj), extent, extent*10);
    r->extent = extent;
}

mercurial