# HG changeset patch # User Olaf Wintermann # Date 1708029075 -3600 # Node ID b39f0e61fd99e6b69059e78f43779f48be42bf86 # Parent eebb0626d02051559375f8acaab06bffb5d7433a add new label (GTK) diff -r eebb0626d020 -r b39f0e61fd99 application/main.c --- a/application/main.c Thu Feb 15 21:12:54 2024 +0100 +++ b/application/main.c Thu Feb 15 21:31:15 2024 +0100 @@ -76,7 +76,10 @@ ui_button(obj, .label = "cell2"); ui_newline(obj); ui_button(obj, .label = "cell_colspan2", .colspan = 2); + ui_newline(obj); + ui_label(obj, .label = "Label Col 1", .align = UI_ALIGN_LEFT); + ui_label(obj, .label = "Label Col 2", .align = UI_ALIGN_RIGHT); } ui_show(obj); diff -r eebb0626d020 -r b39f0e61fd99 ui/gtk/button.c --- a/ui/gtk/button.c Thu Feb 15 21:12:54 2024 +0100 +++ b/ui/gtk/button.c Thu Feb 15 21:31:15 2024 +0100 @@ -131,13 +131,7 @@ } button_set_icon(widget, args.icon); - UiVar* var = NULL; - if (args.value) { - var = uic_create_value_var(current->ctx, args.value); - } - else if (args.varname) { - var = uic_create_var(obj->ctx, args.varname, UI_VAR_INTEGER); - } + UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_INTEGER); if (var) { UiInteger* value = (UiInteger*)var->value; value->obj = widget; diff -r eebb0626d020 -r b39f0e61fd99 ui/gtk/display.c --- a/ui/gtk/display.c Thu Feb 15 21:12:54 2024 +0100 +++ b/ui/gtk/display.c Thu Feb 15 21:31:15 2024 +0100 @@ -33,6 +33,7 @@ #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 >= 3 && GTK_MINOR_VERSION >= 16 @@ -43,30 +44,60 @@ #endif } -UIWIDGET ui_label(UiObject *obj, char *label) { - GtkWidget *widget = gtk_label_new(label); +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 + } - UiContainer *ct = uic_get_current_container(obj); - ct->add(ct, widget, FALSE); + 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(UiObject *obj, char *label) { - UIWIDGET widget = ui_label(obj, label); - set_alignment(widget, 0, .5); - 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); } -UIWIDGET ui_rlabel(UiObject *obj, char *label) { - UIWIDGET widget = ui_label(obj, label); - //gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_RIGHT); - - set_alignment(widget, 1, .5); - return widget; +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; } -UIWIDGET ui_space(UiObject *obj) { +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); @@ -88,7 +119,7 @@ /* ------------------------- progress bar ------------------------- */ -UIWIDGET ui_progressbar(UiObject *obj, UiDouble *value) { +UIWIDGET ui_progressbar_deprecated(UiObject *obj, UiDouble *value) { UiVar *var = malloc(sizeof(UiVar)); var->value = value; var->type = UI_VAR_SPECIAL; diff -r eebb0626d020 -r b39f0e61fd99 ui/gtk/display.h --- a/ui/gtk/display.h Thu Feb 15 21:12:54 2024 +0100 +++ b/ui/gtk/display.h Thu Feb 15 21:31:15 2024 +0100 @@ -36,6 +36,9 @@ extern "C" { #endif +char* ui_label_get(UiString *s); +void ui_label_set(UiString *s, const char *value); + UIWIDGET ui_progressbar_var(UiObject *obj, UiVar *var); double ui_progressbar_get(UiDouble *d); void ui_progressbar_set(UiDouble *d, double value); diff -r eebb0626d020 -r b39f0e61fd99 ui/ui/display.h --- a/ui/ui/display.h Thu Feb 15 21:12:54 2024 +0100 +++ b/ui/ui/display.h Thu Feb 15 21:31:15 2024 +0100 @@ -97,8 +97,8 @@ UIEXPORT UIWIDGET ui_llabel_create(UiObject* obj, UiLabelArgs args); UIEXPORT UIWIDGET ui_rlabel_create(UiObject* obj, UiLabelArgs args); -UIWIDGET ui_space(UiObject *obj); -UIWIDGET ui_separator(UiObject *obj); +UIWIDGET ui_space_deprecated(UiObject *obj); +UIWIDGET ui_separator_deprecated(UiObject *obj); /* progress bar/spinner */