1 /****************************************************************************
2 * Copyright (c) 2006-2017,2018 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 * $Id: movewindow.c,v 1.48 2018/11/24 21:29:46 tom Exp $
31 * Demonstrate move functions for windows and derived windows from the curses
34 * Author: Thomas E. Dickey
43 add command to reset subwindow's origin to coincide with parent.
44 add command to delete subwindow (check if it has subwindows though)
47 #include <test.priv.h>
49 #if HAVE_MVDERWIN && HAVE_MVWIN
51 #include <popup_msg.h>
57 #if defined(NCURSES_VERSION) || defined(PDCURSES)
58 #define CONST_FMT const
60 #define CONST_FMT /* nothing */
66 #define LINE_MAX (LINES - 2)
68 #define COL_MAX (COLS - 2)
75 WINDOW *parent; /* need this since WINDOW->_parent is not portable */
76 WINDOW *child; /* the actual value */
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);
82 static unsigned num_windows;
83 static FRAME *all_windows;
90 ExitProgram(EXIT_FAILURE);
94 message(int lineno, CONST_FMT char *fmt, va_list argp)
105 vsprintf(buffer, fmt, argp);
108 #elif defined(HAVE_VW_PRINTW)
109 vw_printw(stdscr, fmt, argp);
111 vwprintw(stdscr, fmt, argp);
119 head_line(CONST_FMT char *fmt,...)
124 message(0, fmt, argp);
129 tail_line(CONST_FMT char *fmt,...)
134 message(LINES - 1, fmt, argp);
139 * Arrow keys move cursor, return location at current on non-arrow key.
142 selectcell(WINDOW *parent,
149 static PAIR res; /* result cell */
150 int si = lri - uli + 1; /* depth of the select area */
151 int sj = lrj - ulj + 1; /* width of the select area */
152 int i = 0, j = 0; /* offsets into the select area */
159 getparyx(child, i, j);
161 getbegyx(child, i, j);
162 i -= uli + getbegy(parent);
163 j -= ulj + getbegx(parent);
173 tail_line("Upper left [%2d,%2d] Lower right [%2d,%2d] -> %d,%d -> %d,%d",
178 wmove(parent, uli + i, ulj + j);
180 switch (wgetch(parent)) {
201 #ifdef NCURSES_MOUSE_VERSION
207 if (event.y > uli && event.x > ulj) {
208 if (parent != stdscr) {
209 i = event.y - getbegy(parent) - uli;
210 j = event.x - getbegx(parent) - ulj;
239 * If the caller can handle continuous movement, return the result.
251 * Ask user for a window definition.
254 getwindow(WINDOW *parent, PAIR * ul, PAIR * lr)
256 int min_col = (parent == stdscr) ? COL_MIN : 0;
257 int max_col = (parent == stdscr) ? COL_MAX : getmaxx(parent);
258 int min_line = (parent == stdscr) ? LINE_MIN : 0;
259 int max_line = (parent == stdscr) ? LINE_MAX : getmaxy(parent);
263 head_line("Use arrows to move cursor, anything else to mark corner 1");
264 if ((tmp = selectcell(parent, 0,
270 MvWAddCh(parent, ul->y, ul->x, '*');
272 head_line("Use arrows to move cursor, anything else to mark corner 2");
273 if ((tmp = selectcell(parent, 0,
279 MvWAddCh(parent, lr->y, lr->x, '*');
280 wmove(parent, lr->y, lr->x);
283 result = (lr->y != ul->y && lr->x != ul->x);
291 * Draw a box inside the given window.
294 box_inside(WINDOW *win)
300 getmaxyx(win, y1, x1);
302 MvWHLine(win, 0, 0, ACS_HLINE, x1);
303 MvWHLine(win, y1 - 1, 0, ACS_HLINE, x1);
305 MvWVLine(win, 0, 0, ACS_VLINE, y1);
306 MvWVLine(win, 0, x1 - 1, ACS_VLINE, y1);
308 MvWAddCh(win, 0, 0, ACS_ULCORNER);
309 MvWAddCh(win, y1 - 1, 0, ACS_LLCORNER);
310 MvWAddCh(win, 0, x1 - 1, ACS_URCORNER);
311 MvWAddCh(win, y1 - 1, x1 - 1, ACS_LRCORNER);
319 * Add a window to our list.
322 add_window(WINDOW *parent, WINDOW *child)
324 static unsigned have = 0;
325 unsigned need = ((num_windows + 1) | 31) + 1;
329 all_windows = typeRealloc(FRAME, need, all_windows);
331 failed("add_window");
333 all_windows[num_windows].parent = parent;
334 all_windows[num_windows].child = child;
339 window2num(WINDOW *win)
343 for (n = 0; n < (int) num_windows; ++n) {
344 if (win == all_windows[n].child) {
353 parent_of(WINDOW *win)
356 int n = window2num(win);
358 result = all_windows[n].parent;
363 repaint_one(WINDOW *win)
370 refresh_all(WINDOW *win)
374 for (n = 0; n < num_windows; ++n) {
375 if (all_windows[n].child != win) {
376 repaint_one(all_windows[n].child);
385 next_window(WINDOW *win)
387 WINDOW *result = win;
388 int n = window2num(win);
391 result = all_windows[(unsigned) n % num_windows].child;
399 prev_window(WINDOW *win)
401 WINDOW *result = win;
402 int n = window2num(win);
406 n = (int) (num_windows - 1);
407 result = all_windows[(unsigned) n % num_windows].child;
415 recur_move_window(WINDOW *parent, int dy, int dx)
419 for (n = 0; n < num_windows; ++n) {
420 if (all_windows[n].parent == parent) {
421 mvwin(all_windows[n].child, dy, dx);
422 recur_move_window(all_windows[n].child, dy, dx);
431 move_window(WINDOW *win, bool recur)
433 WINDOW *parent = parent_of(win);
437 bool top = (parent == stdscr);
438 int min_col = top ? COL_MIN : 0;
439 int max_col = top ? COL_MAX : getmaxx(parent);
440 int min_line = top ? LINE_MIN : 0;
441 int max_line = top ? LINE_MAX : getmaxy(parent);
445 head_line("Select new position for %swindow", top ? "" : "sub");
447 while ((tmp = selectcell(parent,
454 getbegyx(parent, y0, x0);
456 * Moving a subwindow has the effect of moving a viewport around
457 * the screen. The parent window retains the contents of the
458 * subwindow in the original location, but the viewport will show
459 * the contents (again) at the new location. So it will look odd
462 if (mvwin(win, y0 + tmp->y, x0 + tmp->x) != ERR) {
464 recur_move_window(win, tmp->y, tmp->x);
481 show_derwin(WINDOW *win)
483 int pary, parx, maxy, maxx;
485 getmaxyx(win, maxy, maxx);
486 getparyx(win, pary, parx);
488 head_line("Select new position for derived window at %d,%d (%d,%d)",
489 pary, parx, maxy, maxx);
496 move_derwin(WINDOW *win)
498 WINDOW *parent = parent_of(win);
502 bool top = (parent == stdscr);
503 int min_col = top ? COL_MIN : 0;
504 int max_col = top ? COL_MAX : getmaxx(parent);
505 int min_line = top ? LINE_MIN : 0;
506 int max_line = top ? LINE_MAX : getmaxy(parent);
511 while ((tmp = selectcell(parent,
517 if (mvderwin(win, tmp->y, tmp->x) != ERR) {
536 fill_window(WINDOW *win, chtype ch)
543 getmaxyx(win, y1, x1);
544 for (y = 0; y < y1; ++y) {
545 for (x = 0; x < x1; ++x) {
546 MvWAddCh(win, y, x, ch);
555 fill_with_pattern(WINDOW *win)
563 getmaxyx(win, y1, x1);
564 for (y = 0; y < y1; ++y) {
565 for (x = 0; x < x1; ++x) {
566 MvWAddCh(win, y, x, (chtype) ch);
576 #define lines_of(ul,lr) (lr.y - ul.y + 1)
577 #define cols_of(ul,lr) (lr.x - ul.x + 1)
578 #define pair_of(ul) ul.y, ul.x
581 create_my_window(WINDOW *current)
586 if (getwindow(stdscr, &ul, &lr)) {
587 result = newwin(lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
589 fill_window(result, 'c');
590 add_window(stdscr, result);
599 create_my_derwin(WINDOW *parent)
604 if (getwindow(parent, &ul, &lr)) {
605 result = derwin(parent, lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
607 fill_window(result, 'd');
608 add_window(parent, result);
617 create_my_subwin(WINDOW *parent)
622 if (getwindow(parent, &ul, &lr)) {
623 result = subwin(parent,
626 ul.y + getbegy(parent),
627 ul.x + getbegx(parent));
629 fill_window(result, 's');
630 add_window(parent, result);
639 show_help(WINDOW *current)
644 CONST_FMT char * msg;
646 { HELP_KEY_1, "Show this screen" },
647 { 'b', "Draw a box inside the current window" },
648 { 'c', "Create a new window" },
649 { 'd', "Create a new derived window" },
650 { 'D', "Move derived window (moves viewport)" },
651 { 'f', "Fill the current window with the next character" },
652 { 'F', "Fill the current window with a pattern" },
653 { 'm', "Move the current window" },
654 { 'M', "Move the current window (and its children)" },
656 { 's', "Create a new subwindow" },
657 { CTRL('L'), "Repaint all windows, doing current one last" },
658 { CTRL('N'), "Cursor to next window" },
659 { CTRL('P'), "Cursor to previous window" },
663 char **msgs = typeCalloc(char *, SIZEOF(help) + 1);
666 for (n = 0; n < SIZEOF(help); ++n) {
667 msgs[n] = typeMalloc(char, 21 + strlen(help[n].msg));
668 sprintf(msgs[n], "%-20s%s", keyname(help[n].key), help[n].msg);
670 popup_msg2(current, msgs);
671 for (n = 0; n < SIZEOF(help); ++n) {
678 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
688 intrflush(stdscr, FALSE);
690 add_window(0, current_win = stdscr);
692 #ifdef NCURSES_MOUSE_VERSION
693 (void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL);
694 #endif /* NCURSES_MOUSE_VERSION */
696 while (!done && (ch = wgetch(current_win)) != ERR) {
699 getyx(current_win, y, x);
703 show_help(current_win);
706 box_inside(current_win);
709 current_win = create_my_window(current_win);
712 current_win = create_my_derwin(current_win);
715 if (!move_derwin(current_win)) {
721 fill_window(current_win, (chtype) wgetch(current_win));
724 fill_with_pattern(current_win);
728 if (!move_window(current_win, (ch == 'M'))) {
737 current_win = create_my_subwin(current_win);
740 refresh_all(current_win);
743 current_win = next_window(current_win);
746 current_win = prev_window(current_win);
749 /* want to allow cursor to move around the current window too */
750 /* want to test the resizing of windows and subwindows too */
751 /* want to allow deleting a window also */
754 wmove(current_win, y, x);
755 tail_line("unrecognized key (use '?' for help)");
759 tail_line("size [%d,%d] begin [%d,%d] parent [%d,%d]",
760 getmaxy(current_win),
761 getmaxx(current_win),
762 getbegy(current_win),
763 getbegx(current_win),
764 getpary(current_win),
765 getparx(current_win));
766 wmove(current_win, 0, 0);
772 ExitProgram(EXIT_SUCCESS);
778 printf("This program requires the curses mvderwin and mvwin functions\n");
779 ExitProgram(EXIT_FAILURE);