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