summaryrefslogtreecommitdiff
path: root/lib/luavgl/src/widgets/dropdown.c
blob: a211c10b4f7f7bf63b09405d7303217e9530061e (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "luavgl.h"
#include "private.h"

static int luavgl_dropdown_create(lua_State *L)
{
  return luavgl_obj_create_helper(L, lv_dropdown_create);
}

static void _lv_dropdown_set_text(void *obj, lua_State *L)
{
  const char *str = lua_tostring(L, -1);

  lv_dropdown_set_text(obj, str);
}

/* clang-format off */
static const luavgl_value_setter_t dropdown_property_table[] = {
    {"text", SETTER_TYPE_STACK, {.setter_stack = _lv_dropdown_set_text}},
    {"options", SETTER_TYPE_STACK, {.setter_stack = _lv_dummy_set}},
    {"selected", 0, {.setter = (setter_int_t)lv_dropdown_set_selected}},
    {"dir", 0, {.setter = (setter_int_t)lv_dropdown_set_dir}},
    {"symbol", SETTER_TYPE_IMGSRC, {.setter_pointer = (setter_pointer_t)lv_dropdown_set_symbol}},
    {"highlight", 0, {.setter = (setter_int_t)lv_dropdown_set_selected_highlight}},
};
/* clang-format on */

LUALIB_API int luavgl_dropdown_set_property_kv(lua_State *L, void *data)
{
  lv_obj_t *obj = data;
  int ret = luavgl_set_property(L, obj, dropdown_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 dropdown.\n");
  }

  return ret;
}

static int luavgl_dropdown_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;
  }

  /* set options firstly, otherwise set selected may fail */
  lua_getfield(L, -1, "options");
  if (lua_type(L, -1) == LUA_TSTRING) {
    lv_dropdown_set_options(obj, lua_tostring(L, -1));
  }
  lua_pop(L, 1);

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

  return 0;
}

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

  if (lua_type(L, 2) != LUA_TSTRING) {
    return luaL_argerror(L, 2, "expect string");
  }

  const char *key = lua_tostring(L, 2);
  if (strcmp(key, "list") == 0) {
    lv_obj_t *list = lv_dropdown_get_list(obj);
    lua_pushlightuserdata(L, list);
    lua_rawget(L, LUA_REGISTRYINDEX);
    if (lua_isnoneornil(L, -1)) {
      lua_pop(L, 1);
      luavgl_add_lobj(L, list)->lua_created = false;
    }
    return 1;
  }

  if (strcmp(key, "text") == 0) {
    lua_pushstring(L, lv_dropdown_get_text(obj));
    return 1;
  }

  if (strcmp(key, "options") == 0) {
    lua_pushstring(L, lv_dropdown_get_options(obj));
    return 1;
  }

  if (strcmp(key, "selected") == 0) {
    lua_pushinteger(L, lv_dropdown_get_selected(obj));
    return 1;
  }

  if (strcmp(key, "option_cnt") == 0) {
    lua_pushinteger(L, lv_dropdown_get_option_cnt(obj));
    return 1;
  }

  if (strcmp(key, "selected_str") == 0) {
    char buf[64];
    lv_dropdown_get_selected_str(obj, buf, sizeof(buf));
    lua_pushstring(L, buf);
    return 1;
  }

  if (strcmp(key, "option_index") == 0) {
    const char *option = lua_tostring(L, 3);
    lua_pushinteger(L, lv_dropdown_get_option_index(obj, option));
    return 1;
  }

  if (strcmp(key, "symbol") == 0) {
    lua_pushlightuserdata(L, (void *)lv_dropdown_get_symbol(obj));
    return 1;
  }

  if (strcmp(key, "dir") == 0) {
    lua_pushinteger(L, lv_dropdown_get_dir(obj));
    return 1;
  }

  return 0;
}

static int luavgl_dropdown_open(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_dropdown_open(obj);
  return 0;
}

static int luavgl_dropdown_close(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_dropdown_close(obj);
  return 0;
}

static int luavgl_dropdown_is_open(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lua_pushboolean(L, lv_dropdown_is_open(obj));
  return 1;
}

static int luavgl_dropdown_add_option(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  const char *str = lua_tostring(L, 2);
  uint32_t pos = lua_tointeger(L, 3);
  lv_dropdown_add_option(obj, str, pos);

  return 0;
}

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

  lv_dropdown_clear_options(obj);
  return 0;
}

static const luaL_Reg luavgl_dropdown_methods[] = {
    {"set",          luavgl_dropdown_set         },
    {"get",          luavgl_dropdown_get         },
    {"open",         luavgl_dropdown_open        },
    {"close",        luavgl_dropdown_close       },
    {"is_open",      luavgl_dropdown_is_open     },
    {"add_option",   luavgl_dropdown_add_option  },
    {"clear_option", luavgl_dropdown_clear_option},

    {NULL,           NULL                      },
};

static void luavgl_dropdown_init(lua_State *L)
{
  luavgl_obj_newmetatable(L, &lv_dropdown_class, "lv_dropdown",
                        luavgl_dropdown_methods);
  lua_pop(L, 1);
}