1 /****************************************************************************
2 * Copyright (c) 2006-2008,2010 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.24 2010/11/13 23:34:55 tom Exp $
31 * Demonstrate move functions for windows and derived windows from the curses
34 * Thomas Dickey - 2006/2/11
43 #include <test.priv.h>
50 #ifdef NCURSES_VERSION
51 #define CONST_FMT const
53 #define CONST_FMT /* nothing */
59 #define LINE_MAX (LINES - 2)
61 #define COL_MAX (COLS - 2)
68 WINDOW *parent; /* need this since WINDOW->_parent is not portable */
69 WINDOW *child; /* the actual value */
72 static void head_line(CONST_FMT char *fmt,...) GCC_PRINTFLIKE(1, 2);
73 static void tail_line(CONST_FMT char *fmt,...) GCC_PRINTFLIKE(1, 2);
75 static unsigned num_windows;
76 static FRAME *all_windows;
79 message(int lineno, CONST_FMT char *fmt, va_list argp)
90 vsprintf(buffer, fmt, argp);
94 vwprintw(stdscr, fmt, argp);
102 head_line(CONST_FMT char *fmt,...)
107 message(0, fmt, argp);
112 tail_line(CONST_FMT char *fmt,...)
117 message(LINES - 1, fmt, argp);
122 * Arrow keys move cursor, return location at current on non-arrow key.
125 selectcell(WINDOW *parent, int uli, int ulj, int lri, int lrj)
127 static PAIR res; /* result cell */
128 int si = lri - uli + 1; /* depth of the select area */
129 int sj = lrj - ulj + 1; /* width of the select area */
130 int i = 0, j = 0; /* offsets into the select area */
135 tail_line("Upper left [%2d,%2d] Lower right [%2d,%2d] -> %d,%d",
139 wmove(parent, uli + i, ulj + j);
141 switch (wgetch(parent)) {
157 #ifdef NCURSES_MOUSE_VERSION
163 if (event.y > uli && event.x > ulj) {
184 * Ask user for a window definition.
187 getwindow(WINDOW *parent, PAIR * ul, PAIR * lr)
189 int min_col = (parent == stdscr) ? COL_MIN : 0;
190 int max_col = (parent == stdscr) ? COL_MAX : getmaxx(parent);
191 int min_line = (parent == stdscr) ? LINE_MIN : 0;
192 int max_line = (parent == stdscr) ? LINE_MAX : getmaxy(parent);
196 head_line("Use arrows to move cursor, anything else to mark corner 1");
197 if ((tmp = selectcell(parent, min_line, min_col, max_line, max_col)) != 0) {
199 MvWAddCh(parent, ul->y, ul->x, '*');
201 head_line("Use arrows to move cursor, anything else to mark corner 2");
202 if ((tmp = selectcell(parent, ul->y, ul->x, max_line, max_col)) != 0) {
204 MvWAddCh(parent, lr->y, lr->x, '*');
205 wmove(parent, lr->y, lr->x);
208 result = (lr->y != ul->y && lr->x != ul->x);
216 * Draw a box inside the given window.
219 box_inside(WINDOW *win)
225 getmaxyx(win, y1, x1);
227 MvWHLine(win, 0, 0, ACS_HLINE, x1);
228 MvWHLine(win, y1 - 1, 0, ACS_HLINE, x1);
230 MvWVLine(win, 0, 0, ACS_VLINE, y1);
231 MvWVLine(win, 0, x1 - 1, ACS_VLINE, y1);
233 MvWAddCh(win, 0, 0, ACS_ULCORNER);
234 MvWAddCh(win, y1 - 1, 0, ACS_LLCORNER);
235 MvWAddCh(win, 0, x1 - 1, ACS_URCORNER);
236 MvWAddCh(win, y1 - 1, x1 - 1, ACS_LRCORNER);
244 * Add a window to our list.
247 add_window(WINDOW *parent, WINDOW *child)
249 static unsigned have = 0;
250 unsigned need = ((num_windows + 1) | 31) + 1;
254 all_windows = typeRealloc(FRAME, need, all_windows);
256 all_windows[num_windows].parent = parent;
257 all_windows[num_windows].child = child;
262 window2num(WINDOW *win)
266 for (n = 0; n < (int) num_windows; ++n) {
267 if (win == all_windows[n].child) {
276 parent_of(WINDOW *win)
279 int n = window2num(win);
281 result = all_windows[n].parent;
286 repaint_one(WINDOW *win)
293 refresh_all(WINDOW *win)
297 for (n = 0; n < num_windows; ++n) {
298 if (all_windows[n].child != win) {
299 repaint_one(all_windows[n].child);
308 next_window(WINDOW *win)
310 WINDOW *result = win;
311 int n = window2num(win);
314 result = all_windows[(unsigned) n % num_windows].child;
322 prev_window(WINDOW *win)
324 WINDOW *result = win;
325 int n = window2num(win);
329 n = (int) (num_windows - 1);
330 result = all_windows[(unsigned) n % num_windows].child;
338 recur_move_window(WINDOW *parent, int dy, int dx)
342 for (n = 0; n < num_windows; ++n) {
343 if (all_windows[n].parent == parent) {
346 getbegyx(all_windows[n].child, y0, x0);
347 mvwin(all_windows[n].child, y0 + dy, x0 + dx);
348 recur_move_window(all_windows[n].child, dy, dx);
357 move_window(WINDOW *win, bool recur)
359 WINDOW *parent = parent_of(win);
363 bool top = (parent == stdscr);
364 int min_col = top ? COL_MIN : 0;
365 int max_col = top ? COL_MAX : getmaxx(parent);
366 int min_line = top ? LINE_MIN : 0;
367 int max_line = top ? LINE_MAX : getmaxy(parent);
370 head_line("Select new position for %swindow", top ? "" : "sub");
372 if ((tmp = selectcell(parent,
374 max_line, max_col)) != 0) {
376 getbegyx(parent, y0, x0);
378 * Note: Moving a subwindow has the effect of moving a viewport
379 * around the screen. The parent window retains the contents of
380 * the subwindow in the original location, but the viewport will
381 * show the contents (again) at the new location. So it will look
384 if (mvwin(win, y0 + tmp->y, x0 + tmp->x) != ERR) {
386 recur_move_window(win, tmp->y, tmp->x);
401 move_subwin(WINDOW *win)
403 WINDOW *parent = parent_of(win);
407 bool top = (parent == stdscr);
409 int min_col = top ? COL_MIN : 0;
410 int max_col = top ? COL_MAX : getmaxx(parent);
411 int min_line = top ? LINE_MIN : 0;
412 int max_line = top ? LINE_MAX : getmaxy(parent);
415 head_line("Select new position for subwindow");
417 if ((tmp = selectcell(parent,
419 max_line, max_col)) != 0) {
421 getbegyx(parent, y0, x0);
422 if (mvderwin(win, y0 + tmp->y, x0 + tmp->x) != ERR) {
434 fill_window(WINDOW *win, chtype ch)
441 getmaxyx(win, y1, x1);
442 for (y = 0; y < y1; ++y) {
443 for (x = 0; x < x1; ++x) {
444 MvWAddCh(win, y, x, ch);
452 #define lines_of(ul,lr) (lr.y - ul.y + 1)
453 #define cols_of(ul,lr) (lr.x - ul.x + 1)
454 #define pair_of(ul) ul.y, ul.x
457 create_my_window(WINDOW *current)
462 if (getwindow(stdscr, &ul, &lr)) {
463 result = newwin(lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
465 fill_window(result, 'c');
466 add_window(stdscr, result);
475 create_my_derwin(WINDOW *parent)
480 if (getwindow(parent, &ul, &lr)) {
481 result = derwin(parent, lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
483 fill_window(result, 'd');
484 add_window(parent, result);
493 create_my_subwin(WINDOW *parent)
498 if (getwindow(parent, &ul, &lr)) {
499 result = subwin(parent,
502 ul.y + getbegy(parent),
503 ul.x + getbegx(parent));
505 fill_window(result, 's');
506 add_window(parent, result);
515 show_help(WINDOW *current)
520 CONST_FMT char * msg;
522 { '?', "Show this screen" },
523 { 'b', "Draw a box inside the current window" },
524 { 'c', "Create a new window" },
525 { 'd', "Create a new derived window" },
526 { 'f', "Fill the current window with the next character" },
527 { 'm', "Move the current window" },
528 { 'M', "Move the current window (and its children)" },
530 { 's', "Create a new subwindow" },
531 { 't', "Move the current subwindow (moves content)" },
532 { CTRL('L'), "Repaint all windows, doing current one last" },
533 { CTRL('N'), "Cursor to next window" },
534 { CTRL('P'), "Cursor to previous window" },
538 WINDOW *mywin = newwin(LINES, COLS, 0, 0);
541 for (row = 0; row < LINES - 2 && row < (int) SIZEOF(help); ++row) {
542 wmove(mywin, row + 1, 1);
543 wprintw(mywin, "%s", keyname(help[row].key));
544 wmove(mywin, row + 1, 20);
545 wprintw(mywin, "%s", help[row].msg);
551 refresh_all(current);
555 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
565 intrflush(stdscr, FALSE);
567 add_window(0, current_win = stdscr);
569 #ifdef NCURSES_MOUSE_VERSION
570 (void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL);
571 #endif /* NCURSES_MOUSE_VERSION */
573 while (!done && (ch = wgetch(current_win)) != ERR) {
576 show_help(current_win);
579 box_inside(current_win);
582 current_win = create_my_window(current_win);
585 current_win = create_my_derwin(current_win);
588 fill_window(current_win, (chtype) wgetch(current_win));
592 if (!move_window(current_win, (ch == 'M'))) {
601 current_win = create_my_subwin(current_win);
604 if (!move_subwin(current_win)) {
610 refresh_all(current_win);
613 current_win = next_window(current_win);
616 current_win = prev_window(current_win);
619 /* want to allow cursor to move around the current window too */
620 /* want to test the resizing of windows and subwindows too */
621 /* want to allow deleting a window also */
624 tail_line("unrecognized key (use '?' for help)");
628 tail_line("size [%d,%d] begin [%d,%d] parent [%d,%d]",
629 getmaxy(current_win),
630 getmaxx(current_win),
631 getbegy(current_win),
632 getbegx(current_win),
633 getpary(current_win),
634 getparx(current_win));
635 wmove(current_win, 0, 0);
638 ExitProgram(EXIT_SUCCESS);