summaryrefslogtreecommitdiff
path: root/lib/luavgl/src/widgets/roller.c
blob: 574fc066dbf8c8ea165f0c3eea71f1e8ea87b297 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "luavgl.h"
#include "private.h"

static int luavgl_roller_create(lua_State *L)
{
  return luavgl_obj_create_helper(L, lv_roller_create);
}

static void _lv_roller_set_options(void *obj, lua_State *L)
{
  int type = lua_type(L, -1);
  if (type == LUA_TSTRING) {
    lv_roller_set_options(obj, lua_tostring(L, -1), 0);
  } else if (type == LUA_TTABLE) {
    lua_getfield(L, -1, "options");
    if (!lua_isstring(L, -1)) {
      luaL_error(L, "expect string.");
      return;
    }

    const char *options = lua_tostring(L, -1);
    lua_pop(L, 1);
    lua_getfield(L, -1, "mode");
    lv_roller_mode_t mode = lua_tointeger(L, -1);
    lua_pop(L, 1);
    lv_roller_set_options(obj, options, mode);
    return;
  }

  luaL_error(L, "expect string or table.");
}

static void _lv_roller_set_selected(void *obj, lua_State *L)
{
  int type = lua_type(L, -1);
  uint16_t selected;
  int anim = 0;

  if (type == LUA_TTABLE) {
    lua_getfield(L, -1, "selected");
    selected = lua_tointeger(L, -1);
    lua_pop(L, 1);
    lua_getfield(L, -1, "anim");
    anim = luavgl_tointeger(L, -1);
    lua_pop(L, 1);
  } else {
    selected = lua_tointeger(L, -1);
  }

  lv_roller_set_selected(obj, selected, anim);
}

static const luavgl_value_setter_t roller_property_table[] = {
    {"options",     SETTER_TYPE_STACK, {.setter_stack = _lv_roller_set_options}                 },
    {"selected",    SETTER_TYPE_STACK, {.setter_stack = _lv_roller_set_selected}                },
    {"visible_cnt",
     0,                                {.setter = (setter_int_t)lv_roller_set_visible_row_count}},
};

LUALIB_API int luavgl_roller_set_property_kv(lua_State *L, void *data)
{
  lv_obj_t *obj = data;
  int ret = luavgl_set_property(L, obj, roller_property_table);

  if (ret == 0) {
    return 0;
  }

  /* a base obj property? */
  ret = luavgl_obj_set_property_kv(L, obj);
  if (ret != 0) {
    debug("unkown property for roller.\n");
  }

  return ret;
}

static int luavgl_roller_set(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);

  if (!lua_istable(L, -1)) {
    luaL_error(L, "expect a table on 2nd para.");
    return 0;
  }

  luavgl_iterate(L, -1, luavgl_roller_set_property_kv, obj);

  return 0;
}

static int luavgl_roller_get_options(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lua_pushstring(L, lv_roller_get_options(obj));
  return 1;
}

static int luavgl_roller_get_selected(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lua_pushinteger(L, lv_roller_get_selected(obj));
  return 1;
}

static int luavgl_roller_get_selected_str(lua_State *L)
{
  char buf[64];
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_roller_get_selected_str(obj, buf, sizeof(buf));
  lua_pushstring(L, buf);
  return 1;
}

static int luavgl_roller_get_options_cnt(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lua_pushinteger(L, lv_roller_get_option_cnt(obj));
  return 1;
}

static const luaL_Reg luavgl_roller_methods[] = {
    {"set",              luavgl_roller_set             },
    {"get_options",      luavgl_roller_get_options     },
    {"get_selected",     luavgl_roller_get_selected    },
    {"get_selected_str", luavgl_roller_get_selected_str},
    {"get_options_cnt",  luavgl_roller_get_options_cnt },

    {NULL,               NULL                        },
};

static void luavgl_roller_init(lua_State *L)
{
  luavgl_obj_newmetatable(L, &lv_roller_class, "lv_roller", luavgl_roller_methods);
  lua_pop(L, 1);
}