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

static int luavgl_calendar_create(lua_State *L)
{
  return luavgl_obj_create_helper(L, lv_calendar_create);
}

static void _lv_calendar_set_today(void *obj, lua_State *L)
{
  if (!lua_istable(L, -1)) {
    luaL_argerror(L, -1, "expect date table.");
    return;
  }

  uint32_t year, month, day;
  lua_getfield(L, -1, "year");
  year = lua_tointeger(L, -1);
  lua_pop(L, 1);

  lua_getfield(L, -1, "month");
  month = lua_tointeger(L, -1);
  lua_pop(L, 1);

  lua_getfield(L, -1, "day");
  day = lua_tointeger(L, -1);
  lua_pop(L, 1);

  lv_calendar_set_today_date(obj, year, month, day);
}

static void _lv_calendar_set_showed(void *obj, lua_State *L)
{
  if (!lua_istable(L, -1)) {
    luaL_argerror(L, -1, "expect date table.");
    return;
  }

  uint32_t year, month;
  lua_getfield(L, -1, "year");
  year = lua_tointeger(L, -1);
  lua_pop(L, 1);

  lua_getfield(L, -1, "month");
  month = lua_tointeger(L, -1);
  lua_pop(L, 1);

  lv_calendar_set_showed_date(obj, year, month);
}

static const luavgl_value_setter_t calendar_property_table[] = {
    {"today",  SETTER_TYPE_STACK, {.setter_stack = _lv_calendar_set_today} },
    {"showed", SETTER_TYPE_STACK, {.setter_stack = _lv_calendar_set_showed}},
};

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

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

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

  return ret;
}

static int luavgl_calendar_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_calendar_set_property_kv, obj);

  return 0;
}

static inline int calendar_new_date(lua_State *L,
                                    const lv_calendar_date_t *date)
{
  lua_createtable(L, 0, 3);
  lua_pushinteger(L, date->year);
  lua_setfield(L, -2, "year");

  lua_pushinteger(L, date->month);
  lua_setfield(L, -2, "month");

  lua_pushinteger(L, date->day);
  lua_setfield(L, -2, "day");

  return 1;
}

static int luavgl_calendar_get_today(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  const lv_calendar_date_t *date = lv_calendar_get_today_date(obj);

  return calendar_new_date(L, date);
}

static int luavgl_calendar_get_showed(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  const lv_calendar_date_t *date = lv_calendar_get_showed_date(obj);

  return calendar_new_date(L, date);
}

static int luavgl_calendar_get_pressed(lua_State *L)
{
  lv_calendar_date_t date;
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_calendar_get_pressed_date(obj, &date);

  return calendar_new_date(L, &date);
}

static int luavgl_calendar_get_btnm(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_obj_t *btm = lv_calendar_get_btnmatrix(obj);
  lua_pushlightuserdata(L, btm);
  lua_rawget(L, LUA_REGISTRYINDEX);
  if (lua_isnoneornil(L, -1)) {
    luavgl_add_lobj(L, btm)->lua_created = false;
  }

  return 1; /* obj userdata is already on stack */
}

static int luavgl_calendar_create_arrow(lua_State *L)
{
  return luavgl_obj_create_helper(L, lv_calendar_header_arrow_create);
}

static int luavgl_calendar_create_dropdown(lua_State *L)
{
  return luavgl_obj_create_helper(L, lv_calendar_header_dropdown_create);
}

static const luaL_Reg luavgl_calendar_methods[] = {
    {"set",         luavgl_calendar_set            },
    {"get_today",   luavgl_calendar_get_today      },
    {"get_showed",  luavgl_calendar_get_showed     },
    {"get_pressed", luavgl_calendar_get_pressed    },
    {"get_btnm",    luavgl_calendar_get_btnm       },
    {"Arrow",       luavgl_calendar_create_arrow   },
    {"Dropdown",    luavgl_calendar_create_dropdown},

    {NULL,          NULL                           },
};

static void luavgl_calendar_init(lua_State *L)
{
  luavgl_obj_newmetatable(L, &lv_calendar_class, "lv_calendar",
                          luavgl_calendar_methods);
  lua_pop(L, 1);
}