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