]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/movewindow.c
ncurses 6.1 - patch 20200111
[ncurses.git] / test / movewindow.c
1 /****************************************************************************
2  * Copyright (c) 2006-2018,2019 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.50 2019/01/21 20:11:22 tom Exp $
30  *
31  * Demonstrate move functions for windows and derived windows from the curses
32  * library.
33  *
34  * Author: Thomas E. Dickey
35  */
36 /*
37 derwin
38 mvderwin
39 subwin
40 mvwin
41
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)
45  */
46
47 #include <test.priv.h>
48
49 #if HAVE_MVDERWIN && HAVE_MVWIN
50
51 #include <popup_msg.h>
52
53 #ifdef HAVE_XCURSES
54 #undef derwin
55 #endif
56
57 #if defined(NCURSES_CONST)
58 #define CONST_FMT NCURSES_CONST
59 #elif defined(PDCURSES)
60 #define CONST_FMT const
61 #else
62 #define CONST_FMT               /* nothing */
63 #endif
64
65 #undef LINE_MAX
66
67 #define LINE_MIN        2
68 #define LINE_MAX        (LINES - 2)
69 #define COL_MIN         2
70 #define COL_MAX         (COLS - 2)
71
72 typedef struct {
73     int y, x;
74 } PAIR;
75
76 typedef struct {
77     WINDOW *parent;             /* need this since WINDOW->_parent is not portable */
78     WINDOW *child;              /* the actual value */
79 } FRAME;
80
81 static void head_line(CONST_FMT char *fmt, ...) GCC_PRINTFLIKE(1, 2);
82 static void tail_line(CONST_FMT char *fmt, ...) GCC_PRINTFLIKE(1, 2);
83
84 static unsigned num_windows;
85 static FRAME *all_windows;
86
87 static void
88 failed(const char *s)
89 {
90     perror(s);
91     endwin();
92     ExitProgram(EXIT_FAILURE);
93 }
94
95 static void
96 message(int lineno, CONST_FMT char *fmt, va_list argp)
97 {
98     int y, x;
99
100     getyx(stdscr, y, x);
101     move(lineno, 0);
102     clrtoeol();
103
104 #ifdef HAVE_XCURSES
105     {
106         char buffer[1024];
107         vsprintf(buffer, fmt, argp);
108         addstr(buffer);
109     }
110 #elif defined(HAVE_VW_PRINTW)
111     vw_printw(stdscr, fmt, argp);
112 #else
113     vwprintw(stdscr, fmt, argp);
114 #endif
115
116     move(y, x);
117     refresh();
118 }
119
120 static void
121 head_line(CONST_FMT char *fmt, ...)
122 {
123     va_list argp;
124
125     va_start(argp, fmt);
126     message(0, fmt, argp);
127     va_end(argp);
128 }
129
130 static void
131 tail_line(CONST_FMT char *fmt, ...)
132 {
133     va_list argp;
134
135     va_start(argp, fmt);
136     message(LINES - 1, fmt, argp);
137     va_end(argp);
138 }
139
140 /*
141  * Arrow keys move cursor, return location at current on non-arrow key.
142  */
143 static PAIR *
144 selectcell(WINDOW *parent,
145            WINDOW *child,
146            int uli, int ulj,
147            int lri, int lrj,
148            bool relative,
149            bool * more)
150 {
151     static PAIR res;            /* result cell */
152     int si = lri - uli + 1;     /* depth of the select area */
153     int sj = lrj - ulj + 1;     /* width of the select area */
154     int i = 0, j = 0;           /* offsets into the select area */
155
156     res.y = uli;
157     res.x = ulj;
158
159     if (child != 0) {
160         if (relative) {
161             getparyx(child, i, j);
162         } else {
163             getbegyx(child, i, j);
164             i -= uli + getbegy(parent);
165             j -= ulj + getbegx(parent);
166         }
167     }
168
169     if (more)
170         *more = FALSE;
171
172     for (;;) {
173         bool moved = FALSE;
174
175         tail_line("Upper left [%2d,%2d] Lower right [%2d,%2d] -> %d,%d -> %d,%d",
176                   uli, ulj,
177                   lri, lrj,
178                   i, j,
179                   uli + i, ulj + j);
180         wmove(parent, uli + i, ulj + j);
181
182         switch (wgetch(parent)) {
183         case KEY_UP:
184             i += si - 1;
185             moved = TRUE;
186             break;
187         case KEY_DOWN:
188             i++;
189             moved = TRUE;
190             break;
191         case KEY_LEFT:
192             j += sj - 1;
193             moved = TRUE;
194             break;
195         case KEY_RIGHT:
196             j++;
197             moved = TRUE;
198             break;
199         case QUIT:
200             /* FALLTHRU */
201         case ESCAPE:
202             return ((PAIR *) 0);
203 #ifdef NCURSES_MOUSE_VERSION
204         case KEY_MOUSE:
205             {
206                 MEVENT event;
207
208                 getmouse(&event);
209                 if (event.y > uli && event.x > ulj) {
210                     if (parent != stdscr) {
211                         i = event.y - getbegy(parent) - uli;
212                         j = event.x - getbegx(parent) - ulj;
213                     } else {
214                         i = event.y - uli;
215                         j = event.x - ulj;
216                     }
217                 } else {
218                     beep();
219                     break;
220                 }
221             }
222 #endif
223             /* FALLTHRU */
224         default:
225             res.y = uli + i;
226             res.x = ulj + j;
227             return (&res);
228         }
229
230         if (si <= 0)
231             i = 0;
232         else
233             i %= si;
234
235         if (sj <= 0)
236             j = 0;
237         else
238             j %= sj;
239
240         /*
241          * If the caller can handle continuous movement, return the result.
242          */
243         if (moved && more) {
244             *more = TRUE;
245             res.y = uli + i;
246             res.x = ulj + j;
247             return (&res);
248         }
249     }
250 }
251
252 /*
253  * Ask user for a window definition.
254  */
255 static bool
256 getwindow(WINDOW *parent, PAIR * ul, PAIR * lr)
257 {
258     int min_col = (parent == stdscr) ? COL_MIN : 0;
259     int max_col = (parent == stdscr) ? COL_MAX : getmaxx(parent);
260     int min_line = (parent == stdscr) ? LINE_MIN : 0;
261     int max_line = (parent == stdscr) ? LINE_MAX : getmaxy(parent);
262     PAIR *tmp;
263     bool result = FALSE;
264
265     head_line("Use arrows to move cursor, anything else to mark corner 1");
266     if ((tmp = selectcell(parent, 0,
267                           min_line, min_col,
268                           max_line, max_col,
269                           FALSE,
270                           (bool *) 0)) != 0) {
271         *ul = *tmp;
272         MvWAddCh(parent, ul->y, ul->x, '*');
273
274         head_line("Use arrows to move cursor, anything else to mark corner 2");
275         if ((tmp = selectcell(parent, 0,
276                               ul->y, ul->x,
277                               max_line, max_col,
278                               FALSE,
279                               (bool *) 0)) != 0) {
280             *lr = *tmp;
281             MvWAddCh(parent, lr->y, lr->x, '*');
282             wmove(parent, lr->y, lr->x);
283             wsyncdown(parent);
284             wrefresh(parent);
285             result = (lr->y != ul->y && lr->x != ul->x);
286         }
287     }
288     head_line("done");
289     return result;
290 }
291
292 /*
293  * Draw a box inside the given window.
294  */
295 static void
296 box_inside(WINDOW *win)
297 {
298     int y0, x0;
299     int y1, x1;
300
301     getyx(win, y0, x0);
302     getmaxyx(win, y1, x1);
303
304     MvWHLine(win, 0, 0, ACS_HLINE, x1);
305     MvWHLine(win, y1 - 1, 0, ACS_HLINE, x1);
306
307     MvWVLine(win, 0, 0, ACS_VLINE, y1);
308     MvWVLine(win, 0, x1 - 1, ACS_VLINE, y1);
309
310     MvWAddCh(win, 0, 0, ACS_ULCORNER);
311     MvWAddCh(win, y1 - 1, 0, ACS_LLCORNER);
312     MvWAddCh(win, 0, x1 - 1, ACS_URCORNER);
313     MvWAddCh(win, y1 - 1, x1 - 1, ACS_LRCORNER);
314
315     wsyncdown(win);
316     wmove(win, y0, x0);
317     wrefresh(win);
318 }
319
320 /*
321  * Add a window to our list.
322  */
323 static void
324 add_window(WINDOW *parent, WINDOW *child)
325 {
326     static unsigned have = 0;
327     unsigned need = ((num_windows + 1) | 31) + 1;
328
329     keypad(child, TRUE);
330     if (need > have) {
331         all_windows = typeRealloc(FRAME, need, all_windows);
332         if (!all_windows)
333             failed("add_window");
334     }
335     all_windows[num_windows].parent = parent;
336     all_windows[num_windows].child = child;
337     num_windows++;
338 }
339
340 static int
341 window2num(WINDOW *win)
342 {
343     int n;
344     int result = -1;
345     for (n = 0; n < (int) num_windows; ++n) {
346         if (win == all_windows[n].child) {
347             result = n;
348             break;
349         }
350     }
351     return result;
352 }
353
354 static WINDOW *
355 parent_of(WINDOW *win)
356 {
357     WINDOW *result = 0;
358     int n = window2num(win);
359     if (n >= 0)
360         result = all_windows[n].parent;
361     return result;
362 }
363
364 static void
365 repaint_one(WINDOW *win)
366 {
367     touchwin(win);
368     wnoutrefresh(win);
369 }
370
371 static void
372 refresh_all(WINDOW *win)
373 {
374     unsigned n;
375
376     for (n = 0; n < num_windows; ++n) {
377         if (all_windows[n].child != win) {
378             repaint_one(all_windows[n].child);
379         }
380     }
381
382     repaint_one(win);
383     doupdate();
384 }
385
386 static WINDOW *
387 next_window(WINDOW *win)
388 {
389     WINDOW *result = win;
390     int n = window2num(win);
391
392     if (n++ >= 0) {
393         result = all_windows[(unsigned) n % num_windows].child;
394         wmove(result, 0, 0);
395         wrefresh(result);
396     }
397     return result;
398 }
399
400 static WINDOW *
401 prev_window(WINDOW *win)
402 {
403     WINDOW *result = win;
404     int n = window2num(win);
405
406     if (n-- >= 0) {
407         if (n < 0)
408             n = (int) (num_windows - 1);
409         result = all_windows[(unsigned) n % num_windows].child;
410         wmove(result, 0, 0);
411         wrefresh(result);
412     }
413     return result;
414 }
415
416 static void
417 recur_move_window(WINDOW *parent, int dy, int dx)
418 {
419     unsigned n;
420
421     for (n = 0; n < num_windows; ++n) {
422         if (all_windows[n].parent == parent) {
423             mvwin(all_windows[n].child, dy, dx);
424             recur_move_window(all_windows[n].child, dy, dx);
425         }
426     }
427 }
428
429 /*
430  * test mvwin().
431  */
432 static bool
433 move_window(WINDOW *win, bool recur)
434 {
435     WINDOW *parent = parent_of(win);
436     bool result = FALSE;
437
438     if (parent != 0) {
439         bool top = (parent == stdscr);
440         int min_col = top ? COL_MIN : 0;
441         int max_col = top ? COL_MAX : getmaxx(parent);
442         int min_line = top ? LINE_MIN : 0;
443         int max_line = top ? LINE_MAX : getmaxy(parent);
444         PAIR *tmp;
445         bool more;
446
447         head_line("Select new position for %swindow", top ? "" : "sub");
448
449         while ((tmp = selectcell(parent,
450                                  win,
451                                  min_line, min_col,
452                                  max_line, max_col,
453                                  FALSE,
454                                  &more)) != 0) {
455             int y0, x0;
456             getbegyx(parent, y0, x0);
457             /*
458              * Moving a subwindow has the effect of moving a viewport around
459              * the screen.  The parent window retains the contents of the
460              * subwindow in the original location, but the viewport will show
461              * the contents (again) at the new location.  So it will look odd
462              * when testing.
463              */
464             if (mvwin(win, y0 + tmp->y, x0 + tmp->x) != ERR) {
465                 if (recur) {
466                     recur_move_window(win, tmp->y, tmp->x);
467                 }
468                 refresh_all(win);
469                 doupdate();
470                 result = TRUE;
471             } else {
472                 result = FALSE;
473             }
474             if (!more)
475                 break;
476         }
477     }
478     head_line("done");
479     return result;
480 }
481
482 static void
483 show_derwin(WINDOW *win)
484 {
485     int pary, parx, maxy, maxx;
486
487     getmaxyx(win, maxy, maxx);
488     getparyx(win, pary, parx);
489
490     head_line("Select new position for derived window at %d,%d (%d,%d)",
491               pary, parx, maxy, maxx);
492 }
493
494 /*
495  * test mvderwin().
496  */
497 static bool
498 move_derwin(WINDOW *win)
499 {
500     WINDOW *parent = parent_of(win);
501     bool result = FALSE;
502
503     if (parent != 0) {
504         bool top = (parent == stdscr);
505         int min_col = top ? COL_MIN : 0;
506         int max_col = top ? COL_MAX : getmaxx(parent);
507         int min_line = top ? LINE_MIN : 0;
508         int max_line = top ? LINE_MAX : getmaxy(parent);
509         PAIR *tmp;
510         bool more;
511
512         show_derwin(win);
513         while ((tmp = selectcell(parent,
514                                  win,
515                                  min_line, min_col,
516                                  max_line, max_col,
517                                  TRUE,
518                                  &more)) != 0) {
519             if (mvderwin(win, tmp->y, tmp->x) != ERR) {
520                 refresh_all(win);
521                 doupdate();
522                 repaint_one(win);
523                 doupdate();
524                 result = TRUE;
525                 show_derwin(win);
526             } else {
527                 flash();
528             }
529             if (!more)
530                 break;
531         }
532     }
533     head_line("done");
534     return result;
535 }
536
537 static void
538 fill_window(WINDOW *win, chtype ch)
539 {
540     int y, x;
541     int y0, x0;
542     int y1, x1;
543
544     getyx(win, y0, x0);
545     getmaxyx(win, y1, x1);
546     for (y = 0; y < y1; ++y) {
547         for (x = 0; x < x1; ++x) {
548             MvWAddCh(win, y, x, ch);
549         }
550     }
551     wsyncdown(win);
552     wmove(win, y0, x0);
553     wrefresh(win);
554 }
555
556 static void
557 fill_with_pattern(WINDOW *win)
558 {
559     int y, x;
560     int y0, x0;
561     int y1, x1;
562     int ch = 'a';
563
564     getyx(win, y0, x0);
565     getmaxyx(win, y1, x1);
566     for (y = 0; y < y1; ++y) {
567         for (x = 0; x < x1; ++x) {
568             MvWAddCh(win, y, x, (chtype) ch);
569             if (++ch > 'z')
570                 ch = 'a';
571         }
572     }
573     wsyncdown(win);
574     wmove(win, y0, x0);
575     wrefresh(win);
576 }
577
578 #define lines_of(ul,lr) (lr.y - ul.y + 1)
579 #define cols_of(ul,lr)  (lr.x - ul.x + 1)
580 #define pair_of(ul)     ul.y, ul.x
581
582 static WINDOW *
583 create_my_window(WINDOW *current)
584 {
585     PAIR ul, lr;
586     WINDOW *result = 0;
587
588     if (getwindow(stdscr, &ul, &lr)) {
589         result = newwin(lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
590         if (result != 0) {
591             fill_window(result, 'c');
592             add_window(stdscr, result);
593         }
594     }
595     if (result == 0)
596         result = current;
597     return result;
598 }
599
600 static WINDOW *
601 create_my_derwin(WINDOW *parent)
602 {
603     PAIR ul, lr;
604     WINDOW *result = 0;
605
606     if (getwindow(parent, &ul, &lr)) {
607         result = derwin(parent, lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
608         if (result != 0) {
609             fill_window(result, 'd');
610             add_window(parent, result);
611         }
612     }
613     if (result == 0)
614         result = parent;
615     return result;
616 }
617
618 static WINDOW *
619 create_my_subwin(WINDOW *parent)
620 {
621     PAIR ul, lr;
622     WINDOW *result = 0;
623
624     if (getwindow(parent, &ul, &lr)) {
625         result = subwin(parent,
626                         lines_of(ul, lr),
627                         cols_of(ul, lr),
628                         ul.y + getbegy(parent),
629                         ul.x + getbegx(parent));
630         if (result != 0) {
631             fill_window(result, 's');
632             add_window(parent, result);
633         }
634     }
635     if (result == 0)
636         result = parent;
637     return result;
638 }
639
640 static void
641 show_help(WINDOW *current)
642 {
643     /* *INDENT-OFF* */
644     static struct {
645         int     key;
646         CONST_FMT char * msg;
647     } help[] = {
648         { HELP_KEY_1,   "Show this screen" },
649         { 'b',          "Draw a box inside the current window" },
650         { 'c',          "Create a new window" },
651         { 'd',          "Create a new derived window" },
652         { 'D',          "Move derived window (moves viewport)" },
653         { 'f',          "Fill the current window with the next character" },
654         { 'F',          "Fill the current window with a pattern" },
655         { 'm',          "Move the current window" },
656         { 'M',          "Move the current window (and its children)" },
657         { 'q',          "Quit" },
658         { 's',          "Create a new subwindow" },
659         { CTRL('L'),    "Repaint all windows, doing current one last" },
660         { CTRL('N'),    "Cursor to next window" },
661         { CTRL('P'),    "Cursor to previous window" },
662     };
663     /* *INDENT-ON* */
664
665     char **msgs = typeCalloc(char *, SIZEOF(help) + 1);
666     size_t n;
667
668     for (n = 0; n < SIZEOF(help); ++n) {
669         size_t need = (21 + strlen(help[n].msg));
670         msgs[n] = typeMalloc(char, need);
671         _nc_SPRINTF(msgs[n], _nc_SLIMIT(need)
672                     "%-20s%s", keyname(help[n].key), help[n].msg);
673     }
674     popup_msg2(current, msgs);
675     for (n = 0; n < SIZEOF(help); ++n) {
676         free(msgs[n]);
677     }
678     free(msgs);
679 }
680
681 int
682 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
683 {
684     WINDOW *current_win;
685     int ch;
686     bool done = FALSE;
687
688     initscr();
689     cbreak();
690     noecho();
691     nonl();
692     intrflush(stdscr, FALSE);
693
694     add_window(0, current_win = stdscr);
695
696 #ifdef NCURSES_MOUSE_VERSION
697     (void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL);
698 #endif /* NCURSES_MOUSE_VERSION */
699
700     while (!done && (ch = wgetch(current_win)) != ERR) {
701         int y, x;
702
703         getyx(current_win, y, x);
704
705         switch (ch) {
706         case HELP_KEY_1:
707             show_help(current_win);
708             break;
709         case 'b':
710             box_inside(current_win);
711             break;
712         case 'c':
713             current_win = create_my_window(current_win);
714             break;
715         case 'd':
716             current_win = create_my_derwin(current_win);
717             break;
718         case 'D':
719             if (!move_derwin(current_win)) {
720                 tail_line("error");
721                 continue;
722             }
723             break;
724         case 'f':
725             fill_window(current_win, (chtype) wgetch(current_win));
726             break;
727         case 'F':
728             fill_with_pattern(current_win);
729             break;
730         case 'm':
731         case 'M':
732             if (!move_window(current_win, (ch == 'M'))) {
733                 tail_line("error");
734                 continue;
735             }
736             break;
737         case 'q':
738             done = TRUE;
739             break;
740         case 's':
741             current_win = create_my_subwin(current_win);
742             break;
743         case CTRL('L'):
744             refresh_all(current_win);
745             break;
746         case CTRL('N'):
747             current_win = next_window(current_win);
748             break;
749         case CTRL('P'):
750             current_win = prev_window(current_win);
751             break;
752 #if 0
753             /* want to allow cursor to move around the current window too */
754             /* want to test the resizing of windows and subwindows too */
755             /* want to allow deleting a window also */
756 #endif
757         default:
758             wmove(current_win, y, x);
759             tail_line("unrecognized key (use '?' for help)");
760             beep();
761             continue;
762         }
763         tail_line("size [%d,%d] begin [%d,%d] parent [%d,%d]",
764                   getmaxy(current_win),
765                   getmaxx(current_win),
766                   getbegy(current_win),
767                   getbegx(current_win),
768                   getpary(current_win),
769                   getparx(current_win));
770         wmove(current_win, 0, 0);
771     }
772     endwin();
773 #if NO_LEAKS
774     free(all_windows);
775 #endif
776     ExitProgram(EXIT_SUCCESS);
777 }
778 #else
779 int
780 main(void)
781 {
782     printf("This program requires the curses mvderwin and mvwin functions\n");
783     ExitProgram(EXIT_FAILURE);
784 }
785 #endif