Navigation C API Pages Python bindings Applications

Table widget example

This is a simple example how to render a table widget. The table columns are exported by the application in the col_ops structure and the header in the json describes which columns and how are rendered on the screen.

//SPDX-License-Identifier: LGPL-2.0-or-later

/*

   Copyright (c) 2021-2023 Cyril Hrubis <metan@ucw.cz>

 */

#include <widgets/gp_widgets.h>

#define TABLE_ROWS 4

static const char *const table_data[TABLE_ROWS][3] = {
        {"Apple", "Yes", "1"},
        {"Banana", "Yes", "2"},
        {"Orange", "Yes", "3"},
        {"Dog", "No", "4"}
};

enum tbl_ids {
        OBJECT,
        IS_FRUIT,
        NUMBER,
};

static int tbl_seek_row(gp_widget *self, int op, unsigned int pos)
{
        switch (op) {
        case GP_TABLE_ROW_RESET:
                self->tbl->row_idx = 0;
        break;
        case GP_TABLE_ROW_ADVANCE:
                self->tbl->row_idx += pos;
        break;
        case GP_TABLE_ROW_MAX:
                return TABLE_ROWS;
        }

        if (self->tbl->row_idx >= TABLE_ROWS)
                return 0;

        return 1;
}

static int tbl_get_cell(gp_widget *self, gp_widget_table_cell *cell, unsigned int col_id)
{
        unsigned int row = self->tbl->row_idx;

        switch (col_id) {
        case OBJECT:
                cell->text = table_data[row][0];
        break;
        case IS_FRUIT:
                cell->text = table_data[row][1];
        break;
        case NUMBER:
                cell->text = table_data[row][2];
        break;
        }

        return 1;
}

const gp_widget_table_col_ops table_col_ops = {
        .seek_row = tbl_seek_row,
        .get_cell = tbl_get_cell,
        .col_map = {
                {.id = "object", .idx = OBJECT},
                {.id = "is_fruit", .idx = IS_FRUIT},
                {.id = "number", .idx = NUMBER},
                {}
        }
};

int table_on_event(gp_widget_event *ev)
{
        if (ev->type != GP_WIDGET_EVENT_WIDGET)
                return 0;

        printf("Selected row %u\n", ev->self->tbl->selected_row);

        return 1;
}

gp_app_info app_info = {
        .name = "Table Example",
        .desc = "Table widget OPS example",
        .version = "1.0",
        .license = "GPL-2.0-or-later",
        .url = "http://gfxprim.ucw.cz",
        .authors = (gp_app_info_author []) {
                {.name = "Cyril Hrubis", .email = "metan@ucw.cz", .years = "2021-2023"},
                {}
        }
};

int main(int argc, char *argv[])
{
        gp_widget *layout = gp_widget_layout_json("table_example.json", NULL, NULL);
        if (!layout)
                return 0;

        gp_widgets_main_loop(layout, NULL, argc, argv);

        return 0;
}
{
 "info": {"version": 1, "license": "GPL-2.0-or-later"},
 "layout": {
  "widgets": [
   {
    "type": "table",
    "min_rows": 4,
    "col_ops": "table_col_ops",
    "on_event": "table_on_event",
    "header": [
     {"label": "Object", "min_size": 8, "id": "object"},
     {"label": "Fruit?", "min_size": 3, "id": "is_fruit"}
    ]
   }
  ]
 }
}