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
|
#include <cdk_int.h>
/*
* $Author: tom $
* $Date: 2025/01/09 00:20:21 $
* $Revision: 1.129 $
*/
/*
* Declare file local prototypes.
*/
static int createList (CDKSWINDOW *swindow, int listSize);
static void drawCDKSwindowList (CDKSWINDOW *swindow, boolean Box);
DeclareCDKObjects (SWINDOW, Swindow, setCdk, Int);
/*
* This function creates a scrolling window widget.
*/
CDKSWINDOW *newCDKSwindow (CDKSCREEN *cdkscreen,
int xplace,
int yplace,
int height,
int width,
const char *title,
int saveLines,
boolean Box,
boolean shadow)
{
/* *INDENT-EQLS* */
CDKSWINDOW *swindow = NULL;
int parentWidth = getmaxx (cdkscreen->window);
int parentHeight = getmaxy (cdkscreen->window);
int boxWidth;
int boxHeight;
int xpos = xplace;
int ypos = yplace;
int x;
/* *INDENT-OFF* */
static const struct { int from; int to; } bindings[] = {
{ CDK_BACKCHAR, KEY_PPAGE },
{ 'b', KEY_PPAGE },
{ 'B', KEY_PPAGE },
{ CDK_FORCHAR, KEY_NPAGE },
{ SPACE, KEY_NPAGE },
{ 'f', KEY_NPAGE },
{ 'F', KEY_NPAGE },
{ '|', KEY_HOME },
{ '$', KEY_END },
};
/* *INDENT-ON* */
if ((swindow = newCDKObject (CDKSWINDOW, &my_funcs)) == NULL)
return (NULL);
setCDKSwindowBox (swindow, Box);
/*
* If the height is a negative value, the height will
* be ROWS-height, otherwise, the height will be the
* given height.
*/
boxHeight = setWidgetDimension (parentHeight, height, 0);
/*
* If the width is a negative value, the width will
* be COLS-width, otherwise, the width will be the
* given width.
*/
boxWidth = setWidgetDimension (parentWidth, width, 0);
boxWidth = setCdkTitle (ObjOf (swindow), title, boxWidth);
/* Set the box height. */
boxHeight += TitleLinesOf (swindow) + 1;
/*
* Make sure we didn't extend beyond the dimensions of the window.
*/
boxWidth = (boxWidth > parentWidth ? parentWidth : boxWidth);
boxHeight = (boxHeight > parentHeight ? parentHeight : boxHeight);
/* Set the rest of the variables. */
swindow->titleAdj = TitleLinesOf (swindow) + 1;
/* Rejustify the x and y positions if we need to. */
alignxy (cdkscreen->window, &xpos, &ypos, boxWidth, boxHeight);
/* Make the scrolling window */
swindow->win = newwin (boxHeight, boxWidth, ypos, xpos);
if (swindow->win == NULL)
{
destroyCDKObject (swindow);
return (NULL);
}
keypad (swindow->win, TRUE);
/* Make the field window. */
swindow->fieldWin = subwin (swindow->win,
(boxHeight - TitleLinesOf (swindow) - 2),
boxWidth - 2,
ypos + TitleLinesOf (swindow) + 1,
xpos + 1);
keypad (swindow->fieldWin, TRUE);
/* *INDENT-EQLS* Set the rest of the variables */
ScreenOf (swindow) = cdkscreen;
swindow->parent = cdkscreen->window;
swindow->shadowWin = NULL;
swindow->boxHeight = boxHeight;
swindow->boxWidth = boxWidth;
swindow->viewSize = boxHeight - TitleLinesOf (swindow) - 2;
swindow->currentTop = 0;
swindow->maxTopLine = 0;
swindow->leftChar = 0;
swindow->maxLeftChar = 0;
swindow->listSize = 0;
swindow->widestLine = -1;
swindow->saveLines = saveLines;
initExitType (swindow);
ObjOf (swindow)->acceptsFocus = TRUE;
ObjOf (swindow)->inputWindow = swindow->win;
swindow->shadow = shadow;
if (!createList (swindow, saveLines))
{
destroyCDKObject (swindow);
return (NULL);
}
/* Do we need to create a shadow??? */
if (shadow)
{
swindow->shadowWin = newwin (boxHeight, boxWidth, ypos + 1, xpos + 1);
}
/* Clean the key bindings. */
for (x = 0; x < (int)SIZEOF (bindings); ++x)
bindCDKObject (vSWINDOW,
swindow,
(chtype)bindings[x].from,
getcCDKBind,
(void *)(long)bindings[x].to);
/* Register this baby. */
registerCDKObject (cdkscreen, vSWINDOW, swindow);
/* Return the scrolling window */
return (swindow);
}
/*
* This sets the lines and the box attribute of the scrolling window.
*/
void setCDKSwindow (CDKSWINDOW *swindow, CDK_CSTRING2 list, int lines, boolean Box)
{
setCDKSwindowContents (swindow, list, lines);
setCDKSwindowBox (swindow, Box);
}
static void setupLine (CDKSWINDOW *swindow, const char *list, int x)
{
/* *INDENT-EQLS* */
swindow->list[x] = char2Chtype (list,
&swindow->listLen[x],
&swindow->listPos[x]);
swindow->listPos[x] = justifyString (swindow->boxWidth,
swindow->listLen[x],
swindow->listPos[x]);
swindow->widestLine = MAXIMUM (swindow->widestLine, swindow->listLen[x]);
}
/*
* This sets all the lines inside the scrolling window.
*/
void setCDKSwindowContents (CDKSWINDOW *swindow, CDK_CSTRING2 list, int listSize)
{
int x = 0;
/* First lets clean all the lines in the window. */
cleanCDKSwindow (swindow);
createList (swindow, listSize);
/* Now lets set all the lines inside the window. */
for (x = 0; x < listSize; x++)
{
setupLine (swindow, list[x], x);
}
/* *INDENT-EQLS* Set some of the more important members of the scrolling window. */
swindow->listSize = listSize;
swindow->maxTopLine = swindow->listSize - swindow->viewSize;
swindow->maxTopLine = (swindow->maxTopLine < 0 ? 0 : swindow->maxTopLine);
swindow->maxLeftChar = swindow->widestLine - (swindow->boxWidth - 2);
swindow->currentTop = 0;
swindow->leftChar = 0;
}
chtype **getCDKSwindowContents (CDKSWINDOW *swindow, int *size)
{
(*size) = swindow->listSize;
return swindow->list;
}
/*
* This sets the box attribute for the widget.
*/
void setCDKSwindowBox (CDKSWINDOW *swindow, boolean Box)
{
ObjOf (swindow)->box = Box;
ObjOf (swindow)->borderSize = Box ? 1 : 0;
}
boolean getCDKSwindowBox (CDKSWINDOW *swindow)
{
return ObjOf (swindow)->box;
}
static void freeLine (CDKSWINDOW *swindow, int x)
{
if (x < swindow->listSize)
{
freeChtype (swindow->list[x]);
swindow->list[x] = NULL;
}
}
/*
* This adds a line to the scrolling window.
*/
void addCDKSwindow (CDKSWINDOW *swindow, const char *list, int insertPos)
{
int x = 0;
/*
* If we are at the maximum number of save lines. Erase
* the first position and bump everything up one spot.
*/
if (swindow->listSize == swindow->saveLines)
{
/* Free up the memory. */
freeLine (swindow, 0);
/* Bump everything up one spot. */
for (x = 0; x < swindow->listSize; x++)
{
/* *INDENT-EQLS* */
swindow->list[x] = swindow->list[x + 1];
swindow->listPos[x] = swindow->listPos[x + 1];
swindow->listLen[x] = swindow->listLen[x + 1];
}
/* *INDENT-EQLS* Clean out the last position. */
swindow->list[swindow->listSize] = NULL;
swindow->listLen[swindow->listSize] = 0;
swindow->listPos[swindow->listSize] = 0;
swindow->listSize--;
}
/* Determine where the line is being added. */
if (insertPos == TOP)
{
/* We need to 'bump' everything down one line... */
for (x = swindow->listSize; x > 0; x--)
{
/* *INDENT-EQLS* Copy in the new row. */
swindow->list[x] = swindow->list[x - 1];
swindow->listPos[x] = swindow->listPos[x - 1];
swindow->listLen[x] = swindow->listLen[x - 1];
}
/* Add it into the scrolling window. */
setupLine (swindow, list, 0);
/* Set some variables. */
swindow->currentTop = 0;
if (swindow->listSize < swindow->saveLines)
{
swindow->listSize++;
}
/* Set the maximum top line. */
swindow->maxTopLine = swindow->listSize - swindow->viewSize;
swindow->maxTopLine = (swindow->maxTopLine < 0 ? 0 : swindow->maxTopLine);
swindow->maxLeftChar = swindow->widestLine - (swindow->boxWidth - 2);
}
else
{
/* Add to the bottom. */
setupLine (swindow, list, swindow->listSize);
swindow->maxLeftChar = swindow->widestLine - (swindow->boxWidth - 2);
/* Increment the item count and zero out the next row. */
if (swindow->listSize < swindow->saveLines)
{
swindow->listSize++;
freeLine (swindow, swindow->listSize);
}
/* Set the maximum top line. */
if (swindow->listSize <= swindow->viewSize)
{
swindow->maxTopLine = 0;
swindow->currentTop = 0;
}
else
{
swindow->maxTopLine = (swindow->listSize - swindow->viewSize);
swindow->currentTop = swindow->maxTopLine;
}
}
/* Draw in the list. */
drawCDKSwindowList (swindow, ObjOf (swindow)->box);
}
/*
* This jumps to a given line.
*/
void jumpToLineCDKSwindow (CDKSWINDOW *swindow, int line)
{
/*
* Make sure the line is in bounds.
*/
if (line == BOTTOM || line >= swindow->listSize)
{
/* We are moving to the last page. */
swindow->currentTop = swindow->listSize - swindow->viewSize;
}
else if (line == TOP || line <= 0)
{
/* We are moving to the top of the page. */
swindow->currentTop = 0;
}
else
{
/* We are moving in the middle somewhere. */
if ((swindow->viewSize + line) < swindow->listSize)
{
swindow->currentTop = line;
}
else
{
swindow->currentTop = swindow->listSize - swindow->viewSize;
}
}
/* A little sanity check to make we don't something silly. */
if (swindow->currentTop < 0)
{
swindow->currentTop = 0;
}
/* Redraw the window. */
drawCDKSwindow (swindow, ObjOf (swindow)->box);
}
/*
* This removes all the lines inside the scrolling window.
*/
void cleanCDKSwindow (CDKSWINDOW *swindow)
{
int x;
/* Clean up the memory used ... */
for (x = 0; x < swindow->listSize; x++)
{
freeLine (swindow, x);
}
/* *INDENT-EQLS* Reset some variables. */
swindow->listSize = 0;
swindow->maxLeftChar = 0;
swindow->widestLine = 0;
swindow->currentTop = 0;
swindow->maxTopLine = 0;
/* Redraw the window. */
drawCDKSwindow (swindow, ObjOf (swindow)->box);
}
/*
* This trims lines from the scrolling window.
*/
void trimCDKSwindow (CDKSWINDOW *swindow, int begin, int end)
{
int start, finish, lines, x;
/*
* Do nothing if the list is empty, the interval is empty,
* or the entire interval lies outside of the list.
*/
if ((swindow->listSize == 0) ||
(begin > end) ||
(begin < 0 && end < 0) ||
(begin >= swindow->listSize && end >= swindow->listSize))
{
return;
}
/* Check the value of begin. */
if (begin < 0)
{
start = 0;
}
else if (begin >= swindow->listSize)
{
start = swindow->listSize - 1;
}
else
{
start = begin;
}
/* Check the value of end. */
if (end < 0)
{
finish = 0;
}
else if (end >= swindow->listSize)
{
finish = swindow->listSize - 1;
}
else
{
finish = end;
}
lines = finish - start + 1;
/* Start nuking elements from the window. */
for (x = start; x <= finish; x++)
{
freeLine (swindow, x);
}
/* Move the lines after the trimmed lines up. */
for (x = start; x < swindow->listSize - lines; x++)
{
/* Move the line up. */
swindow->list[x] = swindow->list[x + lines];
swindow->listPos[x] = swindow->listPos[x + lines];
swindow->listLen[x] = swindow->listLen[x + lines];
/* Zero out the moved line's original entries. */
swindow->list[x + lines] = NULL;
swindow->listPos[x + lines] = 0;
swindow->listLen[x + lines] = 0;
}
/* Adjust the item count correctly. */
swindow->listSize = swindow->listSize - lines;
/* Set the maximum top line. */
if (swindow->listSize <= swindow->viewSize)
{
swindow->maxTopLine = 0;
}
else
{
swindow->maxTopLine = (swindow->listSize - swindow->viewSize);
}
/* Set the current top line, but only if it is no longer valid. */
if (swindow->currentTop > swindow->maxTopLine)
{
swindow->currentTop = swindow->maxTopLine;
}
/* Redraw the list. */
drawCDKSwindowList (swindow, ObjOf (swindow)->box);
}
/*
* This allows the user to play inside the scrolling window.
*/
void activateCDKSwindow (CDKSWINDOW *swindow, chtype *actions)
{
/* Draw the scrolling list */
drawCDKSwindow (swindow, ObjOf (swindow)->box);
if (actions == NULL)
{
boolean functionKey;
for (;;)
{
chtype input = (chtype)getchCDKObject (ObjOf (swindow), &functionKey);
/* Inject the character into the widget. */
(void)injectCDKSwindow (swindow, input);
if (swindow->exitType != vEARLY_EXIT)
{
return;
}
}
}
else
{
int length = chlen (actions);
int x = 0;
/* Inject each character one at a time. */
for (x = 0; x < length; x++)
{
(void)injectCDKSwindow (swindow, actions[x]);
if (swindow->exitType != vEARLY_EXIT)
{
return;
}
}
}
/* Set the exit type and return. */
setExitType (swindow, 0);
return;
}
/*
* This injects a single character into the widget.
*/
static int _injectCDKSwindow (CDKOBJS *object, chtype input)
{
/* *INDENT-EQLS* */
CDKSWINDOW *widget = (CDKSWINDOW *)object;
int ppReturn = 1;
int ret = unknownInt;
bool complete = FALSE;
/* Set the exit type. */
setExitType (widget, 0);
/* Draw the window.... */
drawCDKSwindow (widget, ObjOf (widget)->box);
/* Check if there is a pre-process function to be called. */
if (PreProcessFuncOf (widget) != NULL)
{
/* Call the pre-process function. */
ppReturn = PreProcessFuncOf (widget) (vSWINDOW,
widget,
PreProcessDataOf (widget),
input);
}
/* Should we continue? */
if (ppReturn != 0)
{
/* Check for a key binding. */
if (checkCDKObjectBind (vSWINDOW, widget, input) != 0)
{
checkEarlyExit (widget);
complete = TRUE;
}
else
{
switch (input)
{
case KEY_UP:
if (widget->currentTop > 0)
{
widget->currentTop--;
}
else
{
Beep ();
}
break;
case KEY_DOWN:
if (widget->currentTop >= 0 && widget->currentTop < widget->maxTopLine)
{
widget->currentTop++;
}
else
{
Beep ();
}
break;
case KEY_RIGHT:
if (widget->leftChar < widget->maxLeftChar)
{
widget->leftChar++;
}
else
{
Beep ();
}
break;
case KEY_LEFT:
if (widget->leftChar > 0)
{
widget->leftChar--;
}
else
{
Beep ();
}
break;
case KEY_PPAGE:
if (widget->currentTop != 0)
{
if (widget->currentTop >= widget->viewSize)
{
widget->currentTop = (widget->currentTop
- (widget->viewSize - 1));
}
else
{
widget->currentTop = 0;
}
}
else
{
Beep ();
}
break;
case KEY_NPAGE:
if (widget->currentTop != widget->maxTopLine)
{
if ((widget->currentTop + widget->viewSize) < widget->maxTopLine)
{
widget->currentTop = (widget->currentTop
+ (widget->viewSize - 1));
}
else
{
widget->currentTop = widget->maxTopLine;
}
}
else
{
Beep ();
}
break;
case KEY_HOME:
widget->leftChar = 0;
break;
case KEY_END:
widget->leftChar = widget->maxLeftChar + 1;
break;
case 'g':
case '1':
case '<':
widget->currentTop = 0;
break;
case 'G':
case '>':
widget->currentTop = widget->maxTopLine;
break;
case 'l':
case 'L':
loadCDKSwindowInformation (widget);
break;
case 's':
case 'S':
saveCDKSwindowInformation (widget);
break;
case KEY_TAB:
case KEY_ENTER:
setExitType (widget, input);
ret = 1;
complete = TRUE;
break;
case KEY_ESC:
setExitType (widget, input);
complete = TRUE;
break;
case KEY_ERROR:
setExitType (widget, input);
complete = TRUE;
break;
case CDK_REFRESH:
eraseCDKScreen (ScreenOf (widget));
refreshCDKScreen (ScreenOf (widget));
break;
default:
break;
}
}
/* Should we call a post-process? */
if (!complete && (PostProcessFuncOf (widget) != NULL))
{
PostProcessFuncOf (widget) (vSWINDOW,
widget,
PostProcessDataOf (widget),
input);
}
}
if (!complete)
{
drawCDKSwindowList (widget, ObjOf (widget)->box);
setExitType (widget, 0);
}
ResultOf (widget).valueInt = ret;
return (ret != unknownInt);
}
/*
* This moves the swindow field to the given location.
*/
static void _moveCDKSwindow (CDKOBJS *object,
int xplace,
int yplace,
boolean relative,
boolean refresh_flag)
{
CDKSWINDOW *swindow = (CDKSWINDOW *)object;
/* *INDENT-EQLS* */
int currentX = getbegx (swindow->win);
int currentY = getbegy (swindow->win);
int xpos = xplace;
int ypos = yplace;
int xdiff = 0;
int ydiff = 0;
/*
* If this is a relative move, then we will adjust where we want
* to move to.
*/
if (relative)
{
xpos = getbegx (swindow->win) + xplace;
ypos = getbegy (swindow->win) + yplace;
}
/* Adjust the window if we need to. */
alignxy (WindowOf (swindow), &xpos, &ypos, swindow->boxWidth, swindow->boxHeight);
/* Get the difference. */
xdiff = currentX - xpos;
ydiff = currentY - ypos;
/* Move the window to the new location. */
moveCursesWindow (swindow->win, -xdiff, -ydiff);
moveCursesWindow (swindow->shadowWin, -xdiff, -ydiff);
/* Touch the windows so they 'move'. */
refreshCDKWindow (WindowOf (swindow));
/* Redraw the window, if they asked for it. */
if (refresh_flag)
{
drawCDKSwindow (swindow, ObjOf (swindow)->box);
}
}
/*
* This function draws the swindow window widget.
*/
static void _drawCDKSwindow (CDKOBJS *object, boolean Box)
{
CDKSWINDOW *swindow = (CDKSWINDOW *)object;
/* Do we need to draw in the shadow. */
if (swindow->shadowWin != NULL)
{
drawShadow (swindow->shadowWin);
}
/* Box the widget if needed */
if (Box)
{
drawObjBox (swindow->win, ObjOf (swindow));
}
drawCdkTitle (swindow->win, object);
wrefresh (swindow->win);
/* Draw in the list. */
drawCDKSwindowList (swindow, Box);
}
/*
* This draws in the contents of the scrolling window.
*/
static void drawCDKSwindowList (CDKSWINDOW *swindow, boolean Box GCC_UNUSED)
{
int lastLine, x;
/* Determine the last line to draw. */
if (swindow->listSize < swindow->viewSize)
{
lastLine = swindow->listSize;
}
else
{
lastLine = swindow->viewSize;
}
/* Erase the scrolling window. */
werase (swindow->fieldWin);
/* Start drawing in each line. */
for (x = 0; x < lastLine; x++)
{
int screenPos;
if ((x + swindow->currentTop) >= swindow->listSize)
break;
screenPos = swindow->listPos[x + swindow->currentTop] - swindow->leftChar;
/* Write in the correct line. */
if (screenPos >= 0)
{
writeChtype (swindow->fieldWin, screenPos, x,
swindow->list[x + swindow->currentTop],
HORIZONTAL, 0,
swindow->listLen[x + swindow->currentTop]);
}
else
{
writeChtype (swindow->fieldWin, 0, x,
swindow->list[x + swindow->currentTop],
HORIZONTAL,
swindow->leftChar - swindow->listPos[x + swindow->currentTop],
swindow->listLen[x + swindow->currentTop]);
}
}
wrefresh (swindow->fieldWin);
}
/*
* This sets the background attribute of the widget.
*/
static void _setBKattrSwindow (CDKOBJS *object, chtype attrib)
{
if (object != NULL)
{
CDKSWINDOW *widget = (CDKSWINDOW *)object;
wbkgd (widget->win, attrib);
wbkgd (widget->fieldWin, attrib);
}
}
/*
* Free any storage associated with the info-list.
*/
static void destroyInfo (CDKSWINDOW *swindow)
{
CDKfreeChtypes (swindow->list);
freeChecked (swindow->listPos);
freeChecked (swindow->listLen);
swindow->list = NULL;
swindow->listPos = NULL;
swindow->listLen = NULL;
}
/*
* This function destroys the scrolling window widget.
*/
static void _destroyCDKSwindow (CDKOBJS *object)
{
if (object != NULL)
{
CDKSWINDOW *swindow = (CDKSWINDOW *)object;
destroyInfo (swindow);
cleanCdkTitle (object);
/* Delete the windows. */
deleteCursesWindow (swindow->shadowWin);
deleteCursesWindow (swindow->fieldWin);
deleteCursesWindow (swindow->win);
/* Clean the key bindings. */
cleanCDKObjectBindings (vSWINDOW, swindow);
/* Unregister this object. */
unregisterCDKObject (vSWINDOW, swindow);
}
}
/*
* This function erases the scrolling window widget.
*/
static void _eraseCDKSwindow (CDKOBJS *object)
{
if (validCDKObject (object))
{
CDKSWINDOW *swindow = (CDKSWINDOW *)object;
eraseCursesWindow (swindow->win);
eraseCursesWindow (swindow->shadowWin);
}
}
/*
* This exec's a command and redirects the output to the scrolling window.
*/
int execCDKSwindow (CDKSWINDOW *swindow, const char *command, int insertPos)
{
FILE *ps;
int count = -1;
endwin ();
/* Try to open the command. */
if ((ps = popen (command, "r")) != NULL)
{
char temp[BUFSIZ];
/* Start reading. */
while (fgets (temp, sizeof (temp), ps) != NULL)
{
size_t len = strlen (temp);
if (len != 0 && temp[len - 1] == '\n')
temp[--len] = '\0';
/* Add the line to the scrolling window. */
addCDKSwindow (swindow, temp, insertPos);
count++;
}
/* Close the pipe. */
pclose (ps);
}
return count;
}
static void showMessage2 (CDKSWINDOW *swindow,
const char *msg,
const char *msg2,
const char *filename)
{
char *mesg[10];
char *temp = (char *)malloc (80 + strlen (filename));
int n = 0;
mesg[n++] = copyChar (msg);
mesg[n++] = copyChar (msg2);
sprintf (temp, "<C>(%s)", filename);
mesg[n++] = copyChar (temp);
mesg[n++] = copyChar (" ");
mesg[n++] = copyChar ("<C>Press any key to continue.");
popupLabel (ScreenOf (swindow), (CDK_CSTRING2)mesg, n);
freeCharList (mesg, (unsigned)n);
free (temp);
}
/*
* This function allows the user to dump the information from the
* scrolling window to a file.
*/
void saveCDKSwindowInformation (CDKSWINDOW *swindow)
{
CDKENTRY *entry = NULL;
char *filename = NULL;
int linesSaved;
/* Create the entry field to get the filename. */
entry = newCDKEntry (ScreenOf (swindow), CENTER, CENTER,
"<C></B/5>Enter the filename of the save file.",
"Filename: ",
A_NORMAL, '_', vMIXED,
20, 1, 256,
TRUE, FALSE);
/* Get the filename. */
filename = activateCDKEntry (entry, NULL);
/* Did they hit escape? */
if (entry->exitType == vESCAPE_HIT)
{
const char *mesg[10];
/* Popup a message. */
mesg[0] = "<C></B/5>Save Canceled.";
mesg[1] = "<C>Escape hit. Scrolling window information not saved.";
mesg[2] = " ";
mesg[3] = "<C>Press any key to continue.";
popupLabel (ScreenOf (swindow), (CDK_CSTRING2)mesg, 4);
/* Clean up and exit. */
destroyCDKEntry (entry);
return;
}
/* Write the contents of the scrolling window to the file. */
linesSaved = dumpCDKSwindow (swindow, filename);
/* Was the save successful? */
if (linesSaved == -1)
{
/* Nope, tell 'em. */
showMessage2 (swindow,
"<C></B/16>Error",
"<C>Could not save to the file.",
filename);
}
else
{
char temp[256];
/* Yep, let them know how many lines were saved. */
sprintf (temp, "<C>There were %d lines saved to the file", linesSaved);
showMessage2 (swindow,
"<C></B/5>Save Successful",
temp,
filename);
}
/* Clean up and exit. */
destroyCDKEntry (entry);
eraseCDKScreen (ScreenOf (swindow));
drawCDKScreen (ScreenOf (swindow));
}
/*
* This function allows the user to load new information into the scrolling
* window.
*/
void loadCDKSwindowInformation (CDKSWINDOW *swindow)
{
/* *INDENT-EQLS* */
CDKFSELECT *fselect = NULL;
char *filename = NULL;
const char *mesg[15];
char **fileInfo = NULL;
int lines;
/* Create the file selector to choose the file. */
fselect = newCDKFselect (ScreenOf (swindow), CENTER, CENTER, 20, 55,
"<C>Load Which File",
"Filename",
A_NORMAL, '.',
A_REVERSE,
"</5>", "</48>", "</N>", "</N>",
TRUE, FALSE);
/* Get the filename to load. */
(void)activateCDKFselect (fselect, NULL);
/* Make sure they selected a file. */
if (fselect->exitType == vESCAPE_HIT)
{
/* Popup a message. */
mesg[0] = "<C></B/5>Load Canceled.";
mesg[1] = " ";
mesg[2] = "<C>Press any key to continue.";
popupLabel (ScreenOf (swindow), (CDK_CSTRING2)mesg, 3);
/* Clean up and exit. */
destroyCDKFselect (fselect);
return;
}
/* Copy the filename and destroy the file selector. */
filename = copyChar (fselect->pathname);
destroyCDKFselect (fselect);
/*
* Maye we should check before nuking all the information
* in the scrolling window...
*/
if (swindow->listSize > 0)
{
CDKDIALOG *dialog = NULL;
const char *button[5];
int answer;
/* Create the dialog message. */
mesg[0] = "<C></B/5>Save Information First";
mesg[1] = "<C>There is information in the scrolling window.";
mesg[2] = "<C>Do you want to save it to a file first?";
button[0] = "(Yes)";
button[1] = "(No)";
/* Create the dialog widget. */
dialog = newCDKDialog (ScreenOf (swindow), CENTER, CENTER,
(CDK_CSTRING2)mesg, 3,
(CDK_CSTRING2)button, 2,
COLOR_PAIR (2) | A_REVERSE,
TRUE, TRUE, FALSE);
/* Activate the widget. */
answer = activateCDKDialog (dialog, NULL);
destroyCDKDialog (dialog);
/* Check the answer. */
if (answer == -1 || answer == 0)
{
/* Save the information. */
saveCDKSwindowInformation (swindow);
}
}
/* Open the file and read it in. */
lines = CDKreadFile (filename, &fileInfo);
if (lines == -1)
{
/* The file read didn't work. */
showMessage2 (swindow,
"<C></B/16>Error",
"<C>Could not read the file",
filename);
freeChar (filename);
return;
}
/* Clean out the scrolling window. */
cleanCDKSwindow (swindow);
/* Set the new information in the scrolling window. */
setCDKSwindow (swindow, (CDK_CSTRING2)fileInfo, lines, ObjOf (swindow)->box);
/* Clean up. */
CDKfreeStrings (fileInfo);
freeChar (filename);
}
/*
* This actually dumps the information from the scrolling window to a
* file.
*/
int dumpCDKSwindow (CDKSWINDOW *swindow, const char *filename)
{
FILE *outputFile = NULL;
int x;
/* Try to open the file. */
if ((outputFile = CDKopenFile (filename, "w")) == NULL)
{
return -1;
}
/* Start writing out the file. */
for (x = 0; x < swindow->listSize; x++)
{
char *rawLine = chtype2Char (swindow->list[x]);
fprintf (outputFile, "%s\n", rawLine);
freeChar (rawLine);
}
/* Close the file and return the number of lines written. */
fclose (outputFile);
return swindow->listSize;
}
static void _focusCDKSwindow (CDKOBJS *object)
{
CDKSWINDOW *widget = (CDKSWINDOW *)object;
drawCDKSwindow (widget, ObjOf (widget)->box);
}
static void _unfocusCDKSwindow (CDKOBJS *object)
{
CDKSWINDOW *widget = (CDKSWINDOW *)object;
drawCDKSwindow (widget, ObjOf (widget)->box);
}
static int createList (CDKSWINDOW *swindow, int listSize)
{
int status = 0;
if (listSize >= 0)
{
chtype **newList = typeCallocN (chtype *, listSize + 1);
int *newPos = typeCallocN (int, listSize + 1);
int *newLen = typeCallocN (int, listSize + 1);
if (newList != NULL
&& newPos != NULL
&& newLen != NULL)
{
status = 1;
destroyInfo (swindow);
/* *INDENT-EQLS* */
swindow->list = newList;
swindow->listPos = newPos;
swindow->listLen = newLen;
}
if (!status)
{
CDKfreeChtypes (newList);
freeChecked (newPos);
freeChecked (newLen);
}
}
else
{
destroyInfo (swindow);
status = 1;
}
return status;
}
dummyRefreshData (Swindow)
dummySaveData (Swindow)
|