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 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
|
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2005-2006 Novell, Inc. (http://www.novell.com)
//
// Authors:
// Peter Bartok <pbartok@novell.com>
//
//
// #define DEBUG
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using RTF=System.Windows.Forms.RTF;
namespace System.Windows.Forms {
[ClassInterface (ClassInterfaceType.AutoDispatch)]
[Docking (DockingBehavior.Ask)]
[ComVisible (true)]
[Designer ("System.Windows.Forms.Design.RichTextBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
public class RichTextBox : TextBoxBase {
#region Local Variables
internal bool auto_word_select;
internal int bullet_indent;
internal bool detect_urls;
private bool reuse_line; // Sometimes we are loading text with already available lines
internal int margin_right;
internal float zoom;
private StringBuilder rtf_line;
private RtfSectionStyle rtf_style; // Replaces individual style
// properties so we can revert
private Stack rtf_section_stack;
private RTF.TextMap rtf_text_map;
private int rtf_skip_count;
private int rtf_cursor_x;
private int rtf_cursor_y;
private int rtf_chars;
private bool enable_auto_drag_drop;
private RichTextBoxLanguageOptions language_option;
private bool rich_text_shortcuts_enabled;
private Color selection_back_color;
#endregion // Local Variables
#region Public Constructors
public RichTextBox() {
accepts_return = true;
auto_size = false;
auto_word_select = false;
bullet_indent = 0;
base.MaxLength = Int32.MaxValue;
margin_right = 0;
zoom = 1;
base.Multiline = true;
document.CRLFSize = 1;
shortcuts_enabled = true;
base.EnableLinks = true;
richtext = true;
rtf_style = new RtfSectionStyle ();
rtf_section_stack = null;
scrollbars = RichTextBoxScrollBars.Both;
alignment = HorizontalAlignment.Left;
LostFocus += new EventHandler(RichTextBox_LostFocus);
GotFocus += new EventHandler(RichTextBox_GotFocus);
BackColor = ThemeEngine.Current.ColorWindow;
backcolor_set = false;
language_option = RichTextBoxLanguageOptions.AutoFontSizeAdjust;
rich_text_shortcuts_enabled = true;
selection_back_color = DefaultBackColor;
ForeColor = ThemeEngine.Current.ColorWindowText;
base.HScrolled += new EventHandler(RichTextBox_HScrolled);
base.VScrolled += new EventHandler(RichTextBox_VScrolled);
SetStyle (ControlStyles.StandardDoubleClick, false);
}
#endregion // Public Constructors
#region Private & Internal Methods
internal override void HandleLinkClicked (LinkRectangle link)
{
OnLinkClicked (new LinkClickedEventArgs (link.LinkTag.LinkText));
}
internal override Color ChangeBackColor (Color backColor)
{
if (backColor == Color.Empty) {
backcolor_set = false;
if (!ReadOnly) {
backColor = SystemColors.Window;
}
}
return backColor;
}
internal override void RaiseSelectionChanged()
{
OnSelectionChanged (EventArgs.Empty);
}
private void RichTextBox_LostFocus(object sender, EventArgs e) {
Invalidate();
}
private void RichTextBox_GotFocus(object sender, EventArgs e) {
Invalidate();
}
#endregion // Private & Internal Methods
#region Public Instance Properties
[Browsable (false)]
public override bool AllowDrop {
get {
return base.AllowDrop;
}
set {
base.AllowDrop = value;
}
}
[DefaultValue(false)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
[RefreshProperties (RefreshProperties.Repaint)]
[EditorBrowsable (EditorBrowsableState.Never)]
[Browsable (false)]
public override bool AutoSize {
get {
return auto_size;
}
set {
base.AutoSize = value;
}
}
[MonoTODO ("Value not respected, always true")]
[DefaultValue(false)]
public bool AutoWordSelection {
get { return auto_word_select; }
set { auto_word_select = value; }
}
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public override System.Drawing.Image BackgroundImage {
get { return base.BackgroundImage; }
set { base.BackgroundImage = value; }
}
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Never)]
public override ImageLayout BackgroundImageLayout {
get { return base.BackgroundImageLayout; }
set { base.BackgroundImageLayout = value; }
}
[DefaultValue(0)]
[Localizable(true)]
public int BulletIndent {
get {
return bullet_indent;
}
set {
bullet_indent = value;
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool CanRedo {
get {
return document.undo.CanRedo;
}
}
[DefaultValue(true)]
public bool DetectUrls {
get { return base.EnableLinks; }
set { base.EnableLinks = value; }
}
[MonoTODO ("Stub, does nothing")]
[DefaultValue (false)]
public bool EnableAutoDragDrop {
get { return enable_auto_drag_drop; }
set { enable_auto_drag_drop = value; }
}
public override Font Font {
get {
return base.Font;
}
set {
if (font != value) {
Line start;
Line end;
if (auto_size) {
if (PreferredHeight != Height) {
Height = PreferredHeight;
}
}
base.Font = value;
// Font changes always set the whole doc to that font
start = document.GetLine(1);
end = document.GetLine(document.Lines);
document.FormatText(start, 1, end, end.text.Length + 1, base.Font, Color.Empty, Color.Empty, FormatSpecified.Font);
}
}
}
public override Color ForeColor {
get {
return base.ForeColor;
}
set {
base.ForeColor = value;
}
}
[MonoTODO ("Stub, does nothing")]
[Browsable (false)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public RichTextBoxLanguageOptions LanguageOption {
get { return language_option; }
set { language_option = value; }
}
[DefaultValue(Int32.MaxValue)]
public override int MaxLength {
get { return base.MaxLength; }
set { base.MaxLength = value; }
}
[DefaultValue(true)]
public override bool Multiline {
get {
return base.Multiline;
}
set {
base.Multiline = value;
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string RedoActionName {
get {
return document.undo.RedoActionName;
}
}
[MonoTODO ("Stub, does nothing")]
[Browsable (false)]
[DefaultValue (true)]
[EditorBrowsable (EditorBrowsableState.Never)]
public bool RichTextShortcutsEnabled {
get { return rich_text_shortcuts_enabled; }
set { rich_text_shortcuts_enabled = value; }
}
[DefaultValue(0)]
[Localizable(true)]
[MonoTODO ("Stub, does nothing")]
[MonoInternalNote ("Teach TextControl.RecalculateLine to consider the right margin as well")]
public int RightMargin {
get {
return margin_right;
}
set {
margin_right = value;
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[RefreshProperties (RefreshProperties.All)]
public string Rtf {
get {
Line start_line;
Line end_line;
start_line = document.GetLine(1);
end_line = document.GetLine(document.Lines);
return GenerateRTF(start_line, 0, end_line, end_line.text.Length).ToString();
}
set {
MemoryStream data;
document.Empty();
data = new MemoryStream(Encoding.ASCII.GetBytes(value), false);
InsertRTFFromStream(data, 0, 1);
data.Close();
Invalidate();
}
}
[DefaultValue(RichTextBoxScrollBars.Both)]
[Localizable(true)]
public RichTextBoxScrollBars ScrollBars {
get {
return scrollbars;
}
set {
if (!Enum.IsDefined (typeof (RichTextBoxScrollBars), value))
throw new InvalidEnumArgumentException ("value", (int) value,
typeof (RichTextBoxScrollBars));
if (value != scrollbars) {
scrollbars = value;
CalculateDocument ();
}
}
}
[Browsable(false)]
[DefaultValue("")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string SelectedRtf {
get {
return GenerateRTF(document.selection_start.line, document.selection_start.pos, document.selection_end.line, document.selection_end.pos).ToString();
}
set {
MemoryStream data;
int x;
int y;
int sel_start;
int chars;
Line line;
LineTag tag;
if (document.selection_visible) {
document.ReplaceSelection("", false);
}
sel_start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
data = new MemoryStream(Encoding.ASCII.GetBytes(value), false);
int cursor_x = document.selection_start.pos;
int cursor_y = document.selection_start.line.line_no;
// The RFT parser by default, when finds our x cursor in 0, it thinks if needs to
// add a new line; but in *this* scenario the line is already created, so force it to reuse it.
// Hackish, but works without touching the heart of the buggy parser.
if (cursor_x == 0)
reuse_line = true;
InsertRTFFromStream(data, cursor_x, cursor_y, out x, out y, out chars);
data.Close();
int nl_length = document.LineEndingLength (XplatUI.RunningOnUnix ? LineEnding.Rich : LineEnding.Hard);
document.CharIndexToLineTag(sel_start + chars + (y - document.selection_start.line.line_no) * nl_length,
out line, out tag, out sel_start);
if (sel_start >= line.text.Length)
sel_start = line.text.Length -1;
document.SetSelection(line, sel_start);
document.PositionCaret(line, sel_start);
document.DisplayCaret();
ScrollToCaret();
OnTextChanged(EventArgs.Empty);
}
}
[Browsable(false)]
[DefaultValue("")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override string SelectedText {
get {
return base.SelectedText;
}
set {
// TextBox/TextBoxBase don't set Modified in this same property
Modified = true;
base.SelectedText = value;
}
}
[Browsable(false)]
[DefaultValue(HorizontalAlignment.Left)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public HorizontalAlignment SelectionAlignment {
get {
HorizontalAlignment align;
Line start;
Line end;
Line line;
start = document.ParagraphStart(document.selection_start.line);
align = start.alignment;
end = document.ParagraphEnd(document.selection_end.line);
line = start;
while (true) {
if (line.alignment != align) {
return HorizontalAlignment.Left;
}
if (line == end) {
break;
}
line = document.GetLine(line.line_no + 1);
}
return align;
}
set {
Line start;
Line end;
Line line;
start = document.ParagraphStart(document.selection_start.line);
end = document.ParagraphEnd(document.selection_end.line);
line = start;
while (true) {
line.alignment = value;
if (line == end) {
break;
}
line = document.GetLine(line.line_no + 1);
}
this.CalculateDocument();
}
}
[MonoTODO ("Stub, does nothing")]
[Browsable (false)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public Color SelectionBackColor {
get { return selection_back_color; }
set { selection_back_color = value; }
}
[Browsable(false)]
[DefaultValue(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[MonoTODO ("Stub, does nothing")]
public bool SelectionBullet {
get {
return false;
}
set {
}
}
[Browsable(false)]
[DefaultValue(0)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[MonoTODO ("Stub, does nothing")]
public int SelectionCharOffset {
get {
return 0;
}
set {
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Color SelectionColor {
get {
Color color;
LineTag start;
LineTag end;
LineTag tag;
if (selection_length > 0) {
start = document.selection_start.line.FindTag (document.selection_start.pos + 1);
end = document.selection_start.line.FindTag (document.selection_end.pos);
} else {
start = document.selection_start.line.FindTag (document.selection_start.pos);
end = start;
}
color = start.Color;
tag = start;
while (tag != null) {
if (!color.Equals (tag.Color))
return Color.Empty;
if (tag == end)
break;
tag = document.NextTag (tag);
}
return color;
}
set {
if (value == Color.Empty)
value = DefaultForeColor;
int sel_start;
int sel_end;
sel_start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
sel_end = document.LineTagToCharIndex(document.selection_end.line, document.selection_end.pos);
document.FormatText (document.selection_start.line, document.selection_start.pos + 1,
document.selection_end.line, document.selection_end.pos + 1, null,
value, Color.Empty, FormatSpecified.Color);
document.CharIndexToLineTag(sel_start, out document.selection_start.line, out document.selection_start.tag, out document.selection_start.pos);
document.CharIndexToLineTag(sel_end, out document.selection_end.line, out document.selection_end.tag, out document.selection_end.pos);
document.UpdateView(document.selection_start.line, 0);
//Re-Align the caret in case its changed size or position
//probably not necessary here
document.AlignCaret(false);
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Font SelectionFont {
get {
Font font;
LineTag start;
LineTag end;
LineTag tag;
if (selection_length > 0) {
start = document.selection_start.line.FindTag (document.selection_start.pos + 1);
end = document.selection_start.line.FindTag (document.selection_end.pos);
} else {
start = document.selection_start.line.FindTag (document.selection_start.pos);
end = start;
}
font = start.Font;
if (selection_length > 1) {
tag = start;
while (tag != null) {
if (!font.Equals(tag.Font))
return null;
if (tag == end)
break;
tag = document.NextTag (tag);
}
}
return font;
}
set {
int sel_start;
int sel_end;
sel_start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
sel_end = document.LineTagToCharIndex(document.selection_end.line, document.selection_end.pos);
document.FormatText (document.selection_start.line, document.selection_start.pos + 1,
document.selection_end.line, document.selection_end.pos + 1, value,
Color.Empty, Color.Empty, FormatSpecified.Font);
document.CharIndexToLineTag(sel_start, out document.selection_start.line, out document.selection_start.tag, out document.selection_start.pos);
document.CharIndexToLineTag(sel_end, out document.selection_end.line, out document.selection_end.tag, out document.selection_end.pos);
document.UpdateView(document.selection_start.line, 0);
//Re-Align the caret in case its changed size or position
Document.AlignCaret (false);
}
}
[Browsable(false)]
[DefaultValue(0)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[MonoTODO ("Stub, does nothing")]
public int SelectionHangingIndent {
get {
return 0;
}
set {
}
}
[Browsable(false)]
[DefaultValue(0)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[MonoTODO ("Stub, does nothing")]
public int SelectionIndent {
get {
return 0;
}
set {
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override int SelectionLength {
get {
return base.SelectionLength;
}
set {
base.SelectionLength = value;
}
}
[Browsable(false)]
[DefaultValue(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[MonoTODO ("Stub, does nothing")]
public bool SelectionProtected {
get {
return false;
}
set {
}
}
[Browsable(false)]
[DefaultValue(0)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[MonoTODO ("Stub, does nothing")]
public int SelectionRightIndent {
get {
return 0;
}
set {
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[MonoTODO ("Stub, does nothing")]
public int[] SelectionTabs {
get {
return new int[0];
}
set {
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public RichTextBoxSelectionTypes SelectionType {
get {
if (document.selection_start == document.selection_end) {
return RichTextBoxSelectionTypes.Empty;
}
// Lazy, but works
if (SelectedText.Length > 1) {
return RichTextBoxSelectionTypes.MultiChar | RichTextBoxSelectionTypes.Text;
}
return RichTextBoxSelectionTypes.Text;
}
}
[DefaultValue(false)]
[MonoTODO ("Stub, does nothing")]
public bool ShowSelectionMargin {
get {
return false;
}
set {
}
}
[Localizable(true)]
[RefreshProperties (RefreshProperties.All)]
public override string Text {
get {
return base.Text;
}
set {
base.Text = value;
}
}
[Browsable(false)]
public override int TextLength {
get {
return base.TextLength;
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string UndoActionName {
get {
return document.undo.UndoActionName;
}
}
[Localizable(true)]
[DefaultValue(1)]
public float ZoomFactor {
get {
return zoom;
}
set {
zoom = value;
}
}
#endregion // Public Instance Properties
#region Protected Instance Properties
protected override CreateParams CreateParams {
get {
return base.CreateParams;
}
}
protected override Size DefaultSize {
get {
return new Size(100, 96);
}
}
#endregion // Protected Instance Properties
#region Public Instance Methods
public bool CanPaste(DataFormats.Format clipFormat) {
if ((clipFormat.Name == DataFormats.Rtf) ||
(clipFormat.Name == DataFormats.Text) ||
(clipFormat.Name == DataFormats.UnicodeText)) {
return true;
}
return false;
}
public int Find(char[] characterSet) {
return Find(characterSet, -1, -1);
}
public int Find(char[] characterSet, int start) {
return Find(characterSet, start, -1);
}
public int Find(char[] characterSet, int start, int end) {
Document.Marker start_mark;
Document.Marker end_mark;
Document.Marker result;
if (start == -1) {
document.GetMarker(out start_mark, true);
} else {
Line line;
LineTag tag;
int pos;
start_mark = new Document.Marker();
document.CharIndexToLineTag(start, out line, out tag, out pos);
start_mark.line = line;
start_mark.tag = tag;
start_mark.pos = pos;
}
if (end == -1) {
document.GetMarker(out end_mark, false);
} else {
Line line;
LineTag tag;
int pos;
end_mark = new Document.Marker();
document.CharIndexToLineTag(end, out line, out tag, out pos);
end_mark.line = line;
end_mark.tag = tag;
end_mark.pos = pos;
}
if (document.FindChars(characterSet, start_mark, end_mark, out result)) {
return document.LineTagToCharIndex(result.line, result.pos);
}
return -1;
}
public int Find(string str) {
return Find(str, -1, -1, RichTextBoxFinds.None);
}
public int Find(string str, int start, int end, RichTextBoxFinds options) {
Document.Marker start_mark;
Document.Marker end_mark;
Document.Marker result;
if (start == -1) {
document.GetMarker(out start_mark, true);
} else {
Line line;
LineTag tag;
int pos;
start_mark = new Document.Marker();
document.CharIndexToLineTag(start, out line, out tag, out pos);
start_mark.line = line;
start_mark.tag = tag;
start_mark.pos = pos;
}
if (end == -1) {
document.GetMarker(out end_mark, false);
} else {
Line line;
LineTag tag;
int pos;
end_mark = new Document.Marker();
document.CharIndexToLineTag(end, out line, out tag, out pos);
end_mark.line = line;
end_mark.tag = tag;
end_mark.pos = pos;
}
if (document.Find(str, start_mark, end_mark, out result, options)) {
return document.LineTagToCharIndex(result.line, result.pos);
}
return -1;
}
public int Find(string str, int start, RichTextBoxFinds options) {
return Find(str, start, -1, options);
}
public int Find(string str, RichTextBoxFinds options) {
return Find(str, -1, -1, options);
}
internal override char GetCharFromPositionInternal (Point p)
{
LineTag tag;
int pos;
PointToTagPos (p, out tag, out pos);
if (pos >= tag.Line.text.Length)
return '\n';
return tag.Line.text[pos];
}
public override int GetCharIndexFromPosition(Point pt) {
LineTag tag;
int pos;
PointToTagPos(pt, out tag, out pos);
return document.LineTagToCharIndex(tag.Line, pos);
}
public override int GetLineFromCharIndex(int index) {
Line line;
LineTag tag;
int pos;
document.CharIndexToLineTag(index, out line, out tag, out pos);
return line.LineNo - 1;
}
public override Point GetPositionFromCharIndex(int index) {
Line line;
LineTag tag;
int pos;
document.CharIndexToLineTag(index, out line, out tag, out pos);
return new Point(line.X + (int)line.widths[pos] + document.OffsetX - document.ViewPortX,
line.Y + document.OffsetY - document.ViewPortY);
}
public void LoadFile(System.IO.Stream data, RichTextBoxStreamType fileType) {
document.Empty();
// FIXME - ignoring unicode
if (fileType == RichTextBoxStreamType.PlainText) {
StringBuilder sb;
char[] buffer;
try {
sb = new StringBuilder ((int) data.Length);
buffer = new char [1024];
} catch {
throw new IOException("Not enough memory to load document");
}
StreamReader sr = new StreamReader (data, Encoding.Default, true);
int charsRead = sr.Read (buffer, 0, buffer.Length);
while (charsRead > 0) {
sb.Append (buffer, 0, charsRead);
charsRead = sr.Read (buffer, 0, buffer.Length);
}
// Remove the EOF converted to an extra EOL by the StreamReader
if (sb.Length > 0 && sb [sb.Length - 1] == '\n')
sb.Remove (sb.Length - 1, 1);
base.Text = sb.ToString();
return;
}
InsertRTFFromStream(data, 0, 1);
document.PositionCaret (document.GetLine (1), 0);
document.SetSelectionToCaret (true);
ScrollToCaret ();
}
public void LoadFile(string path) {
LoadFile (path, RichTextBoxStreamType.RichText);
}
public void LoadFile(string path, RichTextBoxStreamType fileType) {
FileStream data;
data = null;
try {
data = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 1024);
LoadFile(data, fileType);
}
#if !DEBUG
catch (Exception ex) {
throw new IOException("Could not open file " + path, ex);
}
#endif
finally {
if (data != null) {
data.Close();
}
}
}
public void Paste(DataFormats.Format clipFormat) {
base.Paste(Clipboard.GetDataObject(), clipFormat, false);
}
public void Redo()
{
if (document.undo.Redo ())
OnTextChanged (EventArgs.Empty);
}
public void SaveFile(Stream data, RichTextBoxStreamType fileType) {
Encoding encoding;
int i;
Byte[] bytes;
if (fileType == RichTextBoxStreamType.UnicodePlainText) {
encoding = Encoding.Unicode;
} else {
encoding = Encoding.ASCII;
}
switch(fileType) {
case RichTextBoxStreamType.PlainText:
case RichTextBoxStreamType.TextTextOleObjs:
case RichTextBoxStreamType.UnicodePlainText: {
if (!Multiline) {
bytes = encoding.GetBytes(document.Root.text.ToString());
data.Write(bytes, 0, bytes.Length);
return;
}
for (i = 1; i < document.Lines; i++) {
// Normalize the new lines to the system ones
string line_text = document.GetLine (i).TextWithoutEnding () + Environment.NewLine;
bytes = encoding.GetBytes(line_text);
data.Write(bytes, 0, bytes.Length);
}
bytes = encoding.GetBytes(document.GetLine(document.Lines).text.ToString());
data.Write(bytes, 0, bytes.Length);
return;
}
}
// If we're here we're saving RTF
Line start_line;
Line end_line;
StringBuilder rtf;
int current;
int total;
start_line = document.GetLine(1);
end_line = document.GetLine(document.Lines);
rtf = GenerateRTF(start_line, 0, end_line, end_line.text.Length);
total = rtf.Length;
bytes = new Byte[4096];
// Let's chunk it so we don't use up all memory...
for (i = 0; i < total; i += 1024) {
if ((i + 1024) < total) {
current = encoding.GetBytes(rtf.ToString(i, 1024), 0, 1024, bytes, 0);
} else {
current = total - i;
current = encoding.GetBytes(rtf.ToString(i, current), 0, current, bytes, 0);
}
data.Write(bytes, 0, current);
}
}
public void SaveFile(string path) {
if (path.EndsWith(".rtf")) {
SaveFile(path, RichTextBoxStreamType.RichText);
} else {
SaveFile(path, RichTextBoxStreamType.PlainText);
}
}
public void SaveFile(string path, RichTextBoxStreamType fileType) {
FileStream data;
data = null;
// try {
data = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, 1024, false);
SaveFile(data, fileType);
// }
// catch {
// throw new IOException("Could not write document to file " + path);
// }
// finally {
if (data != null) {
data.Close();
}
// }
}
[EditorBrowsable (EditorBrowsableState.Never)]
public new void DrawToBitmap (Bitmap bitmap, Rectangle targetBounds)
{
using (Graphics dc = Graphics.FromImage (bitmap))
Draw (dc, targetBounds);
}
#endregion // Public Instance Methods
#region Protected Instance Methods
protected virtual object CreateRichEditOleCallback()
{
throw new NotImplementedException ();
}
protected override void OnBackColorChanged (EventArgs e)
{
base.OnBackColorChanged (e);
}
protected virtual void OnContentsResized (ContentsResizedEventArgs e)
{
ContentsResizedEventHandler eh = (ContentsResizedEventHandler)(Events [ContentsResizedEvent]);
if (eh != null)
eh (this, e);
}
protected override void OnContextMenuChanged (EventArgs e)
{
base.OnContextMenuChanged (e);
}
protected override void OnHandleCreated (EventArgs e)
{
base.OnHandleCreated (e);
}
protected override void OnHandleDestroyed (EventArgs e)
{
base.OnHandleDestroyed (e);
}
protected virtual void OnHScroll(EventArgs e)
{
EventHandler eh = (EventHandler)(Events [HScrollEvent]);
if (eh != null)
eh (this, e);
}
[MonoTODO ("Stub, never called")]
protected virtual void OnImeChange(EventArgs e) {
EventHandler eh = (EventHandler)(Events [ImeChangeEvent]);
if (eh != null)
eh (this, e);
}
protected virtual void OnLinkClicked(LinkClickedEventArgs e) {
LinkClickedEventHandler eh = (LinkClickedEventHandler)(Events [LinkClickedEvent]);
if (eh != null)
eh (this, e);
}
protected virtual void OnProtected(EventArgs e) {
EventHandler eh = (EventHandler)(Events [ProtectedEvent]);
if (eh != null)
eh (this, e);
}
protected override void OnRightToLeftChanged(EventArgs e) {
base.OnRightToLeftChanged (e);
}
protected virtual void OnSelectionChanged(EventArgs e) {
EventHandler eh = (EventHandler)(Events [SelectionChangedEvent]);
if (eh != null)
eh (this, e);
}
protected virtual void OnVScroll(EventArgs e) {
EventHandler eh = (EventHandler)(Events [VScrollEvent]);
if (eh != null)
eh (this, e);
}
protected override void WndProc(ref Message m) {
base.WndProc (ref m);
}
protected override bool ProcessCmdKey (ref Message m, Keys keyData)
{
return base.ProcessCmdKey (ref m, keyData);
}
#endregion // Protected Instance Methods
#region Events
static object ContentsResizedEvent = new object ();
static object HScrollEvent = new object ();
static object ImeChangeEvent = new object ();
static object LinkClickedEvent = new object ();
static object ProtectedEvent = new object ();
static object SelectionChangedEvent = new object ();
static object VScrollEvent = new object ();
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public new event EventHandler BackgroundImageChanged {
add { base.BackgroundImageChanged += value; }
remove { base.BackgroundImageChanged -= value; }
}
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Never)]
public new event EventHandler BackgroundImageLayoutChanged {
add { base.BackgroundImageLayoutChanged += value; }
remove { base.BackgroundImageLayoutChanged -= value; }
}
public event ContentsResizedEventHandler ContentsResized {
add { Events.AddHandler (ContentsResizedEvent, value); }
remove { Events.RemoveHandler (ContentsResizedEvent, value); }
}
[Browsable(false)]
public new event DragEventHandler DragDrop {
add { base.DragDrop += value; }
remove { base.DragDrop -= value; }
}
[Browsable(false)]
public new event DragEventHandler DragEnter {
add { base.DragEnter += value; }
remove { base.DragEnter -= value; }
}
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public new event EventHandler DragLeave {
add { base.DragLeave += value; }
remove { base.DragLeave -= value; }
}
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public new event DragEventHandler DragOver {
add { base.DragOver += value; }
remove { base.DragOver -= value; }
}
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public new event GiveFeedbackEventHandler GiveFeedback {
add { base.GiveFeedback += value; }
remove { base.GiveFeedback -= value; }
}
public event EventHandler HScroll {
add { Events.AddHandler (HScrollEvent, value); }
remove { Events.RemoveHandler (HScrollEvent, value); }
}
public event EventHandler ImeChange {
add { Events.AddHandler (ImeChangeEvent, value); }
remove { Events.RemoveHandler (ImeChangeEvent, value); }
}
public event LinkClickedEventHandler LinkClicked {
add { Events.AddHandler (LinkClickedEvent, value); }
remove { Events.RemoveHandler (LinkClickedEvent, value); }
}
public event EventHandler Protected {
add { Events.AddHandler (ProtectedEvent, value); }
remove { Events.RemoveHandler (ProtectedEvent, value); }
}
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public new event QueryContinueDragEventHandler QueryContinueDrag {
add { base.QueryContinueDrag += value; }
remove { base.QueryContinueDrag -= value; }
}
[MonoTODO ("Event never raised")]
public event EventHandler SelectionChanged {
add { Events.AddHandler (SelectionChangedEvent, value); }
remove { Events.RemoveHandler (SelectionChangedEvent, value); }
}
public event EventHandler VScroll {
add { Events.AddHandler (VScrollEvent, value); }
remove { Events.RemoveHandler (VScrollEvent, value); }
}
#endregion // Events
#region Private Methods
internal override void SelectWord ()
{
document.ExpandSelection(CaretSelection.Word, false);
}
private class RtfSectionStyle : ICloneable {
internal Color rtf_color;
internal RTF.Font rtf_rtffont;
internal int rtf_rtffont_size;
internal FontStyle rtf_rtfstyle;
internal HorizontalAlignment rtf_rtfalign;
internal int rtf_par_line_left_indent;
internal bool rtf_visible;
internal int rtf_skip_width;
public object Clone ()
{
RtfSectionStyle new_style = new RtfSectionStyle ();
new_style.rtf_color = rtf_color;
new_style.rtf_par_line_left_indent = rtf_par_line_left_indent;
new_style.rtf_rtfalign = rtf_rtfalign;
new_style.rtf_rtffont = rtf_rtffont;
new_style.rtf_rtffont_size = rtf_rtffont_size;
new_style.rtf_rtfstyle = rtf_rtfstyle;
new_style.rtf_visible = rtf_visible;
new_style.rtf_skip_width = rtf_skip_width;
return new_style;
}
}
// To allow us to keep track of the sections and revert formatting
// as we go in and out of sections of the document.
private void HandleGroup (RTF.RTF rtf)
{
//start group - save the current formatting on to a stack
//end group - go back to the formatting at the current group
if (rtf_section_stack == null) {
rtf_section_stack = new Stack ();
}
if (rtf.Major == RTF.Major.BeginGroup) {
rtf_section_stack.Push (rtf_style.Clone ());
//spec specifies resetting unicode ignore at begin group as an attempt at error
//recovery.
rtf_skip_count = 0;
} else if (rtf.Major == RTF.Major.EndGroup) {
if (rtf_section_stack.Count > 0) {
FlushText (rtf, false);
rtf_style = (RtfSectionStyle) rtf_section_stack.Pop ();
}
}
}
[MonoInternalNote ("Add QuadJust support for justified alignment")]
private void HandleControl(RTF.RTF rtf) {
switch(rtf.Major) {
case RTF.Major.Unicode: {
switch(rtf.Minor) {
case RTF.Minor.UnicodeCharBytes: {
rtf_style.rtf_skip_width = rtf.Param;
break;
}
case RTF.Minor.UnicodeChar: {
FlushText (rtf, false);
rtf_skip_count += rtf_style.rtf_skip_width;
rtf_line.Append((char)rtf.Param);
break;
}
}
break;
}
case RTF.Major.Destination: {
// Console.Write("[Got Destination control {0}]", rtf.Minor);
rtf.SkipGroup();
break;
}
case RTF.Major.PictAttr:
if (rtf.Picture != null && rtf.Picture.IsValid ()) {
Line line = document.GetLine (rtf_cursor_y);
document.InsertPicture (line, 0, rtf.Picture);
rtf_cursor_x++;
FlushText (rtf, true);
rtf.Picture = null;
}
break;
case RTF.Major.CharAttr: {
switch(rtf.Minor) {
case RTF.Minor.ForeColor: {
System.Windows.Forms.RTF.Color color;
color = System.Windows.Forms.RTF.Color.GetColor(rtf, rtf.Param);
if (color != null) {
FlushText(rtf, false);
if (color.Red == -1 && color.Green == -1 && color.Blue == -1) {
this.rtf_style.rtf_color = ForeColor;
} else {
this.rtf_style.rtf_color = Color.FromArgb(color.Red, color.Green, color.Blue);
}
FlushText (rtf, false);
}
break;
}
case RTF.Minor.FontSize: {
FlushText(rtf, false);
this.rtf_style.rtf_rtffont_size = rtf.Param / 2;
break;
}
case RTF.Minor.FontNum: {
System.Windows.Forms.RTF.Font font;
font = System.Windows.Forms.RTF.Font.GetFont(rtf, rtf.Param);
if (font != null) {
FlushText(rtf, false);
this.rtf_style.rtf_rtffont = font;
}
break;
}
case RTF.Minor.Plain: {
FlushText(rtf, false);
rtf_style.rtf_rtfstyle = FontStyle.Regular;
break;
}
case RTF.Minor.Bold: {
FlushText(rtf, false);
if (rtf.Param == RTF.RTF.NoParam) {
rtf_style.rtf_rtfstyle |= FontStyle.Bold;
} else {
rtf_style.rtf_rtfstyle &= ~FontStyle.Bold;
}
break;
}
case RTF.Minor.Italic: {
FlushText(rtf, false);
if (rtf.Param == RTF.RTF.NoParam) {
rtf_style.rtf_rtfstyle |= FontStyle.Italic;
} else {
rtf_style.rtf_rtfstyle &= ~FontStyle.Italic;
}
break;
}
case RTF.Minor.StrikeThru: {
FlushText(rtf, false);
if (rtf.Param == RTF.RTF.NoParam) {
rtf_style.rtf_rtfstyle |= FontStyle.Strikeout;
} else {
rtf_style.rtf_rtfstyle &= ~FontStyle.Strikeout;
}
break;
}
case RTF.Minor.Underline: {
FlushText(rtf, false);
if (rtf.Param == RTF.RTF.NoParam) {
rtf_style.rtf_rtfstyle |= FontStyle.Underline;
} else {
rtf_style.rtf_rtfstyle = rtf_style.rtf_rtfstyle & ~FontStyle.Underline;
}
break;
}
case RTF.Minor.Invisible: {
FlushText (rtf, false);
rtf_style.rtf_visible = false;
break;
}
case RTF.Minor.NoUnderline: {
FlushText(rtf, false);
rtf_style.rtf_rtfstyle &= ~FontStyle.Underline;
break;
}
}
break;
}
case RTF.Major.ParAttr: {
switch (rtf.Minor) {
case RTF.Minor.ParDef:
FlushText (rtf, false);
rtf_style.rtf_par_line_left_indent = 0;
rtf_style.rtf_rtfalign = HorizontalAlignment.Left;
break;
case RTF.Minor.LeftIndent:
using (Graphics g = CreateGraphics ())
rtf_style.rtf_par_line_left_indent = (int) (((float) rtf.Param / 1440.0F) * g.DpiX + 0.5F);
break;
case RTF.Minor.QuadCenter:
FlushText (rtf, false);
rtf_style.rtf_rtfalign = HorizontalAlignment.Center;
break;
case RTF.Minor.QuadJust:
FlushText (rtf, false);
rtf_style.rtf_rtfalign = HorizontalAlignment.Center;
break;
case RTF.Minor.QuadLeft:
FlushText (rtf, false);
rtf_style.rtf_rtfalign = HorizontalAlignment.Left;
break;
case RTF.Minor.QuadRight:
FlushText (rtf, false);
rtf_style.rtf_rtfalign = HorizontalAlignment.Right;
break;
}
break;
}
case RTF.Major.SpecialChar: {
//Console.Write("[Got SpecialChar control {0}]", rtf.Minor);
SpecialChar (rtf);
break;
}
}
}
private void SpecialChar(RTF.RTF rtf) {
switch(rtf.Minor) {
case RTF.Minor.Page:
case RTF.Minor.Sect:
case RTF.Minor.Row:
case RTF.Minor.Line:
case RTF.Minor.Par: {
FlushText(rtf, true);
break;
}
case RTF.Minor.Cell: {
Console.Write(" ");
break;
}
case RTF.Minor.NoBrkSpace: {
Console.Write(" ");
break;
}
case RTF.Minor.Tab: {
rtf_line.Append ("\t");
// FlushText (rtf, false);
break;
}
case RTF.Minor.NoReqHyphen:
case RTF.Minor.NoBrkHyphen: {
rtf_line.Append ("-");
// FlushText (rtf, false);
break;
}
case RTF.Minor.Bullet: {
Console.WriteLine("*");
break;
}
case RTF.Minor.WidowCtrl:
break;
case RTF.Minor.EmDash: {
rtf_line.Append ("\u2014");
break;
}
case RTF.Minor.EnDash: {
rtf_line.Append ("\u2013");
break;
}
/*
case RTF.Minor.LQuote: {
Console.Write("\u2018");
break;
}
case RTF.Minor.RQuote: {
Console.Write("\u2019");
break;
}
case RTF.Minor.LDblQuote: {
Console.Write("\u201C");
break;
}
case RTF.Minor.RDblQuote: {
Console.Write("\u201D");
break;
}
*/
default: {
// Console.WriteLine ("skipped special char: {0}", rtf.Minor);
// rtf.SkipGroup();
break;
}
}
}
private void HandleText(RTF.RTF rtf) {
string str = rtf.EncodedText;
//todo - simplistically skips characters, should skip bytes?
if (rtf_skip_count > 0 && str.Length > 0) {
int iToRemove = Math.Min (rtf_skip_count, str.Length);
str = str.Substring (iToRemove);
rtf_skip_count-=iToRemove;
}
/*
if ((RTF.StandardCharCode)rtf.Minor != RTF.StandardCharCode.nothing) {
rtf_line.Append(rtf_text_map[(RTF.StandardCharCode)rtf.Minor]);
} else {
if ((int)rtf.Major > 31 && (int)rtf.Major < 128) {
rtf_line.Append((char)rtf.Major);
} else {
//rtf_line.Append((char)rtf.Major);
Console.Write("[Literal:0x{0:X2}]", (int)rtf.Major);
}
}
*/
if (rtf_style.rtf_visible)
rtf_line.Append (str);
}
private void FlushText(RTF.RTF rtf, bool newline) {
int length;
Font font;
length = rtf_line.Length;
if (!newline && (length == 0)) {
return;
}
if (rtf_style.rtf_rtffont == null) {
// First font in table is default
rtf_style.rtf_rtffont = System.Windows.Forms.RTF.Font.GetFont (rtf, 0);
}
font = new Font (rtf_style.rtf_rtffont.Name, rtf_style.rtf_rtffont_size, rtf_style.rtf_rtfstyle);
if (rtf_style.rtf_color == Color.Empty) {
System.Windows.Forms.RTF.Color color;
// First color in table is default
color = System.Windows.Forms.RTF.Color.GetColor (rtf, 0);
if ((color == null) || (color.Red == -1 && color.Green == -1 && color.Blue == -1)) {
rtf_style.rtf_color = ForeColor;
} else {
rtf_style.rtf_color = Color.FromArgb (color.Red, color.Green, color.Blue);
}
}
rtf_chars += rtf_line.Length;
// Try to re-use if we are told so - this usually happens when we are inserting a flow of rtf text
// with an already alive line.
if (rtf_cursor_x == 0 && !reuse_line) {
if (newline && rtf_line.ToString ().EndsWith (Environment.NewLine) == false)
rtf_line.Append (Environment.NewLine);
document.Add (rtf_cursor_y, rtf_line.ToString (), rtf_style.rtf_rtfalign, font, rtf_style.rtf_color,
newline ? LineEnding.Rich : LineEnding.Wrap);
if (rtf_style.rtf_par_line_left_indent != 0) {
Line line = document.GetLine (rtf_cursor_y);
line.indent = rtf_style.rtf_par_line_left_indent;
}
} else {
Line line;
line = document.GetLine (rtf_cursor_y);
line.indent = rtf_style.rtf_par_line_left_indent;
if (rtf_line.Length > 0) {
document.InsertString (line, rtf_cursor_x, rtf_line.ToString ());
document.FormatText (line, rtf_cursor_x + 1, line, rtf_cursor_x + 1 + length,
font, rtf_style.rtf_color, Color.Empty,
FormatSpecified.Font | FormatSpecified.Color);
}
if (newline) {
line = document.GetLine (rtf_cursor_y);
line.ending = LineEnding.Rich;
if (line.Text.EndsWith (Environment.NewLine) == false)
line.Text += Environment.NewLine;
}
reuse_line = false; // sanity assignment - in this case we have already re-used one line.
}
if (newline) {
rtf_cursor_x = 0;
rtf_cursor_y++;
} else {
rtf_cursor_x += length;
}
rtf_line.Length = 0; // Empty line
}
private void InsertRTFFromStream(Stream data, int cursor_x, int cursor_y) {
int x;
int y;
int chars;
InsertRTFFromStream(data, cursor_x, cursor_y, out x, out y, out chars);
}
private void InsertRTFFromStream(Stream data, int cursor_x, int cursor_y, out int to_x, out int to_y, out int chars) {
RTF.RTF rtf;
rtf = new RTF.RTF(data);
// Prepare
rtf.ClassCallback[RTF.TokenClass.Text] = new RTF.ClassDelegate(HandleText);
rtf.ClassCallback[RTF.TokenClass.Control] = new RTF.ClassDelegate(HandleControl);
rtf.ClassCallback[RTF.TokenClass.Group] = new RTF.ClassDelegate(HandleGroup);
rtf_skip_count = 0;
rtf_line = new StringBuilder();
rtf_style.rtf_color = Color.Empty;
rtf_style.rtf_rtffont_size = (int)this.Font.Size;
rtf_style.rtf_rtfalign = HorizontalAlignment.Left;
rtf_style.rtf_rtfstyle = FontStyle.Regular;
rtf_style.rtf_rtffont = null;
rtf_style.rtf_visible = true;
rtf_style.rtf_skip_width = 1;
rtf_cursor_x = cursor_x;
rtf_cursor_y = cursor_y;
rtf_chars = 0;
rtf.DefaultFont(this.Font.Name);
rtf_text_map = new RTF.TextMap();
RTF.TextMap.SetupStandardTable(rtf_text_map.Table);
document.SuspendRecalc ();
try {
rtf.Read(); // That's it
FlushText(rtf, false);
}
catch (RTF.RTFException e) {
#if DEBUG
throw e;
#endif
// Seems to be plain text or broken RTF
Console.WriteLine("RTF Parsing failure: {0}", e.Message);
}
to_x = rtf_cursor_x;
to_y = rtf_cursor_y;
chars = rtf_chars;
// clear the section stack if it was used
if (rtf_section_stack != null)
rtf_section_stack.Clear();
document.RecalculateDocument(CreateGraphicsInternal(), cursor_y, document.Lines, false);
document.ResumeRecalc (true);
document.Invalidate (document.GetLine(cursor_y), 0, document.GetLine(document.Lines), -1);
}
private void RichTextBox_HScrolled(object sender, EventArgs e) {
OnHScroll(e);
}
private void RichTextBox_VScrolled(object sender, EventArgs e) {
OnVScroll(e);
}
private void PointToTagPos(Point pt, out LineTag tag, out int pos) {
Point p;
p = pt;
if (p.X >= document.ViewPortWidth) {
p.X = document.ViewPortWidth - 1;
} else if (p.X < 0) {
p.X = 0;
}
if (p.Y >= document.ViewPortHeight) {
p.Y = document.ViewPortHeight - 1;
} else if (p.Y < 0) {
p.Y = 0;
}
tag = document.FindCursor(p.X + document.ViewPortX, p.Y + document.ViewPortY, out pos);
}
private void EmitRTFFontProperties(StringBuilder rtf, int prev_index, int font_index, Font prev_font, Font font) {
if (prev_index != font_index) {
rtf.Append(String.Format("\\f{0}", font_index)); // Font table entry
}
if ((prev_font == null) || (prev_font.Size != font.Size)) {
rtf.Append(String.Format("\\fs{0}", (int)(font.Size * 2))); // Font size
}
if ((prev_font == null) || (font.Bold != prev_font.Bold)) {
if (font.Bold) {
rtf.Append("\\b");
} else {
if (prev_font != null) {
rtf.Append("\\b0");
}
}
}
if ((prev_font == null) || (font.Italic != prev_font.Italic)) {
if (font.Italic) {
rtf.Append("\\i");
} else {
if (prev_font != null) {
rtf.Append("\\i0");
}
}
}
if ((prev_font == null) || (font.Strikeout != prev_font.Strikeout)) {
if (font.Strikeout) {
rtf.Append("\\strike");
} else {
if (prev_font != null) {
rtf.Append("\\strike0");
}
}
}
if ((prev_font == null) || (font.Underline != prev_font.Underline)) {
if (font.Underline) {
rtf.Append("\\ul");
} else {
if (prev_font != null) {
rtf.Append("\\ul0");
}
}
}
}
static readonly char [] ReservedRTFChars = new char [] { '\\', '{', '}' };
private void EmitRTFText(StringBuilder rtf, string text) {
int start = rtf.Length;
int count = text.Length;
// First emit simple unicode chars as escaped
EmitEscapedUnicode (rtf, text);
// This method emits user text *only*, so it's safe to escape any reserved rtf chars
// Escape '\' first, since it is used later to escape the other chars
if (text.IndexOfAny (ReservedRTFChars) > -1) {
rtf.Replace ("\\", "\\\\", start, count);
rtf.Replace ("{", "\\{", start, count);
rtf.Replace ("}", "\\}", start, count);
}
}
// The chars to be escaped use "\'" + its hexadecimal value.
private void EmitEscapedUnicode (StringBuilder sb, string text)
{
int pos;
int start = 0;
while ((pos = IndexOfNonAscii (text, start)) > -1) {
sb.Append (text, start, pos - start);
int n = (int)text [pos];
sb.Append ("\\'");
sb.Append (n.ToString ("X"));
start = pos + 1;
}
// Append remaining (maybe all) the text value.
if (start < text.Length)
sb.Append (text, start, text.Length - start);
}
// MS seems to be escaping values larger than 0x80
private int IndexOfNonAscii (string text, int startIndex)
{
for (int i = startIndex; i < text.Length; i++) {
int n = (int)text [i];
if (n < 0 || n >= 0x80)
return i;
}
return -1;
}
// start_pos and end_pos are 0-based
private StringBuilder GenerateRTF(Line start_line, int start_pos, Line end_line, int end_pos) {
StringBuilder sb;
ArrayList fonts;
ArrayList colors;
Color color;
Font font;
Line line;
LineTag tag;
int pos;
int line_no;
int line_len;
int i;
int length;
sb = new StringBuilder();
fonts = new ArrayList(10);
colors = new ArrayList(10);
// Two runs, first we parse to determine tables;
// and unlike most of our processing here we work on tags
line = start_line;
line_no = start_line.line_no;
pos = start_pos;
// Add default font and color; to optimize document content we don't
// use this.Font and this.ForeColor but the font/color from the first tag
tag = LineTag.FindTag(start_line, pos);
font = tag.Font;
color = tag.Color;
fonts.Add(font.Name);
colors.Add(color);
while (line_no <= end_line.line_no) {
line = document.GetLine(line_no);
tag = LineTag.FindTag(line, pos);
if (line_no != end_line.line_no) {
line_len = line.text.Length;
} else {
line_len = end_pos;
}
while (pos < line_len) {
if (tag.Font.Name != font.Name) {
font = tag.Font;
if (!fonts.Contains(font.Name)) {
fonts.Add(font.Name);
}
}
if (tag.Color != color) {
color = tag.Color;
if (!colors.Contains(color)) {
colors.Add(color);
}
}
pos = tag.Start + tag.Length - 1;
tag = tag.Next;
}
pos = 0;
line_no++;
}
// We have the tables, emit the header
sb.Append("{\\rtf1\\ansi");
sb.Append("\\ansicpg1252"); // FIXME - is this correct?
// Default Font
sb.Append(String.Format("\\deff{0}", fonts.IndexOf(this.Font.Name)));
// Default Language
sb.Append("\\deflang1033" + Environment.NewLine); // FIXME - always 1033?
// Emit the font table
sb.Append("{\\fonttbl");
for (i = 0; i < fonts.Count; i++) {
sb.Append(String.Format("{{\\f{0}", i)); // {Font
sb.Append("\\fnil"); // Family
sb.Append("\\fcharset0 "); // Charset ANSI<space>
sb.Append((string)fonts[i]); // Font name
sb.Append(";}"); // }
}
sb.Append("}");
sb.Append(Environment.NewLine);
// Emit the color table (if needed)
if ((colors.Count > 1) || ((((Color)colors[0]).R != this.ForeColor.R) || (((Color)colors[0]).G != this.ForeColor.G) || (((Color)colors[0]).B != this.ForeColor.B))) {
sb.Append("{\\colortbl "); // Header and NO! default color
for (i = 0; i < colors.Count; i++) {
sb.Append(String.Format("\\red{0}", ((Color)colors[i]).R));
sb.Append(String.Format("\\green{0}", ((Color)colors[i]).G));
sb.Append(String.Format("\\blue{0}", ((Color)colors[i]).B));
sb.Append(";");
}
sb.Append("}");
sb.Append(Environment.NewLine);
}
sb.Append("{\\*\\generator Mono RichTextBox;}");
// Emit initial paragraph settings
tag = LineTag.FindTag(start_line, start_pos);
sb.Append("\\pard"); // Reset to default paragraph properties
EmitRTFFontProperties(sb, -1, fonts.IndexOf(tag.Font.Name), null, tag.Font); // Font properties
sb.Append(" "); // Space separator
font = tag.Font;
color = (Color)colors[0];
line = start_line;
line_no = start_line.line_no;
pos = start_pos;
while (line_no <= end_line.line_no) {
line = document.GetLine(line_no);
tag = LineTag.FindTag(line, pos);
if (line_no != end_line.line_no) {
line_len = line.text.Length;
} else {
line_len = end_pos;
}
while (pos < line_len) {
length = sb.Length;
if (tag.Font != font) {
EmitRTFFontProperties(sb, fonts.IndexOf(font.Name), fonts.IndexOf(tag.Font.Name), font, tag.Font);
font = tag.Font;
}
if (tag.Color != color) {
color = tag.Color;
sb.Append(String.Format("\\cf{0}", colors.IndexOf(color)));
}
if (length != sb.Length) {
sb.Append(" "); // Emit space to separate keywords from text
}
// Emit the string itself
if (line_no != end_line.line_no) {
EmitRTFText(sb, tag.Line.text.ToString(pos, tag.Start + tag.Length - pos - 1));
} else {
if (end_pos < (tag.Start + tag.Length - 1)) {
// Emit partial tag only, end_pos is inside this tag
EmitRTFText(sb, tag.Line.text.ToString(pos, end_pos - pos));
} else {
EmitRTFText(sb, tag.Line.text.ToString(pos, tag.Start + tag.Length - pos - 1));
}
}
pos = tag.Start + tag.Length - 1;
tag = tag.Next;
}
if (pos >= line.text.Length) {
if (line.ending != LineEnding.Wrap) {
sb.Append("\\par");
sb.Append(Environment.NewLine);
}
}
pos = 0;
line_no++;
}
sb.Append("}");
sb.Append(Environment.NewLine);
return sb;
}
#endregion // Private Methods
}
}
|