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