summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvincentdarley <vincentdarley>2003-11-15 02:49:36 (GMT)
committervincentdarley <vincentdarley>2003-11-15 02:49:36 (GMT)
commit0c99365df4a6cd92a6a36881416e0270eff5c84a (patch)
tree8979b737880c0adfc66b464810266148ba1b1f07
parentcc4bbc337ac4cf772cfd6975afeae4cd77e2e99b (diff)
downloadtk-0c99365df4a6cd92a6a36881416e0270eff5c84a.zip
tk-0c99365df4a6cd92a6a36881416e0270eff5c84a.tar.gz
tk-0c99365df4a6cd92a6a36881416e0270eff5c84a.tar.bz2
cleanup
-rw-r--r--generic/tkTextDisp.c38
1 files changed, 20 insertions, 18 deletions
diff --git a/generic/tkTextDisp.c b/generic/tkTextDisp.c
index e1a3fee..7e6713b 100644
--- a/generic/tkTextDisp.c
+++ b/generic/tkTextDisp.c
@@ -13,7 +13,7 @@
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
- * RCS: @(#) $Id: tkTextDisp.c,v 1.29 2003/11/15 02:33:50 vincentdarley Exp $
+ * RCS: @(#) $Id: tkTextDisp.c,v 1.30 2003/11/15 02:49:36 vincentdarley Exp $
*/
#include "tkPort.h"
@@ -4698,7 +4698,7 @@ TkTextXviewCmd(textPtr, interp, objc, objv)
* objv[1] is "xview". */
{
TextDInfo *dInfoPtr = textPtr->dInfoPtr;
- int type, charsPerPage, count, newOffset;
+ int type, count;
double fraction;
if (dInfoPtr->flags & DINFO_OUT_OF_DATE) {
@@ -4710,7 +4710,6 @@ TkTextXviewCmd(textPtr, interp, objc, objv)
return TCL_OK;
}
- newOffset = dInfoPtr->newXPixelOffset;
type = TextGetScrollInfoObj(interp, textPtr, objc, objv,
&fraction, &count);
switch (type) {
@@ -4723,23 +4722,26 @@ TkTextXviewCmd(textPtr, interp, objc, objv)
if (fraction < 0) {
fraction = 0;
}
- newOffset = (int) (fraction * dInfoPtr->maxLength + 0.5);
+ dInfoPtr->newXPixelOffset = (int) (fraction
+ * dInfoPtr->maxLength + 0.5);
break;
- case TKTEXT_SCROLL_PAGES:
- charsPerPage = (dInfoPtr->maxX-dInfoPtr->x)/textPtr->charWidth - 2;
- if (charsPerPage < 1) {
- charsPerPage = 1;
+ case TKTEXT_SCROLL_PAGES: {
+ int pixelsPerPage;
+ pixelsPerPage = (dInfoPtr->maxX-dInfoPtr->x) - 2*textPtr->charWidth;
+ if (pixelsPerPage < 1) {
+ pixelsPerPage = 1;
}
- newOffset += charsPerPage * count * textPtr->charWidth;
+ dInfoPtr->newXPixelOffset += pixelsPerPage * count;
break;
+ }
case TKTEXT_SCROLL_UNITS:
- newOffset += count * textPtr->charWidth;
+ dInfoPtr->newXPixelOffset += count * textPtr->charWidth;
break;
case TKTEXT_SCROLL_PIXELS:
- newOffset += count;
+ dInfoPtr->newXPixelOffset += count;
break;
}
- dInfoPtr->newXPixelOffset = newOffset;
+
dInfoPtr->flags |= DINFO_OUT_OF_DATE;
if (!(dInfoPtr->flags & REDRAW_PENDING)) {
dInfoPtr->flags |= REDRAW_PENDING;
@@ -6900,12 +6902,12 @@ MeasureChars(tkfont, source, maxBytes, startX, maxX, tabOrigin, nextXPtr)
*
* TextGetScrollInfoObj --
*
- * This procedure is invoked to parse "yview" scrolling commands for
- * text widgets using the new scrolling command syntax ("moveto" or
- * "scroll" options). It extends the public Tk_GetScrollInfoObj
- * function with the addition of "pixels" as a valid unit alongside
- * "pages" and "units". It is a shame the core API isn't more
- * flexible in this regard.
+ * This procedure is invoked to parse "xview" and "yview" scrolling
+ * commands for text widgets using the new scrolling command syntax
+ * ("moveto" or "scroll" options). It extends the public
+ * Tk_GetScrollInfoObj function with the addition of "pixels" as a
+ * valid unit alongside "pages" and "units". It is a shame the core
+ * API isn't more flexible in this regard.
*
* Results:
* The return value is either TKTEXT_SCROLL_MOVETO,
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 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
/*
 * tkMacOSXDialog.c --
 *
 *        Contains the Mac implementation of the common dialog boxes.
 *
 * Copyright (c) 1996-1997 Sun Microsystems, Inc.
 * Copyright 2001, Apple Computer, Inc.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id: tkMacOSXDialog.c,v 1.4.2.11 2006/04/11 12:05:18 das Exp $
 */

#include "tkMacOSXInt.h"
#include "tkFileFilter.h"

#ifndef StrLength
#define StrLength(s)	(*((unsigned char *) (s)))
#endif
#ifndef StrBody
#define StrBody(s)	((char *) (s) + 1)
#endif

/*
 * The following are ID's for resources that are defined in tkMacOSXResource.r
 */
#define OPEN_BOX        130
#define OPEN_POPUP      131
#define OPEN_MENU       132
#define OPEN_POPUP_ITEM 10

#define SAVE_FILE        0
#define OPEN_FILE        1
#define CHOOSE_FOLDER    2

#define MATCHED          0
#define UNMATCHED        1

#define TK_DEFAULT_ABOUT 128

/*
 * The following structure is used in the GetFileName() function. It stored
 * information about the file dialog and the file filters.
 */
typedef struct _OpenFileData {
    FileFilterList fl;		/* List of file filters. */
    SInt16 curType;		/* The filetype currently being listed. */
    short popupItem;		/* Item number of the popup in the dialog. */
    int usePopup;		/* True if we show the popup menu (this is
				 * an open operation and the -filetypes
				 * option is set). */
} OpenFileData;


/*
 * The following structure is used in the tk_messageBox
 * implementation.
 */
typedef struct {
    WindowRef	windowRef;
    int		buttonIndex;
} CallbackUserData;


static OSStatus		AlertHandler(EventHandlerCallRef callRef,
			    EventRef eventRef, void *userData);
static Boolean                MatchOneType(StringPtr fileNamePtr, OSType fileType,
			    OpenFileData *myofdPtr, FileFilter *filterPtr);
static pascal Boolean   OpenFileFilterProc(AEDesc* theItem, void* info,
			    NavCallBackUserData callBackUD,
			    NavFilterModes filterMode);
static pascal void      OpenEventProc(NavEventCallbackMessage callBackSelector,
			    NavCBRecPtr callBackParms,
			    NavCallBackUserData callBackUD);
static void             InitFileDialogs();
static int              NavServicesGetFile(Tcl_Interp *interp, OpenFileData *ofd,
			    AEDesc *initialDescPtr,
			    char *initialFile, AEDescList *selectDescPtr,
			    CFStringRef title, CFStringRef message, int multiple, int isOpen);
static int              HandleInitialDirectory (Tcl_Interp *interp,
						char *initialFile, char *initialDir,
						FSRef *dirRef,
						AEDescList *selectDescPtr,
						AEDesc *dirDescPtr);

/*
 * Have we initialized the file dialog subsystem
 */

static int fileDlgInited = 0;

/*
 * Filter and hook functions used by the tk_getOpenFile and tk_getSaveFile
 * commands.
 */

static NavObjectFilterUPP openFileFilterUPP;
static NavEventUPP openFileEventUPP;


/*
 *----------------------------------------------------------------------
 *
 * Tk_ChooseColorObjCmd --
 *
 *        This procedure implements the color dialog box for the Mac
 *        platform. See the user documentation for details on what it
 *        does.
 *
 * Results:
 *      A standard Tcl result.
 *
 * Side effects:
 *      See the user documentation.
 *
 *----------------------------------------------------------------------
 */

int
Tk_ChooseColorObjCmd(
    ClientData clientData,        /* Main window associated with interpreter. */
    Tcl_Interp *interp,                /* Current interpreter. */
    int objc,                        /* Number of arguments. */
    Tcl_Obj *CONST objv[])        /* Argument objects. */
{
    Tk_Window parent;
    char *title;
    int i, picked, srcRead, dstWrote;
    ColorPickerInfo cpinfo;
    static int inited = 0;
    static RGBColor in;
    static CONST char *optionStrings[] = {
	"-initialcolor",    "-parent",            "-title",            NULL
    };
    enum options {
	COLOR_INITIAL,            COLOR_PARENT,   COLOR_TITLE
    };

    if (inited == 0) {
	    /*
	     * 'in' stores the last color picked.  The next time the color dialog
	     * pops up, the last color will remain in the dialog.
	     */

	in.red = 0xffff;
	in.green = 0xffff;
	in.blue = 0xffff;
	inited = 1;
    }

    parent = (Tk_Window) clientData;
    title = "Choose a color:";
    picked = 0;

    for (i = 1; i < objc; i += 2) {
	    int index;
	    char *option, *value;

	if (Tcl_GetIndexFromObj(interp, objv[i], optionStrings, "option",
		TCL_EXACT, &index) != TCL_OK) {
	    return TCL_ERROR;
	}
	if (i + 1 == objc) {
	    option = Tcl_GetStringFromObj(objv[i], NULL);
	    Tcl_AppendResult(interp, "value for \"", option, "\" missing",
		    (char *) NULL);
	    return TCL_ERROR;
	}
	value = Tcl_GetStringFromObj(objv[i + 1], NULL);

	switch ((enum options) index) {
	    case COLOR_INITIAL: {
		XColor *colorPtr;

		colorPtr = Tk_GetColor(interp, parent, value);
		if (colorPtr == NULL) {
		    return TCL_ERROR;
		}
		in.red   = colorPtr->red;
		in.green = colorPtr->green;
		in.blue  = colorPtr->blue;
		Tk_FreeColor(colorPtr);
		break;
	    }
	    case COLOR_PARENT: {
		parent = Tk_NameToWindow(interp, value, parent);
		if (parent == NULL) {
		    return TCL_ERROR;
		}
		break;
	    }
	    case COLOR_TITLE: {
		title = value;
		break;
	    }
	}
    }


    cpinfo.theColor.profile = 0L;
    cpinfo.theColor.color.rgb.red   = in.red;
    cpinfo.theColor.color.rgb.green = in.green;
    cpinfo.theColor.color.rgb.blue  = in.blue;
    cpinfo.dstProfile = 0L;
    cpinfo.flags = kColorPickerCanModifyPalette
	    |  kColorPickerCanAnimatePalette;
    cpinfo.placeWhere = kDeepestColorScreen;
    cpinfo.pickerType = 0L;
    cpinfo.eventProc = NULL;
    cpinfo.colorProc = NULL;
    cpinfo.colorProcData = 0;

    /* This doesn't seem to actually set the title! */
    Tcl_UtfToExternal(NULL, NULL, title, -1, 0, NULL,
	StrBody(cpinfo.prompt), 255, &srcRead, &dstWrote, NULL);
    StrLength(cpinfo.prompt) = (unsigned char) dstWrote;

    if ((PickColor(&cpinfo) == noErr) && (cpinfo.newColorChosen != 0)) {
	in.red    = cpinfo.theColor.color.rgb.red;
	in.green  = cpinfo.theColor.color.rgb.green;
	in.blue   = cpinfo.theColor.color.rgb.blue;
	picked = 1;
    }

    if (picked != 0) {
	char result[32];

	sprintf(result, "#%02x%02x%02x", in.red >> 8, in.green >> 8,
		in.blue >> 8);
	Tcl_AppendResult(interp, result, NULL);
    }

    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * Tk_GetOpenFileObjCmd --
 *
 *        This procedure implements the "open file" dialog box for the
 *        Mac platform. See the user documentation for details on what
 *        it does.
 *
 * Results:
 *      A standard Tcl result.
 *
 * Side effects:
 *        See user documentation.
 *----------------------------------------------------------------------
 */

int
Tk_GetOpenFileObjCmd(
    ClientData clientData,        /* Main window associated with interpreter. */
    Tcl_Interp *interp,                /* Current interpreter. */
    int objc,                        /* Number of arguments. */
    Tcl_Obj *CONST objv[])        /* Argument objects. */
{
    int i, result, multiple;
    OpenFileData ofd;
    Tk_Window parent;
    CFStringRef message, title;
    AEDesc initialDesc = {typeNull, NULL};
    FSRef dirRef;
    AEDesc *initialPtr = NULL;
    AEDescList selectDesc = {typeNull, NULL};
    char *initialFile = NULL, *initialDir = NULL;
    static CONST char *openOptionStrings[] = {
	    "-defaultextension", "-filetypes",
	    "-initialdir", "-initialfile",
	    "-message", "-multiple",
	    "-parent", "-title", NULL
    };
    enum openOptions {
	    OPEN_DEFAULT, OPEN_FILETYPES,
	    OPEN_INITDIR, OPEN_INITFILE,
	    OPEN_MESSAGE, OPEN_MULTIPLE,
	    OPEN_PARENT, OPEN_TITLE
    };

    if (!fileDlgInited) {
	InitFileDialogs();
    }

    result = TCL_ERROR;
    parent = (Tk_Window) clientData;
    multiple = false;
    title = NULL;
    message = NULL;

    TkInitFileFilters(&ofd.fl);

    ofd.curType                = 0;
    ofd.popupItem        = OPEN_POPUP_ITEM;
    ofd.usePopup         = 1;

    for (i = 1; i < objc; i += 2) {
	char *choice;
	int index, choiceLen;
	char *string;

	if (Tcl_GetIndexFromObj(interp, objv[i], openOptionStrings, "option",
		TCL_EXACT, &index) != TCL_OK) {
	    result = TCL_ERROR;
	    goto end;
	}
	if (i + 1 == objc) {
	    string = Tcl_GetStringFromObj(objv[i], NULL);
	    Tcl_AppendResult(interp, "value for \"", string, "\" missing",
		    (char *) NULL);
	    result = TCL_ERROR;
	    goto end;
	}

	switch (index) {
	    case OPEN_DEFAULT:
		break;
	    case OPEN_FILETYPES:
		choice = Tcl_GetStringFromObj(objv[i + 1], NULL);
		if (TkGetFileFilters(interp, &ofd.fl, choice, 0) != TCL_OK) {
		    result = TCL_ERROR;
		    goto end;
		}
		break;
	    case OPEN_INITDIR:
		initialDir = Tcl_GetStringFromObj(objv[i + 1], &choiceLen);
		/* empty strings should be like no selection given */
		if (choiceLen == 0) { initialDir = NULL; }
		break;
	    case OPEN_INITFILE:
		initialFile = Tcl_GetStringFromObj(objv[i + 1], NULL);
		/* empty strings should be like no selection given */
		if (choiceLen == 0) { initialFile = NULL; }
		break;
	    case OPEN_MESSAGE:
		choice = Tcl_GetStringFromObj(objv[i + 1], &choiceLen);
		message = CFStringCreateWithBytes(NULL, (unsigned char*) choice, choiceLen,
			kCFStringEncodingUTF8, false);
		break;
	    case OPEN_MULTIPLE:
		if (Tcl_GetBooleanFromObj(interp, objv[i + 1], &multiple)
			!= TCL_OK) {
		    result = TCL_ERROR;
		    goto end;
		}
		break;
	    case OPEN_PARENT:
		choice = Tcl_GetStringFromObj(objv[i + 1], &choiceLen);
		parent = Tk_NameToWindow(interp, choice, parent);
		if (parent == NULL) {
		    result = TCL_ERROR;
		    goto end;
		}
		break;
	    case OPEN_TITLE:
		choice = Tcl_GetStringFromObj(objv[i + 1], &choiceLen);
		title = CFStringCreateWithBytes(NULL, (unsigned char*) choice, choiceLen,
			kCFStringEncodingUTF8, false);
		break;
	}
    }

    if (HandleInitialDirectory(interp, initialFile, initialDir, &dirRef,
			       &selectDesc, &initialDesc) != TCL_OK) {
	result = TCL_ERROR;
	goto end;
    }

    if (initialDesc.descriptorType == typeFSRef) {
	initialPtr = &initialDesc;
    }
    result = NavServicesGetFile(interp, &ofd, initialPtr,
	    NULL, &selectDesc, title, message, multiple, OPEN_FILE);

    end:
    TkFreeFileFilters(&ofd.fl);
    AEDisposeDesc(&initialDesc);
    AEDisposeDesc(&selectDesc);
    if (title != NULL) {
	CFRelease(title);
    }
    if (message != NULL) {
	CFRelease(message);
    }

    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * Tk_GetSaveFileObjCmd --
 *
 *        Same as Tk_GetOpenFileCmd but opens a "save file" dialog box
 *        instead
 *
 * Results:
 *      A standard Tcl result.
 *
 * Side effects:
 *        See user documentation.
 *----------------------------------------------------------------------
 */

int
Tk_GetSaveFileObjCmd(
    ClientData clientData,     /* Main window associated with interpreter. */
    Tcl_Interp *interp,        /* Current interpreter. */
    int objc,                  /* Number of arguments. */
    Tcl_Obj *CONST objv[])     /* Argument objects. */
{
    int i, result;
    char *initialFile = NULL;
    Tk_Window parent;
    AEDesc initialDesc = {typeNull, NULL};
    AEDesc *initialPtr = NULL;
    FSRef dirRef;
    CFStringRef title, message;
    OpenFileData ofd;
    static CONST char *saveOptionStrings[] = {
	    "-defaultextension", "-filetypes", "-initialdir", "-initialfile",
	    "-message", "-parent",        "-title",         NULL
    };
    enum saveOptions {
	    SAVE_DEFAULT, SAVE_FILETYPES, SAVE_INITDIR, SAVE_INITFILE,
	    SAVE_MESSAGE, SAVE_PARENT, SAVE_TITLE
    };

    if (!fileDlgInited) {
	InitFileDialogs();
    }

    result = TCL_ERROR;
    parent = (Tk_Window) clientData;
    title = NULL;
    message = NULL;

    for (i = 1; i < objc; i += 2) {
	char *choice;
	int index, choiceLen;
	char *string;

	if (Tcl_GetIndexFromObj(interp, objv[i], saveOptionStrings, "option",
		TCL_EXACT, &index) != TCL_OK) {
	    return TCL_ERROR;
	}
	if (i + 1 == objc) {
	    string = Tcl_GetStringFromObj(objv[i], NULL);
	    Tcl_AppendResult(interp, "value for \"", string, "\" missing",
		    (char *) NULL);
	    return TCL_ERROR;
	}
	switch (index) {
	    case SAVE_DEFAULT:
		break;
	    case SAVE_FILETYPES:
		/* Currently unimplemented - what would we do here anyway? */
		break;
	    case SAVE_INITDIR:
		choice = Tcl_GetStringFromObj(objv[i + 1], &choiceLen);
		/* empty strings should be like no selection given */
		if (choiceLen &&
			HandleInitialDirectory(interp, NULL, choice, &dirRef,
				NULL, &initialDesc) != TCL_OK) {
		    result = TCL_ERROR;
		    goto end;
		}
		break;
	    case SAVE_INITFILE:
		initialFile = Tcl_GetStringFromObj(objv[i + 1], &choiceLen);
		/* empty strings should be like no selection given */
		if (choiceLen == 0) { initialFile = NULL; }
		break;
	    case SAVE_MESSAGE:
		choice = Tcl_GetStringFromObj(objv[i + 1], &choiceLen);
		message = CFStringCreateWithBytes(NULL, (unsigned char*) choice, choiceLen,
			kCFStringEncodingUTF8, false);
		break;
	    case SAVE_PARENT:
		choice = Tcl_GetStringFromObj(objv[i + 1], &choiceLen);
		parent = Tk_NameToWindow(interp, choice, parent);
		if (parent == NULL) {
		    result = TCL_ERROR;
		    goto end;
		}
		break;
	    case SAVE_TITLE:
		choice = Tcl_GetStringFromObj(objv[i + 1], &choiceLen);
		title = CFStringCreateWithBytes(NULL, (unsigned char*) choice, choiceLen,
			kCFStringEncodingUTF8, false);
		break;
	}
    }

    TkInitFileFilters(&ofd.fl);
    ofd.usePopup = 0;

    if (initialDesc.descriptorType == typeFSRef) {
	initialPtr = &initialDesc;
    }
    result = NavServicesGetFile(interp, &ofd, initialPtr, initialFile, NULL,
	title, message, false, SAVE_FILE);

    end:

    AEDisposeDesc(&initialDesc);
    if (title != NULL) {
	CFRelease(title);
    }
    if (message != NULL) {
	CFRelease(message);
    }

    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * Tk_ChooseDirectoryObjCmd --
 *
 *        This procedure implements the "tk_chooseDirectory" dialog box
 *        for the Windows platform. See the user documentation for details
 *        on what it does.
 *
 * Results:
 *        See user documentation.
 *
 * Side effects:
 *        A modal dialog window is created.  Tcl_SetServiceMode() is
 *        called to allow background events to be processed
 *
 *----------------------------------------------------------------------
 */

int
Tk_ChooseDirectoryObjCmd(clientData, interp, objc, objv)
    ClientData clientData;        /* Main window associated with interpreter. */
    Tcl_Interp *interp;                /* Current interpreter. */
    int objc;                        /* Number of arguments. */
    Tcl_Obj *CONST objv[];        /* Argument objects. */
{
    int i, result;
    Tk_Window parent;
    AEDesc initialDesc = {typeNull, NULL};
    AEDesc *initialPtr = NULL;
    FSRef dirRef;
    CFStringRef message, title;
    OpenFileData ofd;
    static CONST char *chooseOptionStrings[] = {
	"-initialdir", "-message", "-mustexist", "-parent", "-title", NULL
    };
    enum chooseOptions {
	CHOOSE_INITDIR, CHOOSE_MESSAGE, CHOOSE_MUSTEXIST,
	CHOOSE_PARENT, CHOOSE_TITLE
    };

    if (!NavServicesAvailable()) {
	return TCL_ERROR;
    }

    if (!fileDlgInited) {
	InitFileDialogs();
    }
    result = TCL_ERROR;
    parent = (Tk_Window) clientData;
    title = NULL;
    message = NULL;

    for (i = 1; i < objc; i += 2) {
	char *choice;
	int index, choiceLen;
	char *string;

	if (Tcl_GetIndexFromObj(interp, objv[i], chooseOptionStrings, "option",
		TCL_EXACT, &index) != TCL_OK) {
	    return TCL_ERROR;
	}
	if (i + 1 == objc) {
	    string = Tcl_GetStringFromObj(objv[i], NULL);
	    Tcl_AppendResult(interp, "value for \"", string, "\" missing",
		    (char *) NULL);
	    return TCL_ERROR;
	}
	switch (index) {
	    case CHOOSE_INITDIR:
		choice = Tcl_GetStringFromObj(objv[i + 1], &choiceLen);
		if (choiceLen &&
			HandleInitialDirectory(interp, NULL, choice, &dirRef,
				NULL, &initialDesc) != TCL_OK) {
		    result = TCL_ERROR;
		    goto end;
		}
		break;
	    case CHOOSE_MESSAGE:
		choice = Tcl_GetStringFromObj(objv[i + 1], &choiceLen);
		message = CFStringCreateWithBytes(NULL, (unsigned char*) choice, choiceLen,
			kCFStringEncodingUTF8, false);
		break;
	    case CHOOSE_PARENT:
		choice = Tcl_GetStringFromObj(objv[i + 1], &choiceLen);
		parent = Tk_NameToWindow(interp, choice, parent);
		if (parent == NULL) {
		    result = TCL_ERROR;
		    goto end;
		}
		break;
	    case CHOOSE_TITLE:
		choice = Tcl_GetStringFromObj(objv[i + 1], &choiceLen);
		title = CFStringCreateWithBytes(NULL, (unsigned char*) choice, choiceLen,
			kCFStringEncodingUTF8, false);
		break;
	}
    }

    TkInitFileFilters(&ofd.fl);
    ofd.usePopup = 0;

    if (initialDesc.descriptorType == typeFSRef) {
	initialPtr = &initialDesc;
    }
    result = NavServicesGetFile(interp, &ofd, initialPtr, NULL, NULL,
	title, message, false, CHOOSE_FOLDER);

    end:
    AEDisposeDesc(&initialDesc);
    if (title != NULL) {
	CFRelease(title);
    }
    if (message != NULL) {
	CFRelease(message);
    }

    return result;
}

int
HandleInitialDirectory (
    Tcl_Interp *interp,
    char *initialFile,
    char *initialDir,
    FSRef *dirRef,
    AEDescList *selectDescPtr,
    AEDesc *dirDescPtr)
{
    Tcl_DString ds;
    OSErr err;
    Boolean isDirectory;
    char *dirName = NULL;
    int result = TCL_OK;

    if (initialDir != NULL) {
	dirName = Tcl_TranslateFileName(interp, initialDir, &ds);
	if (dirName == NULL) {
	    return TCL_ERROR;
	}

	err = FSPathMakeRef((unsigned char*) dirName,
		dirRef, &isDirectory);

	if (err != noErr) {
	    Tcl_AppendResult(interp, "bad directory \"",
			     initialDir, "\"", NULL);
	    result = TCL_ERROR;
	    goto end;
	}
	if (!isDirectory) {
	    Tcl_AppendResult(interp, "-intialdir \"",
		    initialDir, " is a file, not a directory.\"", NULL);
	    result = TCL_ERROR;
	    goto end;
	}

	AECreateDesc(typeFSRef, dirRef, sizeof(*dirRef), dirDescPtr);
    }

    if (initialFile != NULL && selectDescPtr != NULL) {
	FSRef fileRef;
	AEDesc fileDesc;
	char *namePtr;

	if (initialDir != NULL) {
	    Tcl_DStringAppend(&ds, "/", 1);
	    Tcl_DStringAppend(&ds, initialFile, -1);
	    namePtr = Tcl_DStringValue(&ds);
	} else {
	    namePtr = initialFile;
	}

	AECreateList(NULL, 0, false, selectDescPtr);

	err = FSPathMakeRef((unsigned char*) namePtr, &fileRef, &isDirectory);
	if (err != noErr) {
	    Tcl_AppendResult(interp, "bad initialfile \"", initialFile,
		    "\" file does not exist.", NULL);
	    return TCL_ERROR;
	}
	AECreateDesc(typeFSRef, &fileRef, sizeof(fileRef), &fileDesc);
	AEPutDesc(selectDescPtr, 1, &fileDesc);
	AEDisposeDesc(&fileDesc);
    }

end:
    if (dirName != NULL) {
	Tcl_DStringFree(&ds);
    }
    return result;
}

static void
InitFileDialogs()
{
    fileDlgInited = 1;
    openFileFilterUPP = NewNavObjectFilterUPP(OpenFileFilterProc);
    openFileEventUPP = NewNavEventUPP(OpenEventProc);
}

static int
NavServicesGetFile(
    Tcl_Interp *interp,
    OpenFileData *ofdPtr,
    AEDesc *initialDescPtr,
    char *initialFile,
    AEDescList *selectDescPtr,
    CFStringRef title,
    CFStringRef message,
    int multiple,
    int isOpen)
{
    NavReplyRecord theReply;
    NavDialogCreationOptions diagOptions;
    NavDialogRef dialogRef = NULL;
    CFStringRef * menuItemNames = NULL;
    OSErr err;
    Tcl_Obj *theResult = NULL;
    int result;

    err = NavGetDefaultDialogCreationOptions(&diagOptions);
    if (err!=noErr) {
	return TCL_ERROR;
    }
    diagOptions.location.h = -1;
    diagOptions.location.v = -1;
    diagOptions.optionFlags = kNavDontAutoTranslate
	+ kNavDontAddTranslateItems;
    if (multiple) {
	diagOptions.optionFlags += kNavAllowMultipleFiles;
    }
    diagOptions.modality = kWindowModalityAppModal;

    if (ofdPtr != NULL && ofdPtr->usePopup) {
	FileFilter *filterPtr;

	filterPtr = ofdPtr->fl.filters;
	if (filterPtr == NULL) {
	    ofdPtr->usePopup = 0;
	}
    }

    if (ofdPtr != NULL && ofdPtr->usePopup) {
	FileFilter *filterPtr;
	int index = 0;
	ofdPtr->curType = 0;

	menuItemNames = (CFStringRef *)ckalloc(ofdPtr->fl.numFilters
	    * sizeof(CFStringRef));

	for (filterPtr = ofdPtr->fl.filters; filterPtr != NULL;
		filterPtr = filterPtr->next, index++) {
	    menuItemNames[index] = CFStringCreateWithCString(NULL,
		    filterPtr->name, kCFStringEncodingUTF8);
	}
	diagOptions.popupExtension = CFArrayCreate(NULL,
		(const void **) menuItemNames, ofdPtr->fl.numFilters, NULL);
    } else {
	diagOptions.optionFlags += kNavNoTypePopup;
	diagOptions.popupExtension = NULL;
    }

    /*
     * This is required to allow App packages to be selectable in the
     * file dialogs...
     */

    diagOptions.optionFlags += kNavSupportPackages;

    diagOptions.clientName = CFStringCreateWithCString(NULL, "Wish", kCFStringEncodingUTF8);
    diagOptions.message = message;
    diagOptions.windowTitle = title;
    if (initialFile) {
	diagOptions.saveFileName = CFStringCreateWithCString(NULL,
		initialFile, kCFStringEncodingUTF8);
    } else {
	diagOptions.saveFileName = NULL;
    }

    diagOptions.actionButtonLabel = NULL;
    diagOptions.cancelButtonLabel = NULL;
    diagOptions.preferenceKey = 0;

    /*
     * Now process the selection list.  We have to use the popupExtension
     * to fill the menu.
     */

    if (isOpen == OPEN_FILE) {
	err = NavCreateGetFileDialog(&diagOptions,
	    NULL,
	    openFileEventUPP,
	    NULL,
	    openFileFilterUPP,
	    ofdPtr,
	    &dialogRef);
	if (err != noErr) {
#ifdef TK_MAC_DEBUG
	    fprintf(stderr,"NavCreateGetFileDialog failed, %d\n", err);
#endif
	    dialogRef = NULL;
	}
    } else if (isOpen == SAVE_FILE) {
	err = NavCreatePutFileDialog(&diagOptions, 'TEXT', 'WIsH',
		openFileEventUPP, NULL, &dialogRef);
	if (err!=noErr){
#ifdef TK_MAC_DEBUG
	    fprintf(stderr,"NavCreatePutFileDialog failed, %d\n", err);
#endif
	    dialogRef = NULL;
	}
    } else if (isOpen == CHOOSE_FOLDER) {
	err = NavCreateChooseFolderDialog(&diagOptions, openFileEventUPP,
		openFileFilterUPP, NULL, &dialogRef);
	if (err!=noErr){
#ifdef TK_MAC_DEBUG
	    fprintf(stderr,"NavCreateChooseFolderDialog failed, %d\n", err);
#endif
	    dialogRef = NULL;
	}
    }

    if (dialogRef) {
	if (initialDescPtr != NULL) {
	    NavCustomControl (dialogRef, kNavCtlSetLocation, initialDescPtr);
	}
	if ((selectDescPtr != NULL)
		&& (selectDescPtr->descriptorType != typeNull)) {
	    NavCustomControl(dialogRef, kNavCtlSetSelection, selectDescPtr);
	}

	if ((err = NavDialogRun(dialogRef)) != noErr){
#ifdef TK_MAC_DEBUG
	    fprintf(stderr,"NavDialogRun failed, %d\n", err);
#endif
	} else {
	    if ((err = NavDialogGetReply(dialogRef, &theReply)) != noErr) {
#ifdef TK_MAC_DEBUG
		fprintf(stderr,"NavGetReply failed, %d\n", err);
#endif
	    }
	}
    }

    /*
     * Most commands assume that the file dialogs return a single
     * item, not a list.  So only build a list if multiple is true...
     */
    if (err == noErr) {
	if (multiple) {
	    theResult = Tcl_NewListObj(0, NULL);
	} else {
	    theResult = Tcl_NewObj();
	}
	if (!theResult) {
	    err = memFullErr;
	}
    }
    if (theReply.validRecord && err == noErr) {
	AEDesc resultDesc;
	long count;
	FSRef  fsRef;
	char   pathPtr[1024];
	int    pathValid = 0;
	err = AECountItems(&theReply.selection, &count);
	if (err == noErr) {
	    long i;
	    for (i = 1; i <= count; i++) {
		err = AEGetNthDesc(&theReply.selection,
		    i, typeFSRef, NULL, &resultDesc);
		pathValid = 0;
		if (err == noErr) {
		    if ((err = AEGetDescData(&resultDesc, &fsRef, sizeof(fsRef)))
			    != noErr) {
#ifdef TK_MAC_DEBUG
			fprintf(stderr,"AEGetDescData failed %d\n", err);
#endif
		    } else {
			if ((err = FSRefMakePath(&fsRef, (unsigned char*) pathPtr, 1024))) {
#ifdef TK_MAC_DEBUG
			    fprintf(stderr,"FSRefMakePath failed, %d\n", err);
#endif
			} else {
			    if (isOpen == SAVE_FILE) {
				CFStringRef saveNameRef;
				char saveName [1024];
				if ((saveNameRef = NavDialogGetSaveFileName(dialogRef))) {
				    if (CFStringGetCString(saveNameRef, saveName,
					    1024, kCFStringEncodingUTF8)) {
					if (strlen(pathPtr) + strlen(saveName) < 1023) {
					    strcat(pathPtr, "/");
					    strcat(pathPtr, saveName);
					    pathValid = 1;
					} else {
#ifdef TK_MAC_DEBUG
					    fprintf(stderr, "Path name too long\n");
#endif
					}
				    } else {
#ifdef TK_MAC_DEBUG
					fprintf(stderr, "CFStringGetCString failed\n");
#endif
				    }
				} else {
#ifdef TK_MAC_DEBUG
				    fprintf(stderr, "NavDialogGetSaveFileName failed\n");
#endif
				}
			    } else {
				pathValid = 1;
			    }
			    if (pathValid) {
				if (multiple) {
				    Tcl_ListObjAppendElement(interp, theResult,
					Tcl_NewStringObj(pathPtr, -1));
				} else {
				    Tcl_SetStringObj(theResult, pathPtr, -1);
				}
			    }
			}
		    }
		    AEDisposeDesc(&resultDesc);
		}
	    }
	 }
	 err = NavDisposeReply(&theReply);
	 Tcl_SetObjResult(interp, theResult);
	 result = TCL_OK;
    } else if (err == userCanceledErr) {
	result = TCL_OK;
    } else {
	result = TCL_ERROR;
    }

    /*
     * Clean up any allocated strings
     * dispose of things in reverse order of creation
     */

    if (diagOptions.windowTitle) {
	CFRelease(diagOptions.windowTitle);
    }
    if (diagOptions.saveFileName) {
	CFRelease(diagOptions.saveFileName);
    }
    if (diagOptions.message) {
	CFRelease(diagOptions.message);
    }
    if (diagOptions.clientName) {
	CFRelease(diagOptions.clientName);
    }
    /*
     * dispose of the CFArray diagOptions.popupExtension
     */

    if (menuItemNames) {
	int i;
	for (i = 0;i < ofdPtr->fl.numFilters; i++) {
	    CFRelease(menuItemNames[i]);
	}
	ckfree((void *)menuItemNames);
    }
    if (diagOptions.popupExtension != NULL) {
	CFRelease(diagOptions.popupExtension);
    }

    return result;
}

static pascal Boolean
OpenFileFilterProc(
    AEDesc* theItem, void* info,
    NavCallBackUserData callBackUD,
    NavFilterModes filterMode)
{
    OpenFileData *ofdPtr = (OpenFileData *) callBackUD;

    if (!ofdPtr || !ofdPtr->usePopup) {
	return true;
    } else {
	if (ofdPtr->fl.numFilters == 0) {
	    return true;
	} else {
	    if ((theItem->descriptorType == typeFSS)
		    || (theItem->descriptorType = typeFSRef)) {
		NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo *) info;
		char fileName[256];
		int result;

		if (!theInfo->isFolder) {
		    OSType fileType;
		    StringPtr fileNamePtr;
		    Tcl_DString fileNameDString;
		    int i;
		    FileFilter *filterPtr;

		    fileType =
			    theInfo->fileAndFolder.fileInfo.finderInfo.fdType;
		    Tcl_DStringInit (&fileNameDString);

		    if (theItem->descriptorType == typeFSS) {
			int len;
			fileNamePtr = (((FSSpec *) *theItem->dataHandle)->name);
			len = fileNamePtr[0];
			strncpy(fileName, (char*) fileNamePtr + 1, len);
			fileName[len] = '\0';
			fileNamePtr = (unsigned char*) fileName;

		    } else if ((theItem->descriptorType = typeFSRef)) {
			OSStatus err;
			FSRef *theRef = (FSRef *) *theItem->dataHandle;
			HFSUniStr255 uniFileName;
			err = FSGetCatalogInfo (theRef, kFSCatInfoNone, NULL,
				&uniFileName, NULL, NULL);

			if (err == noErr) {
			    Tcl_UniCharToUtfDString (
				    (Tcl_UniChar *) uniFileName.unicode,
				    uniFileName.length,
				    &fileNameDString);
			    fileNamePtr = (unsigned char*) Tcl_DStringValue(&fileNameDString);
			} else {
			    fileNamePtr = NULL;
			}
		    }
		    if (ofdPtr->usePopup) {
			i = ofdPtr->curType;
			for (filterPtr = ofdPtr->fl.filters;
				filterPtr && i > 0; i--) {
			    filterPtr = filterPtr->next;
			}
			if (filterPtr) {
			    result = MatchOneType(fileNamePtr, fileType,
				    ofdPtr, filterPtr);
			} else {
			    result = false;
			}
		    } else {
			/*
			 * We are not using the popup menu. In this case, the
			 * file is considered matched if it matches any of
			 * the file filters.
			 */

			result = UNMATCHED;
			for (filterPtr = ofdPtr->fl.filters; filterPtr;
				filterPtr = filterPtr->next) {
			    if (MatchOneType(fileNamePtr, fileType,
				    ofdPtr, filterPtr) == MATCHED) {
				result = MATCHED;
				break;
			    }
			}
		    }

		    Tcl_DStringFree (&fileNameDString);
		    return (result == MATCHED);
		} else {
		    return true;
		}
	    }
	}

	return true;
    }
}

pascal void
OpenEventProc(
    NavEventCallbackMessage callBackSelector,
    NavCBRecPtr callBackParams,
    NavCallBackUserData callBackUD)
{
    NavMenuItemSpec *chosenItem;
    OpenFileData *ofd = (OpenFileData *) callBackUD;
    static SInt32 otherEvent = ~(kNavCBCustomize|kNavCBStart|kNavCBTerminate
	    |kNavCBNewLocation|kNavCBShowDesktop|kNavCBSelectEntry|kNavCBAccept
	    |kNavCBCancel|kNavCBAdjustPreview);

    if (callBackSelector ==  kNavCBPopupMenuSelect) {
	chosenItem = (NavMenuItemSpec *) callBackParams->eventData.eventDataParms.param;
	ofd->curType = chosenItem->menuType;
    } else if (callBackSelector == kNavCBAdjustRect
	    || (callBackSelector & otherEvent) != 0) {
	while (Tcl_DoOneEvent(TCL_IDLE_EVENTS
		| TCL_DONT_WAIT
		| TCL_WINDOW_EVENTS)) {
	    /* Empty Body */
	}
    }
}

/*
 *----------------------------------------------------------------------
 *
 * MatchOneType --
 *
 *        Match a file with one file type in the list of file types.
 *
 * Results:
 *        Returns MATCHED if the file matches with the file type; returns
 *        UNMATCHED otherwise.
 *
 * Side effects:
 *        None
 *
 *----------------------------------------------------------------------
 */

static Boolean
MatchOneType(
    StringPtr fileNamePtr,        /* Name of the file */
    OSType    fileType,         /* Type of the file, 0 means there was no specified type.  */
    OpenFileData * ofdPtr,        /* Information about this file dialog */
    FileFilter * filterPtr)        /* Match the file described by pb against
				 * this filter */
{
    FileFilterClause * clausePtr;

    /*
     * A file matches with a file type if it matches with at least one
     * clause of the type.
     *
     * If the clause has both glob patterns and ostypes, the file must
     * match with at least one pattern AND at least one ostype.
     *
     * If the clause has glob patterns only, the file must match with at least
     * one pattern.
     *
     * If the clause has mac types only, the file must match with at least
     * one mac type.
     *
     * If the clause has neither glob patterns nor mac types, it's
     * considered an error.
     */

    for (clausePtr = filterPtr->clauses; clausePtr;
	    clausePtr = clausePtr->next) {
	int macMatched  = 0;
	int globMatched = 0;
	GlobPattern * globPtr;
	MacFileType * mfPtr;

	if (clausePtr->patterns == NULL) {
	    globMatched = 1;
	}
	if (clausePtr->macTypes == NULL) {
	    macMatched = 1;
	}

	for (globPtr = clausePtr->patterns; globPtr;
		globPtr = globPtr->next) {
	    char *q, *ext;

	    if (fileNamePtr == NULL) {
		continue;
	    }
	    ext = globPtr->pattern;

	    if (ext[0] == '\0') {
		/*
		 * We don't want any extensions: OK if the filename doesn't
		 * have "." in it
		 */

		for (q = (char*) fileNamePtr; *q; q++) {
		    if (*q == '.') {
			goto glob_unmatched;
		    }
		}
		goto glob_matched;
	    }

	    if (Tcl_StringMatch((char*) fileNamePtr, ext)) {
		goto glob_matched;
	    } else {
		goto glob_unmatched;
	    }

	  glob_unmatched:
	    continue;

	  glob_matched:
	    globMatched = 1;
	    break;
	}

	for (mfPtr = clausePtr->macTypes; mfPtr; mfPtr = mfPtr->next) {
	    if (fileType == mfPtr->type) {
		macMatched = 1;
		break;
	    }
	}

	/*
	 * On Mac OS X, it is not uncommon for files to have NO
	 * file type.  But folks with Tcl code on Classic MacOS pretty
	 * much assume that a generic file will have type TEXT.  So
	 * if we were strict about matching types when the source file
	 * had NO type set, they would have to add another rule always
	 * with no fileType.  To avoid that, we pass the macMatch side
	 * of the test if no fileType is set.
	 */

	if (globMatched && (macMatched || (fileType == 0))) {
	    return MATCHED;
	}
    }

    return UNMATCHED;
}

/*
 *----------------------------------------------------------------------
 *
 * TkAboutDlg --
 *
 *        Displays the default Tk About box.  This code uses Macintosh
 *        resources to define the content of the About Box.
 *
 * Results:
 *        None.
 *
 * Side effects:
 *        None.
 *
 *----------------------------------------------------------------------
 */

void
TkAboutDlg()
{
    DialogPtr aboutDlog;
    WindowRef windowRef;
    short itemHit = -9;

    aboutDlog = GetNewDialog(128, NULL, (void *) (-1));

    if (!aboutDlog) {
	return;
    }

    windowRef = GetDialogWindow(aboutDlog);
    SelectWindow(windowRef);

    while (itemHit != 1) {
	ModalDialog(NULL, &itemHit);
    }
    DisposeDialog(aboutDlog);
    aboutDlog = NULL;

    SelectWindow(ActiveNonFloatingWindow());

    return;
}

/*
 *----------------------------------------------------------------------
 *
 * Tk_MessageBoxObjCmd --
 *
 *		Implements the tk_messageBox in native Mac OS X style.
 *
 * Results:
 *		A standard Tcl result.
 *
 * Side effects:
 *		none
 *
 *----------------------------------------------------------------------
 */

int
Tk_MessageBoxObjCmd(
    ClientData  clientData,	    /* Main window associated with interpreter. */
    Tcl_Interp  *interp,		/* Current interpreter. */
    int         objc,		    /* Number of arguments. */
    Tcl_Obj     *CONST objv[])	/* Argument objects. */
{
    Tk_Window   	    tkwin = (Tk_Window) clientData;
    AlertStdCFStringAlertParamRec paramCFStringRec;
    AlertType               alertType;
    DialogRef				dialogRef;
    CFStringRef 			messageTextCF = NULL;
    CFStringRef 			finemessageTextCF = NULL;
    OSErr                   osError;
    SInt16                  itemHit;
    Boolean                 haveDefaultOption = false;
    Boolean                 haveParentOption = false;
    char                    *str;
    int                     index;
    int                     defaultButtonIndex;
    int                     defaultNativeButtonIndex;    /* 1, 2, 3: right to left. */
    int                     typeIndex;
    int                     i;
    int                     indexDefaultOption = 0;
    int                     result = TCL_OK;

    static CONST char *movableAlertStrings[] = {
	"-default",/* "-detail",*/ "-icon",
	"-message", "-parent",
	"-title", "-type",
	(char *)NULL
    };
    static CONST char *movableTypeStrings[] = {
	"abortretryignore", "ok",
	"okcancel", "retrycancel",
	"yesno", "yesnocancel",
	(char *)NULL
    };
    static CONST char *movableButtonStrings[] = {
	"abort", "retry", "ignore",
	"ok", "cancel", "yes", "no",
	(char *)NULL
    };
    static CONST char *movableIconStrings[] = {
	"error", "info", "question", "warning",
	(char *)NULL
    };
    enum movableAlertOptions {
	ALERT_DEFAULT,/* ALERT_DETAIL,*/ ALERT_ICON,
	ALERT_MESSAGE, ALERT_PARENT,
	ALERT_TITLE, ALERT_TYPE
    };
    enum movableTypeOptions {
	TYPE_ABORTRETRYIGNORE, TYPE_OK,
	TYPE_OKCANCEL, TYPE_RETRYCANCEL,
	TYPE_YESNO, TYPE_YESNOCANCEL
    };
    enum movableButtonOptions {
	TEXT_ABORT, TEXT_RETRY, TEXT_IGNORE,
	TEXT_OK, TEXT_CANCEL, TEXT_YES, TEXT_NO
    };
    enum movableIconOptions {
	ICON_ERROR, ICON_INFO, ICON_QUESTION, ICON_WARNING
    };

    /*
     * Need to map from 'movableButtonStrings' and its corresponding integer index,
     * to the native button index, which is 1, 2, 3, from right to left.
     * This is necessary to do for each separate '-type' of button sets.
     */

    short   buttonIndexAndTypeToNativeButtonIndex[][7] = {
	/*  abort retry ignore ok   cancel yes   no    */
	{1,    2,    3,    0,    0,    0,    0},        /*  abortretryignore */
	{0,    0,    0,    1,    0,    0,    0},        /*  ok */
	{0,    0,    0,    1,    2,    0,    0},        /*  okcancel */
	{0,    1,    0,    0,    2,    0,    0},        /*  retrycancel */
	{0,    0,    0,    0,    0,    1,    2},        /*  yesno */
	{0,    0,    0,    0,    3,    1,    2},        /*  yesnocancel */
    };

    /*
     * Need also the inverse mapping, from native button (1, 2, 3) to the
     * descriptive button text string index.
     */

    short   nativeButtonIndexAndTypeToButtonIndex[][4] = {
	{-1, 0, 1, 2},        /*  abortretryignore */
	{-1, 3, 0, 0},        /*  ok */
	{-1, 3, 4, 0},        /*  okcancel */
	{-1, 1, 4, 0},        /*  retrycancel */
	{-1, 5, 6, 0},        /*  yesno */
	{-1, 5, 6, 4},        /*  yesnocancel */
    };

    alertType = kAlertPlainAlert;
    typeIndex = TYPE_OK;

    GetStandardAlertDefaultParams(&paramCFStringRec, kStdCFStringAlertVersionOne);
    paramCFStringRec.movable = true;
    paramCFStringRec.helpButton = false;
    paramCFStringRec.defaultButton = kAlertStdAlertOKButton;
    paramCFStringRec.cancelButton = kAlertStdAlertCancelButton;

    for (i = 1; i < objc; i += 2) {
	int     iconIndex;
	char    *string;

	if (Tcl_GetIndexFromObj(interp, objv[i], movableAlertStrings, "option",
				TCL_EXACT, &index) != TCL_OK) {
	    result = TCL_ERROR;
	    goto end;
	}
	if (i + 1 == objc) {
	    string = Tcl_GetStringFromObj(objv[i], NULL);
	    Tcl_AppendResult(interp, "value for \"", string, "\" missing",
			     (char *) NULL);
	    result = TCL_ERROR;
	    goto end;
	}

	switch (index) {

	    case ALERT_DEFAULT:

	    /*
	     * Need to postpone processing of this option until we are
	     * sure to know the '-type' as well.
	     */

	    haveDefaultOption = true;
	    indexDefaultOption = i;
	    break;

/*
	    case ALERT_DETAIL:
	    str = Tcl_GetStringFromObj(objv[i + 1], NULL);
	    finemessageTextCF = CFStringCreateWithCString(NULL, str, kCFStringEncodingUTF8);
	    break;
*/

	    case ALERT_ICON:
	    /*  not sure about UTF translation here... */
	    if (Tcl_GetIndexFromObj(interp, objv[i + 1], movableIconStrings,
				    "value", TCL_EXACT, &iconIndex) != TCL_OK) {
		result = TCL_ERROR;
		goto end;
	    }
	    switch (iconIndex) {
		case ICON_ERROR:
		alertType = kAlertStopAlert;
		break;
		case ICON_INFO:
		alertType = kAlertNoteAlert;
		break;
		case ICON_QUESTION:
		alertType = kAlertCautionAlert;
		break;
		case ICON_WARNING:
		alertType = kAlertCautionAlert;
		break;
	    }
	    break;

	    case ALERT_MESSAGE:
	    str = Tcl_GetStringFromObj(objv[i + 1], NULL);
	    messageTextCF = CFStringCreateWithCString(NULL, str, kCFStringEncodingUTF8);
	    break;

	    case ALERT_PARENT:
	    str = Tcl_GetStringFromObj(objv[i + 1], NULL);
	    tkwin = Tk_NameToWindow(interp, str, tkwin);
	    if (tkwin == NULL) {
		result = TCL_ERROR;
		goto end;
	    }
	    haveParentOption = true;
	    break;

	    case ALERT_TITLE:
	    break;

	    case ALERT_TYPE:
	    /*  not sure about UTF translation here... */
	    if (Tcl_GetIndexFromObj(interp, objv[i + 1], movableTypeStrings,
				    "value", TCL_EXACT, &typeIndex) != TCL_OK) {
		result = TCL_ERROR;
		goto end;
	    }
	    switch (typeIndex) {
		case TYPE_ABORTRETRYIGNORE:
		paramCFStringRec.defaultText = CFSTR("Abort");
		paramCFStringRec.cancelText = CFSTR("Retry");
		paramCFStringRec.otherText = CFSTR("Ignore");
		paramCFStringRec.cancelButton = kAlertStdAlertOtherButton;
		break;
		case TYPE_OK:
		paramCFStringRec.defaultText = CFSTR("OK");
		break;
		case TYPE_OKCANCEL:
		paramCFStringRec.defaultText = CFSTR("OK");
		paramCFStringRec.cancelText = CFSTR("Cancel");
		break;
		case TYPE_RETRYCANCEL:
		paramCFStringRec.defaultText = CFSTR("Retry");
		paramCFStringRec.cancelText = CFSTR("Cancel");
		break;
		case TYPE_YESNO:
		paramCFStringRec.defaultText = CFSTR("Yes");
		paramCFStringRec.cancelText = CFSTR("No");
		break;
		case TYPE_YESNOCANCEL:
		paramCFStringRec.defaultText = CFSTR("Yes");
		paramCFStringRec.cancelText = CFSTR("No");
		paramCFStringRec.otherText = CFSTR("Cancel");
		paramCFStringRec.cancelButton = kAlertStdAlertOtherButton;
		break;
	    }
	    break;
	}
    }

    if (haveDefaultOption) {

	/*
	 * Any '-default' option needs to know the '-type' option, which is why
	 * we do this here.
	 */

	str = Tcl_GetStringFromObj(objv[indexDefaultOption + 1], NULL);
	if (Tcl_GetIndexFromObj(interp, objv[indexDefaultOption + 1],
				movableButtonStrings, "value", TCL_EXACT,
				&defaultButtonIndex) != TCL_OK) {
	    result = TCL_ERROR;
	    goto end;
	}

	/* Need to map from "ok" etc. to 1, 2, 3, right to left. */

	defaultNativeButtonIndex =
	buttonIndexAndTypeToNativeButtonIndex[typeIndex][defaultButtonIndex];
	if (defaultNativeButtonIndex == 0) {
	    Tcl_SetObjResult(interp,
			     Tcl_NewStringObj("Illegal default option", -1));
	    result = TCL_ERROR;
	    goto end;
	}
	paramCFStringRec.defaultButton = defaultNativeButtonIndex;
    }
    SetThemeCursor(kThemeArrowCursor);

    if (haveParentOption) {
	TkWindow 			*winPtr;
	WindowRef			windowRef;
	EventTargetRef 		notifyTarget;
	EventHandlerUPP		handler;
	CallbackUserData	data;
	const EventTypeSpec kEvents[] = {
	    {kEventClassCommand, kEventProcessCommand}
	};

	winPtr = (TkWindow *) tkwin;

	/*
	 * Create the underlying Mac window for this Tk window.
	 */

	windowRef = GetWindowFromPort(
			TkMacOSXGetDrawablePort(Tk_WindowId(tkwin)));
	notifyTarget = GetWindowEventTarget(windowRef);
	osError = CreateStandardSheet(alertType, messageTextCF,
				      finemessageTextCF, &paramCFStringRec,
				      notifyTarget, &dialogRef);
	if(osError != noErr) {
	    result = TCL_ERROR;
	    goto end;
	}
	data.windowRef = windowRef;
	data.buttonIndex = 1;
	handler = NewEventHandlerUPP(AlertHandler);
	InstallEventHandler(notifyTarget, handler,
		GetEventTypeCount(kEvents),
		kEvents, &data, NULL);
	osError = ShowSheetWindow(GetDialogWindow(dialogRef), windowRef);
	if(osError != noErr) {
	    result = TCL_ERROR;
	    goto end;
	}
	osError = RunAppModalLoopForWindow(windowRef);
	if (osError != noErr) {
	    result = TCL_ERROR;
	    goto end;
	}
	itemHit = data.buttonIndex;
	DisposeEventHandlerUPP(handler);
    } else {
	osError = CreateStandardAlert(alertType, messageTextCF,
				      finemessageTextCF, &paramCFStringRec, &dialogRef);
	if(osError != noErr) {
	    result = TCL_ERROR;
	    goto end;
	}
	osError = RunStandardAlert(dialogRef, NULL, &itemHit);
	if (osError != noErr) {
	    result = TCL_ERROR;
	    goto end;
	}
    }
    if(osError == noErr) {
	int     ind;

	/*
	 * Map 'itemHit' (1, 2, 3) to descriptive text string.
	 */

	ind = nativeButtonIndexAndTypeToButtonIndex[typeIndex][itemHit];
	Tcl_SetObjResult(interp,
			 Tcl_NewStringObj(movableButtonStrings[ind], -1));
    } else {
	result = TCL_ERROR;
    }

    end:
    if (finemessageTextCF != NULL) {
	CFRelease(finemessageTextCF);
    }
    if (messageTextCF != NULL) {
	CFRelease(messageTextCF);
    }
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * AlertHandler --
 *
 *	Carbon event handler for the Standard Sheet dialog.
 *
 * Results:
 *	OSStatus if event handled or not.
 *
 * Side effects:
 *	May set userData.
 *
 *----------------------------------------------------------------------
 */

static OSStatus
AlertHandler(EventHandlerCallRef callRef, EventRef eventRef, void *userData)
{
    OSStatus		result = eventNotHandledErr;
    HICommand		cmd;
    CallbackUserData	*dataPtr = (CallbackUserData *) userData;

    GetEventParameter(eventRef, kEventParamDirectObject, typeHICommand,
		      NULL, sizeof(cmd), NULL, &cmd);
    switch (cmd.commandID) {
	case kHICommandOK:
	dataPtr->buttonIndex = 1;
	result = noErr;
	break;
	case kHICommandCancel:
	dataPtr->buttonIndex = 2;
	result = noErr;
	break;
	case kHICommandOther:
	dataPtr->buttonIndex = 3;
	result = noErr;
	break;
    }
    if (result == noErr) {
	result = QuitAppModalLoopForWindow(dataPtr->windowRef);
    }
    return result;
}