aboutsummaryrefslogtreecommitdiffstats
path: root/com32/lua/src/cmenu.c
blob: 1b128618700f7d2e0227a144873bac0a6f9b5e89 (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
#include "cmenu.h"

#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

static int l_init (lua_State *L)
{
  init_menusystem (luaL_optstring (L, 1, NULL));
  return 0;
}

static int l_set_window_size (lua_State *L)
{
  set_window_size (luaL_checkint (L, 1), luaL_checkint (L, 2),
                   luaL_checkint (L, 3), luaL_checkint (L, 4));
  return 0;
}

static int l_add_menu (lua_State *L)
{
  lua_pushinteger (L, add_menu (luaL_checkstring (L, 1),
                                luaL_optint (L, 2, -1)));
  return 1;
}

static int l_add_named_menu (lua_State *L)
{
  lua_pushinteger (L, add_named_menu (luaL_checkstring (L, 1),
                                      luaL_checkstring (L, 2),
                                      luaL_optint (L, 3, -1)));
  return 1;
}

static int l_add_item (lua_State *L)
{
  add_item (luaL_checkstring (L, 1), luaL_checkstring (L, 2), luaL_checkint (L, 3),
            luaL_optstring (L, 4, NULL), luaL_optint (L, 5, 0));
  return 0; /* FIXME return menuitem for advanced functions */
}

static int l_add_sep (lua_State *L)
{
  add_sep ();
  return 0; /* FIXME return menuitem for advanced functions */
}

static int l_find_menu_num (lua_State *L)
{
  lua_pushinteger (L, find_menu_num (luaL_checkstring (L, 1)));
  return 1;
}

static int l_showmenus (lua_State *L)
{
  t_menuitem *reply = showmenus (luaL_checkint (L, 1));
  if (!reply) return 0;
  lua_pushinteger (L, reply->action);
  lua_pushstring (L, reply->data);
  return 2;
}

static const luaL_Reg menulib[] = {
  {"init", l_init},
  {"set_window_size", l_set_window_size},
  {"add_menu", l_add_menu},
  {"add_named_menu", l_add_named_menu},
  {"add_item", l_add_item},
  {"add_sep", l_add_sep},
  {"find_menu_num", l_find_menu_num},
  {"showmenus", l_showmenus},
  {NULL, NULL}
};

LUALIB_API int luaopen_cmenu (lua_State *L) {
  luaL_newlib(L, menulib);
  lua_newtable (L);
#define export_opt(x) lua_pushinteger (L, OPT_##x); lua_setfield (L, -2, #x);
  export_opt (INACTIVE);
  export_opt (SUBMENU);
  export_opt (RUN);
  export_opt (EXITMENU);
  export_opt (CHECKBOX);
  export_opt (RADIOMENU);
  export_opt (SEP);
  export_opt (INVISIBLE);
  export_opt (RADIOITEM);
  lua_setfield (L, -2, "action");
  return 1;
}