demo_forms.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /****************************************************************************
  2. * Copyright (c) 2003-2007,2008 Free Software Foundation, Inc. *
  3. * *
  4. * Permission is hereby granted, free of charge, to any person obtaining a *
  5. * copy of this software and associated documentation files (the *
  6. * "Software"), to deal in the Software without restriction, including *
  7. * without limitation the rights to use, copy, modify, merge, publish, *
  8. * distribute, distribute with modifications, sublicense, and/or sell *
  9. * copies of the Software, and to permit persons to whom the Software is *
  10. * furnished to do so, subject to the following conditions: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included *
  13. * in all copies or substantial portions of the Software. *
  14. * *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  18. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  21. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  22. * *
  23. * Except as contained in this notice, the name(s) of the above copyright *
  24. * holders shall not be used in advertising or otherwise to promote the *
  25. * sale, use or other dealings in this Software without prior written *
  26. * authorization. *
  27. ****************************************************************************/
  28. /*
  29. * $Id: demo_forms.c,v 1.30 2008/10/18 20:38:20 tom Exp $
  30. *
  31. * Demonstrate a variety of functions from the form library.
  32. * Thomas Dickey - 2003/4/26
  33. */
  34. /*
  35. TYPE_ENUM -
  36. TYPE_REGEXP -
  37. dup_field -
  38. field_init -
  39. field_just -
  40. field_term -
  41. form_init -
  42. form_opts -
  43. form_opts_off -
  44. form_opts_on -
  45. form_request_by_name -
  46. form_term -
  47. form_userptr -
  48. free_fieldtype -
  49. link_field -
  50. link_fieldtype -
  51. move_field -
  52. new_page -
  53. pos_form_cursor -
  54. set_field_init -
  55. set_field_term -
  56. set_fieldtype_arg -
  57. set_fieldtype_choice -
  58. set_form_fields -
  59. set_form_init -
  60. set_form_opts -
  61. set_form_page -
  62. set_form_term -
  63. set_form_userptr -
  64. set_max_field -
  65. */
  66. #include <test.priv.h>
  67. #if USE_LIBFORM
  68. #include <edit_field.h>
  69. static int d_option = 0;
  70. static int j_value = 0;
  71. static int m_value = 0;
  72. static int o_value = 0;
  73. static char *t_value = 0;
  74. static FIELD *
  75. make_label(int frow, int fcol, NCURSES_CONST char *label)
  76. {
  77. FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
  78. if (f) {
  79. set_field_buffer(f, 0, label);
  80. set_field_opts(f, (int) (field_opts(f) & ~O_ACTIVE));
  81. }
  82. return (f);
  83. }
  84. /*
  85. * Define each field with an extra one, for reflecting "actual" text.
  86. */
  87. static FIELD *
  88. make_field(int frow, int fcol, int rows, int cols)
  89. {
  90. FIELD *f = new_field(rows, cols, frow, fcol, o_value, 1);
  91. if (f) {
  92. set_field_back(f, A_UNDERLINE);
  93. /*
  94. * If -j and -d options are combined, -j loses. It is documented in
  95. * "Character User Interface Programming", page 12-15 that setting
  96. * O_STATIC off makes the form library ignore justification.
  97. */
  98. set_field_just(f, j_value);
  99. if (d_option) {
  100. if (has_colors()) {
  101. set_field_fore(f, COLOR_PAIR(2));
  102. set_field_back(f, A_UNDERLINE | COLOR_PAIR(3));
  103. } else {
  104. set_field_fore(f, A_BOLD);
  105. }
  106. /*
  107. * The field_opts_off() call dumps core with Solaris curses,
  108. * but that is a known bug in Solaris' form library -TD
  109. */
  110. field_opts_off(f, O_STATIC);
  111. set_max_field(f, m_value);
  112. }
  113. /*
  114. * The userptr is used in edit_field.c's inactive_field().
  115. */
  116. set_field_userptr(f, (void *) (long) field_back(f));
  117. if (t_value)
  118. set_field_buffer(f, 0, t_value);
  119. }
  120. return (f);
  121. }
  122. static void
  123. display_form(FORM * f)
  124. {
  125. WINDOW *w;
  126. int rows, cols;
  127. scale_form(f, &rows, &cols);
  128. /*
  129. * Put the form at the upper-left corner of the display, with just a box
  130. * around it.
  131. */
  132. if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
  133. set_form_win(f, w);
  134. set_form_sub(f, derwin(w, rows, cols, 1, 2));
  135. box(w, 0, 0);
  136. keypad(w, TRUE);
  137. }
  138. if (post_form(f) != E_OK)
  139. wrefresh(w);
  140. }
  141. static void
  142. erase_form(FORM * f)
  143. {
  144. WINDOW *w = form_win(f);
  145. WINDOW *s = form_sub(f);
  146. unpost_form(f);
  147. werase(w);
  148. wrefresh(w);
  149. delwin(s);
  150. delwin(w);
  151. }
  152. static void
  153. show_insert_mode(bool insert_mode)
  154. {
  155. mvaddstr(5, 57, (insert_mode
  156. ? "form_status: insert "
  157. : "form_status: overlay"));
  158. }
  159. #define O_SELECTABLE (O_ACTIVE | O_VISIBLE)
  160. static FIELD *
  161. another_field(FORM * form, FIELD * field)
  162. {
  163. FIELD **f = form_fields(form);
  164. FIELD *result = 0;
  165. int n;
  166. for (n = 0; f[n] != 0; ++n) {
  167. if (f[n] != field) {
  168. result = f[n];
  169. field_opts_on(result, O_SELECTABLE);
  170. break;
  171. }
  172. }
  173. return result;
  174. }
  175. static int
  176. my_form_driver(FORM * form, int c)
  177. {
  178. static bool insert_mode = TRUE;
  179. FIELD *field;
  180. switch (c) {
  181. case MY_QUIT:
  182. if (form_driver(form, REQ_VALIDATION) == E_OK)
  183. return (TRUE);
  184. break;
  185. case MY_HELP:
  186. help_edit_field();
  187. break;
  188. case MY_EDT_MODE:
  189. if ((field = current_field(form)) != 0) {
  190. set_current_field(form, another_field(form, field));
  191. if (field_opts(field) & O_EDIT) {
  192. field_opts_off(field, O_EDIT);
  193. set_field_status(field, 0);
  194. } else {
  195. field_opts_on(field, O_EDIT);
  196. }
  197. set_current_field(form, field);
  198. }
  199. break;
  200. case MY_INS_MODE:
  201. /* there should be a form_status() function, but there is none */
  202. if (!insert_mode) {
  203. if (form_driver(form, REQ_INS_MODE) == E_OK) {
  204. insert_mode = TRUE;
  205. }
  206. } else {
  207. if (form_driver(form, REQ_OVL_MODE) == E_OK) {
  208. insert_mode = FALSE;
  209. }
  210. }
  211. show_insert_mode(insert_mode);
  212. refresh();
  213. break;
  214. default:
  215. beep();
  216. break;
  217. }
  218. return (FALSE);
  219. }
  220. static void
  221. show_current_field(WINDOW *win, FORM * form)
  222. {
  223. FIELD *field;
  224. FIELDTYPE *type;
  225. char *buffer;
  226. int nbuf;
  227. int field_rows, field_cols, field_max;
  228. if (has_colors()) {
  229. wbkgd(win, COLOR_PAIR(1));
  230. }
  231. werase(win);
  232. wprintw(win, "Cursor: %d,%d", form->currow, form->curcol);
  233. if (data_ahead(form))
  234. waddstr(win, " ahead");
  235. if (data_behind(form))
  236. waddstr(win, " behind");
  237. waddch(win, '\n');
  238. if ((field = current_field(form)) != 0) {
  239. wprintw(win, "Page %d%s, Field %d/%d%s:",
  240. form_page(form),
  241. new_page(field) ? "*" : "",
  242. field_index(field), field_count(form),
  243. field_arg(field) ? "(arg)" : "");
  244. if ((type = field_type(field)) != 0) {
  245. if (type == TYPE_ALNUM)
  246. waddstr(win, "ALNUM");
  247. else if (type == TYPE_ALPHA)
  248. waddstr(win, "ALPHA");
  249. else if (type == TYPE_ENUM)
  250. waddstr(win, "ENUM");
  251. else if (type == TYPE_INTEGER)
  252. waddstr(win, "INTEGER");
  253. #ifdef NCURSES_VERSION
  254. else if (type == TYPE_IPV4)
  255. waddstr(win, "IPV4");
  256. #endif
  257. else if (type == TYPE_NUMERIC)
  258. waddstr(win, "NUMERIC");
  259. else if (type == TYPE_REGEXP)
  260. waddstr(win, "REGEXP");
  261. else
  262. waddstr(win, "other");
  263. }
  264. if (field_opts(field) & O_EDIT)
  265. waddstr(win, " editable");
  266. else
  267. waddstr(win, " readonly");
  268. if (field_status(field))
  269. waddstr(win, " modified");
  270. if (dynamic_field_info(field, &field_rows, &field_cols, &field_max)
  271. != ERR) {
  272. wprintw(win, " size %dx%d (max %d)",
  273. field_rows, field_cols, field_max);
  274. }
  275. waddch(win, ' ');
  276. wattrset(win, field_fore(field));
  277. waddstr(win, "fore");
  278. wattroff(win, field_fore(field));
  279. waddch(win, '/');
  280. wattrset(win, field_back(field));
  281. waddstr(win, "back");
  282. wattroff(win, field_back(field));
  283. wprintw(win, ", pad '%c'",
  284. field_pad(field));
  285. waddstr(win, "\n");
  286. for (nbuf = 0; nbuf <= 2; ++nbuf) {
  287. if ((buffer = field_buffer(field, nbuf)) != 0) {
  288. wprintw(win, "buffer %d:", nbuf);
  289. wattrset(win, A_REVERSE);
  290. waddstr(win, buffer);
  291. wattroff(win, A_REVERSE);
  292. waddstr(win, "\n");
  293. }
  294. }
  295. }
  296. wrefresh(win);
  297. }
  298. static void
  299. demo_forms(void)
  300. {
  301. WINDOW *w;
  302. FORM *form;
  303. FIELD *f[100]; /* FIXME memset to zero */
  304. int finished = 0, c;
  305. unsigned n = 0;
  306. int pg;
  307. WINDOW *also;
  308. #ifdef NCURSES_MOUSE_VERSION
  309. mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
  310. #endif
  311. help_edit_field();
  312. mvaddstr(4, 57, "Forms Entry Test");
  313. show_insert_mode(TRUE);
  314. refresh();
  315. /* describe the form */
  316. memset(f, 0, sizeof(f));
  317. for (pg = 0; pg < 4; ++pg) {
  318. char label[80];
  319. sprintf(label, "Sample Form Page %d", pg + 1);
  320. f[n++] = make_label(0, 15, label);
  321. set_new_page(f[n - 1], TRUE);
  322. switch (pg) {
  323. default:
  324. f[n++] = make_label(2, 0, "Last Name");
  325. f[n++] = make_field(3, 0, 1, 18);
  326. set_field_type(f[n - 1], TYPE_ALPHA, 1);
  327. f[n++] = make_label(2, 20, "First Name");
  328. f[n++] = make_field(3, 20, 1, 12);
  329. set_field_type(f[n - 1], TYPE_ALPHA, 1);
  330. f[n++] = make_label(2, 34, "Middle Name");
  331. f[n++] = make_field(3, 34, 1, 12);
  332. set_field_type(f[n - 1], TYPE_ALPHA, 1);
  333. break;
  334. case 1:
  335. f[n++] = make_label(2, 0, "Last Name");
  336. f[n++] = make_field(3, 0, 1, 18);
  337. set_field_type(f[n - 1], TYPE_ALPHA, 1);
  338. f[n++] = make_label(2, 20, "First Name");
  339. f[n++] = make_field(3, 20, 1, 12);
  340. set_field_type(f[n - 1], TYPE_ALPHA, 1);
  341. f[n++] = make_label(2, 34, "MI");
  342. f[n++] = make_field(3, 34, 1, 1);
  343. set_field_pad(f[n - 1], '?');
  344. set_field_type(f[n - 1], TYPE_ALPHA, 1);
  345. break;
  346. case 2:
  347. f[n++] = make_label(2, 0, "Host Name");
  348. f[n++] = make_field(3, 0, 1, 18);
  349. set_field_type(f[n - 1], TYPE_ALNUM, 1);
  350. #ifdef NCURSES_VERSION
  351. f[n++] = make_label(2, 20, "IP Address");
  352. f[n++] = make_field(3, 20, 1, 12);
  353. set_field_type(f[n - 1], TYPE_IPV4, 1);
  354. #endif
  355. break;
  356. case 3:
  357. f[n++] = make_label(2, 0, "Four digits");
  358. f[n++] = make_field(3, 0, 1, 18);
  359. set_field_type(f[n - 1], TYPE_INTEGER, 4, 0, 0);
  360. f[n++] = make_label(2, 20, "Numeric");
  361. f[n++] = make_field(3, 20, 1, 12);
  362. set_field_type(f[n - 1], TYPE_NUMERIC, 3, -10000.0, 100000000.0);
  363. break;
  364. }
  365. f[n++] = make_label(5, 0, "Comments");
  366. f[n++] = make_field(6, 0, 4, 46);
  367. set_field_buffer(f[n - 1], 0, "HELLO\nWORLD!");
  368. set_field_buffer(f[n - 1], 1, "Hello\nWorld!");
  369. }
  370. f[n++] = (FIELD *) 0;
  371. if ((form = new_form(f)) != 0) {
  372. display_form(form);
  373. w = form_win(form);
  374. also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0);
  375. show_current_field(also, form);
  376. while (!finished) {
  377. switch (edit_field(form, &c)) {
  378. case E_OK:
  379. break;
  380. case E_UNKNOWN_COMMAND:
  381. finished = my_form_driver(form, c);
  382. break;
  383. default:
  384. beep();
  385. break;
  386. }
  387. show_current_field(also, form);
  388. }
  389. erase_form(form);
  390. free_form(form);
  391. }
  392. for (c = 0; f[c] != 0; c++)
  393. free_field(f[c]);
  394. noraw();
  395. nl();
  396. #ifdef NCURSES_MOUSE_VERSION
  397. mousemask(0, (mmask_t *) 0);
  398. #endif
  399. }
  400. static void
  401. usage(void)
  402. {
  403. static const char *tbl[] =
  404. {
  405. "Usage: demo_forms [options]"
  406. ,""
  407. ," -d make fields dynamic"
  408. ," -j value justify (1=left, 2=center, 3=right)"
  409. ," -m value set maximum size of dynamic fields"
  410. ," -o value specify number of offscreen rows in new_field()"
  411. ," -t value specify text to fill fields initially"
  412. };
  413. unsigned int j;
  414. for (j = 0; j < SIZEOF(tbl); ++j)
  415. fprintf(stderr, "%s\n", tbl[j]);
  416. exit(EXIT_FAILURE);
  417. }
  418. int
  419. main(int argc, char *argv[])
  420. {
  421. int ch;
  422. setlocale(LC_ALL, "");
  423. while ((ch = getopt(argc, argv, "dj:m:o:t:")) != -1) {
  424. switch (ch) {
  425. case 'd':
  426. d_option = TRUE;
  427. break;
  428. case 'j':
  429. j_value = atoi(optarg);
  430. if (j_value < NO_JUSTIFICATION
  431. || j_value > JUSTIFY_RIGHT)
  432. usage();
  433. break;
  434. case 'm':
  435. m_value = atoi(optarg);
  436. break;
  437. case 'o':
  438. o_value = atoi(optarg);
  439. break;
  440. case 't':
  441. t_value = optarg;
  442. break;
  443. default:
  444. usage();
  445. }
  446. }
  447. initscr();
  448. cbreak();
  449. noecho();
  450. raw();
  451. nonl(); /* lets us read ^M's */
  452. intrflush(stdscr, FALSE);
  453. keypad(stdscr, TRUE);
  454. if (has_colors()) {
  455. start_color();
  456. init_pair(1, COLOR_WHITE, COLOR_BLUE);
  457. init_pair(2, COLOR_GREEN, COLOR_BLACK);
  458. init_pair(3, COLOR_CYAN, COLOR_BLACK);
  459. bkgd(COLOR_PAIR(1));
  460. refresh();
  461. }
  462. demo_forms();
  463. endwin();
  464. ExitProgram(EXIT_SUCCESS);
  465. }
  466. #else
  467. int
  468. main(void)
  469. {
  470. printf("This program requires the curses form library\n");
  471. ExitProgram(EXIT_FAILURE);
  472. }
  473. #endif