test_get_wstr.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /****************************************************************************
  2. * Copyright (c) 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: test_get_wstr.c,v 1.5 2007/08/11 17:01:43 tom Exp $
  30. *
  31. * Author: Thomas E Dickey
  32. *
  33. * Demonstrate the get_wstr functions from the curses library.
  34. int get_wstr(wint_t *wstr);
  35. int getn_wstr(wint_t *wstr, int n);
  36. int wget_wstr(WINDOW *win, wint_t *wstr);
  37. int wgetn_wstr(WINDOW *win, wint_t *wstr, int n);
  38. int mvget_wstr(int y, int x, wint_t *wstr);
  39. int mvgetn_wstr(int y, int x, wint_t *wstr, int n);
  40. int mvwget_wstr(WINDOW *win, int y, int x, wint_t *wstr);
  41. int mvwgetn_wstr(WINDOW *win, int y, int x, wint_t *wstr, int n);
  42. */
  43. #include <test.priv.h>
  44. #if USE_WIDEC_SUPPORT
  45. #define BASE_Y 6
  46. #define MAX_COLS 1024
  47. typedef enum {
  48. eGetStr = 0,
  49. eGetNStr,
  50. eMvGetStr,
  51. eMvGetNStr,
  52. eMaxFlavor
  53. } Flavors;
  54. static bool
  55. Quit(int ch)
  56. {
  57. return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
  58. }
  59. static int
  60. Remainder(WINDOW *txtwin)
  61. {
  62. int result = getmaxx(txtwin) - getcurx(txtwin);
  63. return (result > 0) ? result : 0;
  64. }
  65. /*
  66. * Show a highlighted line in the place where input will happen.
  67. */
  68. static void
  69. ShowPrompt(WINDOW *txtwin, int limit)
  70. {
  71. wchgat(txtwin, limit, A_REVERSE, 0, NULL);
  72. wnoutrefresh(txtwin);
  73. }
  74. static void
  75. MovePrompt(WINDOW *txtwin, int limit, int y, int x)
  76. {
  77. wchgat(txtwin, Remainder(txtwin), A_NORMAL, 0, NULL);
  78. wmove(txtwin, y, x);
  79. ShowPrompt(txtwin, limit);
  80. }
  81. static int
  82. ShowFlavor(WINDOW *strwin, WINDOW *txtwin, int flavor, int limit)
  83. {
  84. const char *name = "?";
  85. bool limited = FALSE;
  86. bool wins = (txtwin != stdscr);
  87. int result;
  88. switch (flavor) {
  89. case eGetStr:
  90. name = wins ? "wget_wstr" : "get_wstr";
  91. break;
  92. case eGetNStr:
  93. limited = TRUE;
  94. name = wins ? "wgetn_wstr" : "getn_wstr";
  95. break;
  96. case eMvGetStr:
  97. name = wins ? "mvwget_wstr" : "mvget_wstr";
  98. break;
  99. case eMvGetNStr:
  100. limited = TRUE;
  101. name = wins ? "mvwgetn_wstr" : "mvgetn_wstr";
  102. break;
  103. case eMaxFlavor:
  104. break;
  105. }
  106. wmove(strwin, 0, 0);
  107. werase(strwin);
  108. if (limited) {
  109. wprintw(strwin, "%s(%d):", name, limit);
  110. } else {
  111. wprintw(strwin, "%s:", name);
  112. }
  113. result = limited ? limit : Remainder(txtwin);
  114. ShowPrompt(txtwin, result);
  115. wnoutrefresh(strwin);
  116. return result;
  117. }
  118. static int
  119. test_get_wstr(int level, char **argv, WINDOW *strwin)
  120. {
  121. WINDOW *txtbox = 0;
  122. WINDOW *txtwin = 0;
  123. FILE *fp;
  124. int ch;
  125. int rc;
  126. int txt_x = 0, txt_y = 0;
  127. int base_y;
  128. int flavor = 0;
  129. int limit = getmaxx(strwin) - 5;
  130. int actual;
  131. wint_t buffer[MAX_COLS];
  132. if (argv[level] == 0) {
  133. beep();
  134. return FALSE;
  135. }
  136. if (level > 1) {
  137. txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
  138. box(txtbox, 0, 0);
  139. wnoutrefresh(txtbox);
  140. txtwin = derwin(txtbox,
  141. getmaxy(txtbox) - 2,
  142. getmaxx(txtbox) - 2,
  143. 1, 1);
  144. base_y = 0;
  145. } else {
  146. txtwin = stdscr;
  147. base_y = BASE_Y;
  148. }
  149. keypad(txtwin, TRUE); /* enable keyboard mapping */
  150. (void) cbreak(); /* take input chars one at a time, no wait for \n */
  151. (void) noecho(); /* don't echo input */
  152. txt_y = base_y;
  153. txt_x = 0;
  154. wmove(txtwin, txt_y, txt_x);
  155. if ((fp = fopen(argv[level], "r")) != 0) {
  156. while ((ch = fgetc(fp)) != EOF) {
  157. if (waddch(txtwin, UChar(ch)) != OK) {
  158. break;
  159. }
  160. }
  161. fclose(fp);
  162. } else {
  163. wprintw(txtwin, "Cannot open:\n%s", argv[1]);
  164. }
  165. wmove(txtwin, txt_y, txt_x);
  166. actual = ShowFlavor(strwin, txtwin, flavor, limit);
  167. while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
  168. switch (ch) {
  169. case KEY_DOWN:
  170. case 'j':
  171. if (txt_y < getmaxy(txtwin) - 1) {
  172. MovePrompt(txtwin, actual, ++txt_y, txt_x);
  173. } else {
  174. beep();
  175. }
  176. break;
  177. case KEY_UP:
  178. case 'k':
  179. if (txt_y > base_y) {
  180. MovePrompt(txtwin, actual, --txt_y, txt_x);
  181. } else {
  182. beep();
  183. }
  184. break;
  185. case KEY_LEFT:
  186. case 'h':
  187. if (txt_x > 0) {
  188. MovePrompt(txtwin, actual, txt_y, --txt_x);
  189. } else {
  190. beep();
  191. }
  192. break;
  193. case KEY_RIGHT:
  194. case 'l':
  195. if (txt_x < getmaxx(txtwin) - 1) {
  196. MovePrompt(txtwin, actual, txt_y, ++txt_x);
  197. } else {
  198. beep();
  199. }
  200. break;
  201. case 'w':
  202. test_get_wstr(level + 1, argv, strwin);
  203. if (txtbox != 0) {
  204. touchwin(txtbox);
  205. wnoutrefresh(txtbox);
  206. } else {
  207. touchwin(txtwin);
  208. wnoutrefresh(txtwin);
  209. }
  210. break;
  211. case '-':
  212. if (limit > 0) {
  213. actual = ShowFlavor(strwin, txtwin, flavor, --limit);
  214. MovePrompt(txtwin, actual, txt_y, txt_x);
  215. } else {
  216. beep();
  217. }
  218. break;
  219. case '+':
  220. actual = ShowFlavor(strwin, txtwin, flavor, ++limit);
  221. MovePrompt(txtwin, actual, txt_y, txt_x);
  222. break;
  223. case '<':
  224. if (flavor > 0) {
  225. actual = ShowFlavor(strwin, txtwin, --flavor, limit);
  226. MovePrompt(txtwin, actual, txt_y, txt_x);
  227. } else {
  228. beep();
  229. }
  230. break;
  231. case '>':
  232. if (flavor + 1 < eMaxFlavor) {
  233. actual = ShowFlavor(strwin, txtwin, ++flavor, limit);
  234. MovePrompt(txtwin, actual, txt_y, txt_x);
  235. } else {
  236. beep();
  237. }
  238. break;
  239. case ':':
  240. actual = ShowFlavor(strwin, txtwin, flavor, limit);
  241. *buffer = '\0';
  242. rc = ERR;
  243. echo();
  244. wattrset(txtwin, A_REVERSE);
  245. switch (flavor) {
  246. case eGetStr:
  247. if (txtwin != stdscr) {
  248. wmove(txtwin, txt_y, txt_x);
  249. rc = wget_wstr(txtwin, buffer);
  250. } else {
  251. move(txt_y, txt_x);
  252. rc = get_wstr(buffer);
  253. }
  254. break;
  255. case eGetNStr:
  256. if (txtwin != stdscr) {
  257. wmove(txtwin, txt_y, txt_x);
  258. rc = wgetn_wstr(txtwin, buffer, limit);
  259. } else {
  260. move(txt_y, txt_x);
  261. rc = getn_wstr(buffer, limit);
  262. }
  263. break;
  264. case eMvGetStr:
  265. if (txtwin != stdscr) {
  266. rc = mvwget_wstr(txtwin, txt_y, txt_x, buffer);
  267. } else {
  268. rc = mvget_wstr(txt_y, txt_x, buffer);
  269. }
  270. break;
  271. case eMvGetNStr:
  272. if (txtwin != stdscr) {
  273. rc = mvwgetn_wstr(txtwin, txt_y, txt_x, buffer, limit);
  274. } else {
  275. rc = mvgetn_wstr(txt_y, txt_x, buffer, limit);
  276. }
  277. break;
  278. case eMaxFlavor:
  279. break;
  280. }
  281. noecho();
  282. wattrset(txtwin, A_NORMAL);
  283. wprintw(strwin, "%d", rc);
  284. waddwstr(strwin, (wchar_t *) buffer);
  285. wnoutrefresh(strwin);
  286. break;
  287. default:
  288. beep();
  289. break;
  290. }
  291. doupdate();
  292. }
  293. if (level > 1) {
  294. delwin(txtwin);
  295. delwin(txtbox);
  296. }
  297. return TRUE;
  298. }
  299. int
  300. main(int argc, char *argv[])
  301. {
  302. WINDOW *chrbox;
  303. WINDOW *strwin;
  304. setlocale(LC_ALL, "");
  305. if (argc < 2) {
  306. fprintf(stderr, "usage: %s file\n", argv[0]);
  307. return EXIT_FAILURE;
  308. }
  309. initscr();
  310. chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
  311. box(chrbox, 0, 0);
  312. wnoutrefresh(chrbox);
  313. strwin = derwin(chrbox, 4, COLS - 2, 1, 1);
  314. test_get_wstr(1, argv, strwin);
  315. endwin();
  316. ExitProgram(EXIT_SUCCESS);
  317. }
  318. #else
  319. int
  320. main(void)
  321. {
  322. printf("This program requires the wide-ncurses library\n");
  323. ExitProgram(EXIT_FAILURE);
  324. }
  325. #endif