test_inwstr.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /****************************************************************************
  2. * Copyright 2020 Thomas E. Dickey *
  3. * Copyright 2007-2010,2017 Free Software Foundation, Inc. *
  4. * *
  5. * Permission is hereby granted, free of charge, to any person obtaining a *
  6. * copy of this software and associated documentation files (the *
  7. * "Software"), to deal in the Software without restriction, including *
  8. * without limitation the rights to use, copy, modify, merge, publish, *
  9. * distribute, distribute with modifications, sublicense, and/or sell *
  10. * copies of the Software, and to permit persons to whom the Software is *
  11. * furnished to do so, subject to the following conditions: *
  12. * *
  13. * The above copyright notice and this permission notice shall be included *
  14. * in all copies or substantial portions of the Software. *
  15. * *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  17. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  19. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  20. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  21. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  22. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  23. * *
  24. * Except as contained in this notice, the name(s) of the above copyright *
  25. * holders shall not be used in advertising or otherwise to promote the *
  26. * sale, use or other dealings in this Software without prior written *
  27. * authorization. *
  28. ****************************************************************************/
  29. /*
  30. * $Id: test_inwstr.c,v 1.6 2020/02/02 23:34:34 tom Exp $
  31. *
  32. * Author: Thomas E Dickey
  33. *
  34. * Demonstrate the inwstr functions from the curses library.
  35. int inwstr(wchar_t *str);
  36. int innwstr(wchar_t *str, int n);
  37. int winwstr(WINDOW *win, wchar_t *str);
  38. int winnwstr(WINDOW *win, wchar_t *str, int n);
  39. int mvinwstr(int y, int x, wchar_t *str);
  40. int mvinnwstr(int y, int x, wchar_t *str, int n);
  41. int mvwinwstr(WINDOW *win, int y, int x, wchar_t *str);
  42. int mvwinnwstr(WINDOW *win, int y, int x, wchar_t *str, int n);
  43. */
  44. #include <test.priv.h>
  45. #if USE_WIDEC_SUPPORT
  46. #define BASE_Y 6
  47. #define MAX_COLS 1024
  48. static bool
  49. Quit(int ch)
  50. {
  51. return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
  52. }
  53. static void
  54. show_1st(WINDOW *win, int line, wchar_t *buffer)
  55. {
  56. (void) mvwaddwstr(win, line, 5, buffer);
  57. }
  58. static void
  59. showmore(WINDOW *win, int line, wchar_t *buffer)
  60. {
  61. wmove(win, line, 0);
  62. wclrtoeol(win);
  63. show_1st(win, line, buffer);
  64. }
  65. static int
  66. recursive_test(int level, char **argv, WINDOW *chrwin, WINDOW *strwin)
  67. {
  68. WINDOW *txtbox = 0;
  69. WINDOW *txtwin = 0;
  70. FILE *fp;
  71. int ch;
  72. int txt_x = 0, txt_y = 0;
  73. int base_y;
  74. int limit = getmaxx(strwin) - 5;
  75. wchar_t buffer[MAX_COLS];
  76. if (argv[level] == 0) {
  77. beep();
  78. return FALSE;
  79. }
  80. if (level > 1) {
  81. txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
  82. box(txtbox, 0, 0);
  83. wnoutrefresh(txtbox);
  84. txtwin = derwin(txtbox,
  85. getmaxy(txtbox) - 2,
  86. getmaxx(txtbox) - 2,
  87. 1, 1);
  88. base_y = 0;
  89. } else {
  90. txtwin = stdscr;
  91. base_y = BASE_Y;
  92. }
  93. keypad(txtwin, TRUE); /* enable keyboard mapping */
  94. (void) cbreak(); /* take input chars one at a time, no wait for \n */
  95. (void) noecho(); /* don't echo input */
  96. txt_y = base_y;
  97. txt_x = 0;
  98. wmove(txtwin, txt_y, txt_x);
  99. if ((fp = fopen(argv[level], "r")) != 0) {
  100. while ((ch = fgetc(fp)) != EOF) {
  101. if (waddch(txtwin, UChar(ch)) != OK) {
  102. break;
  103. }
  104. }
  105. fclose(fp);
  106. } else {
  107. wprintw(txtwin, "Cannot open:\n%s", argv[1]);
  108. }
  109. while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
  110. switch (ch) {
  111. case KEY_DOWN:
  112. case 'j':
  113. if (txt_y < getmaxy(txtwin) - 1)
  114. txt_y++;
  115. else
  116. beep();
  117. break;
  118. case KEY_UP:
  119. case 'k':
  120. if (txt_y > base_y)
  121. txt_y--;
  122. else
  123. beep();
  124. break;
  125. case KEY_LEFT:
  126. case 'h':
  127. if (txt_x > 0)
  128. txt_x--;
  129. else
  130. beep();
  131. break;
  132. case KEY_RIGHT:
  133. case 'l':
  134. if (txt_x < getmaxx(txtwin) - 1)
  135. txt_x++;
  136. else
  137. beep();
  138. break;
  139. case 'w':
  140. recursive_test(level + 1, argv, chrwin, strwin);
  141. if (txtbox != 0) {
  142. touchwin(txtbox);
  143. wnoutrefresh(txtbox);
  144. } else {
  145. touchwin(txtwin);
  146. wnoutrefresh(txtwin);
  147. }
  148. break;
  149. case '-':
  150. if (limit > 0) {
  151. --limit;
  152. } else {
  153. beep();
  154. }
  155. break;
  156. case '+':
  157. ++limit;
  158. break;
  159. default:
  160. beep();
  161. break;
  162. }
  163. MvWPrintw(chrwin, 0, 0, "line:");
  164. wclrtoeol(chrwin);
  165. if (txtwin != stdscr) {
  166. wmove(txtwin, txt_y, txt_x);
  167. if (winwstr(txtwin, buffer) != ERR) {
  168. show_1st(chrwin, 0, buffer);
  169. }
  170. if (mvwinwstr(txtwin, txt_y, txt_x, buffer) != ERR) {
  171. showmore(chrwin, 1, buffer);
  172. }
  173. } else {
  174. move(txt_y, txt_x);
  175. if (inwstr(buffer) != ERR) {
  176. show_1st(chrwin, 0, buffer);
  177. }
  178. if (mvinwstr(txt_y, txt_x, buffer) != ERR) {
  179. showmore(chrwin, 1, buffer);
  180. }
  181. }
  182. wnoutrefresh(chrwin);
  183. MvWPrintw(strwin, 0, 0, "%4d:", limit);
  184. wclrtobot(strwin);
  185. if (txtwin != stdscr) {
  186. wmove(txtwin, txt_y, txt_x);
  187. if (winnwstr(txtwin, buffer, limit) != ERR) {
  188. show_1st(strwin, 0, buffer);
  189. }
  190. if (mvwinnwstr(txtwin, txt_y, txt_x, buffer, limit) != ERR) {
  191. showmore(strwin, 1, buffer);
  192. }
  193. } else {
  194. move(txt_y, txt_x);
  195. if (innwstr(buffer, limit) != ERR) {
  196. show_1st(strwin, 0, buffer);
  197. }
  198. if (mvinnwstr(txt_y, txt_x, buffer, limit) != ERR) {
  199. showmore(strwin, 1, buffer);
  200. }
  201. }
  202. wnoutrefresh(strwin);
  203. }
  204. if (level > 1) {
  205. delwin(txtwin);
  206. delwin(txtbox);
  207. }
  208. return TRUE;
  209. }
  210. int
  211. main(int argc, char *argv[])
  212. {
  213. WINDOW *chrbox;
  214. WINDOW *chrwin;
  215. WINDOW *strwin;
  216. setlocale(LC_ALL, "");
  217. if (argc < 2) {
  218. fprintf(stderr, "usage: %s file\n", argv[0]);
  219. return EXIT_FAILURE;
  220. }
  221. initscr();
  222. chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
  223. box(chrbox, 0, 0);
  224. wnoutrefresh(chrbox);
  225. chrwin = derwin(chrbox, 2, COLS - 2, 1, 1);
  226. strwin = derwin(chrbox, 2, COLS - 2, 3, 1);
  227. recursive_test(1, argv, chrwin, strwin);
  228. endwin();
  229. ExitProgram(EXIT_SUCCESS);
  230. }
  231. #else
  232. int
  233. main(void)
  234. {
  235. printf("This program requires the wide-ncurses library\n");
  236. ExitProgram(EXIT_FAILURE);
  237. }
  238. #endif