luanewt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. #include <stdlib.h>
  2. #include "luanewt.h"
  3. #define MYNAME "NEWT"
  4. #define MYVERSION MYNAME " binding for " LUA_VERSION " 2013.03.20"
  5. #define TYPE_COMPONENT "Newt.Component"
  6. #define lua_newncom(L) ((component)(lua_newuserdata(L, sizeof(struct com_t))))
  7. #define lua_toncom(L, i) ((component)(lua_touserdata(L, i)))
  8. #define bool int
  9. #define false 0
  10. #define true 1
  11. /* check and return pointer */
  12. void *luaL_checkpointer(lua_State* L, int i) {
  13. luaL_checktype(L, i, LUA_TLIGHTUSERDATA);
  14. return lua_touserdata(L, i);
  15. }
  16. /* check and return component object parameter */
  17. component luaL_checkcomponent(lua_State* L, int i) {
  18. luaL_checkudata(L, i, TYPE_COMPONENT);
  19. return lua_toncom(L, i);
  20. }
  21. /* push a component object on the stack */
  22. void lua_pushcomponent(lua_State *L, newtComponent com, int type) {
  23. if (com) {
  24. component p = lua_newncom(L);
  25. luaL_setmetatable(L, TYPE_COMPONENT);
  26. p->p = com;
  27. p->t = type;
  28. } else {
  29. lua_pushnil(L);
  30. }
  31. }
  32. /* register component tag */
  33. void lua_regtag(lua_State *L, newtComponent com, const char *tag) {
  34. char id[32];
  35. snprintf(id, 32, "newt.%p", (void *)(com));
  36. lua_pushstring(L, id);
  37. if (tag != NULL) lua_pushstring(L, tag);
  38. else lua_pushnil(L);
  39. lua_settable(L, LUA_REGISTRYINDEX);
  40. }
  41. /* push component tag onto Lua stack */
  42. void lua_pushtag(lua_State *L, newtComponent com) {
  43. char id[20];
  44. sprintf(id, "newt.%p", (void *)(com));
  45. lua_pushstring(L, id);
  46. lua_gettable(L, LUA_REGISTRYINDEX);
  47. }
  48. /** module registration **/
  49. /* base functions */
  50. static const luaL_Reg R_newt_functions[] = {
  51. {"Init", L_Init},
  52. {"Cls", L_Cls},
  53. {"WaitForKey", L_WaitForKey},
  54. {"ClearKeyBuffer", L_ClearKeyBuffer},
  55. {"DrawRootText", L_DrawRootText},
  56. {"OpenWindow", L_OpenWindow},
  57. {"CenteredWindow", L_CenteredWindow},
  58. {"PopWindow", L_PopWindow},
  59. {"PushHelpLine", L_PushHelpLine},
  60. {"PopHelpLine", L_PopHelpLine},
  61. {"Refresh", L_Refresh},
  62. {"Finished", L_Finished},
  63. {"WinMessage", L_WinMessage},
  64. {"Suspend", L_Suspend},
  65. {"Resume", L_Resume},
  66. {"SetSuspendCallback", L_SetSuspendCallback},
  67. {"Bell", L_Bell},
  68. {"CursorOff", L_CursorOff},
  69. {"CursorOn", L_CursorOn},
  70. {"Delay", L_Delay},
  71. {"GetScreenSize", L_GetScreenSize},
  72. {"ReflowText", L_ReflowText},
  73. {"Button", L_Button},
  74. {"CompactButton", L_CompactButton},
  75. {"Checkbox", L_Checkbox},
  76. {"Entry", L_Entry},
  77. {"Form", L_Form},
  78. {"Label", L_Label},
  79. {"Textbox", L_Textbox},
  80. {"TextboxReflowed", L_TextboxReflowed},
  81. {"Listbox", L_Listbox},
  82. {"Radiobutton", L_Radiobutton},
  83. {"Scale", L_Scale},
  84. {"VerticalScrollbar", L_VerticalScrollbar},
  85. {"ClearSelection", L_ClearSelection},
  86. {"DeleteEntry", L_DeleteEntry},
  87. {"GetSelection", L_GetSelection},
  88. {"GetNumLines ", L_GetNumLines },
  89. {"InsertEntry", L_InsertEntry},
  90. {"SelectItem", L_SelectItem},
  91. {"SetValue", L_SetValue},
  92. {"SetBackground", L_SetBackground},
  93. {"SetHeight", L_SetHeight},
  94. {"SetCurrent", L_SetCurrent},
  95. {"SetCurrentByKey", L_SetCurrentByKey},
  96. {"SetEntry", L_SetEntry},
  97. {"SetWidth", L_SetWidth},
  98. {NULL, NULL}
  99. };
  100. /* Newt.Component methods */
  101. static const luaL_Reg R_comp_methods[] = {
  102. {"AddCallback", L_AddCallback},
  103. {"AddComponents", L_AddComponents},
  104. {"AddHotKey", L_AddHotKey},
  105. {"AppendEntry", L_AppendEntry},
  106. {"Clear", L_Clear},
  107. {"Destroy", L_Destroy},
  108. {"Draw", L_Draw},
  109. {"GetCurrent", L_GetCurrent},
  110. {"GetValue", L_GetValue},
  111. {"ID", L_ID},
  112. {"Run", L_Run},
  113. {"Set", L_Set},
  114. {"SetText", L_SetText},
  115. {"SetTimer", L_SetTimer},
  116. {"SetType", L_SetType},
  117. {"TakesFocus", L_TakesFocus},
  118. {"Text", L_Text},
  119. {NULL, NULL}
  120. };
  121. LUALIB_API int luaopen_newt(lua_State *L) {
  122. /* Newt.Component type & methods */
  123. luaL_newmetatable(L, TYPE_COMPONENT);
  124. lua_pushvalue(L, -1);
  125. lua_setfield(L, -2, "__index");
  126. luaL_setfuncs(L, R_comp_methods, 0);
  127. /* register the base functions and module flags */
  128. luaL_newlib(L, R_newt_functions);
  129. lua_pushliteral(L, MYVERSION);
  130. lua_setfield(L, -2, "version"); /** version */
  131. #define luaL_push_const(name, value) \
  132. lua_pushinteger(L, value); \
  133. lua_setfield(L, -2, name)
  134. luaL_push_const("FLAG_RETURNEXIT", NEWT_FLAG_RETURNEXIT);
  135. luaL_push_const("FLAG_HIDDEN", NEWT_FLAG_HIDDEN);
  136. luaL_push_const("FLAG_SCROLL", NEWT_FLAG_SCROLL);
  137. luaL_push_const("FLAG_DISABLED", NEWT_FLAG_DISABLED);
  138. luaL_push_const("FLAG_BORDER", NEWT_FLAG_BORDER);
  139. luaL_push_const("FLAG_WRAP", NEWT_FLAG_WRAP);
  140. luaL_push_const("FLAG_NOF12", NEWT_FLAG_NOF12);
  141. luaL_push_const("FLAG_MULTIPLE", NEWT_FLAG_MULTIPLE);
  142. luaL_push_const("FLAG_SELECTED", NEWT_FLAG_SELECTED);
  143. luaL_push_const("FLAG_CHECKBOX", NEWT_FLAG_CHECKBOX);
  144. luaL_push_const("FLAG_PASSWORD", NEWT_FLAG_PASSWORD);
  145. luaL_push_const("FLAG_SHOWCURSOR", NEWT_FLAG_SHOWCURSOR);
  146. luaL_push_const("FLAG_CHECKBOXTREE_UNSELECTABLE", NEWT_CHECKBOXTREE_UNSELECTABLE);
  147. luaL_push_const("FLAG_CHECKBOXTREE_HIDE_BOX", NEWT_CHECKBOXTREE_HIDE_BOX);
  148. luaL_push_const("EXIT_HOTKEY", NEWT_EXIT_HOTKEY);
  149. luaL_push_const("EXIT_COMPONENT", NEWT_EXIT_COMPONENT);
  150. luaL_push_const("EXIT_FDREADY", NEWT_EXIT_FDREADY);
  151. luaL_push_const("EXIT_TIMER", NEWT_EXIT_TIMER);
  152. luaL_push_const("KEY_ESCAPE", NEWT_KEY_ESCAPE);
  153. luaL_push_const("KEY_RETURN", NEWT_KEY_RETURN);
  154. luaL_push_const("KEY_PGDN", NEWT_KEY_PGDN);
  155. luaL_push_const("KEY_PGUP", NEWT_KEY_PGUP);
  156. luaL_push_const("KEY_F1", NEWT_KEY_F1);
  157. luaL_push_const("KEY_F2", NEWT_KEY_F2);
  158. luaL_push_const("KEY_F3", NEWT_KEY_F3);
  159. luaL_push_const("KEY_F4", NEWT_KEY_F4);
  160. luaL_push_const("KEY_F5", NEWT_KEY_F5);
  161. luaL_push_const("KEY_F6", NEWT_KEY_F6);
  162. luaL_push_const("KEY_F7", NEWT_KEY_F7);
  163. luaL_push_const("KEY_F8", NEWT_KEY_F8);
  164. luaL_push_const("KEY_F9", NEWT_KEY_F9);
  165. luaL_push_const("KEY_F10", NEWT_KEY_F10);
  166. luaL_push_const("KEY_F11", NEWT_KEY_F11);
  167. luaL_push_const("KEY_F12", NEWT_KEY_F12);
  168. luaL_push_const("TYPE_UNKNOWN", TYPE_UNKNOWN);
  169. luaL_push_const("TYPE_FORM", TYPE_FORM);
  170. luaL_push_const("TYPE_LABEL", TYPE_LABEL);
  171. luaL_push_const("TYPE_ENTRY", TYPE_ENTRY);
  172. luaL_push_const("TYPE_BUTTON", TYPE_BUTTON);
  173. luaL_push_const("TYPE_CHECKBOX", TYPE_CHECKBOX);
  174. luaL_push_const("TYPE_RADIOBUTTON", TYPE_RADIOBUTTON);
  175. luaL_push_const("TYPE_LISTBOX", TYPE_LISTBOX);
  176. luaL_push_const("TYPE_SCALE", TYPE_SCALE);
  177. return 1;
  178. }
  179. /** root functions **/
  180. /* bool Init() */
  181. static int L_Init(lua_State *L) {
  182. int result;
  183. result = newtInit();
  184. lua_pushboolean(L, result);
  185. return 1;
  186. }
  187. /* Cls() */
  188. static int L_Cls(lua_State *L) {
  189. newtCls();
  190. return 0;
  191. }
  192. /* WaitForKey() */
  193. static int L_WaitForKey(lua_State *L) {
  194. newtWaitForKey();
  195. return 0;
  196. }
  197. /* ClearKeyBuffer() */
  198. static int L_ClearKeyBuffer(lua_State *L) {
  199. newtClearKeyBuffer();
  200. return 0;
  201. }
  202. /* DrawRootText(left, top, text) */
  203. static int L_DrawRootText(lua_State *L) {
  204. int left = (int)luaL_checknumber(L, 1);
  205. int top = (int)luaL_checknumber(L, 2);
  206. const char * text = luaL_checkstring(L, 3);
  207. newtDrawRootText(left, top, text);
  208. return 0;
  209. }
  210. /* newtOpenWindow(left, top, width, height, [title]) */
  211. static int L_OpenWindow(lua_State *L) {
  212. int left = luaL_checkinteger(L, 1);
  213. int top = luaL_checkinteger(L, 2);
  214. int width = luaL_checkinteger(L, 3);
  215. int height = luaL_checkinteger(L, 4);
  216. const char *title;
  217. if ((lua_gettop(L) < 5) || (lua_isnil(L, 5) == 1)) {
  218. title = NULL;
  219. } else {
  220. title = luaL_checkstring(L, 5);
  221. }
  222. int result = newtOpenWindow(left, top, width, height, title);
  223. lua_pushboolean(L, result);
  224. return 1;
  225. }
  226. /* bool newtCenteredWindow(width, height, [title]) */
  227. static int L_CenteredWindow(lua_State *L) {
  228. int result;
  229. int width; int height;
  230. const char *title;
  231. width = luaL_checkinteger(L, 1);
  232. height = luaL_checkinteger(L, 2);
  233. if ((lua_gettop(L) < 3) || (lua_isnil(L, 3) == 1)) title = NULL;
  234. else title = luaL_checkstring(L, 3);
  235. result = newtCenteredWindow(width, height, title);
  236. lua_pushboolean(L, result);
  237. return 1;
  238. }
  239. /* PopWindow() */
  240. static int L_PopWindow(lua_State *L) {
  241. newtPopWindow();
  242. return 0;
  243. }
  244. /* PushHelpLine([text]) */
  245. static int L_PushHelpLine(lua_State *L) {
  246. const char *text;
  247. if ((lua_gettop(L) < 1) || (lua_isnil(L, 1) == 1)) text = NULL;
  248. else text = luaL_checkstring(L, 1);
  249. newtPushHelpLine(text);
  250. return 0;
  251. }
  252. /* PopHelpLine() */
  253. static int L_PopHelpLine(lua_State *L) {
  254. newtPopHelpLine();
  255. return 0;
  256. }
  257. /* Refresh() */
  258. static int L_Refresh(lua_State *L) {
  259. newtRefresh();
  260. return 0;
  261. }
  262. /* bool Finished() */
  263. static int L_Finished(lua_State *L) {
  264. int result;
  265. result = newtFinished();
  266. lua_pushboolean(L, result);
  267. return 1;
  268. }
  269. /* Suspend() */
  270. static int L_Suspend(lua_State *L) {
  271. newtSuspend();
  272. return 0;
  273. }
  274. /* Resume */
  275. static int L_Resume(lua_State *L) {
  276. newtResume();
  277. return 0;
  278. }
  279. /* Bell() */
  280. static int L_Bell(lua_State *L) {
  281. newtBell();
  282. return 0;
  283. }
  284. /* CursorOff() */
  285. static int L_CursorOff(lua_State *L) {
  286. newtCursorOff();
  287. return 0;
  288. }
  289. /* CursorOn() */
  290. static int L_CursorOn(lua_State *L) {
  291. newtCursorOn();
  292. return 0;
  293. }
  294. /* Delay() */
  295. static int L_Delay(lua_State *L) {
  296. int msec;
  297. msec = luaL_checkinteger(L, 1);
  298. newtDelay(msec * 1000);
  299. return 0;
  300. }
  301. /* cols, rows = GetScreenSize() */
  302. static int L_GetScreenSize(lua_State *L) {
  303. int cols; int rows;
  304. newtGetScreenSize(&cols, &rows);
  305. lua_pushinteger(L, cols);
  306. lua_pushinteger(L, rows);
  307. return 2;
  308. }
  309. /* widget functions */
  310. /* com = Button(left, top, text) */
  311. static int L_Button(lua_State *L) {
  312. newtComponent result;
  313. int left; int top;
  314. const char *text;
  315. left = luaL_checkinteger(L, 1);
  316. top = luaL_checkinteger(L, 2);
  317. text = luaL_checkstring(L, 3);
  318. result = newtButton(left, top, text);
  319. lua_regtag(L, result, text);
  320. lua_pushcomponent(L, result, TYPE_BUTTON);
  321. return 1;
  322. }
  323. /* com = CompactButton(left, top, text) */
  324. static int L_CompactButton(lua_State *L) {
  325. int left; int top;
  326. const char *text;
  327. newtComponent result;
  328. left = luaL_checkinteger(L, 1);
  329. top = luaL_checkinteger(L, 2);
  330. text = luaL_checkstring(L, 3);
  331. result = newtCompactButton(left, top, text);
  332. lua_regtag(L, result, text);
  333. lua_pushcomponent(L, result, TYPE_BUTTON);
  334. return 1;
  335. }
  336. /* com = Checkbox(left, top, text, [checked]) */
  337. static int L_Checkbox(lua_State *L) {
  338. int left; int top;
  339. const char *text;
  340. bool checked;
  341. newtComponent result;
  342. left = luaL_checkinteger(L, 1);
  343. top = luaL_checkinteger(L, 2);
  344. text = luaL_checkstring(L, 3);
  345. if (lua_gettop(L) < 4 || lua_isnil(L, 4) == 1) checked = false;
  346. else checked = lua_toboolean(L, 4);
  347. if (checked == false) result = newtCheckbox(left, top, text, ' ', " *", NULL);
  348. else result = newtCheckbox(left, top, text, '*', " *", NULL);
  349. lua_regtag(L, result, text);
  350. lua_pushcomponent(L, result, TYPE_CHECKBOX);
  351. return 1;
  352. }
  353. /* com = Entry(left, top, value, width, [flags])*/
  354. static int L_Entry(lua_State *L) {
  355. int left; int top;
  356. const char *value;
  357. int width; int flags;
  358. newtComponent result;
  359. left = luaL_checkinteger(L, 1);
  360. top = luaL_checkinteger(L, 2);
  361. if (lua_isnil(L, 3) == 1) value = NULL;
  362. else value = luaL_checkstring(L, 3);
  363. width = luaL_checkinteger(L, 4);
  364. if (lua_gettop(L) < 5 || lua_isnil(L, 5) == 1) flags = 0;
  365. else flags = luaL_checkinteger(L, 5);
  366. result = newtEntry(left, top, value, width, NULL, flags);
  367. lua_pushcomponent(L, result, TYPE_ENTRY);
  368. return 1;
  369. }
  370. /* com = Form([vertBar], [help], [flags]) */
  371. static int L_Form(lua_State *L) {
  372. newtComponent vertBar;
  373. const char *help;
  374. int flags;
  375. newtComponent result;
  376. if (lua_gettop(L) < 1 || lua_isnil(L, 1) == 1) vertBar = NULL;
  377. else vertBar = luaL_checkcomponent(L, 1)->p;
  378. if (lua_gettop(L) < 2 || lua_isnil(L, 2) == 1) help = NULL;
  379. else help = luaL_checkstring(L, 2);
  380. if (lua_gettop(L) < 3 || lua_isnil(L, 3) == 1) flags = 0;
  381. else flags = luaL_checkinteger(L, 3);
  382. result = newtForm(vertBar, (void *)help, flags);
  383. lua_pushcomponent(L, result, TYPE_FORM);
  384. return 1;
  385. }
  386. /* com = Label(left, top, text) */
  387. static int L_Label(lua_State *L) {
  388. int left; int top;
  389. const char *text;
  390. newtComponent result;
  391. left = luaL_checkinteger(L, 1);
  392. top = luaL_checkinteger(L, 2);
  393. text = luaL_checkstring(L, 3);
  394. result = newtLabel(left, top, text);
  395. lua_pushcomponent(L, result, TYPE_LABEL);
  396. return 1;
  397. }
  398. /* textbox = Textbox(left, top, width, height, [flags]) */
  399. static int L_Textbox(lua_State *L) {
  400. int left; int top;
  401. int width; int height;
  402. int flags;
  403. newtComponent result;
  404. left = luaL_checkinteger(L, 1);
  405. top = luaL_checkinteger(L, 2);
  406. width = luaL_checkinteger(L, 3);
  407. height = luaL_checkinteger(L, 4);
  408. if (lua_gettop(L) < 5 || lua_isnil(L, 5) == 1) flags = 0;
  409. else flags = luaL_checkinteger(L, 5);
  410. result = newtTextbox(left, top, width, height, flags);
  411. lua_pushcomponent(L, result, TYPE_TEXTBOX);
  412. return 1;
  413. }
  414. /* list = Listbox(left, top, height, flags) */
  415. static int L_Listbox(lua_State *L) {
  416. int left; int top;
  417. int height; int flags;
  418. newtComponent result;
  419. left = luaL_checkinteger(L, 1);
  420. top = luaL_checkinteger(L, 2);
  421. height = luaL_checkinteger(L, 3);
  422. if (lua_gettop(L) < 4 || lua_isnil(L, 4) == 1) flags = 0;
  423. else flags = luaL_checkinteger(L, 4);
  424. result = newtListbox(left, top, height, flags);
  425. lua_pushcomponent(L, result, TYPE_LISTBOX);
  426. return 1;
  427. }
  428. /* com = Radiobutton(left, top, text, [selected], [prev]) */
  429. static int L_Radiobutton(lua_State *L) {
  430. int left; int top;
  431. const char *text;
  432. bool selected;
  433. component com;
  434. newtComponent prev;
  435. newtComponent result;
  436. left = luaL_checkinteger(L, 1);
  437. top = luaL_checkinteger(L, 2);
  438. text = luaL_checkstring(L, 3);
  439. if (lua_gettop(L) < 4 || lua_isnil(L, 4) == 1) selected = false;
  440. else selected = lua_toboolean(L, 4);
  441. if (lua_gettop(L) < 5 || lua_isnil(L, 5) == 1) prev = NULL;
  442. else {
  443. com = luaL_checkcomponent(L, 5);
  444. if (com->t != TYPE_RADIOBUTTON) return luaL_error(L, "Previous component must be a Radiobutton");
  445. prev = com->p;
  446. }
  447. result = newtRadiobutton(left, top, text, selected, prev);
  448. lua_regtag(L, result, text);
  449. lua_pushcomponent(L, result, TYPE_RADIOBUTTON);
  450. return 1;
  451. }
  452. /* com = Scale(left, top, width, max) */
  453. static int L_Scale(lua_State *L) {
  454. int left; int top;
  455. int width; lua_Integer max;
  456. newtComponent result;
  457. left = luaL_checkinteger(L, 1);
  458. top = luaL_checkinteger(L, 2);
  459. width = luaL_checkinteger(L, 3);
  460. max = luaL_checkinteger(L, 4);
  461. result = newtScale(left, top, width, max);
  462. lua_pushcomponent(L, result, TYPE_SCALE);
  463. return 1;
  464. }
  465. static int L_VerticalScrollbar(lua_State *L) {
  466. return 0;
  467. }
  468. /** Newt.Component object **/
  469. /* AddComponents(com, ...) */
  470. static int L_AddComponents(lua_State *L) {
  471. int argi; int argc;
  472. component form;
  473. component com;
  474. form = luaL_checkcomponent(L, 1);
  475. if (form->t != TYPE_FORM) return luaL_error(L, "Invalid Method");
  476. argc = lua_gettop(L);
  477. argi = 2;
  478. while ((argc > 1) && (argi <= argc)) {
  479. if ((lua_type(L, argi) == LUA_TUSERDATA) && (luaL_testudata(L, argi, TYPE_COMPONENT) != NULL)) {
  480. com = lua_toncom(L, argi);
  481. newtFormAddComponent(form->p, com->p);
  482. } else if (lua_type(L, argi) == LUA_TTABLE) {
  483. /* iterate through the array */
  484. lua_pushnil(L); /* first key */
  485. while (lua_next(L, argi) != 0) {
  486. if ((lua_type(L, -1) == LUA_TUSERDATA) && (luaL_testudata(L, -1, TYPE_COMPONENT) != NULL)) {
  487. com = lua_toncom(L, -1);
  488. newtFormAddComponent(form->p, com->p);
  489. }
  490. /* removes 'value'; keeps 'key' for next iteration */
  491. lua_pop(L, 1);
  492. }
  493. }
  494. argi++;
  495. }
  496. return 0;
  497. }
  498. /* form:AddHotKey(key) */
  499. /* form:AddHotKey({key, ...}) */
  500. static int L_AddHotKey(lua_State *L) {
  501. component com;
  502. int key;
  503. com = luaL_checkcomponent(L, 1);
  504. if (com->t != TYPE_FORM) return luaL_error(L, "Invalid Method");
  505. if (lua_type(L, 2) == LUA_TTABLE) {
  506. /* iterate through the array */
  507. lua_pushnil(L); /* first key */
  508. while (lua_next(L, 2) != 0) {
  509. key = luaL_checkinteger(L, -1);
  510. newtFormAddHotKey(com->p, key);
  511. /* removes 'value'; keeps 'key' for next iteration */
  512. lua_pop(L, 1);
  513. }
  514. } else {
  515. key = luaL_checkinteger(L, 2);
  516. newtFormAddHotKey(com->p, key);
  517. }
  518. return 0;
  519. }
  520. /* listbox:AppendEntry(text, [index]) */
  521. /* listbox:AppendEntry({text, text}, [startindex]) */
  522. static int L_AppendEntry(lua_State *L) {
  523. component com;
  524. const char *text;
  525. size_t key;
  526. com = luaL_checkcomponent(L, 1);
  527. if (com->t != TYPE_LISTBOX) return luaL_error(L, "Invalid Method");
  528. if (lua_gettop(L) < 3 || lua_isnil(L, 3) == 1)
  529. /* hack for newtListItemCount */
  530. key = ((struct listbox *)com->p->data)->numItems + 1;
  531. else key = luaL_checkinteger(L, 3);
  532. if (lua_type(L, 2) == LUA_TTABLE) {
  533. /* iterate through the array */
  534. lua_pushnil(L); /* first key */
  535. while (lua_next(L, 2) != 0) {
  536. text = luaL_checkstring(L, -1);
  537. newtListboxAppendEntry(com->p, text, (const void *)key);
  538. /* removes 'value'; keeps 'key' for next iteration */
  539. lua_pop(L, 1);
  540. key++;
  541. }
  542. } else {
  543. text = luaL_checkstring(L, 2);
  544. newtListboxAppendEntry(com->p, text, (const void *)key);
  545. }
  546. return 0;
  547. }
  548. /* listbox:Clear() */
  549. static int L_Clear(lua_State *L) {
  550. component com;
  551. com = luaL_checkcomponent(L, 1);
  552. if (com->t == TYPE_LISTBOX) return luaL_error(L, "Invalid Method");
  553. newtListboxClear(com->p);
  554. return 0;
  555. }
  556. static int L_Destroy(lua_State *L) {
  557. component form;
  558. form = luaL_checkcomponent(L, 1);
  559. if (form->t != TYPE_FORM) return luaL_error(L, "Invalid Method");
  560. newtFormDestroy(form->p);
  561. return 0;
  562. }
  563. /* form:Draw() */
  564. static int L_Draw(lua_State *L) {
  565. component form;
  566. form = luaL_checkcomponent(L, 1);
  567. if (form->t != TYPE_FORM) return luaL_error(L, "Invalid Method");
  568. newtDrawForm(form->p);
  569. return 0;
  570. }
  571. /* com = radiobutton:GetCurrent() */
  572. static int L_GetCurrent(lua_State *L) {
  573. component com;
  574. size_t iresult;
  575. newtComponent cresult;
  576. com = luaL_checkcomponent(L, 1);
  577. switch (com->t) {
  578. case TYPE_RADIOBUTTON:
  579. cresult = newtRadioGetCurrent(com->p);
  580. lua_pushcomponent(L, cresult, TYPE_RADIOBUTTON);
  581. break;
  582. case TYPE_LISTBOX:
  583. iresult = (size_t)newtListboxGetCurrent(com->p);
  584. lua_pushinteger(L, iresult);
  585. break;
  586. default:
  587. return luaL_error(L, "Invalid Method");
  588. }
  589. return 1;
  590. }
  591. /* value = entry:GetValue() */
  592. /* value = checkbox:GetValue() */
  593. static int L_GetValue(lua_State *L) {
  594. component com;
  595. char c;
  596. com = luaL_checkcomponent(L, 1);
  597. switch (com->t) {
  598. case TYPE_ENTRY:
  599. lua_pushstring(L, newtEntryGetValue(com->p));
  600. break;
  601. case TYPE_CHECKBOX:
  602. c = newtCheckboxGetValue(com->p);
  603. if (c == ' ') lua_pushboolean(L, false);
  604. else lua_pushboolean(L, true);
  605. break;
  606. default:
  607. return luaL_error(L, "Invalid Method");
  608. }
  609. return 1;
  610. }
  611. /* hex = com:ID() */
  612. static int L_ID(lua_State *L) {
  613. component com;
  614. char result[20];
  615. com = luaL_checkcomponent(L, 1);
  616. sprintf(result, "%p", (void *)(com->p));
  617. lua_pushstring(L, result);
  618. return 1;
  619. }
  620. /* reason, value = form:Run() */
  621. static int L_Run(lua_State *L) {
  622. component form;
  623. struct newtExitStruct result;
  624. form = luaL_checkcomponent(L, 1);
  625. if (form->t != TYPE_FORM) return luaL_error(L, "Invalid Method");
  626. newtFormRun(form->p, &result);
  627. lua_pushinteger(L, (int)result.reason);
  628. if (result.reason == NEWT_EXIT_COMPONENT) {
  629. lua_pushcomponent(L, result.u.co, TYPE_UNKNOWN);
  630. } else {
  631. lua_pushinteger(L, result.u.key);
  632. }
  633. return 2;
  634. }
  635. /* entry:Set(value, [cursoratend]) */
  636. /* scale:Set(value) */
  637. static int L_Set(lua_State *L) {
  638. component com;
  639. const char *svalue;
  640. bool cursoratend;
  641. lua_Integer ivalue;
  642. com = luaL_checkcomponent(L, 1);
  643. switch (com->t) {
  644. case TYPE_ENTRY:
  645. svalue = luaL_checkstring(L, 2);
  646. if (lua_gettop(L) < 3) cursoratend = false;
  647. else cursoratend = lua_toboolean(L, 3);
  648. newtEntrySet(com->p, svalue, cursoratend);
  649. break;
  650. case TYPE_SCALE:
  651. ivalue = luaL_checkinteger(L, 2);
  652. newtScaleSet(com->p, ivalue);
  653. break;
  654. default:
  655. return luaL_error(L, "Invalid Method");
  656. }
  657. return 0;
  658. }
  659. /* com = com:SetType(type) */
  660. static int L_SetType(lua_State *L) {
  661. component com;
  662. int type;
  663. com = luaL_checkcomponent(L, 1);
  664. type = luaL_checkinteger(L, 2);
  665. lua_pushcomponent(L, com->p, type);
  666. return 1;
  667. }
  668. /* form:SetTimer(millisecs) */
  669. static int L_SetTimer(lua_State *L) {
  670. component com;
  671. int period;
  672. com = luaL_checkcomponent(L, 1);
  673. period = luaL_checkinteger(L, 2);
  674. switch (com->t) {
  675. case TYPE_FORM:
  676. newtFormSetTimer(com->p, period);
  677. break;
  678. default:
  679. return luaL_error(L, "Invalid Method");
  680. }
  681. return 0;
  682. }
  683. /* label:SetText(text) */
  684. static int L_SetText(lua_State *L) {
  685. component com;
  686. const char *text;
  687. com = luaL_checkcomponent(L, 1);
  688. text = luaL_checkstring(L, 2);
  689. switch (com->t) {
  690. case TYPE_LABEL:
  691. newtLabelSetText(com->p, text);
  692. break;
  693. case TYPE_TEXTBOX:
  694. newtTextboxSetText(com->p, text);
  695. break;
  696. default:
  697. return luaL_error(L, "Invalid Method");
  698. }
  699. return 0;
  700. }
  701. /* com:TakesFocus(bool) */
  702. static int L_TakesFocus(lua_State *L) {
  703. component com;
  704. int val;
  705. com = luaL_checkcomponent(L, 1);
  706. if (lua_gettop(L) < 2) val = true;
  707. else val = lua_toboolean(L, 2);
  708. newtComponentTakesFocus(com->p, val);
  709. return 0;
  710. }
  711. /* tag = com:Text() */
  712. static int L_Text(lua_State *L) {
  713. component com;
  714. com = luaL_checkcomponent(L, 1);
  715. lua_pushtag(L, com->p);
  716. return 1;
  717. }
  718. static int L_WinMessage(lua_State *L) {
  719. return 0;
  720. }
  721. static int L_SetSuspendCallback(lua_State *L) {
  722. return 0;
  723. }
  724. static int L_ReflowText(lua_State *L) {
  725. return 0;
  726. }
  727. static int L_TextboxReflowed(lua_State *L) {
  728. return 0;
  729. }
  730. static int L_AddCallback(lua_State *L) {
  731. return 0;
  732. }
  733. static int L_ClearSelection(lua_State *L) {
  734. return 0;
  735. }
  736. static int L_DeleteEntry(lua_State *L) {
  737. return 0;
  738. }
  739. static int L_SelectItem(lua_State *L) {
  740. return 0;
  741. }
  742. static int L_GetSelection(lua_State *L) {
  743. return 0;
  744. }
  745. static int L_GetNumLines(lua_State *L) {
  746. return 0;
  747. }
  748. static int L_InsertEntry(lua_State *L) {
  749. return 0;
  750. }
  751. static int L_SetValue(lua_State *L) {
  752. return 0;
  753. }
  754. static int L_SetBackground(lua_State *L) {
  755. return 0;
  756. }
  757. static int L_SetHeight(lua_State *L) {
  758. return 0;
  759. }
  760. static int L_SetCurrent(lua_State *L) {
  761. return 0;
  762. }
  763. static int L_SetCurrentByKey(lua_State *L) {
  764. return 0;
  765. }
  766. static int L_SetEntry(lua_State *L) {
  767. return 0;
  768. }
  769. static int L_SetWidth(lua_State *L) {
  770. return 0;
  771. }