ins_wide.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /****************************************************************************
  2. * Copyright (c) 2002-2006,2007 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: ins_wide.c,v 1.9 2007/07/21 17:41:55 tom Exp $
  30. *
  31. * Demonstrate the wins_wstr() and wins_wch functions.
  32. * Thomas Dickey - 2002/11/23
  33. *
  34. * Note: to provide inputs for *ins_wch(), we use setcchar(). A quirk of the
  35. * X/Open definition for that function is that the string contains no
  36. * characters with negative width. Any control character (such as tab) falls
  37. * into that category. So it follows that *ins_wch() cannot render a tab
  38. * character because there is no legal way to construct a cchar_t containing
  39. * one. X/Open does not document this, and it would be logical to assume that
  40. * *ins_wstr() has the same limitation, but it uses a wchar_t string directly,
  41. * and does not document how tabs are handled.
  42. */
  43. #include <test.priv.h>
  44. #if USE_WIDEC_SUPPORT
  45. /* definitions to make it simpler to compare with inserts.c */
  46. #define InsNStr ins_nwstr
  47. #define InsStr ins_wstr
  48. #define MvInsNStr mvins_nwstr
  49. #define MvInsStr mvins_wstr
  50. #define MvWInsNStr mvwins_nwstr
  51. #define MvWInsStr mvwins_wstr
  52. #define WInsNStr wins_nwstr
  53. #define WInsStr wins_wstr
  54. #define MY_TABSIZE 8
  55. typedef enum {
  56. oDefault = 0,
  57. oMove = 1,
  58. oWindow = 2,
  59. oMoveWindow = 3
  60. } Options;
  61. static bool m_opt = FALSE;
  62. static bool w_opt = FALSE;
  63. static int n_opt = -1;
  64. static void
  65. legend(WINDOW *win, int level, Options state, wchar_t *buffer, int length)
  66. {
  67. NCURSES_CONST char *showstate;
  68. switch (state) {
  69. default:
  70. case oDefault:
  71. showstate = "";
  72. break;
  73. case oMove:
  74. showstate = " (mvXXX)";
  75. break;
  76. case oWindow:
  77. showstate = " (winXXX)";
  78. break;
  79. case oMoveWindow:
  80. showstate = " (mvwinXXX)";
  81. break;
  82. }
  83. wmove(win, 0, 0);
  84. wprintw(win,
  85. "The Strings/Chars displays should match. Enter any characters, except:\n");
  86. wprintw(win,
  87. "down-arrow or ^N to repeat on next line, 'w' for inner window, 'q' to exit.\n");
  88. wclrtoeol(win);
  89. wprintw(win, "Level %d,%s inserted %d characters <", level,
  90. showstate, length);
  91. waddwstr(win, buffer);
  92. waddstr(win, ">");
  93. }
  94. static int
  95. ColOf(wchar_t *buffer, int length, int margin)
  96. {
  97. int n;
  98. int result;
  99. for (n = 0, result = margin + 1; n < length; ++n) {
  100. int ch = buffer[n];
  101. switch (ch) {
  102. case '\n':
  103. /* actually newline should clear the remainder of the line
  104. * and move to the next line - but that seems a little awkward
  105. * in this example.
  106. */
  107. case '\r':
  108. result = 0;
  109. break;
  110. case '\b':
  111. if (result > 0)
  112. --result;
  113. break;
  114. case '\t':
  115. result += (MY_TABSIZE - (result % MY_TABSIZE));
  116. break;
  117. case '\177':
  118. result += 2;
  119. break;
  120. default:
  121. result += wcwidth(ch);
  122. if (ch < 32)
  123. ++result;
  124. break;
  125. }
  126. }
  127. return result;
  128. }
  129. static int
  130. ConvertCh(chtype source, cchar_t *target)
  131. {
  132. wchar_t tmp_wchar[2];
  133. tmp_wchar[0] = source;
  134. tmp_wchar[1] = 0;
  135. if (setcchar(target, tmp_wchar, A_NORMAL, 0, (void *) 0) == ERR) {
  136. beep();
  137. return FALSE;
  138. }
  139. return TRUE;
  140. }
  141. static int
  142. MvWInsCh(WINDOW *win, int y, int x, chtype ch)
  143. {
  144. int code;
  145. cchar_t tmp_cchar;
  146. if (ConvertCh(ch, &tmp_cchar)) {
  147. code = mvwins_wch(win, y, x, &tmp_cchar);
  148. } else {
  149. code = mvwinsch(win, y, x, ch);
  150. }
  151. return code;
  152. }
  153. static int
  154. MvInsCh(int y, int x, chtype ch)
  155. {
  156. int code;
  157. cchar_t tmp_cchar;
  158. if (ConvertCh(ch, &tmp_cchar)) {
  159. code = mvins_wch(y, x, &tmp_cchar);
  160. } else {
  161. code = mvinsch(y, x, ch);
  162. }
  163. return code;
  164. }
  165. static int
  166. WInsCh(WINDOW *win, chtype ch)
  167. {
  168. int code;
  169. cchar_t tmp_cchar;
  170. if (ConvertCh(ch, &tmp_cchar)) {
  171. code = wins_wch(win, &tmp_cchar);
  172. } else {
  173. code = winsch(win, ch);
  174. }
  175. return code;
  176. }
  177. static int
  178. InsCh(chtype ch)
  179. {
  180. int code;
  181. cchar_t tmp_cchar;
  182. if (ConvertCh(ch, &tmp_cchar)) {
  183. code = ins_wch(&tmp_cchar);
  184. } else {
  185. code = insch(ch);
  186. }
  187. return code;
  188. }
  189. #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
  190. static void
  191. test_inserts(int level)
  192. {
  193. static bool first = TRUE;
  194. wint_t ch;
  195. int code;
  196. int limit;
  197. int row = 1;
  198. int col;
  199. int row2, col2;
  200. int length;
  201. wchar_t buffer[BUFSIZ];
  202. WINDOW *look = 0;
  203. WINDOW *work = 0;
  204. WINDOW *show = 0;
  205. int margin = (2 * MY_TABSIZE) - 1;
  206. Options option = ((m_opt ? oMove : oDefault)
  207. | ((w_opt || (level > 0)) ? oWindow : oDefault));
  208. if (first) {
  209. static char cmd[80];
  210. setlocale(LC_ALL, "");
  211. putenv(strcpy(cmd, "TABSIZE=8"));
  212. initscr();
  213. (void) cbreak(); /* take input chars one at a time, no wait for \n */
  214. (void) noecho(); /* don't echo input */
  215. keypad(stdscr, TRUE);
  216. }
  217. limit = LINES - 5;
  218. if (level > 0) {
  219. look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
  220. work = newwin(limit - 2, COLS - (2 * level), 1, level);
  221. show = newwin(4, COLS, limit + 1, 0);
  222. box(look, 0, 0);
  223. wnoutrefresh(look);
  224. limit -= 2;
  225. } else {
  226. work = stdscr;
  227. show = derwin(stdscr, 4, COLS, limit + 1, 0);
  228. }
  229. keypad(work, TRUE);
  230. for (col = margin + 1; col < COLS; col += MY_TABSIZE)
  231. mvwvline(work, row, col, '.', limit - 2);
  232. mvwvline(work, row, margin, ACS_VLINE, limit - 2);
  233. mvwvline(work, row, margin + 1, ACS_VLINE, limit - 2);
  234. limit /= 2;
  235. mvwaddstr(work, 1, 2, "String");
  236. mvwaddstr(work, limit + 1, 2, "Chars");
  237. wnoutrefresh(work);
  238. buffer[length = 0] = '\0';
  239. legend(show, level, option, buffer, length);
  240. wnoutrefresh(show);
  241. doupdate();
  242. /*
  243. * Show the characters inserted in color, to distinguish from those that
  244. * are shifted.
  245. */
  246. if (has_colors()) {
  247. start_color();
  248. init_pair(1, COLOR_WHITE, COLOR_BLUE);
  249. wbkgdset(work, COLOR_PAIR(1) | ' ');
  250. }
  251. while ((code = wget_wch(work, &ch)) != ERR) {
  252. if (code == KEY_CODE_YES) {
  253. switch (ch) {
  254. case KEY_DOWN:
  255. ch = CTRL('N');
  256. break;
  257. case KEY_BACKSPACE:
  258. ch = '\b';
  259. break;
  260. default:
  261. beep();
  262. continue;
  263. }
  264. } else if (code == ERR) {
  265. beep();
  266. break;
  267. }
  268. if (ch == 'q')
  269. break;
  270. wmove(work, row, margin + 1);
  271. switch (ch) {
  272. case 'w':
  273. test_inserts(level + 1);
  274. touchwin(look);
  275. touchwin(work);
  276. touchwin(show);
  277. wnoutrefresh(look);
  278. wnoutrefresh(work);
  279. wnoutrefresh(show);
  280. doupdate();
  281. break;
  282. case CTRL('N'):
  283. if (row < limit) {
  284. ++row;
  285. /* put the whole string in, all at once */
  286. col2 = margin + 1;
  287. switch (option) {
  288. case oDefault:
  289. if (n_opt > 1) {
  290. for (col = 0; col < length; col += n_opt) {
  291. col2 = ColOf(buffer, col, margin);
  292. if (move(row, col2) != ERR) {
  293. InsNStr(buffer + col, LEN(col));
  294. }
  295. }
  296. } else {
  297. if (move(row, col2) != ERR) {
  298. InsStr(buffer);
  299. }
  300. }
  301. break;
  302. case oMove:
  303. if (n_opt > 1) {
  304. for (col = 0; col < length; col += n_opt) {
  305. col2 = ColOf(buffer, col, margin);
  306. MvInsNStr(row, col2, buffer + col, LEN(col));
  307. }
  308. } else {
  309. MvInsStr(row, col2, buffer);
  310. }
  311. break;
  312. case oWindow:
  313. if (n_opt > 1) {
  314. for (col = 0; col < length; col += n_opt) {
  315. col2 = ColOf(buffer, col, margin);
  316. if (wmove(work, row, col2) != ERR) {
  317. WInsNStr(work, buffer + col, LEN(col));
  318. }
  319. }
  320. } else {
  321. if (wmove(work, row, col2) != ERR) {
  322. WInsStr(work, buffer);
  323. }
  324. }
  325. break;
  326. case oMoveWindow:
  327. if (n_opt > 1) {
  328. for (col = 0; col < length; col += n_opt) {
  329. col2 = ColOf(buffer, col, margin);
  330. MvWInsNStr(work, row, col2, buffer + col, LEN(col));
  331. }
  332. } else {
  333. MvWInsStr(work, row, col2, buffer);
  334. }
  335. break;
  336. }
  337. /* do the corresponding single-character insertion */
  338. row2 = limit + row;
  339. for (col = 0; col < length; ++col) {
  340. col2 = ColOf(buffer, col, margin);
  341. switch (option) {
  342. case oDefault:
  343. if (move(row2, col2) != ERR) {
  344. InsCh((chtype) buffer[col]);
  345. }
  346. break;
  347. case oMove:
  348. MvInsCh(row2, col2, (chtype) buffer[col]);
  349. break;
  350. case oWindow:
  351. if (wmove(work, row2, col2) != ERR) {
  352. WInsCh(work, (chtype) buffer[col]);
  353. }
  354. break;
  355. case oMoveWindow:
  356. MvWInsCh(work, row2, col2, (chtype) buffer[col]);
  357. break;
  358. }
  359. }
  360. } else {
  361. beep();
  362. }
  363. break;
  364. case KEY_BACKSPACE:
  365. ch = '\b';
  366. /* FALLTHRU */
  367. default:
  368. buffer[length++] = ch;
  369. buffer[length] = '\0';
  370. /* put the string in, one character at a time */
  371. col = ColOf(buffer, length - 1, margin);
  372. switch (option) {
  373. case oDefault:
  374. if (move(row, col) != ERR) {
  375. InsStr(buffer + length - 1);
  376. }
  377. break;
  378. case oMove:
  379. MvInsStr(row, col, buffer + length - 1);
  380. break;
  381. case oWindow:
  382. if (wmove(work, row, col) != ERR) {
  383. WInsStr(work, buffer + length - 1);
  384. }
  385. break;
  386. case oMoveWindow:
  387. MvWInsStr(work, row, col, buffer + length - 1);
  388. break;
  389. }
  390. /* do the corresponding single-character insertion */
  391. switch (option) {
  392. case oDefault:
  393. if (move(limit + row, col) != ERR) {
  394. InsCh(ch);
  395. }
  396. break;
  397. case oMove:
  398. MvInsCh(limit + row, col, ch);
  399. break;
  400. case oWindow:
  401. if (wmove(work, limit + row, col) != ERR) {
  402. WInsCh(work, ch);
  403. }
  404. break;
  405. case oMoveWindow:
  406. MvWInsCh(work, limit + row, col, ch);
  407. break;
  408. }
  409. wnoutrefresh(work);
  410. legend(show, level, option, buffer, length);
  411. wnoutrefresh(show);
  412. doupdate();
  413. break;
  414. }
  415. }
  416. if (level > 0) {
  417. delwin(show);
  418. delwin(work);
  419. delwin(look);
  420. }
  421. }
  422. static void
  423. usage(void)
  424. {
  425. static const char *tbl[] =
  426. {
  427. "Usage: inserts [options]"
  428. ,""
  429. ,"Options:"
  430. ," -n NUM limit string-inserts to NUM bytes on ^N replay"
  431. ," -m perform wmove/move separately from insert-functions"
  432. ," -w use window-parameter even when stdscr would be implied"
  433. };
  434. unsigned n;
  435. for (n = 0; n < SIZEOF(tbl); ++n)
  436. fprintf(stderr, "%s\n", tbl[n]);
  437. ExitProgram(EXIT_FAILURE);
  438. }
  439. int
  440. main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
  441. {
  442. int ch;
  443. setlocale(LC_ALL, "");
  444. while ((ch = getopt(argc, argv, "mn:w")) != -1) {
  445. switch (ch) {
  446. case 'm':
  447. m_opt = TRUE;
  448. break;
  449. case 'n':
  450. n_opt = atoi(optarg);
  451. if (n_opt == 0)
  452. n_opt = -1;
  453. break;
  454. case 'w':
  455. w_opt = TRUE;
  456. break;
  457. default:
  458. usage();
  459. break;
  460. }
  461. }
  462. if (optind < argc)
  463. usage();
  464. test_inserts(0);
  465. endwin();
  466. ExitProgram(EXIT_SUCCESS);
  467. }
  468. #else
  469. int
  470. main(void)
  471. {
  472. printf("This program requires the wide-ncurses library\n");
  473. ExitProgram(EXIT_FAILURE);
  474. }
  475. #endif