summaryrefslogtreecommitdiff
path: root/lib/luavgl/src/widgets/switch.c
blob: e66f620e365e207d690a9350993ba20541067aa1 (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
#include "luavgl.h"
#include "private.h"
#include <src/widgets/lv_switch.h>

static int luavgl_switch_create(lua_State *L) {
  return luavgl_obj_create_helper(L, lv_switch_create);
}

LUALIB_API int luavgl_switch_set_property_kv(lua_State *L, void *data) {
  lv_obj_t *obj = data;
  /* switches only use base properties */
  int ret = luavgl_obj_set_property_kv(L, obj);
  if (ret != 0) {
    debug("unkown property for switch.\n");
  }

  return ret;
}

static int luavgl_switch_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_switch_set_property_kv, obj);

  return 0;
}

static int luavgl_switch_enabled(lua_State *L) {
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lua_pushboolean(L, lv_obj_has_state(obj, LV_STATE_CHECKED));
  return 1;
}

static int luavgl_switch_tostring(lua_State *L) {
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lua_pushfstring(L, "lv_switch:%p", obj);
  return 1;
}

static const luaL_Reg luavgl_switch_methods[] = {
    {"set", luavgl_switch_set},
    {"enabled", luavgl_switch_enabled},
    {NULL, NULL},
};

static void luavgl_switch_init(lua_State *L) {
  luavgl_obj_newmetatable(L, &lv_switch_class, "lv_switch",
                          luavgl_switch_methods);
  lua_pushcfunction(L, luavgl_switch_tostring);
  lua_setfield(L, -2, "__tostring");
  lua_pop(L, 1);
}