1 2 3 4 5 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
|
/* gtwindow.c: Window objects
for GlkTerm, curses.h implementation of the Glk API.
Designed by Andrew Plotkin <erkyrath@eblong.com>
http://www.eblong.com/zarf/glk/index.html
*/
#include "gtoption.h"
#include <stdio.h>
#include <stdlib.h>
#include <wctype.h>
#include <curses.h>
#ifdef OPT_USE_SIGNALS
#include <signal.h>
#endif /* OPT_USE_SIGNALS */
#include "glk.h"
#include "glkterm.h"
#include "gtw_pair.h"
#include "gtw_blnk.h"
#include "gtw_grid.h"
#include "gtw_buf.h"
/* Linked list of all windows */
static window_t *gli_windowlist = NULL;
/* For use by gli_print_spaces() */
#define NUMSPACES (16)
static wchar_t spacebuffer[NUMSPACES+1];
window_t *gli_rootwin = NULL; /* The topmost window. */
window_t *gli_focuswin = NULL; /* The window selected by the player.
(This has nothing to do with the "current output stream", which is
gli_currentstr in gtstream.c. In fact, the program doesn't know
about gli_focuswin at all.) */
/* This is the screen region which is enclosed by the root window. */
grect_t content_box;
void (*gli_interrupt_handler)(void) = NULL;
static void compute_content_box(void);
#ifdef OPT_USE_SIGNALS
int just_resumed;
int just_killed;
static void gli_sig_resume(int val);
static void gli_sig_interrupt(int val);
#ifdef OPT_WINCHANGED_SIGNAL
int screen_size_changed;
static void gli_sig_winsize(int val);
#endif /* OPT_WINCHANGED_SIGNAL */
#endif /* OPT_USE_SIGNALS */
/* Set up the window system. This is called from main(). */
void gli_initialize_windows()
{
int ix;
gli_rootwin = NULL;
gli_focuswin = NULL;
/* Build a convenient array of spaces. */
for (ix=0; ix<NUMSPACES; ix++)
spacebuffer[ix] = ' ';
spacebuffer[NUMSPACES] = '\0';
/* Create the curses.h attribute values for each style. */
for (ix=0; ix<style_NUMSTYLES; ix++) {
int val = 0;
if (ix == style_Emphasized || ix == style_Note)
val |= A_UNDERLINE;
if (ix == style_Alert)
val |= A_REVERSE;
if (ix == style_Header || ix == style_Subheader || ix == style_Input)
val |= A_BOLD;
win_textbuffer_styleattrs[ix] = val;
if (pref_reverse_textgrids)
val ^= A_REVERSE;
win_textgrid_styleattrs[ix] = val;
}
/* Figure out the screen size. */
compute_content_box();
#ifdef OPT_USE_SIGNALS
just_resumed = FALSE;
just_killed = FALSE;
signal(SIGCONT, &gli_sig_resume);
signal(SIGHUP, &gli_sig_interrupt);
signal(SIGINT, &gli_sig_interrupt);
#ifdef OPT_WINCHANGED_SIGNAL
screen_size_changed = FALSE;
signal(SIGWINCH, &gli_sig_winsize);
#endif /* OPT_WINCHANGED_SIGNAL */
#endif /* OPT_USE_SIGNALS */
/* Draw the initial setup (no windows) */
gli_windows_redraw();
}
/* Set up all the curses parameters. This is called from main() --
before gli_initialize_windows, actually -- and also when curses
is reinitialized for a screen-size change. */
void gli_setup_curses()
{
initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
scrollok(stdscr, FALSE);
}
#ifdef OPT_USE_SIGNALS
/* Signal handler for SIGCONT. */
static void gli_sig_resume(int val)
{
signal(SIGCONT, &gli_sig_resume);
just_resumed = TRUE;
}
/* Signal handler for SIGINT. */
static void gli_sig_interrupt(int val)
{
just_killed = TRUE;
}
#ifdef OPT_WINCHANGED_SIGNAL
/* Signal handler for SIGWINCH. */
static void gli_sig_winsize(int val)
{
endwin();
newterm(getenv("TERM"), stdout, stdin);
gli_setup_curses();
gli_set_halfdelay();
screen_size_changed = TRUE;
signal(SIGWINCH, &gli_sig_winsize);
}
#endif /* OPT_WINCHANGED_SIGNAL */
#endif /* OPT_USE_SIGNALS */
/* Get out fast. This is used by the ctrl-C interrupt handler, under Unix.
It doesn't pause and wait for a keypress, and it calls the Glk interrupt
handler. Otherwise it's the same as glk_exit(). */
void gli_fast_exit()
{
if (gli_interrupt_handler) {
(*gli_interrupt_handler)();
}
gli_streams_close_all();
endwin();
putchar('\n');
exit(0);
}
static void compute_content_box()
{
/* Set content_box to the entire screen, although one could also
leave a border for messages or decoration. This is the only
place where COLS and LINES are checked. All the rest of the
layout code uses content_box. */
int width, height;
if (pref_screenwidth)
width = pref_screenwidth;
else
width = COLS;
if (pref_screenheight)
height = pref_screenheight;
else
height = LINES;
content_box.left = 0;
content_box.top = 0;
content_box.right = width;
content_box.bottom = height;
if (pref_messageline && height > 0)
content_box.bottom--; /* allow a message line */
}
window_t *gli_new_window(glui32 type, glui32 rock)
{
window_t *win = (window_t *)malloc(sizeof(window_t));
if (!win)
return NULL;
win->magicnum = MAGIC_WINDOW_NUM;
win->rock = rock;
win->type = type;
win->parent = NULL; /* for now */
win->data = NULL; /* for now */
win->char_request = FALSE;
win->line_request = FALSE;
win->line_request_uni = FALSE;
win->char_request_uni = FALSE;
win->echo_line_input = TRUE;
win->terminate_line_input = 0;
win->style = style_Normal;
win->str = gli_stream_open_window(win);
win->echostr = NULL;
win->prev = NULL;
win->next = gli_windowlist;
gli_windowlist = win;
if (win->next) {
win->next->prev = win;
}
if (gli_register_obj)
win->disprock = (*gli_register_obj)(win, gidisp_Class_Window);
return win;
}
void gli_delete_window(window_t *win)
{
window_t *prev, *next;
if (gli_unregister_obj)
(*gli_unregister_obj)(win, gidisp_Class_Window, win->disprock);
win->magicnum = 0;
win->echostr = NULL;
if (win->str) {
gli_delete_stream(win->str);
win->str = NULL;
}
prev = win->prev;
next = win->next;
win->prev = NULL;
win->next = NULL;
if (prev)
prev->next = next;
else
gli_windowlist = next;
if (next)
next->prev = prev;
free(win);
}
winid_t glk_window_open(winid_t splitwin, glui32 method, glui32 size,
glui32 wintype, glui32 rock)
{
window_t *newwin, *pairwin, *oldparent;
window_pair_t *dpairwin;
grect_t box;
glui32 val;
if (!gli_rootwin) {
if (splitwin) {
gli_strict_warning(L"window_open: ref must be NULL");
return 0;
}
/* ignore method and size now */
oldparent = NULL;
box = content_box;
}
else {
if (!splitwin) {
gli_strict_warning(L"window_open: ref must not be NULL");
return 0;
}
val = (method & winmethod_DivisionMask);
if (val != winmethod_Fixed && val != winmethod_Proportional) {
gli_strict_warning(L"window_open: invalid method (not fixed or proportional)");
return 0;
}
val = (method & winmethod_DirMask);
if (val != winmethod_Above && val != winmethod_Below
&& val != winmethod_Left && val != winmethod_Right) {
gli_strict_warning(L"window_open: invalid method (bad direction)");
return 0;
}
box = splitwin->bbox;
oldparent = splitwin->parent;
if (oldparent && oldparent->type != wintype_Pair) {
gli_strict_warning(L"window_open: parent window is not Pair");
return 0;
}
}
newwin = gli_new_window(wintype, rock);
if (!newwin) {
gli_strict_warning(L"window_open: unable to create window");
return 0;
}
switch (wintype) {
case wintype_Blank:
newwin->data = win_blank_create(newwin);
break;
case wintype_TextGrid:
newwin->data = win_textgrid_create(newwin);
break;
case wintype_TextBuffer:
newwin->data = win_textbuffer_create(newwin);
break;
case wintype_Pair:
gli_strict_warning(L"window_open: cannot open pair window directly");
gli_delete_window(newwin);
return 0;
default:
/* Unknown window type -- do not print a warning, just return 0
to indicate that it's not possible. */
gli_delete_window(newwin);
return 0;
}
if (!newwin->data) {
gli_strict_warning(L"window_open: unable to create window");
return 0;
}
if (!splitwin) {
gli_rootwin = newwin;
gli_window_rearrange(newwin, &box);
/* redraw everything, which is just the new first window */
gli_windows_redraw();
}
else {
/* create pairwin, with newwin as the key */
pairwin = gli_new_window(wintype_Pair, 0);
dpairwin = win_pair_create(pairwin, method, newwin, size);
pairwin->data = dpairwin;
dpairwin->child1 = splitwin;
dpairwin->child2 = newwin;
splitwin->parent = pairwin;
newwin->parent = pairwin;
pairwin->parent = oldparent;
if (oldparent) {
window_pair_t *dparentwin = oldparent->data;
if (dparentwin->child1 == splitwin)
dparentwin->child1 = pairwin;
else
dparentwin->child2 = pairwin;
}
else {
gli_rootwin = pairwin;
}
gli_window_rearrange(pairwin, &box);
/* redraw the new pairwin and all its contents */
gli_window_redraw(pairwin);
}
return newwin;
}
static void gli_window_close(window_t *win, int recurse)
{
window_t *wx;
if (gli_focuswin == win) {
gli_focuswin = NULL;
}
for (wx=win->parent; wx; wx=wx->parent) {
if (wx->type == wintype_Pair) {
window_pair_t *dwx = wx->data;
if (dwx->key == win) {
dwx->key = NULL;
dwx->keydamage = TRUE;
}
}
}
switch (win->type) {
case wintype_Blank: {
window_blank_t *dwin = win->data;
win_blank_destroy(dwin);
}
break;
case wintype_Pair: {
window_pair_t *dwin = win->data;
if (recurse) {
if (dwin->child1)
gli_window_close(dwin->child1, TRUE);
if (dwin->child2)
gli_window_close(dwin->child2, TRUE);
}
win_pair_destroy(dwin);
}
break;
case wintype_TextBuffer: {
window_textbuffer_t *dwin = win->data;
win_textbuffer_destroy(dwin);
}
break;
case wintype_TextGrid: {
window_textgrid_t *dwin = win->data;
win_textgrid_destroy(dwin);
}
break;
}
gli_delete_window(win);
}
void glk_window_close(window_t *win, stream_result_t *result)
{
if (!win) {
gli_strict_warning(L"window_close: invalid ref");
return;
}
if (win == gli_rootwin || win->parent == NULL) {
/* close the root window, which means all windows. */
gli_rootwin = 0;
/* begin (simpler) closation */
gli_stream_fill_result(win->str, result);
gli_window_close(win, TRUE);
/* redraw everything */
gli_windows_redraw();
}
else {
/* have to jigger parent */
grect_t box;
window_t *pairwin, *sibwin, *grandparwin, *wx;
window_pair_t *dpairwin, *dgrandparwin;
int keydamage_flag;
pairwin = win->parent;
dpairwin = pairwin->data;
if (win == dpairwin->child1) {
sibwin = dpairwin->child2;
}
else if (win == dpairwin->child2) {
sibwin = dpairwin->child1;
}
else {
gli_strict_warning(L"window_close: window tree is corrupted");
return;
}
box = pairwin->bbox;
grandparwin = pairwin->parent;
if (!grandparwin) {
gli_rootwin = sibwin;
sibwin->parent = NULL;
}
else {
dgrandparwin = grandparwin->data;
if (dgrandparwin->child1 == pairwin)
dgrandparwin->child1 = sibwin;
else
dgrandparwin->child2 = sibwin;
sibwin->parent = grandparwin;
}
/* Begin closation */
gli_stream_fill_result(win->str, result);
/* Close the child window (and descendants), so that key-deletion can
crawl up the tree to the root window. */
gli_window_close(win, TRUE);
/* This probably isn't necessary, but the child *is* gone, so just
in case. */
if (win == dpairwin->child1) {
dpairwin->child1 = NULL;
}
else if (win == dpairwin->child2) {
dpairwin->child2 = NULL;
}
/* Now we can delete the parent pair. */
gli_window_close(pairwin, FALSE);
keydamage_flag = FALSE;
for (wx=sibwin; wx; wx=wx->parent) {
if (wx->type == wintype_Pair) {
window_pair_t *dwx = wx->data;
if (dwx->keydamage) {
keydamage_flag = TRUE;
dwx->keydamage = FALSE;
}
}
}
if (keydamage_flag) {
box = content_box;
gli_window_rearrange(gli_rootwin, &box);
gli_windows_redraw();
}
else {
gli_window_rearrange(sibwin, &box);
gli_window_redraw(sibwin);
}
}
}
void glk_window_get_arrangement(window_t *win, glui32 *method, glui32 *size,
winid_t *keywin)
{
window_pair_t *dwin;
glui32 val;
if (!win) {
gli_strict_warning(L"window_get_arrangement: invalid ref");
return;
}
if (win->type != wintype_Pair) {
gli_strict_warning(L"window_get_arrangement: not a Pair window");
return;
}
dwin = win->data;
val = dwin->dir | dwin->division;
if (!dwin->hasborder)
val |= winmethod_NoBorder;
if (size)
*size = dwin->size;
if (keywin) {
if (dwin->key)
*keywin = dwin->key;
else
*keywin = NULL;
}
if (method)
*method = val;
}
void glk_window_set_arrangement(window_t *win, glui32 method, glui32 size,
winid_t key)
{
window_pair_t *dwin;
glui32 newdir;
grect_t box;
int newvertical, newbackward;
if (!win) {
gli_strict_warning(L"window_set_arrangement: invalid ref");
return;
}
if (win->type != wintype_Pair) {
gli_strict_warning(L"window_set_arrangement: not a Pair window");
return;
}
if (key) {
window_t *wx;
if (key->type == wintype_Pair) {
gli_strict_warning(L"window_set_arrangement: keywin cannot be a Pair");
return;
}
for (wx=key; wx; wx=wx->parent) {
if (wx == win)
break;
}
if (wx == NULL) {
gli_strict_warning(L"window_set_arrangement: keywin must be a descendant");
return;
}
}
dwin = win->data;
box = win->bbox;
newdir = method & winmethod_DirMask;
newvertical = (newdir == winmethod_Left || newdir == winmethod_Right);
newbackward = (newdir == winmethod_Left || newdir == winmethod_Above);
if (!key)
key = dwin->key;
if ((newvertical && !dwin->vertical) || (!newvertical && dwin->vertical)) {
if (!dwin->vertical)
gli_strict_warning(L"window_set_arrangement: split must stay horizontal");
else
gli_strict_warning(L"window_set_arrangement: split must stay vertical");
return;
}
if (key && key->type == wintype_Blank
&& (method & winmethod_DivisionMask) == winmethod_Fixed) {
gli_strict_warning(L"window_set_arrangement: a Blank window cannot have a fixed size");
return;
}
if ((newbackward && !dwin->backward) || (!newbackward && dwin->backward)) {
/* switch the children */
window_t *tmpwin = dwin->child1;
dwin->child1 = dwin->child2;
dwin->child2 = tmpwin;
}
/* set up everything else */
dwin->dir = newdir;
dwin->division = method & winmethod_DivisionMask;
dwin->key = key;
dwin->size = size;
dwin->hasborder = ((method & winmethod_BorderMask) == winmethod_Border);
dwin->vertical = (dwin->dir == winmethod_Left || dwin->dir == winmethod_Right);
dwin->backward = (dwin->dir == winmethod_Left || dwin->dir == winmethod_Above);
gli_window_rearrange(win, &box);
gli_window_redraw(win);
}
winid_t glk_window_iterate(winid_t win, glui32 *rock)
{
if (!win) {
win = gli_windowlist;
}
else {
win = win->next;
}
if (win) {
if (rock)
*rock = win->rock;
return win;
}
if (rock)
*rock = 0;
return NULL;
}
window_t *gli_window_iterate_treeorder(window_t *win)
{
if (!win)
return gli_rootwin;
if (win->type == wintype_Pair) {
window_pair_t *dwin = win->data;
if (!dwin->backward)
return dwin->child1;
else
return dwin->child2;
}
else {
window_t *parwin;
window_pair_t *dwin;
while (win->parent) {
parwin = win->parent;
dwin = parwin->data;
if (!dwin->backward) {
if (win == dwin->child1)
return dwin->child2;
}
else {
if (win == dwin->child2)
return dwin->child1;
}
win = parwin;
}
return NULL;
}
}
glui32 glk_window_get_rock(window_t *win)
{
if (!win) {
gli_strict_warning(L"window_get_rock: invalid ref.");
return 0;
}
return win->rock;
}
winid_t glk_window_get_root()
{
if (!gli_rootwin)
return NULL;
return gli_rootwin;
}
winid_t glk_window_get_parent(window_t *win)
{
if (!win) {
gli_strict_warning(L"window_get_parent: invalid ref");
return 0;
}
if (win->parent)
return win->parent;
else
return 0;
}
winid_t glk_window_get_sibling(window_t *win)
{
window_pair_t *dparwin;
if (!win) {
gli_strict_warning(L"window_get_sibling: invalid ref");
return 0;
}
if (!win->parent)
return 0;
dparwin = win->parent->data;
if (dparwin->child1 == win)
return dparwin->child2;
else if (dparwin->child2 == win)
return dparwin->child1;
return 0;
}
glui32 glk_window_get_type(window_t *win)
{
if (!win) {
gli_strict_warning(L"window_get_parent: invalid ref");
return 0;
}
return win->type;
}
void glk_window_get_size(window_t *win, glui32 *width, glui32 *height)
{
glui32 wid = 0;
glui32 hgt = 0;
if (!win) {
gli_strict_warning(L"window_get_size: invalid ref");
return;
}
switch (win->type) {
case wintype_Blank:
case wintype_Pair:
/* always zero */
break;
case wintype_TextGrid:
case wintype_TextBuffer:
wid = win->bbox.right - win->bbox.left;
hgt = win->bbox.bottom - win->bbox.top;
break;
}
if (width)
*width = wid;
if (height)
*height = hgt;
}
strid_t glk_window_get_stream(window_t *win)
{
if (!win) {
gli_strict_warning(L"window_get_stream: invalid ref");
return NULL;
}
return win->str;
}
strid_t glk_window_get_echo_stream(window_t *win)
{
if (!win) {
gli_strict_warning(L"window_get_echo_stream: invalid ref");
return 0;
}
if (win->echostr)
return win->echostr;
else
return 0;
}
void glk_window_set_echo_stream(window_t *win, stream_t *str)
{
if (!win) {
gli_strict_warning(L"window_set_echo_stream: invalid window id");
return;
}
win->echostr = str;
}
void glk_set_window(window_t *win)
{
if (!win) {
gli_stream_set_current(NULL);
}
else {
gli_stream_set_current(win->str);
}
}
void gli_windows_unechostream(stream_t *str)
{
window_t *win;
for (win=gli_windowlist; win; win=win->next) {
if (win->echostr == str)
win->echostr = NULL;
}
}
/* Some trivial switch functions which make up for the fact that we're not
doing this in C++. */
void gli_window_rearrange(window_t *win, grect_t *box)
{
switch (win->type) {
case wintype_Blank:
win_blank_rearrange(win, box);
break;
case wintype_Pair:
win_pair_rearrange(win, box);
break;
case wintype_TextGrid:
win_textgrid_rearrange(win, box);
break;
case wintype_TextBuffer:
win_textbuffer_rearrange(win, box);
break;
}
}
void gli_windows_update()
{
window_t *win;
for (win=gli_windowlist; win; win=win->next) {
switch (win->type) {
case wintype_TextGrid:
win_textgrid_update(win);
break;
case wintype_TextBuffer:
win_textbuffer_update(win);
break;
}
}
}
void gli_window_redraw(window_t *win)
{
if (win->bbox.left >= win->bbox.right
|| win->bbox.top >= win->bbox.bottom)
return;
switch (win->type) {
case wintype_Blank:
win_blank_redraw(win);
break;
case wintype_Pair:
win_pair_redraw(win);
break;
case wintype_TextGrid:
win_textgrid_redraw(win);
break;
case wintype_TextBuffer:
win_textbuffer_redraw(win);
break;
}
}
void gli_windows_redraw()
{
int ix, jx;
if (gli_rootwin) {
/* We could draw a border around content_box, if we wanted. */
gli_window_redraw(gli_rootwin);
}
else {
/* There are no windows at all. */
clear();
ix = (content_box.left+content_box.right) / 2 - 7;
if (ix < 0)
ix = 0;
jx = (content_box.top+content_box.bottom) / 2;
move(jx, ix);
local_addwstr(L"Please wait...");
}
}
void gli_windows_size_change()
{
compute_content_box();
if (gli_rootwin) {
gli_window_rearrange(gli_rootwin, &content_box);
}
gli_windows_redraw();
gli_msgline_redraw();
gli_event_store(evtype_Arrange, NULL, 0, 0);
}
void gli_windows_place_cursor()
{
if (gli_rootwin && gli_focuswin) {
int xpos, ypos;
xpos = 0;
ypos = 0;
switch (gli_focuswin->type) {
case wintype_TextGrid:
win_textgrid_place_cursor(gli_focuswin, &xpos, &ypos);
break;
case wintype_TextBuffer:
win_textbuffer_place_cursor(gli_focuswin, &xpos, &ypos);
break;
default:
break;
}
move(gli_focuswin->bbox.top + ypos, gli_focuswin->bbox.left + xpos);
}
else {
move(content_box.bottom-1, content_box.right-1);
}
}
void gli_windows_set_paging(int forcetoend)
{
window_t *win;
for (win=gli_windowlist; win; win=win->next) {
switch (win->type) {
case wintype_TextBuffer:
win_textbuffer_set_paging(win, forcetoend);
break;
}
}
}
void gli_windows_trim_buffers()
{
window_t *win;
for (win=gli_windowlist; win; win=win->next) {
switch (win->type) {
case wintype_TextBuffer:
win_textbuffer_trim_buffer(win);
break;
}
}
}
void glk_request_char_event(window_t *win)
{
if (!win) {
gli_strict_warning(L"request_char_event: invalid ref");
return;
}
if (win->char_request || win->line_request) {
gli_strict_warning(L"request_char_event: window already has keyboard request");
return;
}
switch (win->type) {
case wintype_TextBuffer:
case wintype_TextGrid:
win->char_request = TRUE;
win->char_request_uni = FALSE;
break;
default:
gli_strict_warning(L"request_char_event: window does not support keyboard input");
break;
}
}
void glk_request_line_event(window_t *win, char *buf, glui32 maxlen,
glui32 initlen)
{
if (!win) {
gli_strict_warning(L"request_line_event: invalid ref");
return;
}
if (win->char_request || win->line_request) {
gli_strict_warning(L"request_line_event: window already has keyboard request");
return;
}
switch (win->type) {
case wintype_TextBuffer:
win->line_request = TRUE;
win->line_request_uni = FALSE;
win_textbuffer_init_line(win, buf, FALSE, maxlen, initlen);
break;
case wintype_TextGrid:
win->line_request = TRUE;
win->line_request_uni = FALSE;
win_textgrid_init_line(win, buf, FALSE, maxlen, initlen);
break;
default:
gli_strict_warning(L"request_line_event: window does not support keyboard input");
break;
}
}
#ifdef GLK_MODULE_UNICODE
void glk_request_char_event_uni(window_t *win)
{
if (!win) {
gli_strict_warning(L"request_char_event: invalid ref");
return;
}
if (win->char_request || win->line_request) {
gli_strict_warning(L"request_char_event: window already has keyboard request");
return;
}
switch (win->type) {
case wintype_TextBuffer:
case wintype_TextGrid:
win->char_request = TRUE;
win->char_request_uni = TRUE;
break;
default:
gli_strict_warning(L"request_char_event: window does not support keyboard input");
break;
}
}
void glk_request_line_event_uni(window_t *win, glui32 *buf, glui32 maxlen,
glui32 initlen)
{
if (!win) {
gli_strict_warning(L"request_line_event: invalid ref");
return;
}
if (win->char_request || win->line_request) {
gli_strict_warning(L"request_line_event: window already has keyboard request");
return;
}
switch (win->type) {
case wintype_TextBuffer:
win->line_request = TRUE;
win->line_request_uni = TRUE;
win_textbuffer_init_line(win, buf, TRUE, maxlen, initlen);
break;
case wintype_TextGrid:
win->line_request = TRUE;
win->line_request_uni = TRUE;
win_textgrid_init_line(win, buf, TRUE, maxlen, initlen);
break;
default:
gli_strict_warning(L"request_line_event: window does not support keyboard input");
break;
}
}
#endif /* GLK_MODULE_UNICODE */
void glk_request_mouse_event(window_t *win)
{
if (!win) {
gli_strict_warning(L"request_mouse_event: invalid ref");
return;
}
/* But, in fact, we can't do much about this. */
return;
}
void glk_cancel_char_event(window_t *win)
{
if (!win) {
gli_strict_warning(L"cancel_char_event: invalid ref");
return;
}
switch (win->type) {
case wintype_TextBuffer:
case wintype_TextGrid:
win->char_request = FALSE;
break;
default:
/* do nothing */
break;
}
}
void glk_cancel_line_event(window_t *win, event_t *ev)
{
event_t dummyev;
if (!ev) {
ev = &dummyev;
}
gli_event_clearevent(ev);
if (!win) {
gli_strict_warning(L"cancel_line_event: invalid ref");
return;
}
switch (win->type) {
case wintype_TextBuffer:
if (win->line_request) {
win_textbuffer_cancel_line(win, ev);
}
break;
case wintype_TextGrid:
if (win->line_request) {
win_textgrid_cancel_line(win, ev);
}
break;
default:
/* do nothing */
break;
}
}
void glk_cancel_mouse_event(window_t *win)
{
if (!win) {
gli_strict_warning(L"cancel_mouse_event: invalid ref");
return;
}
/* But, in fact, we can't do much about this. */
return;
}
void gli_window_put_char(window_t *win, glui32 ch)
{
wchar_t wc = glui32_to_wchar(ch);
/* In case the program hasn't checked gestalt for this character
* we should filter it here for a legal value.
*/
if ( gli_bad_latin_key(ch) || (ch >= 0x100 && ! iswprint(wc)) )
wc = L'?';
switch (win->type) {
case wintype_TextBuffer:
win_textbuffer_putchar(win, wc);
break;
case wintype_TextGrid:
win_textgrid_putchar(win, wc);
break;
}
}
void glk_window_clear(window_t *win)
{
if (!win) {
gli_strict_warning(L"window_clear: invalid ref");
return;
}
if (win->line_request) {
gli_strict_warning(L"window_clear: window has pending line request");
return;
}
switch (win->type) {
case wintype_TextBuffer:
win_textbuffer_clear(win);
break;
case wintype_TextGrid:
win_textgrid_clear(win);
break;
}
}
void glk_window_move_cursor(window_t *win, glui32 xpos, glui32 ypos)
{
if (!win) {
gli_strict_warning(L"window_move_cursor: invalid ref");
return;
}
switch (win->type) {
case wintype_TextGrid:
win_textgrid_move_cursor(win, xpos, ypos);
break;
default:
gli_strict_warning(L"window_move_cursor: not a TextGrid window");
break;
}
}
void gli_print_spaces(int len)
{
while (len >= NUMSPACES) {
local_addwstr(spacebuffer);
len -= NUMSPACES;
}
if (len > 0) {
local_addwstr(&(spacebuffer[NUMSPACES - len]));
}
}
#ifdef GLK_MODULE_LINE_ECHO
void glk_set_echo_line_event(window_t *win, glui32 val)
{
if (!win) {
gli_strict_warning(L"set_echo_line_event: invalid ref");
return;
}
win->echo_line_input = (val != 0);
}
#endif /* GLK_MODULE_LINE_ECHO */
#ifdef GLK_MODULE_LINE_TERMINATORS
void glk_set_terminators_line_event(window_t *win, glui32 *keycodes,
glui32 count)
{
int ix;
glui32 res, val;
if (!win) {
gli_strict_warning(L"set_terminators_line_event: invalid ref");
return;
}
/* We only allow escape and the function keys as line input terminators.
We encode those in a bitmask. */
res = 0;
if (keycodes) {
for (ix=0; ix<count; ix++) {
if (keycodes[ix] == keycode_Escape) {
res |= 0x10000;
}
else {
val = keycode_Func1 + 1 - keycodes[ix];
if (val >= 1 && val <= 12)
res |= (1 << val);
}
}
}
win->terminate_line_input = res;
}
#endif /* GLK_MODULE_LINE_TERMINATORS */
/* Keybinding functions. */
void gcmd_win_change_focus(window_t *win, glui32 arg)
{
win = gli_window_iterate_treeorder(gli_focuswin);
while (win == NULL || win->type == wintype_Pair) {
if (win == gli_focuswin)
return;
win = gli_window_iterate_treeorder(win);
}
gli_focuswin = win;
}
void gcmd_win_refresh(window_t *win, glui32 arg)
{
clear();
gli_windows_redraw();
gli_msgline_redraw();
wrefresh(curscr);
}
#ifdef GLK_MODULE_IMAGE
glui32 glk_image_draw(winid_t win, glui32 image, glsi32 val1, glsi32 val2)
{
gli_strict_warning(L"image_draw: graphics not supported.");
return FALSE;
}
glui32 glk_image_draw_scaled(winid_t win, glui32 image,
glsi32 val1, glsi32 val2, glui32 width, glui32 height)
{
gli_strict_warning(L"image_draw_scaled: graphics not supported.");
return FALSE;
}
glui32 glk_image_get_info(glui32 image, glui32 *width, glui32 *height)
{
gli_strict_warning(L"image_get_info: graphics not supported.");
return FALSE;
}
void glk_window_flow_break(winid_t win)
{
gli_strict_warning(L"window_flow_break: graphics not supported.");
}
void glk_window_erase_rect(winid_t win,
glsi32 left, glsi32 top, glui32 width, glui32 height)
{
gli_strict_warning(L"window_erase_rect: graphics not supported.");
}
void glk_window_fill_rect(winid_t win, glui32 color,
glsi32 left, glsi32 top, glui32 width, glui32 height)
{
gli_strict_warning(L"window_fill_rect: graphics not supported.");
}
void glk_window_set_background_color(winid_t win, glui32 color)
{
gli_strict_warning(L"window_set_background_color: graphics not supported.");
}
#endif /* GLK_MODULE_IMAGE */
#ifdef GLK_MODULE_HYPERLINKS
void glk_set_hyperlink(glui32 linkval)
{
gli_strict_warning(L"set_hyperlink: hyperlinks not supported.");
}
void glk_set_hyperlink_stream(strid_t str, glui32 linkval)
{
gli_strict_warning(L"set_hyperlink_stream: hyperlinks not supported.");
}
void glk_request_hyperlink_event(winid_t win)
{
gli_strict_warning(L"request_hyperlink_event: hyperlinks not supported.");
}
void glk_cancel_hyperlink_event(winid_t win)
{
gli_strict_warning(L"cancel_hyperlink_event: hyperlinks not supported.");
}
#endif /* GLK_MODULE_HYPERLINKS */
|