| 1 |
|
| 2 |
|
| 3 |
#include <curspriv.h> |
| 4 |
|
| 5 |
RCSID("$Id: window.c,v 1.62 2008/07/13 16:08:18 wmcbrine Exp $") |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
#include <stdlib.h> |
| 134 |
|
| 135 |
WINDOW *PDC_makenew(int nlines, int ncols, int begy, int begx) |
| 136 |
{ |
| 137 |
WINDOW *win; |
| 138 |
|
| 139 |
PDC_LOG(("PDC_makenew() - called: lines %d cols %d begy %d begx %d\n", |
| 140 |
nlines, ncols, begy, begx)); |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
if ((win = calloc(1, sizeof(WINDOW))) == (WINDOW *)NULL) |
| 145 |
return win; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
if ((win->_y = malloc(nlines * sizeof(chtype *))) == NULL) |
| 150 |
{ |
| 151 |
free(win); |
| 152 |
return (WINDOW *)NULL; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
if ((win->_firstch = malloc(nlines * sizeof(int))) == NULL) |
| 158 |
{ |
| 159 |
free(win->_y); |
| 160 |
free(win); |
| 161 |
return (WINDOW *)NULL; |
| 162 |
} |
| 163 |
|
| 164 |
if ((win->_lastch = malloc(nlines * sizeof(int))) == NULL) |
| 165 |
{ |
| 166 |
free(win->_firstch); |
| 167 |
free(win->_y); |
| 168 |
free(win); |
| 169 |
return (WINDOW *)NULL; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
win->_maxy = nlines; |
| 175 |
win->_maxx = ncols; |
| 176 |
win->_begy = begy; |
| 177 |
win->_begx = begx; |
| 178 |
win->_bkgd = ' '; |
| 179 |
win->_clear = (bool) ((nlines == LINES) && (ncols == COLS)); |
| 180 |
win->_bmarg = nlines - 1; |
| 181 |
win->_parx = win->_pary = -1; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
touchwin(win); |
| 186 |
|
| 187 |
return win; |
| 188 |
} |
| 189 |
|
| 190 |
WINDOW *PDC_makelines(WINDOW *win) |
| 191 |
{ |
| 192 |
int i, j, nlines, ncols; |
| 193 |
|
| 194 |
PDC_LOG(("PDC_makelines() - called: lines %d cols %d\n", nlines, ncols)); |
| 195 |
|
| 196 |
if (!win) |
| 197 |
return (WINDOW *)NULL; |
| 198 |
|
| 199 |
nlines = win->_maxy; |
| 200 |
ncols = win->_maxx; |
| 201 |
|
| 202 |
for (i = 0; i < nlines; i++) |
| 203 |
{ |
| 204 |
if ((win->_y[i] = malloc(ncols * sizeof(chtype))) == NULL) |
| 205 |
{ |
| 206 |
|
| 207 |
|
| 208 |
for (j = 0; j < i; j++) |
| 209 |
free(win->_y[j]); |
| 210 |
|
| 211 |
free(win->_firstch); |
| 212 |
free(win->_lastch); |
| 213 |
free(win->_y); |
| 214 |
free(win); |
| 215 |
|
| 216 |
return (WINDOW *)NULL; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
return win; |
| 221 |
} |
| 222 |
|
| 223 |
void PDC_sync(WINDOW *win) |
| 224 |
{ |
| 225 |
PDC_LOG(("PDC_sync() - called:\n")); |
| 226 |
|
| 227 |
if (win->_immed) |
| 228 |
wrefresh(win); |
| 229 |
if (win->_sync) |
| 230 |
wsyncup(win); |
| 231 |
} |
| 232 |
|
| 233 |
WINDOW *newwin(int nlines, int ncols, int begy, int begx) |
| 234 |
{ |
| 235 |
WINDOW *win; |
| 236 |
|
| 237 |
PDC_LOG(("newwin() - called:lines=%d cols=%d begy=%d begx=%d\n", |
| 238 |
nlines, ncols, begy, begx)); |
| 239 |
|
| 240 |
if (!nlines) |
| 241 |
nlines = LINES - begy; |
| 242 |
if (!ncols) |
| 243 |
ncols = COLS - begx; |
| 244 |
|
| 245 |
if ( (begy + nlines > SP->lines || begx + ncols > SP->cols) |
| 246 |
|| !(win = PDC_makenew(nlines, ncols, begy, begx)) |
| 247 |
|| !(win = PDC_makelines(win)) ) |
| 248 |
return (WINDOW *)NULL; |
| 249 |
|
| 250 |
werase(win); |
| 251 |
|
| 252 |
return win; |
| 253 |
} |
| 254 |
|
| 255 |
int delwin(WINDOW *win) |
| 256 |
{ |
| 257 |
int i; |
| 258 |
|
| 259 |
PDC_LOG(("delwin() - called\n")); |
| 260 |
|
| 261 |
if (!win) |
| 262 |
return ERR; |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
if (!(win->_flags & (_SUBWIN|_SUBPAD))) |
| 267 |
for (i = 0; i < win->_maxy && win->_y[i]; i++) |
| 268 |
if (win->_y[i]) |
| 269 |
free(win->_y[i]); |
| 270 |
|
| 271 |
free(win->_firstch); |
| 272 |
free(win->_lastch); |
| 273 |
free(win->_y); |
| 274 |
free(win); |
| 275 |
|
| 276 |
return OK; |
| 277 |
} |
| 278 |
|
| 279 |
int mvwin(WINDOW *win, int y, int x) |
| 280 |
{ |
| 281 |
PDC_LOG(("mvwin() - called\n")); |
| 282 |
|
| 283 |
if (!win || (y + win->_maxy > LINES || y < 0) |
| 284 |
|| (x + win->_maxx > COLS || x < 0)) |
| 285 |
return ERR; |
| 286 |
|
| 287 |
win->_begy = y; |
| 288 |
win->_begx = x; |
| 289 |
touchwin(win); |
| 290 |
|
| 291 |
return OK; |
| 292 |
} |
| 293 |
|
| 294 |
WINDOW *subwin(WINDOW *orig, int nlines, int ncols, int begy, int begx) |
| 295 |
{ |
| 296 |
WINDOW *win; |
| 297 |
int i, j, k; |
| 298 |
|
| 299 |
PDC_LOG(("subwin() - called: lines %d cols %d begy %d begx %d\n", |
| 300 |
nlines, ncols, begy, begx)); |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
if (!orig || (begy < orig->_begy) || (begx < orig->_begx) || |
| 305 |
(begy + nlines) > (orig->_begy + orig->_maxy) || |
| 306 |
(begx + ncols) > (orig->_begx + orig->_maxx)) |
| 307 |
return (WINDOW *)NULL; |
| 308 |
|
| 309 |
j = begy - orig->_begy; |
| 310 |
k = begx - orig->_begx; |
| 311 |
|
| 312 |
if (!nlines) |
| 313 |
nlines = orig->_maxy - 1 - j; |
| 314 |
if (!ncols) |
| 315 |
ncols = orig->_maxx - 1 - k; |
| 316 |
|
| 317 |
if ( !(win = PDC_makenew(nlines, ncols, begy, begx)) ) |
| 318 |
return (WINDOW *)NULL; |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
win->_attrs = orig->_attrs; |
| 323 |
win->_bkgd = orig->_bkgd; |
| 324 |
win->_leaveit = orig->_leaveit; |
| 325 |
win->_scroll = orig->_scroll; |
| 326 |
win->_nodelay = orig->_nodelay; |
| 327 |
win->_use_keypad = orig->_use_keypad; |
| 328 |
win->_immed = orig->_immed; |
| 329 |
win->_sync = orig->_sync; |
| 330 |
win->_pary = j; |
| 331 |
win->_parx = k; |
| 332 |
win->_parent = orig; |
| 333 |
|
| 334 |
for (i = 0; i < nlines; i++, j++) |
| 335 |
win->_y[i] = orig->_y[j] + k; |
| 336 |
|
| 337 |
win->_flags |= _SUBWIN; |
| 338 |
|
| 339 |
return win; |
| 340 |
} |
| 341 |
|
| 342 |
WINDOW *derwin(WINDOW *orig, int nlines, int ncols, int begy, int begx) |
| 343 |
{ |
| 344 |
return subwin(orig, nlines, ncols, begy + orig->_begy, begx + orig->_begx); |
| 345 |
} |
| 346 |
|
| 347 |
int mvderwin(WINDOW *win, int pary, int parx) |
| 348 |
{ |
| 349 |
int i, j; |
| 350 |
WINDOW *mypar; |
| 351 |
|
| 352 |
if (!win || !(win->_parent)) |
| 353 |
return ERR; |
| 354 |
|
| 355 |
mypar = win->_parent; |
| 356 |
|
| 357 |
if (pary < 0 || parx < 0 || (pary + win->_maxy) > mypar->_maxy || |
| 358 |
(parx + win->_maxx) > mypar->_maxx) |
| 359 |
return ERR; |
| 360 |
|
| 361 |
j = pary; |
| 362 |
|
| 363 |
for (i = 0; i < win->_maxy; i++) |
| 364 |
win->_y[i] = (mypar->_y[j++]) + parx; |
| 365 |
|
| 366 |
win->_pary = pary; |
| 367 |
win->_parx = parx; |
| 368 |
|
| 369 |
return OK; |
| 370 |
} |
| 371 |
|
| 372 |
WINDOW *dupwin(WINDOW *win) |
| 373 |
{ |
| 374 |
WINDOW *new; |
| 375 |
chtype *ptr, *ptr1; |
| 376 |
int nlines, ncols, begy, begx, i; |
| 377 |
|
| 378 |
if (!win) |
| 379 |
return (WINDOW *)NULL; |
| 380 |
|
| 381 |
nlines = win->_maxy; |
| 382 |
ncols = win->_maxx; |
| 383 |
begy = win->_begy; |
| 384 |
begx = win->_begx; |
| 385 |
|
| 386 |
if ( !(new = PDC_makenew(nlines, ncols, begy, begx)) |
| 387 |
|| !(new = PDC_makelines(new)) ) |
| 388 |
return (WINDOW *)NULL; |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
for (i = 0; i < nlines; i++) |
| 393 |
{ |
| 394 |
for (ptr = new->_y[i], ptr1 = win->_y[i]; |
| 395 |
ptr < new->_y[i] + ncols; ptr++, ptr1++) |
| 396 |
*ptr = *ptr1; |
| 397 |
|
| 398 |
new->_firstch[i] = 0; |
| 399 |
new->_lastch[i] = ncols - 1; |
| 400 |
} |
| 401 |
|
| 402 |
new->_curx = win->_curx; |
| 403 |
new->_cury = win->_cury; |
| 404 |
new->_maxy = win->_maxy; |
| 405 |
new->_maxx = win->_maxx; |
| 406 |
new->_begy = win->_begy; |
| 407 |
new->_begx = win->_begx; |
| 408 |
new->_flags = win->_flags; |
| 409 |
new->_attrs = win->_attrs; |
| 410 |
new->_clear = win->_clear; |
| 411 |
new->_leaveit = win->_leaveit; |
| 412 |
new->_scroll = win->_scroll; |
| 413 |
new->_nodelay = win->_nodelay; |
| 414 |
new->_use_keypad = win->_use_keypad; |
| 415 |
new->_tmarg = win->_tmarg; |
| 416 |
new->_bmarg = win->_bmarg; |
| 417 |
new->_parx = win->_parx; |
| 418 |
new->_pary = win->_pary; |
| 419 |
new->_parent = win->_parent; |
| 420 |
new->_bkgd = win->_bkgd; |
| 421 |
new->_flags = win->_flags; |
| 422 |
|
| 423 |
return new; |
| 424 |
} |
| 425 |
|
| 426 |
WINDOW *resize_window(WINDOW *win, int nlines, int ncols) |
| 427 |
{ |
| 428 |
WINDOW *new; |
| 429 |
int i, save_cury, save_curx, new_begy, new_begx; |
| 430 |
|
| 431 |
PDC_LOG(("resize_window() - called: nlines %d ncols %d\n", |
| 432 |
nlines, ncols)); |
| 433 |
|
| 434 |
if (!win) |
| 435 |
return (WINDOW *)NULL; |
| 436 |
|
| 437 |
if (win->_flags & _SUBPAD) |
| 438 |
{ |
| 439 |
if ( !(new = subpad(win->_parent, nlines, ncols, |
| 440 |
win->_begy, win->_begx)) ) |
| 441 |
return (WINDOW *)NULL; |
| 442 |
} |
| 443 |
else if (win->_flags & _SUBWIN) |
| 444 |
{ |
| 445 |
if ( !(new = subwin(win->_parent, nlines, ncols, |
| 446 |
win->_begy, win->_begx)) ) |
| 447 |
return (WINDOW *)NULL; |
| 448 |
} |
| 449 |
else |
| 450 |
{ |
| 451 |
if (win == SP->slk_winptr) |
| 452 |
{ |
| 453 |
new_begy = SP->lines - SP->slklines; |
| 454 |
new_begx = 0; |
| 455 |
} |
| 456 |
else |
| 457 |
{ |
| 458 |
new_begy = win->_begy; |
| 459 |
new_begx = win->_begx; |
| 460 |
} |
| 461 |
|
| 462 |
if ( !(new = PDC_makenew(nlines, ncols, new_begy, new_begx)) ) |
| 463 |
return (WINDOW *)NULL; |
| 464 |
} |
| 465 |
|
| 466 |
save_curx = min(win->_curx, new->_maxx); |
| 467 |
save_cury = min(win->_cury, new->_maxy); |
| 468 |
|
| 469 |
if (!(win->_flags & (_SUBPAD|_SUBWIN))) |
| 470 |
{ |
| 471 |
if ( !(new = PDC_makelines(new)) ) |
| 472 |
return (WINDOW *)NULL; |
| 473 |
|
| 474 |
werase(new); |
| 475 |
|
| 476 |
copywin(win, new, 0, 0, 0, 0, min(win->_maxy, new->_maxy) - 1, |
| 477 |
min(win->_maxx, new->_maxx) - 1, FALSE); |
| 478 |
|
| 479 |
for (i = 0; i < win->_maxy && win->_y[i]; i++) |
| 480 |
if (win->_y[i]) |
| 481 |
free(win->_y[i]); |
| 482 |
} |
| 483 |
|
| 484 |
new->_flags = win->_flags; |
| 485 |
new->_attrs = win->_attrs; |
| 486 |
new->_clear = win->_clear; |
| 487 |
new->_leaveit = win->_leaveit; |
| 488 |
new->_scroll = win->_scroll; |
| 489 |
new->_nodelay = win->_nodelay; |
| 490 |
new->_use_keypad = win->_use_keypad; |
| 491 |
new->_tmarg = (win->_tmarg > new->_maxy - 1) ? 0 : win->_tmarg; |
| 492 |
new->_bmarg = (win->_bmarg == win->_maxy - 1) ? |
| 493 |
new->_maxy - 1 : min(win->_bmarg, (new->_maxy - 1)); |
| 494 |
new->_parent = win->_parent; |
| 495 |
new->_immed = win->_immed; |
| 496 |
new->_sync = win->_sync; |
| 497 |
new->_bkgd = win->_bkgd; |
| 498 |
|
| 499 |
new->_curx = save_curx; |
| 500 |
new->_cury = save_cury; |
| 501 |
|
| 502 |
free(win->_firstch); |
| 503 |
free(win->_lastch); |
| 504 |
free(win->_y); |
| 505 |
|
| 506 |
*win = *new; |
| 507 |
free(new); |
| 508 |
|
| 509 |
return win; |
| 510 |
} |
| 511 |
|
| 512 |
int wresize(WINDOW *win, int nlines, int ncols) |
| 513 |
{ |
| 514 |
return (resize_window(win, nlines, ncols) ? OK : ERR); |
| 515 |
} |
| 516 |
|
| 517 |
void wsyncup(WINDOW *win) |
| 518 |
{ |
| 519 |
WINDOW *tmp; |
| 520 |
|
| 521 |
PDC_LOG(("wsyncup() - called\n")); |
| 522 |
|
| 523 |
for (tmp = win; tmp; tmp = tmp->_parent) |
| 524 |
touchwin(tmp); |
| 525 |
} |
| 526 |
|
| 527 |
int syncok(WINDOW *win, bool bf) |
| 528 |
{ |
| 529 |
PDC_LOG(("syncok() - called\n")); |
| 530 |
|
| 531 |
if (!win) |
| 532 |
return ERR; |
| 533 |
|
| 534 |
win->_sync = bf; |
| 535 |
|
| 536 |
return OK; |
| 537 |
} |
| 538 |
|
| 539 |
void wcursyncup(WINDOW *win) |
| 540 |
{ |
| 541 |
WINDOW *tmp; |
| 542 |
|
| 543 |
PDC_LOG(("wcursyncup() - called\n")); |
| 544 |
|
| 545 |
for (tmp = win; tmp && tmp->_parent; tmp = tmp->_parent) |
| 546 |
wmove(tmp->_parent, tmp->_pary + tmp->_cury, tmp->_parx + tmp->_curx); |
| 547 |
} |
| 548 |
|
| 549 |
void wsyncdown(WINDOW *win) |
| 550 |
{ |
| 551 |
WINDOW *tmp; |
| 552 |
|
| 553 |
PDC_LOG(("wsyncdown() - called\n")); |
| 554 |
|
| 555 |
for (tmp = win; tmp; tmp = tmp->_parent) |
| 556 |
{ |
| 557 |
if (is_wintouched(tmp)) |
| 558 |
{ |
| 559 |
touchwin(win); |
| 560 |
break; |
| 561 |
} |
| 562 |
} |
| 563 |
} |