1 /****************************************************************************
2 * Copyright (c) 2006-2010,2012 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.35 2012/06/09 20:30:32 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>
54 #ifdef NCURSES_VERSION
55 #define CONST_FMT const
57 #define CONST_FMT /* nothing */
63 #define LINE_MAX (LINES - 2)
65 #define COL_MAX (COLS - 2)
72 WINDOW *parent; /* need this since WINDOW->_parent is not portable */
73 WINDOW *child; /* the actual value */
76 static void head_line(CONST_FMT char *fmt,...) GCC_PRINTFLIKE(1, 2);
77 static void tail_line(CONST_FMT char *fmt,...) GCC_PRINTFLIKE(1, 2);
79 static unsigned num_windows;
80 static FRAME *all_windows;
83 message(int lineno, CONST_FMT char *fmt, va_list argp)
94 vsprintf(buffer, fmt, argp);
98 vwprintw(stdscr, fmt, argp);
106 head_line(CONST_FMT char *fmt,...)
111 message(0, fmt, argp);
116 tail_line(CONST_FMT char *fmt,...)
121 message(LINES - 1, fmt, argp);
126 * Arrow keys move cursor, return location at current on non-arrow key.
129 selectcell(WINDOW *parent,
136 static PAIR res; /* result cell */
137 int si = lri - uli + 1; /* depth of the select area */
138 int sj = lrj - ulj + 1; /* width of the select area */
139 int i = 0, j = 0; /* offsets into the select area */
146 getparyx(child, i, j);
148 getbegyx(child, i, j);
149 i -= uli + getbegy(parent);
150 j -= ulj + getbegx(parent);
160 tail_line("Upper left [%2d,%2d] Lower right [%2d,%2d] -> %d,%d -> %d,%d",
165 wmove(parent, uli + i, ulj + j);
167 switch (wgetch(parent)) {
187 #ifdef NCURSES_MOUSE_VERSION
193 if (event.y > uli && event.x > ulj) {
194 if (parent != stdscr) {
195 i = event.y - getbegy(parent) - uli;
196 j = event.x - getbegx(parent) - ulj;
218 * If the caller can handle continuous movement, return the result.
230 * Ask user for a window definition.
233 getwindow(WINDOW *parent, PAIR * ul, PAIR * lr)
235 int min_col = (parent == stdscr) ? COL_MIN : 0;
236 int max_col = (parent == stdscr) ? COL_MAX : getmaxx(parent);
237 int min_line = (parent == stdscr) ? LINE_MIN : 0;
238 int max_line = (parent == stdscr) ? LINE_MAX : getmaxy(parent);
242 head_line("Use arrows to move cursor, anything else to mark corner 1");
243 if ((tmp = selectcell(parent, 0,
249 MvWAddCh(parent, ul->y, ul->x, '*');
251 head_line("Use arrows to move cursor, anything else to mark corner 2");
252 if ((tmp = selectcell(parent, 0,
258 MvWAddCh(parent, lr->y, lr->x, '*');
259 wmove(parent, lr->y, lr->x);
262 result = (lr->y != ul->y && lr->x != ul->x);
270 * Draw a box inside the given window.
273 box_inside(WINDOW *win)
279 getmaxyx(win, y1, x1);
281 MvWHLine(win, 0, 0, ACS_HLINE, x1);
282 MvWHLine(win, y1 - 1, 0, ACS_HLINE, x1);
284 MvWVLine(win, 0, 0, ACS_VLINE, y1);
285 MvWVLine(win, 0, x1 - 1, ACS_VLINE, y1);
287 MvWAddCh(win, 0, 0, ACS_ULCORNER);
288 MvWAddCh(win, y1 - 1, 0, ACS_LLCORNER);
289 MvWAddCh(win, 0, x1 - 1, ACS_URCORNER);
290 MvWAddCh(win, y1 - 1, x1 - 1, ACS_LRCORNER);
298 * Add a window to our list.
301 add_window(WINDOW *parent, WINDOW *child)
303 static unsigned have = 0;
304 unsigned need = ((num_windows + 1) | 31) + 1;
308 all_windows = typeRealloc(FRAME, need, all_windows);
310 all_windows[num_windows].parent = parent;
311 all_windows[num_windows].child = child;
316 window2num(WINDOW *win)
320 for (n = 0; n < (int) num_windows; ++n) {
321 if (win == all_windows[n].child) {
330 parent_of(WINDOW *win)
333 int n = window2num(win);
335 result = all_windows[n].parent;
340 repaint_one(WINDOW *win)
347 refresh_all(WINDOW *win)
351 for (n = 0; n < num_windows; ++n) {
352 if (all_windows[n].child != win) {
353 repaint_one(all_windows[n].child);
362 next_window(WINDOW *win)
364 WINDOW *result = win;
365 int n = window2num(win);
368 result = all_windows[(unsigned) n % num_windows].child;
376 prev_window(WINDOW *win)
378 WINDOW *result = win;
379 int n = window2num(win);
383 n = (int) (num_windows - 1);
384 result = all_windows[(unsigned) n % num_windows].child;
392 recur_move_window(WINDOW *parent, int dy, int dx)
396 for (n = 0; n < num_windows; ++n) {
397 if (all_windows[n].parent == parent) {
398 mvwin(all_windows[n].child, dy, dx);
399 recur_move_window(all_windows[n].child, dy, dx);
408 move_window(WINDOW *win, bool recur)
410 WINDOW *parent = parent_of(win);
414 bool top = (parent == stdscr);
415 int min_col = top ? COL_MIN : 0;
416 int max_col = top ? COL_MAX : getmaxx(parent);
417 int min_line = top ? LINE_MIN : 0;
418 int max_line = top ? LINE_MAX : getmaxy(parent);
422 head_line("Select new position for %swindow", top ? "" : "sub");
424 while ((tmp = selectcell(parent,
431 getbegyx(parent, y0, x0);
433 * Moving a subwindow has the effect of moving a viewport around
434 * the screen. The parent window retains the contents of the
435 * subwindow in the original location, but the viewport will show
436 * the contents (again) at the new location. So it will look odd
439 if (mvwin(win, y0 + tmp->y, x0 + tmp->x) != ERR) {
441 recur_move_window(win, tmp->y, tmp->x);
458 show_derwin(WINDOW *win)
460 int pary, parx, maxy, maxx;
462 getmaxyx(win, maxy, maxx);
463 getparyx(win, pary, parx);
465 head_line("Select new position for derived window at %d,%d (%d,%d)",
466 pary, parx, maxy, maxx);
473 move_derwin(WINDOW *win)
475 WINDOW *parent = parent_of(win);
479 bool top = (parent == stdscr);
481 int min_col = top ? COL_MIN : 0;
482 int max_col = top ? COL_MAX : getmaxx(parent);
483 int min_line = top ? LINE_MIN : 0;
484 int max_line = top ? LINE_MAX : getmaxy(parent);
489 while ((tmp = selectcell(parent,
495 if (mvderwin(win, tmp->y, tmp->x) != ERR) {
515 fill_window(WINDOW *win, chtype ch)
522 getmaxyx(win, y1, x1);
523 for (y = 0; y < y1; ++y) {
524 for (x = 0; x < x1; ++x) {
525 MvWAddCh(win, y, x, ch);
534 fill_with_pattern(WINDOW *win)
542 getmaxyx(win, y1, x1);
543 for (y = 0; y < y1; ++y) {
544 for (x = 0; x < x1; ++x) {
545 MvWAddCh(win, y, x, (chtype) ch);
555 #define lines_of(ul,lr) (lr.y - ul.y + 1)
556 #define cols_of(ul,lr) (lr.x - ul.x + 1)
557 #define pair_of(ul) ul.y, ul.x
560 create_my_window(WINDOW *current)
565 if (getwindow(stdscr, &ul, &lr)) {
566 result = newwin(lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
568 fill_window(result, 'c');
569 add_window(stdscr, result);
578 create_my_derwin(WINDOW *parent)
583 if (getwindow(parent, &ul, &lr)) {
584 result = derwin(parent, lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
586 fill_window(result, 'd');
587 add_window(parent, result);
596 create_my_subwin(WINDOW *parent)
601 if (getwindow(parent, &ul, &lr)) {
602 result = subwin(parent,
605 ul.y + getbegy(parent),
606 ul.x + getbegx(parent));
608 fill_window(result, 's');
609 add_window(parent, result);
618 show_help(WINDOW *current)
623 CONST_FMT char * msg;
625 { '?', "Show this screen" },
626 { 'b', "Draw a box inside the current window" },
627 { 'c', "Create a new window" },
628 { 'd', "Create a new derived window" },
629 { 'D', "Move derived window (moves viewport)" },
630 { 'f', "Fill the current window with the next character" },
631 { 'F', "Fill the current window with a pattern" },
632 { 'm', "Move the current window" },
633 { 'M', "Move the current window (and its children)" },
635 { 's', "Create a new subwindow" },
636 { CTRL('L'), "Repaint all windows, doing current one last" },
637 { CTRL('N'), "Cursor to next window" },
638 { CTRL('P'), "Cursor to previous window" },
642 WINDOW *mywin = newwin(LINES, COLS, 0, 0);
645 for (row = 0; row < LINES - 2 && row < (int) SIZEOF(help); ++row) {
646 wmove(mywin, row + 1, 1);
647 wprintw(mywin, "%s", keyname(help[row].key));
648 wmove(mywin, row + 1, 20);
649 wprintw(mywin, "%s", help[row].msg);
655 refresh_all(current);
659 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
669 intrflush(stdscr, FALSE);
671 add_window(0, current_win = stdscr);
673 #ifdef NCURSES_MOUSE_VERSION
674 (void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL);
675 #endif /* NCURSES_MOUSE_VERSION */
677 while (!done && (ch = wgetch(current_win)) != ERR) {
680 getyx(current_win, y, x);
684 show_help(current_win);
687 box_inside(current_win);
690 current_win = create_my_window(current_win);
693 current_win = create_my_derwin(current_win);
696 if (!move_derwin(current_win)) {
702 fill_window(current_win, (chtype) wgetch(current_win));
705 fill_with_pattern(current_win);
709 if (!move_window(current_win, (ch == 'M'))) {
718 current_win = create_my_subwin(current_win);
721 refresh_all(current_win);
724 current_win = next_window(current_win);
727 current_win = prev_window(current_win);
730 /* want to allow cursor to move around the current window too */
731 /* want to test the resizing of windows and subwindows too */
732 /* want to allow deleting a window also */
735 wmove(current_win, y, x);
736 tail_line("unrecognized key (use '?' for help)");
740 tail_line("size [%d,%d] begin [%d,%d] parent [%d,%d]",
741 getmaxy(current_win),
742 getmaxx(current_win),
743 getbegy(current_win),
744 getbegx(current_win),
745 getpary(current_win),
746 getparx(current_win));
747 wmove(current_win, 0, 0);
750 ExitProgram(EXIT_SUCCESS);