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