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