1 /****************************************************************************
2 * Copyright 2018-2019,2020 Thomas E. Dickey *
3 * Copyright 2006-2013,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * $Id: movewindow.c,v 1.51 2020/02/02 23:34:34 tom Exp $
32 * Demonstrate move functions for windows and derived windows from the curses
35 * Author: Thomas E. Dickey
44 add command to reset subwindow's origin to coincide with parent.
45 add command to delete subwindow (check if it has subwindows though)
48 #include <test.priv.h>
50 #if HAVE_MVDERWIN && HAVE_MVWIN
52 #include <popup_msg.h>
58 #if defined(NCURSES_CONST)
59 #define CONST_FMT NCURSES_CONST
60 #elif defined(PDCURSES)
61 #define CONST_FMT const
63 #define CONST_FMT /* nothing */
69 #define LINE_MAX (LINES - 2)
71 #define COL_MAX (COLS - 2)
78 WINDOW *parent; /* need this since WINDOW->_parent is not portable */
79 WINDOW *child; /* the actual value */
82 static void head_line(CONST_FMT char *fmt, ...) GCC_PRINTFLIKE(1, 2);
83 static void tail_line(CONST_FMT char *fmt, ...) GCC_PRINTFLIKE(1, 2);
85 static unsigned num_windows;
86 static FRAME *all_windows;
93 ExitProgram(EXIT_FAILURE);
97 message(int lineno, CONST_FMT char *fmt, va_list argp)
108 vsprintf(buffer, fmt, argp);
111 #elif defined(HAVE_VW_PRINTW)
112 vw_printw(stdscr, fmt, argp);
114 vwprintw(stdscr, fmt, argp);
122 head_line(CONST_FMT char *fmt, ...)
127 message(0, fmt, argp);
132 tail_line(CONST_FMT char *fmt, ...)
137 message(LINES - 1, fmt, argp);
142 * Arrow keys move cursor, return location at current on non-arrow key.
145 selectcell(WINDOW *parent,
152 static PAIR res; /* result cell */
153 int si = lri - uli + 1; /* depth of the select area */
154 int sj = lrj - ulj + 1; /* width of the select area */
155 int i = 0, j = 0; /* offsets into the select area */
162 getparyx(child, i, j);
164 getbegyx(child, i, j);
165 i -= uli + getbegy(parent);
166 j -= ulj + getbegx(parent);
176 tail_line("Upper left [%2d,%2d] Lower right [%2d,%2d] -> %d,%d -> %d,%d",
181 wmove(parent, uli + i, ulj + j);
183 switch (wgetch(parent)) {
204 #ifdef NCURSES_MOUSE_VERSION
210 if (event.y > uli && event.x > ulj) {
211 if (parent != stdscr) {
212 i = event.y - getbegy(parent) - uli;
213 j = event.x - getbegx(parent) - ulj;
242 * If the caller can handle continuous movement, return the result.
254 * Ask user for a window definition.
257 getwindow(WINDOW *parent, PAIR * ul, PAIR * lr)
259 int min_col = (parent == stdscr) ? COL_MIN : 0;
260 int max_col = (parent == stdscr) ? COL_MAX : getmaxx(parent);
261 int min_line = (parent == stdscr) ? LINE_MIN : 0;
262 int max_line = (parent == stdscr) ? LINE_MAX : getmaxy(parent);
266 head_line("Use arrows to move cursor, anything else to mark corner 1");
267 if ((tmp = selectcell(parent, 0,
273 MvWAddCh(parent, ul->y, ul->x, '*');
275 head_line("Use arrows to move cursor, anything else to mark corner 2");
276 if ((tmp = selectcell(parent, 0,
282 MvWAddCh(parent, lr->y, lr->x, '*');
283 wmove(parent, lr->y, lr->x);
286 result = (lr->y != ul->y && lr->x != ul->x);
294 * Draw a box inside the given window.
297 box_inside(WINDOW *win)
303 getmaxyx(win, y1, x1);
305 MvWHLine(win, 0, 0, ACS_HLINE, x1);
306 MvWHLine(win, y1 - 1, 0, ACS_HLINE, x1);
308 MvWVLine(win, 0, 0, ACS_VLINE, y1);
309 MvWVLine(win, 0, x1 - 1, ACS_VLINE, y1);
311 MvWAddCh(win, 0, 0, ACS_ULCORNER);
312 MvWAddCh(win, y1 - 1, 0, ACS_LLCORNER);
313 MvWAddCh(win, 0, x1 - 1, ACS_URCORNER);
314 MvWAddCh(win, y1 - 1, x1 - 1, ACS_LRCORNER);
322 * Add a window to our list.
325 add_window(WINDOW *parent, WINDOW *child)
327 static unsigned have = 0;
328 unsigned need = ((num_windows + 1) | 31) + 1;
332 all_windows = typeRealloc(FRAME, need, all_windows);
334 failed("add_window");
336 all_windows[num_windows].parent = parent;
337 all_windows[num_windows].child = child;
342 window2num(WINDOW *win)
346 for (n = 0; n < (int) num_windows; ++n) {
347 if (win == all_windows[n].child) {
356 parent_of(WINDOW *win)
359 int n = window2num(win);
361 result = all_windows[n].parent;
366 repaint_one(WINDOW *win)
373 refresh_all(WINDOW *win)
377 for (n = 0; n < num_windows; ++n) {
378 if (all_windows[n].child != win) {
379 repaint_one(all_windows[n].child);
388 next_window(WINDOW *win)
390 WINDOW *result = win;
391 int n = window2num(win);
394 result = all_windows[(unsigned) n % num_windows].child;
402 prev_window(WINDOW *win)
404 WINDOW *result = win;
405 int n = window2num(win);
409 n = (int) (num_windows - 1);
410 result = all_windows[(unsigned) n % num_windows].child;
418 recur_move_window(WINDOW *parent, int dy, int dx)
422 for (n = 0; n < num_windows; ++n) {
423 if (all_windows[n].parent == parent) {
424 mvwin(all_windows[n].child, dy, dx);
425 recur_move_window(all_windows[n].child, dy, dx);
434 move_window(WINDOW *win, bool recur)
436 WINDOW *parent = parent_of(win);
440 bool top = (parent == stdscr);
441 int min_col = top ? COL_MIN : 0;
442 int max_col = top ? COL_MAX : getmaxx(parent);
443 int min_line = top ? LINE_MIN : 0;
444 int max_line = top ? LINE_MAX : getmaxy(parent);
448 head_line("Select new position for %swindow", top ? "" : "sub");
450 while ((tmp = selectcell(parent,
457 getbegyx(parent, y0, x0);
459 * Moving a subwindow has the effect of moving a viewport around
460 * the screen. The parent window retains the contents of the
461 * subwindow in the original location, but the viewport will show
462 * the contents (again) at the new location. So it will look odd
465 if (mvwin(win, y0 + tmp->y, x0 + tmp->x) != ERR) {
467 recur_move_window(win, tmp->y, tmp->x);
484 show_derwin(WINDOW *win)
486 int pary, parx, maxy, maxx;
488 getmaxyx(win, maxy, maxx);
489 getparyx(win, pary, parx);
491 head_line("Select new position for derived window at %d,%d (%d,%d)",
492 pary, parx, maxy, maxx);
499 move_derwin(WINDOW *win)
501 WINDOW *parent = parent_of(win);
505 bool top = (parent == stdscr);
506 int min_col = top ? COL_MIN : 0;
507 int max_col = top ? COL_MAX : getmaxx(parent);
508 int min_line = top ? LINE_MIN : 0;
509 int max_line = top ? LINE_MAX : getmaxy(parent);
514 while ((tmp = selectcell(parent,
520 if (mvderwin(win, tmp->y, tmp->x) != ERR) {
539 fill_window(WINDOW *win, chtype ch)
546 getmaxyx(win, y1, x1);
547 for (y = 0; y < y1; ++y) {
548 for (x = 0; x < x1; ++x) {
549 MvWAddCh(win, y, x, ch);
558 fill_with_pattern(WINDOW *win)
566 getmaxyx(win, y1, x1);
567 for (y = 0; y < y1; ++y) {
568 for (x = 0; x < x1; ++x) {
569 MvWAddCh(win, y, x, (chtype) ch);
579 #define lines_of(ul,lr) (lr.y - ul.y + 1)
580 #define cols_of(ul,lr) (lr.x - ul.x + 1)
581 #define pair_of(ul) ul.y, ul.x
584 create_my_window(WINDOW *current)
589 if (getwindow(stdscr, &ul, &lr)) {
590 result = newwin(lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
592 fill_window(result, 'c');
593 add_window(stdscr, result);
602 create_my_derwin(WINDOW *parent)
607 if (getwindow(parent, &ul, &lr)) {
608 result = derwin(parent, lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
610 fill_window(result, 'd');
611 add_window(parent, result);
620 create_my_subwin(WINDOW *parent)
625 if (getwindow(parent, &ul, &lr)) {
626 result = subwin(parent,
629 ul.y + getbegy(parent),
630 ul.x + getbegx(parent));
632 fill_window(result, 's');
633 add_window(parent, result);
642 show_help(WINDOW *current)
647 CONST_FMT char * msg;
649 { HELP_KEY_1, "Show this screen" },
650 { 'b', "Draw a box inside the current window" },
651 { 'c', "Create a new window" },
652 { 'd', "Create a new derived window" },
653 { 'D', "Move derived window (moves viewport)" },
654 { 'f', "Fill the current window with the next character" },
655 { 'F', "Fill the current window with a pattern" },
656 { 'm', "Move the current window" },
657 { 'M', "Move the current window (and its children)" },
659 { 's', "Create a new subwindow" },
660 { CTRL('L'), "Repaint all windows, doing current one last" },
661 { CTRL('N'), "Cursor to next window" },
662 { CTRL('P'), "Cursor to previous window" },
666 char **msgs = typeCalloc(char *, SIZEOF(help) + 1);
669 for (n = 0; n < SIZEOF(help); ++n) {
670 size_t need = (21 + strlen(help[n].msg));
671 msgs[n] = typeMalloc(char, need);
672 _nc_SPRINTF(msgs[n], _nc_SLIMIT(need)
673 "%-20s%s", keyname(help[n].key), help[n].msg);
675 popup_msg2(current, msgs);
676 for (n = 0; n < SIZEOF(help); ++n) {
683 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
693 intrflush(stdscr, FALSE);
695 add_window(0, current_win = stdscr);
697 #ifdef NCURSES_MOUSE_VERSION
698 (void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL);
699 #endif /* NCURSES_MOUSE_VERSION */
701 while (!done && (ch = wgetch(current_win)) != ERR) {
704 getyx(current_win, y, x);
708 show_help(current_win);
711 box_inside(current_win);
714 current_win = create_my_window(current_win);
717 current_win = create_my_derwin(current_win);
720 if (!move_derwin(current_win)) {
726 fill_window(current_win, (chtype) wgetch(current_win));
729 fill_with_pattern(current_win);
733 if (!move_window(current_win, (ch == 'M'))) {
742 current_win = create_my_subwin(current_win);
745 refresh_all(current_win);
748 current_win = next_window(current_win);
751 current_win = prev_window(current_win);
754 /* want to allow cursor to move around the current window too */
755 /* want to test the resizing of windows and subwindows too */
756 /* want to allow deleting a window also */
759 wmove(current_win, y, x);
760 tail_line("unrecognized key (use '?' for help)");
764 tail_line("size [%d,%d] begin [%d,%d] parent [%d,%d]",
765 getmaxy(current_win),
766 getmaxx(current_win),
767 getbegy(current_win),
768 getbegx(current_win),
769 getpary(current_win),
770 getparx(current_win));
771 wmove(current_win, 0, 0);
777 ExitProgram(EXIT_SUCCESS);
783 printf("This program requires the curses mvderwin and mvwin functions\n");
784 ExitProgram(EXIT_FAILURE);