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 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
|
/*
* This is a curses forms wrapper as part of ncurses-ruby
* Contributed by Simon Kaczor <skaczor@cox.net>
* Prognosoft Inc. <http://www.prognosoft.biz>
* Copyright 2004
*
* Changes:
* (C) 2004 Tobias Peters
* (C) 2005 2009 Tobias Herzke
* (C) 2013 Gaute Hope <eg@gaute.vetsj.com>
*
* This module is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This module is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this module; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Changes
* (C) 2011 Tobias Herzke
*/
#if defined(HAVE_FORM_H) || defined(HAVE_NCURSESW_FORM_H)
# include <wchar.h>
#include "form_wrap.h"
#include "ncurses_wrap.h"
#include "compat.h"
VALUE mForm;
VALUE cFIELD;
VALUE cFIELDTYPE;
VALUE cFORM;
void init_err_codes()
{
/* The routine succeeded. */
FORM_DEF_CONST(E_OK);
/* The field is already connected to a form. */
FORM_DEF_CONST(E_CONNECTED);
/* System error occurred (see errno). */
FORM_DEF_CONST(E_SYSTEM_ERROR);
/* Routine detected an incorrect or out-of-range argument. */
FORM_DEF_CONST(E_BAD_ARGUMENT);
/* The form is already posted. */
FORM_DEF_CONST(E_POSTED);
/* Routine was called from an initialization or termination function. */
FORM_DEF_CONST(E_BAD_STATE);
/* Form is too large for its window. */
FORM_DEF_CONST(E_NO_ROOM);
/* The form has not been posted. */
FORM_DEF_CONST(E_NOT_POSTED);
/* The form driver code saw an unknown request code. */
FORM_DEF_CONST(E_UNKNOWN_COMMAND);
/* Contents of a field are not valid. */
FORM_DEF_CONST(E_INVALID_FIELD);
/* No fields are connected to the form. */
FORM_DEF_CONST(E_NOT_CONNECTED);
/* The form driver could not process the request.} */
FORM_DEF_CONST(E_REQUEST_DENIED);
}
/*
* Form driver request characters listed in form_driver(3x) man page
*/
void init_req_constants() {
/* Move to the next page */
FORM_DEF_CONST(REQ_NEXT_PAGE);
/* Move to the previous page. */
FORM_DEF_CONST(REQ_PREV_PAGE);
/* Move to the first page. */
FORM_DEF_CONST(REQ_FIRST_PAGE);
/* Move to the last field. */
FORM_DEF_CONST(REQ_LAST_PAGE);
/* Move to the next field. */
FORM_DEF_CONST(REQ_NEXT_FIELD);
/* Move to the previous field. */
FORM_DEF_CONST(REQ_PREV_FIELD);
/* Move to the first field. */
FORM_DEF_CONST(REQ_FIRST_FIELD);
/* Move to the last field. */
FORM_DEF_CONST(REQ_LAST_FIELD);
/* Move to the sorted next field. */
FORM_DEF_CONST(REQ_SNEXT_FIELD);
/* Move to the sorted previous field. */
FORM_DEF_CONST(REQ_SPREV_FIELD);
/* Move to the sorted first field. */
FORM_DEF_CONST(REQ_SFIRST_FIELD);
/* Move to the sorted last field. */
FORM_DEF_CONST(REQ_SLAST_FIELD);
/* Move left to a field. */
FORM_DEF_CONST(REQ_LEFT_FIELD);
/* Move right to a field. */
FORM_DEF_CONST(REQ_RIGHT_FIELD);
/* Move up to a field. */
FORM_DEF_CONST(REQ_UP_FIELD);
/* Move down to a field. */
FORM_DEF_CONST(REQ_DOWN_FIELD);
/* Move to the next char. */
FORM_DEF_CONST(REQ_NEXT_CHAR);
/* Move to the previous char. */
FORM_DEF_CONST(REQ_PREV_CHAR);
/* Move to the next line. */
FORM_DEF_CONST(REQ_NEXT_LINE);
/* Move to the previous line. */
FORM_DEF_CONST(REQ_PREV_LINE);
/* Move to the next word. */
FORM_DEF_CONST(REQ_NEXT_WORD);
/* Move to the previous word. */
FORM_DEF_CONST(REQ_PREV_WORD);
/* Move to the beginning of the field. */
FORM_DEF_CONST(REQ_BEG_FIELD);
/* Move to the end of the field. */
FORM_DEF_CONST(REQ_END_FIELD);
/* Move to the beginning of the line. */
FORM_DEF_CONST(REQ_BEG_LINE);
/* Move to the end of the line. */
FORM_DEF_CONST(REQ_END_LINE);
/* Move left in the field. */
FORM_DEF_CONST(REQ_LEFT_CHAR);
/* Move right in the field. */
FORM_DEF_CONST(REQ_RIGHT_CHAR);
/* Move up in the field. */
FORM_DEF_CONST(REQ_UP_CHAR);
/* Move down in the field. */
FORM_DEF_CONST(REQ_DOWN_CHAR);
/* Insert or overlay a new line. */
FORM_DEF_CONST(REQ_NEW_LINE);
/* Insert a blank at the cursor. */
FORM_DEF_CONST(REQ_INS_CHAR);
/* Insert a blank line at the cursor. */
FORM_DEF_CONST(REQ_INS_LINE);
/* Delete character at the cursor. */
FORM_DEF_CONST(REQ_DEL_CHAR);
/* Delete character before the cursor. */
FORM_DEF_CONST(REQ_DEL_PREV);
/* Delete line at the cursor. */
FORM_DEF_CONST(REQ_DEL_LINE);
/* Delete blank-delimited word at the cursor. */
FORM_DEF_CONST(REQ_DEL_WORD);
/* Clear to end of line from cursor. */
FORM_DEF_CONST(REQ_CLR_EOL);
/* Clear to end of field from cursor. */
FORM_DEF_CONST(REQ_CLR_EOF);
/* Clear the entire field. */
FORM_DEF_CONST(REQ_CLR_FIELD);
/* Enter overlay mode. */
FORM_DEF_CONST(REQ_OVL_MODE);
/* Enter insert mode. */
FORM_DEF_CONST(REQ_INS_MODE);
/* Scroll the field forward a line. */
FORM_DEF_CONST(REQ_SCR_FLINE);
/* Scroll the field backward a line. */
FORM_DEF_CONST(REQ_SCR_BLINE);
/* Scroll the field forward a page. */
FORM_DEF_CONST(REQ_SCR_FPAGE);
/* Scroll the field backward a page. */
FORM_DEF_CONST(REQ_SCR_BPAGE);
/* Scroll the field forward half a page. */
FORM_DEF_CONST(REQ_SCR_FHPAGE);
/* Scroll the field backward half a page. */
FORM_DEF_CONST(REQ_SCR_BHPAGE);
/* Scroll the field forward a character. */
FORM_DEF_CONST(REQ_SCR_FCHAR);
/* Scroll the field backward a character. */
FORM_DEF_CONST(REQ_SCR_BCHAR);
/* Horizontal scroll the field forward a line. */
FORM_DEF_CONST(REQ_SCR_HFLINE);
/* Horizontal scroll the field backward a line. */
FORM_DEF_CONST(REQ_SCR_HBLINE);
/* Horizontal scroll the field forward half a line. */
FORM_DEF_CONST(REQ_SCR_HFHALF);
/* Horizontal scroll the field backward half a line. */
FORM_DEF_CONST(REQ_SCR_HBHALF);
/* Validate field. */
FORM_DEF_CONST(REQ_VALIDATION);
/* Display next field choice. */
FORM_DEF_CONST(REQ_NEXT_CHOICE);
/* Display previous field choice. */
FORM_DEF_CONST(REQ_PREV_CHOICE);
}
/*
* field justification constants
*/
void init_just_constants() {
FORM_DEF_CONST(NO_JUSTIFICATION);
FORM_DEF_CONST(JUSTIFY_RIGHT);
FORM_DEF_CONST(JUSTIFY_LEFT);
FORM_DEF_CONST(JUSTIFY_CENTER);
}
/*
* field options constants
*/
void init_opts_constants() {
FORM_DEF_CONST(O_VISIBLE);
FORM_DEF_CONST(O_ACTIVE);
FORM_DEF_CONST(O_PUBLIC);
FORM_DEF_CONST(O_EDIT);
FORM_DEF_CONST(O_WRAP);
FORM_DEF_CONST(O_BLANK);
FORM_DEF_CONST(O_AUTOSKIP);
FORM_DEF_CONST(O_NULLOK);
FORM_DEF_CONST(O_STATIC);
FORM_DEF_CONST(O_PASSOK);
}
void init_form_opts_constants() {
FORM_DEF_CONST(O_NL_OVERLOAD);
FORM_DEF_CONST(O_BS_OVERLOAD);
}
/*
* _PAGE wrapper
*/
/* static VALUE wrap_page(_PAGE* page) */
/* { */
/* if (page == 0) return Qnil; */
/* { */
/* VALUE pages_hash = rb_iv_get(mForm, "@pages_hash"); */
/* VALUE page_adress = INT2NUM((long)(page)); */
/* VALUE rb_page = rb_hash_aref(pages_hash, page_adress); */
/* if (rb_page == Qnil) { */
/* rb_page = Data_Wrap_Struct(cPAGE, 0, 0, page); */
/* rb_iv_set(rb_page, "@destroyed", Qfalse); */
/* rb_hash_aset(pages_hash, page_adress, rb_page); */
/* } */
/* return rb_page; */
/* } */
/* } */
/* static _PAGE* get_page(VALUE rb_page) */
/* { */
/* _PAGE* page; */
/* if (rb_page == Qnil) return 0; */
/* if (rb_iv_get(rb_page, "@destroyed") == Qtrue) { */
/* rb_raise(rb_eRuntimeError, "Attempt to access a destroyed page"); */
/* return 0; */
/* } */
/* Data_Get_Struct(rb_page, _PAGE, page); */
/* return page; */
/* } */
/*
* FIELD wrapper
*/
static VALUE wrap_field(FIELD* field)
{
if (field == 0) return Qnil;
{
VALUE fields_hash = rb_iv_get(mForm, "@fields_hash");
VALUE field_adress = INT2NUM((long)(field));
VALUE rb_field = rb_hash_aref(fields_hash, field_adress);
if (rb_field == Qnil) {
rb_field = Data_Wrap_Struct(cFIELD, 0, 0, field);
rb_iv_set(rb_field, "@destroyed", Qfalse);
rb_hash_aset(fields_hash, field_adress, rb_field);
}
return rb_field;
}
}
static FIELD* get_field(VALUE rb_field)
{
FIELD* field;
if (rb_field == Qnil) return 0;
if (rb_iv_get(rb_field, "@destroyed") == Qtrue) {
rb_raise(rb_eRuntimeError, "Attempt to access a destroyed field");
return 0;
}
Data_Get_Struct(rb_field, FIELD, field);
return field;
}
/*
* FIELDTYPE wrapper
*/
static VALUE wrap_fieldtype(FIELDTYPE* fieldtype)
{
if (fieldtype == NULL) return Qnil;
{
VALUE fieldtypes_hash = rb_iv_get(mForm, "@fieldtypes_hash");
VALUE fieldtype_adress = INT2NUM((long)(fieldtype));
VALUE rb_fieldtype = rb_hash_aref(fieldtypes_hash, fieldtype_adress);
if (rb_fieldtype == Qnil) {
rb_fieldtype = Data_Wrap_Struct(cFIELDTYPE, 0, 0, fieldtype);
rb_iv_set(rb_fieldtype, "@destroyed", Qfalse);
rb_hash_aset(fieldtypes_hash, fieldtype_adress, rb_fieldtype);
}
return rb_fieldtype;
}
}
static FIELDTYPE* get_fieldtype(VALUE rb_fieldtype)
{
FIELDTYPE* fieldtype;
if (rb_fieldtype == Qnil) return 0;
if (rb_iv_get(rb_fieldtype, "@destroyed") == Qtrue) {
rb_raise(rb_eRuntimeError, "Attempt to access a destroyed fieldtype");
return 0;
}
Data_Get_Struct(rb_fieldtype, FIELDTYPE, fieldtype);
return fieldtype;
}
/*
* FORM wrapper
*/
static VALUE wrap_form(FORM* form)
{
if (form == 0) return Qnil;
{
VALUE forms_hash = rb_iv_get(mForm, "@forms_hash");
VALUE form_adress = INT2NUM((long)(form));
VALUE rb_form = rb_hash_aref(forms_hash, form_adress);
if (rb_form == Qnil) {
rb_form = Data_Wrap_Struct(cFORM, 0, 0, form);
rb_iv_set(rb_form, "@destroyed", Qfalse);
rb_hash_aset(forms_hash, form_adress, rb_form);
}
return rb_form;
}
}
static FORM* get_form(VALUE rb_form)
{
FORM* form;
if (rb_form == Qnil) return 0;
if (rb_iv_get(rb_form, "@destroyed") == Qtrue) {
rb_raise(rb_eRuntimeError, "Attempt to access a destroyed form");
return 0;
}
Data_Get_Struct(rb_form, FORM, form);
return form;
}
/*
* Proc objects are registered using hashes (one for each type of hook)
* The key in the hash is the address of the ncurses "object" and the value is
* the Proc object.
*/
#define FIELD_INIT_HOOK 0
#define FIELD_TERM_HOOK 1
#define FORM_INIT_HOOK 2
#define FORM_TERM_HOOK 3
#define FIELDTYPE_FIELD_CHECK_HOOK 4
#define FIELDTYPE_CHAR_CHECK_HOOK 5
#define FIELDTYPE_NEXT_CHOICE_HOOK 6
#define FIELDTYPE_PREV_CHOICE_HOOK 7
#define FIELDTYPE_ARGS 8
#define PROC_HASHES_COUNT 9
static VALUE get_proc_hash(int hook) {
VALUE arr = rb_iv_get(mForm, "@proc_hashes");
VALUE hash = rb_ary_entry(arr, (long)hook);
if (hash == Qnil) {
rb_raise(rb_eRuntimeError, "Invalid proc hash.");
}
return hash;
}
/*
* Returns an existing Ruby Proc for a given owning "object" and hook type.
* Qnil will be returned if no Proc was associated with the owner
*/
static VALUE get_proc(void* owner, int hook) {
if (owner == 0) return Qnil;
{
VALUE owner_adress = INT2NUM((long)(owner));
VALUE proc_hash = get_proc_hash(hook);
VALUE proc = rb_hash_aref(proc_hash, owner_adress);
return proc;
}
}
/*
* Registers the Proc object with a given owner "object" and hook type.
* If proc is Qnil, the hook is unregistered instead.
*/
static void reg_proc(void* owner, int hook, VALUE proc) {
if (owner == NULL) return;
{
VALUE proc_hash = get_proc_hash(hook);
VALUE owner_address = INT2NUM((long)(owner));
if (proc == Qnil) {
rb_hash_delete(proc_hash, owner_address);
}
else {
rb_hash_aset(proc_hash, owner_address, proc);
}
}
}
/*
* Form creation/destruction functions
*/
static VALUE rbncurs_m_new_form(VALUE dummy, VALUE rb_field_array)
{
long n = rbncurs_array_length(rb_field_array);
/* Will ncurses free this array? If not, must do it after calling free_form(). */
FIELD** fields = ALLOC_N(FIELD*, (n+1));
long i;
for (i=0; i<n; i++){
fields[i] = get_field(rb_ary_entry(rb_field_array, i));
}
fields[n] = NULL;
return wrap_form(new_form(fields));
}
static VALUE rbncurs_c_free_form(VALUE rb_form) {
VALUE forms_hash = rb_iv_get(mForm, "@forms_hash");
FORM* form = get_form(rb_form);
VALUE form_adress = INT2NUM((long)(form));
rb_funcall(forms_hash, rb_intern("delete"), 1, form_adress);
rb_iv_set(rb_form, "@destroyed", Qtrue);
return INT2NUM(free_form(form));
}
static VALUE rbncurs_m_free_form(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_free_form(rb_form); }
/*
* form_post
*/
static VALUE rbncurs_c_post_form(VALUE rb_form) {
FORM* form = get_form(rb_form);
return INT2NUM(post_form(form));
}
static VALUE rbncurs_m_post_form(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_post_form(rb_form); }
static VALUE rbncurs_c_unpost_form(VALUE rb_form) {
FORM* form = get_form(rb_form);
return INT2NUM(unpost_form(form));
}
static VALUE rbncurs_m_unpost_form(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_unpost_form(rb_form); }
/*
* Form driver
*/
static VALUE rbncurs_c_form_driver(VALUE rb_form, VALUE c) {
FORM* form = get_form(rb_form);
return INT2NUM(form_driver(form, NUM2INT(c)));
}
static VALUE rbncurs_m_form_driver(VALUE dummy, VALUE rb_form, VALUE c)
{ return rbncurs_c_form_driver(rb_form, c); }
# ifdef HAVE_FORM_DRIVER_W
/* Form driver W */
static VALUE rbncurs_c_form_driver_w(VALUE rb_form, VALUE type, VALUE c) {
FORM* form = get_form(rb_form);
return INT2NUM(form_driver_w(form, NUM2INT(type), NUM2INT(c)));
}
static VALUE rbncurs_m_form_driver_w(VALUE dummy, VALUE rb_form, VALUE type, VALUE c)
{ return rbncurs_c_form_driver_w(rb_form, type, c); }
# endif
/*
* form_page(3x)
*/
static VALUE rbncurs_c_set_current_field(VALUE rb_form, VALUE rb_field) {
FORM* form = get_form(rb_form);
FIELD* field = get_field(rb_field);
return INT2NUM(set_current_field(form, field));
}
static VALUE rbncurs_m_set_current_field(VALUE dummy, VALUE rb_form, VALUE rb_field)
{ return rbncurs_c_set_current_field(rb_form, rb_field); }
static VALUE rbncurs_c_current_field(VALUE rb_form) {
FORM* form = get_form(rb_form);
return wrap_field(current_field(form));
}
static VALUE rbncurs_m_current_field(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_current_field(rb_form); }
static VALUE rbncurs_c_set_form_page(VALUE rb_form, VALUE n) {
FORM* form = get_form(rb_form);
return INT2NUM(set_form_page(form, NUM2INT(n)));
}
static VALUE rbncurs_m_set_form_page(VALUE dummy, VALUE rb_form, VALUE n)
{ return rbncurs_c_set_form_page(rb_form, n); }
static VALUE rbncurs_c_form_page(VALUE rb_form) {
FORM* form = get_form(rb_form);
return INT2NUM(form_page(form));
}
static VALUE rbncurs_m_form_page(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_form_page(rb_form); }
static VALUE rbncurs_c_field_index(VALUE rb_field) {
FIELD* field = get_field(rb_field);
return INT2NUM(field_index(field));
}
static VALUE rbncurs_m_field_index(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_index(rb_field); }
/*
* form_data(3x)
*/
static VALUE rbncurs_c_data_ahead(VALUE rb_form) {
FORM* form = get_form(rb_form);
return (data_ahead(form)) ? Qtrue: Qfalse;
}
static VALUE rbncurs_m_data_ahead(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_data_ahead(rb_form); }
static VALUE rbncurs_c_data_behind(VALUE rb_form) {
FORM* form = get_form(rb_form);
return (data_behind(form)) ? Qtrue: Qfalse;
}
static VALUE rbncurs_m_data_behind(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_data_behind(rb_form); }
/*
* Field functions (form_field_new(3x))
*/
static VALUE rbncurs_m_new_field(VALUE dummy,
VALUE height, VALUE width,
VALUE toprow, VALUE leftcol,
VALUE offscreen, VALUE nbuffers) {
return wrap_field(new_field(NUM2INT(height), NUM2INT(width),
NUM2INT(toprow), NUM2INT(leftcol),
NUM2INT(offscreen), NUM2INT(nbuffers)));
}
static VALUE rbncurs_c_dup_field(VALUE rb_field, VALUE toprow, VALUE leftcol) {
FIELD* field = get_field(rb_field);
return wrap_field(dup_field(field, NUM2INT(toprow),NUM2INT(leftcol)));
}
static VALUE rbncurs_m_dup_field(VALUE dummy, VALUE rb_field, VALUE toprow, VALUE leftcol)
{ return rbncurs_c_dup_field(rb_field, toprow, leftcol); }
static VALUE rbncurs_c_link_field(VALUE rb_field, VALUE toprow, VALUE leftcol) {
FIELD* field = get_field(rb_field);
return wrap_field(link_field(field, NUM2INT(toprow),NUM2INT(leftcol)));
}
static VALUE rbncurs_m_link_field(VALUE dummy, VALUE rb_field, VALUE toprow, VALUE leftcol)
{ return rbncurs_c_link_field(rb_field, toprow, leftcol); }
static VALUE rbncurs_c_free_field(VALUE rb_field) {
VALUE fields_hash = rb_iv_get(mForm, "@fields_hash");
FIELD* field = get_field(rb_field);
VALUE field_adress = INT2NUM((long)(field));
rb_funcall(fields_hash, rb_intern("delete"), 1, field_adress);
rb_iv_set(rb_field, "@destroyed", Qtrue);
return INT2NUM(free_field(field));
}
static VALUE rbncurs_m_free_field(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_free_field(rb_field); }
/*
* form_field_info(3x)
*/
static VALUE rbncurs_c_field_info(VALUE rb_field, VALUE rows, VALUE cols,
VALUE frow, VALUE fcol, VALUE nrow, VALUE nbuf) {
if (rb_obj_is_instance_of(rows, rb_cArray) != Qtrue
|| rb_obj_is_instance_of(cols, rb_cArray) != Qtrue
|| rb_obj_is_instance_of(frow, rb_cArray) != Qtrue
|| rb_obj_is_instance_of(fcol, rb_cArray) != Qtrue
|| rb_obj_is_instance_of(nrow, rb_cArray) != Qtrue
|| rb_obj_is_instance_of(nbuf, rb_cArray) != Qtrue) {
rb_raise(rb_eArgError,
"rows, cols, frow, fcol, nrow and nbuf arguments must be empty Arrays");
return Qnil;
}
else {
FIELD* field = get_field(rb_field);
int vals[6] = {0,0,0,0,0,0};
int result = field_info(field, &vals[0],&vals[1],&vals[2],&vals[3],&vals[4],&vals[5]);
rb_ary_push(rows, INT2NUM(vals[0]));
rb_ary_push(cols, INT2NUM(vals[1]));
rb_ary_push(frow, INT2NUM(vals[2]));
rb_ary_push(fcol, INT2NUM(vals[3]));
rb_ary_push(nrow, INT2NUM(vals[4]));
rb_ary_push(nbuf, INT2NUM(vals[5]));
return INT2NUM(result);
}
}
static VALUE rbncurs_m_field_info(VALUE dummy, VALUE rb_field, VALUE rows, VALUE cols,
VALUE frow, VALUE fcol, VALUE nrow, VALUE nbuf)
{ return rbncurs_c_field_info(rb_field, rows, cols, frow, fcol, nrow, nbuf); }
static VALUE rbncurs_c_dynamic_field_info(VALUE rb_field, VALUE rows, VALUE cols,
VALUE max) {
if (rb_obj_is_instance_of(rows, rb_cArray) != Qtrue
|| rb_obj_is_instance_of(cols, rb_cArray) != Qtrue
|| rb_obj_is_instance_of(max, rb_cArray) != Qtrue) {
rb_raise(rb_eArgError,
"rows, cols and max arguments must be empty Arrays");
return Qnil;
}
else {
FIELD* field = get_field(rb_field);
int vals[3] = {0,0,0};
int result = dynamic_field_info(field, &vals[0],&vals[1],&vals[2]);
rb_ary_push(rows, INT2NUM(vals[0]));
rb_ary_push(cols, INT2NUM(vals[1]));
rb_ary_push(max, INT2NUM(vals[2]));
return INT2NUM(result);
}
}
static VALUE rbncurs_m_dynamic_field_info(VALUE dummy, VALUE rb_field, VALUE rows, VALUE cols,
VALUE max)
{ return rbncurs_c_dynamic_field_info(rb_field, rows, cols, max); }
/*
* field_validation
*/
static VALUE rbncurs_c_set_field_type(int argc, VALUE* argv, VALUE rb_field) {
VALUE rb_fieldtype, arg3, arg4, arg5;
FIELD* field = get_field(rb_field);
FIELDTYPE* ftype = NULL;
rb_scan_args(argc, argv, "13", &rb_fieldtype, &arg3, &arg4, &arg5);
ftype = get_fieldtype(rb_fieldtype);
if (ftype == TYPE_ALNUM ||
ftype == TYPE_ALPHA) {
if (argc != 2)
rb_raise(rb_eArgError, "TYPE_ALNUM and TYPE_ALPHA require one additional argument");
return INT2NUM(set_field_type(field, ftype,
NUM2INT(arg3)));
}
if (ftype == TYPE_ENUM) {
if (argc != 4) {
rb_raise(rb_eArgError, "TYPE_ENUM requires three additional arguments");
}
else {
int n = (int) rbncurs_array_length(arg3);
/* Will ncurses free this array of strings in free_field()? */
char** list = ALLOC_N(char*, n+1);
int i;
for (i = 0; i < n; i++) {
list[i] = STR2CSTR(rb_ary_entry(arg3, (long)i));
}
list[n] = NULL;
return INT2NUM(set_field_type(field, ftype,
list,
RTEST(arg4),
RTEST(arg5)));
}
}
else if (ftype == TYPE_INTEGER) {
if (argc != 4)
rb_raise(rb_eArgError, "TYPE_INTEGER requires three additional arguments");
return INT2NUM(set_field_type(field, ftype,
NUM2INT(arg3),
NUM2LONG(arg4),
NUM2LONG(arg5)));
}
else if (ftype == TYPE_NUMERIC) {
if (argc != 4)
rb_raise(rb_eArgError, "TYPE_NUMERIC requires three additional arguments");
return INT2NUM(set_field_type(field, ftype,
NUM2INT(arg3),
NUM2DBL(arg4),
NUM2DBL(arg5)));
}
else if (ftype == TYPE_REGEXP){
if (argc != 2)
rb_raise(rb_eArgError, "TYPE_REGEXP requires one additional argument");
return INT2NUM(set_field_type(field, ftype,
STR2CSTR(arg3)));
}
else if (ftype == TYPE_IPV4){
if (argc != 1)
rb_raise(rb_eArgError, "TYPE_IPV4 has no additional arguments");
return INT2NUM(set_field_type(field, ftype));
}
else {
/* It is a user-defined field type. */
/* Will store the arguments associated with this field */
/* for use in the callback function. */
VALUE rest;
rb_scan_args(argc, argv, "1*", &rb_fieldtype, &rest);
reg_proc(field, FIELDTYPE_ARGS, rest);
/* Pass field as an optional parameter so that make_arg can create */
/* the block-argument used in finding the appropriate Ruby Proc */
return INT2NUM(set_field_type(field, ftype, field));
}
}
static VALUE rbncurs_m_set_field_type(int argc, VALUE* argv, VALUE dummy)
{ return rbncurs_c_set_field_type(argc-1, argv+1, argv[0]); }
static VALUE rbncurs_c_field_type(VALUE rb_field) {
FIELD* field = get_field(rb_field);
return wrap_fieldtype(field_type(field));
}
static VALUE rbncurs_m_field_type(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_type(rb_field); }
/* What is this function doing??? */
static VALUE rbncurs_c_field_arg(VALUE rb_field) {
FIELD* field = get_field(rb_field);
field_arg(field);
return Qfalse;
}
static VALUE rbncurs_m_field_arg(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_arg(rb_field); }
/*
* field_attributes
*/
static VALUE rbncurs_c_set_field_fore(VALUE rb_field, VALUE attr) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_field_fore(field, (int) NUM2ULONG(attr)));
}
static VALUE rbncurs_m_set_field_fore(VALUE dummy, VALUE rb_field, VALUE attr)
{ return rbncurs_c_set_field_fore(rb_field, attr); }
static VALUE rbncurs_c_field_fore(VALUE rb_field) {
FIELD* field = get_field(rb_field);
return ULONG2NUM(field_fore(field));
}
static VALUE rbncurs_m_field_fore(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_fore(rb_field); }
static VALUE rbncurs_c_set_field_back(VALUE rb_field, VALUE attr) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_field_back(field, (int) NUM2ULONG(attr)));
}
static VALUE rbncurs_m_set_field_back(VALUE dummy, VALUE rb_field, VALUE attr)
{ return rbncurs_c_set_field_back(rb_field, attr); }
static VALUE rbncurs_c_field_back(VALUE rb_field) {
FIELD* field = get_field(rb_field);
return ULONG2NUM(field_back(field));
}
static VALUE rbncurs_m_field_back(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_back(rb_field); }
static VALUE rbncurs_c_set_field_pad(VALUE rb_field, VALUE pad) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_field_pad(field, NUM2INT(pad)));
}
static VALUE rbncurs_m_set_field_pad(VALUE dummy, VALUE rb_field, VALUE pad)
{ return rbncurs_c_set_field_pad(rb_field, pad); }
static VALUE rbncurs_c_field_pad(VALUE rb_field) {
FIELD* field = get_field(rb_field);
return INT2NUM(field_pad(field));
}
static VALUE rbncurs_m_field_pad(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_pad(rb_field); }
/*
* field_buffer
*/
static VALUE rbncurs_c_set_field_buffer(VALUE rb_field, VALUE buf, VALUE value) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_field_buffer(field, NUM2INT(buf), STR2CSTR(value)));
}
static VALUE rbncurs_m_set_field_buffer(VALUE dummy, VALUE rb_field, VALUE buf, VALUE value)
{ return rbncurs_c_set_field_buffer(rb_field, buf, value); }
static VALUE rbncurs_c_field_buffer(VALUE rb_field, VALUE buffer) {
FIELD* field = get_field(rb_field);
return rb_str_new2(field_buffer(field, NUM2INT(buffer)));
}
static VALUE rbncurs_m_field_buffer(VALUE dummy, VALUE rb_field, VALUE buffer)
{ return rbncurs_c_field_buffer(rb_field, buffer); }
static VALUE rbncurs_c_set_field_status(VALUE rb_field, VALUE status) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_field_status(field, RTEST(status)));
}
static VALUE rbncurs_m_set_field_status(VALUE dummy, VALUE rb_field, VALUE status)
{ return rbncurs_c_set_field_status(rb_field, status); }
static VALUE rbncurs_c_field_status(VALUE rb_field) {
FIELD* field = get_field(rb_field);
return (field_status(field)) ? Qtrue: Qfalse;
}
static VALUE rbncurs_m_field_status(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_status(rb_field); }
static VALUE rbncurs_c_set_max_field(VALUE rb_field, VALUE max) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_max_field(field, NUM2INT(max)));
}
static VALUE rbncurs_m_set_max_field(VALUE dummy, VALUE rb_field, VALUE max)
{ return rbncurs_c_set_max_field(rb_field, max); }
/*
* form_field
*/
static VALUE rbncurs_c_set_form_fields(VALUE rb_form, VALUE rb_field_array) {
long n = rbncurs_array_length(rb_field_array);
/* If ncurses does not free memory used by the previous array of strings, */
/* we will have to do it now. */
FIELD** fields = ALLOC_N(FIELD*, (n+1));
long i;
FORM* form = NULL;
for (i=0; i<n; i++){
fields[i] = get_field(rb_ary_entry(rb_field_array, i));
}
fields[n] = NULL;
form = get_form(rb_form);
return INT2NUM(set_form_fields(form, fields));
}
static VALUE rbncurs_m_set_form_fields(VALUE dummy, VALUE rb_form, VALUE rb_field_array)
{ return rbncurs_c_set_form_fields(rb_form, rb_field_array); }
static VALUE rbncurs_c_form_fields(VALUE rb_form) {
FORM* form = get_form(rb_form);
FIELD** fields = form_fields(form);
VALUE arr = Qundef;
int i;
if (fields == NULL)
rb_raise(rb_eRuntimeError, "Error retrieving form fields");
arr = rb_ary_new();
i=0;
while (fields[i] != NULL)
{
rb_ary_push(arr, wrap_field(fields[i++]));
}
return arr;
}
static VALUE rbncurs_m_form_fields(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_form_fields(rb_form); }
static VALUE rbncurs_c_field_count(VALUE rb_form) {
FORM* form = get_form(rb_form);
return INT2NUM(field_count(form));
}
static VALUE rbncurs_m_field_count(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_field_count(rb_form); }
static VALUE rbncurs_c_move_field(VALUE rb_field, VALUE frow, VALUE fcol) {
FIELD* field = get_field(rb_field);
return INT2NUM(move_field(field, NUM2INT(frow), NUM2INT(fcol)));
}
static VALUE rbncurs_m_move_field(VALUE dummy, VALUE rb_field, VALUE frow, VALUE fcol)
{ return rbncurs_c_move_field(rb_field, frow, fcol); }
/*
* form_hook
*/
static void field_init_hook(FORM* form) {
/* Find the Proc object associated with this form */
VALUE proc = get_proc(form, FIELD_INIT_HOOK);
if (proc != Qnil) {
VALUE rb_form = wrap_form(form);
rb_funcall(proc, rb_intern("call"), 1, rb_form);
}
}
static VALUE rbncurs_c_set_field_init(VALUE rb_form, VALUE proc) {
FORM* form = NULL;
if (!rb_obj_is_kind_of(rb_form, cFORM))
rb_raise(rb_eArgError, "arg1 must be a FORM object");
if (!rb_obj_is_kind_of(proc, rb_cProc))
rb_raise(rb_eArgError, "arg2 must be a Proc object");
form = get_form(rb_form);
reg_proc(form, FIELD_INIT_HOOK, proc);
if (proc != Qnil) {
return INT2NUM(set_field_init(form, field_init_hook));
}
else {
return INT2NUM(set_field_init(form, NULL));
}
}
static VALUE rbncurs_m_set_field_init(VALUE dummy, VALUE rb_form, VALUE proc)
{ return rbncurs_c_set_field_init(rb_form, proc); }
static VALUE rbncurs_c_field_init(VALUE rb_form) {
FORM* form = get_form(rb_form);
return get_proc(form, FIELD_INIT_HOOK);
}
static VALUE rbncurs_m_field_init(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_field_init(rb_form); }
static void field_term_hook(FORM* form) {
/* Find the Proc object associated with this form */
VALUE proc = get_proc(form, FIELD_TERM_HOOK);
if (proc != Qnil) {
VALUE rb_form = wrap_form(form);
rb_funcall(proc, rb_intern("call"), 1, rb_form);
}
}
static VALUE rbncurs_c_set_field_term(VALUE rb_form, VALUE proc) {
FORM * form = NULL;
if (!rb_obj_is_kind_of(rb_form, cFORM))
rb_raise(rb_eArgError, "arg1 must be a FORM object");
if (!rb_obj_is_kind_of(proc, rb_cProc))
rb_raise(rb_eArgError, "arg2 must be a Proc object");
form = get_form(rb_form);
reg_proc(form, FIELD_TERM_HOOK, proc);
if (proc != Qnil) {
return INT2NUM(set_field_term(form, field_term_hook));
}
else {
return INT2NUM(set_field_term(form, NULL));
}
}
static VALUE rbncurs_m_set_field_term(VALUE dummy, VALUE rb_form, VALUE proc)
{ return rbncurs_c_set_field_term(rb_form, proc); }
static VALUE rbncurs_c_field_term(VALUE rb_form) {
FORM* form = get_form(rb_form);
return get_proc(form, FIELD_TERM_HOOK);
}
static VALUE rbncurs_m_field_term(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_field_term(rb_form); }
static void form_init_hook(FORM* form) {
/* Find the Proc object associated with this form */
VALUE proc = get_proc(form, FORM_INIT_HOOK);
if (proc != Qnil) {
VALUE rb_form = wrap_form(form);
rb_funcall(proc, rb_intern("call"), 1, rb_form);
}
}
static VALUE rbncurs_c_set_form_init(VALUE rb_form, VALUE proc) {
FORM * form = NULL;
if (!rb_obj_is_kind_of(rb_form, cFORM))
rb_raise(rb_eArgError, "arg1 must be a FORM object");
if (!rb_obj_is_kind_of(proc, rb_cProc))
rb_raise(rb_eArgError, "arg2 must be a Proc object");
form = get_form(rb_form);
reg_proc(form, FORM_INIT_HOOK, proc);
if (proc != Qnil) {
return INT2NUM(set_form_init(form, form_init_hook));
}
else {
return INT2NUM(set_form_init(form, NULL));
}
}
static VALUE rbncurs_m_set_form_init(VALUE dummy, VALUE rb_form, VALUE proc)
{ return rbncurs_c_set_form_init(rb_form, proc); }
static VALUE rbncurs_c_form_init(VALUE rb_form) {
FORM* form = get_form(rb_form);
return get_proc(form, FORM_INIT_HOOK);
}
static VALUE rbncurs_m_form_init(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_form_init(rb_form); }
static void form_term_hook(FORM* form) {
/* Find the Proc object associated with this form */
VALUE proc = get_proc(form, FORM_TERM_HOOK);
if (proc != Qnil) {
VALUE rb_form = wrap_form(form);
rb_funcall(proc, rb_intern("call"), 1, rb_form);
}
}
static VALUE rbncurs_c_set_form_term(VALUE rb_form, VALUE proc) {
FORM * form = NULL;
if (!rb_obj_is_kind_of(rb_form, cFORM))
rb_raise(rb_eArgError, "arg1 must be a FORM object");
if (!rb_obj_is_kind_of(proc, rb_cProc))
rb_raise(rb_eArgError, "arg2 must be a Proc object");
form = get_form(rb_form);
reg_proc(form, FORM_TERM_HOOK, proc);
if (proc != Qnil) {
return INT2NUM(set_form_term(form, form_term_hook));
}
else {
return INT2NUM(set_form_term(form, NULL));
}
}
static VALUE rbncurs_m_set_form_term(VALUE dummy, VALUE rb_form, VALUE proc)
{ return rbncurs_c_set_form_term(rb_form, proc); }
static VALUE rbncurs_c_form_term(VALUE rb_form) {
FORM* form = get_form(rb_form);
return get_proc(form, FORM_TERM_HOOK);
}
static VALUE rbncurs_m_form_term(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_form_term(rb_form); }
/*
* form_field_just
*/
static VALUE rbncurs_c_set_field_just(VALUE rb_field, VALUE justification) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_field_just(field, NUM2INT(justification)));
}
static VALUE rbncurs_m_set_field_just(VALUE dummy, VALUE rb_field, VALUE justification)
{ return rbncurs_c_set_field_just(rb_field, justification); }
static VALUE rbncurs_c_field_just(VALUE rb_field) {
FIELD* field = get_field(rb_field);
return INT2NUM(field_just(field));
}
static VALUE rbncurs_m_field_just(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_just(rb_field); }
/*
* form_field_opts
*/
static VALUE rbncurs_c_set_field_opts(VALUE rb_field, VALUE opts) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_field_opts(field, NUM2INT(opts)));
}
static VALUE rbncurs_m_set_field_opts(VALUE dummy, VALUE rb_field, VALUE opts)
{ return rbncurs_c_set_field_opts(rb_field, opts); }
static VALUE rbncurs_c_field_opts_on(VALUE rb_field, VALUE opts) {
FIELD* field = get_field(rb_field);
return INT2NUM(field_opts_on(field, NUM2INT(opts)));
}
static VALUE rbncurs_m_field_opts_on(VALUE dummy, VALUE rb_field, VALUE opts)
{ return rbncurs_c_field_opts_on(rb_field, opts); }
static VALUE rbncurs_c_field_opts_off(VALUE rb_field, VALUE opts) {
FIELD* field = get_field(rb_field);
return INT2NUM(field_opts_off(field, NUM2INT(opts)));
}
static VALUE rbncurs_m_field_opts_off(VALUE dummy, VALUE rb_field, VALUE opts)
{ return rbncurs_c_field_opts_off(rb_field, opts); }
static VALUE rbncurs_c_field_opts(VALUE rb_field) {
FIELD* field = get_field(rb_field);
return INT2NUM(field_opts(field));
}
static VALUE rbncurs_m_field_opts(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_field_opts(rb_field); }
/*
* form_opts
*/
static VALUE rbncurs_c_set_form_opts(VALUE rb_form, VALUE opts) {
FORM* form = get_form(rb_form);
return INT2NUM(set_form_opts(form, NUM2INT(opts)));
}
static VALUE rbncurs_m_set_form_opts(VALUE dummy, VALUE rb_form, VALUE opts)
{ return rbncurs_c_set_form_opts(rb_form, opts); }
static VALUE rbncurs_c_form_opts_on(VALUE rb_form, VALUE opts) {
FORM* form = get_form(rb_form);
return INT2NUM(form_opts_on(form, NUM2INT(opts)));
}
static VALUE rbncurs_m_form_opts_on(VALUE dummy, VALUE rb_form, VALUE opts)
{ return rbncurs_c_form_opts_on(rb_form, opts); }
static VALUE rbncurs_c_form_opts_off(VALUE rb_form, VALUE opts) {
FORM* form = get_form(rb_form);
return INT2NUM(form_opts_off(form, NUM2INT(opts)));
}
static VALUE rbncurs_m_form_opts_off(VALUE dummy, VALUE rb_form, VALUE opts)
{ return rbncurs_c_form_opts_off(rb_form, opts); }
static VALUE rbncurs_c_form_opts(VALUE rb_form) {
FORM* form = get_form(rb_form);
return INT2NUM(form_opts(form));
}
static VALUE rbncurs_m_form_opts(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_form_opts(rb_form); }
/*
* form_requestname
*/
static VALUE rbncurs_c_form_request_name(VALUE request) {
return rb_str_new2(form_request_name(NUM2INT(request)));
}
static VALUE rbncurs_m_form_request_name(VALUE dummy, VALUE request)
{ return rbncurs_c_form_request_name(request); }
static VALUE rbncurs_c_form_request_by_name(VALUE name) {
return INT2NUM(form_request_by_name(STR2CSTR(name)));
}
static VALUE rbncurs_m_form_request_by_name(VALUE dummy, VALUE name)
{ return rbncurs_c_form_request_by_name(name); }
/*
* form_win
*/
static VALUE rbncurs_c_set_form_win(VALUE rb_form, VALUE rb_win) {
FORM* form = get_form(rb_form);
WINDOW* win = get_window(rb_win);
return INT2NUM(set_form_win(form, win));
}
static VALUE rbncurs_m_set_form_win(VALUE dummy, VALUE rb_form, VALUE rb_win)
{ return rbncurs_c_set_form_win(rb_form, rb_win); }
static VALUE rbncurs_c_form_win(VALUE rb_form) {
FORM* form = get_form(rb_form);
return wrap_window(form_win(form));
}
static VALUE rbncurs_m_form_win(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_form_win(rb_form); }
static VALUE rbncurs_c_set_form_sub(VALUE rb_form, VALUE rb_sub) {
FORM* form = get_form(rb_form);
WINDOW* win = get_window(rb_sub);
return INT2NUM(set_form_sub(form, win));
}
static VALUE rbncurs_m_set_form_sub(VALUE dummy, VALUE rb_form, VALUE rb_sub)
{ return rbncurs_c_set_form_sub(rb_form, rb_sub); }
static VALUE rbncurs_c_form_sub(VALUE rb_form) {
FORM* form = get_form(rb_form);
return wrap_window(form_sub(form));
}
static VALUE rbncurs_m_form_sub(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_form_sub(rb_form); }
static VALUE rbncurs_c_scale_form(VALUE rb_form, VALUE rows, VALUE columns) {
FORM* form = get_form(rb_form);
if (rb_obj_is_instance_of(rows, rb_cArray) != Qtrue
|| rb_obj_is_instance_of(columns, rb_cArray) != Qtrue) {
rb_raise(rb_eArgError,
"rows and columns arguments must be empty Arrays");
return Qnil;
}
else {
int vals[2] = {0,0};
int result = scale_form(form, &vals[0],&vals[1]);
rb_ary_push(rows, INT2NUM(vals[0]));
rb_ary_push(columns, INT2NUM(vals[1]));
return INT2NUM(result);
}
}
static VALUE rbncurs_m_scale_form(VALUE dummy, VALUE rb_form, VALUE rows, VALUE columns)
{ return rbncurs_c_scale_form(rb_form, rows, columns); }
/*
* form_fieldtype
*/
static void* make_arg(va_list* ap) {
/* This method creates a list of arguments to be passed */
/* to the validation functions (char_check and field_check). */
FIELD* field = va_arg(*ap, FIELD*);
FIELDTYPE* fieldtype = field_type(field);
VALUE proc = get_proc(fieldtype, FIELDTYPE_FIELD_CHECK_HOOK);
if (proc == Qnil) {
proc = get_proc(fieldtype, FIELDTYPE_CHAR_CHECK_HOOK);
}
/* Compare number of arguments in Ruby Proc with that of set_field_type */
if (proc != Qnil) {
VALUE argc = rb_funcall(proc, rb_intern("arity"),0);
VALUE args = get_proc(field, FIELDTYPE_ARGS);
if (args != Qnil) {
if (NUM2INT(argc)-1 != rbncurs_array_length(args)) {
char msg[500];
snprintf(msg, 500, "The validation functions for this field type need %d additional arguments.",(int)(NUM2INT(argc)-1));
msg[499]=0;
rb_raise(rb_eArgError, "%s", msg);
}
}
}
/* field will be the only argument in field_check/char_check callback */
/* and will be used to locate the appropriate Ruby Proc */
return field;
}
static bool field_check(FIELD* field, const void* argblock) {
FIELDTYPE* fieldtype = field_type(field);
VALUE proc = get_proc(fieldtype, FIELDTYPE_FIELD_CHECK_HOOK);
if (proc != Qnil)
{
VALUE args = rb_ary_dup(get_proc(field, FIELDTYPE_ARGS));
rb_ary_unshift(args, wrap_field(field));
return RTEST(rb_apply(proc, rb_intern("call"), args));
}
return 1;
}
static bool char_check(int c, const void* argblock) {
FIELD* field = (FIELD*)argblock;
FIELDTYPE* fieldtype = field_type(field);
VALUE proc = get_proc(fieldtype, FIELDTYPE_CHAR_CHECK_HOOK);
if (proc != Qnil) {
VALUE args = rb_ary_dup(get_proc(field, FIELDTYPE_ARGS));
char str[2];
str[0] = c;
str[1] = 0;
rb_ary_unshift(args, rb_str_new2(str));
return RTEST(rb_apply(proc, rb_intern("call"), args));
}
return 1;
}
static VALUE rbncurs_m_new_fieldtype(VALUE dummy, VALUE field_check_proc, VALUE char_check_proc)
{
FIELDTYPE* fieldtype = new_fieldtype(field_check_proc == Qnil ? NULL : field_check,
char_check_proc == Qnil ? NULL : char_check);
set_fieldtype_arg(fieldtype, make_arg, NULL, NULL);
if (field_check_proc != Qnil) {
reg_proc(fieldtype, FIELDTYPE_FIELD_CHECK_HOOK, field_check_proc);
}
if (char_check_proc != Qnil) {
reg_proc(fieldtype, FIELDTYPE_CHAR_CHECK_HOOK, char_check_proc);
}
return wrap_fieldtype(fieldtype);
}
static VALUE rbncurs_c_free_fieldtype(VALUE rb_fieldtype) {
FIELDTYPE* fieldtype = get_fieldtype(rb_fieldtype);
return INT2NUM(free_fieldtype(fieldtype));
}
static VALUE rbncurs_m_free_fieldtype(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_free_fieldtype(rb_field); }
static bool next_choice(FIELD* field, const void* argblock) {
FIELDTYPE* fieldtype = field_type(field);
VALUE proc = get_proc(fieldtype, FIELDTYPE_NEXT_CHOICE_HOOK);
if (proc != Qnil) {
/* Need to find out what exactly is argblock about... */
return RTEST(rb_funcall(proc, rb_intern("call"), 1, wrap_field(field)));
}
return 1;
}
static bool prev_choice(FIELD* field, const void* argblock) {
FIELDTYPE* fieldtype = field_type(field);
VALUE proc = get_proc(fieldtype, FIELDTYPE_PREV_CHOICE_HOOK);
if (proc != Qnil) {
/* Need to find out what exactly is argblock about... */
return RTEST(rb_funcall(proc, rb_intern("call"), 1, wrap_field(field)));
}
return 1;
}
static VALUE rbncurs_c_set_fieldtype_choice(VALUE rb_fieldtype, VALUE next_choice_proc, VALUE prev_choice_proc) {
FIELDTYPE* fieldtype = get_fieldtype(rb_fieldtype);
int result = set_fieldtype_choice(fieldtype,
next_choice_proc == Qnil ? NULL : next_choice,
prev_choice_proc == Qnil ? NULL : prev_choice);
if (next_choice_proc != Qnil)
reg_proc(fieldtype, FIELDTYPE_NEXT_CHOICE_HOOK, next_choice_proc);
if (prev_choice_proc != Qnil)
reg_proc(fieldtype, FIELDTYPE_PREV_CHOICE_HOOK, prev_choice_proc);
return INT2NUM(result);
}
static VALUE rbncurs_m_set_fieldtype_choice(VALUE dummy, VALUE rb_fieldtype, VALUE next_choice_proc, VALUE prev_choice_proc)
{ return rbncurs_c_set_fieldtype_choice(rb_fieldtype, next_choice_proc, prev_choice_proc); }
static VALUE rbncurs_c_link_fieldtype(VALUE rb_fieldtype1, VALUE rb_fieldtype2) {
FIELDTYPE* fieldtype1 = get_fieldtype(rb_fieldtype1);
FIELDTYPE* fieldtype2 = get_fieldtype(rb_fieldtype2);
return wrap_fieldtype(link_fieldtype(fieldtype1, fieldtype2));
}
static VALUE rbncurs_m_link_fieldtype(VALUE dummy, VALUE rb_fieldtype1, VALUE fieldtype2)
{ return rbncurs_c_link_fieldtype(rb_fieldtype1, fieldtype2); }
static VALUE rbncurs_c_set_new_page(VALUE rb_field, VALUE new_page_flag) {
FIELD* field = get_field(rb_field);
return INT2NUM(set_new_page(field, RTEST(new_page_flag)));
}
static VALUE rbncurs_m_set_new_page(VALUE dummy, VALUE rb_field, VALUE new_page_flag)
{ return rbncurs_c_set_new_page(rb_field, new_page_flag); }
static VALUE rbncurs_c_new_page(VALUE rb_field) {
FIELD* field = get_field(rb_field);
return (new_page(field))? Qtrue : Qfalse;
}
static VALUE rbncurs_m_new_page(VALUE dummy, VALUE rb_field)
{ return rbncurs_c_new_page(rb_field); }
/*
* form_cursor
*/
static VALUE rbncurs_c_pos_form_cursor(VALUE rb_form) {
FORM* form = get_form(rb_form);
return INT2NUM(pos_form_cursor(form));
}
static VALUE rbncurs_m_pos_form_cursor(VALUE dummy, VALUE rb_form)
{ return rbncurs_c_pos_form_cursor(rb_form); }
void init_form(void)
{
mForm = rb_define_module_under(mNcurses, "Form");
FORM_SNG_FUNC(current_field,1);
FORM_SNG_FUNC(data_ahead,1);
FORM_SNG_FUNC(data_behind,1);
FORM_SNG_FUNC(dup_field,3);
FORM_SNG_FUNC(dynamic_field_info,4);
FORM_SNG_FUNC(field_arg,1);
FORM_SNG_FUNC(field_back,1);
FORM_SNG_FUNC(field_buffer,2);
FORM_SNG_FUNC(field_count,1);
FORM_SNG_FUNC(field_fore,1);
FORM_SNG_FUNC(field_index,1);
FORM_SNG_FUNC(field_info,7);
FORM_SNG_FUNC(field_init,1);
FORM_SNG_FUNC(field_just,1);
FORM_SNG_FUNC(field_opts,1);
FORM_SNG_FUNC(field_opts_off,2);
FORM_SNG_FUNC(field_opts_on,2);
FORM_SNG_FUNC(field_pad,1);
FORM_SNG_FUNC(field_status,1);
FORM_SNG_FUNC(field_term,1);
FORM_SNG_FUNC(field_type,1);
/* FORM_SNG_FUNC(field_userptr,1); */
FORM_SNG_FUNC(form_driver,2);
# ifdef HAVE_FORM_DRIVER_W
FORM_SNG_FUNC(form_driver_w,3);
#endif
FORM_SNG_FUNC(form_fields,1);
FORM_SNG_FUNC(form_init,1);
FORM_SNG_FUNC(form_opts,1);
FORM_SNG_FUNC(form_opts_off,2);
FORM_SNG_FUNC(form_opts_on,2);
FORM_SNG_FUNC(form_page,1);
FORM_SNG_FUNC(form_request_by_name,1);
FORM_SNG_FUNC(form_request_name,1);
FORM_SNG_FUNC(form_sub,1);
FORM_SNG_FUNC(form_term,1);
/* FORM_SNG_FUNC(form_userptr,1); */
FORM_SNG_FUNC(form_win,1);
FORM_SNG_FUNC(free_field,1);
FORM_SNG_FUNC(free_fieldtype,1);
FORM_SNG_FUNC(free_form,1);
FORM_SNG_FUNC(link_field,3);
FORM_SNG_FUNC(link_fieldtype,2);
FORM_SNG_FUNC(move_field,3);
FORM_SNG_FUNC(new_field,6);
FORM_SNG_FUNC(new_fieldtype,2);
FORM_SNG_FUNC(new_form,1);
FORM_SNG_FUNC(new_page,1);
FORM_SNG_FUNC(pos_form_cursor,1);
FORM_SNG_FUNC(post_form,1);
FORM_SNG_FUNC(scale_form,3);
FORM_SNG_FUNC(set_current_field,2);
FORM_SNG_FUNC(set_field_back,2);
FORM_SNG_FUNC(set_field_buffer,3);
FORM_SNG_FUNC(set_field_fore,2);
FORM_SNG_FUNC(set_field_init,2);
FORM_SNG_FUNC(set_field_just,2);
FORM_SNG_FUNC(set_field_opts,2);
FORM_SNG_FUNC(set_field_pad,2);
FORM_SNG_FUNC(set_field_status,2);
FORM_SNG_FUNC(set_field_term,2);
FORM_SNG_FUNC(set_field_type,-1);
/* FORM_SNG_FUNC(set_field_userptr,2); */
FORM_SNG_FUNC(set_fieldtype_choice,3);
FORM_SNG_FUNC(set_form_fields,2);
FORM_SNG_FUNC(set_form_init,2);
FORM_SNG_FUNC(set_form_opts,2);
FORM_SNG_FUNC(set_form_page,2);
FORM_SNG_FUNC(set_form_sub,2);
FORM_SNG_FUNC(set_form_term,2);
/* FORM_SNG_FUNC(set_form_userptr,2); */
FORM_SNG_FUNC(set_form_win,2);
FORM_SNG_FUNC(set_max_field,2);
FORM_SNG_FUNC(set_new_page,2);
FORM_SNG_FUNC(unpost_form,1);
init_err_codes();
init_req_constants();
init_opts_constants();
init_just_constants();
init_form_opts_constants();
/* Hashes to store registered blocks (Proc) */
{
VALUE hashes = rb_iv_set(mForm, "@proc_hashes", rb_ary_new());
int i;
for (i = 0; i < PROC_HASHES_COUNT; i++)
{
rb_ary_push(hashes, rb_hash_new());
}
}
/* Forms */
rb_iv_set(mForm, "@forms_hash", rb_hash_new());
cFORM = rb_define_class_under(mForm, "FORM", rb_cObject);
rb_define_singleton_method(cFORM, "new",
(&rbncurs_m_new_form),
1);
RB_CLASS_METH(cFORM, NULL, current_field,0);
RB_CLASS_METH(cFORM, NULL, data_ahead,0);
RB_CLASS_METH(cFORM, NULL, data_behind,0);
RB_CLASS_METH(cFORM, NULL, dup_field,2);
RB_CLASS_METH(cFORM, NULL, field_count,0);
RB_CLASS_METH(cFORM, NULL, field_init,0);
RB_CLASS_METH(cFORM, NULL, field_term,0);
RB_CLASS_METH(cFORM, "driver", form_driver,1);
# ifdef HAVE_FORM_DRIVER_W
RB_CLASS_METH(cFORM, "driver_w", form_driver_w,2);
#endif
RB_CLASS_METH(cFORM, "fields", form_fields,0);
RB_CLASS_METH(cFORM, "init", form_init,0);
RB_CLASS_METH(cFORM, "opts", form_opts,0);
RB_CLASS_METH(cFORM, "opts_off", form_opts_off,1);
RB_CLASS_METH(cFORM, "opts_on", form_opts_on,1);
RB_CLASS_METH(cFORM, "page", form_page,0);
RB_CLASS_METH(cFORM, "sub", form_sub,0);
RB_CLASS_METH(cFORM, "term", form_term,0);
/* RB_CLASS_METH(cFORM, "userptr", form_userptr,0); */
RB_CLASS_METH(cFORM, "win", form_win,0);
RB_CLASS_METH(cFORM, "free", free_form,0);
RB_CLASS_METH(cFORM, "pos_cursor", pos_form_cursor,0);
RB_CLASS_METH(cFORM, "post", post_form,0);
RB_CLASS_METH(cFORM, "scale", scale_form,2);
RB_CLASS_METH(cFORM, "current_field=", set_current_field,1);
RB_CLASS_METH(cFORM, "field_init=", set_field_init,1);
RB_CLASS_METH(cFORM, "field_term=", set_field_term,1);
RB_CLASS_METH(cFORM, "fields=", set_form_fields,1);
RB_CLASS_METH(cFORM, "init=", set_form_init,1);
RB_CLASS_METH(cFORM, "opts=", set_form_opts,1);
RB_CLASS_METH(cFORM, "page=", set_form_page,1);
RB_CLASS_METH(cFORM, "sub=", set_form_sub,1);
RB_CLASS_METH(cFORM, "term=", set_form_term,1);
/* RB_CLASS_METH(cFORM, "userptr=", set_form_userptr,1); */
RB_CLASS_METH(cFORM, "win=", set_form_win,1);
RB_CLASS_METH(cFORM, "unpost", unpost_form,0);
/* Fields */
rb_iv_set(mForm, "@fields_hash", rb_hash_new());
cFIELD = rb_define_class_under(mForm, "FIELD", rb_cObject);
rb_define_singleton_method(cFIELD, "new",
(&rbncurs_m_new_field),
6);
RB_CLASS_METH(cFIELD, "dup", dup_field,2);
RB_CLASS_METH(cFIELD, "dynamic_info", dynamic_field_info,3);
RB_CLASS_METH(cFIELD, "arg", field_arg,0);
RB_CLASS_METH(cFIELD, "back", field_back,1);
RB_CLASS_METH(cFIELD, "buffer", field_buffer,1);
RB_CLASS_METH(cFIELD, "fore", field_fore,1);
RB_CLASS_METH(cFIELD, "index", field_index,0);
RB_CLASS_METH(cFIELD, "info", field_info,6);
RB_CLASS_METH(cFIELD, "just", field_just,0);
RB_CLASS_METH(cFIELD, "opts", field_opts,0);
RB_CLASS_METH(cFIELD, "opts_off", field_opts_off,1);
RB_CLASS_METH(cFIELD, "opts_on", field_opts_on,1);
RB_CLASS_METH(cFIELD, "pad", field_pad,1);
RB_CLASS_METH(cFIELD, "status", field_status,0);
RB_CLASS_METH(cFIELD, "type", field_type,0);
/* RB_CLASS_METH(cFIELD, "userptr", field_userptr,0); */
RB_CLASS_METH(cFIELD, "free", free_field,0);
RB_CLASS_METH(cFIELD, "link", link_field,2);
RB_CLASS_METH(cFIELD, "move", move_field,2);
RB_CLASS_METH(cFIELD, NULL, new_page,0);
RB_CLASS_METH(cFIELD, "back=", set_field_back,1);
RB_CLASS_METH(cFIELD, "set_buffer", set_field_buffer,2);
RB_CLASS_METH(cFIELD, "fore=", set_field_fore,1);
RB_CLASS_METH(cFIELD, "just=", set_field_just,1);
RB_CLASS_METH(cFIELD, "opts=", set_field_opts,1);
RB_CLASS_METH(cFIELD, "pad=", set_field_pad,1);
RB_CLASS_METH(cFIELD, "status=", set_field_status,1);
RB_CLASS_METH(cFIELD, "set_type", set_field_type,-1);
/* RB_CLASS_METH(cFIELD, "userptr=", set_field_userptr,1); */
RB_CLASS_METH(cFIELD, "max_field=", set_max_field,1);
RB_CLASS_METH(cFIELD, "new_page=", set_new_page,1);
/* Field types */
rb_iv_set(mForm, "@fieldtypes_hash", rb_hash_new());
cFIELDTYPE = rb_define_class_under(mForm, "FIELDTYPE", rb_cObject);
rb_define_singleton_method(cFIELDTYPE, "new",
(&rbncurs_m_new_fieldtype),
2);
RB_CLASS_METH(cFIELDTYPE, "free", free_fieldtype,0);
RB_CLASS_METH(cFIELDTYPE, "link", link_fieldtype,1);
RB_CLASS_METH(cFIELDTYPE, "set_choice", set_fieldtype_choice,2);
/* Create predefined types */
rb_define_const(mForm, "TYPE_ALNUM", wrap_fieldtype(TYPE_ALNUM));
rb_define_const(mForm, "TYPE_ALPHA", wrap_fieldtype(TYPE_ALPHA));
rb_define_const(mForm, "TYPE_ENUM", wrap_fieldtype(TYPE_ENUM));
rb_define_const(mForm, "TYPE_INTEGER", wrap_fieldtype(TYPE_INTEGER));
rb_define_const(mForm, "TYPE_NUMERIC", wrap_fieldtype(TYPE_NUMERIC));
rb_define_const(mForm, "TYPE_REGEXP", wrap_fieldtype(TYPE_REGEXP));
rb_define_const(mForm, "TYPE_IPV4", wrap_fieldtype(TYPE_IPV4));
}
#endif
|