1 /****************************************************************************
2 * Copyright (c) 2006-2013,2017 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.46 2017/12/23 21:36:59 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);
109 vwprintw(stdscr, fmt, argp);
117 head_line(CONST_FMT char *fmt,...)
122 message(0, fmt, argp);
127 tail_line(CONST_FMT char *fmt,...)
132 message(LINES - 1, fmt, argp);
137 * Arrow keys move cursor, return location at current on non-arrow key.
140 selectcell(WINDOW *parent,
147 static PAIR res; /* result cell */
148 int si = lri - uli + 1; /* depth of the select area */
149 int sj = lrj - ulj + 1; /* width of the select area */
150 int i = 0, j = 0; /* offsets into the select area */
157 getparyx(child, i, j);
159 getbegyx(child, i, j);
160 i -= uli + getbegy(parent);
161 j -= ulj + getbegx(parent);
171 tail_line("Upper left [%2d,%2d] Lower right [%2d,%2d] -> %d,%d -> %d,%d",
176 wmove(parent, uli + i, ulj + j);
178 switch (wgetch(parent)) {
199 #ifdef NCURSES_MOUSE_VERSION
205 if (event.y > uli && event.x > ulj) {
206 if (parent != stdscr) {
207 i = event.y - getbegy(parent) - uli;
208 j = event.x - getbegx(parent) - ulj;
237 * If the caller can handle continuous movement, return the result.
249 * Ask user for a window definition.
252 getwindow(WINDOW *parent, PAIR * ul, PAIR * lr)
254 int min_col = (parent == stdscr) ? COL_MIN : 0;
255 int max_col = (parent == stdscr) ? COL_MAX : getmaxx(parent);
256 int min_line = (parent == stdscr) ? LINE_MIN : 0;
257 int max_line = (parent == stdscr) ? LINE_MAX : getmaxy(parent);
261 head_line("Use arrows to move cursor, anything else to mark corner 1");
262 if ((tmp = selectcell(parent, 0,
268 MvWAddCh(parent, ul->y, ul->x, '*');
270 head_line("Use arrows to move cursor, anything else to mark corner 2");
271 if ((tmp = selectcell(parent, 0,
277 MvWAddCh(parent, lr->y, lr->x, '*');
278 wmove(parent, lr->y, lr->x);
281 result = (lr->y != ul->y && lr->x != ul->x);
289 * Draw a box inside the given window.
292 box_inside(WINDOW *win)
298 getmaxyx(win, y1, x1);
300 MvWHLine(win, 0, 0, ACS_HLINE, x1);
301 MvWHLine(win, y1 - 1, 0, ACS_HLINE, x1);
303 MvWVLine(win, 0, 0, ACS_VLINE, y1);
304 MvWVLine(win, 0, x1 - 1, ACS_VLINE, y1);
306 MvWAddCh(win, 0, 0, ACS_ULCORNER);
307 MvWAddCh(win, y1 - 1, 0, ACS_LLCORNER);
308 MvWAddCh(win, 0, x1 - 1, ACS_URCORNER);
309 MvWAddCh(win, y1 - 1, x1 - 1, ACS_LRCORNER);
317 * Add a window to our list.
320 add_window(WINDOW *parent, WINDOW *child)
322 static unsigned have = 0;
323 unsigned need = ((num_windows + 1) | 31) + 1;
327 all_windows = typeRealloc(FRAME, need, all_windows);
329 failed("add_window");
331 all_windows[num_windows].parent = parent;
332 all_windows[num_windows].child = child;
337 window2num(WINDOW *win)
341 for (n = 0; n < (int) num_windows; ++n) {
342 if (win == all_windows[n].child) {
351 parent_of(WINDOW *win)
354 int n = window2num(win);
356 result = all_windows[n].parent;
361 repaint_one(WINDOW *win)
368 refresh_all(WINDOW *win)
372 for (n = 0; n < num_windows; ++n) {
373 if (all_windows[n].child != win) {
374 repaint_one(all_windows[n].child);
383 next_window(WINDOW *win)
385 WINDOW *result = win;
386 int n = window2num(win);
389 result = all_windows[(unsigned) n % num_windows].child;
397 prev_window(WINDOW *win)
399 WINDOW *result = win;
400 int n = window2num(win);
404 n = (int) (num_windows - 1);
405 result = all_windows[(unsigned) n % num_windows].child;
413 recur_move_window(WINDOW *parent, int dy, int dx)
417 for (n = 0; n < num_windows; ++n) {
418 if (all_windows[n].parent == parent) {
419 mvwin(all_windows[n].child, dy, dx);
420 recur_move_window(all_windows[n].child, dy, dx);
429 move_window(WINDOW *win, bool recur)
431 WINDOW *parent = parent_of(win);
435 bool top = (parent == stdscr);
436 int min_col = top ? COL_MIN : 0;
437 int max_col = top ? COL_MAX : getmaxx(parent);
438 int min_line = top ? LINE_MIN : 0;
439 int max_line = top ? LINE_MAX : getmaxy(parent);
443 head_line("Select new position for %swindow", top ? "" : "sub");
445 while ((tmp = selectcell(parent,
452 getbegyx(parent, y0, x0);
454 * Moving a subwindow has the effect of moving a viewport around
455 * the screen. The parent window retains the contents of the
456 * subwindow in the original location, but the viewport will show
457 * the contents (again) at the new location. So it will look odd
460 if (mvwin(win, y0 + tmp->y, x0 + tmp->x) != ERR) {
462 recur_move_window(win, tmp->y, tmp->x);
479 show_derwin(WINDOW *win)
481 int pary, parx, maxy, maxx;
483 getmaxyx(win, maxy, maxx);
484 getparyx(win, pary, parx);
486 head_line("Select new position for derived window at %d,%d (%d,%d)",
487 pary, parx, maxy, maxx);
494 move_derwin(WINDOW *win)
496 WINDOW *parent = parent_of(win);
500 bool top = (parent == stdscr);
501 int min_col = top ? COL_MIN : 0;
502 int max_col = top ? COL_MAX : getmaxx(parent);
503 int min_line = top ? LINE_MIN : 0;
504 int max_line = top ? LINE_MAX : getmaxy(parent);
509 while ((tmp = selectcell(parent,
515 if (mvderwin(win, tmp->y, tmp->x) != ERR) {
534 fill_window(WINDOW *win, chtype ch)
541 getmaxyx(win, y1, x1);
542 for (y = 0; y < y1; ++y) {
543 for (x = 0; x < x1; ++x) {
544 MvWAddCh(win, y, x, ch);
553 fill_with_pattern(WINDOW *win)
561 getmaxyx(win, y1, x1);
562 for (y = 0; y < y1; ++y) {
563 for (x = 0; x < x1; ++x) {
564 MvWAddCh(win, y, x, (chtype) ch);
574 #define lines_of(ul,lr) (lr.y - ul.y + 1)
575 #define cols_of(ul,lr) (lr.x - ul.x + 1)
576 #define pair_of(ul) ul.y, ul.x
579 create_my_window(WINDOW *current)
584 if (getwindow(stdscr, &ul, &lr)) {
585 result = newwin(lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
587 fill_window(result, 'c');
588 add_window(stdscr, result);
597 create_my_derwin(WINDOW *parent)
602 if (getwindow(parent, &ul, &lr)) {
603 result = derwin(parent, lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
605 fill_window(result, 'd');
606 add_window(parent, result);
615 create_my_subwin(WINDOW *parent)
620 if (getwindow(parent, &ul, &lr)) {
621 result = subwin(parent,
624 ul.y + getbegy(parent),
625 ul.x + getbegx(parent));
627 fill_window(result, 's');
628 add_window(parent, result);
637 show_help(WINDOW *current)
642 CONST_FMT char * msg;
644 { HELP_KEY_1, "Show this screen" },
645 { 'b', "Draw a box inside the current window" },
646 { 'c', "Create a new window" },
647 { 'd', "Create a new derived window" },
648 { 'D', "Move derived window (moves viewport)" },
649 { 'f', "Fill the current window with the next character" },
650 { 'F', "Fill the current window with a pattern" },
651 { 'm', "Move the current window" },
652 { 'M', "Move the current window (and its children)" },
654 { 's', "Create a new subwindow" },
655 { CTRL('L'), "Repaint all windows, doing current one last" },
656 { CTRL('N'), "Cursor to next window" },
657 { CTRL('P'), "Cursor to previous window" },
661 char **msgs = typeCalloc(char *, SIZEOF(help) + 1);
664 for (n = 0; n < SIZEOF(help); ++n) {
665 msgs[n] = typeMalloc(char, 21 + strlen(help[n].msg));
666 sprintf(msgs[n], "%-20s%s", keyname(help[n].key), help[n].msg);
668 popup_msg2(current, msgs);
669 for (n = 0; n < SIZEOF(help); ++n) {
676 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
686 intrflush(stdscr, FALSE);
688 add_window(0, current_win = stdscr);
690 #ifdef NCURSES_MOUSE_VERSION
691 (void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL);
692 #endif /* NCURSES_MOUSE_VERSION */
694 while (!done && (ch = wgetch(current_win)) != ERR) {
697 getyx(current_win, y, x);
701 show_help(current_win);
704 box_inside(current_win);
707 current_win = create_my_window(current_win);
710 current_win = create_my_derwin(current_win);
713 if (!move_derwin(current_win)) {
719 fill_window(current_win, (chtype) wgetch(current_win));
722 fill_with_pattern(current_win);
726 if (!move_window(current_win, (ch == 'M'))) {
735 current_win = create_my_subwin(current_win);
738 refresh_all(current_win);
741 current_win = next_window(current_win);
744 current_win = prev_window(current_win);
747 /* want to allow cursor to move around the current window too */
748 /* want to test the resizing of windows and subwindows too */
749 /* want to allow deleting a window also */
752 wmove(current_win, y, x);
753 tail_line("unrecognized key (use '?' for help)");
757 tail_line("size [%d,%d] begin [%d,%d] parent [%d,%d]",
758 getmaxy(current_win),
759 getmaxx(current_win),
760 getbegy(current_win),
761 getbegx(current_win),
762 getpary(current_win),
763 getparx(current_win));
764 wmove(current_win, 0, 0);
770 ExitProgram(EXIT_SUCCESS);
776 printf("This program requires the curses mvderwin and mvwin functions\n");
777 ExitProgram(EXIT_FAILURE);