1 /****************************************************************************
2 * Copyright (c) 2006,2007 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.20 2007/01/06 23:28:53 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>
53 #define LINE_MAX (LINES - 2)
55 #define COL_MAX (COLS - 2)
62 WINDOW *parent; /* need this since WINDOW->_parent is not portable */
63 WINDOW *child; /* the actual value */
66 static void head_line(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
67 static void tail_line(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
69 static unsigned num_windows;
70 static FRAME *all_windows;
73 message(int lineno, const char *fmt, va_list argp)
84 vsprintf(buffer, fmt, argp);
88 vwprintw(stdscr, fmt, argp);
96 head_line(const char *fmt,...)
101 message(0, fmt, argp);
106 tail_line(const char *fmt,...)
111 message(LINES - 1, fmt, argp);
116 * Arrow keys move cursor, return location at current on non-arrow key.
119 selectcell(WINDOW *parent, int uli, int ulj, int lri, int lrj)
121 static PAIR res; /* result cell */
122 int si = lri - uli + 1; /* depth of the select area */
123 int sj = lrj - ulj + 1; /* width of the select area */
124 int i = 0, j = 0; /* offsets into the select area */
129 tail_line("Upper left [%2d,%2d] Lower right [%2d,%2d] -> %d,%d",
133 wmove(parent, uli + i, ulj + j);
135 switch (wgetch(parent)) {
151 #ifdef NCURSES_MOUSE_VERSION
157 if (event.y > uli && event.x > ulj) {
178 * Ask user for a window definition.
181 getwindow(WINDOW *parent, PAIR * ul, PAIR * lr)
183 int min_col = (parent == stdscr) ? COL_MIN : 0;
184 int max_col = (parent == stdscr) ? COL_MAX : getmaxx(parent);
185 int min_line = (parent == stdscr) ? LINE_MIN : 0;
186 int max_line = (parent == stdscr) ? LINE_MAX : getmaxy(parent);
190 head_line("Use arrows to move cursor, anything else to mark corner 1");
191 if ((tmp = selectcell(parent, min_line, min_col, max_line, max_col)) != 0) {
193 mvwaddch(parent, ul->y, ul->x, '*');
195 head_line("Use arrows to move cursor, anything else to mark corner 2");
196 if ((tmp = selectcell(parent, ul->y, ul->x, max_line, max_col)) != 0) {
198 mvwaddch(parent, lr->y, lr->x, '*');
199 wmove(parent, lr->y, lr->x);
202 result = (lr->y != ul->y && lr->x != ul->x);
210 * Draw a box inside the given window.
213 box_inside(WINDOW *win)
219 getmaxyx(win, y1, x1);
221 mvwhline(win, 0, 0, ACS_HLINE, x1);
222 mvwhline(win, y1 - 1, 0, ACS_HLINE, x1);
224 mvwvline(win, 0, 0, ACS_VLINE, y1);
225 mvwvline(win, 0, x1 - 1, ACS_VLINE, y1);
227 mvwaddch(win, 0, 0, ACS_ULCORNER);
228 mvwaddch(win, y1 - 1, 0, ACS_LLCORNER);
229 mvwaddch(win, 0, x1 - 1, ACS_URCORNER);
230 mvwaddch(win, y1 - 1, x1 - 1, ACS_LRCORNER);
238 * Add a window to our list.
241 add_window(WINDOW *parent, WINDOW *child)
243 static unsigned have = 0;
244 unsigned need = ((num_windows + 1) | 31) + 1;
248 all_windows = (FRAME *) realloc(all_windows, need * sizeof(FRAME));
250 all_windows[num_windows].parent = parent;
251 all_windows[num_windows].child = child;
256 window2num(WINDOW *win)
260 for (n = 0; n < (int) num_windows; ++n) {
261 if (win == all_windows[n].child) {
270 parent_of(WINDOW *win)
273 int n = window2num(win);
275 result = all_windows[n].parent;
280 repaint_one(WINDOW *win)
287 refresh_all(WINDOW *win)
291 for (n = 0; n < num_windows; ++n) {
292 if (all_windows[n].child != win) {
293 repaint_one(all_windows[n].child);
302 next_window(WINDOW *win)
304 WINDOW *result = win;
305 int n = window2num(win);
308 result = all_windows[n % num_windows].child;
316 prev_window(WINDOW *win)
318 WINDOW *result = win;
319 int n = window2num(win);
324 result = all_windows[n % num_windows].child;
332 recur_move_window(WINDOW *parent, int dy, int dx)
336 for (n = 0; n < num_windows; ++n) {
337 if (all_windows[n].parent == parent) {
340 getbegyx(all_windows[n].child, y0, x0);
341 mvwin(all_windows[n].child, y0 + dy, x0 + dx);
342 recur_move_window(all_windows[n].child, dy, dx);
351 move_window(WINDOW *win, bool recur)
353 WINDOW *parent = parent_of(win);
357 bool top = (parent == stdscr);
358 int min_col = top ? COL_MIN : 0;
359 int max_col = top ? COL_MAX : getmaxx(parent);
360 int min_line = top ? LINE_MIN : 0;
361 int max_line = top ? LINE_MAX : getmaxy(parent);
364 head_line("Select new position for %swindow", top ? "" : "sub");
366 if ((tmp = selectcell(parent,
368 max_line, max_col)) != 0) {
370 getbegyx(parent, y0, x0);
372 * Note: Moving a subwindow has the effect of moving a viewport
373 * around the screen. The parent window retains the contents of
374 * the subwindow in the original location, but the viewport will
375 * show the contents (again) at the new location. So it will look
378 if (mvwin(win, y0 + tmp->y, x0 + tmp->x) != ERR) {
380 recur_move_window(win, tmp->y, tmp->x);
395 move_subwin(WINDOW *win)
397 WINDOW *parent = parent_of(win);
401 bool top = (parent == stdscr);
403 int min_col = top ? COL_MIN : 0;
404 int max_col = top ? COL_MAX : getmaxx(parent);
405 int min_line = top ? LINE_MIN : 0;
406 int max_line = top ? LINE_MAX : getmaxy(parent);
409 head_line("Select new position for subwindow");
411 if ((tmp = selectcell(parent,
413 max_line, max_col)) != 0) {
415 getbegyx(parent, y0, x0);
416 if (mvderwin(win, y0 + tmp->y, x0 + tmp->x) != ERR) {
428 fill_window(WINDOW *win, chtype ch)
435 getmaxyx(win, y1, x1);
436 for (y = 0; y < y1; ++y) {
437 for (x = 0; x < x1; ++x) {
438 mvwaddch(win, y, x, ch);
446 #define lines_of(ul,lr) (lr.y - ul.y + 1)
447 #define cols_of(ul,lr) (lr.x - ul.x + 1)
448 #define pair_of(ul) ul.y, ul.x
451 create_my_window(WINDOW *current)
456 if (getwindow(stdscr, &ul, &lr)) {
457 result = newwin(lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
459 fill_window(result, 'c');
460 add_window(stdscr, result);
469 create_my_derwin(WINDOW *parent)
474 if (getwindow(parent, &ul, &lr)) {
475 result = derwin(parent, lines_of(ul, lr), cols_of(ul, lr), pair_of(ul));
477 fill_window(result, 'd');
478 add_window(parent, result);
487 create_my_subwin(WINDOW *parent)
492 if (getwindow(parent, &ul, &lr)) {
493 result = subwin(parent,
496 ul.y + getbegy(parent),
497 ul.x + getbegx(parent));
499 fill_window(result, 's');
500 add_window(parent, result);
509 show_help(WINDOW *current)
516 { '?', "Show this screen" },
517 { 'b', "Draw a box inside the current window" },
518 { 'c', "Create a new window" },
519 { 'd', "Create a new derived window" },
520 { 'f', "Fill the current window with the next character" },
521 { 'm', "Move the current window" },
522 { 'M', "Move the current window (and its children)" },
524 { 's', "Create a new subwindow" },
525 { 't', "Move the current subwindow (moves content)" },
526 { CTRL('L'), "Repaint all windows, doing current one last" },
527 { CTRL('N'), "Cursor to next window" },
528 { CTRL('P'), "Cursor to previous window" },
532 WINDOW *mywin = newwin(LINES, COLS, 0, 0);
535 for (row = 0; row < LINES - 2 && row < (int) SIZEOF(help); ++row) {
536 wmove(mywin, row + 1, 1);
537 wprintw(mywin, "%s", keyname(help[row].key));
538 wmove(mywin, row + 1, 20);
539 wprintw(mywin, "%s", help[row].msg);
545 refresh_all(current);
549 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
559 intrflush(stdscr, FALSE);
561 add_window(0, current_win = stdscr);
563 #ifdef NCURSES_MOUSE_VERSION
564 (void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL);
565 #endif /* NCURSES_MOUSE_VERSION */
567 while (!done && (ch = wgetch(current_win)) != ERR) {
570 show_help(current_win);
573 box_inside(current_win);
576 current_win = create_my_window(current_win);
579 current_win = create_my_derwin(current_win);
582 fill_window(current_win, (chtype) wgetch(current_win));
586 if (!move_window(current_win, (ch == 'M'))) {
595 current_win = create_my_subwin(current_win);
598 if (!move_subwin(current_win)) {
604 refresh_all(current_win);
607 current_win = next_window(current_win);
610 current_win = prev_window(current_win);
613 /* want to allow cursor to move around the current window too */
614 /* want to test the resizing of windows and subwindows too */
615 /* want to allow deleting a window also */
618 tail_line("unrecognized key (use '?' for help)");
622 tail_line("size [%d,%d] begin [%d,%d] parent [%d,%d]",
623 getmaxy(current_win),
624 getmaxx(current_win),
625 getbegy(current_win),
626 getbegx(current_win),
627 getpary(current_win),
628 getparx(current_win));
629 wmove(current_win, 0, 0);
632 ExitProgram(EXIT_SUCCESS);