DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Untracked file

Line Code
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 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 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.0 (the "NPL"); you may not use this file except in
 * compliance with the NPL.  You may obtain a copy of the NPL at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the NPL is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
 * for the specific language governing rights and limitations under the
 * NPL.
 *
 * The Initial Developer of this code under the NPL is Netscape
 * Communications Corporation.  Portions created by Netscape are
 * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
 * Reserved.
 */

#include "selection.h"
#include <libi18n.h>
#include <layers.h>
#include <sys/utsname.h>
#include <pwd.h>
#include <Xm/CutPaste.h>	/* for PasteQuote clipboard hackery */
/*#include <Xm/AtomMgr.h>*/

#include "intl_csi.h"
#include "felocale.h"

static char          *fe_clipboard_text   = NULL;
static char          *fe_clipboard_html   = NULL;
static char          *fe_clipboard_data   = NULL;
static unsigned long  fe_clipboard_dlen   = 0;
static Time           fe_clipboard_time   = 0;
static Widget         fe_clipboard_widget = NULL;
static XP_Bool        fe_clipboard_motif  = False;
static XP_Bool        fe_clipboard_empty  = True;

static char          *fe_selection_text   = NULL;
static char          *fe_selection_html   = NULL;
static char          *fe_selection_data   = NULL;
static unsigned long  fe_selection_dlen   = 0;
static Time           fe_selection_time   = 0;
static Widget         fe_selection_widget = NULL;
static MWContext     *fe_selection_ctx    = NULL;
static MWContext     *fe_selection_lost   = NULL;
static XP_Bool        fe_selection_empty  = True;

static char fe_clipboard_data_name[] = "NETSCAPE_HTML";
static char fe_clipboard_html_name[] = "HTML";
static char fe_clipboard_text_name[] = "STRING";
static char fe_clipboard_app_name[] = "MOZILLA";

#define XFE_CCP_DATA (fe_clipboard_data_name)
#define XFE_CCP_HTML (fe_clipboard_html_name)
#define XFE_CCP_TEXT (fe_clipboard_text_name)
#define XFE_CCP_NAME (fe_clipboard_app_name)

/* Selections
 */

static Boolean
fe_selection_converter (Widget widget, Atom *selection, Atom *target,
			Atom *type_ret, XtPointer *value_ret,
			unsigned long *length_ret, int *format_ret)
{
  Display *dpy = XtDisplay (widget);
  XTextProperty tmp_prop;
  int status;
  MWContext *context = fe_WidgetToMWContext (widget);

  Atom COMPOUND_TEXT = XmInternAtom(dpy, "COMPOUND_TEXT", False);
  Atom XA_TARGETS   = XmInternAtom (dpy, "TARGETS", False);
  Atom XA_TIMESTAMP = XmInternAtom (dpy, "TIMESTAMP", False);
  Atom XA_NETSCAPE  = XmInternAtom (dpy, XFE_CCP_DATA, False);
  Atom XA_HTML      = XmInternAtom (dpy, XFE_CCP_HTML, False);
  Atom XA_TEXT      = XmInternAtom (dpy, "TEXT", False);
  Atom XA_LENGTH    = XmInternAtom (dpy, "LENGTH", False);
  Atom XA_FILE_NAME = XmInternAtom (dpy, "FILE_NAME", False);
  Atom XA_OWNER_OS  = XmInternAtom (dpy, "OWNER_OS", False);
  Atom XA_HOST_NAME = XmInternAtom (dpy, "HOST_NAME", False);
  Atom XA_USER      = XmInternAtom (dpy, "USER", False);
  Atom XA_CLASS     = XmInternAtom (dpy, "CLASS", False);
  Atom XA_NAME      = XmInternAtom (dpy, "NAME", False);
  Atom XA_CLIENT_WINDOW = XmInternAtom (dpy, "CLIENT_WINDOW", False);
  Atom XA_PROCESS   = XmInternAtom (dpy, "PROCESS", False);
#if 0
  Atom XA_CHARACTER_POSITION = XmInternAtom (dpy, "CHARACTER_POSITION", False);
#endif

  char *text = 0;
  char *html = 0;
  char *data = 0;
  unsigned long dlen = 0;
  Time time = 0;
  char *loc;
  INTL_CharSetInfo c;

  XP_ASSERT (context);

  c = LO_GetDocumentCharacterSetInfo(context);

  if (*selection == XA_PRIMARY) {
      text = fe_selection_text;
      html = fe_selection_html;
	  data = fe_selection_data;
	  dlen = fe_selection_dlen;
      time = fe_selection_time;
  }
  else {
#ifdef DEBUG_rhess
	  fprintf(stderr, "fe_selection_converter::[ CLIPBOARD ] -- NOT\n");
#endif
	  return False;
  }

  if (!text && !html && !data) {
#ifdef DEBUG_rhess
	  fprintf(stderr, "fe_selection_converter::[ NULL ]\n");
#endif
      *value_ret = NULL;
      *length_ret = 0;
      *type_ret = None;
      *format_ret = 0;
      return False;
  }

  /* I can't believe the contortions we need to go through here!! */

  if (*target == XA_TARGETS)
    {
      Atom *av = (Atom *) malloc (sizeof (Atom) * 20);
      int ac = 0;
      av [ac++] = XA_TARGETS;
      av [ac++] = XA_TIMESTAMP;
      av [ac++] = XA_NETSCAPE;
      av [ac++] = XA_HTML;
      av [ac++] = XA_TEXT;
      av [ac++] = XA_STRING;
      av [ac++] = XA_LENGTH;
#if 0
      av [ac++] = XA_CHARACTER_POSITION;
#endif
      av [ac++] = XA_FILE_NAME;
      av [ac++] = XA_OWNER_OS;
      av [ac++] = XA_HOST_NAME;
      av [ac++] = XA_USER;
      av [ac++] = XA_CLASS;
      av [ac++] = XA_NAME;
      av [ac++] = XA_CLIENT_WINDOW;
      av [ac++] = XA_PROCESS;
      av [ac++] = COMPOUND_TEXT;

      /* Other types that might be interesting (from the R6 ICCCM):

	 XA_MULTIPLE		(this is a pain and nobody uses it)
	 XA_LINE_NUMBER		start and end lines of the selection
	 XA_COLUMN_NUMBER	start and end column of the selection
	 XA_BACKGROUND		a list of Pixel values
	 XA_FOREGROUND		a list of Pixel values
	 XA_PIXMAP		a list of pixmap IDs (of the images?)
	 XA_DRAWABLE		a list of drawable IDs (?)

	 XA_COMPOUND_TEXT	for text

	 Hairy Image Conversions:
	 XA_APPLE_PICT		for images
	 XA_POSTSCRIPT
	 XA_ENCAPSULATED_POSTSCRIPT aka _ADOBE_EPS
	 XA_ENCAPSULATED_POSTSCRIPT_INTERCHANGE aka _ADOBE_EPSI
	 XA_ODIF		ISO Office Document Interchange Format
       */
      *value_ret = av;
      *length_ret = ac;
      *type_ret = XA_ATOM;
      *format_ret = 32;
      return True;
   }
  else if (*target == XA_TIMESTAMP)
    {
      Time *timestamp;
      timestamp = (Time *) malloc (sizeof (Time));
      *timestamp = time;
      *value_ret = (char *) timestamp;
      *length_ret = 1;
      *type_ret = XA_TIMESTAMP;
      *format_ret = 32;
      return True;
    }
  else if (*target == XA_NETSCAPE)
	  {
		  if (dlen > 0) {
			  text = XP_ALLOC(dlen+1);
			  
			  memcpy(text, data, dlen);
			  
			  *value_ret = text;
			  *length_ret = dlen;
		  }
		  else {
			  *value_ret = NULL;
			  *length_ret = 0;
		  }
		  *type_ret = XA_NETSCAPE;
		  *format_ret = 8;
		  return True;
	  }
  else if (*target == XA_HTML)
	  {
		  *value_ret = strdup (html);
		  *length_ret = strlen (html);
		  *type_ret = XA_HTML;
		  *format_ret = 8;
		  return True;
	  }
  else if (((*target == XA_TEXT) && (INTL_GetCSIWinCSID(c) == CS_LATIN1)) ||
           (*target == XA_STRING))
    {
      *value_ret = strdup (text);
      *length_ret = strlen (text);
      *type_ret = XA_STRING;
      *format_ret = 8;
      return True;
    }
  else if (*target == XA_TEXT)
    {
      loc = (char *) fe_ConvertToLocaleEncoding (INTL_GetCSIWinCSID(c),
                                                 (unsigned char *) text);
      status = XmbTextListToTextProperty (dpy, &loc, 1, XStdICCTextStyle,
                                          &tmp_prop);
      if (loc != text)
        {
          XP_FREE (loc);
        }
      if (status == Success)
        {
          *value_ret = (XtPointer) tmp_prop.value;
          *length_ret = tmp_prop.nitems;
          *type_ret = tmp_prop.encoding;    /* STRING or COMPOUND_TEXT */
          *format_ret = tmp_prop.format;
        }
      else
        {
          *value_ret = NULL;
          *length_ret = 0;
          return False;
        }
      return True;
    }
  else if (*target == COMPOUND_TEXT)
    {
      loc = (char *) fe_ConvertToLocaleEncoding (INTL_GetCSIWinCSID(c),
                                                 (unsigned char *) text);
      status = XmbTextListToTextProperty (dpy, &loc, 1, XCompoundTextStyle,
                                          &tmp_prop);
      if (loc != text)
        {
          XP_FREE (loc);
        }
      if (status == Success)
        {
          *value_ret = (XtPointer) tmp_prop.value;
          *length_ret = tmp_prop.nitems;
          *type_ret = COMPOUND_TEXT;
          *format_ret = 8;
        }
      else
        {
          *value_ret = NULL;
          *length_ret = 0;
          return False;
        }
      return True;
    }
  else if (*target == XA_LENGTH)
    {
      int *len = (int *) malloc (sizeof (int));
      *len = strlen (text);
      *value_ret = len;
      *length_ret = 1;
      *type_ret = XA_INTEGER;
      *format_ret = 32;
      return True;
    }
#if 0
  else if (*target == XA_CHARACTER_POSITION)
    {
      int32 *ends = (int32 *) malloc (sizeof (int32) * 2);
      LO_Element *s, *e;
      CL_Layer *layer;
      
      /* oops, this doesn't actually give me character positions -
	 just LO_Elements and indexes into them. */
      LO_GetSelectionEndpoints (context, &s, &e, &ends[0], &ends[1], &layer);
      *value_ret = ends;
      *length_ret = 2;
      *type_ret = XA_INTEGER;
      *format_ret = 32;
      return True;
    }
#endif
  else if (*target == XA_FILE_NAME)
    {
      History_entry *current = SHIST_GetCurrent (&context->hist);
      if (!current || !current->address)
	return False;
      *value_ret = strdup (current->address);
      *length_ret = strlen (current->address);
      *type_ret = XA_STRING;
      *format_ret = 8;
      return True;
    }
  else if (*target == XA_OWNER_OS)
    {
      char *os;
      struct utsname uts;
      if (uname (&uts) < 0)
	os = "uname failed";
      else
	os = uts.sysname;
      *value_ret = strdup (os);
      *length_ret = strlen (os);
      *type_ret = XA_STRING;
      *format_ret = 8;
      return True;
    }
  else if (*target == XA_HOST_NAME)
    {
      char name [255];
      if (gethostname (name, sizeof (name)))
	return False;
      *value_ret = strdup (name);
      *length_ret = strlen (name);
      *type_ret = XA_STRING;
      *format_ret = 8;
      return True;
    }
  else if (*target == XA_USER)
    {
      struct passwd *pw = getpwuid (geteuid ());
      char *real_uid = (pw ? pw->pw_name : "");
      char *user_uid = getenv ("LOGNAME");
      if (! user_uid) user_uid = getenv ("USER");
      if (! user_uid) user_uid = real_uid;
      if (! user_uid)
	return False;
      *value_ret = strdup (user_uid);
      *length_ret = strlen (user_uid);
      *type_ret = XA_STRING;
      *format_ret = 8;
      return True;
    }
  else if (*target == XA_CLASS)
    {
      *value_ret = strdup (fe_progclass);
      *length_ret = strlen (fe_progclass);
      *type_ret = XA_STRING;
      *format_ret = 8;
      return True;
    }
  else if (*target == XA_NAME)
    {
      *value_ret = strdup (fe_progname);
      *length_ret = strlen (fe_progname);
      *type_ret = XA_STRING;
      *format_ret = 8;
      return True;
    }
  else if (*target == XA_CLIENT_WINDOW)
    {
      Window *window;
      window = (Window *) malloc (sizeof (Window));
      *window = XtWindow (CONTEXT_WIDGET (context));
      *value_ret = window;
      *length_ret = 1;
      *type_ret = XA_WINDOW;
      *format_ret = 32;
      return True;
    }
  else if (*target == XA_PROCESS)
    {
      pid_t *pid;
      pid = (pid_t *) malloc (sizeof (pid_t));
      *pid = getpid ();
      *value_ret = pid;
      *length_ret = 1;
      *type_ret = XA_INTEGER;
      *format_ret = 32;
      return True;
    }
  else if (*target == XA_COLORMAP)
    {
      Colormap *cmap;
      cmap = (Colormap *) malloc (sizeof (Colormap));
      *cmap = fe_cmap(context);
      *value_ret = cmap;
      *length_ret = 1;
      *type_ret = XA_COLORMAP;
      *format_ret = 32;
      return True;
    }

  return False;
}

static Boolean
fe_clipboard_converter (Widget widget, Atom *selection, Atom *target,
						Atom *type_ret, XtPointer *value_ret,
						unsigned long *length_ret, int *format_ret)
{
	Display *dpy = XtDisplay (widget);
	XTextProperty tmp_prop;
	int status;
	
	Atom XA_TARGETS       = XmInternAtom (dpy, "TARGETS", False);
	Atom XA_TIMESTAMP     = XmInternAtom (dpy, "TIMESTAMP", False);
	Atom XA_TEXT          = XmInternAtom (dpy, "TEXT", False);
	Atom XA_HTML          = XmInternAtom (dpy, XFE_CCP_HTML, False);
	Atom XA_NETSCAPE      = XmInternAtom (dpy, XFE_CCP_DATA, False);
	Atom XA_LENGTH        = XmInternAtom (dpy, "LENGTH", False);
	
	char *text = NULL;
	char *html = NULL;
	char *data = NULL;
	unsigned long dlen = 0;
	Time time = 0;
	
	if (*selection == XmInternAtom (dpy, "CLIPBOARD", False)) {
		text = fe_clipboard_text;
		html = fe_clipboard_html;
		data = fe_clipboard_data;
		dlen = fe_clipboard_dlen;
		time = fe_clipboard_time;
	}
	else {
#ifdef DEBUG_rhess
		fprintf(stderr, "fe_clipboard_converter::[ PRIMARY ] -- NOT\n");
#endif
		return False;
	}
	
	if (!text && !html && !data) {
#ifdef DEBUG_rhess
		fprintf(stderr, "fe_clipboard_converter::[ NULL ]\n");
#endif
		return False;
	}

	if (*target == XA_TARGETS)
		{
			Atom *av = (Atom *) malloc (sizeof (Atom) * 20);
			int ac = 0;
			av [ac++] = XA_TARGETS;
			av [ac++] = XA_TIMESTAMP;
			av [ac++] = XA_TEXT;
			av [ac++] = XA_HTML;
			av [ac++] = XA_NETSCAPE;
			av [ac++] = XA_STRING;
			av [ac++] = XA_LENGTH;
			
			*value_ret = av;
			*length_ret = ac;
			*type_ret = XA_ATOM;
			*format_ret = 32;
			return True;
		}
	else if (*target == XA_TIMESTAMP) 
		{
			Time *timestamp;
			timestamp = (Time *) malloc (sizeof (Time));
			*timestamp = time;
			*value_ret = (char *) timestamp;
			*length_ret = 1;
			*type_ret = XA_TIMESTAMP;
			*format_ret = 32;
			return True;
		}
	else if ((*target == XA_TEXT) || 
			 (*target == XA_STRING)) 
		{
			if (text) {
				*value_ret = strdup (text);
				*length_ret = strlen (text);
				*type_ret = XA_STRING;
				*format_ret = 8;
				return True;
			}
			else {
				return False;
			}
		}
	else if (*target == XA_HTML)
		{
			if (html) {
				*value_ret = strdup (html);
				*length_ret = strlen (html);
				*type_ret = XA_HTML;
				*format_ret = 8;
				return True;
			}
			else {
				return False;
			}
		}
	else if (*target == XA_NETSCAPE)
		{
			if (dlen > 0) {
				text = XP_ALLOC(dlen+1);

				memcpy(text, data, dlen);

				*value_ret = text;
				*length_ret = dlen;
			}
			else {
				*value_ret = NULL;
				*length_ret = 0;
			}
			*type_ret = XA_NETSCAPE;
			*format_ret = 8;
			return True;
		}
	else if (*target == XA_LENGTH) 
		{
			int *len = (int *) malloc (sizeof (int));

			if (text) {
				*len = strlen(text);
				*value_ret = len;
				*length_ret = 1;
				*type_ret = XA_INTEGER;
				*format_ret = 32;
				return True;
			}
			else {
				return False;
			}
		}

	return False;
}

Boolean
fe_own_selection_p (MWContext *context, Boolean clip_p)
{
	Widget mainw = CONTEXT_WIDGET(context);
	Display *dpy = XtDisplay (mainw);

	if (clip_p) {

        /* Is there an internal clipboard entry? */
		if (fe_clipboard_text) {
            return True;
		} else {
			Atom atom = XmInternAtom (dpy, "CLIPBOARD", False);

			/* Is there an external clipboard owner? */
			if (XGetSelectionOwner (dpy, atom)) {
                return True;
			}
		}
	}
	else {
		/* Do we own the internal selection entry? */
		if (mainw == fe_selection_widget) {
            return True;
		} else {
			Window window = XGetSelectionOwner (dpy, XA_PRIMARY);
			Widget selection_widget = XtWindowToWidget(dpy, window);

			/* Is there a selection widget? */
			if (selection_widget) {
				XmTextPosition left, right;
				MWContext *ref;

                ref = fe_WidgetToMWContext(selection_widget);
                ref = XP_GetNonGridContext (ref);

				/* Does the selection widget belong to us?
                 * and is there a true selection?     
                 */
				if (ref == context
                    && (XmIsText(selection_widget)
                        || XmIsTextField(selection_widget))
                    && XmTextGetSelectionPosition(selection_widget,
                                                  &left, &right)
                    && ((right - left) > 0)) {
                    return True;
                }
            }
		}
	}
    return False;
}

/* Used by XFE_HTMLView::isCommandEnabled() */
Boolean
fe_can_cut (MWContext *context)
{
  return fe_own_selection_p(context, FALSE);
}

/* Used by XFE_HTMLView::isCommandEnabled() */
Boolean
fe_can_copy (MWContext *context)
{
  return fe_own_selection_p(context, FALSE);
}

/* Used by XFE_HTMLView::isCommandEnabled() */
Boolean
fe_can_paste (MWContext *context)
{
  return fe_own_selection_p(context, TRUE);
}

Window
xfe_GetClipboardWindow(Widget focus)
{
	Window target = NULL;

	if (fe_clipboard_widget)
		target = XtWindow(fe_clipboard_widget);
	else
		target = XtWindow(focus);

#ifdef DEBUG_rhess
	fprintf(stderr, "xfe_GetClipboardWindow::[ %p ][ %p ]\n", 
			XtWindow(focus), target);
#endif
	return target;
}

static void
fe_clipboard_loser (Widget widget, Atom *selection)
{
	Display *dpy = XtDisplay (widget);

	if (*selection == XmInternAtom (dpy, "CLIPBOARD", False)) {
#ifdef DEBUG_rhess
		fprintf(stderr, "fe_clipboard_loser::[ ]\n");
#endif
		fe_DisownClipboard (widget, 0);
	}
	else {
#ifdef DEBUG_rhess
		fprintf(stderr, "fe_clipboard_loser::[ invalid atom ]\n");
#endif
	}
}

static void
fe_selection_loser (Widget widget, Atom *selection)
{
	Display *dpy = XtDisplay (widget);
	MWContext *context = fe_WidgetToMWContext (widget);

	if (! context) {
		/* Are you talking to me? */
#ifdef DEBUG_rhess
		fprintf(stderr, "fe_selection_loser::[ NULL ]\n");
#endif
	}
	else if (*selection == XA_PRIMARY) {
#ifdef DEBUG_rhess
		fprintf(stderr, "fe_selection_loser::[ PRIMARY ]\n");
#endif
		fe_DisownPrimarySelection(widget, 0);
	}
	else {
#ifdef DEBUG_rhess
		fprintf(stderr, "fe_selection_loser::[ ERROR ][ invalid target ]\n");
#endif
	}
}

static void
fe_set_clipboard(Widget focus, Time time, 
				 char *text, char* html, char* data, unsigned long data_len,
				 XP_Bool motif_p)
{
	if (fe_clipboard_text) {
		XP_FREE(fe_clipboard_text);
		fe_clipboard_text = NULL;
	}
	
	if (fe_clipboard_html) {
		XP_FREE(fe_clipboard_html);
		fe_clipboard_html = NULL;
	}
	
	if (fe_clipboard_data) {
		XP_FREE(fe_clipboard_data);
		fe_clipboard_data = NULL;
	}

	if (text) {
		fe_clipboard_text = XP_STRDUP(text);
#ifdef DEBUG_rhess
		fprintf(stderr, 
				"fe_set_clipboard::[ %d ] TEXT\n", strlen(text));
#endif
	}

	if (html) {
		fe_clipboard_html = XP_STRDUP(html);
#ifdef DEBUG_rhess
		fprintf(stderr, 
				"fe_set_clipboard::[ %d ] HTML\n", strlen(html));
#endif
	}

	if (data) {
		fe_clipboard_data = XP_ALLOC(data_len+1);
		memcpy(fe_clipboard_data, data, data_len);
#ifdef DEBUG_rhess
		fprintf(stderr, 
				"fe_set_clipboard::[ %d ] DATA\n", data_len);
#endif
	}

	fe_clipboard_dlen   = data_len;
	fe_clipboard_time   = time;
	fe_clipboard_widget = focus;
	fe_clipboard_motif  = motif_p;
	fe_clipboard_empty  = False;
}

static void
fe_set_selection(Widget focus, Time time, 
				 char *text, char* html, char* data, unsigned long data_len,
				 MWContext* ctx
				 )
{
	if (fe_selection_text) {
		XP_FREE(fe_selection_text);
		fe_selection_text = NULL;
	}

	if (fe_selection_html) {
		XP_FREE(fe_selection_html);
		fe_selection_html = NULL;
	}

	if (fe_selection_data) {
		XP_FREE(fe_selection_data);
		fe_selection_data = NULL;
	}

	if (text) {
		fe_selection_text = XP_STRDUP(text);
#ifdef DEBUG_rhess
		fprintf(stderr, 
				"fe_set_selection::[ %d ] TEXT\n", strlen(text));
#endif
	}

	if (html) {
		fe_selection_html = XP_STRDUP(html);
#ifdef DEBUG_rhess
		fprintf(stderr, 
				"fe_set_selection::[ %d ] HTML\n", strlen(html));
#endif
	}

	if (data) {
		fe_selection_data = XP_ALLOC(data_len+1);
		memcpy(fe_selection_data, data, data_len);
#ifdef DEBUG_rhess
		fprintf(stderr, 
				"fe_set_selection::[ %d ] DATA\n", data_len);
#endif
	}

	if (fe_selection_ctx) {
		if (fe_selection_lost) {
#ifdef DEBUG_rhess
			fprintf(stderr, "WARNING... [ lost in fe_set_selection ][ %p ]\n",
					fe_selection_lost);
#endif			
		}
		fe_selection_lost = fe_selection_ctx;
	}

	fe_selection_ctx    = ctx;
	fe_selection_dlen   = data_len;
	fe_selection_time   = time;
	fe_selection_widget = focus;
	fe_selection_empty  = False;
}

static void
fe_clear_clipboard()
{
	if (fe_clipboard_empty) {
	}
	else {
		fe_clipboard_empty  = True;
#ifdef DEBUG_rhess
		fprintf(stderr, "fe_clear_clipboard::[ ]\n");
#endif
		if (fe_clipboard_text) {
			XP_FREE( fe_clipboard_text );
			fe_clipboard_text = NULL;
		}
	
		if (fe_clipboard_html) {
			XP_FREE( fe_clipboard_html );
			fe_clipboard_html = NULL;
		}
	
		if (fe_clipboard_data) {
			XP_FREE( fe_clipboard_data );
			fe_clipboard_data = NULL;
		}
	
		fe_clipboard_dlen   = 0;
		fe_clipboard_time   = 0;
		fe_clipboard_widget = NULL;
		fe_clipboard_motif  = False;
	}
}

static void
fe_clear_selection()
{
	if (fe_selection_empty) {
	}
	else {
		fe_selection_empty  = True;
#ifdef DEBUG_rhess
		fprintf(stderr, "fe_clear_selection::[ ]\n");
#endif
		if (fe_selection_text) {
			XP_FREE( fe_selection_text );
			fe_selection_text = NULL;
		}
		
		if (fe_selection_html) {
			XP_FREE( fe_selection_html );
			fe_selection_html = NULL;
		}
		
		if (fe_selection_data) {
			XP_FREE( fe_selection_data );
			fe_selection_data = NULL;
		}
	
		if (fe_selection_ctx) {
			if (fe_selection_lost) {
#ifdef DEBUG_rhess
				fprintf(stderr, 
						"WARING... [ lost in fe_clear_selection ][ %p ]\n",
						fe_selection_lost);
#endif			
			}
			fe_selection_lost = fe_selection_ctx;
		}

		fe_selection_ctx    = NULL;
		fe_selection_dlen   = 0;
		fe_selection_time   = 0;
		fe_selection_widget = NULL;
	}
}

#ifdef EDITOR

static void
fe_edt_selection_loser (Widget widget, Atom *selection)
{
	Display *dpy = XtDisplay (widget);
	MWContext *context = fe_WidgetToMWContext (widget);

	if (! context) {
		/* Are you talking to me? */
#ifdef DEBUG_rhess
		fprintf(stderr, "fe_edt_selection_loser::[ NULL ]\n");
#endif
	}
	else if (*selection == XA_PRIMARY) {
#ifdef DEBUG_rhess
		fprintf(stderr, "fe_edt_selection_loser::[ PRIMARY ]\n");
#endif
		fe_EditorDisownSelection (context, 0, False);
	}
	else if (*selection == XmInternAtom (dpy, "CLIPBOARD", False)) {
#ifdef DEBUG_rhess
		fprintf(stderr, "fe_edt_selection_loser::[ CLIPBOARD ]\n");
#endif
		fe_EditorDisownSelection (context, 0, True);
	}
}

void
fe_OwnEDTSelection (MWContext *context, Time time, Boolean clip_p, 
					char *text, char *html, char *data, unsigned long data_len)
{
	Widget mainw = CONTEXT_WIDGET(context);
	Display *dpy = XtDisplay (mainw);
	Atom atom = (clip_p ? XmInternAtom (dpy, "CLIPBOARD", False) : XA_PRIMARY);

	if (clip_p)
		{
#ifdef DEBUG_rhess
			fprintf(stderr, "fe_own_edt_selection::[ CLIPBOARD ]\n");
#endif
			fe_clear_clipboard();

			fe_set_clipboard(mainw, time, 
							 text, html, data, data_len, False);

			if (text) {
				XtOwnSelection (mainw, atom, time,
								fe_clipboard_converter,  /* conversion proc */
								fe_edt_selection_loser,  /* lost proc */
								0);	                     /* ACK proc */
			}
		}
	else
		{
#ifdef DEBUG_rhess
			fprintf(stderr, "fe_own_edt_selection::[ PRIMARY ]\n");
#endif
			fe_clear_selection();

			fe_set_selection(mainw, time, 
							 text, html, data, data_len, context);

			if (text) {
				XtOwnSelection (mainw, atom, time,
								fe_selection_converter,  /* conversion proc */
								fe_edt_selection_loser,  /* lost proc */
								0);	                     /* ACK proc */
			}
		}
}

#endif /* EDITOR */

void
fe_OwnClipboard(Widget focus, Time time,
				char* text, char* html, char* data, unsigned long data_len,
				XP_Bool motif_p)
{
  Display *dpy = XtDisplay (focus);
  Atom atom = XmInternAtom (dpy, "CLIPBOARD", False);

  if ((text && (strlen(text) > 0)) || 
	  (html && (strlen(html) > 0)) || 
	  (data && (data_len > 0))
	  ) {
	  fe_clear_clipboard();

	  fe_set_clipboard(focus, time, text, html, data, data_len, motif_p);

#ifdef DEBUG_rhess
	  if (data) {
		  if (html) {
			  if (text) {
				  fprintf(stderr, "fe_OwnClipboard::[ %p ][ %d, %d, %d ]\n", 
						  focus, strlen(text), strlen(html), data_len);
			  }
			  else {
				  fprintf(stderr, "fe_OwnClipboard::[ %p ][ NULL, %d, %d ]\n", 
						  focus, strlen(html), data_len);
			  }
		  }
		  else {
			  if (text) {
				  fprintf(stderr, "fe_OwnClipboard::[ %p ][ %d, NULL, %d ]\n", 
						  focus, strlen(text), data_len);
			  }
			  else {
				  fprintf(stderr, 
						  "fe_OwnClipboard::[ %p ][ NULL, NULL, %d ]\n", 
						  focus, data_len);
			  }
		  }
	  }
	  else {
		  if (html) {
			  if (text) {
				  fprintf(stderr, "fe_OwnClipboard::[ %p ][ %d, %d, NULL ]\n",
						  focus, strlen(text), strlen(html));
			  }
			  else {
				  fprintf(stderr, 
						  "fe_OwnClipboard::[ %p ][ NULL, %d, NULL ]\n", 
						  focus, strlen(html));
			  }
		  }
		  else {
			  if (text) {
				  fprintf(stderr, 
						  "fe_OwnClipboard::[ %p ][ %d, NULL, NULL ]\n",
						  focus, strlen(text));
			  }
			  else {
				  fprintf(stderr, 
						  "fe_OwnClipboard::[ %p ][ NULL, NULL, NULL ]\n", 
						  focus);
			  }
		  }
	  }
#endif
	  XtOwnSelection (focus, atom, time,
					  fe_clipboard_converter,	/* conversion proc */
					  fe_clipboard_loser,		/* lost proc */
					  0);						/* ACK proc */
  }

}

void
fe_DisownClipboard(Widget focus, Time time)
{
	Display *dpy = XtDisplay (focus);
	Atom atom = XmInternAtom (dpy, "CLIPBOARD", False);
	XtDisownSelection (focus, atom, time);

	if (focus == fe_clipboard_widget) {
#ifdef DEBUG_rhess
		fprintf(stderr, "fe_DisownClipboard::[ %p ]\n", focus);
#endif
		fe_clear_clipboard();
	}
}

void
fe_OwnPrimarySelection(Widget focus, Time time,
					   char* text, char* html, 
					   char* data, unsigned long data_len,
					   MWContext* context)
{
  Display *dpy = XtDisplay (focus);
  Atom atom = XA_PRIMARY;

  if ((text && (strlen(text) > 0)) || 
	  (html && (strlen(html) > 0)) || 
	  (data && (data_len > 0))
	  ) {
	  fe_clear_selection();

	  fe_set_selection(focus, time, text, html, data, data_len, context);

#ifdef DEBUG_rhess
	  if (text) {
		  if (html) {
			  fprintf(stderr, 
					  "fe_OwnPrimarySelection::[ %p ][ %d, %d, %d ]\n", 
					  focus, strlen(text), strlen(html), data_len);
		  }
		  else {
			  fprintf(stderr, 
					  "fe_OwnPrimarySelection::[ %p ][ %d, 0, %d ]\n", 
					  focus, strlen(text), data_len);
		  }
	  }
	  else {
		  if (html) {
			  fprintf(stderr, "fe_OwnPrimarySelection::[ %p ][ 0, %d, %d ]\n", 
					  focus, strlen(html), data_len);
		  }
		  else {
			  fprintf(stderr, "fe_OwnPrimarySelection::[ %p ][ 0, 0, %d ]\n", 
					  focus, data_len);
		  }
	  }
#endif
	  XtOwnSelection (focus, atom, time,
					  fe_selection_converter,	/* conversion proc */
					  fe_selection_loser,		/* lost proc */
					  0);						/* ACK proc */
  }

}

void
fe_DisownPrimarySelection(Widget focus, Time time)
{
	Display *dpy = XtDisplay (focus);
	Atom atom = XA_PRIMARY;

	XtDisownSelection (focus, atom, time);

	if (fe_selection_lost) {
		LO_ClearSelection(fe_selection_lost);
		fe_selection_lost = NULL;
	}
	else {
		if (focus == fe_selection_widget) {
#ifdef DEBUG_rhess
			fprintf(stderr, "fe_DisownPrimarySelection::[ %p ]\n", focus);
#endif
			if (fe_selection_empty) {
#ifdef DEBUG_rhess
				fprintf(stderr, "fe_DisownPrimarySelection::[ punt ]\n");
#endif
			}
			else {
				if (fe_selection_ctx) {
#ifdef EDITOR
					if ((fe_selection_ctx->type == 
						 MWContextMessageComposition) ||
						(fe_selection_ctx->type == MWContextEditor)) {
						if (!fe_ContextHasPopups(fe_selection_ctx)) {
#ifdef DEBUG_rhess
							fprintf(stderr, 
									"fe_DisownPrimarySelection::[ %p ] EDT\n",
									fe_selection_ctx);
#endif
							EDT_ClearSelection(fe_selection_ctx);
						}
					}
					else {
#endif /* EDITOR */

#ifdef DEBUG_rhess
						fprintf(stderr, 
								"fe_DisownPrimarySelection::[ %p ] LO\n",
								fe_selection_ctx);
#endif
						LO_ClearSelection(fe_selection_ctx);
#ifdef EDITOR
					}
#endif
					fe_selection_ctx = NULL;
				}
				fe_clear_selection();
			}
		}
	}
}

void
fe_OwnSelection (MWContext *context, Time time, Boolean clip_p)
{
	Widget mainw = CONTEXT_WIDGET(context);
	char *text = (char *) LO_GetSelectionText (context);

	if (clip_p) {
		fe_OwnClipboard(mainw, time, 
						text, NULL, NULL, 0, False);
	}
	else {
		fe_OwnPrimarySelection(mainw, time, 
							   text, NULL, NULL, 0, context);
	}

        if (text) XP_FREE(text);
}

void
fe_DisownSelection (MWContext *context, Time time, Boolean clip_p)
{
	Widget mainw = CONTEXT_WIDGET(context);

	if (clip_p) {
		if (fe_clipboard_widget) {
#ifdef DEBUG_rhess
			fprintf(stderr, "fe_DisownSelection::[ CLIPBOARD ]\n");
#endif
			fe_DisownClipboard(mainw, time);
		}
	}
	else {
		if (fe_selection_widget) {
#ifdef DEBUG_rhess
			fprintf(stderr, "fe_DisownSelection::[ PRIMARY ]\n");
#endif
			fe_DisownPrimarySelection(mainw, time);
		}
	}
}


int
xfe_clipboard_retrieve_work(Widget focus,
							char* name,
							char** buf, unsigned long* buf_len_a,
							Time timestamp) 
{
    Display* display  = XtDisplay(focus);
    Window   window   = xfe_GetClipboardWindow(focus);
    int      cb_result;
    long     private_id;
	int      i;

	char    *clip_data = NULL;
	char     work_data[512];

	unsigned long length   = 0;
	unsigned long clip_len = 0;

	cb_result = XmClipboardLock(display, window);

	if (cb_result != ClipboardSuccess) {
#ifdef DEBUG_rhess
		fprintf(stderr, "xfe_clipboard_retrieve::[ FAILED LOCK ]\n");
#endif
		*buf_len_a = 0;
		*buf       = NULL;
		return cb_result;
	}
		
#ifdef DEBUG_rhess
	fprintf(stderr, "xfe_clipboard_retrieve::[ LOCK ]\n");
#endif

	cb_result = XmClipboardInquireLength(display, window,
										 name, &length);

	if (cb_result != ClipboardSuccess) {
#ifdef DEBUG_rhess
		fprintf(stderr, "xfe_clipboard_retrieve::[ FAILED INQUIRE ]\n");
#endif
		XmClipboardUnlock(display, window, TRUE);

		*buf_len_a = 0;
		*buf       = NULL;
		return cb_result;
	}
		
	clip_data = XP_ALLOC(length+1);
	clip_len  = 0;

#ifdef DEBUG_rhess
	fprintf(stderr, "xfe_clipboard_retrieve::[ %d ]\n", length);
#endif

	cb_result = XmClipboardRetrieve(display, window,
									name,
									(XtPointer) clip_data,
									length,
									&clip_len,
									&private_id);

#ifdef DEBUG_rhess
	fprintf(stderr, "xfe_clipboard_retrieve::[ %d ]\n", clip_len);
#endif
	clip_data[clip_len] = '\0';

	*buf_len_a = clip_len;
	*buf       = clip_data;

	XmClipboardUnlock(display, window, TRUE);

#ifdef DEBUG_rhess
	fprintf(stderr, "xfe_clipboard_retrieve::[ UNLOCK ]\n");
#endif

	return cb_result;
}

char *
xfe2_GetClipboardText (Widget focus, int32 *internal)
{
    unsigned long length;
    int       cb_result;

    Display*  display   = XtDisplay(focus);
    Window    window    = xfe_GetClipboardWindow(focus);
	Time      timestamp = XtLastTimestampProcessed (display);

    struct fe_MWContext_cons *rest;

	char     *clip_data = NULL;
	char     *clip_name = NULL;
	int       i;

	if (fe_clipboard_motif || !fe_clipboard_widget) {
		*internal = 0;
	}
	else {
		MWContext *context = fe_WidgetToMWContext(fe_clipboard_widget);

		*internal = (int) context;
	}

	/*
	 * NOTE:  look in our "internal" clipboard cache...
	 *        - [ shortcut for internal paste ]
	 *
	 */
	if (!clip_data) {
		if (fe_clipboard_text) {
#ifdef DEBUG_rhess
			fprintf(stderr, "xfe2_GetClipboardText::[ cache ]\n");
#endif
			clip_data = XP_STRDUP(fe_clipboard_text);
		}
	}

    if (!clip_data) { /* go look on real CLIPBOARD */

		clip_name = XFE_CCP_TEXT;
#ifdef DEBUG_rhess
		fprintf(stderr, "xfe2_GetClipboardText::[ retrieve ]\n");
#endif
		cb_result = xfe_clipboard_retrieve_work(focus,
												clip_name,
												&clip_data, &length,
												timestamp);

		if (cb_result != ClipboardSuccess) {
#ifdef DEBUG_rhess
			fprintf(stderr, "xfe2_GetClipboardText::[ failed ]\n");
#endif
			XP_FREE(clip_data);
			clip_data = NULL;
			clip_name = NULL;
		}
    } 

	return clip_data;
}

static void
fe_text_insert (Widget text, const char *s, int16 charset)
{
  /* Gee, I wonder what other gratuitous junk I need to worry about? */
  Boolean pdel = True;
  XtVaGetValues (text, XmNpendingDelete, &pdel, 0);

  if (XmIsText (text))
    {
      char *s2 = strdup (s); /* This is nuts: it wants to modify the string! */
      char *loc;
      if (pdel) XmTextRemove (text);
      loc = (char *) fe_ConvertToLocaleEncoding (charset,
                                                 (unsigned char *) s2);
      XmTextInsert (text, XmTextGetInsertionPosition (text), loc);
      if (loc != s2)
        {
          free (loc);
        }
      free (s2);
    }
  else if (XmIsTextField (text))
    {
      char *s2 = strdup (s); /* This is nuts: it wants to modify the string! */
      char *loc;
      if (pdel) XmTextFieldRemove (text);
      loc = (char *) fe_ConvertToLocaleEncoding (charset,
                                                 (unsigned char *) s2);
      XmTextFieldInsert (text, XmTextFieldGetInsertionPosition (text), loc);
      if (loc != s2)
        {
          free (loc);
        }
      free (s2);
    }
  else
    abort ();
}

XP_Bool
fe_InternalSelection(XP_Bool clip_p, 
					 unsigned long *text_len, 
					 unsigned long *html_len,
					 unsigned long *data_len) 
{
	XP_Bool gotcha = False;
	char *text = NULL;
	char *html = NULL;
	char *data = NULL;
	unsigned long dlen = 0;

	if (clip_p) {
		text = fe_clipboard_text;
		html = fe_clipboard_html;
		data = fe_clipboard_data;
		dlen = fe_clipboard_dlen;
	}
	else {
		text = fe_selection_text;
		html = fe_selection_html;
		data = fe_selection_data;
		dlen = fe_selection_dlen;
	}

	if (text) {
		*text_len = strlen(text);
		gotcha = True;
	}
	else {
		*text_len = 0;
	}

	if (html) {
		*html_len = strlen(html);
		gotcha = True;
	}
	else {
		*html_len = 0;
	}

	if (dlen > 0) {
		*data_len = dlen;
		gotcha = True;
	}
	else {
		*data_len = 0;
	}
		
	return gotcha;
}

char *
fe_GetPrimaryText(void)
{
#ifdef DEBUG_rhess
	if (fe_selection_text) 
		fprintf(stderr, 
				"fe_GetPrimaryText::[ %d ]\n", strlen(fe_selection_text));
	else
		fprintf(stderr,
				"fe_GetPrimaryText::[ NULL ]\n");
#endif
	return fe_selection_text;
}

char *
fe_GetPrimaryHtml(void)
{
#ifdef DEBUG_rhess
	if (fe_selection_html)
		fprintf(stderr, 
				"fe_GetPrimaryHtml::[ %d ]\n", strlen(fe_selection_html));
	else
		fprintf(stderr,
				"fe_GetPrimaryHtml::[ NULL ]\n");
#endif
	return fe_selection_html;
}

char *
fe_GetPrimaryData(unsigned long *length)
{
#ifdef DEBUG_rhess
	if (fe_selection_data && (fe_selection_dlen > 0)) 
		fprintf(stderr, 
				"fe_GetPrimaryData::[ %d ]\n", fe_selection_dlen);
	else
		fprintf(stderr,
				"fe_GetPrimaryData::[ NULL ]\n");
#endif
	*length = fe_selection_dlen;

	if (fe_selection_dlen > 0) {
		return fe_selection_data;
	}
	else {
		return NULL;
	}
}

char *
fe_GetInternalText(void)
{
#ifdef DEBUG_rhess
	if (fe_clipboard_text) 
		fprintf(stderr, 
				"fe_GetInternalText::[ %d ]\n", strlen(fe_clipboard_text));
	else
		fprintf(stderr,
				"fe_GetInternalText::[ NULL ]\n");
#endif
	return fe_clipboard_text;
}

char *
fe_GetInternalHtml(void)
{
#ifdef DEBUG_rhess
	if (fe_clipboard_html) 
		fprintf(stderr, 
				"fe_GetInternalHtml::[ %d ]\n", strlen(fe_clipboard_html));
	else
		fprintf(stderr,
				"fe_GetInternalHtml::[ NULL ]\n");
#endif
	return fe_clipboard_html;
}

char *
fe_GetInternalData(unsigned long *length)
{
#ifdef DEBUG_rhess
	if (fe_clipboard_data && (fe_clipboard_dlen > 0)) 
		fprintf(stderr, 
				"fe_GetInternalData::[ %d ]\n", fe_clipboard_dlen);
	else
		fprintf(stderr,
				"fe_GetInternalData::[ NULL ]\n");
#endif
	*length = fe_clipboard_dlen;

	if (fe_clipboard_dlen > 0) {
		return fe_clipboard_data;
	}
	else {
		return NULL;
	}
}

/*
 * NOTE:  we need this to support the unified cut/copy/paste mechanism...
 *
 *        - this allows translations to call cut/copy on a specific widget
 *
 */
void
xfe_TextCopy(MWContext *context, Widget focus, XP_Bool cut)
{
	XP_Bool text_wp = False;
	XP_Bool text_fp = False;
	char      *clip = NULL;
	Time       time = 0;

	if (!focus) 
		return;

	text_wp = XmIsText(focus);
	text_fp = XmIsTextField(focus);

	if (text_wp || text_fp) {

		clip = fe_GetTextSelection(focus);
		if (clip) {
			time = XtLastTimestampProcessed(XtDisplay(focus));

			fe_OwnClipboard(focus, time, 
							clip, NULL, NULL, 0, True);
			XP_FREE(clip);
			
			if (cut) {
				if (text_wp)
					XmTextRemove(focus);
				else
					XmTextFieldRemove(focus);
#ifdef DEBUG_rhess
				fprintf(stderr, "xfe_TextCopy::[ CUT ]\n");
#endif
			}
#ifdef DEBUG_rhess
			else {
				fprintf(stderr, "xfe_TextCopy::[ ]\n");
			}
#endif			
		}
#ifdef DEBUG_rhess
		else {
			fprintf(stderr, "xfe_TextCopy::[ failed to get selection ]\n");
		}
#endif
	}
}

/* Motif sucks!  XmTextPaste() gets stuck if you're pasting from one
   widget to another in the same application! */
void
xfe_TextPaste (MWContext *context, Widget text, XP_Bool quote_p)
{
  struct fe_MWContext_cons *rest;
  char *clip = 0;
  int16 charset = CS_LATIN1;
  INTL_CharSetInfo csi;

  int32 internal = 0;
  MWContext *con = NULL;

  clip = xfe2_GetClipboardText(text, &internal);

  if (internal) {
	  con = (MWContext *) internal;
	  csi = LO_GetDocumentCharacterSetInfo(con);
	  charset = INTL_GetCSIWinCSID(csi);
  }
  else {
	  charset = INTL_DefaultWinCharSetID(NULL);
  }

#ifdef MOZ_MAIL_NEWS
  if (clip && quote_p)
    {
      char *n = MSG_ConvertToQuotation (clip);
      XP_ASSERT(n);
      XP_FREE(clip);
      clip = n;
      if (!clip) return;
    }
#endif

  if (clip)
    {
#ifdef DEBUG_rhess
	  fprintf(stderr, "xfe_TextPaste::[ insert ][ %d ]--[ %d ] [ %s ]\n", 
			  charset, CS_LATIN1, clip);
#endif
      fe_text_insert (text, clip, charset);
      XP_FREE(clip);
    }
  else
    XBell (XtDisplay (text), 0);
}


typedef enum { fe_Cut, fe_Copy, fe_Paste, fe_PasteQuoted } fe_CCP;

static void
fe_ccp (MWContext *context, XEvent *event, fe_CCP ccp)
{
  Widget focus = XmGetFocusWidget (CONTEXT_WIDGET (context));
  char *clip = NULL;
  Time time = (event && (event->type == KeyPress ||
			 event->type == KeyRelease)
	       ? event->xkey.time :
	       event && (event->type == ButtonPress ||
			 event->type == ButtonRelease)
	       ? event->xbutton.time :
	       XtLastTimestampProcessed (XtDisplay(CONTEXT_WIDGET (context))));

  if ((CONTEXT_WIDGET(context) != fe_selection_widget
       || (ccp != fe_Cut && ccp != fe_Copy))
      && focus && (XmIsText (focus) || XmIsTextField (focus)))
    switch (ccp)
      {
      case fe_Cut:	   
		  xfe_TextCopy (context, focus, TRUE);
		  break;
      case fe_Copy:	   
		  xfe_TextCopy (context, focus, FALSE);
		  break;
      case fe_Paste:	   
		  xfe_TextPaste (context, focus, FALSE); 
		  break;
      case fe_PasteQuoted: 
		  xfe_TextPaste (context, focus, TRUE); 
		  break;
      default:		   
		  XP_ASSERT(0); 
		  break;
      }
  else
    {
		if (ccp == fe_Copy) {
			fe_OwnSelection(context, time, True);
		}
		else {
			XBell (XtDisplay (CONTEXT_WIDGET (context)), 0);
		}
    }
}

void
fe_cut_cb (Widget widget, XtPointer closure, XtPointer call_data)
{
  MWContext *context = (MWContext *) closure;
  XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
  fe_UserActivity (context);

  fe_ccp (context, (cb ? cb->event : 0), fe_Cut);
}

void
fe_copy_cb (Widget widget, XtPointer closure, XtPointer call_data)
{
  MWContext *context = (MWContext *) closure;
  XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
  fe_UserActivity (context);

  fe_ccp (context, (cb ? cb->event : 0), fe_Copy);
}

void
fe_paste_cb (Widget widget, XtPointer closure, XtPointer call_data)
{
  MWContext *context = (MWContext *) closure;
  XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
  fe_UserActivity (context);

  fe_ccp (context, (cb ? cb->event : 0), fe_Paste);
}

void
fe_paste_quoted_cb (Widget widget, XtPointer closure, XtPointer call_data)
{
  MWContext *context = (MWContext *) closure;
  XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
  fe_UserActivity (context);

  fe_ccp (context, (cb ? cb->event : 0), fe_PasteQuoted);
}