+ if (win != stdscr)
+ transient(curp, (char *) 0);
+ getyx(win, y, x);
+ move(LINES - 1, COLS - 17);
+ printw("Y = %2d X = %2d", y, x);
+ if (win != stdscr)
+ refresh();
+ else
+ wmove(win, y, x);
+}
+
+static pair *
+selectcell(int uli, int ulj, int lri, int lrj)
+/* arrows keys move cursor, return location at current on non-arrow key */
+{
+ static pair res; /* result cell */
+ int si = lri - uli + 1; /* depth of the select area */
+ int sj = lrj - ulj + 1; /* width of the select area */
+ int i = 0, j = 0; /* offsets into the select area */
+
+ res.y = uli;
+ res.x = ulj;
+ for (;;) {
+ move(uli + i, ulj + j);
+ newwin_report((FRAME *) 0);
+
+ switch (Getchar()) {
+ case KEY_UP:
+ i += si - 1;
+ break;
+ case KEY_DOWN:
+ i++;
+ break;
+ case KEY_LEFT:
+ j += sj - 1;
+ break;
+ case KEY_RIGHT:
+ j++;
+ break;
+ case case_QUIT:
+ return ((pair *) 0);
+#ifdef NCURSES_MOUSE_VERSION
+ case KEY_MOUSE:
+ {
+ MEVENT event;
+
+ getmouse(&event);
+ if (event.y > uli && event.x > ulj) {
+ i = event.y - uli;
+ j = event.x - ulj;
+ } else {
+ beep();
+ break;
+ }
+ }
+#endif
+ /* FALLTHRU */
+ default:
+ res.y = uli + i;
+ res.x = ulj + j;
+ return (&res);
+ }
+ i %= si;
+ j %= sj;
+ }
+}
+
+static void
+outerbox(pair ul, pair lr, bool onoff)
+/* draw or erase a box *outside* the given pair of corners */
+{
+ MvAddCh(ul.y - 1, lr.x - 1, onoff ? ACS_ULCORNER : ' ');
+ MvAddCh(ul.y - 1, lr.x + 1, onoff ? ACS_URCORNER : ' ');
+ MvAddCh(lr.y + 1, lr.x + 1, onoff ? ACS_LRCORNER : ' ');
+ MvAddCh(lr.y + 1, ul.x - 1, onoff ? ACS_LLCORNER : ' ');
+ move(ul.y - 1, ul.x);
+ hline(onoff ? ACS_HLINE : ' ', lr.x - ul.x + 1);
+ move(ul.y, ul.x - 1);
+ vline(onoff ? ACS_VLINE : ' ', lr.y - ul.y + 1);
+ move(lr.y + 1, ul.x);
+ hline(onoff ? ACS_HLINE : ' ', lr.x - ul.x + 1);
+ move(ul.y, lr.x + 1);
+ vline(onoff ? ACS_VLINE : ' ', lr.y - ul.y + 1);
+}
+
+static WINDOW *
+getwindow(void)
+/* Ask user for a window definition */
+{
+ WINDOW *rwindow;
+ pair ul, lr, *tmp;
+
+ move(0, 0);
+ clrtoeol();
+ addstr("Use arrows to move cursor, anything else to mark corner 1");
+ refresh();
+ if ((tmp = selectcell(2, 1, LINES - BOTLINES - 2, COLS - 2)) == (pair *) 0)
+ return ((WINDOW *) 0);
+ memcpy(&ul, tmp, sizeof(pair));
+ MvAddCh(ul.y - 1, ul.x - 1, ACS_ULCORNER);
+ move(0, 0);
+ clrtoeol();
+ addstr("Use arrows to move cursor, anything else to mark corner 2");
+ refresh();
+ if ((tmp = selectcell(ul.y, ul.x, LINES - BOTLINES - 2, COLS - 2)) ==
+ (pair *) 0)
+ return ((WINDOW *) 0);
+ memcpy(&lr, tmp, sizeof(pair));
+
+ rwindow = subwin(stdscr, lr.y - ul.y + 1, lr.x - ul.x + 1, ul.y, ul.x);
+
+ outerbox(ul, lr, TRUE);
+ refresh();
+
+ if (rwindow != 0)
+ wrefresh(rwindow);
+
+ move(0, 0);
+ clrtoeol();
+ return (rwindow);
+}
+
+static void
+newwin_move(FRAME * curp, int dy, int dx)
+{
+ WINDOW *win = frame_win(curp);
+ int cur_y, cur_x;
+ int max_y, max_x;
+
+ getyx(win, cur_y, cur_x);
+ getmaxyx(win, max_y, max_x);
+ if ((cur_x += dx) < 0)
+ cur_x = 0;
+ else if (cur_x >= max_x)
+ cur_x = max_x - 1;
+ if ((cur_y += dy) < 0)
+ cur_y = 0;
+ else if (cur_y >= max_y)
+ cur_y = max_y - 1;
+ wmove(win, cur_y, cur_x);
+}
+
+static FRAME *
+delete_framed(FRAME * fp, bool showit)
+{
+ FRAME *np = 0;
+
+ if (fp != 0) {
+ fp->last->next = fp->next;
+ fp->next->last = fp->last;
+
+ if (showit) {
+ werase(fp->wind);
+ wrefresh(fp->wind);
+ }
+ delwin(fp->wind);
+
+ np = (fp == fp->next) ? NULL : fp->next;
+ free(fp);
+ }
+ return np;
+}
+
+static int
+scroll_test(bool recur GCC_UNUSED)
+/* Demonstrate windows */
+{
+ int c;
+ FRAME *current = (FRAME *) 0, *neww;
+ WINDOW *usescr;
+#if HAVE_PUTWIN && HAVE_GETWIN
+ FILE *fp;
+#endif
+
+#define DUMPFILE "screendump"
+
+#ifdef NCURSES_MOUSE_VERSION
+ mousemask(BUTTON1_CLICKED, (mmask_t *) 0);
+#endif
+ c = CTRL('C');
+ raw();
+ do {
+ transient((FRAME *) 0, (char *) 0);
+ switch (c) {
+ case CTRL('C'):
+ if ((neww = typeCalloc(FRAME, (size_t) 1)) == 0) {
+ failed("scroll_test");
+ goto breakout;
+ }
+ if ((neww->wind = getwindow()) == (WINDOW *) 0) {
+ failed("scroll_test");
+ free(neww);
+ goto breakout;
+ }
+
+ if (current == 0) { /* First element, */
+ neww->next = neww; /* so point it at itself */
+ neww->last = neww;
+ } else {
+ neww->next = current->next;
+ neww->last = current;
+ neww->last->next = neww;
+ neww->next->last = neww;
+ }
+ current = neww;
+ /* SVr4 curses sets the keypad on all newly-created windows to
+ * false. Someone reported that PDCurses makes new windows inherit
+ * this flag. Remove the following 'keypad()' call to test this
+ */
+ keypad(current->wind, TRUE);
+ current->do_keypad = HaveKeypad(current);
+ current->do_scroll = HaveScroll(current);
+ break;
+
+ case CTRL('N'): /* go to next window */
+ if (current)
+ current = current->next;
+ break;
+
+ case CTRL('P'): /* go to previous window */
+ if (current)
+ current = current->last;
+ break;
+
+ case CTRL('F'): /* scroll current window forward */
+ if (current)
+ wscrl(frame_win(current), 1);
+ break;
+
+ case CTRL('B'): /* scroll current window backwards */
+ if (current)
+ wscrl(frame_win(current), -1);
+ break;
+
+ case CTRL('K'): /* toggle keypad mode for current */
+ if (current) {
+ current->do_keypad = !current->do_keypad;
+ keypad(current->wind, current->do_keypad);
+ }
+ break;
+
+ case CTRL('S'):
+ if (current) {
+ current->do_scroll = !current->do_scroll;
+ scrollok(current->wind, current->do_scroll);
+ }
+ break;
+
+#if HAVE_PUTWIN && HAVE_GETWIN
+ case CTRL('W'): /* save and delete window */
+ if ((current != 0) && (current == current->next)) {
+ transient(current, "Will not save/delete ONLY window");
+ break;
+ } else if ((fp = fopen(DUMPFILE, "w")) == (FILE *) 0) {
+ transient(current, "Can't open screen dump file");
+ } else {
+ int rc = putwin(frame_win(current), fp);
+ (void) fclose(fp);
+
+ if (rc == OK) {
+ current = delete_framed(current, TRUE);
+ } else {
+ transient(current, "Can't write screen dump file");
+ }
+ }
+ break;
+
+ case CTRL('R'): /* restore window */
+ if ((fp = fopen(DUMPFILE, "r")) == (FILE *) 0) {
+ transient(current, "Can't open screen dump file");
+ } else {
+ if ((neww = typeCalloc(FRAME, (size_t) 1)) != 0) {
+
+ neww->next = current ? current->next : 0;
+ neww->last = current;
+ if (neww->last != 0)
+ neww->last->next = neww;
+ if (neww->next != 0)
+ neww->next->last = neww;
+
+ neww->wind = getwin(fp);
+
+ wrefresh(neww->wind);
+ } else {
+ failed("scroll_test");
+ }
+ (void) fclose(fp);
+ }
+ break;
+#endif
+
+#if HAVE_WRESIZE
+ case CTRL('X'): /* resize window */
+ if (current) {
+ pair *tmp, ul, lr;
+ int i, mx, my;
+
+ move(0, 0);
+ clrtoeol();
+ addstr("Use arrows to move cursor, anything else to mark new corner");
+ refresh();
+
+ getbegyx(current->wind, ul.y, ul.x);
+
+ tmp = selectcell(ul.y, ul.x, LINES - BOTLINES - 2, COLS - 2);
+ if (tmp == (pair *) 0) {
+ beep();
+ break;
+ }
+
+ getmaxyx(current->wind, lr.y, lr.x);
+ lr.y += (ul.y - 1);
+ lr.x += (ul.x - 1);
+ outerbox(ul, lr, FALSE);
+ wnoutrefresh(stdscr);
+
+ /* strictly cosmetic hack for the test */
+ getmaxyx(current->wind, my, mx);
+ if (my > tmp->y - ul.y) {
+ getyx(current->wind, lr.y, lr.x);
+ wmove(current->wind, tmp->y - ul.y + 1, 0);
+ wclrtobot(current->wind);
+ wmove(current->wind, lr.y, lr.x);
+ }
+ if (mx > tmp->x - ul.x)
+ for (i = 0; i < my; i++) {
+ wmove(current->wind, i, tmp->x - ul.x + 1);
+ wclrtoeol(current->wind);
+ }
+ wnoutrefresh(current->wind);
+
+ memcpy(&lr, tmp, sizeof(pair));
+ (void) wresize(current->wind, lr.y - ul.y + 0, lr.x - ul.x + 0);
+
+ getbegyx(current->wind, ul.y, ul.x);
+ getmaxyx(current->wind, lr.y, lr.x);
+ lr.y += (ul.y - 1);
+ lr.x += (ul.x - 1);
+ outerbox(ul, lr, TRUE);
+ wnoutrefresh(stdscr);
+
+ wnoutrefresh(current->wind);
+ move(0, 0);
+ clrtoeol();
+ doupdate();
+ }
+ break;
+#endif /* HAVE_WRESIZE */
+
+ case KEY_UP:
+ newwin_move(current, -1, 0);
+ break;
+ case KEY_DOWN:
+ newwin_move(current, 1, 0);
+ break;
+ case KEY_LEFT:
+ newwin_move(current, 0, -1);
+ break;
+ case KEY_RIGHT:
+ newwin_move(current, 0, 1);
+ break;
+
+ case KEY_BACKSPACE:
+ /* FALLTHROUGH */
+ case KEY_DC:
+ {
+ int y, x;
+ getyx(frame_win(current), y, x);
+ if (--x < 0) {
+ if (--y < 0)
+ break;
+ x = getmaxx(frame_win(current)) - 1;
+ }
+ (void) mvwdelch(frame_win(current), y, x);
+ }
+ break;
+
+ case '\r':
+ c = '\n';
+ /* FALLTHROUGH */
+
+ default:
+ if (current)
+ waddch(current->wind, (chtype) c);
+ else
+ beep();
+ break;
+ }
+ newwin_report(current);
+ usescr = frame_win(current);
+ wrefresh(usescr);
+ } while
+ (!isQuit(c = wGetchar(usescr), TRUE)
+ && (c != ERR));
+
+ breakout:
+ while (current != 0)
+ current = delete_framed(current, FALSE);
+
+ scrollok(stdscr, TRUE); /* reset to driver's default */
+#ifdef NCURSES_MOUSE_VERSION
+ mousemask(0, (mmask_t *) 0);
+#endif
+ noraw();
+ erase();
+ endwin();
+ return OK;
+}
+
+/****************************************************************************
+ *
+ * Panels tester
+ *
+ ****************************************************************************/
+
+#if USE_LIBPANEL
+static int nap_msec = 1;
+
+static NCURSES_CONST char *mod[] =
+{
+ "test ",
+ "TEST ",
+ "(**) ",
+ "*()* ",
+ "<--> ",
+ "LAST "
+};
+
+/*+-------------------------------------------------------------------------
+ wait_a_while(msec)
+--------------------------------------------------------------------------*/
+static void
+wait_a_while(int msec GCC_UNUSED)
+{
+#if HAVE_NAPMS
+ if (nap_msec == 1)
+ wGetchar(stdscr);
+ else
+ napms(nap_msec);
+#else
+ if (nap_msec == 1)
+ wGetchar(stdscr);
+ else if (msec > 1000)
+ sleep((unsigned) msec / 1000);
+ else
+ sleep(1);
+#endif
+} /* end of wait_a_while */
+
+/*+-------------------------------------------------------------------------
+ saywhat(text)
+--------------------------------------------------------------------------*/
+static void
+saywhat(NCURSES_CONST char *text)
+{
+ wmove(stdscr, LINES - 1, 0);
+ wclrtoeol(stdscr);
+ if (text != 0 && *text != '\0') {
+ waddstr(stdscr, text);
+ waddstr(stdscr, "; ");
+ }
+ waddstr(stdscr, "press any key to continue");
+} /* end of saywhat */
+
+/*+-------------------------------------------------------------------------
+ mkpanel(rows,cols,tly,tlx) - alloc a win and panel and associate them
+--------------------------------------------------------------------------*/
+static PANEL *
+mkpanel(NCURSES_COLOR_T color, int rows, int cols, int tly, int tlx)
+{
+ WINDOW *win;
+ PANEL *pan = 0;
+
+ if ((win = newwin(rows, cols, tly, tlx)) != 0) {
+ if ((pan = new_panel(win)) == 0) {
+ delwin(win);
+ } else if (UseColors) {
+ NCURSES_COLOR_T fg = (NCURSES_COLOR_T) ((color == COLOR_BLUE)
+ ? COLOR_WHITE
+ : COLOR_BLACK);
+ NCURSES_COLOR_T bg = color;
+
+ init_pair(color, fg, bg);
+ wbkgdset(win, (attr_t) (COLOR_PAIR(color) | ' '));
+ } else {
+ wbkgdset(win, A_BOLD | ' ');
+ }
+ }
+ return pan;
+} /* end of mkpanel */
+
+/*+-------------------------------------------------------------------------
+ rmpanel(pan)
+--------------------------------------------------------------------------*/
+static void
+rmpanel(PANEL *pan)
+{
+ WINDOW *win = panel_window(pan);
+ del_panel(pan);
+ delwin(win);
+} /* end of rmpanel */
+
+/*+-------------------------------------------------------------------------
+ pflush()
+--------------------------------------------------------------------------*/
+static void
+pflush(void)
+{
+ update_panels();
+ doupdate();
+} /* end of pflush */
+
+/*+-------------------------------------------------------------------------
+ fill_panel(win)
+--------------------------------------------------------------------------*/
+static void
+init_panel(WINDOW *win)
+{
+ register int y, x;
+
+ for (y = 0; y < LINES - 1; y++) {
+ for (x = 0; x < COLS; x++)
+ wprintw(win, "%d", (y + x) % 10);
+ }
+}
+
+static void
+fill_panel(PANEL *pan)
+{
+ WINDOW *win = panel_window(pan);
+ const char *userptr = (const char *) panel_userptr(pan);
+ int num = (userptr && *userptr) ? userptr[1] : '?';
+ int y, x;
+
+ wmove(win, 1, 1);
+ wprintw(win, "-pan%c-", num);
+ wclrtoeol(win);
+ box(win, 0, 0);
+ for (y = 2; y < getmaxy(win) - 1; y++) {
+ for (x = 1; x < getmaxx(win) - 1; x++) {
+ wmove(win, y, x);
+ waddch(win, UChar(num));
+ }
+ }
+}
+
+#if USE_WIDEC_SUPPORT
+static void
+init_wide_panel(WINDOW *win)
+{
+ int digit;
+ cchar_t temp[10];
+
+ for (digit = 0; digit < 10; ++digit)
+ make_fullwidth_digit(&temp[digit], digit);
+
+ do {
+ int y, x;
+ getyx(stdscr, y, x);
+ digit = (y + x / 2) % 10;
+ } while (wadd_wch(win, &temp[digit]) != ERR);
+}
+
+static void
+fill_wide_panel(PANEL *pan)
+{
+ WINDOW *win = panel_window(pan);
+ const char *userptr = (const char *) panel_userptr(pan);
+ int num = (userptr && *userptr) ? userptr[1] : '?';
+ int y, x;
+
+ wmove(win, 1, 1);
+ wprintw(win, "-pan%c-", num);
+ wclrtoeol(win);
+ box(win, 0, 0);
+ for (y = 2; y < getmaxy(win) - 1; y++) {
+ for (x = 1; x < getmaxx(win) - 1; x++) {
+ wmove(win, y, x);
+ waddch(win, UChar(num));
+ }
+ }
+}
+#endif
+
+#define MAX_PANELS 5
+
+static void
+canned_panel(PANEL *px[MAX_PANELS + 1], NCURSES_CONST char *cmd)
+{
+ int which = cmd[1] - '0';
+
+ saywhat(cmd);
+ switch (*cmd) {
+ case 'h':
+ hide_panel(px[which]);
+ break;
+ case 's':
+ show_panel(px[which]);
+ break;
+ case 't':
+ top_panel(px[which]);
+ break;
+ case 'b':
+ bottom_panel(px[which]);
+ break;
+ case 'd':
+ rmpanel(px[which]);
+ break;
+ }
+ pflush();
+ wait_a_while(nap_msec);
+}
+
+static int
+demo_panels(void (*InitPanel) (WINDOW *), void (*FillPanel) (PANEL *))
+{
+ int count;
+ int itmp;
+ PANEL *px[MAX_PANELS + 1];
+
+ scrollok(stdscr, FALSE); /* we don't want stdscr to scroll! */
+ refresh();
+
+ InitPanel(stdscr);
+ for (count = 0; count < 5; count++) {
+ px[1] = mkpanel(COLOR_RED,
+ LINES / 2 - 2,
+ COLS / 8 + 1,
+ 0,
+ 0);
+ set_panel_userptr(px[1], (NCURSES_CONST void *) "p1");
+
+ px[2] = mkpanel(COLOR_GREEN,
+ LINES / 2 + 1,
+ COLS / 7,
+ LINES / 4,
+ COLS / 10);
+ set_panel_userptr(px[2], (NCURSES_CONST void *) "p2");
+
+ px[3] = mkpanel(COLOR_YELLOW,
+ LINES / 4,
+ COLS / 10,
+ LINES / 2,
+ COLS / 9);
+ set_panel_userptr(px[3], (NCURSES_CONST void *) "p3");
+
+ px[4] = mkpanel(COLOR_BLUE,
+ LINES / 2 - 2,
+ COLS / 8,
+ LINES / 2 - 2,
+ COLS / 3);
+ set_panel_userptr(px[4], (NCURSES_CONST void *) "p4");
+
+ px[5] = mkpanel(COLOR_MAGENTA,
+ LINES / 2 - 2,
+ COLS / 8,
+ LINES / 2,
+ COLS / 2 - 2);
+ set_panel_userptr(px[5], (NCURSES_CONST void *) "p5");
+
+ FillPanel(px[1]);
+ FillPanel(px[2]);
+ FillPanel(px[3]);
+ FillPanel(px[4]);
+ FillPanel(px[5]);
+
+ hide_panel(px[4]);
+ hide_panel(px[5]);
+ pflush();
+ saywhat("");
+ wait_a_while(nap_msec);
+
+ saywhat("h3 s1 s2 s4 s5");
+ move_panel(px[1], 0, 0);
+ hide_panel(px[3]);
+ show_panel(px[1]);
+ show_panel(px[2]);
+ show_panel(px[4]);
+ show_panel(px[5]);
+ pflush();
+ wait_a_while(nap_msec);
+
+ canned_panel(px, "s1");
+ canned_panel(px, "s2");
+
+ saywhat("m2");
+ move_panel(px[2], LINES / 3 + 1, COLS / 8);
+ pflush();
+ wait_a_while(nap_msec);
+
+ canned_panel(px, "s3");
+
+ saywhat("m3");
+ move_panel(px[3], LINES / 4 + 1, COLS / 15);
+ pflush();
+ wait_a_while(nap_msec);
+
+ canned_panel(px, "b3");
+ canned_panel(px, "s4");
+ canned_panel(px, "s5");
+ canned_panel(px, "t3");
+ canned_panel(px, "t1");
+ canned_panel(px, "t2");
+ canned_panel(px, "t3");
+ canned_panel(px, "t4");
+
+ for (itmp = 0; itmp < 6; itmp++) {
+ WINDOW *w4 = panel_window(px[4]);
+ WINDOW *w5 = panel_window(px[5]);
+
+ saywhat("m4");
+ wmove(w4, LINES / 8, 1);
+ waddstr(w4, mod[itmp]);
+ move_panel(px[4], LINES / 6, itmp * (COLS / 8));
+ wmove(w5, LINES / 6, 1);
+ waddstr(w5, mod[itmp]);
+ pflush();
+ wait_a_while(nap_msec);
+
+ saywhat("m5");
+ wmove(w4, LINES / 6, 1);
+ waddstr(w4, mod[itmp]);
+ move_panel(px[5], LINES / 3 - 1, (itmp * 10) + 6);
+ wmove(w5, LINES / 8, 1);
+ waddstr(w5, mod[itmp]);
+ pflush();
+ wait_a_while(nap_msec);
+ }
+
+ saywhat("m4");
+ move_panel(px[4], LINES / 6, itmp * (COLS / 8));
+ pflush();
+ wait_a_while(nap_msec);
+
+ canned_panel(px, "t5");
+ canned_panel(px, "t2");
+ canned_panel(px, "t1");
+ canned_panel(px, "d2");
+ canned_panel(px, "h3");
+ canned_panel(px, "d1");
+ canned_panel(px, "d4");
+ canned_panel(px, "d5");
+ canned_panel(px, "d3");
+
+ wait_a_while(nap_msec);
+ if (nap_msec == 1)
+ break;
+ nap_msec = 100L;
+ }
+
+ erase();
+ endwin();
+ return OK;
+}
+
+#if USE_LIBPANEL
+static int
+panel_test(bool recur GCC_UNUSED)
+{
+ return demo_panels(init_panel, fill_panel);
+}
+#endif
+
+#if USE_WIDEC_SUPPORT && USE_LIBPANEL
+static int
+x_panel_test(bool recur GCC_UNUSED)
+{
+ return demo_panels(init_wide_panel, fill_wide_panel);
+}
+#endif
+#endif /* USE_LIBPANEL */
+
+/****************************************************************************
+ *
+ * Pad tester
+ *
+ ****************************************************************************/
+
+#if HAVE_NEWPAD
+
+/* The behavior of mvhline, mvvline for negative/zero length is unspecified,
+ * though we can rely on negative x/y values to stop the macro.
+ */
+static void
+do_h_line(int y, int x, chtype c, int to)
+{
+ if ((to) > (x))
+ MvHLine(y, x, c, (to) - (x));
+}
+
+static void
+do_v_line(int y, int x, chtype c, int to)
+{
+ if ((to) > (y))
+ MvVLine(y, x, c, (to) - (y));
+}
+
+#define GRIDSIZE 3
+
+static bool pending_pan = FALSE;
+static bool show_panner_legend = TRUE;
+
+static int
+panner_legend(int line)
+{
+ static const char *const legend[] =
+ {
+ "Use arrow keys (or U,D,L,R) to pan, ESC to quit, ! to shell-out.",
+ "Use +,- (or j,k) to grow/shrink the panner vertically.",
+ "Use <,> (or h,l) to grow/shrink the panner horizontally.",
+ "Number repeats. Toggle legend:? filler:a timer:t scrollmark:s."
+ };
+ int n = ((int) SIZEOF(legend) - (LINES - line));
+ if (n >= 0) {
+ if (move(line, 0) != ERR) {
+ if (show_panner_legend)
+ printw("%s", legend[n]);
+ clrtoeol();
+ return show_panner_legend;
+ }
+ }
+ return FALSE;
+}
+
+static void
+panner_h_cleanup(int from_y, int from_x, int to_x)
+{
+ if (!panner_legend(from_y))
+ do_h_line(from_y, from_x, ' ', to_x);
+}
+
+static void
+panner_v_cleanup(int from_y, int from_x, int to_y)
+{
+ if (!panner_legend(from_y))
+ do_v_line(from_y, from_x, ' ', to_y);
+}
+
+static void
+fill_pad(WINDOW *panpad, bool pan_lines, bool colored)
+{
+ int y, x;
+ unsigned gridcount = 0;
+ chtype fill = 0;
+#ifdef A_COLOR
+ if (colored)
+ fill = (chtype) COLOR_PAIR(1);
+#endif
+
+ wmove(panpad, 0, 0);
+ for (y = 0; y < getmaxy(panpad); y++) {
+ for (x = 0; x < getmaxx(panpad); x++) {
+ if (y % GRIDSIZE == 0 && x % GRIDSIZE == 0) {
+ if (y == 0 && x == 0)
+ waddch(panpad, pan_lines ? ACS_ULCORNER : '+');
+ else if (y == 0)
+ waddch(panpad, pan_lines ? ACS_TTEE : '+');
+ else if (y == 0 || x == 0)
+ waddch(panpad, pan_lines ? ACS_LTEE : '+');
+ else
+ waddch(panpad, (chtype) ((pan_lines ? 'a' : 'A') +
+ (int) (gridcount++ % 26)) | fill);
+ } else if (y % GRIDSIZE == 0)
+ waddch(panpad, pan_lines ? ACS_HLINE : '-');
+ else if (x % GRIDSIZE == 0)
+ waddch(panpad, pan_lines ? ACS_VLINE : '|');
+ else
+ waddch(panpad, ' ');
+ }
+ }
+}
+
+static void
+panner(WINDOW *pad,
+ int top_x, int top_y, int porty, int portx,
+ int (*pgetc) (WINDOW *),
+ bool colored)
+{
+#if HAVE_GETTIMEOFDAY
+ struct timeval before, after;
+ bool timing = TRUE;
+#endif
+ bool pan_lines = FALSE;
+ bool scrollers = TRUE;
+ int basex = 0;
+ int basey = 0;
+ int pxmax, pymax, lowend, highend, c;
+
+ getmaxyx(pad, pymax, pxmax);
+ scrollok(stdscr, FALSE); /* we don't want stdscr to scroll! */
+
+ c = KEY_REFRESH;
+ do {
+#ifdef NCURSES_VERSION
+ /*
+ * During shell-out, the user may have resized the window. Adjust
+ * the port size of the pad to accommodate this. Ncurses automatically
+ * resizes all of the normal windows to fit on the new screen.
+ */
+ if (top_x > COLS)
+ top_x = COLS;
+ if (portx > COLS)
+ portx = COLS;
+ if (top_y > LINES)
+ top_y = LINES;
+ if (porty > LINES)
+ porty = LINES;
+#endif
+ switch (c) {
+ case KEY_REFRESH:
+ erase();
+
+ /* FALLTHRU */
+ case HELP_KEY_1:
+ if (c == HELP_KEY_1)
+ show_panner_legend = !show_panner_legend;
+ panner_legend(LINES - 4);
+ panner_legend(LINES - 3);
+ panner_legend(LINES - 2);
+ panner_legend(LINES - 1);
+ break;
+ case 'a':
+ pan_lines = !pan_lines;
+ fill_pad(pad, pan_lines, colored);
+ pending_pan = FALSE;
+ break;
+
+#if HAVE_GETTIMEOFDAY
+ case 't':
+ timing = !timing;
+ if (!timing)
+ panner_legend(LINES - 1);
+ break;
+#endif
+ case 's':
+ scrollers = !scrollers;
+ break;
+
+ /* Move the top-left corner of the pad, keeping the bottom-right
+ * corner fixed.
+ */
+ case 'h': /* increase-columns: move left edge to left */
+ if (top_x <= 0)
+ beep();
+ else {
+ panner_v_cleanup(top_y, top_x, porty);
+ top_x--;
+ }
+ break;
+
+ case 'j': /* decrease-lines: move top-edge down */
+ if (top_y >= porty)
+ beep();
+ else {
+ panner_h_cleanup(top_y - 1, top_x - (top_x > 0), portx);
+ top_y++;
+ }
+ break;
+
+ case 'k': /* increase-lines: move top-edge up */
+ if (top_y <= 0)
+ beep();
+ else {
+ top_y--;
+ panner_h_cleanup(top_y, top_x, portx);
+ }
+ break;
+
+ case 'l': /* decrease-columns: move left-edge to right */
+ if (top_x >= portx)
+ beep();
+ else {
+ panner_v_cleanup(top_y - (top_y > 0), top_x - 1, porty);
+ top_x++;
+ }
+ break;
+
+ /* Move the bottom-right corner of the pad, keeping the top-left
+ * corner fixed.
+ */
+ case KEY_IC: /* increase-columns: move right-edge to right */
+ if (portx >= pxmax || portx >= COLS)
+ beep();
+ else {
+ panner_v_cleanup(top_y - (top_y > 0), portx - 1, porty);
+ ++portx;
+ }
+ break;
+
+ case KEY_IL: /* increase-lines: move bottom-edge down */
+ if (porty >= pymax || porty >= LINES)
+ beep();
+ else {
+ panner_h_cleanup(porty - 1, top_x - (top_x > 0), portx);
+ ++porty;
+ }
+ break;
+
+ case KEY_DC: /* decrease-columns: move bottom edge up */
+ if (portx <= top_x)
+ beep();
+ else {
+ portx--;
+ panner_v_cleanup(top_y - (top_y > 0), portx, porty);
+ }
+ break;
+
+ case KEY_DL: /* decrease-lines */
+ if (porty <= top_y)
+ beep();
+ else {
+ porty--;
+ panner_h_cleanup(porty, top_x - (top_x > 0), portx);
+ }
+ break;
+
+ case KEY_LEFT: /* pan leftwards */
+ if (basex > 0)
+ basex--;
+ else
+ beep();
+ break;
+
+ case KEY_RIGHT: /* pan rightwards */
+ if (basex + portx - (pymax > porty) < pxmax)
+ basex++;
+ else
+ beep();
+ break;
+
+ case KEY_UP: /* pan upwards */
+ if (basey > 0)
+ basey--;
+ else
+ beep();
+ break;
+
+ case KEY_DOWN: /* pan downwards */
+ if (basey + porty - (pxmax > portx) < pymax)
+ basey++;
+ else
+ beep();
+ break;
+
+ case 'H':
+ case KEY_HOME:
+ case KEY_FIND:
+ basey = 0;
+ break;
+
+ case 'E':
+ case KEY_END:
+ case KEY_SELECT:
+ basey = pymax - porty;
+ if (basey < 0)
+ basey = 0;
+ break;
+
+ default:
+ beep();
+ break;
+ }
+
+ MvAddCh(top_y - 1, top_x - 1, ACS_ULCORNER);
+ do_v_line(top_y, top_x - 1, ACS_VLINE, porty);
+ do_h_line(top_y - 1, top_x, ACS_HLINE, portx);
+
+ if (scrollers && (pxmax > portx - 1)) {
+ int length = (portx - top_x - 1);
+ float ratio = ((float) length) / ((float) pxmax);
+
+ lowend = (int) ((float) top_x + ((float) basex * ratio));
+ highend = (int) ((float) top_x + ((float) (basex + length) * ratio));
+
+ do_h_line(porty - 1, top_x, ACS_HLINE, lowend);
+ if (highend < portx) {
+ attron(A_REVERSE);
+ do_h_line(porty - 1, lowend, ' ', highend + 1);
+ attroff(A_REVERSE);
+ do_h_line(porty - 1, highend + 1, ACS_HLINE, portx);
+ }
+ } else
+ do_h_line(porty - 1, top_x, ACS_HLINE, portx);
+
+ if (scrollers && (pymax > porty - 1)) {
+ int length = (porty - top_y - 1);
+ float ratio = ((float) length) / ((float) pymax);
+
+ lowend = (int) ((float) top_y + ((float) basey * ratio));
+ highend = (int) ((float) top_y + ((float) (basey + length) * ratio));
+
+ do_v_line(top_y, portx - 1, ACS_VLINE, lowend);
+ if (highend < porty) {
+ attron(A_REVERSE);
+ do_v_line(lowend, portx - 1, ' ', highend + 1);
+ attroff(A_REVERSE);
+ do_v_line(highend + 1, portx - 1, ACS_VLINE, porty);
+ }
+ } else
+ do_v_line(top_y, portx - 1, ACS_VLINE, porty);
+
+ MvAddCh(top_y - 1, portx - 1, ACS_URCORNER);
+ MvAddCh(porty - 1, top_x - 1, ACS_LLCORNER);
+ MvAddCh(porty - 1, portx - 1, ACS_LRCORNER);
+
+ if (!pending_pan) {
+#if HAVE_GETTIMEOFDAY
+ gettimeofday(&before, 0);
+#endif
+ wnoutrefresh(stdscr);
+
+ pnoutrefresh(pad,
+ basey, basex,
+ top_y, top_x,
+ porty - (pxmax > portx) - 1,
+ portx - (pymax > porty) - 1);
+
+ doupdate();
+#if HAVE_GETTIMEOFDAY
+#define TIMEVAL2S(data) ((double) data.tv_sec + ((double) data.tv_usec / 1.0e6))
+ if (timing) {
+ double elapsed;
+ gettimeofday(&after, 0);
+ elapsed = (TIMEVAL2S(after) - TIMEVAL2S(before));
+ move(LINES - 1, COLS - 12);
+ printw("Secs: %2.03f", elapsed);
+ refresh();
+ }
+#endif
+ }
+
+ } while
+ ((c = pgetc(pad)) != KEY_EXIT);
+
+ scrollok(stdscr, TRUE); /* reset to driver's default */
+}
+
+static int
+padgetch(WINDOW *win)
+{
+ static int count;
+ static int last;
+ int c;
+
+ if ((pending_pan = (count > 0)) != FALSE) {
+ count--;
+ pending_pan = (count != 0);
+ } else {
+ for (;;) {
+ switch (c = wGetchar(win)) {
+ case '!':