Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | * Copyright 2018-2022,2023 Thomas E. Dickey * |
| 3 | * Copyright 2006-2013,2017 Free Software Foundation, Inc. * |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4 | * * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a * |
| 6 | * copy of this software and associated documentation files (the * |
| 7 | * "Software"), to deal in the Software without restriction, including * |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, * |
| 9 | * distribute, distribute with modifications, sublicense, and/or sell * |
| 10 | * copies of the Software, and to permit persons to whom the Software is * |
| 11 | * furnished to do so, subject to the following conditions: * |
| 12 | * * |
| 13 | * The above copyright notice and this permission notice shall be included * |
| 14 | * in all copies or substantial portions of the Software. * |
| 15 | * * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * |
| 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * |
| 19 | * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * |
| 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * |
| 21 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * |
| 22 | * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * |
| 23 | * * |
| 24 | * Except as contained in this notice, the name(s) of the above copyright * |
| 25 | * holders shall not be used in advertising or otherwise to promote the * |
| 26 | * sale, use or other dealings in this Software without prior written * |
| 27 | * authorization. * |
| 28 | ****************************************************************************/ |
| 29 | /* |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 30 | * $Id: movewindow.c,v 1.54 2023/05/27 20:13:10 tom Exp $ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 31 | * |
| 32 | * Demonstrate move functions for windows and derived windows from the curses |
| 33 | * library. |
| 34 | * |
| 35 | * Author: Thomas E. Dickey |
| 36 | */ |
| 37 | /* |
| 38 | derwin |
| 39 | mvderwin |
| 40 | subwin |
| 41 | mvwin |
| 42 | |
| 43 | TODO: |
| 44 | add command to reset subwindow's origin to coincide with parent. |
| 45 | add command to delete subwindow (check if it has subwindows though) |
| 46 | */ |
| 47 | |
| 48 | #include <test.priv.h> |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 49 | |
| 50 | #if HAVE_MVDERWIN && HAVE_MVWIN |
| 51 | |
| 52 | #include <popup_msg.h> |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 53 | |
| 54 | #ifdef HAVE_XCURSES |
| 55 | #undef derwin |
| 56 | #endif |
| 57 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 58 | #undef LINE_MAX |
| 59 | |
| 60 | #define LINE_MIN 2 |
| 61 | #define LINE_MAX (LINES - 2) |
| 62 | #define COL_MIN 2 |
| 63 | #define COL_MAX (COLS - 2) |
| 64 | |
| 65 | typedef struct { |
| 66 | int y, x; |
| 67 | } PAIR; |
| 68 | |
| 69 | typedef struct { |
| 70 | WINDOW *parent; /* need this since WINDOW->_parent is not portable */ |
| 71 | WINDOW *child; /* the actual value */ |
| 72 | } FRAME; |
| 73 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 74 | static void head_line(CONST_FMT char *fmt, ...) GCC_PRINTFLIKE(1, 2); |
| 75 | static void tail_line(CONST_FMT char *fmt, ...) GCC_PRINTFLIKE(1, 2); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 76 | |
| 77 | static unsigned num_windows; |
| 78 | static FRAME *all_windows; |
| 79 | |
| 80 | static void |
| 81 | failed(const char *s) |
| 82 | { |
| 83 | perror(s); |
| 84 | endwin(); |
| 85 | ExitProgram(EXIT_FAILURE); |
| 86 | } |
| 87 | |
| 88 | static void |
| 89 | message(int lineno, CONST_FMT char *fmt, va_list argp) |
| 90 | { |
| 91 | int y, x; |
| 92 | |
| 93 | getyx(stdscr, y, x); |
| 94 | move(lineno, 0); |
| 95 | clrtoeol(); |
| 96 | |
| 97 | #ifdef HAVE_XCURSES |
| 98 | { |
| 99 | char buffer[1024]; |
| 100 | vsprintf(buffer, fmt, argp); |
| 101 | addstr(buffer); |
| 102 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 103 | #elif defined(HAVE_VW_PRINTW) |
| 104 | vw_printw(stdscr, fmt, argp); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 105 | #else |
| 106 | vwprintw(stdscr, fmt, argp); |
| 107 | #endif |
| 108 | |
| 109 | move(y, x); |
| 110 | refresh(); |
| 111 | } |
| 112 | |
| 113 | static void |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 114 | head_line(CONST_FMT char *fmt, ...) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 115 | { |
| 116 | va_list argp; |
| 117 | |
| 118 | va_start(argp, fmt); |
| 119 | message(0, fmt, argp); |
| 120 | va_end(argp); |
| 121 | } |
| 122 | |
| 123 | static void |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 124 | tail_line(CONST_FMT char *fmt, ...) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 125 | { |
| 126 | va_list argp; |
| 127 | |
| 128 | va_start(argp, fmt); |
| 129 | message(LINES - 1, fmt, argp); |
| 130 | va_end(argp); |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Arrow keys move cursor, return location at current on non-arrow key. |
| 135 | */ |
| 136 | static PAIR * |
| 137 | selectcell(WINDOW *parent, |
| 138 | WINDOW *child, |
| 139 | int uli, int ulj, |
| 140 | int lri, int lrj, |
| 141 | bool relative, |
| 142 | bool * more) |
| 143 | { |
| 144 | static PAIR res; /* result cell */ |
| 145 | int si = lri - uli + 1; /* depth of the select area */ |
| 146 | int sj = lrj - ulj + 1; /* width of the select area */ |
| 147 | int i = 0, j = 0; /* offsets into the select area */ |
| 148 | |
| 149 | res.y = uli; |
| 150 | res.x = ulj; |
| 151 | |
| 152 | if (child != 0) { |
| 153 | if (relative) { |
| 154 | getparyx(child, i, j); |
| 155 | } else { |
| 156 | getbegyx(child, i, j); |
| 157 | i -= uli + getbegy(parent); |
| 158 | j -= ulj + getbegx(parent); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (more) |
| 163 | *more = FALSE; |
| 164 | |
| 165 | for (;;) { |
| 166 | bool moved = FALSE; |
| 167 | |
| 168 | tail_line("Upper left [%2d,%2d] Lower right [%2d,%2d] -> %d,%d -> %d,%d", |
| 169 | uli, ulj, |
| 170 | lri, lrj, |
| 171 | i, j, |
| 172 | uli + i, ulj + j); |
| 173 | wmove(parent, uli + i, ulj + j); |
| 174 | |
| 175 | switch (wgetch(parent)) { |
| 176 | case KEY_UP: |
| 177 | i += si - 1; |
| 178 | moved = TRUE; |
| 179 | break; |
| 180 | case KEY_DOWN: |
| 181 | i++; |
| 182 | moved = TRUE; |
| 183 | break; |
| 184 | case KEY_LEFT: |
| 185 | j += sj - 1; |
| 186 | moved = TRUE; |
| 187 | break; |
| 188 | case KEY_RIGHT: |
| 189 | j++; |
| 190 | moved = TRUE; |
| 191 | break; |
| 192 | case QUIT: |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 193 | /* FALLTHRU */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 194 | case ESCAPE: |
| 195 | return ((PAIR *) 0); |
| 196 | #ifdef NCURSES_MOUSE_VERSION |
| 197 | case KEY_MOUSE: |
| 198 | { |
| 199 | MEVENT event; |
| 200 | |
| 201 | getmouse(&event); |
| 202 | if (event.y > uli && event.x > ulj) { |
| 203 | if (parent != stdscr) { |
| 204 | i = event.y - getbegy(parent) - uli; |
| 205 | j = event.x - getbegx(parent) - ulj; |
| 206 | } else { |
| 207 | i = event.y - uli; |
| 208 | j = event.x - ulj; |
| 209 | } |
| 210 | } else { |
| 211 | beep(); |
| 212 | break; |
| 213 | } |
| 214 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 215 | #endif |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 216 | /* FALLTHRU */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 217 | default: |
| 218 | res.y = uli + i; |
| 219 | res.x = ulj + j; |
| 220 | return (&res); |
| 221 | } |
| 222 | |
| 223 | if (si <= 0) |
| 224 | i = 0; |
| 225 | else |
| 226 | i %= si; |
| 227 | |
| 228 | if (sj <= 0) |
| 229 | j = 0; |
| 230 | else |
| 231 | j %= sj; |
| 232 | |
| 233 | /* |
| 234 | * If the caller can handle continuous movement, return the result. |
| 235 | */ |
| 236 | if (moved && more) { |
| 237 | *more = TRUE; |
| 238 | res.y = uli + i; |
| 239 | res.x = ulj + j; |
| 240 | return (&res); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Ask user for a window definition. |
| 247 | */ |
| 248 | static bool |
| 249 | getwindow(WINDOW *parent, PAIR * ul, PAIR * lr) |
| 250 | { |
| 251 | int min_col = (parent == stdscr) ? COL_MIN : 0; |
| 252 | int max_col = (parent == stdscr) ? COL_MAX : getmaxx(parent); |
| 253 | int min_line = (parent == stdscr) ? LINE_MIN : 0; |
| 254 | int max_line = (parent == stdscr) ? LINE_MAX : getmaxy(parent); |
| 255 | PAIR *tmp; |
| 256 | bool result = FALSE; |
| 257 | |
| 258 | head_line("Use arrows to move cursor, anything else to mark corner 1"); |
| 259 | if ((tmp = selectcell(parent, 0, |
| 260 | min_line, min_col, |
| 261 | max_line, max_col, |
| 262 | FALSE, |
| 263 | (bool *) 0)) != 0) { |
| 264 | *ul = *tmp; |
| 265 | MvWAddCh(parent, ul->y, ul->x, '*'); |
| 266 | |
| 267 | head_line("Use arrows to move cursor, anything else to mark corner 2"); |
| 268 | if ((tmp = selectcell(parent, 0, |
| 269 | ul->y, ul->x, |
| 270 | max_line, max_col, |
| 271 | FALSE, |
| 272 | (bool *) 0)) != 0) { |
| 273 | *lr = *tmp; |
| 274 | MvWAddCh(parent, lr->y, lr->x, '*'); |
| 275 | wmove(parent, lr->y, lr->x); |
| 276 | wsyncdown(parent); |
| 277 | wrefresh(parent); |
| 278 | result = (lr->y != ul->y && lr->x != ul->x); |
| 279 | } |
| 280 | } |
| 281 | head_line("done"); |
| 282 | return result; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Draw a box inside the given window. |
| 287 | */ |
| 288 | static void |
| 289 | box_inside(WINDOW *win) |
| 290 | { |
| 291 | int y0, x0; |
| 292 | int y1, x1; |
| 293 | |
| 294 | getyx(win, y0, x0); |
| 295 | getmaxyx(win, y1, x1); |
| 296 | |
| 297 | MvWHLine(win, 0, 0, ACS_HLINE, x1); |
| 298 | MvWHLine(win, y1 - 1, 0, ACS_HLINE, x1); |
| 299 | |
| 300 | MvWVLine(win, 0, 0, ACS_VLINE, y1); |
| 301 | MvWVLine(win, 0, x1 - 1, ACS_VLINE, y1); |
| 302 | |
| 303 | MvWAddCh(win, 0, 0, ACS_ULCORNER); |
| 304 | MvWAddCh(win, y1 - 1, 0, ACS_LLCORNER); |
| 305 | MvWAddCh(win, 0, x1 - 1, ACS_URCORNER); |
| 306 | MvWAddCh(win, y1 - 1, x1 - 1, ACS_LRCORNER); |
| 307 | |
| 308 | wsyncdown(win); |
| 309 | wmove(win, y0, x0); |
| 310 | wrefresh(win); |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Add a window to our list. |
| 315 | */ |
| 316 | static void |
| 317 | add_window(WINDOW *parent, WINDOW *child) |
| 318 | { |
| 319 | static unsigned have = 0; |
| 320 | unsigned need = ((num_windows + 1) | 31) + 1; |
| 321 | |
| 322 | keypad(child, TRUE); |
| 323 | if (need > have) { |
| 324 | all_windows = typeRealloc(FRAME, need, all_windows); |
| 325 | if (!all_windows) |
| 326 | failed("add_window"); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 327 | have = need; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 328 | } |
| 329 | all_windows[num_windows].parent = parent; |
| 330 | all_windows[num_windows].child = child; |
| 331 | num_windows++; |
| 332 | } |
| 333 | |
| 334 | static int |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 335 | window2num(const WINDOW *const win) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 336 | { |
| 337 | int n; |
| 338 | int result = -1; |
| 339 | for (n = 0; n < (int) num_windows; ++n) { |
| 340 | if (win == all_windows[n].child) { |
| 341 | result = n; |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | return result; |
| 346 | } |
| 347 | |
| 348 | static WINDOW * |
| 349 | parent_of(WINDOW *win) |
| 350 | { |
| 351 | WINDOW *result = 0; |
| 352 | int n = window2num(win); |
| 353 | if (n >= 0) |
| 354 | result = all_windows[n].parent; |
| 355 | return result; |
| 356 | } |
| 357 | |
| 358 | static void |
| 359 | repaint_one(WINDOW *win) |
| 360 | { |
| 361 | touchwin(win); |
| 362 | wnoutrefresh(win); |
| 363 | } |
| 364 | |
| 365 | static void |
| 366 | refresh_all(WINDOW *win) |
| 367 | { |
| 368 | unsigned n; |
| 369 | |
| 370 | for (n = 0; n < num_windows; ++n) { |
| 371 | if (all_windows[n].child != win) { |
| 372 | repaint_one(all_windows[n].child); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | repaint_one(win); |
| 377 | doupdate(); |
| 378 | } |
| 379 | |
| 380 | static WINDOW * |
| 381 | next_window(WINDOW *win) |
| 382 | { |
| 383 | WINDOW *result = win; |
| 384 | int n = window2num(win); |
| 385 | |
| 386 | if (n++ >= 0) { |
| 387 | result = all_windows[(unsigned) n % num_windows].child; |
| 388 | wmove(result, 0, 0); |
| 389 | wrefresh(result); |
| 390 | } |
| 391 | return result; |
| 392 | } |
| 393 | |
| 394 | static WINDOW * |
| 395 | prev_window(WINDOW *win) |
| 396 | { |
| 397 | WINDOW *result = win; |
| 398 | int n = window2num(win); |
| 399 | |
| 400 | if (n-- >= 0) { |
| 401 | if (n < 0) |
| 402 | n = (int) (num_windows - 1); |
| 403 | result = all_windows[(unsigned) n % num_windows].child; |
| 404 | wmove(result, 0, 0); |
| 405 | wrefresh(result); |
| 406 | } |
| 407 | return result; |
| 408 | } |
| 409 | |
| 410 | static void |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 411 | recur_move_window(const WINDOW *const parent, int dy, int dx) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 412 | { |
| 413 | unsigned n; |
| 414 | |
| 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); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * test mvwin(). |
| 425 | */ |
| 426 | static bool |
| 427 | move_window(WINDOW *win, bool recur) |
| 428 | { |
| 429 | WINDOW *parent = parent_of(win); |
| 430 | bool result = FALSE; |
| 431 | |
| 432 | if (parent != 0) { |
| 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); |
| 438 | PAIR *tmp; |
| 439 | bool more; |
| 440 | |
| 441 | head_line("Select new position for %swindow", top ? "" : "sub"); |
| 442 | |
| 443 | while ((tmp = selectcell(parent, |
| 444 | win, |
| 445 | min_line, min_col, |
| 446 | max_line, max_col, |
| 447 | FALSE, |
| 448 | &more)) != 0) { |
| 449 | int y0, x0; |
| 450 | getbegyx(parent, y0, x0); |
| 451 | /* |
| 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 |
| 456 | * when testing. |
| 457 | */ |
| 458 | if (mvwin(win, y0 + tmp->y, x0 + tmp->x) != ERR) { |
| 459 | if (recur) { |
| 460 | recur_move_window(win, tmp->y, tmp->x); |
| 461 | } |
| 462 | refresh_all(win); |
| 463 | doupdate(); |
| 464 | result = TRUE; |
| 465 | } else { |
| 466 | result = FALSE; |
| 467 | } |
| 468 | if (!more) |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 | head_line("done"); |
| 473 | return result; |
| 474 | } |
| 475 | |
| 476 | static void |
| 477 | show_derwin(WINDOW *win) |
| 478 | { |
| 479 | int pary, parx, maxy, maxx; |
| 480 | |
| 481 | getmaxyx(win, maxy, maxx); |
| 482 | getparyx(win, pary, parx); |
| 483 | |
| 484 | head_line("Select new position for derived window at %d,%d (%d,%d)", |
| 485 | pary, parx, maxy, maxx); |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * test mvderwin(). |
| 490 | */ |
| 491 | static bool |
| 492 | move_derwin(WINDOW *win) |
| 493 | { |
| 494 | WINDOW *parent = parent_of(win); |
| 495 | bool result = FALSE; |
| 496 | |
| 497 | if (parent != 0) { |
| 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); |
| 503 | PAIR *tmp; |
| 504 | bool more; |
| 505 | |
| 506 | show_derwin(win); |
| 507 | while ((tmp = selectcell(parent, |
| 508 | win, |
| 509 | min_line, min_col, |
| 510 | max_line, max_col, |
| 511 | TRUE, |
| 512 | &more)) != 0) { |
| 513 | if (mvderwin(win, tmp->y, tmp->x) != ERR) { |
| 514 | refresh_all(win); |
| 515 | doupdate(); |
| 516 | repaint_one(win); |
| 517 | doupdate(); |
| 518 | result = TRUE; |
| 519 | show_derwin(win); |
| 520 | } else { |
| 521 | flash(); |
| 522 | } |
| 523 | if (!more) |
| 524 | break; |
| 525 | } |
| 526 | } |
| 527 | head_line("done"); |
| 528 | return result; |
| 529 | } |
| 530 | |
| 531 | static void |
| 532 | fill_window(WINDOW *win, chtype ch) |
| 533 | { |
| 534 | int y, x; |
| 535 | int y0, x0; |
| 536 | int y1, x1; |
| 537 | |
| 538 | getyx(win, y0, x0); |
| 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); |
| 543 | } |
| 544 | } |
| 545 | wsyncdown(win); |
| 546 | wmove(win, y0, x0); |
| 547 | wrefresh(win); |
| 548 | } |
| 549 | |
| 550 | static void |
| 551 | fill_with_pattern(WINDOW *win) |
| 552 | { |
| 553 | int y, x; |
| 554 | int y0, x0; |
| 555 | int y1, x1; |
| 556 | int ch = 'a'; |
| 557 | |
| 558 | getyx(win, y0, x0); |
| 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); |
| 563 | if (++ch > 'z') |
| 564 | ch = 'a'; |
| 565 | } |
| 566 | } |
| 567 | wsyncdown(win); |
| 568 | wmove(win, y0, x0); |
| 569 | wrefresh(win); |
| 570 | } |
| 571 | |
| 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 |
| 575 | |
| 576 | static WINDOW * |
| 577 | create_my_window(WINDOW *current) |
| 578 | { |
| 579 | PAIR ul, lr; |
| 580 | WINDOW *result = 0; |
| 581 | |
| 582 | if (getwindow(stdscr, &ul, &lr)) { |
| 583 | result = newwin(lines_of(ul, lr), cols_of(ul, lr), pair_of(ul)); |
| 584 | if (result != 0) { |
| 585 | fill_window(result, 'c'); |
| 586 | add_window(stdscr, result); |
| 587 | } |
| 588 | } |
| 589 | if (result == 0) |
| 590 | result = current; |
| 591 | return result; |
| 592 | } |
| 593 | |
| 594 | static WINDOW * |
| 595 | create_my_derwin(WINDOW *parent) |
| 596 | { |
| 597 | PAIR ul, lr; |
| 598 | WINDOW *result = 0; |
| 599 | |
| 600 | if (getwindow(parent, &ul, &lr)) { |
| 601 | result = derwin(parent, lines_of(ul, lr), cols_of(ul, lr), pair_of(ul)); |
| 602 | if (result != 0) { |
| 603 | fill_window(result, 'd'); |
| 604 | add_window(parent, result); |
| 605 | } |
| 606 | } |
| 607 | if (result == 0) |
| 608 | result = parent; |
| 609 | return result; |
| 610 | } |
| 611 | |
| 612 | static WINDOW * |
| 613 | create_my_subwin(WINDOW *parent) |
| 614 | { |
| 615 | PAIR ul, lr; |
| 616 | WINDOW *result = 0; |
| 617 | |
| 618 | if (getwindow(parent, &ul, &lr)) { |
| 619 | result = subwin(parent, |
| 620 | lines_of(ul, lr), |
| 621 | cols_of(ul, lr), |
| 622 | ul.y + getbegy(parent), |
| 623 | ul.x + getbegx(parent)); |
| 624 | if (result != 0) { |
| 625 | fill_window(result, 's'); |
| 626 | add_window(parent, result); |
| 627 | } |
| 628 | } |
| 629 | if (result == 0) |
| 630 | result = parent; |
| 631 | return result; |
| 632 | } |
| 633 | |
| 634 | static void |
| 635 | show_help(WINDOW *current) |
| 636 | { |
| 637 | /* *INDENT-OFF* */ |
| 638 | static struct { |
| 639 | int key; |
| 640 | CONST_FMT char * msg; |
| 641 | } help[] = { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 642 | { HELP_KEY_1, "Show this screen" }, |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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)" }, |
| 651 | { 'q', "Quit" }, |
| 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" }, |
| 656 | }; |
| 657 | /* *INDENT-ON* */ |
| 658 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 659 | char **msgs = typeCalloc(char *, SIZEOF(help) + 1); |
| 660 | size_t n; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 661 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 662 | for (n = 0; n < SIZEOF(help); ++n) { |
| 663 | size_t need = (21 + strlen(help[n].msg)); |
| 664 | msgs[n] = typeMalloc(char, need); |
| 665 | _nc_SPRINTF(msgs[n], _nc_SLIMIT(need) |
| 666 | "%-20s%s", keyname(help[n].key), help[n].msg); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 667 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 668 | popup_msg2(current, msgs); |
| 669 | for (n = 0; n < SIZEOF(help); ++n) { |
| 670 | free(msgs[n]); |
| 671 | } |
| 672 | free(msgs); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 673 | } |
| 674 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 675 | static void |
| 676 | usage(int ok) |
| 677 | { |
| 678 | static const char *msg[] = |
| 679 | { |
| 680 | "Usage: movewindow [options]" |
| 681 | ,"" |
| 682 | ,USAGE_COMMON |
| 683 | }; |
| 684 | size_t n; |
| 685 | |
| 686 | for (n = 0; n < SIZEOF(msg); n++) |
| 687 | fprintf(stderr, "%s\n", msg[n]); |
| 688 | |
| 689 | ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); |
| 690 | } |
| 691 | /* *INDENT-OFF* */ |
| 692 | VERSION_COMMON() |
| 693 | /* *INDENT-ON* */ |
| 694 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 695 | int |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 696 | main(int argc, char *argv[]) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 697 | { |
| 698 | WINDOW *current_win; |
| 699 | int ch; |
| 700 | bool done = FALSE; |
| 701 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 702 | while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) { |
| 703 | switch (ch) { |
| 704 | case OPTS_VERSION: |
| 705 | show_version(argv); |
| 706 | ExitProgram(EXIT_SUCCESS); |
| 707 | default: |
| 708 | usage(ch == OPTS_USAGE); |
| 709 | /* NOTREACHED */ |
| 710 | } |
| 711 | } |
| 712 | if (optind < argc) |
| 713 | usage(FALSE); |
| 714 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 715 | initscr(); |
| 716 | cbreak(); |
| 717 | noecho(); |
| 718 | nonl(); |
| 719 | intrflush(stdscr, FALSE); |
| 720 | |
| 721 | add_window(0, current_win = stdscr); |
| 722 | |
| 723 | #ifdef NCURSES_MOUSE_VERSION |
| 724 | (void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL); |
| 725 | #endif /* NCURSES_MOUSE_VERSION */ |
| 726 | |
| 727 | while (!done && (ch = wgetch(current_win)) != ERR) { |
| 728 | int y, x; |
| 729 | |
| 730 | getyx(current_win, y, x); |
| 731 | |
| 732 | switch (ch) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 733 | case HELP_KEY_1: |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 734 | show_help(current_win); |
| 735 | break; |
| 736 | case 'b': |
| 737 | box_inside(current_win); |
| 738 | break; |
| 739 | case 'c': |
| 740 | current_win = create_my_window(current_win); |
| 741 | break; |
| 742 | case 'd': |
| 743 | current_win = create_my_derwin(current_win); |
| 744 | break; |
| 745 | case 'D': |
| 746 | if (!move_derwin(current_win)) { |
| 747 | tail_line("error"); |
| 748 | continue; |
| 749 | } |
| 750 | break; |
| 751 | case 'f': |
| 752 | fill_window(current_win, (chtype) wgetch(current_win)); |
| 753 | break; |
| 754 | case 'F': |
| 755 | fill_with_pattern(current_win); |
| 756 | break; |
| 757 | case 'm': |
| 758 | case 'M': |
| 759 | if (!move_window(current_win, (ch == 'M'))) { |
| 760 | tail_line("error"); |
| 761 | continue; |
| 762 | } |
| 763 | break; |
| 764 | case 'q': |
| 765 | done = TRUE; |
| 766 | break; |
| 767 | case 's': |
| 768 | current_win = create_my_subwin(current_win); |
| 769 | break; |
| 770 | case CTRL('L'): |
| 771 | refresh_all(current_win); |
| 772 | break; |
| 773 | case CTRL('N'): |
| 774 | current_win = next_window(current_win); |
| 775 | break; |
| 776 | case CTRL('P'): |
| 777 | current_win = prev_window(current_win); |
| 778 | break; |
| 779 | #if 0 |
| 780 | /* want to allow cursor to move around the current window too */ |
| 781 | /* want to test the resizing of windows and subwindows too */ |
| 782 | /* want to allow deleting a window also */ |
| 783 | #endif |
| 784 | default: |
| 785 | wmove(current_win, y, x); |
| 786 | tail_line("unrecognized key (use '?' for help)"); |
| 787 | beep(); |
| 788 | continue; |
| 789 | } |
| 790 | tail_line("size [%d,%d] begin [%d,%d] parent [%d,%d]", |
| 791 | getmaxy(current_win), |
| 792 | getmaxx(current_win), |
| 793 | getbegy(current_win), |
| 794 | getbegx(current_win), |
| 795 | getpary(current_win), |
| 796 | getparx(current_win)); |
| 797 | wmove(current_win, 0, 0); |
| 798 | } |
| 799 | endwin(); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 800 | #if NO_LEAKS |
| 801 | free(all_windows); |
| 802 | #endif |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 803 | ExitProgram(EXIT_SUCCESS); |
| 804 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 805 | #else |
| 806 | int |
| 807 | main(void) |
| 808 | { |
| 809 | printf("This program requires the curses mvderwin and mvwin functions\n"); |
| 810 | ExitProgram(EXIT_FAILURE); |
| 811 | } |
| 812 | #endif |