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 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
|
/* praat.cpp
*
* Copyright (C) 1992-2011 Paul Boersma
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* pb 2003/03/12 preferences in home directory on shared Windows machines
* pb 2003/06/19 NUMmachar
* pb 2003/07/10 GSL initialization
* pb 2003/10/03 praat-executeFromFile without arguments
* pb 2004/06/17 made Objects label visible on Unix
* pb 2004/12/29 removed .praat-user-startUp for Windows (empty file name error)
* pb 2005/06/28 TextEditor_prefs
* pb 2005/08/22 renamed Control menu to "Praat"
* pb 2005/11/18 URL support
* pb 2006/02/23 corrected callbacks in praat_installEditorN
* pb 2006/08/07 removed quotes from around file paths in openDocument message
* pb 2006/09/30 praat_selection () can take NULL as an argument
* pb 2006/10/28 removed MacOS 9 stuff
* pb 2006/12/26 theCurrentPraat
* pb 2007/01/25 width of object list is 50 procent
* pb 2007/06/10 wchar_t
* pb 2007/06/16 text encoding prefs
* pb 2007/08/31 praat_new1-9
* pb 2007/09/02 include Editor prefs
* sdk 2008/01/14 GTK
* pb 2008/02/01 made sure that praat_dataChanged can be called at error time
* pb 2008/03/13 Windows: better file dropping
* pb 2008/04/09 removed explicit GSL
* pb 2008/11/01 praatcon -a
* pb 2009/01/17 arguments to UiForm callbacks
* pb 2009/03/17 split up theCurrentPraat into Application, Objects and Picture
* pb 2009/12/22 invokingButtonTitle
* pb 2010/05/24 sendpraat for GTK
* pb 2010/07/29 removed GuiWindow_show
* pb 2010/12/08 can read Collections created from multiple objects read from one file (e.g. a labelled sound file)
* pb 2011/07/05 C++
*/
#include "melder.h"
#include "NUMmachar.h"
#include <ctype.h>
#include <stdarg.h>
#if defined (UNIX) || defined (macintosh)
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <locale.h>
#endif
#if defined (UNIX) || defined __MWERKS__
#include <unistd.h>
#endif
#include "praatP.h"
#include "praat_script.h"
#include "site.h"
#include "machine.h"
#include "Printer.h"
#include "ScriptEditor.h"
#include "Strings.h"
#if gtk
#include <gdk/gdkx.h>
#endif
#define EDITOR theCurrentPraatObjects -> list [IOBJECT]. editors
#define WINDOW_WIDTH 520
#define WINDOW_HEIGHT 700
structPraatApplication theForegroundPraatApplication;
PraatApplication theCurrentPraatApplication = & theForegroundPraatApplication;
structPraatObjects theForegroundPraatObjects;
PraatObjects theCurrentPraatObjects = & theForegroundPraatObjects;
structPraatPicture theForegroundPraatPicture;
PraatPicture theCurrentPraatPicture = & theForegroundPraatPicture;
struct PraatP praatP;
static int doingCommandLineInterface;
static char programName [64];
static structMelderDir homeDir = { { 0 } };
/*
* praatDirectory: preferences and buttons files.
* Unix: /u/miep/.myProg-dir (without slash)
* Windows 2000/XP/Vista: \\myserver\myshare\Miep\MyProg
* or: C:\Documents and settings\Miep\MyProg
* Mac X: /Users/Miep/Library/Preferences/MyProg Prefs
*/
extern structMelderDir praatDir;
structMelderDir praatDir = { { 0 } };
/*
* prefs5File: preferences file.
* Unix: /u/miep/.myProg-dir/prefs5
* Windows 2000/XP/Vista: \\myserver\myshare\Miep\MyProg\Preferences5.ini
* or: C:\Documents and settings\Miep\MyProg\Preferences5.ini
* Mac X: /Users/Miep/Library/Preferences/MyProg Prefs/Prefs5
*/
static structMelderFile prefs4File = { 0 }, prefs5File = { 0 };
/*
* buttons5File: buttons file.
* Unix: /u/miep/.myProg-dir/buttons
* Windows 2000/XP/Vista: \\myserver\myshare\Miep\MyProg\Buttons5.ini
* or: C:\Documents and settings\Miep\MyProg\Buttons5.ini
* Mac X: /Users/Miep/Library/Preferences/MyProg Prefs/Buttons5
*/
static structMelderFile buttons4File = { 0 }, buttons5File = { 0 };
#if defined (UNIX)
static structMelderFile pidFile = { 0 }; /* Like /u/miep/.myProg-dir/pid */
static structMelderFile messageFile = { 0 }; /* Like /u/miep/.myProg-dir/message */
#elif defined (_WIN32)
static structMelderFile messageFile = { 0 }; /* Like C:\Windows\myProg\Message.txt */
#endif
static GuiObject praatList_objects;
/***** selection *****/
long praat_getIdOfSelected (ClassInfo klas, int inplace) {
int place = inplace, IOBJECT;
if (place == 0) place = 1;
if (place > 0) {
WHERE (SELECTED && (klas == NULL || CLASS == klas)) {
if (place == 1) return ID;
place --;
}
} else {
WHERE_DOWN (SELECTED && (klas == NULL || CLASS == klas)) {
if (place == -1) return ID;
place ++;
}
}
if (inplace) {
Melder_throw ("No ", klas ? klas -> className : L"object", " #", inplace, " selected.");
} else {
Melder_throw ("No ", klas ? klas -> className : L"object", " selected.");
}
return 0;
}
wchar * praat_getNameOfSelected (ClassInfo klas, int inplace) {
int place = inplace, IOBJECT;
if (place == 0) place = 1;
if (place > 0) {
WHERE (SELECTED && (klas == NULL || CLASS == klas)) {
if (place == 1) return klas == NULL ? FULL_NAME : NAME;
place --;
}
} else {
WHERE_DOWN (SELECTED && (klas == NULL || CLASS == klas)) {
if (place == -1) return klas == NULL ? FULL_NAME : NAME;
place ++;
}
}
if (inplace) {
Melder_throw ("No ", klas ? klas -> className : L"object", " #", inplace, " selected.");
} else {
Melder_throw ("No ", klas ? klas -> className : L"object", " selected.");
}
return 0; // Failure.
}
int praat_selection (ClassInfo klas) {
if (klas == NULL) return theCurrentPraatObjects -> totalSelection;
long readableClassId = klas -> sequentialUniqueIdOfReadableClass;
if (readableClassId == 0) Melder_fatal ("No sequential unique ID for class %ls.", klas -> className);
return theCurrentPraatObjects -> numberOfSelected [readableClassId];
}
void praat_deselect (int IOBJECT) {
if (! SELECTED) return;
SELECTED = FALSE;
theCurrentPraatObjects -> totalSelection -= 1;
long readableClassId = ((Thing) theCurrentPraatObjects -> list [IOBJECT]. object) -> classInfo -> sequentialUniqueIdOfReadableClass;
Melder_assert (readableClassId != 0);
theCurrentPraatObjects -> numberOfSelected [readableClassId] -= 1;
if (! theCurrentPraatApplication -> batch && ! Melder_backgrounding) {
GuiList_deselectItem (praatList_objects, IOBJECT);
}
}
void praat_deselectAll (void) { int IOBJECT; WHERE (1) praat_deselect (IOBJECT); }
void praat_select (int IOBJECT) {
if (SELECTED) return;
SELECTED = TRUE;
theCurrentPraatObjects -> totalSelection += 1;
Thing object = (Thing) theCurrentPraatObjects -> list [IOBJECT]. object;
Melder_assert (object != NULL);
long readableClassId = object -> classInfo -> sequentialUniqueIdOfReadableClass;
if (readableClassId == 0) Melder_fatal ("No sequential unique ID for class %ls.", object -> classInfo -> className);
theCurrentPraatObjects -> numberOfSelected [readableClassId] += 1;
if (! theCurrentPraatApplication -> batch && ! Melder_backgrounding) {
GuiList_selectItem (praatList_objects, IOBJECT);
}
}
void praat_selectAll (void) { int IOBJECT; WHERE (1) praat_select (IOBJECT); }
void praat_list_background (void) {
int IOBJECT;
WHERE (SELECTED) GuiList_deselectItem (praatList_objects, IOBJECT);
}
void praat_list_foreground (void) {
int IOBJECT;
WHERE (SELECTED) {
GuiList_selectItem (praatList_objects, IOBJECT);
}
}
Data praat_onlyObject (ClassInfo klas) {
int IOBJECT, result = 0, found = 0;
WHERE (SELECTED && CLASS == klas) { result = IOBJECT; found += 1; }
if (found != 1) return NULL;
return theCurrentPraatObjects -> list [result]. object;
}
Data praat_firstObject (ClassInfo klas) {
int IOBJECT;
LOOP {
if (CLASS == klas) return theCurrentPraatObjects -> list [IOBJECT]. object;
}
return NULL; // this is often OK
}
Data praat_onlyObject_generic (ClassInfo klas) {
int IOBJECT, result = 0, found = 0;
WHERE (SELECTED && Thing_subclass ((ClassInfo) CLASS, klas)) { result = IOBJECT; found += 1; }
if (found != 1) return NULL;
return theCurrentPraatObjects -> list [result]. object;
}
Data praat_firstObject_generic (ClassInfo klas) {
int IOBJECT;
LOOP {
if (Thing_subclass ((ClassInfo) CLASS, klas)) return theCurrentPraatObjects -> list [IOBJECT]. object;
}
return NULL; // this is often OK
}
praat_Object praat_onlyScreenObject (void) {
int IOBJECT, result = 0, found = 0;
WHERE (SELECTED) { result = IOBJECT; found += 1; }
if (found != 1) Melder_fatal ("praat_onlyScreenObject: found %d objects instead of 1.", found);
return & theCurrentPraatObjects -> list [result];
}
Data praat_firstObject_any () {
int IOBJECT;
LOOP {
return theCurrentPraatObjects -> list [IOBJECT]. object;
}
return NULL; // this is often OK
}
Collection praat_getSelectedObjects (void) {
autoCollection thee = Collection_create (NULL, 10);
Collection_dontOwnItems (thee.peek());
int IOBJECT;
LOOP {
iam_LOOP (Data);
Collection_addItem (thee.peek(), me);
}
return thee.transfer();
}
wchar_t *praat_name (int IOBJECT) { return wcschr (FULL_NAME, ' ') + 1; }
void praat_write_do (Any dia, const wchar_t *extension) {
int IOBJECT, found = 0;
Data data = NULL;
static MelderString defaultFileName = { 0 };
MelderString_empty (& defaultFileName);
WHERE (SELECTED) { if (! data) data = (Data) OBJECT; found += 1; }
if (found == 1) {
MelderString_append (& defaultFileName, data -> name);
if (defaultFileName.length > 50) { defaultFileName.string [50] = '\0'; defaultFileName.length = 50; }
MelderString_append (& defaultFileName, L".", extension ? extension : Thing_className (data));
} else if (extension == NULL) {
MelderString_append (& defaultFileName, L"praat.Collection");
} else {
MelderString_append (& defaultFileName, L"praat.", extension);
}
UiOutfile_do (dia, defaultFileName.string);
}
static void removeAllReferencesToEditor (Any editor) {
int iobject, ieditor;
/*
* Remove all references to this editor.
* It may be editing multiple objects.
*/
for (iobject = 1; iobject <= theCurrentPraatObjects -> n; iobject ++)
for (ieditor = 0; ieditor < praat_MAXNUM_EDITORS; ieditor ++)
if (theCurrentPraatObjects -> list [iobject]. editors [ieditor] == editor)
theCurrentPraatObjects -> list [iobject]. editors [ieditor] = NULL;
if (praatP. editor == editor)
praatP. editor = NULL;
}
static void praat_remove (int iobject) {
/* Remove the "object" from the list. */
/* Kill everything to do with selection. */
int ieditor;
Melder_assert (iobject >= 1 && iobject <= theCurrentPraatObjects -> n);
if (theCurrentPraatObjects -> list [iobject]. _beingCreated) {
theCurrentPraatObjects -> list [iobject]. _beingCreated = FALSE;
theCurrentPraatObjects -> totalBeingCreated --;
}
praat_deselect (iobject);
/*
* To prevent synchronization problems, kill editors before killing the data.
*/
for (ieditor = 0; ieditor < praat_MAXNUM_EDITORS; ieditor ++) {
Editor editor = (Editor) theCurrentPraatObjects -> list [iobject]. editors [ieditor]; /* Save this one reference. */
if (editor) {
removeAllReferencesToEditor (editor);
forget (editor);
}
}
MelderFile_setToNull (& theCurrentPraatObjects -> list [iobject]. file);
Melder_free (theCurrentPraatObjects -> list [iobject]. name);
forget (theCurrentPraatObjects -> list [iobject]. object);
}
void praat_cleanUpName (wchar_t *name) {
/*
* Replaces spaces and special characters by underscores.
*/
for (; *name; name ++) {
#if 1
if (wcschr (L" ,.:;\\/()[]{}~`\'<>*&^%#@!?$\"|", *name)) *name = '_';
#else
if (! iswalnum (*name) && *name != '-' && *name != '+') *name = '_';
#endif
}
}
/***** objects + commands *****/
void praat_newWithFile1 (Data me, const wchar *myName, MelderFile file) {
int IOBJECT, ieditor; // must be local: praat_new can be called from within a loop!!!
if (me == NULL)
Melder_throw ("No object was put into the list.");
/*
* If my class is Collection, I'll have to be unpacked.
*/
if (my classInfo == classCollection) {
Collection list = (Collection) me;
try {
for (long idata = 1; idata <= list -> size; idata ++) {
Data object = (Data) list -> item [idata];
const wchar *name = object -> name ? object -> name : myName;
Melder_assert (name != NULL);
praat_new1 (object, name); // recurse
}
} catch (MelderError) {
list -> size = 0; // disown
forget (list);
throw;
}
list -> size = 0; // disown
forget (list);
return;
}
MelderString name = { 0 }, givenName = { 0 };
if (myName && myName [0]) {
MelderString_copy (& givenName, myName);
/*
* Remove extension.
*/
wchar *p = wcsrchr (givenName.string, '.');
if (p) *p = '\0';
praat_cleanUpName (givenName.string);
} else {
MelderString_copy (& givenName, my name && my name [0] ? my name : L"untitled");
}
MelderString_append (& name, Thing_className (me), L" ", givenName.string);
if (theCurrentPraatObjects -> n == praat_MAXNUM_OBJECTS) {
forget (me);
Melder_throw ("The Object Window cannot contain more than ", praat_MAXNUM_OBJECTS, " objects. You could remove some objects.");
}
IOBJECT = ++ theCurrentPraatObjects -> n;
Melder_assert (FULL_NAME == NULL);
FULL_NAME = Melder_wcsdup_f (name.string); // all right to crash if out of memory
++ theCurrentPraatObjects -> uniqueId;
if (! theCurrentPraatApplication -> batch) { // put a new object on the screen, at the bottom of the list
#ifdef UNIX
#if motif
XtVaSetValues (praatList_objects, XmNvisibleItemCount, theCurrentPraatObjects -> n + 2, NULL);
#endif
#endif
MelderString listName = { 0 };
MelderString_append (& listName, Melder_integer (theCurrentPraatObjects -> uniqueId), L". ", name.string);
GuiList_insertItem (praatList_objects, listName.string, theCurrentPraatObjects -> n);
MelderString_free (& listName);
}
OBJECT = me;
SELECTED = FALSE;
CLASS = my classInfo;
for (ieditor = 0; ieditor < praat_MAXNUM_EDITORS; ieditor ++)
EDITOR [ieditor] = NULL;
if (file != NULL) {
MelderFile_copy (file, & theCurrentPraatObjects -> list [IOBJECT]. file);
} else {
MelderFile_setToNull (& theCurrentPraatObjects -> list [IOBJECT]. file);
}
ID = theCurrentPraatObjects -> uniqueId;
theCurrentPraatObjects -> list [IOBJECT]. _beingCreated = TRUE;
Thing_setName ((Thing) OBJECT, givenName.string);
theCurrentPraatObjects -> totalBeingCreated ++;
MelderString_free (& givenName);
MelderString_free (& name);
}
static MelderString thePraatNewName = { 0 };
void praat_newWithFile2 (Data me, const wchar *s1, const wchar *s2, MelderFile file) {
MelderString_empty (& thePraatNewName);
MelderString_append (& thePraatNewName, s1, s2);
praat_newWithFile1 (me, thePraatNewName.string, file);
}
void praat_newWithFile3 (Data me, const wchar *s1, const wchar *s2, const wchar *s3, MelderFile file) {
MelderString_empty (& thePraatNewName);
MelderString_append (& thePraatNewName, s1, s2, s3);
praat_newWithFile1 (me, thePraatNewName.string, file);
}
void praat_newWithFile4 (Data me, const wchar *s1, const wchar *s2, const wchar *s3, const wchar *s4, MelderFile file) {
MelderString_empty (& thePraatNewName);
MelderString_append (& thePraatNewName, s1, s2, s3, s4);
praat_newWithFile1 (me, thePraatNewName.string, file);
}
void praat_newWithFile5 (Data me, const wchar *s1, const wchar *s2, const wchar *s3, const wchar *s4, const wchar *s5, MelderFile file) {
MelderString_empty (& thePraatNewName);
MelderString_append (& thePraatNewName, s1, s2, s3, s4, s5);
praat_newWithFile1 (me, thePraatNewName.string, file);
}
void praat_new1 (Data me, const wchar *s1) {
praat_newWithFile1 (me, s1, NULL);
}
void praat_new2 (Data me, const wchar *s1, const wchar *s2) {
MelderString_empty (& thePraatNewName);
MelderString_append (& thePraatNewName, s1, s2);
praat_new1 (me, thePraatNewName.string);
}
void praat_new3 (Data me, const wchar *s1, const wchar *s2, const wchar *s3) {
MelderString_empty (& thePraatNewName);
MelderString_append (& thePraatNewName, s1, s2, s3);
praat_new1 (me, thePraatNewName.string);
}
void praat_new4 (Data me, const wchar *s1, const wchar *s2, const wchar *s3, const wchar *s4) {
MelderString_empty (& thePraatNewName);
MelderString_append (& thePraatNewName, s1, s2, s3, s4);
return praat_new1 (me, thePraatNewName.string);
}
void praat_new5 (Data me, const wchar *s1, const wchar *s2, const wchar *s3, const wchar *s4, const wchar *s5) {
MelderString_empty (& thePraatNewName);
MelderString_append (& thePraatNewName, s1, s2, s3, s4, s5);
praat_new1 (me, thePraatNewName.string);
}
void praat_new6 (Data me, const wchar *s1, const wchar *s2, const wchar *s3, const wchar *s4, const wchar *s5, const wchar *s6) {
MelderString_empty (& thePraatNewName);
MelderString_append (& thePraatNewName, s1, s2, s3, s4, s5, s6);
praat_new1 (me, thePraatNewName.string);
}
void praat_new7 (Data me, const wchar *s1, const wchar *s2, const wchar *s3, const wchar *s4, const wchar *s5, const wchar *s6, const wchar *s7) {
MelderString_empty (& thePraatNewName);
MelderString_append (& thePraatNewName, s1, s2, s3, s4, s5, s6, s7);
praat_new1 (me, thePraatNewName.string);
}
void praat_new8 (Data me, const wchar *s1, const wchar *s2, const wchar *s3, const wchar *s4, const wchar *s5, const wchar *s6, const wchar *s7, const wchar *s8) {
MelderString_empty (& thePraatNewName);
MelderString_append (& thePraatNewName, s1, s2, s3, s4, s5, s6, s7, s8);
praat_new1 (me, thePraatNewName.string);
}
void praat_new9 (Data me, const wchar *s1, const wchar *s2, const wchar *s3, const wchar *s4, const wchar *s5, const wchar *s6, const wchar *s7, const wchar *s8, const wchar *s9) {
MelderString_empty (& thePraatNewName);
MelderString_append (& thePraatNewName, s1, s2, s3, s4, s5, s6, s7, s8, s9);
praat_new1 (me, thePraatNewName.string);
}
void praat_updateSelection (void) {
if (theCurrentPraatObjects -> totalBeingCreated) {
int IOBJECT;
praat_deselectAll ();
WHERE (theCurrentPraatObjects -> list [IOBJECT]. _beingCreated) {
praat_select (IOBJECT);
theCurrentPraatObjects -> list [IOBJECT]. _beingCreated = FALSE;
}
theCurrentPraatObjects -> totalBeingCreated = 0;
praat_show ();
}
}
static void gui_cb_list (void *void_me, GuiListEvent event) {
(void) event; (void) void_me;
int IOBJECT, first = TRUE;
WHERE (SELECTED) {
SELECTED = FALSE;
long readableClassId = ((Thing) theCurrentPraatObjects -> list [IOBJECT]. object) -> classInfo -> sequentialUniqueIdOfReadableClass;
theCurrentPraatObjects -> numberOfSelected [readableClassId] --;
Melder_assert (theCurrentPraatObjects -> numberOfSelected [readableClassId] >= 0);
}
theCurrentPraatObjects -> totalSelection = 0;
long numberOfSelected, *selected = GuiList_getSelectedPositions (praatList_objects, & numberOfSelected);
if (selected != NULL) {
for (long iselected = 1; iselected <= numberOfSelected; iselected ++) {
IOBJECT = selected [iselected];
SELECTED = TRUE;
long readableClassId = ((Thing) theCurrentPraatObjects -> list [IOBJECT]. object) -> classInfo -> sequentialUniqueIdOfReadableClass;
theCurrentPraatObjects -> numberOfSelected [readableClassId] ++;
Melder_assert (theCurrentPraatObjects -> numberOfSelected [readableClassId] > 0);
UiHistory_write (first ? L"\nselect " : L"\nplus ");
UiHistory_write (FULL_NAME);
first = FALSE;
theCurrentPraatObjects -> totalSelection += 1;
}
NUMvector_free <long> (selected, 1);
}
praat_show ();
}
void praat_list_renameAndSelect (int position, const wchar_t *name) {
if (! theCurrentPraatApplication -> batch) {
GuiList_replaceItem (praatList_objects, name, position); /* Void if name equal. */
if (! Melder_backgrounding)
GuiList_selectItem (praatList_objects, position);
}
}
/***** objects *****/
void praat_name2 (wchar *name, ClassInfo klas1, ClassInfo klas2) {
int i1 = 1, i2;
wchar_t *name1, *name2;
while (theCurrentPraatObjects -> list [i1]. selected == 0 || theCurrentPraatObjects -> list [i1]. klas != klas1) i1 ++;
i2 = 1; /* This late initialization works around a Think C BUG. */
while (theCurrentPraatObjects -> list [i2]. selected == 0 || theCurrentPraatObjects -> list [i2]. klas != klas2) i2 ++;
name1 = wcschr (theCurrentPraatObjects -> list [i1]. name, ' ') + 1;
name2 = wcschr (theCurrentPraatObjects -> list [i2]. name, ' ') + 1;
if (wcsequ (name1, name2))
wcscpy (name, name1);
else
swprintf (name, 200, L"%ls_%ls", name1, name2);
}
void praat_removeObject (int i) {
int j, ieditor;
praat_remove (i); /* Dangle. */
for (j = i; j < theCurrentPraatObjects -> n; j ++)
theCurrentPraatObjects -> list [j] = theCurrentPraatObjects -> list [j + 1]; /* Undangle but create second references. */
theCurrentPraatObjects -> list [theCurrentPraatObjects -> n]. name = NULL; /* Undangle or remove second reference. */
theCurrentPraatObjects -> list [theCurrentPraatObjects -> n]. object = NULL; /* Undangle or remove second reference. */
theCurrentPraatObjects -> list [theCurrentPraatObjects -> n]. selected = 0;
for (ieditor = 0; ieditor < praat_MAXNUM_EDITORS; ieditor ++)
theCurrentPraatObjects -> list [theCurrentPraatObjects -> n]. editors [ieditor] = NULL; /* Undangle or remove second reference. */
MelderFile_setToNull (& theCurrentPraatObjects -> list [theCurrentPraatObjects -> n]. file); /* Undangle or remove second reference. */
-- theCurrentPraatObjects -> n;
if (! theCurrentPraatApplication -> batch) {
GuiList_deleteItem (praatList_objects, i);
#ifdef UNIX
//XtVaSetValues (praatList_objects, XmNvisibleItemCount, theCurrentPraatObjects -> n + 1, NULL);
#endif
}
}
static void praat_exit (int exit_code) {
int IOBJECT;
#ifdef _WIN32
if (! theCurrentPraatApplication -> batch)
XtDestroyWidget (theCurrentPraatApplication -> topShell);
#endif
praat_picture_exit ();
praat_statistics_exit (); /* Record total memory use across sessions. */
/*
* Stop receiving messages.
*/
#if defined (UNIX)
/*
* We are going to delete the process id ("pid") file, if it's ours.
*/
if (pidFile. path [0]) {
try {
/*
* To see whether we own the pid file,
* we look into it to see whether its pid equals our pid.
* If not, then we are probably living in an old invocation of the program,
* and the pid file was written by the latest invocation of the program,
* which owns the pid (this means sendpraat can only send to the latest Praat if more than one are open).
*/
autofile f = Melder_fopen (& pidFile, "r");
long pid;
if (fscanf (f, "%ld", & pid) < 1) throw MelderError ();
f.close (& pidFile);
if (pid == getpid ()) { // is the pid in the pid file equal to our pid?
MelderFile_delete (& pidFile); // ...then we own the pid file and can delete it
}
} catch (MelderError) {
Melder_clearError (); // if the pid file is somehow missing or corrupted, we just ignore that
}
}
#endif
/*
* Save the preferences.
*/
Preferences_write (& prefs5File);
MelderFile_setMacTypeAndCreator (& prefs5File, 'pref', 'PpgB');
/*
* Save the script buttons.
*/
if (! theCurrentPraatApplication -> batch) {
try {
autofile f = Melder_fopen (& buttons5File, "wb");
fwprintf (f, L"\ufeff# Buttons (1).\n");
fwprintf (f, L"# This file is generated automatically when you quit the %ls program.\n", Melder_peekUtf8ToWcs (praatP.title));
fwprintf (f, L"# It contains the buttons that you added interactively to the fixed or dynamic menus,\n");
fwprintf (f, L"# and the buttons that you hid or showed.\n\n");
praat_saveMenuCommands (f);
praat_saveAddedActions (f);
f.close (& buttons5File);
MelderFile_setMacTypeAndCreator (& buttons5File, 'pref', 'PpgB');
} catch (MelderError) {
Melder_clearError ();
}
}
/*
* Flush the file-based objects.
*/
WHERE_DOWN (! MelderFile_isNull (& theCurrentPraatObjects -> list [IOBJECT]. file)) praat_remove (IOBJECT);
Melder_files_cleanUp (); /* If a URL is open. */
/*
* Finally, leave the program.
*/
exit (exit_code);
}
static void cb_Editor_destruction (Editor me, void *closure) {
(void) closure;
removeAllReferencesToEditor (me); // remove reference(s) to moribund Editor
}
static void cb_Editor_dataChanged (Editor me, void *closure) {
(void) closure;
for (int iobject = 1; iobject <= theCurrentPraatObjects -> n; iobject ++) {
bool editingThisObject = false;
/*
* Am I editing this object?
*/
for (int ieditor = 0; ieditor < praat_MAXNUM_EDITORS; ieditor ++) {
if (theCurrentPraatObjects -> list [iobject]. editors [ieditor] == me) {
editingThisObject = true;
}
}
if (editingThisObject) {
/*
* Notify all other editors associated with this object.
*/
for (int ieditor = 0; ieditor < praat_MAXNUM_EDITORS; ieditor ++) {
Editor otherEditor = (Editor) theCurrentPraatObjects -> list [iobject]. editors [ieditor];
if (otherEditor != NULL && otherEditor != me) {
otherEditor -> dataChanged ();
}
}
}
}
}
static void cb_Editor_publication (Editor me, void *closure, Data publication) {
/*
The default publish callback.
Works nicely if the publisher invents a name.
*/
(void) me;
(void) closure;
try {
praat_new1 (publication, NULL);
} catch (MelderError) {
Melder_flushError (NULL);
}
praat_updateSelection ();
}
int praat_installEditor (Editor editor, int IOBJECT) {
if (editor == NULL) return 0;
for (int ieditor = 0; ieditor < praat_MAXNUM_EDITORS; ieditor ++) {
if (EDITOR [ieditor] == NULL) {
EDITOR [ieditor] = editor;
editor -> setDestructionCallback (cb_Editor_destruction, NULL);
editor -> setDataChangedCallback (cb_Editor_dataChanged, NULL);
if (! editor -> d_publicationCallback) editor -> setPublicationCallback (cb_Editor_publication, NULL);
return 1;
}
}
forget (editor);
Melder_throw ("(praat_installEditor:) Cannot have more than ", praat_MAXNUM_EDITORS, " editors with one object.");
}
int praat_installEditor2 (Editor editor, int i1, int i2) {
if (editor == NULL) return 0;
int ieditor1 = 0;
for (; ieditor1 < praat_MAXNUM_EDITORS; ieditor1 ++)
if (theCurrentPraatObjects -> list [i1]. editors [ieditor1] == NULL)
break;
int ieditor2 = 0;
for (; ieditor2 < praat_MAXNUM_EDITORS; ieditor2 ++)
if (theCurrentPraatObjects -> list [i2]. editors [ieditor2] == NULL)
break;
if (ieditor1 < praat_MAXNUM_EDITORS && ieditor2 < praat_MAXNUM_EDITORS) {
theCurrentPraatObjects -> list [i1]. editors [ieditor1] = theCurrentPraatObjects -> list [i2]. editors [ieditor2] = editor;
editor -> setDestructionCallback (cb_Editor_destruction, NULL);
editor -> setDataChangedCallback (cb_Editor_dataChanged, NULL);
if (! editor -> d_publicationCallback) editor -> setPublicationCallback (cb_Editor_publication, NULL);
} else {
forget (editor);
Melder_throw ("(praat_installEditor2:) Cannot have more than ", praat_MAXNUM_EDITORS, " editors with one object.");
}
return 1;
}
int praat_installEditor3 (Editor editor, int i1, int i2, int i3) {
if (! editor) return 0;
int ieditor1 = 0;
for (; ieditor1 < praat_MAXNUM_EDITORS; ieditor1 ++)
if (theCurrentPraatObjects -> list [i1]. editors [ieditor1] == NULL)
break;
int ieditor2 = 0;
for (; ieditor2 < praat_MAXNUM_EDITORS; ieditor2 ++)
if (theCurrentPraatObjects -> list [i2]. editors [ieditor2] == NULL)
break;
int ieditor3 = 0;
for (; ieditor3 < praat_MAXNUM_EDITORS; ieditor3 ++)
if (theCurrentPraatObjects -> list [i3]. editors [ieditor3] == NULL)
break;
if (ieditor1 < praat_MAXNUM_EDITORS && ieditor2 < praat_MAXNUM_EDITORS && ieditor3 < praat_MAXNUM_EDITORS) {
theCurrentPraatObjects -> list [i1]. editors [ieditor1] = theCurrentPraatObjects -> list [i2]. editors [ieditor2] = theCurrentPraatObjects -> list [i3]. editors [ieditor3] = editor;
editor -> setDestructionCallback (cb_Editor_destruction, NULL);
editor -> setDataChangedCallback (cb_Editor_dataChanged, NULL);
if (! editor -> d_publicationCallback) editor -> setPublicationCallback (cb_Editor_publication, NULL);
} else {
forget (editor);
Melder_throw ("(praat_installEditor3:) Cannot have more than ", praat_MAXNUM_EDITORS, " editors with one object.");
}
return 1;
}
int praat_installEditorN (Editor editor, Ordered objects) {
long iOrderedObject, iPraatObject;
if (editor == NULL) return 0;
/*
* First check whether all objects in the Ordered are also in the List of Objects (Praat crashes if not),
* and check whether there is room to add an editor for each.
*/
for (iOrderedObject = 1; iOrderedObject <= objects -> size; iOrderedObject ++) {
Data object = (Data) objects -> item [iOrderedObject];
for (iPraatObject = 1; iPraatObject <= theCurrentPraatObjects -> n; iPraatObject ++) {
if (object == theCurrentPraatObjects -> list [iPraatObject]. object) {
int ieditor = 0;
for (; ieditor < praat_MAXNUM_EDITORS; ieditor ++) {
if (theCurrentPraatObjects -> list [iPraatObject]. editors [ieditor] == NULL) {
break;
}
}
if (ieditor >= praat_MAXNUM_EDITORS) {
forget (editor);
Melder_throw ("Cannot view the same object in more than ", praat_MAXNUM_EDITORS, " windows.");
}
break;
}
}
Melder_assert (iPraatObject <= theCurrentPraatObjects -> n); /* An element of the Ordered does not occur in the List of Objects. */
}
/*
* There appears to be room for all elements of the Ordered. The editor window can appear. Install the editor in all objects.
*/
for (iOrderedObject = 1; iOrderedObject <= objects -> size; iOrderedObject ++) {
Data object = (Data) objects -> item [iOrderedObject];
for (iPraatObject = 1; iPraatObject <= theCurrentPraatObjects -> n; iPraatObject ++) {
if (object == theCurrentPraatObjects -> list [iPraatObject]. object) {
int ieditor = 0;
for (; ieditor < praat_MAXNUM_EDITORS; ieditor ++) {
if (theCurrentPraatObjects -> list [iPraatObject]. editors [ieditor] == NULL) {
theCurrentPraatObjects -> list [iPraatObject]. editors [ieditor] = editor;
editor -> setDestructionCallback (cb_Editor_destruction, NULL);
editor -> setDataChangedCallback (cb_Editor_dataChanged, NULL);
if (! editor -> d_publicationCallback) editor -> setPublicationCallback (cb_Editor_publication, NULL);
break;
}
}
Melder_assert (ieditor < praat_MAXNUM_EDITORS); /* We just checked, but nevertheless. */
break;
}
}
Melder_assert (iPraatObject <= theCurrentPraatObjects -> n); /* We already checked, but still. */
}
return 1;
}
void praat_dataChanged (Any object) {
/*
* This function can be called at error time, which is weird.
*/
wchar *saveError = NULL;
bool duringError = Melder_hasError ();
if (duringError) {
saveError = Melder_wcsdup_f (Melder_getError ());
Melder_clearError ();
}
int IOBJECT;
WHERE (OBJECT == object) {
for (int ieditor = 0; ieditor < praat_MAXNUM_EDITORS; ieditor ++) {
Editor editor = (Editor) EDITOR [ieditor];
if (editor != NULL) {
editor -> dataChanged ();
}
}
}
if (duringError) {
Melder_error_ (saveError); // BUG: this appends an empty newline to the original error message
Melder_free (saveError); // BUG: who will catch the error?
}
}
static void helpProc (const wchar_t *query) {
if (theCurrentPraatApplication -> batch) {
Melder_flushError ("Cannot view manual from batch.");
return;
}
try {
Manual_create (theCurrentPraatApplication -> topShell, query, theCurrentPraatApplication -> manPages, false);
} catch (MelderError) {
Melder_flushError ("help: no help on \"%ls\".", query);
}
}
static int publishProc (void *anything) {
try {
praat_new1 ((Data) anything, NULL);
praat_updateSelection ();
return 1;
} catch (MelderError) {
Melder_throw ("Not published.");
}
}
/***** QUIT *****/
FORM (Quit, L"Confirm Quit", L"Quit")
LABEL (L"label", L"You have objects in your list!")
OK
{
wchar_t prompt [300];
if (ScriptEditors_dirty ()) {
if (theCurrentPraatObjects -> n)
swprintf (prompt, 300, L"You have objects and unsaved scripts! Do you still want to quit %ls?", Melder_peekUtf8ToWcs (praatP.title));
else
swprintf (prompt, 300, L"You have unsaved scripts! Do you still want to quit %ls?", Melder_peekUtf8ToWcs (praatP.title));
SET_STRING (L"label", prompt);
} else if (theCurrentPraatObjects -> n) {
swprintf (prompt, 300, L"You have objects in your list! Do you still want to quit %ls?", Melder_peekUtf8ToWcs (praatP.title));
SET_STRING (L"label", prompt);
} else {
praat_exit (0);
}
}
DO
praat_exit (0);
END
static void gui_cb_quit (GUI_ARGS) {
(void) w; (void) void_me; (void) call;
DO_Quit (NULL, NULL, NULL, NULL, NULL, NULL);
}
#if gtk
static void gui_cb_quit_gtk (void *p) {
DO_Quit (NULL, NULL, NULL, NULL, NULL, NULL);
}
#endif
void praat_dontUsePictureWindow (void) { praatP.dontUsePictureWindow = TRUE; }
/********** INITIALIZATION OF THE PRAAT SHELL **********/
#if defined (UNIX)
static gboolean cb_userMessage (GtkWidget widget, GdkEventClient *event, gpointer user_data) {
(void) widget;
(void) user_data;
autofile f;
try {
f.reset (Melder_fopen (& messageFile, "r"));
} catch (MelderError) {
Melder_clearError ();
return true; // OK
}
long pid;
int narg = fscanf (f, "#%ld", & pid);
f.close (& messageFile);
{ // scope
autoPraatBackground background;
try {
praat_executeScriptFromFile (& messageFile, NULL);
} catch (MelderError) {
Melder_error_ (Melder_peekUtf8ToWcs (praatP.title), L": message not completely handled.");
Melder_flushError (NULL);
}
}
if (narg) kill (pid, SIGUSR2);
return true;
}
#elif defined (_WIN32)
static int cb_userMessage (void) {
autoPraatBackground background;
try {
praat_executeScriptFromFile (& messageFile, NULL);
} catch (MelderError) {
Melder_error_ (Melder_peekUtf8ToWcs (praatP.title), L": message not completely handled.");
Melder_flushError (NULL);
}
return 0;
}
extern "C" char *sendpraat (void *display, const char *programName, long timeOut, const char *text);
extern "C" wchar *sendpraatW (void *display, const wchar *programName, long timeOut, const wchar *text);
static void cb_openDocument (MelderFile file) {
wchar text [500];
wchar *s = file -> path;
swprintf (text, 500, L"Read from file... %ls", s [0] == ' ' && s [1] == '\"' ? s + 2 : s [0] == '\"' ? s + 1 : s);
long l = wcslen (text);
if (l > 0 && text [l - 1] == '\"') text [l - 1] = '\0';
sendpraatW (NULL, Melder_peekUtf8ToWcs (praatP.title), 0, text);
}
#elif defined (macintosh)
static int cb_userMessageA (char *messageA) {
autoPraatBackground background;
autostring message = Melder_8bitToWcs (messageA, 0);
try {
praat_executeScriptFromText (message.peek());
} catch (MelderError) {
Melder_error_ (praatP.title, ": message not completely handled.");
Melder_flushError (NULL);
}
return 0;
}
static int cb_userMessageW (wchar *message) {
autoPraatBackground background;
try {
praat_executeScriptFromText (message);
} catch (MelderError) {
Melder_error_ (praatP.title, ": message not completely handled.");
Melder_flushError (NULL);
}
return 0;
}
static int cb_quitApplication (void) {
DO_Quit (NULL, NULL, NULL, NULL, NULL, NULL);
return 0;
}
#endif
static wchar * thePraatStandAloneScriptText = NULL;
void praat_setStandAloneScriptText (wchar *text) {
thePraatStandAloneScriptText = text;
}
void praat_init (const char *title, unsigned int argc, char **argv) {
static char truncatedTitle [300]; /* Static because praatP.title will point into it. */
#if defined (UNIX)
setlocale (LC_ALL, "C");
#elif defined (macintosh)
setlocale (LC_ALL, "en_US"); // required to make swprintf work correctly; the default "C" locale does not do that!
#endif
char *p;
#ifdef macintosh
Gestalt ('sysv', (long *) & Melder_systemVersion);
#endif
/*
Initialize numerical libraries.
*/
NUMmachar ();
NUMinit ();
Melder_alloc_init ();
/*
Remember the current directory. Only useful for scripts run from batch.
*/
Melder_rememberShellDirectory ();
/*
* Install the preferences of the Praat shell, and set the defaults.
*/
praat_statistics_prefs (); // Number of sessions, memory used...
praat_picture_prefs (); // Font...
Editor_prefs (); // Erase picture first...
HyperPage_prefs (); // Font...
Site_prefs (); // Print command...
Melder_audio_prefs (); // Use speaker (Sun & HP), output gain (HP)...
Melder_textEncoding_prefs ();
Printer_prefs (); // Paper size, printer command...
TextEditor_prefs (); // Font size...
unsigned int iarg_batchName = 1;
#if defined (UNIX) || defined (macintosh) || defined (_WIN32) && defined (CONSOLE_APPLICATION)
/*
* Running the Praat shell from the Unix command line,
* or running PRAATCON.EXE from the Windows command prompt:
* <programName> <scriptFileName>
*/
if (argc > iarg_batchName
&& argv [iarg_batchName] [0] == '-'
&& argv [iarg_batchName] [1] == 'a'
&& argv [iarg_batchName] [2] == '\0')
{
Melder_consoleIsAnsi = true;
iarg_batchName ++;
}
//fprintf (stdout, "Console <%d> <%s>", Melder_consoleIsAnsi, argv [1]);
bool hasCommandLineInput =
argc > iarg_batchName
&& argv [iarg_batchName] [0] == '-'
&& argv [iarg_batchName] [1] == '\0';
MelderString_copy (& theCurrentPraatApplication -> batchName,
hasCommandLineInput ? L""
: argc > iarg_batchName && argv [iarg_batchName] [0] != '-' /* funny Mac test */ ? Melder_peekUtf8ToWcs (argv [iarg_batchName])
: L"");
Melder_batch = theCurrentPraatApplication -> batchName.string [0] != '\0' || thePraatStandAloneScriptText != NULL;
#if defined (_WIN32) && defined (CONSOLE_APPLICATION)
if (! Melder_batch) {
fprintf (stderr, "Usage: PRAATCON <scriptFileName>\n");
exit (0);
}
#endif
/*
* Running the Praat shell from the command line:
* praat -
*/
if (hasCommandLineInput) {
Melder_batch = true;
doingCommandLineInterface = TRUE; /* Read from stdin. */
}
/* Take from 'title' ("myProg 3.2" becomes "myProg") or from command line ("/ifa/praat" becomes "praat"). */
strcpy (truncatedTitle, argc && argv [0] [0] ? argv [0] : title && title [0] ? title : "praat");
//Melder_fatal ("<%s>", argv [0]);
#else
#if defined (_WIN32)
MelderString_copy (& theCurrentPraatApplication -> batchName,
argv [3] ? Melder_peekUtf8ToWcs (argv [3]) : L""); /* The command line. */
#endif
Melder_batch = false; // PRAAT.EXE on Windows is always interactive
strcpy (truncatedTitle, title && title [0] ? title : "praat");
#endif
theCurrentPraatApplication -> batch = Melder_batch;
/*
* Construct a program name like "myProg 3.2" by removing directory path.
*/
p = strrchr (truncatedTitle, Melder_DIRECTORY_SEPARATOR);
praatP.title = p ? p + 1 : truncatedTitle;
/*
* Construct a program name like "myProg" for file and directory names.
*/
strcpy (programName, praatP.title);
if ((p = strchr (programName, ' ')) != NULL) *p = '\0';
#if defined (_WIN32)
if ((p = strchr (programName, '.')) != NULL) *p = '\0'; /* Chop off ".exe". */
#endif
/*
* Construct a main-window title like "MyProg 3.2".
*/
praatP.title [0] = toupper (praatP.title [0]);
/*
* Get home directory, e.g. "/home/miep/", or "/Users/miep/", or just "/".
*/
Melder_getHomeDir (& homeDir);
/*
* Get the program's private directory:
* "/u/miep/myProg-dir" (Unix)
* "/Users/miep/Library/Preferences/MyProg Prefs" (Macintosh)
* "C:\Documents and Settings\Miep\MyProg" (Windows)
* and construct a preferences-file name and a script-buttons-file name like
* /u/miep/.myProg-dir/prefs (Unix)
* /u/miep/.myProg-dir/script_buttons
* or
* /Users/miep/Library/Preferences/MyProg Prefs/Prefs
* /Users/miep/Library/Preferences/MyProg Prefs/Buttons
* or
* C:\Documents and Settings\Miep\MyProg\Preferences.ini
* C:\Documents and Settings\Miep\MyProg\Buttons.ini
* On Unix, also create names for process-id and message files.
*/
{
structMelderDir prefParentDir = { { 0 } }; /* Directory under which to store our preferences directory. */
wchar_t name [256];
Melder_getPrefDir (& prefParentDir);
/*
* Make sure that the program's private directory exists.
*/
#if defined (UNIX)
swprintf (name, 256, L".%ls-dir", Melder_utf8ToWcs (programName)); /* For example .myProg-dir */
#elif defined (macintosh)
swprintf (name, 256, L"%ls Prefs", Melder_utf8ToWcs (praatP.title)); /* For example MyProg Prefs */
#elif defined (_WIN32)
swprintf (name, 256, L"%ls", Melder_utf8ToWcs (praatP.title)); /* For example MyProg */
#endif
#if defined (UNIX) || defined (macintosh)
Melder_createDirectory (& prefParentDir, name, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
#else
Melder_createDirectory (& prefParentDir, name, 0);
#endif
MelderDir_getSubdir (& prefParentDir, name, & praatDir);
#if defined (UNIX)
MelderDir_getFile (& praatDir, L"prefs", & prefs4File);
MelderDir_getFile (& praatDir, L"prefs5", & prefs5File);
MelderDir_getFile (& praatDir, L"buttons", & buttons4File);
MelderDir_getFile (& praatDir, L"buttons5", & buttons5File);
MelderDir_getFile (& praatDir, L"pid", & pidFile);
MelderDir_getFile (& praatDir, L"message", & messageFile);
#elif defined (_WIN32)
MelderDir_getFile (& praatDir, L"Preferences.ini", & prefs4File);
MelderDir_getFile (& praatDir, L"Preferences5.ini", & prefs5File);
MelderDir_getFile (& praatDir, L"Buttons.ini", & buttons4File);
MelderDir_getFile (& praatDir, L"Buttons5.ini", & buttons5File);
MelderDir_getFile (& praatDir, L"Message.txt", & messageFile);
#elif defined (macintosh)
MelderDir_getFile (& praatDir, L"Prefs", & prefs4File); /* We invite trouble if we call it Preferences! */
MelderDir_getFile (& praatDir, L"Prefs5", & prefs5File);
MelderDir_getFile (& praatDir, L"Buttons", & buttons4File);
MelderDir_getFile (& praatDir, L"Buttons5", & buttons5File);
#endif
}
#if defined (UNIX)
if (! Melder_batch) {
/*
* Make sure that the directory /u/miep/.myProg-dir exists,
* and write our process id into the pid file.
* Messages from "sendpraat" are caught very early this way,
* though they will be responded to much later.
*/
try {
autofile f = Melder_fopen (& pidFile, "w");
fprintf (f, "%ld", (long) getpid ());
f.close (& pidFile);
#if motif
signal (SIGUSR1, handleMessage);
#endif
} catch (MelderError) {
Melder_clearError ();
}
}
#elif defined (_WIN32)
if (! Melder_batch)
motif_win_setUserMessageCallback (cb_userMessage);
#elif defined (macintosh)
#if useCarbon
if (! Melder_batch) {
motif_mac_setUserMessageCallbackA (cb_userMessageA);
motif_mac_setUserMessageCallbackW (cb_userMessageW);
Gui_setQuitApplicationCallback (cb_quitApplication);
}
#else
if (! Melder_batch) {
Gui_setQuitApplicationCallback (cb_quitApplication);
}
#endif
#endif
/*
* Make room for commands.
*/
praat_actions_init ();
praat_menuCommands_init ();
GuiObject raam = NULL;
if (Melder_batch) {
#if defined (UNIX) || defined (macintosh) || defined (_WIN32) && defined (CONSOLE_APPLICATION)
MelderString_empty (& theCurrentPraatApplication -> batchName);
for (unsigned int i = iarg_batchName; i < argc; i ++) {
int needsQuoting = strchr (argv [i], ' ') != NULL && (i == iarg_batchName || i < argc - 1);
if (i > 1) MelderString_append (& theCurrentPraatApplication -> batchName, L" ");
if (needsQuoting) MelderString_append (& theCurrentPraatApplication -> batchName, L"\"");
MelderString_append (& theCurrentPraatApplication -> batchName, Melder_peekUtf8ToWcs (argv [i]));
if (needsQuoting) MelderString_append (& theCurrentPraatApplication -> batchName, L"\"");
}
#elif defined (_WIN32)
MelderString_copy (& theCurrentPraatApplication -> batchName, Melder_peekUtf8ToWcs (argv [3]));
#endif
} else {
char objectWindowTitle [100];
Machine_initLookAndFeel (argc, argv);
sprintf (objectWindowTitle, "%s Objects", praatP.title);
#if gtk
g_set_application_name (title);
raam = GuiWindow_create (NULL, -1, Gui_AUTOMATIC, -1, 600, Melder_peekUtf8ToWcs (objectWindowTitle), gui_cb_quit_gtk, NULL, 0);
theCurrentPraatApplication -> topShell = gtk_widget_get_parent (GTK_WIDGET (raam));
GuiObject_show (theCurrentPraatApplication -> topShell);
#elif defined (_WIN32)
argv [0] = & praatP. title [0]; /* argc == 4 */
Gui_setOpenDocumentCallback (cb_openDocument);
theCurrentPraatApplication -> topShell = GuiAppInitialize ("Praatwulg", NULL, 0, & argc, argv, NULL, NULL);
double x, y;
Gui_getWindowPositioningBounds (& x, & y, NULL, NULL);
XtVaSetValues (theCurrentPraatApplication -> topShell, XmNdeleteResponse, XmDO_NOTHING, XmNtitle, objectWindowTitle,
XmNx, (int) x + 10,
XmNy, (int) y + 0,
NULL);
XtVaSetValues (theCurrentPraatApplication -> topShell, XmNheight, WINDOW_HEIGHT, NULL);
/* Catch Window Manager "Close" and "Quit". */
XmAddWMProtocolCallback (theCurrentPraatApplication -> topShell, 'delw', gui_cb_quit, 0);
#elif defined (macintosh)
#if useCarbon
theCurrentPraatApplication -> topShell = GuiAppInitialize ("Praatwulg", NULL, 0, & argc, argv, NULL, NULL);
double x, y;
Gui_getWindowPositioningBounds (& x, & y, NULL, NULL);
XtVaSetValues (theCurrentPraatApplication -> topShell, XmNdeleteResponse, XmDO_NOTHING, XmNtitle, objectWindowTitle,
XmNx, (int) x + 10,
XmNy, (int) y + 0,
NULL);
XtVaSetValues (theCurrentPraatApplication -> topShell, XmNheight, WINDOW_HEIGHT, NULL);
/* Catch Window Manager "Close" and "Quit". */
XmAddWMProtocolCallback (theCurrentPraatApplication -> topShell, 'delw', gui_cb_quit, 0);
#else
#endif
#endif
}
Thing_recognizeClassesByName (classCollection, classStrings, classManPages, classSortedSetOfString, NULL);
if (Melder_batch) {
Melder_backgrounding = true;
praat_addMenus (NULL);
praat_addFixedButtons (NULL);
} else {
GuiObject Raam = NULL;
#if gtk
GuiObject raHoriz, raLeft; /* I want to have more possibilities for GTK widgets */
#else
#define raHoriz Raam
#define raLeft Raam
#endif
#ifdef macintosh
MelderGui_create (theCurrentPraatApplication -> topShell); /* BUG: default Melder_assert would call printf recursively!!! */
#endif
#if gtk
Raam = raam;
#elif motif
Raam = XmCreateForm (theCurrentPraatApplication -> topShell, "raam", NULL, 0);
#endif
#ifdef macintosh
GuiObject_size (Raam, WINDOW_WIDTH, Gui_AUTOMATIC);
praatP.topBar = Gui_addMenuBar (Raam);
GuiObject_show (praatP.topBar);
#endif
praatP.menuBar = Gui_addMenuBar (Raam);
praat_addMenus (praatP.menuBar);
GuiObject_show (praatP.menuBar);
#ifndef UNIX
GuiObject_size (Raam, WINDOW_WIDTH, Gui_AUTOMATIC);
#endif
#if gtk
raHoriz = gtk_hpaned_new ();
gtk_container_add (GTK_CONTAINER (Raam), GTK_WIDGET (raHoriz));
raLeft = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (raHoriz), GTK_WIDGET (raLeft));
#else
GuiLabel_createShown (raLeft, 3, -250, Machine_getMainWindowMenuBarHeight () + 5, Gui_AUTOMATIC, L"Objects:", 0);
#endif
praatList_objects = GuiList_create (raLeft, 0, -250, Machine_getMainWindowMenuBarHeight () + 26, -100, true, L" Objects ");
GuiList_setSelectionChangedCallback (praatList_objects, gui_cb_list, 0);
//XtVaSetValues (praatList_objects, XmNvisibleItemCount, 20, NULL);
GuiObject_show (praatList_objects);
praat_addFixedButtons (raLeft);
praat_actions_createDynamicMenu (raHoriz, 250);
#if gtk
GuiObject_show (raLeft);
GuiObject_show (raHoriz);
#endif
GuiObject_show (Raam);
#ifdef UNIX
try {
autofile f = Melder_fopen (& pidFile, "a");
#if gtk
fprintf (f, " %ld", (long) GDK_WINDOW_XID (GDK_DRAWABLE (GTK_WIDGET (theCurrentPraatApplication -> topShell) -> window)));
#else
fprintf (f, " %ld", (long) XtWindow (theCurrentPraatApplication -> topShell));
#endif
f.close (& pidFile);
} catch (MelderError) {
Melder_clearError ();
}
#endif
#ifdef UNIX
Preferences_read (MelderFile_readable (& prefs5File) ? & prefs5File : & prefs4File);
#endif
#if ! defined (CONSOLE_APPLICATION) && ! defined (macintosh)
MelderGui_create (theCurrentPraatApplication -> topShell); /* Mac: done this earlier. */
#endif
Melder_setHelpProc (helpProc);
}
Melder_setPublishProc (publishProc);
theCurrentPraatApplication -> manPages = ManPages_create ();
if (! praatP.dontUsePictureWindow) praat_picture_init ();
}
static void executeStartUpFile (MelderDir startUpDirectory, const wchar_t *fileNameTemplate) {
wchar name [256];
swprintf (name, 256, fileNameTemplate, Melder_peekUtf8ToWcs (programName));
if (! MelderDir_isNull (startUpDirectory)) { // Should not occur on modern systems.
structMelderFile startUp = { 0 };
MelderDir_getFile (startUpDirectory, name, & startUp);
if (! MelderFile_readable (& startUp))
return; // it's OK if the file doesn't exist
try {
praat_executeScriptFromFile (& startUp, NULL);
} catch (MelderError) {
Melder_error_ (praatP.title, ": start-up file ", & startUp, " not completed.");
Melder_flushError (NULL);
}
}
}
#if gtk
#include <gdk/gdkkeysyms.h>
static gint theKeySnooper (GtkWidget *widget, GdkEventKey *event, gpointer data) {
//Melder_casual ("keyval %ld, type %ld", (long) event -> keyval, (long) event -> type);
if ((event -> keyval == GDK_Tab || event -> keyval == GDK_KEY_ISO_Left_Tab) && event -> type == GDK_KEY_PRESS) {
//Melder_casual ("tab key pressed in window %ld", widget);
if (event -> state == 0) {
if (GTK_IS_WINDOW (widget)) {
GtkWidget *shell = gtk_widget_get_toplevel (GTK_WIDGET (widget));
//Melder_casual ("tab pressed in window %ld", shell);
void (*tabCallback) (GuiObject, XtPointer, XtPointer) = (void (*) (GuiObject, XtPointer, XtPointer)) g_object_get_data (G_OBJECT (widget), "tabCallback");
if (tabCallback) {
//Melder_casual ("a tab callback exists");
void *tabClosure = g_object_get_data (G_OBJECT (widget), "tabClosure");
tabCallback (widget, tabClosure, tabClosure);
return TRUE;
}
}
} else if (event -> state == GDK_SHIFT_MASK) { // BUG:
if (GTK_IS_WINDOW (widget)) {
GtkWidget *shell = gtk_widget_get_toplevel (GTK_WIDGET (widget));
//Melder_casual ("shift-tab pressed in window %ld", shell);
void (*tabCallback) (GuiObject, XtPointer, XtPointer) = (void (*) (GuiObject, XtPointer, XtPointer)) g_object_get_data (G_OBJECT (widget), "shiftTabCallback");
if (tabCallback) {
//Melder_casual ("a shift tab callback exists");
void *tabClosure = g_object_get_data (G_OBJECT (widget), "shiftTabClosure");
tabCallback (widget, tabClosure, tabClosure);
return TRUE;
}
}
}
}
return FALSE; // pass event on
}
#endif
void praat_run (void) {
FILE *f;
praat_addMenus2 ();
#ifdef macintosh
praat_addMenuCommand (L"Objects", L"Praat", L"Quit", 0, praat_HIDDEN, DO_Quit); // the Quit command is needed for scripts, not for the GUI
#else
praat_addMenuCommand (L"Objects", L"Praat", L"-- quit --", 0, 0, 0);
praat_addMenuCommand (L"Objects", L"Praat", L"Quit", 0, praat_UNHIDABLE + 'Q', DO_Quit);
#endif
/*
* Read the preferences file, and notify those who want to be notified of this,
* namely, those who already have a window (namely, the Picture window),
* and those that regard the start of a new session as a meaningful event
* (namely, the session counter and the cross-session memory counter).
*/
Preferences_read (MelderFile_readable (& prefs5File) ? & prefs5File : & prefs4File);
if (! praatP.dontUsePictureWindow) praat_picture_prefsChanged ();
praat_statistics_prefsChanged ();
//Melder_error3 (L"batch name <<", theCurrentPraatApplication -> batchName.string, L">>");
//Melder_flushError (NULL);
praatP.phase = praat_STARTING_UP;
#if defined (UNIX) || defined (macintosh)
structMelderDir usrLocal = { { 0 } };
Melder_pathToDir (L"/usr/local", & usrLocal);
executeStartUpFile (& usrLocal, L"%ls-startUp");
#endif
#if defined (UNIX) || defined (macintosh)
executeStartUpFile (& homeDir, L".%ls-user-startUp"); // not on Windows (empty file name error)
#endif
#if defined (UNIX) || defined (macintosh) || defined (_WIN32)
executeStartUpFile (& homeDir, L"%ls-user-startUp");
#endif
/*
* Plugins.
* The Praat phase should remain praat_STARTING_UP,
* because any added commands must not be included in the buttons file.
*/
structMelderFile searchPattern = { 0 };
MelderDir_getFile (& praatDir, L"plugin_*", & searchPattern);
try {
autoStrings directoryNames = Strings_createAsDirectoryList (Melder_fileToPath (& searchPattern));
if (directoryNames -> numberOfStrings > 0) {
for (long i = 1; i <= directoryNames -> numberOfStrings; i ++) {
structMelderDir pluginDir = { { 0 } };
structMelderFile plugin = { 0 };
MelderDir_getSubdir (& praatDir, directoryNames -> strings [i], & pluginDir);
MelderDir_getFile (& pluginDir, L"setup.praat", & plugin);
if (MelderFile_readable (& plugin)) {
try {
praat_executeScriptFromFile (& plugin, NULL);
} catch (MelderError) {
Melder_error_ (praatP.title, ": plugin ", & plugin, " contains an error.");
Melder_flushError (NULL);
}
}
}
}
} catch (MelderError) {
Melder_clearError (); // in case Strings_createAsDirectoryList () threw an error
}
Melder_assert (wcsequ (Melder_double (1.5), L"1.5")); // check locale settings; because of the required file portability Praat cannot stand "1,5"
{ int dummy = 200; Melder_assert ((int) (signed char) dummy == -56); } // bingeti1 relies on this
{ int dummy = 200; Melder_assert ((int) (unsigned char) dummy == 200); }
{ uint16_t dummy = 40000; Melder_assert ((int) (int16_t) dummy == -25536); } // bingeti2 relies on this
{ uint16_t dummy = 40000; Melder_assert ((short) (int16_t) dummy == -25536); } // bingete2 relies on this
if (Melder_batch) {
if (thePraatStandAloneScriptText != NULL) {
try {
praat_executeScriptFromText (thePraatStandAloneScriptText);
praat_exit (0);
} catch (MelderError) {
Melder_flushError ("%s: stand-alone script session interrupted.", praatP.title);
praat_exit (-1);
}
} else if (doingCommandLineInterface) {
try {
praat_executeCommandFromStandardInput (praatP.title);
praat_exit (0);
} catch (MelderError) {
Melder_flushError ("%s: command line session interrupted.", praatP.title);
praat_exit (-1);
}
} else {
try {
praat_executeScriptFromFileNameWithArguments (theCurrentPraatApplication -> batchName.string);
praat_exit (0);
} catch (MelderError) {
/*
* Try to get the error message out; this is a bit complicated...
*/
structMelderFile batchFile = { 0 };
try {
Melder_relativePathToFile (theCurrentPraatApplication -> batchName.string, & batchFile);
} catch (MelderError) {
praat_exit (-1);
}
#if defined (_WIN32) && ! defined (CONSOLE_APPLICATION)
MelderGui_create (NULL);
#endif
Melder_error_ (praatP.title, ": command file ", & batchFile, " not completed.");
Melder_flushError (NULL);
praat_exit (-1);
}
}
} else /* GUI */ {
praatP.phase = praat_READING_BUTTONS;
/*
* Read the added script buttons. Each line separately: every error should be ignored.
*/
{ // scope
autostring buttons;
try {
buttons.reset (MelderFile_readText (& buttons5File));
} catch (MelderError) {
try {
buttons.reset (MelderFile_readText (& buttons4File));
} catch (MelderError) {
Melder_clearError ();
}
}
if (buttons.peek()) {
wchar *line = buttons.peek();
for (;;) {
wchar_t *newline = wcschr (line, '\n');
if (newline) *newline = '\0';
try {
praat_executeCommand (NULL, line);
} catch (MelderError) {
Melder_clearError (); // ignore this line, but not necessarily the next
}
if (newline == NULL) break;
line = newline + 1;
}
}
}
/*
* Sort the commands.
*/
praat_sortMenuCommands ();
praat_sortActions ();
praatP.phase = praat_HANDLING_EVENTS;
#if gtk
//gtk_widget_add_events (G_OBJECT (theCurrentPraatApplication -> topShell), GDK_ALL_EVENTS_MASK);
g_signal_connect (G_OBJECT (theCurrentPraatApplication -> topShell), "client-event", G_CALLBACK (cb_userMessage), NULL);
gtk_key_snooper_install (theKeySnooper, 0);
gtk_main ();
#else
#if defined (_WIN32)
if (theCurrentPraatApplication -> batchName.string [0] != '\0') {
wchar_t text [500];
/*
* The user dropped a file on the Praat icon, while Praat was not running yet.
* Windows may have enclosed the path between quotes;
* this is especially likely to happen if the path contains spaces (which is usual).
* And sometimes, Windows prepends a space before the quote.
* Peel all that off.
*/
wchar_t *s = theCurrentPraatApplication -> batchName.string;
swprintf (text, 500, L"Read from file... %ls", s [0] == ' ' && s [1] == '\"' ? s + 2 : s [0] == '\"' ? s + 1 : s);
long l = wcslen (text);
if (l > 0 && text [l - 1] == '\"') text [l - 1] = '\0';
//Melder_error3 (L"command <<", text, L">>");
//Melder_flushError (NULL);
try {
praat_executeScriptFromText (text);
} catch (MelderError) {
Melder_flushError (NULL);
}
}
#endif
for (;;) {
XEvent event;
GuiNextEvent (& event);
XtDispatchEvent (& event);
}
#endif
}
}
/* End of file praat.cpp */
|