movewindow.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /****************************************************************************
  2. * Copyright (c) 2006-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: movewindow.c,v 1.22 2008/04/12 22:01:41 tom Exp $
  30. *
  31. * Demonstrate move functions for windows and derived windows from the curses
  32. * library.
  33. *
  34. * Thomas Dickey - 2006/2/11
  35. */
  36. /*
  37. derwin
  38. mvderwin
  39. subwin
  40. mvwin
  41. */
  42. #include <test.priv.h>
  43. #include <stdarg.h>
  44. #ifdef HAVE_XCURSES
  45. #undef derwin
  46. #endif
  47. #ifdef NCURSES_VERSION
  48. #define CONST_FMT const
  49. #else
  50. #define CONST_FMT /* nothing */
  51. #endif
  52. #undef LINE_MAX
  53. #define LINE_MIN 2
  54. #define LINE_MAX (LINES - 2)
  55. #define COL_MIN 2
  56. #define COL_MAX (COLS - 2)
  57. typedef struct {
  58. int y, x;
  59. } PAIR;
  60. typedef struct {
  61. WINDOW *parent; /* need this since WINDOW->_parent is not portable */
  62. WINDOW *child; /* the actual value */
  63. } FRAME;
  64. static void head_line(CONST_FMT char *fmt,...) GCC_PRINTFLIKE(1, 2);
  65. static void tail_line(CONST_FMT char *fmt,...) GCC_PRINTFLIKE(1, 2);
  66. static unsigned num_windows;
  67. static FRAME *all_windows;
  68. static void
  69. message(int lineno, CONST_FMT char *fmt, va_list argp)
  70. {
  71. int y, x;
  72. getyx(stdscr, y, x);
  73. move(lineno, 0);
  74. clrtoeol();
  75. #ifdef HAVE_XCURSES
  76. {
  77. char buffer[1024];
  78. vsprintf(buffer, fmt, argp);
  79. addstr(buffer);
  80. }
  81. #else
  82. vwprintw(stdscr, fmt, argp);
  83. #endif
  84. move(y, x);
  85. refresh();
  86. }
  87. static void
  88. head_line(CONST_FMT char *fmt,...)
  89. {
  90. va_list argp;
  91. va_start(argp, fmt);
  92. message(0, fmt, argp);
  93. va_end(argp);
  94. }
  95. static void
  96. tail_line(CONST_FMT char *fmt,...)
  97. {
  98. va_list argp;
  99. va_start(argp, fmt);
  100. message(LINES - 1, fmt, argp);
  101. va_end(argp);
  102. }
  103. /*
  104. * Arrow keys move cursor, return location at current on non-arrow key.
  105. */
  106. static PAIR *
  107. selectcell(WINDOW *parent, int uli, int ulj, int lri, int lrj)
  108. {
  109. static PAIR res; /* result cell */
  110. int si = lri - uli + 1; /* depth of the select area */
  111. int sj = lrj - ulj + 1; /* width of the select area */
  112. int i = 0, j = 0; /* offsets into the select area */
  113. res.y = uli;
  114. res.x = ulj;
  115. for (;;) {
  116. tail_line("Upper left [%2d,%2d] Lower right [%2d,%2d] -> %d,%d",
  117. uli, ulj,
  118. lri, lrj,
  119. uli + i, ulj + j);
  120. wmove(parent, uli + i, ulj + j);
  121. switch (wgetch(parent)) {
  122. case KEY_UP:
  123. i += si - 1;
  124. break;
  125. case KEY_DOWN:
  126. i++;
  127. break;
  128. case KEY_LEFT:
  129. j += sj - 1;
  130. break;
  131. case KEY_RIGHT:
  132. j++;
  133. break;
  134. case QUIT:
  135. case ESCAPE:
  136. return ((PAIR *) 0);
  137. #ifdef NCURSES_MOUSE_VERSION
  138. case KEY_MOUSE:
  139. {
  140. MEVENT event;
  141. getmouse(&event);
  142. if (event.y > uli && event.x > ulj) {
  143. i = event.y - uli;
  144. j = event.x - ulj;
  145. } else {
  146. beep();
  147. break;
  148. }
  149. }
  150. /* FALLTHRU */
  151. #endif
  152. default:
  153. res.y = uli + i;
  154. res.x = ulj + j;
  155. return (&res);
  156. }
  157. i %= si;
  158. j %= sj;
  159. }
  160. }
  161. /*
  162. * Ask user for a window definition.
  163. */
  164. static bool
  165. getwindow(WINDOW *parent, PAIR * ul, PAIR * lr)
  166. {
  167. int min_col = (parent == stdscr) ? COL_MIN : 0;
  168. int max_col = (parent == stdscr) ? COL_MAX : getmaxx(parent);
  169. int min_line = (parent == stdscr) ? LINE_MIN : 0;
  170. int max_line = (parent == stdscr) ? LINE_MAX : getmaxy(parent);
  171. PAIR *tmp;
  172. bool result = FALSE;
  173. head_line("Use arrows to move cursor, anything else to mark corner 1");
  174. if ((tmp = selectcell(parent, min_line, min_col, max_line, max_col)) != 0) {
  175. *ul = *tmp;
  176. mvwaddch(parent, ul->y, ul->x, '*');
  177. head_line("Use arrows to move cursor, anything else to mark corner 2");
  178. if ((tmp = selectcell(parent, ul->y, ul->x, max_line, max_col)) != 0) {
  179. *lr = *tmp;
  180. mvwaddch(parent, lr->y, lr->x, '*');
  181. wmove(parent, lr->y, lr->x);
  182. wsyncdown(parent);
  183. wrefresh(parent);
  184. result = (lr->y != ul->y && lr->x != ul->x);
  185. }
  186. }
  187. head_line("done");
  188. return result;
  189. }
  190. /*
  191. * Draw a box inside the given window.
  192. */
  193. static void
  194. box_inside(WINDOW *win)
  195. {
  196. int y0, x0;
  197. int y1, x1;
  198. getyx(win, y0, x0);
  199. getmaxyx(win, y1, x1);
  200. mvwhline(win, 0, 0, ACS_HLINE, x1);
  201. mvwhline(win, y1 - 1, 0, ACS_HLINE, x1);
  202. mvwvline(win, 0, 0, ACS_VLINE, y1);
  203. mvwvline(win, 0, x1 - 1, ACS_VLINE, y1);
  204. mvwaddch(win, 0, 0, ACS_ULCORNER);
  205. mvwaddch(win, y1 - 1, 0, ACS_LLCORNER);
  206. mvwaddch(win, 0, x1 - 1, ACS_URCORNER);
  207. mvwaddch(win, y1 - 1, x1 - 1, ACS_LRCORNER);
  208. wsyncdown(win);
  209. wmove(win, y0, x0);
  210. wrefresh(win);
  211. }
  212. /*
  213. * Add a window to our list.
  214. */
  215. static void
  216. add_window(WINDOW *parent, WINDOW *child)
  217. {
  218. static unsigned have = 0;
  219. unsigned need = ((num_windows + 1) | 31) + 1;
  220. keypad(child, TRUE);
  221. if (need > have) {
  222. all_windows = typeRealloc(FRAME, need, all_windows);
  223. }
  224. all_windows[num_windows].parent = parent;
  225. all_windows[num_windows].child = child;
  226. num_windows++;
  227. }
  228. static int
  229. window2num(WINDOW *win)
  230. {
  231. int n;
  232. int result = -1;
  233. for (n = 0; n < (int) num_windows; ++n) {
  234. if (win == all_windows[n].child) {
  235. result = n;
  236. break;
  237. }
  238. }
  239. return result;
  240. }
  241. static WINDOW *
  242. parent_of(WINDOW *win)
  243. {
  244. WINDOW *result = 0;
  245. int n = window2num(win);
  246. if (n >= 0)
  247. result = all_windows[n].parent;
  248. return result;
  249. }
  250. static void
  251. repaint_one(WINDOW *win)
  252. {
  253. touchwin(win);
  254. wnoutrefresh(win);
  255. }
  256. static void
  257. refresh_all(WINDOW *win)
  258. {
  259. unsigned n;
  260. for (n = 0; n < num_windows; ++n) {
  261. if (all_windows[n].child != win) {
  262. repaint_one(all_windows[n].child);
  263. }
  264. }
  265. repaint_one(win);
  266. doupdate();
  267. }
  268. static WINDOW *
  269. next_window(WINDOW *win)
  270. {
  271. WINDOW *result = win;
  272. int n = window2num(win);
  273. if (n++ >= 0) {
  274. result = all_windows[n % num_windows].child;
  275. wmove(result, 0, 0);
  276. wrefresh(result);
  277. }
  278. return result;
  279. }
  280. static WINDOW *
  281. prev_window(WINDOW *win)
  282. {
  283. WINDOW *result = win;
  284. int n = window2num(win);
  285. if (n-- >= 0) {
  286. if (n < 0)
  287. n = num_windows - 1;
  288. result = all_windows[n % num_windows].child;
  289. wmove(result, 0, 0);
  290. wrefresh(result);
  291. }
  292. return result;
  293. }
  294. static void
  295. recur_move_window(WINDOW *parent, int dy, int dx)
  296. {
  297. unsigned n;
  298. for (n = 0; n < num_windows; ++n) {
  299. if (all_windows[n].parent == parent) {
  300. int y0, x0;
  301. getbegyx(all_windows[n].child, y0, x0);
  302. mvwin(all_windows[n].child, y0 + dy, x0 + dx);
  303. recur_move_window(all_windows[n].child, dy, dx);
  304. }
  305. }
  306. }
  307. /*
  308. * test mvwin().
  309. */
  310. static bool
  311. move_window(WINDOW *win, bool recur)
  312. {
  313. WINDOW *parent = parent_of(win);
  314. bool result = FALSE;
  315. if (parent != 0) {
  316. bool top = (parent == stdscr);
  317. int min_col = top ? COL_MIN : 0;
  318. int max_col = top ? COL_MAX : getmaxx(parent);
  319. int min_line = top ? LINE_MIN : 0;
  320. int max_line = top ? LINE_MAX : getmaxy(parent);
  321. PAIR *tmp;
  322. head_line("Select new position for %swindow", top ? "" : "sub");
  323. if ((tmp = selectcell(parent,
  324. min_line, min_col,
  325. max_line, max_col)) != 0) {
  326. int y0, x0;
  327. getbegyx(parent, y0, x0);
  328. /*
  329. * Note: Moving a subwindow has the effect of moving a viewport
  330. * around the screen. The parent window retains the contents of
  331. * the subwindow in the original location, but the viewport will
  332. * show the contents (again) at the new location. So it will look
  333. * odd when testing.
  334. */
  335. if (mvwin(win, y0 + tmp->y, x0 + tmp->x) != ERR) {
  336. if (recur) {
  337. recur_move_window(win, tmp->y, tmp->x);
  338. }
  339. refresh_all(win);
  340. doupdate();
  341. result = TRUE;
  342. }
  343. }
  344. }
  345. return result;
  346. }
  347. /*
  348. * test mvderwin().
  349. */
  350. static bool
  351. move_subwin(WINDOW *win)
  352. {
  353. WINDOW *parent = parent_of(win);
  354. bool result = FALSE;
  355. if (parent != 0) {
  356. bool top = (parent == stdscr);
  357. if (!top) {
  358. int min_col = top ? COL_MIN : 0;
  359. int max_col = top ? COL_MAX : getmaxx(parent);
  360. int min_line = top ? LINE_MIN : 0;
  361. int max_line = top ? LINE_MAX : getmaxy(parent);
  362. PAIR *tmp;
  363. head_line("Select new position for subwindow");
  364. if ((tmp = selectcell(parent,
  365. min_line, min_col,
  366. max_line, max_col)) != 0) {
  367. int y0, x0;
  368. getbegyx(parent, y0, x0);
  369. if (mvderwin(win, y0 + tmp->y, x0 + tmp->x) != ERR) {
  370. refresh_all(win);
  371. doupdate();
  372. result = TRUE;
  373. }
  374. }
  375. }
  376. }
  377. return result;
  378. }
  379. static void
  380. fill_window(WINDOW *win, chtype ch)
  381. {
  382. int y, x;
  383. int y0, x0;
  384. int y1, x1;
  385. getyx(win, y0, x0);
  386. getmaxyx(win, y1, x1);
  387. for (y = 0; y < y1; ++y) {
  388. for (x = 0; x < x1; ++x) {
  389. mvwaddch(win, y, x, ch);
  390. }
  391. }
  392. wsyncdown(win);
  393. wmove(win, y0, x0);
  394. wrefresh(win);
  395. }
  396. #define lines_of(ul,lr) (lr.y - ul.y + 1)
  397. #define cols_of(ul,lr) (lr.x - ul.x + 1)
  398. #define pair_of(ul) ul.y, ul.x
  399. static WINDOW *
  400. create_my_window(WINDOW *current)
  401. {
  402. PAIR ul, lr;
  403. WINDOW *result = 0;
  404. if (getwindow(stdscr, &ul, &lr)) {
  405. result = newwin(lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
  406. if (result != 0) {
  407. fill_window(result, 'c');
  408. add_window(stdscr, result);
  409. }
  410. }
  411. if (result == 0)
  412. result = current;
  413. return result;
  414. }
  415. static WINDOW *
  416. create_my_derwin(WINDOW *parent)
  417. {
  418. PAIR ul, lr;
  419. WINDOW *result = 0;
  420. if (getwindow(parent, &ul, &lr)) {
  421. result = derwin(parent, lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
  422. if (result != 0) {
  423. fill_window(result, 'd');
  424. add_window(parent, result);
  425. }
  426. }
  427. if (result == 0)
  428. result = parent;
  429. return result;
  430. }
  431. static WINDOW *
  432. create_my_subwin(WINDOW *parent)
  433. {
  434. PAIR ul, lr;
  435. WINDOW *result = 0;
  436. if (getwindow(parent, &ul, &lr)) {
  437. result = subwin(parent,
  438. lines_of(ul, lr),
  439. cols_of(ul, lr),
  440. ul.y + getbegy(parent),
  441. ul.x + getbegx(parent));
  442. if (result != 0) {
  443. fill_window(result, 's');
  444. add_window(parent, result);
  445. }
  446. }
  447. if (result == 0)
  448. result = parent;
  449. return result;
  450. }
  451. static void
  452. show_help(WINDOW *current)
  453. {
  454. /* *INDENT-OFF* */
  455. static struct {
  456. int key;
  457. CONST_FMT char * msg;
  458. } help[] = {
  459. { '?', "Show this screen" },
  460. { 'b', "Draw a box inside the current window" },
  461. { 'c', "Create a new window" },
  462. { 'd', "Create a new derived window" },
  463. { 'f', "Fill the current window with the next character" },
  464. { 'm', "Move the current window" },
  465. { 'M', "Move the current window (and its children)" },
  466. { 'q', "Quit" },
  467. { 's', "Create a new subwindow" },
  468. { 't', "Move the current subwindow (moves content)" },
  469. { CTRL('L'), "Repaint all windows, doing current one last" },
  470. { CTRL('N'), "Cursor to next window" },
  471. { CTRL('P'), "Cursor to previous window" },
  472. };
  473. /* *INDENT-ON* */
  474. WINDOW *mywin = newwin(LINES, COLS, 0, 0);
  475. int row;
  476. for (row = 0; row < LINES - 2 && row < (int) SIZEOF(help); ++row) {
  477. wmove(mywin, row + 1, 1);
  478. wprintw(mywin, "%s", keyname(help[row].key));
  479. wmove(mywin, row + 1, 20);
  480. wprintw(mywin, "%s", help[row].msg);
  481. }
  482. box_inside(mywin);
  483. wmove(mywin, 1, 1);
  484. wgetch(mywin);
  485. delwin(mywin);
  486. refresh_all(current);
  487. }
  488. int
  489. main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
  490. {
  491. WINDOW *current_win;
  492. int ch;
  493. bool done = FALSE;
  494. initscr();
  495. cbreak();
  496. noecho();
  497. nonl();
  498. intrflush(stdscr, FALSE);
  499. add_window(0, current_win = stdscr);
  500. #ifdef NCURSES_MOUSE_VERSION
  501. (void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL);
  502. #endif /* NCURSES_MOUSE_VERSION */
  503. while (!done && (ch = wgetch(current_win)) != ERR) {
  504. switch (ch) {
  505. case '?':
  506. show_help(current_win);
  507. break;
  508. case 'b':
  509. box_inside(current_win);
  510. break;
  511. case 'c':
  512. current_win = create_my_window(current_win);
  513. break;
  514. case 'd':
  515. current_win = create_my_derwin(current_win);
  516. break;
  517. case 'f':
  518. fill_window(current_win, (chtype) wgetch(current_win));
  519. break;
  520. case 'm':
  521. case 'M':
  522. if (!move_window(current_win, (ch == 'M'))) {
  523. tail_line("error");
  524. continue;
  525. }
  526. break;
  527. case 'q':
  528. done = TRUE;
  529. break;
  530. case 's':
  531. current_win = create_my_subwin(current_win);
  532. break;
  533. case 't':
  534. if (!move_subwin(current_win)) {
  535. tail_line("error");
  536. continue;
  537. }
  538. break;
  539. case CTRL('L'):
  540. refresh_all(current_win);
  541. break;
  542. case CTRL('N'):
  543. current_win = next_window(current_win);
  544. break;
  545. case CTRL('P'):
  546. current_win = prev_window(current_win);
  547. break;
  548. #if 0
  549. /* want to allow cursor to move around the current window too */
  550. /* want to test the resizing of windows and subwindows too */
  551. /* want to allow deleting a window also */
  552. #endif
  553. default:
  554. tail_line("unrecognized key (use '?' for help)");
  555. beep();
  556. continue;
  557. }
  558. tail_line("size [%d,%d] begin [%d,%d] parent [%d,%d]",
  559. getmaxy(current_win),
  560. getmaxx(current_win),
  561. getbegy(current_win),
  562. getbegx(current_win),
  563. getpary(current_win),
  564. getparx(current_win));
  565. wmove(current_win, 0, 0);
  566. }
  567. endwin();
  568. ExitProgram(EXIT_SUCCESS);
  569. }