Home | History | Annotate | Download | only in src
      1 #include "cmenu.h"
      2 
      3 #include "lua.h"
      4 #include "lauxlib.h"
      5 #include "lualib.h"
      6 
      7 static int l_init (lua_State *L)
      8 {
      9   init_menusystem (luaL_optstring (L, 1, NULL));
     10   return 0;
     11 }
     12 
     13 static int l_set_window_size (lua_State *L)
     14 {
     15   set_window_size (luaL_checkint (L, 1), luaL_checkint (L, 2),
     16                    luaL_checkint (L, 3), luaL_checkint (L, 4));
     17   return 0;
     18 }
     19 
     20 static int l_add_menu (lua_State *L)
     21 {
     22   lua_pushinteger (L, add_menu (luaL_checkstring (L, 1),
     23                                 luaL_optint (L, 2, -1)));
     24   return 1;
     25 }
     26 
     27 static int l_add_named_menu (lua_State *L)
     28 {
     29   lua_pushinteger (L, add_named_menu (luaL_checkstring (L, 1),
     30                                       luaL_checkstring (L, 2),
     31                                       luaL_optint (L, 3, -1)));
     32   return 1;
     33 }
     34 
     35 static int l_add_item (lua_State *L)
     36 {
     37   add_item (luaL_checkstring (L, 1), luaL_checkstring (L, 2), luaL_checkint (L, 3),
     38             luaL_optstring (L, 4, NULL), luaL_optint (L, 5, 0));
     39   return 0; /* FIXME return menuitem for advanced functions */
     40 }
     41 
     42 static int l_add_sep (lua_State *L)
     43 {
     44   add_sep ();
     45   return 0; /* FIXME return menuitem for advanced functions */
     46 }
     47 
     48 static int l_find_menu_num (lua_State *L)
     49 {
     50   lua_pushinteger (L, find_menu_num (luaL_checkstring (L, 1)));
     51   return 1;
     52 }
     53 
     54 static int l_showmenus (lua_State *L)
     55 {
     56   t_menuitem *reply = showmenus (luaL_checkint (L, 1));
     57   if (!reply) return 0;
     58   lua_pushinteger (L, reply->action);
     59   lua_pushstring (L, reply->data);
     60   return 2;
     61 }
     62 
     63 static const luaL_Reg menulib[] = {
     64   {"init", l_init},
     65   {"set_window_size", l_set_window_size},
     66   {"add_menu", l_add_menu},
     67   {"add_named_menu", l_add_named_menu},
     68   {"add_item", l_add_item},
     69   {"add_sep", l_add_sep},
     70   {"find_menu_num", l_find_menu_num},
     71   {"showmenus", l_showmenus},
     72   {NULL, NULL}
     73 };
     74 
     75 LUALIB_API int luaopen_cmenu (lua_State *L) {
     76   luaL_newlib(L, menulib);
     77   lua_newtable (L);
     78 #define export_opt(x) lua_pushinteger (L, OPT_##x); lua_setfield (L, -2, #x);
     79   export_opt (INACTIVE);
     80   export_opt (SUBMENU);
     81   export_opt (RUN);
     82   export_opt (EXITMENU);
     83   export_opt (CHECKBOX);
     84   export_opt (RADIOMENU);
     85   export_opt (SEP);
     86   export_opt (INVISIBLE);
     87   export_opt (RADIOITEM);
     88   lua_setfield (L, -2, "action");
     89   return 1;
     90 }
     91