ui/gtk/container.c

Sun, 19 Oct 2025 21:20:08 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 19 Oct 2025 21:20:08 +0200
changeset 112
c3f2f16fa4b8
parent 110
c00e968d018b
permissions
-rw-r--r--

update toolkit

/*
 * 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 <limits.h>

#include "container.h"
#include "toolkit.h"
#include "headerbar.h"

#include "../common/context.h"
#include "../common/object.h"
#include "../common/container.h"

#include "../ui/properties.h"


void ui_container_begin_close(UiObject *obj) {
    UiContainerX *ct = obj->container_end;
    ct->close = 1;
}

int ui_container_finish(UiObject *obj) {
    UiContainerX *ct = obj->container_end;
    if(ct->close) {
        ui_end_new(obj);
        return 0;
    }
    return 1;
}

GtkWidget* ui_gtk_vbox_new(int spacing) {
#if GTK_MAJOR_VERSION >= 3
    return gtk_box_new(GTK_ORIENTATION_VERTICAL, spacing);
#else
    return gtk_vbox_new(FALSE, spacing);
#endif
}

GtkWidget* ui_gtk_hbox_new(int spacing) {
#if GTK_MAJOR_VERSION >= 3
    return gtk_box_new(GTK_ORIENTATION_HORIZONTAL, spacing);
#else
    return gtk_hbox_new(FALSE, spacing);
#endif
}

GtkWidget* ui_subcontainer_create(
        UiSubContainerType type,
        UiObject *obj,
        int spacing,
        int columnspacing,
        int rowspacing,
        int margin)
{
    GtkWidget *sub = NULL;
    GtkWidget *add = NULL;
    UiContainerX *container = NULL;
    switch(type) {
        default: {
            sub = ui_gtk_vbox_new(spacing);
            add = ui_gtk_set_margin(sub, margin, 0, 0, 0, 0);
            container = ui_box_container(obj, sub, type);
            break;
        }
        case UI_CONTAINER_HBOX: {
            sub = ui_gtk_hbox_new(spacing);
            add = ui_gtk_set_margin(sub, margin, 0, 0, 0, 0);
            container = ui_box_container(obj, sub, type);
            break;
        }
        case UI_CONTAINER_GRID: {
            sub = ui_create_grid_widget(columnspacing, rowspacing);
            add = ui_gtk_set_margin(sub, margin, 0, 0, 0, 0);
            container = ui_grid_container(obj, sub, FALSE, FALSE, FALSE, FALSE);
            break;
        }
        case UI_CONTAINER_NO_SUB: {
            break;
        }
    }
    if(container) {
        uic_object_push_container(obj, container);
    }
    return add;
}


/* -------------------- Box Container -------------------- */
UiContainerX* ui_box_container(UiObject *obj, GtkWidget *box, UiSubContainerType type) {
    UiBoxContainer *ct = cxCalloc(
            obj->ctx->allocator,
            1,
            sizeof(UiBoxContainer));
    ct->container.widget = box;
    ct->container.add = ui_box_container_add;
    ct->type = type;
    return (UiContainerX*)ct;
}

void ui_box_container_add(UiContainerPrivate *ct, GtkWidget *widget, UiLayout *layout) {
    UiBoxContainer *bc = (UiBoxContainer*)ct;
    widget = ui_gtk_set_margin(widget, layout->margin, layout->margin_left, layout->margin_right, layout->margin_top, layout->margin_bottom);
    
    UiBool fill = layout->fill;
    if(bc->has_fill && fill) {
        fprintf(stderr, "UiError: container has 2 filled widgets");
        fill = FALSE;
    }
    if(fill) {
        bc->has_fill = TRUE;
    }
    
    UiBool expand = fill;
#if GTK_MAJOR_VERSION >= 4
    gtk_box_append(GTK_BOX(ct->widget), widget);
    GtkAlign align = expand ? GTK_ALIGN_FILL : GTK_ALIGN_START; 
    if(bc->type == UI_CONTAINER_VBOX) {
        gtk_widget_set_valign(widget, align);
        gtk_widget_set_vexpand(widget, expand);
        gtk_widget_set_hexpand(widget, TRUE);
    } else if(bc->type == UI_CONTAINER_HBOX) {
        gtk_widget_set_halign(widget, align);
        gtk_widget_set_hexpand(widget, expand);
        gtk_widget_set_vexpand(widget, TRUE);
    }
    
#else
    gtk_box_pack_start(GTK_BOX(ct->widget), widget, expand, fill, 0);
#endif
    
    ct->current = widget;
}

UiContainerX* ui_grid_container(
        UiObject *obj,
        GtkWidget *grid,
        UiBool def_hexpand,
        UiBool def_vexpand,
        UiBool def_hfill,
        UiBool def_vfill)
{
    UiGridContainer *ct = cxCalloc(
            obj->ctx->allocator,
            1,
            sizeof(UiGridContainer));
    ct->def_hexpand = def_hexpand;
    ct->def_vexpand = def_vexpand;
    ct->def_hfill = def_hfill;
    ct->def_vfill = def_vfill;
    ct->container.widget = grid;
    ct->container.add = ui_grid_container_add;
    UI_GTK_V2(ct->width = 0);
    UI_GTK_V2(ct->height = 1);
    return (UiContainerX*)ct;
}


#if GTK_MAJOR_VERSION >= 3
void ui_grid_container_add(UiContainerPrivate *ct, GtkWidget *widget, UiLayout *layout) {
    UiGridContainer *grid = (UiGridContainer*)ct;
    widget = ui_gtk_set_margin(widget, layout->margin, layout->margin_left, layout->margin_right, layout->margin_top, layout->margin_bottom);
    
    if(ct->container.newline) {
        grid->x = 0;
        grid->y++;
        ct->container.newline = FALSE;
    }
    
    uic_layout_setup_expand_fill(layout, grid->def_hexpand, grid->def_vexpand, grid->def_hfill, grid->def_vfill);
    
    if(!layout->hfill) {
        gtk_widget_set_halign(widget, GTK_ALIGN_START);
    }
    if(!layout->vfill) {
        gtk_widget_set_valign(widget, GTK_ALIGN_START);
    }
    
    gtk_widget_set_hexpand(widget, layout->hexpand);
    gtk_widget_set_vexpand(widget, layout->vexpand);
    
    int colspan = layout->colspan > 0 ? layout->colspan : 1;
    int rowspan = layout->rowspan > 0 ? layout->rowspan : 1;
    
    gtk_grid_attach(GTK_GRID(ct->widget), widget, grid->x, grid->y, colspan, rowspan);
    grid->x += colspan;
    
    grid->container.current = widget;
}
#endif
#ifdef UI_GTK2
void ui_grid_container_add(UiContainerPrivate *ct, GtkWidget *widget) {
    UiGridContainer *grid = (UiGridContainer*)ct;
    widget = ui_gtk_set_margin(widget, layout->margin, layout->margin_left, layout->margin_right, layout->margin_top, layout->margin_bottom);
    
    if(ct->container.newline) {
        grid->x = 0;
        grid->y++;
        ct->container.newline = FALSE;
    }
    
    uic_layout_setup_expand_fill(layout, grid->def_hexpand, grid->def_vexpand, grid->def_hfill, grid->def_vfill);
    
    GtkAttachOptions xoptions = 0;
    GtkAttachOptions yoptions = 0;
    if(layout->hexpand) {
        xoptions = GTK_EXPAND;
    }
    if(layout->hfill) {
        xoptions |= GTK_FILL;
    }
    if(layout->vexpand) {
        yoptions = GTK_EXPAND;
    }
    if(layout->vfill) {
        yoptions |= GTK_FILL;
    }
    
    int colspan = layout->colspan > 0 ? layout->colspan : 1;
    int rowspan = layout->rowspan > 0 ? layout->rowspan : 1;
    // TODO: use colspan/rowspan
    
    gtk_table_attach(GTK_TABLE(ct->widget), widget, grid->x, grid->x+1, grid->y, grid->y+1, xoptions, yoptions, 0, 0);
    grid->x++;
    int nw = grid->x > grid->width ? grid->x : grid->width;
    if(grid->x > grid->width || grid->y > grid->height) {
        grid->width = nw;
        grid->height = grid->y + 1;
        gtk_table_resize(GTK_TABLE(ct->widget), grid->width, grid->height);
    }
    
    ct->current = widget;
}
#endif

UiContainerX* ui_frame_container(UiObject *obj, GtkWidget *frame) {
    UiContainerPrivate *ct = cxCalloc(
            obj->ctx->allocator,
            1,
            sizeof(UiContainerPrivate));
    ct->widget = frame;
    ct->add = ui_frame_container_add;
    return (UiContainerX*)ct;
}

void ui_frame_container_add(UiContainerPrivate *ct, GtkWidget *widget, UiLayout *layout) {
    widget = ui_gtk_set_margin(widget, layout->margin, layout->margin_left, layout->margin_right, layout->margin_top, layout->margin_bottom);
    FRAME_SET_CHILD(ct->widget, widget);
    ct->current = widget;
}

UiContainerX* ui_expander_container(UiObject *obj, GtkWidget *expander) {
    UiContainerPrivate *ct = cxCalloc(
            obj->ctx->allocator,
            1,
            sizeof(UiContainerPrivate));
    ct->widget = expander;
    ct->add = ui_expander_container_add;
    return (UiContainerX*)ct;
}

void ui_expander_container_add(UiContainerPrivate *ct, GtkWidget *widget, UiLayout *layout) {
    widget = ui_gtk_set_margin(widget, layout->margin, layout->margin_left, layout->margin_right, layout->margin_top, layout->margin_bottom);
    EXPANDER_SET_CHILD(ct->widget, widget);
    ct->current = widget;
}

void ui_scrolledwindow_container_add(UiContainerPrivate *ct, GtkWidget *widget, UiLayout *layout) {
    widget = ui_gtk_set_margin(widget, layout->margin, layout->margin_left, layout->margin_right, layout->margin_top, layout->margin_bottom);
    // TODO: check if the widget implements GtkScrollable
    SCROLLEDWINDOW_SET_CHILD(ct->widget, widget);
    ct->current = widget;
}

UiContainerX* ui_scrolledwindow_container(UiObject *obj, GtkWidget *scrolledwindow) {
    UiContainerPrivate *ct = cxCalloc(
            obj->ctx->allocator,
            1,
            sizeof(UiContainerPrivate));
    ct->widget = scrolledwindow;
    ct->add = ui_scrolledwindow_container_add;
    return (UiContainerX*)ct;
}

UiContainerX* ui_tabview_container(UiObject *obj, GtkWidget *tabview) {
    UiTabViewContainer *ct = cxCalloc(
            obj->ctx->allocator,
            1,
            sizeof(UiTabViewContainer));
    ct->container.widget = tabview;
    ct->container.add = ui_tabview_container_add;
    return (UiContainerX*)ct;
}

void ui_tabview_container_add(UiContainerPrivate *ct, GtkWidget *widget, UiLayout *layout) {
    UiGtkTabView *data = ui_widget_get_tabview_data(ct->widget);
    if(!data) {
        fprintf(stderr, "UI Error: widget is not a tabview");
        return;
    }
    widget = ui_gtk_set_margin(widget, layout->margin, layout->margin_left, layout->margin_right, layout->margin_top, layout->margin_bottom);
    data->add_tab(ct->widget, -1, layout->label, widget);
    
    ct->current = widget;
}

#ifdef UI_GTK2

static void alignment_child_visibility_changed(GtkWidget *widget, gpointer user_data) {
    gtk_widget_set_visible(gtk_widget_get_parent(widget), gtk_widget_get_visible(widget));
}

#endif

GtkWidget* ui_gtk_set_margin(GtkWidget *widget, int margin, int margin_left, int margin_right, int margin_top, int margin_bottom) {
    if(margin > 0) {
        margin_left = margin;
        margin_right = margin;
        margin_top = margin;
        margin_bottom = margin;
    }
    GtkWidget *ret = widget;
#if GTK_MAJOR_VERSION >= 3
#if GTK_CHECK_VERSION(3, 12, 0)
    gtk_widget_set_margin_start(widget, margin_left);
    gtk_widget_set_margin_end(widget, margin_right);
#else
    gtk_widget_set_margin_left(widget, margin_left);
    gtk_widget_set_margin_right(widget, margin_right);
#endif
    gtk_widget_set_margin_top(widget, margin_top);
    gtk_widget_set_margin_bottom(widget, margin_bottom);
#elif defined(UI_GTK2)
    GtkWidget *a = gtk_alignment_new(0.5, 0.5, 1, 1);
    gtk_alignment_set_padding(GTK_ALIGNMENT(a), margin_top, margin_bottom, margin_left, margin_right);
    gtk_container_add(GTK_CONTAINER(a), widget);
    g_signal_connect(
                widget,
                "show",
                G_CALLBACK(alignment_child_visibility_changed),
                NULL);
    g_signal_connect(
                widget,
                "hide",
                G_CALLBACK(alignment_child_visibility_changed),
                NULL);
    ret = a;
#endif
    return ret;
}

UIWIDGET ui_box_create(UiObject *obj, UiContainerArgs *args, UiSubContainerType type) {
    UiContainerPrivate *ct = (UiContainerPrivate*)obj->container_end;
    UiLayout layout = UI_ARGS2LAYOUT(args);
    
    GtkWidget *box = type == UI_CONTAINER_VBOX ? ui_gtk_vbox_new(args->spacing) : ui_gtk_hbox_new(args->spacing);
    ui_set_name_and_style(box, args->name, args->style_class);
    ct->add(ct, box, &layout);
    
    UiContainerX *container = ui_box_container(obj, box, type);
    uic_object_push_container(obj, container);
    
    return box;
}

UIEXPORT UIWIDGET ui_vbox_create(UiObject *obj, UiContainerArgs *args) {
    return ui_box_create(obj, args, UI_CONTAINER_VBOX);
}

UIEXPORT UIWIDGET ui_hbox_create(UiObject *obj, UiContainerArgs *args) {
    return ui_box_create(obj, args, UI_CONTAINER_HBOX);
}

GtkWidget* ui_create_grid_widget(int colspacing, int rowspacing) {
#if GTK_MAJOR_VERSION >= 3
    GtkWidget *grid = gtk_grid_new();
    gtk_grid_set_column_spacing(GTK_GRID(grid), colspacing);
    gtk_grid_set_row_spacing(GTK_GRID(grid), rowspacing);
#else
    GtkWidget *grid = gtk_table_new(1, 1, FALSE);
    gtk_table_set_col_spacings(GTK_TABLE(grid), colspacing);
    gtk_table_set_row_spacings(GTK_TABLE(grid), rowspacing);
#endif
    return grid;
}

UIWIDGET ui_grid_create(UiObject *obj, UiContainerArgs *args) {
    UiContainerPrivate *ct = (UiContainerPrivate*)obj->container_end;
    UiLayout layout = UI_ARGS2LAYOUT(args);
    GtkWidget *widget;
    
    GtkWidget *grid = ui_create_grid_widget(args->columnspacing, args->rowspacing);
    ui_set_name_and_style(grid, args->name, args->style_class);
    ct->add(ct, grid, &layout);
    
    UiContainerX *container = ui_grid_container(obj, grid, args->def_hexpand, args->def_vexpand, args->def_hfill, args->def_vfill);
    uic_object_push_container(obj, container);
    
    return grid;
}

static void frame_create_subcontainer(UiObject *obj, UiFrameArgs *args) {
    switch(args->subcontainer) {
        default:
        case UI_CONTAINER_VBOX: {
            UiContainerArgs sub_args = { .spacing = args->spacing, .margin = args->padding };
            ui_vbox_create(obj, &sub_args);
            break;
        }
        case UI_CONTAINER_HBOX: {
            UiContainerArgs sub_args = { .spacing = args->spacing, .margin = args->padding };
            ui_hbox_create(obj, &sub_args);
            break;
        }
        case UI_CONTAINER_GRID: {
            UiContainerArgs sub_args = { .columnspacing = args->columnspacing, .rowspacing = args->rowspacing, .margin = args->padding };
            ui_grid_create(obj, &sub_args);
            break;
        }
        case UI_CONTAINER_NO_SUB: {
            break; // NOOP
        }
    }
}

UIWIDGET ui_frame_create(UiObject *obj, UiFrameArgs *args) {
    UiContainerPrivate *ct = (UiContainerPrivate*)obj->container_end;
    UiLayout layout = UI_ARGS2LAYOUT(args);
    
    GtkWidget *frame = gtk_frame_new(args->label);
    ct->add(ct, frame, &layout);
    
    GtkWidget *sub = ui_subcontainer_create(
            args->subcontainer,
            obj, args->spacing,
            args->columnspacing,
            args->rowspacing,
            args->padding);
    if(sub) {
        FRAME_SET_CHILD(frame, sub);
    } else {
        UiContainerX *container = ui_frame_container(obj, frame);
        uic_object_push_container(obj, container);
    }
    
    return frame;
}

UIEXPORT UIWIDGET ui_expander_create(UiObject *obj, UiFrameArgs *args) {
    UiContainerPrivate *ct = (UiContainerPrivate*)obj->container_end;
    UiLayout layout = UI_ARGS2LAYOUT(args);
    
    GtkWidget *expander = gtk_expander_new(args->label);
    gtk_expander_set_expanded(GTK_EXPANDER(expander), args->isexpanded);
    ct->add(ct, expander, &layout);
    
    GtkWidget *sub = ui_subcontainer_create(
            args->subcontainer,
            obj, args->spacing,
            args->columnspacing,
            args->rowspacing,
            args->padding);
    if(sub) {
        EXPANDER_SET_CHILD(expander, sub);
    } else {
        UiContainerX *container = ui_expander_container(obj, expander);
        uic_object_push_container(obj, container);
    }
    
    return expander;
}


UIWIDGET ui_scrolledwindow_create(UiObject* obj, UiFrameArgs *args) {
    UiContainerPrivate *ct = (UiContainerPrivate*)obj->container_end;
    UiLayout layout = UI_ARGS2LAYOUT(args);
    
    GtkWidget *sw = SCROLLEDWINDOW_NEW();
    ui_set_name_and_style(sw, args->name, args->style_class);
    ct->add(ct, sw, &layout);
    
    GtkWidget *sub = ui_subcontainer_create(
            args->subcontainer,
            obj, args->spacing,
            args->columnspacing,
            args->rowspacing,
            args->padding);
    if(sub) {
        SCROLLEDWINDOW_SET_CHILD(sw, sub);
    } else {
        UiContainerX *container = ui_scrolledwindow_container(obj, sw);
        uic_object_push_container(obj, container);
    }
    
    return sw;
}


void ui_notebook_tab_select(UIWIDGET tabview, int tab) {
    gtk_notebook_set_current_page(GTK_NOTEBOOK(tabview), tab);
}

void ui_notebook_tab_remove(UIWIDGET tabview, int tab) {
    gtk_notebook_remove_page(GTK_NOTEBOOK(tabview), tab);
}

void ui_notebook_tab_add(UIWIDGET widget, int index, const char *name, UIWIDGET child) {
    gtk_notebook_insert_page(
            GTK_NOTEBOOK(widget),
            child,
            gtk_label_new(name),
            index);
}

int64_t ui_notebook_get(UiInteger *i) {
    GtkNotebook *nb = i->obj;
    i->value = gtk_notebook_get_current_page(nb);
    return i->value;
}

void ui_notebook_set(UiInteger *i, int64_t value) {
    GtkNotebook *nb = i->obj;
    gtk_notebook_set_current_page(nb, value);
    i->value = gtk_notebook_get_current_page(nb);
}


#if GTK_MAJOR_VERSION >= 4
static int stack_set_page(GtkWidget *stack, int index) {
    GtkSelectionModel *pages = gtk_stack_get_pages(GTK_STACK(stack));
    GListModel *list = G_LIST_MODEL(pages);
    GtkStackPage *page = g_list_model_get_item(list, index);
    if(page) {
        gtk_stack_set_visible_child(GTK_STACK(stack), gtk_stack_page_get_child(page));
    } else {
        fprintf(stderr, "UI Error: ui_stack_set value out of bounds\n");
        return -1;
    }
    return index;
}

void ui_stack_tab_select(UIWIDGET tabview, int tab) {
    stack_set_page(tabview, tab);
}

void ui_stack_tab_remove(UIWIDGET tabview, int tab) {
    GtkStack *stack = GTK_STACK(tabview);
    GtkWidget *current = gtk_stack_get_visible_child(stack);
    GtkSelectionModel *pages = gtk_stack_get_pages(stack);
    GListModel *list = G_LIST_MODEL(pages);
    GtkStackPage *page = g_list_model_get_item(list, tab);
    if(page) {
        gtk_stack_remove(stack, gtk_stack_page_get_child(page));
    }
}

void ui_stack_tab_add(UIWIDGET widget, int index, const char *name, UIWIDGET child) {
    (void)gtk_stack_add_titled(GTK_STACK(widget), child, name, name);
}

int64_t ui_stack_get(UiInteger *i) {
    GtkStack *stack = GTK_STACK(i->obj);
    GtkWidget *current = gtk_stack_get_visible_child(stack);
    GtkSelectionModel *pages = gtk_stack_get_pages(stack);
    GListModel *list = G_LIST_MODEL(pages);
    int nitems = g_list_model_get_n_items(list);
    for(int p=0;p<nitems;p++) {
        GtkStackPage *page = g_list_model_get_item(list, p);
        GtkWidget *child = gtk_stack_page_get_child(page);
        if(child == current) {
            i->value = p;
            break;
        }
    }
    return i->value;
}

void ui_stack_set(UiInteger *i, int64_t value) {
    GtkWidget *widget = i->obj;
    if(stack_set_page(widget, value) >= 0) {
        i->value = value;
    }
}
#elif GTK_MAJOR_VERSION >= 3
static GtkWidget* stack_get_child(GtkWidget *stack, int index) {
    GList *children = gtk_container_get_children(GTK_CONTAINER(stack));
    if(children) {
        return g_list_nth_data(children, index);
    }
    return NULL;
}

void ui_stack_tab_select(UIWIDGET tabview, int tab) {
    GtkWidget *child = stack_get_child(tabview, tab);
    if(child) {
        gtk_stack_set_visible_child(GTK_STACK(tabview), child);
    }
}

void ui_stack_tab_remove(UIWIDGET tabview, int tab) {
    GtkWidget *child = stack_get_child(tabview, tab);
    if(child) {
        gtk_container_remove(GTK_CONTAINER(tabview), child);
    }
}

void ui_stack_tab_add(UIWIDGET widget, int index, const char *name, UIWIDGET child) {
    gtk_stack_add_titled(GTK_STACK(widget), child, name, name);
}

int64_t ui_stack_get(UiInteger *i) {
    GtkWidget *visible = gtk_stack_get_visible_child(GTK_STACK(i->obj));
    GList *children = gtk_container_get_children(GTK_CONTAINER(i->obj));
    GList *elm = children;
    int n = 0;
    int64_t v = -1;
    while(elm) {
        GtkWidget *child = elm->data;
        if(child == visible) {
            v = n;
            break;
        }
        
        elm = elm->next;
        n++;
    }
    g_list_free(children);
    i->value = v;
    return v;
}

void ui_stack_set(UiInteger *i, int64_t value) {
    GtkWidget *child = stack_get_child(i->obj, value);
    if(child) {
        gtk_stack_set_visible_child(GTK_STACK(i->obj), child);
        i->value = value;
    }
}

#endif




UiGtkTabView* ui_widget_get_tabview_data(UIWIDGET tabview) {
    return g_object_get_data(G_OBJECT(tabview), "ui_tabview");
}

static void tabview_switch_page(
        GtkNotebook *self,
        GtkWidget *page,
        guint page_num,
        gpointer userdata)
{
    UiGtkTabView *tabview = userdata;
    if(!tabview->onchange) {
        return;
    }
    
    UiEvent event;
    event.obj = tabview->obj;
    event.window = event.obj->window;
    event.document = event.obj->ctx->document;
    event.set = ui_get_setop();
    event.eventdata = NULL;
    event.eventdatatype = 0;
    event.intval = page_num;
    
    tabview->onchange(&event, tabview->onchange);
}

#if GTK_CHECK_VERSION(3, 10, 0)

static void tabview_stack_changed(
        GObject *object,
        GParamSpec *pspec,
        UiGtkTabView *tabview)
{
    if(!tabview->onchange) {
        return;
    }
    
    UiEvent event;
    event.obj = tabview->obj;
    event.window = event.obj->window;
    event.document = event.obj->ctx->document;
    event.set = ui_get_setop();
    event.eventdata = NULL;
    event.eventdatatype = 0;
    event.intval = 0;
    
    tabview->onchange(&event, tabview->onchange);
}

#endif

typedef int64_t(*ui_tabview_get_func)(UiInteger*);
typedef void (*ui_tabview_set_func)(UiInteger*, int64_t);

UIWIDGET ui_tabview_create(UiObject* obj, UiTabViewArgs *args) {
    UiGtkTabView *data = malloc(sizeof(UiGtkTabView));
    memset(data, 0, sizeof(UiGtkTabView));
    data->padding = args->padding;
    data->spacing = args->spacing;
    data->columnspacing = args->columnspacing;
    data->rowspacing = args->rowspacing;
    
    ui_tabview_get_func getfunc = NULL;
    ui_tabview_set_func setfunc = NULL;
    
    GtkWidget *widget = NULL;
    GtkWidget *data_widget = NULL;
    switch(args->tabview) {
        case UI_TABVIEW_DOC: {
            // TODO
            break;
        }
        case UI_TABVIEW_NAVIGATION_SIDE: {
#if GTK_CHECK_VERSION(3, 10, 0)
            widget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
            GtkWidget *sidebar = gtk_stack_sidebar_new();
            BOX_ADD(widget, sidebar);
            GtkWidget *stack = gtk_stack_new();
            g_signal_connect(stack, "notify::visible-child", G_CALLBACK(tabview_stack_changed), data);
            gtk_stack_set_transition_type (GTK_STACK(stack), GTK_STACK_TRANSITION_TYPE_SLIDE_UP_DOWN);
            gtk_stack_sidebar_set_stack(GTK_STACK_SIDEBAR(sidebar), GTK_STACK(stack));
            BOX_ADD_EXPAND(widget, stack);
            data->select_tab = ui_stack_tab_select;
            data->remove_tab = ui_stack_tab_remove;
            data->add_tab = ui_stack_tab_add;
            getfunc = ui_stack_get;
            setfunc = ui_stack_set;
            data_widget = stack;
#else
            // TODO
#endif
            break;
        }
        case UI_TABVIEW_DEFAULT: /* fall through */
        case UI_TABVIEW_NAVIGATION_TOP: /* fall through */
        case UI_TABVIEW_INVISIBLE: /* fall through */
        case UI_TABVIEW_NAVIGATION_TOP2: {
            widget = gtk_notebook_new();
            g_signal_connect(
                    widget,
                    "switch-page",
                    G_CALLBACK(tabview_switch_page),
                    data);
            data_widget = widget;
            data->select_tab = ui_notebook_tab_select;
            data->remove_tab = ui_notebook_tab_remove;
            data->add_tab = ui_notebook_tab_add;
            getfunc = ui_notebook_get;
            setfunc = ui_notebook_set;
            if(args->tabview == UI_TABVIEW_INVISIBLE) {
                gtk_notebook_set_show_tabs(GTK_NOTEBOOK(widget), FALSE);
                gtk_notebook_set_show_border(GTK_NOTEBOOK(widget), FALSE);
            }
            break;
        }
    }
    
    if(args->value || args->varname) {
        UiVar *var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_INTEGER);
        UiInteger *i = var->value;
        i->get = getfunc;
        i->set = setfunc;
        i->obj = data_widget;
    }
    
    g_object_set_data(G_OBJECT(widget), "ui_tabview", data);
    data->widget = data_widget;
    
    UiContainerPrivate *ct = (UiContainerPrivate*)obj->container_end;
    UiLayout layout = UI_ARGS2LAYOUT(args);
    ct->add(ct, widget, &layout);
    
    UiContainerX *container = ui_tabview_container(obj, widget);
    uic_object_push_container(obj, container);
    
    return widget;
}

static GtkWidget* create_tab(UiObject *obj, UiGtkTabView *tabview, const char *title, int tab) {
    UiContainerX *container;
    GtkWidget *sub;
    switch(tabview->subcontainer) {
        default: {
            sub = ui_gtk_vbox_new(tabview->spacing);
            container = ui_box_container(obj, sub, tabview->subcontainer);
            break;
        }
        case UI_CONTAINER_HBOX: {
            sub = ui_gtk_hbox_new(tabview->spacing);
            container = ui_box_container(obj, sub, tabview->subcontainer);
            break;
        }
        case UI_CONTAINER_GRID: {
            sub = ui_create_grid_widget(tabview->columnspacing, tabview->rowspacing);
            container = ui_grid_container(obj, sub, FALSE, FALSE, FALSE, FALSE);
            break;
        }
    }
    
    uic_object_push_container(obj, container);
    
    GtkWidget *widget = ui_gtk_set_margin(sub, tabview->padding, 0, 0, 0, 0);
    tabview->add_tab(tabview->widget, tab, title, widget);
    
    return sub;
}

void ui_tab_create(UiObject* obj, const char* title) {
    UiContainerPrivate *ct = (UiContainerPrivate*)obj->container_end; 
    GtkWidget *tabview = ct->widget;
    UiGtkTabView *data = ui_widget_get_tabview_data(tabview);
    if(!data) {
        fprintf(stderr, "UI Error: widget is not a tabview\n");
        return;
    }
    
    create_tab(obj, data, title, -1);
}



void ui_tabview_select(UIWIDGET tabview, int tab) {
    UiGtkTabView *data = ui_widget_get_tabview_data(tabview);
    if(!data) {
        fprintf(stderr, "UI Error: widget is not a tabview\n");
        return;
    }
    data->select_tab(tabview, tab);
}

void ui_tabview_remove(UIWIDGET tabview, int tab) {
    UiGtkTabView *data = ui_widget_get_tabview_data(tabview);
    if(!data) {
        fprintf(stderr, "UI Error: widget is not a tabview\n");
        return;
    }
    data->remove_tab(tabview, tab);
}

UiObject* ui_tabview_add(UIWIDGET tabview, const char *name, int tab_index) {
    UiGtkTabView *data = ui_widget_get_tabview_data(tabview);
    if(!data) {
        fprintf(stderr, "UI Error: widget is not a tabview\n");
        return NULL;
    }
    
    UiObject *newobj = uic_object_new_toplevel();
    newobj->widget = create_tab(newobj, data, name, tab_index);
    
    return newobj;
}


/* -------------------- Headerbar -------------------- */

static void hb_set_part(UiObject *obj, int part) {
    UiContainerPrivate *ct = (UiContainerPrivate*)obj->container_end;
    GtkWidget *headerbar = ct->widget;
    
    UiHeaderbarContainer *hb = cxCalloc(
            obj->ctx->allocator,
            1,
            sizeof(UiHeaderbarContainer));
    memcpy(hb, ct, sizeof(UiHeaderbarContainer));
    hb->part = part;
    uic_object_push_container(obj, (UiContainerX*)hb);
}

void ui_headerbar_start_create(UiObject *obj) {
    hb_set_part(obj, 0);
}

void ui_headerbar_center_create(UiObject *obj) {
    hb_set_part(obj, 2);
}

void ui_headerbar_end_create(UiObject *obj) {
    hb_set_part(obj, 1);
}

UIWIDGET ui_headerbar_fallback_create(UiObject *obj, UiHeaderbarArgs *args) {
    UiContainerPrivate *ct = (UiContainerPrivate*)obj->container_end;
    UiLayout layout = UI_ARGS2LAYOUT(args);
    
    GtkWidget *box = ui_gtk_hbox_new(args->alt_spacing);
    ui_set_name_and_style(box, args->name, args->style_class);
    ct->add(ct, box, &layout);
    
    UiContainerX *container = ui_headerbar_fallback_container(obj, box);
    uic_object_push_container(obj, container);
    
    return box;
}

static void hb_fallback_set_part(UiObject *obj, int part) {
     UiContainerPrivate *ct = (UiContainerPrivate*)obj->container_end;
    GtkWidget *headerbar = ct->widget;
    
    UiContainerX *container = ui_headerbar_container(obj, headerbar);
    uic_object_push_container(obj, container);
    
    UiHeaderbarContainer *hb = (UiHeaderbarContainer*)container;
    hb->part = part;
}

UiContainerX* ui_headerbar_fallback_container(UiObject *obj, GtkWidget *headerbar) {
    UiHeaderbarContainer *ct = cxCalloc(
            obj->ctx->allocator,
            1,
            sizeof(UiHeaderbarContainer));
    ct->container.widget = headerbar;
    ct->container.add = ui_headerbar_fallback_container_add;
    return (UiContainerX*)ct;
}

void ui_headerbar_fallback_container_add(UiContainerPrivate *ct, GtkWidget *widget, UiLayout *layout) {
    UiHeaderbarContainer *hb = (UiHeaderbarContainer*)ct;
    BOX_ADD(ct->widget, widget);
}

#if GTK_CHECK_VERSION(3, 10, 0)

UIWIDGET ui_headerbar_create(UiObject *obj, UiHeaderbarArgs *args) {  
    GtkWidget *headerbar = g_object_get_data(G_OBJECT(obj->widget), "ui_headerbar");
    if(!headerbar) {
        return ui_headerbar_fallback_create(obj, args);
    }
    
    UiContainerX *container = ui_headerbar_container(obj, headerbar);
    uic_object_push_container(obj, container);
    
    return headerbar;    
}

UiContainerX* ui_headerbar_container(UiObject *obj, GtkWidget *headerbar) {
    UiHeaderbarContainer *ct = cxCalloc(
            obj->ctx->allocator,
            1,
            sizeof(UiHeaderbarContainer));
    ct->container.widget = headerbar;
    ct->container.add = ui_headerbar_container_add;
    return (UiContainerX*)ct;
}

void ui_headerbar_container_add(UiContainerPrivate *ct, GtkWidget *widget, UiLayout *layout) {
    UiHeaderbarContainer *hb = (UiHeaderbarContainer*)ct;
    if(hb->part == 0) {
        UI_HEADERBAR_PACK_START(ct->widget, widget);
    } else if(hb->part == 1) {
        UI_HEADERBAR_PACK_END(ct->widget, widget);
    } else if(hb->part == 2) {
        if(!hb->centerbox) {
            GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
            hb->centerbox = box;
            UI_HEADERBAR_SET_TITLE_WIDGET(ct->widget, box);
        }
        BOX_ADD(hb->centerbox, widget);
    }
}

#else

UIWIDGET ui_headerbar_create(UiObject *obj, UiHeaderbarArgs *args) {
    return ui_headerbar_fallback_create(obj, args);  
}

#endif

/* -------------------- Sidebar -------------------- */

#ifdef UI_LIBADWAITA
UIWIDGET ui_sidebar_create(UiObject *obj, UiSidebarArgs *args) {
    GtkWidget *sidebar_toolbar_view = g_object_get_data(G_OBJECT(obj->widget), "ui_sidebar");
    if(!sidebar_toolbar_view) {
        fprintf(stderr, "Error: window is not configured for sidebar\n");
        return NULL;
    }
    
    GtkWidget *box = ui_gtk_vbox_new(args->spacing);
    ui_gtk_set_margin(box, args->margin, args->margin_left, args->margin_right, args->margin_top, args->margin_bottom);
    adw_toolbar_view_set_content(ADW_TOOLBAR_VIEW(sidebar_toolbar_view), box);
    
    UiContainerX *container = ui_box_container(obj, box, UI_CONTAINER_VBOX);
    uic_object_push_container(obj, container);
    
    return box;
}
#else
UIWIDGET ui_sidebar_create(UiObject *obj, UiSidebarArgs *args) {
    GtkWidget *sidebar_vbox = g_object_get_data(G_OBJECT(obj->widget), "ui_sidebar");
    
    GtkWidget *box = ui_gtk_vbox_new(args->spacing);
    ui_gtk_set_margin(box, args->margin, args->margin_left, args->margin_right, args->margin_top, args->margin_bottom);
    BOX_ADD_EXPAND(sidebar_vbox, box);
    
    UiContainerX *container = ui_box_container(obj, box, UI_CONTAINER_VBOX);
    uic_object_push_container(obj, container);
    
    return box;
}
#endif

/* ------------------------ Split Window Panels ------------------------ */

static UIWIDGET splitwindow_panel(UiObject *obj, GtkWidget *pbox, UiSidebarArgs *args) {
    if(!pbox) {
        fprintf(stderr, "Error: window is not a splitview window\n");
        return NULL;
    }
    
    GtkWidget *box = ui_gtk_vbox_new(args->spacing);
    ui_set_name_and_style(box, args->name, args->style_class);
    ui_gtk_set_margin(box, args->margin, args->margin_left, args->margin_right, args->margin_top, args->margin_bottom);
    BOX_ADD_EXPAND(pbox, box);
    
    UiContainerX *container = ui_box_container(obj, box, UI_CONTAINER_VBOX);
    uic_object_push_container(obj, container);
    
    return box;
}

UIWIDGET ui_left_panel_create(UiObject *obj, UiSidebarArgs *args) {
    return splitwindow_panel(obj, g_object_get_data(G_OBJECT(obj->widget), "ui_left_panel"), args);
}

UIWIDGET ui_right_panel_create(UiObject *obj, UiSidebarArgs *args) {
    return splitwindow_panel(obj, g_object_get_data(G_OBJECT(obj->widget), "ui_right_panel"), args);
}


/* -------------------- Splitpane -------------------- */

static GtkWidget* create_paned(UiOrientation orientation) {
#if GTK_MAJOR_VERSION >= 3
    switch(orientation) {
        case UI_HORIZONTAL: return gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
        case UI_VERTICAL: return gtk_paned_new(GTK_ORIENTATION_VERTICAL);
    }
#else
    switch(orientation) {
        case UI_HORIZONTAL: return gtk_hpaned_new();
        case UI_VERTICAL: return gtk_vpaned_new();
    }
#endif
    return NULL;
}

static void save_pane_pos(GtkWidget *widget, char *property_name) {
    int pos = gtk_paned_get_position(GTK_PANED(widget));
    char buf[32];
    snprintf(buf, 32, "%d", pos);
    ui_set_property(property_name, buf);
    free(property_name);
}

static UIWIDGET splitpane_create(UiObject *obj, UiOrientation orientation, UiSplitPaneArgs *args) {
    UiContainerPrivate *ct = (UiContainerPrivate*)obj->container_end;
    UiLayout layout = UI_ARGS2LAYOUT(args);
    
    GtkWidget *pane0 = create_paned(orientation);
    ct->add(ct, pane0, &layout);
    
    int max = args->max_panes == 0 ? 2 : args->max_panes;
    
    if(args->position_property) {
        const char *pos_str = ui_get_property(args->position_property);
        if(pos_str) {
            char *end;
            long pos = strtol(pos_str, &end, 10);
            if(*end == '\0') {
                args->initial_position = (int)pos;
            }
        }
        
        g_signal_connect(
                pane0,
                "destroy",
                G_CALLBACK(save_pane_pos),
                strdup(args->position_property));
    }
    
    UiSplitPane *splitpane = ui_create_splitpane_data(pane0, orientation, max, args->initial_position);
    UiContainerX *container = ui_splitpane_container(obj, pane0, splitpane);
    uic_object_push_container(obj, container);
    
    g_object_set_data(G_OBJECT(pane0), "ui_splitpane", splitpane);
    
    UiVar *var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_INTEGER);
    if(var) {
        UiInteger *i = var->value;
        splitpane->initial_position = i->value;
        
        i->obj = splitpane;
        i->get = ui_splitpane_get;
        i->set = ui_splitpane_set;
    }
    
    return pane0;
}

UIWIDGET ui_hsplitpane_create(UiObject *obj, UiSplitPaneArgs *args) {
    return splitpane_create(obj, UI_HORIZONTAL, args);
}

UIWIDGET ui_vsplitpane_create(UiObject *obj, UiSplitPaneArgs *args) {
    return splitpane_create(obj, UI_VERTICAL, args);
}

UiSplitPane* ui_create_splitpane_data(GtkWidget *pane, UiOrientation orientation, int max, int init) {
    UiSplitPane *ct = malloc(sizeof(UiSplitPane));
    ct->current_pane = pane;
    ct->orientation = orientation;
    ct->max = max;
    ct->initial_position = init;
    ct->children = cxArrayListCreateSimple(CX_STORE_POINTERS, 4);
    return ct;
}

UiContainerX* ui_splitpane_container(UiObject *obj, GtkWidget *pane, UiSplitPane *data) {
    UiSplitPaneContainer *ct = ui_calloc(obj->ctx, 1, sizeof(UiSplitPaneContainer));
    ct->container.widget = pane;
    ct->container.add = ui_splitpane_container_add;
    ct->splitpane = data;
    return (UiContainerX*)ct;
}

void ui_splitpane_container_add(UiContainerPrivate *ct, GtkWidget *widget, UiLayout *layout) {
    UiSplitPaneContainer *sct = (UiSplitPaneContainer*)ct;
    UiSplitPane *s = sct->splitpane;
    
    if(s->nchildren >= s->max) {
        fprintf(stderr, "splitpane: maximum number of children reached\n");
        return;
    }
    
    cxListAdd(s->children, widget);
    
    if(s->pos == 0) {
        PANED_SET_CHILD1(s->current_pane, widget);
        if(s->initial_position > 0) {
            gtk_paned_set_position(GTK_PANED(s->current_pane), s->initial_position);
        }
        s->pos++;
        s->nchildren++;
    } else {
        if(s->nchildren+1 == s->max) {
            PANED_SET_CHILD2(s->current_pane, widget);
        } else {
            GtkWidget *pane = create_paned(s->orientation);
            PANED_SET_CHILD1(pane, widget);
            PANED_SET_CHILD2(s->current_pane, pane);
            s->current_pane = pane;
        }
        
        s->pos = 0;
        s->nchildren++;
    }
}

int64_t ui_splitpane_get(UiInteger *i) {
    UiSplitPane *s = i->obj;
    i->value = gtk_paned_get_position(GTK_PANED(s->current_pane));
    return i->value;
}

void ui_splitpane_set(UiInteger *i, int64_t value) {
    UiSplitPane *s = i->obj;
    i->value = value;
    gtk_paned_set_position(GTK_PANED(s->current_pane), (int)value);
}

UIEXPORT void ui_splitpane_set_visible(UIWIDGET splitpane, int child_index, UiBool visible) {
    UiSplitPane *s = g_object_get_data(G_OBJECT(splitpane), "ui_splitpane");
    if(!s) {
        fprintf(stderr, "UI Error: not a splitpane\n");
        return;
    }
    
    GtkWidget *w = cxListAt(s->children, child_index);
    if(w) {
        gtk_widget_set_visible(w, visible);
    }
}

/* -------------------- ItemList Container -------------------- */

static void remove_item(void *data, void *item) {
    UiGtkItemListContainer *ct = data;
    UiObject *obj = item;
    if(ct->remove_items) {
        BOX_REMOVE(ct->widget, obj->widget);
    }
    uic_object_destroy(obj);
}

static void update_itemlist(UiList *list, int c) {
    UiGtkItemListContainer *ct = list->obj;
    
    CxMap *new_items = cxHashMapCreateSimple(CX_STORE_POINTERS);
    new_items->collection.advanced_destructor = remove_item;
    new_items->collection.destructor_data = ct;
    
    // only create new widgets for new elements, so at first we have
    // to find which elements are new
    // check which elements in the list are already in the container
    void *elm = list->first(list);
    int j = 0;
    while(elm) {
        CxHashKey key = cx_hash_key(&elm, sizeof(void*));
        UiObject *item_obj = NULL;
        cxMapRemoveAndGet(ct->current_items, key, &item_obj);
        if(item_obj) {
            g_object_ref(G_OBJECT(item_obj->widget));
            BOX_REMOVE(ct->widget, item_obj->widget);
            cxMapPut(new_items, key, item_obj);
        }
        elm = list->next(list);
        j++;
    }
    
    // ct->current_items only contains elements, that are not in the list
    cxMapFree(ct->current_items); // calls destructor remove_item
    ct->current_items = new_items;
    
    // add all items
    int index = 0;
    elm = list->first(list);
    while(elm) {
        CxHashKey key = cx_hash_key(&elm, sizeof(void*));
        UiObject *item_obj = cxMapGet(ct->current_items, key);
        if(item_obj) {
            // re-add previously created widget
            UiLayout layout = {0};
            ui_box_container_add(ct->container, item_obj->widget, &layout);
        } else {
            // create new widget and object for this list element
            UiObject *obj = uic_object_new_toplevel();
            obj->ctx->parent = ct->parent->ctx;
            obj->window = NULL;
            obj->widget = ui_subcontainer_create(
                    ct->subcontainer,
                    obj,
                    ct->spacing,
                    ct->columnspacing,
                    ct->rowspacing,
                    ct->margin);
            UiLayout layout = {0};
            ui_box_container_add(ct->container, obj->widget, &layout);
            if(ct->create_ui) {
                ct->create_ui(obj, index, elm, ct->userdata);
            }
            cxMapPut(new_items, key, obj);
        }
        elm = list->next(list);
        index++;
    }
    
#if GTK_MAJOR_VERSION < 4
    gtk_widget_show_all(ct->widget);
#endif
}

static void destroy_itemlist_container(GtkWidget *w, UiGtkItemListContainer *container) {
    container->remove_items = FALSE;
    cxMapFree(container->current_items);
    free(container);
}

UIWIDGET ui_itemlist_create(UiObject *obj, UiItemListContainerArgs *args) {
    UiContainerPrivate *ct = (UiContainerPrivate*)obj->container_end;
    UiLayout layout = UI_ARGS2LAYOUT(args);
    
    GtkWidget *box = args->container == UI_CONTAINER_VBOX ? ui_gtk_vbox_new(args->spacing) : ui_gtk_hbox_new(args->spacing);
    ui_set_name_and_style(box, args->name, args->style_class);
    ct->add(ct, box, &layout);
    
    UiGtkItemListContainer *container = malloc(sizeof(UiGtkItemListContainer));
    container->parent = obj;
    container->widget = box;
    container->container = (UiContainerPrivate*)ui_box_container(obj, box, args->container);
    container->create_ui = args->create_ui;
    container->userdata = args->userdata;
    container->subcontainer = args->subcontainer;
    container->current_items = cxHashMapCreateSimple(CX_STORE_POINTERS);
    container->current_items->collection.advanced_destructor = remove_item;
    container->current_items->collection.destructor_data = container;
    container->margin = args->sub_margin;
    container->spacing = args->sub_spacing;
    container->columnspacing = args->sub_columnspacing;
    container->rowspacing = args->sub_rowspacing;
    container->remove_items = TRUE;
    
    UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_LIST);
    if(var) {
        UiList *list = var->value;
        list->obj = container;
        list->update = update_itemlist;
        update_itemlist(list, 0);
    }
    g_signal_connect(
                box,
                "destroy",
                G_CALLBACK(destroy_itemlist_container),
                container);
    
    return box;
}

mercurial