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.43 2017/06/24 20:48: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 #include <popup_msg.h>
55 #ifdef NCURSES_VERSION
56 #define CONST_FMT const
58 #define CONST_FMT /* nothing */
64 #define LINE_MAX (LINES - 2)
66 #define COL_MAX (COLS - 2)
73 WINDOW *parent; /* need this since WINDOW->_parent is not portable */
74 WINDOW *child; /* the actual value */
77 static void head_line(CONST_FMT char *fmt,...) GCC_PRINTFLIKE(1, 2);
78 static void tail_line(CONST_FMT char *fmt,...) GCC_PRINTFLIKE(1, 2);
80 static unsigned num_windows;
81 static FRAME *all_windows;
88 ExitProgram(EXIT_FAILURE);
92 message(int lineno, CONST_FMT char *fmt, va_list argp)
103 vsprintf(buffer, fmt, argp);
107 vwprintw(stdscr, fmt, argp);
115 head_line(CONST_FMT char *fmt,...)
120 message(0, fmt, argp);
125 tail_line(CONST_FMT char *fmt,...)
130 message(LINES - 1, fmt, argp);
135 * Arrow keys move cursor, return location at current on non-arrow key.
138 selectcell(WINDOW *parent,
145 static PAIR res; /* result cell */
146 int si = lri - uli + 1; /* depth of the select area */
147 int sj = lrj - ulj + 1; /* width of the select area */
148 int i = 0, j = 0; /* offsets into the select area */
155 getparyx(child, i, j);
157 getbegyx(child, i, j);
158 i -= uli + getbegy(parent);
159 j -= ulj + getbegx(parent);
169 tail_line("Upper left [%2d,%2d] Lower right [%2d,%2d] -> %d,%d -> %d,%d",
174 wmove(parent, uli + i, ulj + j);
176 switch (wgetch(parent)) {
197 #ifdef NCURSES_MOUSE_VERSION
203 if (event.y > uli && event.x > ulj) {
204 if (parent != stdscr) {
205 i = event.y - getbegy(parent) - uli;
206 j = event.x - getbegx(parent) - ulj;
235 * If the caller can handle continuous movement, return the result.
247 * Ask user for a window definition.
250 getwindow(WINDOW *parent, PAIR * ul, PAIR * lr)
252 int min_col = (parent == stdscr) ? COL_MIN : 0;
253 int max_col = (parent == stdscr) ? COL_MAX : getmaxx(parent);
254 int min_line = (parent == stdscr) ? LINE_MIN : 0;
255 int max_line = (parent == stdscr) ? LINE_MAX : getmaxy(parent);
259 head_line("Use arrows to move cursor, anything else to mark corner 1");
260 if ((tmp = selectcell(parent, 0,
266 MvWAddCh(parent, ul->y, ul->x, '*');
268 head_line("Use arrows to move cursor, anything else to mark corner 2");
269 if ((tmp = selectcell(parent, 0,
275 MvWAddCh(parent, lr->y, lr->x, '*');
276 wmove(parent, lr->y, lr->x);
279 result = (lr->y != ul->y && lr->x != ul->x);
287 * Draw a box inside the given window.
290 box_inside(WINDOW *win)
296 getmaxyx(win, y1, x1);
298 MvWHLine(win, 0, 0, ACS_HLINE, x1);
299 MvWHLine(win, y1 - 1, 0, ACS_HLINE, x1);
301 MvWVLine(win, 0, 0, ACS_VLINE, y1);
302 MvWVLine(win, 0, x1 - 1, ACS_VLINE, y1);
304 MvWAddCh(win, 0, 0, ACS_ULCORNER);
305 MvWAddCh(win, y1 - 1, 0, ACS_LLCORNER);
306 MvWAddCh(win, 0, x1 - 1, ACS_URCORNER);
307 MvWAddCh(win, y1 - 1, x1 - 1, ACS_LRCORNER);
315 * Add a window to our list.
318 add_window(WINDOW *parent, WINDOW *child)
320 static unsigned have = 0;
321 unsigned need = ((num_windows + 1) | 31) + 1;
325 all_windows = typeRealloc(FRAME, need, all_windows);
327 failed("add_window");
329 all_windows[num_windows].parent = parent;
330 all_windows[num_windows].child = child;
335 window2num(WINDOW *win)
339 for (n = 0; n < (int) num_windows; ++n) {
340 if (win == all_windows[n].child) {
349 parent_of(WINDOW *win)
352 int n = window2num(win);
354 result = all_windows[n].parent;
359 repaint_one(WINDOW *win)
366 refresh_all(WINDOW *win)
370 for (n = 0; n < num_windows; ++n) {
371 if (all_windows[n].child != win) {
372 repaint_one(all_windows[n].child);
381 next_window(WINDOW *win)
383 WINDOW *result = win;
384 int n = window2num(win);
387 result = all_windows[(unsigned) n % num_windows].child;
395 prev_window(WINDOW *win)
397 WINDOW *result = win;
398 int n = window2num(win);
402 n = (int) (num_windows - 1);
403 result = all_windows[(unsigned) n % num_windows].child;
411 recur_move_window(WINDOW *parent, int dy, int dx)
415 for (n = 0; n < num_windows; ++n) {
416 if (all_windows[n].parent == parent) {
417 mvwin(all_windows[n].child, dy, dx);
418 recur_move_window(all_windows[n].child, dy, dx);
427 move_window(WINDOW *win, bool recur)
429 WINDOW *parent = parent_of(win);
433 bool top = (parent == stdscr);
434 int min_col = top ? COL_MIN : 0;
435 int max_col = top ? COL_MAX : getmaxx(parent);
436 int min_line = top ? LINE_MIN : 0;
437 int max_line = top ? LINE_MAX : getmaxy(parent);
441 head_line("Select new position for %swindow", top ? "" : "sub");
443 while ((tmp = selectcell(parent,
450 getbegyx(parent, y0, x0);
452 * Moving a subwindow has the effect of moving a viewport around
453 * the screen. The parent window retains the contents of the
454 * subwindow in the original location, but the viewport will show
455 * the contents (again) at the new location. So it will look odd
458 if (mvwin(win, y0 + tmp->y, x0 + tmp->x) != ERR) {
460 recur_move_window(win, tmp->y, tmp->x);
477 show_derwin(WINDOW *win)
479 int pary, parx, maxy, maxx;
481 getmaxyx(win, maxy, maxx);
482 getparyx(win, pary, parx);
484 head_line("Select new position for derived window at %d,%d (%d,%d)",
485 pary, parx, maxy, maxx);
492 move_derwin(WINDOW *win)
494 WINDOW *parent = parent_of(win);
498 bool top = (parent == stdscr);
499 int min_col = top ? COL_MIN : 0;
500 int max_col = top ? COL_MAX : getmaxx(parent);
501 int min_line = top ? LINE_MIN : 0;
502 int max_line = top ? LINE_MAX : getmaxy(parent);
507 while ((tmp = selectcell(parent,
513 if (mvderwin(win, tmp->y, tmp->x) != ERR) {
532 fill_window(WINDOW *win, chtype ch)
539 getmaxyx(win, y1, x1);
540 for (y = 0; y < y1; ++y) {
541 for (x = 0; x < x1; ++x) {
542 MvWAddCh(win, y, x, ch);
551 fill_with_pattern(WINDOW *win)
559 getmaxyx(win, y1, x1);
560 for (y = 0; y < y1; ++y) {
561 for (x = 0; x < x1; ++x) {
562 MvWAddCh(win, y, x, (chtype) ch);
572 #define lines_of(ul,lr) (lr.y - ul.y + 1)
573 #define cols_of(ul,lr) (lr.x - ul.x + 1)
574 #define pair_of(ul) ul.y, ul.x
577 create_my_window(WINDOW *current)
582 if (getwindow(stdscr, &ul, &lr)) {
583 result = newwin(lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
585 fill_window(result, 'c');
586 add_window(stdscr, result);
595 create_my_derwin(WINDOW *parent)
600 if (getwindow(parent, &ul, &lr)) {
601 result = derwin(parent, lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
603 fill_window(result, 'd');
604 add_window(parent, result);
613 create_my_subwin(WINDOW *parent)
618 if (getwindow(parent, &ul, &lr)) {
619 result = subwin(parent,
622 ul.y + getbegy(parent),
623 ul.x + getbegx(parent));
625 fill_window(result, 's');
626 add_window(parent, result);
635 show_help(WINDOW *current)
640 CONST_FMT char * msg;
642 { HELP_KEY_1, "Show this screen" },
643 { 'b', "Draw a box inside the current window" },
644 { 'c', "Create a new window" },
645 { 'd', "Create a new derived window" },
646 { 'D', "Move derived window (moves viewport)" },
647 { 'f', "Fill the current window with the next character" },
648 { 'F', "Fill the current window with a pattern" },
649 { 'm', "Move the current window" },
650 { 'M', "Move the current window (and its children)" },
652 { 's', "Create a new subwindow" },
653 { CTRL('L'), "Repaint all windows, doing current one last" },
654 { CTRL('N'), "Cursor to next window" },
655 { CTRL('P'), "Cursor to previous window" },
659 char **msgs = typeCalloc(char *, SIZEOF(help) + 1);
662 for (n = 0; n < SIZEOF(help); ++n) {
663 msgs[n] = typeMalloc(char, 21 + strlen(help[n].msg));
664 sprintf(msgs[n], "%-20s%s", keyname(help[n].key), help[n].msg);
666 popup_msg2(current, msgs);
667 for (n = 0; n < SIZEOF(help); ++n) {
674 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
684 intrflush(stdscr, FALSE);
686 add_window(0, current_win = stdscr);
688 #ifdef NCURSES_MOUSE_VERSION
689 (void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL);
690 #endif /* NCURSES_MOUSE_VERSION */
692 while (!done && (ch = wgetch(current_win)) != ERR) {
695 getyx(current_win, y, x);
699 show_help(current_win);
702 box_inside(current_win);
705 current_win = create_my_window(current_win);
708 current_win = create_my_derwin(current_win);
711 if (!move_derwin(current_win)) {
717 fill_window(current_win, (chtype) wgetch(current_win));
720 fill_with_pattern(current_win);
724 if (!move_window(current_win, (ch == 'M'))) {
733 current_win = create_my_subwin(current_win);
736 refresh_all(current_win);
739 current_win = next_window(current_win);
742 current_win = prev_window(current_win);
745 /* want to allow cursor to move around the current window too */
746 /* want to test the resizing of windows and subwindows too */
747 /* want to allow deleting a window also */
750 wmove(current_win, y, x);
751 tail_line("unrecognized key (use '?' for help)");
755 tail_line("size [%d,%d] begin [%d,%d] parent [%d,%d]",
756 getmaxy(current_win),
757 getmaxx(current_win),
758 getbegy(current_win),
759 getbegx(current_win),
760 getpary(current_win),
761 getparx(current_win));
762 wmove(current_win, 0, 0);
768 ExitProgram(EXIT_SUCCESS);