libncurses: updated to 6.0
[tomato.git] / release / src / router / libncurses / test / movewindow.c
blob9effda41a4da145f421aa376aa04f7f48d12cad1
1 /****************************************************************************
2 * Copyright (c) 2006-2012,2013 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 ****************************************************************************/
29 * $Id: movewindow.c,v 1.39 2013/05/04 19:41:02 tom Exp $
31 * Demonstrate move functions for windows and derived windows from the curses
32 * library.
34 * Author: Thomas E. Dickey
37 derwin
38 mvderwin
39 subwin
40 mvwin
42 TODO:
43 add command to reset subwindow's origin to coincide with parent.
44 add command to delete subwindow (check if it has subwindows though)
47 #include <test.priv.h>
48 #include <stdarg.h>
50 #ifdef HAVE_XCURSES
51 #undef derwin
52 #endif
54 #ifdef NCURSES_VERSION
55 #define CONST_FMT const
56 #else
57 #define CONST_FMT /* nothing */
58 #endif
60 #undef LINE_MAX
62 #define LINE_MIN 2
63 #define LINE_MAX (LINES - 2)
64 #define COL_MIN 2
65 #define COL_MAX (COLS - 2)
67 typedef struct {
68 int y, x;
69 } PAIR;
71 typedef struct {
72 WINDOW *parent; /* need this since WINDOW->_parent is not portable */
73 WINDOW *child; /* the actual value */
74 } FRAME;
76 static void head_line(CONST_FMT char *fmt,...) GCC_PRINTFLIKE(1, 2);
77 static void tail_line(CONST_FMT char *fmt,...) GCC_PRINTFLIKE(1, 2);
79 static unsigned num_windows;
80 static FRAME *all_windows;
82 static void
83 failed(const char *s)
85 perror(s);
86 endwin();
87 ExitProgram(EXIT_FAILURE);
90 static void
91 message(int lineno, CONST_FMT char *fmt, va_list argp)
93 int y, x;
95 getyx(stdscr, y, x);
96 move(lineno, 0);
97 clrtoeol();
99 #ifdef HAVE_XCURSES
101 char buffer[1024];
102 vsprintf(buffer, fmt, argp);
103 addstr(buffer);
105 #else
106 vwprintw(stdscr, fmt, argp);
107 #endif
109 move(y, x);
110 refresh();
113 static void
114 head_line(CONST_FMT char *fmt,...)
116 va_list argp;
118 va_start(argp, fmt);
119 message(0, fmt, argp);
120 va_end(argp);
123 static void
124 tail_line(CONST_FMT char *fmt,...)
126 va_list argp;
128 va_start(argp, fmt);
129 message(LINES - 1, fmt, argp);
130 va_end(argp);
134 * Arrow keys move cursor, return location at current on non-arrow key.
136 static PAIR *
137 selectcell(WINDOW *parent,
138 WINDOW *child,
139 int uli, int ulj,
140 int lri, int lrj,
141 bool relative,
142 bool * more)
144 static PAIR res; /* result cell */
145 int si = lri - uli + 1; /* depth of the select area */
146 int sj = lrj - ulj + 1; /* width of the select area */
147 int i = 0, j = 0; /* offsets into the select area */
149 res.y = uli;
150 res.x = ulj;
152 if (child != 0) {
153 if (relative) {
154 getparyx(child, i, j);
155 } else {
156 getbegyx(child, i, j);
157 i -= uli + getbegy(parent);
158 j -= ulj + getbegx(parent);
162 if (more)
163 *more = FALSE;
165 for (;;) {
166 bool moved = FALSE;
168 tail_line("Upper left [%2d,%2d] Lower right [%2d,%2d] -> %d,%d -> %d,%d",
169 uli, ulj,
170 lri, lrj,
171 i, j,
172 uli + i, ulj + j);
173 wmove(parent, uli + i, ulj + j);
175 switch (wgetch(parent)) {
176 case KEY_UP:
177 i += si - 1;
178 moved = TRUE;
179 break;
180 case KEY_DOWN:
181 i++;
182 moved = TRUE;
183 break;
184 case KEY_LEFT:
185 j += sj - 1;
186 moved = TRUE;
187 break;
188 case KEY_RIGHT:
189 j++;
190 moved = TRUE;
191 break;
192 case QUIT:
193 case ESCAPE:
194 return ((PAIR *) 0);
195 #ifdef NCURSES_MOUSE_VERSION
196 case KEY_MOUSE:
198 MEVENT event;
200 getmouse(&event);
201 if (event.y > uli && event.x > ulj) {
202 if (parent != stdscr) {
203 i = event.y - getbegy(parent) - uli;
204 j = event.x - getbegx(parent) - ulj;
205 } else {
206 i = event.y - uli;
207 j = event.x - ulj;
209 } else {
210 beep();
211 break;
214 /* FALLTHRU */
215 #endif
216 default:
217 res.y = uli + i;
218 res.x = ulj + j;
219 return (&res);
222 if (si <= 0)
223 i = 0;
224 else
225 i %= si;
227 if (sj <= 0)
228 j = 0;
229 else
230 j %= sj;
233 * If the caller can handle continuous movement, return the result.
235 if (moved && more) {
236 *more = TRUE;
237 res.y = uli + i;
238 res.x = ulj + j;
239 return (&res);
245 * Ask user for a window definition.
247 static bool
248 getwindow(WINDOW *parent, PAIR * ul, PAIR * lr)
250 int min_col = (parent == stdscr) ? COL_MIN : 0;
251 int max_col = (parent == stdscr) ? COL_MAX : getmaxx(parent);
252 int min_line = (parent == stdscr) ? LINE_MIN : 0;
253 int max_line = (parent == stdscr) ? LINE_MAX : getmaxy(parent);
254 PAIR *tmp;
255 bool result = FALSE;
257 head_line("Use arrows to move cursor, anything else to mark corner 1");
258 if ((tmp = selectcell(parent, 0,
259 min_line, min_col,
260 max_line, max_col,
261 FALSE,
262 (bool *) 0)) != 0) {
263 *ul = *tmp;
264 MvWAddCh(parent, ul->y, ul->x, '*');
266 head_line("Use arrows to move cursor, anything else to mark corner 2");
267 if ((tmp = selectcell(parent, 0,
268 ul->y, ul->x,
269 max_line, max_col,
270 FALSE,
271 (bool *) 0)) != 0) {
272 *lr = *tmp;
273 MvWAddCh(parent, lr->y, lr->x, '*');
274 wmove(parent, lr->y, lr->x);
275 wsyncdown(parent);
276 wrefresh(parent);
277 result = (lr->y != ul->y && lr->x != ul->x);
280 head_line("done");
281 return result;
285 * Draw a box inside the given window.
287 static void
288 box_inside(WINDOW *win)
290 int y0, x0;
291 int y1, x1;
293 getyx(win, y0, x0);
294 getmaxyx(win, y1, x1);
296 MvWHLine(win, 0, 0, ACS_HLINE, x1);
297 MvWHLine(win, y1 - 1, 0, ACS_HLINE, x1);
299 MvWVLine(win, 0, 0, ACS_VLINE, y1);
300 MvWVLine(win, 0, x1 - 1, ACS_VLINE, y1);
302 MvWAddCh(win, 0, 0, ACS_ULCORNER);
303 MvWAddCh(win, y1 - 1, 0, ACS_LLCORNER);
304 MvWAddCh(win, 0, x1 - 1, ACS_URCORNER);
305 MvWAddCh(win, y1 - 1, x1 - 1, ACS_LRCORNER);
307 wsyncdown(win);
308 wmove(win, y0, x0);
309 wrefresh(win);
313 * Add a window to our list.
315 static void
316 add_window(WINDOW *parent, WINDOW *child)
318 static unsigned have = 0;
319 unsigned need = ((num_windows + 1) | 31) + 1;
321 keypad(child, TRUE);
322 if (need > have) {
323 all_windows = typeRealloc(FRAME, need, all_windows);
324 if (!all_windows)
325 failed("add_window");
327 all_windows[num_windows].parent = parent;
328 all_windows[num_windows].child = child;
329 num_windows++;
332 static int
333 window2num(WINDOW *win)
335 int n;
336 int result = -1;
337 for (n = 0; n < (int) num_windows; ++n) {
338 if (win == all_windows[n].child) {
339 result = n;
340 break;
343 return result;
346 static WINDOW *
347 parent_of(WINDOW *win)
349 WINDOW *result = 0;
350 int n = window2num(win);
351 if (n >= 0)
352 result = all_windows[n].parent;
353 return result;
356 static void
357 repaint_one(WINDOW *win)
359 touchwin(win);
360 wnoutrefresh(win);
363 static void
364 refresh_all(WINDOW *win)
366 unsigned n;
368 for (n = 0; n < num_windows; ++n) {
369 if (all_windows[n].child != win) {
370 repaint_one(all_windows[n].child);
374 repaint_one(win);
375 doupdate();
378 static WINDOW *
379 next_window(WINDOW *win)
381 WINDOW *result = win;
382 int n = window2num(win);
384 if (n++ >= 0) {
385 result = all_windows[(unsigned) n % num_windows].child;
386 wmove(result, 0, 0);
387 wrefresh(result);
389 return result;
392 static WINDOW *
393 prev_window(WINDOW *win)
395 WINDOW *result = win;
396 int n = window2num(win);
398 if (n-- >= 0) {
399 if (n < 0)
400 n = (int) (num_windows - 1);
401 result = all_windows[(unsigned) n % num_windows].child;
402 wmove(result, 0, 0);
403 wrefresh(result);
405 return result;
408 static void
409 recur_move_window(WINDOW *parent, int dy, int dx)
411 unsigned n;
413 for (n = 0; n < num_windows; ++n) {
414 if (all_windows[n].parent == parent) {
415 mvwin(all_windows[n].child, dy, dx);
416 recur_move_window(all_windows[n].child, dy, dx);
422 * test mvwin().
424 static bool
425 move_window(WINDOW *win, bool recur)
427 WINDOW *parent = parent_of(win);
428 bool result = FALSE;
430 if (parent != 0) {
431 bool top = (parent == stdscr);
432 int min_col = top ? COL_MIN : 0;
433 int max_col = top ? COL_MAX : getmaxx(parent);
434 int min_line = top ? LINE_MIN : 0;
435 int max_line = top ? LINE_MAX : getmaxy(parent);
436 PAIR *tmp;
437 bool more;
439 head_line("Select new position for %swindow", top ? "" : "sub");
441 while ((tmp = selectcell(parent,
442 win,
443 min_line, min_col,
444 max_line, max_col,
445 FALSE,
446 &more)) != 0) {
447 int y0, x0;
448 getbegyx(parent, y0, x0);
450 * Moving a subwindow has the effect of moving a viewport around
451 * the screen. The parent window retains the contents of the
452 * subwindow in the original location, but the viewport will show
453 * the contents (again) at the new location. So it will look odd
454 * when testing.
456 if (mvwin(win, y0 + tmp->y, x0 + tmp->x) != ERR) {
457 if (recur) {
458 recur_move_window(win, tmp->y, tmp->x);
460 refresh_all(win);
461 doupdate();
462 result = TRUE;
463 } else {
464 result = FALSE;
466 if (!more)
467 break;
470 head_line("done");
471 return result;
474 static void
475 show_derwin(WINDOW *win)
477 int pary, parx, maxy, maxx;
479 getmaxyx(win, maxy, maxx);
480 getparyx(win, pary, parx);
482 head_line("Select new position for derived window at %d,%d (%d,%d)",
483 pary, parx, maxy, maxx);
487 * test mvderwin().
489 static bool
490 move_derwin(WINDOW *win)
492 WINDOW *parent = parent_of(win);
493 bool result = FALSE;
495 if (parent != 0) {
496 bool top = (parent == stdscr);
497 int min_col = top ? COL_MIN : 0;
498 int max_col = top ? COL_MAX : getmaxx(parent);
499 int min_line = top ? LINE_MIN : 0;
500 int max_line = top ? LINE_MAX : getmaxy(parent);
501 PAIR *tmp;
502 bool more;
504 show_derwin(win);
505 while ((tmp = selectcell(parent,
506 win,
507 min_line, min_col,
508 max_line, max_col,
509 TRUE,
510 &more)) != 0) {
511 if (mvderwin(win, tmp->y, tmp->x) != ERR) {
512 refresh_all(win);
513 doupdate();
514 repaint_one(win);
515 doupdate();
516 result = TRUE;
517 show_derwin(win);
518 } else {
519 flash();
521 if (!more)
522 break;
525 head_line("done");
526 return result;
529 static void
530 fill_window(WINDOW *win, chtype ch)
532 int y, x;
533 int y0, x0;
534 int y1, x1;
536 getyx(win, y0, x0);
537 getmaxyx(win, y1, x1);
538 for (y = 0; y < y1; ++y) {
539 for (x = 0; x < x1; ++x) {
540 MvWAddCh(win, y, x, ch);
543 wsyncdown(win);
544 wmove(win, y0, x0);
545 wrefresh(win);
548 static void
549 fill_with_pattern(WINDOW *win)
551 int y, x;
552 int y0, x0;
553 int y1, x1;
554 int ch = 'a';
556 getyx(win, y0, x0);
557 getmaxyx(win, y1, x1);
558 for (y = 0; y < y1; ++y) {
559 for (x = 0; x < x1; ++x) {
560 MvWAddCh(win, y, x, (chtype) ch);
561 if (++ch > 'z')
562 ch = 'a';
565 wsyncdown(win);
566 wmove(win, y0, x0);
567 wrefresh(win);
570 #define lines_of(ul,lr) (lr.y - ul.y + 1)
571 #define cols_of(ul,lr) (lr.x - ul.x + 1)
572 #define pair_of(ul) ul.y, ul.x
574 static WINDOW *
575 create_my_window(WINDOW *current)
577 PAIR ul, lr;
578 WINDOW *result = 0;
580 if (getwindow(stdscr, &ul, &lr)) {
581 result = newwin(lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
582 if (result != 0) {
583 fill_window(result, 'c');
584 add_window(stdscr, result);
587 if (result == 0)
588 result = current;
589 return result;
592 static WINDOW *
593 create_my_derwin(WINDOW *parent)
595 PAIR ul, lr;
596 WINDOW *result = 0;
598 if (getwindow(parent, &ul, &lr)) {
599 result = derwin(parent, lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
600 if (result != 0) {
601 fill_window(result, 'd');
602 add_window(parent, result);
605 if (result == 0)
606 result = parent;
607 return result;
610 static WINDOW *
611 create_my_subwin(WINDOW *parent)
613 PAIR ul, lr;
614 WINDOW *result = 0;
616 if (getwindow(parent, &ul, &lr)) {
617 result = subwin(parent,
618 lines_of(ul, lr),
619 cols_of(ul, lr),
620 ul.y + getbegy(parent),
621 ul.x + getbegx(parent));
622 if (result != 0) {
623 fill_window(result, 's');
624 add_window(parent, result);
627 if (result == 0)
628 result = parent;
629 return result;
632 static void
633 show_help(WINDOW *current)
635 /* *INDENT-OFF* */
636 static struct {
637 int key;
638 CONST_FMT char * msg;
639 } help[] = {
640 { '?', "Show this screen" },
641 { 'b', "Draw a box inside the current window" },
642 { 'c', "Create a new window" },
643 { 'd', "Create a new derived window" },
644 { 'D', "Move derived window (moves viewport)" },
645 { 'f', "Fill the current window with the next character" },
646 { 'F', "Fill the current window with a pattern" },
647 { 'm', "Move the current window" },
648 { 'M', "Move the current window (and its children)" },
649 { 'q', "Quit" },
650 { 's', "Create a new subwindow" },
651 { CTRL('L'), "Repaint all windows, doing current one last" },
652 { CTRL('N'), "Cursor to next window" },
653 { CTRL('P'), "Cursor to previous window" },
655 /* *INDENT-ON* */
657 WINDOW *mywin = newwin(LINES, COLS, 0, 0);
658 int row;
660 for (row = 0; row < LINES - 2 && row < (int) SIZEOF(help); ++row) {
661 wmove(mywin, row + 1, 1);
662 wprintw(mywin, "%s", keyname(help[row].key));
663 wmove(mywin, row + 1, 20);
664 wprintw(mywin, "%s", help[row].msg);
666 box_inside(mywin);
667 wmove(mywin, 1, 1);
668 wgetch(mywin);
669 delwin(mywin);
670 refresh_all(current);
674 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
676 WINDOW *current_win;
677 int ch;
678 bool done = FALSE;
680 initscr();
681 cbreak();
682 noecho();
683 nonl();
684 intrflush(stdscr, FALSE);
686 add_window(0, current_win = stdscr);
688 #ifdef NCURSES_MOUSE_VERSION
689 (void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL);
690 #endif /* NCURSES_MOUSE_VERSION */
692 while (!done && (ch = wgetch(current_win)) != ERR) {
693 int y, x;
695 getyx(current_win, y, x);
697 switch (ch) {
698 case '?':
699 show_help(current_win);
700 break;
701 case 'b':
702 box_inside(current_win);
703 break;
704 case 'c':
705 current_win = create_my_window(current_win);
706 break;
707 case 'd':
708 current_win = create_my_derwin(current_win);
709 break;
710 case 'D':
711 if (!move_derwin(current_win)) {
712 tail_line("error");
713 continue;
715 break;
716 case 'f':
717 fill_window(current_win, (chtype) wgetch(current_win));
718 break;
719 case 'F':
720 fill_with_pattern(current_win);
721 break;
722 case 'm':
723 case 'M':
724 if (!move_window(current_win, (ch == 'M'))) {
725 tail_line("error");
726 continue;
728 break;
729 case 'q':
730 done = TRUE;
731 break;
732 case 's':
733 current_win = create_my_subwin(current_win);
734 break;
735 case CTRL('L'):
736 refresh_all(current_win);
737 break;
738 case CTRL('N'):
739 current_win = next_window(current_win);
740 break;
741 case CTRL('P'):
742 current_win = prev_window(current_win);
743 break;
744 #if 0
745 /* want to allow cursor to move around the current window too */
746 /* want to test the resizing of windows and subwindows too */
747 /* want to allow deleting a window also */
748 #endif
749 default:
750 wmove(current_win, y, x);
751 tail_line("unrecognized key (use '?' for help)");
752 beep();
753 continue;
755 tail_line("size [%d,%d] begin [%d,%d] parent [%d,%d]",
756 getmaxy(current_win),
757 getmaxx(current_win),
758 getbegy(current_win),
759 getbegx(current_win),
760 getpary(current_win),
761 getparx(current_win));
762 wmove(current_win, 0, 0);
764 endwin();
765 ExitProgram(EXIT_SUCCESS);