TextControl.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. //
  26. // NOT COMPLETE
  27. #undef Debug
  28. using System;
  29. using System.Collections;
  30. using System.Drawing;
  31. using System.Drawing.Text;
  32. using System.Text;
  33. namespace System.Windows.Forms {
  34. public enum LineColor {
  35. Red = 0,
  36. Black = 1
  37. }
  38. public enum CaretDirection {
  39. CharForward, // Move a char to the right
  40. CharBack, // Move a char to the left
  41. LineUp, // Move a line up
  42. LineDown, // Move a line down
  43. Home, // Move to the beginning of the line
  44. End, // Move to the end of the line
  45. PgUp, // Move one page up
  46. PgDn, // Move one page down
  47. CtrlHome, // Move to the beginning of the document
  48. CtrlEnd, // Move to the end of the document
  49. WordBack, // Move to the beginning of the previous word (or beginning of line)
  50. WordForward // Move to the beginning of the next word (or end of line)
  51. }
  52. // Being cloneable should allow for nice line and document copies...
  53. public class Line : ICloneable, IComparable {
  54. #region Local Variables
  55. // Stuff that matters for our line
  56. internal StringBuilder text; // Characters for the line
  57. internal float[] widths; // Width of each character; always one larger than text.Length
  58. internal int space; // Number of elements in text and widths
  59. internal int line_no; // Line number
  60. internal LineTag tags; // Tags describing the text
  61. internal int Y; // Baseline
  62. internal int height; // Height of the line (height of tallest tag)
  63. internal int ascent; // Ascent of the line (ascent of the tallest tag)
  64. // Stuff that's important for the tree
  65. internal Line parent; // Our parent line
  66. public Line left; // Line with smaller line number
  67. public Line right; // Line with higher line number
  68. internal LineColor color; // We're doing a black/red tree. this is the node color
  69. internal int DEFAULT_TEXT_LEN; //
  70. internal static StringFormat string_format; // For calculating widths/heights
  71. internal bool recalc; // Line changed
  72. #endregion // Local Variables
  73. #region Constructors
  74. public Line() {
  75. color = LineColor.Red;
  76. left = null;
  77. right = null;
  78. parent = null;
  79. text = null;
  80. recalc = true;
  81. if (string_format == null) {
  82. string_format = new StringFormat(StringFormat.GenericTypographic);
  83. string_format.Trimming = StringTrimming.None;
  84. string_format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
  85. }
  86. }
  87. public Line(int LineNo, string Text, Font font, Brush color) : this() {
  88. space = Text.Length > DEFAULT_TEXT_LEN ? Text.Length+1 : DEFAULT_TEXT_LEN;
  89. text = new StringBuilder(Text, space);
  90. line_no = LineNo;
  91. widths = new float[space + 1];
  92. tags = new LineTag(this, 1, text.Length);
  93. tags.font = font;
  94. tags.color = color;
  95. }
  96. public Line(int LineNo, string Text, LineTag tag) : this() {
  97. space = Text.Length > DEFAULT_TEXT_LEN ? Text.Length+1 : DEFAULT_TEXT_LEN;
  98. text = new StringBuilder(Text, space);
  99. line_no = LineNo;
  100. widths = new float[space + 1];
  101. tags = tag;
  102. }
  103. #endregion // Constructors
  104. #region Public Properties
  105. public int Height {
  106. get {
  107. return height;
  108. }
  109. set {
  110. height = value;
  111. }
  112. }
  113. public int LineNo {
  114. get {
  115. return line_no;
  116. }
  117. set {
  118. line_no = value;
  119. }
  120. }
  121. public string Text {
  122. get {
  123. return text.ToString();
  124. }
  125. set {
  126. text = new StringBuilder(value, value.Length > DEFAULT_TEXT_LEN ? value.Length : DEFAULT_TEXT_LEN);
  127. }
  128. }
  129. #if no
  130. public StringBuilder Text {
  131. get {
  132. return text;
  133. }
  134. set {
  135. text = value;
  136. }
  137. }
  138. #endif
  139. #endregion // Public Properties
  140. #region Public Methods
  141. // Make sure we always have enoughs space in text and widths
  142. public void Grow(int minimum) {
  143. int length;
  144. float[] new_widths;
  145. length = text.Length;
  146. if ((length + minimum) > space) {
  147. // We need to grow; double the size
  148. if ((length + minimum) > (space * 2)) {
  149. new_widths = new float[length + minimum * 2 + 1];
  150. space = length + minimum * 2;
  151. } else {
  152. new_widths = new float[space * 2 + 1];
  153. space *= 2;
  154. }
  155. widths.CopyTo(new_widths, 0);
  156. widths = new_widths;
  157. }
  158. }
  159. public void Streamline() {
  160. LineTag current;
  161. LineTag next;
  162. current = this.tags;
  163. next = current.next;
  164. // Catch what the loop below wont; eliminate 0 length
  165. // tags, but only if there are other tags after us
  166. while ((current.length == 0) && (next != null)) {
  167. tags = next;
  168. current = next;
  169. next = current.next;
  170. }
  171. if (next == null) {
  172. return;
  173. }
  174. while (next != null) {
  175. // Take out 0 length tags
  176. if (next.length == 0) {
  177. current.next = next.next;
  178. if (current.next != null) {
  179. current.next.previous = current;
  180. }
  181. next = current.next;
  182. continue;
  183. }
  184. if (current.Combine(next)) {
  185. next = current.next;
  186. continue;
  187. }
  188. current = current.next;
  189. next = current.next;
  190. }
  191. }
  192. // Find the tag on a line based on the character position
  193. public LineTag FindTag(int pos) {
  194. LineTag tag;
  195. if (pos == 0) {
  196. return tags;
  197. }
  198. tag = this.tags;
  199. if (pos > text.Length) {
  200. pos = text.Length;
  201. }
  202. while (tag != null) {
  203. if ((tag.start <= pos) && (pos < (tag.start + tag.length))) {
  204. return tag;
  205. }
  206. tag = tag.next;
  207. }
  208. return null;
  209. }
  210. //
  211. // Go through all tags on a line and recalculate all size-related values
  212. // returns true if lineheight changed
  213. //
  214. public bool RecalculateLine(Graphics g) {
  215. LineTag tag;
  216. int pos;
  217. int len;
  218. SizeF size;
  219. float w;
  220. int prev_height;
  221. pos = 0;
  222. len = this.text.Length;
  223. tag = this.tags;
  224. prev_height = this.height; // For drawing optimization calculations
  225. this.height = 0; // Reset line height
  226. this.ascent = 0; // Reset the ascent for the line
  227. tag.shift = 0;
  228. tag.width = 0;
  229. widths[0] = 0;
  230. this.recalc = false;
  231. while (pos < len) {
  232. size = g.MeasureString(this.text.ToString(pos, 1), tag.font, 10000, string_format);
  233. w = size.Width;
  234. tag.width += w;
  235. pos++;
  236. widths[pos] = widths[pos-1] + w;
  237. if (pos == (tag.start-1 + tag.length)) {
  238. // We just found the end of our current tag
  239. tag.height = (int)tag.font.Height;
  240. // Check if we're the tallest on the line (so far)
  241. if (tag.height > this.height) {
  242. this.height = tag.height; // Yep; make sure the line knows
  243. }
  244. if (tag.ascent == 0) {
  245. int descent;
  246. XplatUI.GetFontMetrics(g, tag.font, out tag.ascent, out descent);
  247. }
  248. if (tag.ascent > this.ascent) {
  249. LineTag t;
  250. // We have a tag that has a taller ascent than the line;
  251. t = tags;
  252. while (t != tag) {
  253. t.shift = tag.ascent - t.ascent;
  254. t = t.next;
  255. }
  256. // Save on our line
  257. this.ascent = tag.ascent;
  258. } else {
  259. tag.shift = this.ascent - tag.ascent;
  260. }
  261. // Update our horizontal starting pixel position
  262. if (tag.previous == null) {
  263. tag.X = 0;
  264. } else {
  265. tag.X = tag.previous.X + (int)tag.previous.width;
  266. }
  267. tag = tag.next;
  268. if (tag != null) {
  269. tag.width = 0;
  270. tag.shift = 0;
  271. }
  272. }
  273. }
  274. if (this.height == 0) {
  275. this.height = tags.font.Height;
  276. tag.height = this.height;
  277. }
  278. if (prev_height != this.height) {
  279. return true;
  280. }
  281. return false;
  282. }
  283. #endregion // Public Methods
  284. #region Administrative
  285. public int CompareTo(object obj) {
  286. if (obj == null) {
  287. return 1;
  288. }
  289. if (! (obj is Line)) {
  290. throw new ArgumentException("Object is not of type Line", "obj");
  291. }
  292. if (line_no < ((Line)obj).line_no) {
  293. return -1;
  294. } else if (line_no > ((Line)obj).line_no) {
  295. return 1;
  296. } else {
  297. return 0;
  298. }
  299. }
  300. public object Clone() {
  301. Line clone;
  302. clone = new Line();
  303. clone.text = text;
  304. if (left != null) {
  305. clone.left = (Line)left.Clone();
  306. }
  307. if (left != null) {
  308. clone.left = (Line)left.Clone();
  309. }
  310. return clone;
  311. }
  312. public object CloneLine() {
  313. Line clone;
  314. clone = new Line();
  315. clone.text = text;
  316. return clone;
  317. }
  318. public override bool Equals(object obj) {
  319. if (obj == null) {
  320. return false;
  321. }
  322. if (!(obj is Line)) {
  323. return false;
  324. }
  325. if (obj == this) {
  326. return true;
  327. }
  328. if (line_no == ((Line)obj).line_no) {
  329. return true;
  330. }
  331. return false;
  332. }
  333. public override string ToString() {
  334. return "Line " + line_no;
  335. }
  336. #endregion // Administrative
  337. }
  338. public class Document : ICloneable, IEnumerable {
  339. #region Structures
  340. internal struct Marker {
  341. internal Line line;
  342. internal LineTag tag;
  343. internal int pos;
  344. internal int height;
  345. }
  346. #endregion Structures
  347. #region Local Variables
  348. private Line document;
  349. private int lines;
  350. private static Line sentinel;
  351. private Line last_found;
  352. private int document_id;
  353. private Random random = new Random();
  354. internal bool multiline;
  355. private Line selection_start_line;
  356. private int selection_start_pos;
  357. private Line selection_end_line;
  358. private int selection_end_pos;
  359. internal Marker caret;
  360. internal Marker selection_start;
  361. internal Marker selection_end;
  362. internal int viewport_x;
  363. internal int viewport_y; // The visible area of the document
  364. internal int document_x; // Width of the document
  365. internal int document_y; // Height of the document
  366. internal Control owner; // Who's owning us?
  367. #endregion // Local Variables
  368. #region Constructors
  369. public Document(Control owner) {
  370. lines = 0;
  371. this.owner = owner;
  372. multiline = true;
  373. // Tree related stuff
  374. sentinel = new Line();
  375. sentinel.color = LineColor.Black;
  376. document = sentinel;
  377. last_found = sentinel;
  378. // We always have a blank line
  379. Add(1, "", owner.Font, new SolidBrush(owner.ForeColor));
  380. this.RecalculateDocument(owner.CreateGraphics());
  381. PositionCaret(0, 0);
  382. lines=1;
  383. // Default selection is empty
  384. document_id = random.Next();
  385. }
  386. #endregion
  387. #region Public Properties
  388. public Line Root {
  389. get {
  390. return document;
  391. }
  392. set {
  393. document = value;
  394. }
  395. }
  396. public int Lines {
  397. get {
  398. return lines;
  399. }
  400. }
  401. public Line CaretLine {
  402. get {
  403. return caret.line;
  404. }
  405. }
  406. public int CaretPosition {
  407. get {
  408. return caret.pos;
  409. }
  410. }
  411. public LineTag CaretTag {
  412. get {
  413. return caret.tag;
  414. }
  415. }
  416. public int ViewPortX {
  417. get {
  418. return viewport_x;
  419. }
  420. set {
  421. viewport_x = value;
  422. }
  423. }
  424. public int ViewPortY {
  425. get {
  426. return viewport_y;
  427. }
  428. set {
  429. viewport_y = value;
  430. }
  431. }
  432. #endregion // Public Properties
  433. #region Private Methods
  434. // For debugging
  435. internal void DumpTree(Line line, bool with_tags) {
  436. Console.Write("Line {0}, Y: {1} Text {2}", line.line_no, line.Y, line.text != null ? line.text.ToString() : "undefined");
  437. if (line.left == sentinel) {
  438. Console.Write(", left = sentinel");
  439. } else if (line.left == null) {
  440. Console.Write(", left = NULL");
  441. }
  442. if (line.right == sentinel) {
  443. Console.Write(", right = sentinel");
  444. } else if (line.right == null) {
  445. Console.Write(", right = NULL");
  446. }
  447. Console.WriteLine("");
  448. if (with_tags) {
  449. LineTag tag;
  450. int count;
  451. tag = line.tags;
  452. count = 1;
  453. Console.Write(" Tags: ");
  454. while (tag != null) {
  455. Console.Write("{0} <{1}>-<{2}> ", count++, tag.start, tag.length);
  456. if (tag.line != line) {
  457. Console.Write("BAD line link");
  458. throw new Exception("Bad line link in tree");
  459. }
  460. tag = tag.next;
  461. if (tag != null) {
  462. Console.Write(", ");
  463. }
  464. }
  465. Console.WriteLine("");
  466. }
  467. if (line.left != null) {
  468. if (line.left != sentinel) {
  469. DumpTree(line.left, with_tags);
  470. }
  471. } else {
  472. if (line != sentinel) {
  473. throw new Exception("Left should not be NULL");
  474. }
  475. }
  476. if (line.right != null) {
  477. if (line.right != sentinel) {
  478. DumpTree(line.right, with_tags);
  479. }
  480. } else {
  481. if (line != sentinel) {
  482. throw new Exception("Right should not be NULL");
  483. }
  484. }
  485. }
  486. private void DecrementLines(int line_no) {
  487. int current;
  488. current = line_no;
  489. while (current <= lines) {
  490. GetLine(current).line_no--;
  491. current++;
  492. }
  493. return;
  494. }
  495. private void IncrementLines(int line_no) {
  496. int current;
  497. current = this.lines;
  498. while (current >= line_no) {
  499. GetLine(current).line_no++;
  500. current--;
  501. }
  502. return;
  503. }
  504. private void RebalanceAfterAdd(Line line1) {
  505. Line line2;
  506. while ((line1 != document) && (line1.parent.color == LineColor.Red)) {
  507. if (line1.parent == line1.parent.parent.left) {
  508. line2 = line1.parent.parent.right;
  509. if ((line2 != null) && (line2.color == LineColor.Red)) {
  510. line1.parent.color = LineColor.Black;
  511. line2.color = LineColor.Black;
  512. line1.parent.parent.color = LineColor.Red;
  513. line1 = line1.parent.parent;
  514. } else {
  515. if (line1 == line1.parent.right) {
  516. line1 = line1.parent;
  517. RotateLeft(line1);
  518. }
  519. line1.parent.color = LineColor.Black;
  520. line1.parent.parent.color = LineColor.Red;
  521. RotateRight(line1.parent.parent);
  522. }
  523. } else {
  524. line2 = line1.parent.parent.left;
  525. if ((line2 != null) && (line2.color == LineColor.Red)) {
  526. line1.parent.color = LineColor.Black;
  527. line2.color = LineColor.Black;
  528. line1.parent.parent.color = LineColor.Red;
  529. line1 = line1.parent.parent;
  530. } else {
  531. if (line1 == line1.parent.left) {
  532. line1 = line1.parent;
  533. RotateRight(line1);
  534. }
  535. line1.parent.color = LineColor.Black;
  536. line1.parent.parent.color = LineColor.Red;
  537. RotateLeft(line1.parent.parent);
  538. }
  539. }
  540. }
  541. document.color = LineColor.Black;
  542. }
  543. private void RebalanceAfterDelete(Line line1) {
  544. Line line2;
  545. while ((line1 != document) && (line1.color == LineColor.Black)) {
  546. if (line1 == line1.parent.left) {
  547. line2 = line1.parent.right;
  548. if (line2.color == LineColor.Red) {
  549. line2.color = LineColor.Black;
  550. line1.parent.color = LineColor.Red;
  551. RotateLeft(line1.parent);
  552. line2 = line1.parent.right;
  553. }
  554. if ((line2.left.color == LineColor.Black) && (line2.right.color == LineColor.Black)) {
  555. line2.color = LineColor.Red;
  556. line1 = line1.parent;
  557. } else {
  558. if (line2.right.color == LineColor.Black) {
  559. line2.left.color = LineColor.Black;
  560. line2.color = LineColor.Red;
  561. RotateRight(line2);
  562. line2 = line1.parent.right;
  563. }
  564. line2.color = line1.parent.color;
  565. line1.parent.color = LineColor.Black;
  566. line2.right.color = LineColor.Black;
  567. RotateLeft(line1.parent);
  568. line1 = document;
  569. }
  570. } else {
  571. line2 = line1.parent.left;
  572. if (line2.color == LineColor.Red) {
  573. line2.color = LineColor.Black;
  574. line1.parent.color = LineColor.Red;
  575. RotateRight(line1.parent);
  576. line2 = line1.parent.left;
  577. }
  578. if ((line2.right.color == LineColor.Black) && (line2.left.color == LineColor.Black)) {
  579. line2.color = LineColor.Red;
  580. line1 = line1.parent;
  581. } else {
  582. if (line2.left.color == LineColor.Black) {
  583. line2.right.color = LineColor.Black;
  584. line2.color = LineColor.Red;
  585. RotateLeft(line2);
  586. line2 = line1.parent.left;
  587. }
  588. line2.color = line1.parent.color;
  589. line1.parent.color = LineColor.Black;
  590. line2.left.color = LineColor.Black;
  591. RotateRight(line1.parent);
  592. line1 = document;
  593. }
  594. }
  595. }
  596. line1.color = LineColor.Black;
  597. }
  598. private void RotateLeft(Line line1) {
  599. Line line2 = line1.right;
  600. line1.right = line2.left;
  601. if (line2.left != sentinel) {
  602. line2.left.parent = line1;
  603. }
  604. if (line2 != sentinel) {
  605. line2.parent = line1.parent;
  606. }
  607. if (line1.parent != null) {
  608. if (line1 == line1.parent.left) {
  609. line1.parent.left = line2;
  610. } else {
  611. line1.parent.right = line2;
  612. }
  613. } else {
  614. document = line2;
  615. }
  616. line2.left = line1;
  617. if (line1 != sentinel) {
  618. line1.parent = line2;
  619. }
  620. }
  621. private void RotateRight(Line line1) {
  622. Line line2 = line1.left;
  623. line1.left = line2.right;
  624. if (line2.right != sentinel) {
  625. line2.right.parent = line1;
  626. }
  627. if (line2 != sentinel) {
  628. line2.parent = line1.parent;
  629. }
  630. if (line1.parent != null) {
  631. if (line1 == line1.parent.right) {
  632. line1.parent.right = line2;
  633. } else {
  634. line1.parent.left = line2;
  635. }
  636. } else {
  637. document = line2;
  638. }
  639. line2.right = line1;
  640. if (line1 != sentinel) {
  641. line1.parent = line2;
  642. }
  643. }
  644. public void UpdateView(Line line, int pos) {
  645. int prev_width;
  646. // This is an optimization; we need to invalidate
  647. prev_width = (int)line.widths[line.text.Length];
  648. if (RecalculateDocument(owner.CreateGraphics(), line.line_no, line.line_no, true)) {
  649. // Lineheight changed, invalidate the rest of the document
  650. if ((line.Y - viewport_y) >=0 ) {
  651. // We formatted something that's in view, only draw parts of the screen
  652. owner.Invalidate(new Rectangle(0, line.Y - viewport_y, owner.Width, owner.Height - line.Y - viewport_y));
  653. } else {
  654. // The tag was above the visible area, draw everything
  655. owner.Invalidate();
  656. }
  657. } else {
  658. owner.Invalidate(new Rectangle((int)line.widths[pos] - viewport_x - 1, line.Y - viewport_y, (int)owner.Width, line.height));
  659. }
  660. }
  661. // Update display from line, down line_count lines; pos is unused, but required for the signature
  662. public void UpdateView(Line line, int line_count, int pos) {
  663. int prev_width;
  664. // This is an optimization; we need to invalidate
  665. prev_width = (int)line.widths[line.text.Length];
  666. if (RecalculateDocument(owner.CreateGraphics(), line.line_no, line.line_no + line_count - 1, true)) {
  667. // Lineheight changed, invalidate the rest of the document
  668. if ((line.Y - viewport_y) >=0 ) {
  669. // We formatted something that's in view, only draw parts of the screen
  670. owner.Invalidate(new Rectangle(0, line.Y - viewport_y, owner.Width, owner.Height - line.Y - viewport_y));
  671. } else {
  672. // The tag was above the visible area, draw everything
  673. owner.Invalidate();
  674. }
  675. } else {
  676. Line end_line;
  677. end_line = GetLine(line.line_no + line_count -1);
  678. if (end_line == null) {
  679. end_line = line;
  680. }
  681. owner.Invalidate(new Rectangle(0 - viewport_x, line.Y - viewport_y, (int)line.widths[line.text.Length], end_line.Y + end_line.height));
  682. }
  683. }
  684. #endregion // Private Methods
  685. #region Public Methods
  686. public void PositionCaret(Line line, int pos) {
  687. caret.tag = line.FindTag(pos);
  688. caret.line = line;
  689. caret.pos = pos;
  690. caret.height = caret.tag.height;
  691. XplatUI.DestroyCaret(owner.Handle);
  692. XplatUI.CreateCaret(owner.Handle, 2, caret.height);
  693. XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos], caret.tag.line.Y + caret.tag.shift);
  694. }
  695. public void PositionCaret(int x, int y) {
  696. caret.tag = FindCursor(x, y, out caret.pos);
  697. caret.line = caret.tag.line;
  698. caret.height = caret.tag.height;
  699. XplatUI.DestroyCaret(owner.Handle);
  700. XplatUI.CreateCaret(owner.Handle, 2, caret.height);
  701. XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos], caret.tag.line.Y + caret.tag.shift);
  702. }
  703. public void CaretHasFocus() {
  704. if (caret.tag != null) {
  705. XplatUI.CreateCaret(owner.Handle, 2, caret.height);
  706. XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos], caret.tag.line.Y + caret.tag.shift);
  707. XplatUI.CaretVisible(owner.Handle, true);
  708. }
  709. }
  710. public void CaretLostFocus() {
  711. XplatUI.DestroyCaret(owner.Handle);
  712. }
  713. public void AlignCaret() {
  714. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  715. caret.height = caret.tag.height;
  716. XplatUI.CreateCaret(owner.Handle, 2, caret.height);
  717. XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos], caret.tag.line.Y + caret.tag.shift);
  718. XplatUI.CaretVisible(owner.Handle, true);
  719. }
  720. public void UpdateCaret() {
  721. if (caret.tag.height != caret.height) {
  722. caret.height = caret.tag.height;
  723. XplatUI.CreateCaret(owner.Handle, 2, caret.height);
  724. }
  725. XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos], caret.tag.line.Y + caret.tag.shift);
  726. XplatUI.CaretVisible(owner.Handle, true);
  727. }
  728. public void DisplayCaret() {
  729. XplatUI.CaretVisible(owner.Handle, true);
  730. }
  731. public void HideCaret() {
  732. XplatUI.CaretVisible(owner.Handle, false);
  733. }
  734. public void MoveCaret(CaretDirection direction) {
  735. switch(direction) {
  736. case CaretDirection.CharForward: {
  737. caret.pos++;
  738. if (caret.pos > caret.line.text.Length) {
  739. if (multiline) {
  740. // Go into next line
  741. if (caret.line.line_no < this.lines) {
  742. caret.line = GetLine(caret.line.line_no+1);
  743. caret.pos = 0;
  744. caret.tag = caret.line.tags;
  745. } else {
  746. caret.pos--;
  747. }
  748. } else {
  749. // Single line; we stay where we are
  750. caret.pos--;
  751. }
  752. } else {
  753. if ((caret.tag.start - 1 + caret.tag.length) < caret.pos) {
  754. caret.tag = caret.tag.next;
  755. }
  756. }
  757. UpdateCaret();
  758. return;
  759. }
  760. case CaretDirection.CharBack: {
  761. if (caret.pos > 0) {
  762. // caret.pos--; // folded into the if below
  763. if (--caret.pos > 0) {
  764. if (caret.tag.start > caret.pos) {
  765. caret.tag = caret.tag.previous;
  766. }
  767. }
  768. } else {
  769. if (caret.line.line_no > 1) {
  770. caret.line = GetLine(caret.line.line_no - 1);
  771. caret.pos = caret.line.text.Length;
  772. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  773. }
  774. }
  775. UpdateCaret();
  776. return;
  777. }
  778. case CaretDirection.WordForward: {
  779. int len;
  780. len = caret.line.text.Length;
  781. if (caret.pos < len) {
  782. while ((caret.pos < len) && (caret.line.text.ToString(caret.pos, 1) != " ")) {
  783. caret.pos++;
  784. }
  785. if (caret.pos < len) {
  786. // Skip any whitespace
  787. while ((caret.pos < len) && (caret.line.text.ToString(caret.pos, 1) == " ")) {
  788. caret.pos++;
  789. }
  790. }
  791. } else {
  792. if (caret.line.line_no < this.lines) {
  793. caret.line = GetLine(caret.line.line_no+1);
  794. caret.pos = 0;
  795. caret.tag = caret.line.tags;
  796. }
  797. }
  798. UpdateCaret();
  799. return;
  800. }
  801. case CaretDirection.WordBack: {
  802. if (caret.pos > 0) {
  803. int len;
  804. len = caret.line.text.Length;
  805. caret.pos--;
  806. while ((caret.pos > 0) && (caret.line.text.ToString(caret.pos, 1) == " ")) {
  807. caret.pos--;
  808. }
  809. while ((caret.pos > 0) && (caret.line.text.ToString(caret.pos, 1) != " ")) {
  810. caret.pos--;
  811. }
  812. if (caret.line.text.ToString(caret.pos, 1) == " ") {
  813. if (caret.pos != 0) {
  814. caret.pos++;
  815. } else {
  816. caret.line = GetLine(caret.line.line_no - 1);
  817. caret.pos = caret.line.text.Length;
  818. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  819. }
  820. }
  821. } else {
  822. if (caret.line.line_no > 1) {
  823. caret.line = GetLine(caret.line.line_no - 1);
  824. caret.pos = caret.line.text.Length;
  825. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  826. }
  827. }
  828. UpdateCaret();
  829. return;
  830. }
  831. case CaretDirection.LineUp: {
  832. if (caret.line.line_no > 1) {
  833. int pixel;
  834. pixel = (int)caret.line.widths[caret.pos];
  835. PositionCaret(pixel, GetLine(caret.line.line_no - 1).Y);
  836. XplatUI.CaretVisible(owner.Handle, true);
  837. }
  838. return;
  839. }
  840. case CaretDirection.LineDown: {
  841. if (caret.line.line_no < lines) {
  842. int pixel;
  843. pixel = (int)caret.line.widths[caret.pos];
  844. PositionCaret(pixel, GetLine(caret.line.line_no + 1).Y);
  845. XplatUI.CaretVisible(owner.Handle, true);
  846. }
  847. return;
  848. }
  849. case CaretDirection.Home: {
  850. if (caret.pos > 0) {
  851. caret.pos = 0;
  852. caret.tag = caret.line.tags;
  853. UpdateCaret();
  854. }
  855. return;
  856. }
  857. case CaretDirection.End: {
  858. if (caret.pos < caret.line.text.Length) {
  859. caret.pos = caret.line.text.Length;
  860. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  861. UpdateCaret();
  862. }
  863. return;
  864. }
  865. case CaretDirection.PgUp: {
  866. return;
  867. }
  868. case CaretDirection.PgDn: {
  869. return;
  870. }
  871. case CaretDirection.CtrlHome: {
  872. caret.line = GetLine(1);
  873. caret.pos = 0;
  874. caret.tag = caret.line.tags;
  875. UpdateCaret();
  876. return;
  877. }
  878. case CaretDirection.CtrlEnd: {
  879. caret.line = GetLine(lines);
  880. caret.pos = 0;
  881. caret.tag = caret.line.tags;
  882. UpdateCaret();
  883. return;
  884. }
  885. }
  886. }
  887. // Draw the document
  888. public void Draw(Graphics g, Rectangle clip) {
  889. Line line; // Current line being drawn
  890. LineTag tag; // Current tag being drawn
  891. int start; // First line to draw
  892. int end; // Last line to draw
  893. string s; // String representing the current line
  894. int line_no; //
  895. // First, figure out from what line to what line we need to draw
  896. start = GetLineByPixel(clip.Top - viewport_y, false).line_no;
  897. end = GetLineByPixel(clip.Bottom - viewport_y, false).line_no;
  898. // Now draw our elements; try to only draw those that are visible
  899. line_no = start;
  900. #if Debug
  901. DateTime n = DateTime.Now;
  902. Console.WriteLine("Started drawing: {0}s {1}ms", n.Second, n.Millisecond);
  903. #endif
  904. while (line_no <= end) {
  905. line = GetLine(line_no);
  906. tag = line.tags;
  907. s = line.text.ToString();
  908. while (tag != null) {
  909. if (((tag.X + tag.width) > (clip.Left - viewport_x)) || (tag.X < (clip.Right - viewport_x))) {
  910. g.DrawString(s.Substring(tag.start-1, tag.length), tag.font, tag.color, tag.X - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
  911. }
  912. tag = tag.next;
  913. }
  914. line_no++;
  915. }
  916. #if Debug
  917. n = DateTime.Now;
  918. Console.WriteLine("Finished drawing: {0}s {1}ms", n.Second, n.Millisecond);
  919. #endif
  920. }
  921. // Inserts a character at the given position
  922. public void InsertChar(Line line, int pos, char ch) {
  923. InsertChar(line.FindTag(pos), pos, ch);
  924. }
  925. // Inserts a character at the given position
  926. public void InsertChar(LineTag tag, int pos, char ch) {
  927. Line line;
  928. line = tag.line;
  929. line.text.Insert(pos, ch);
  930. tag.length++;
  931. tag = tag.next;
  932. while (tag != null) {
  933. tag.start++;
  934. tag = tag.next;
  935. }
  936. line.Grow(1);
  937. line.recalc = true;
  938. UpdateView(line, pos);
  939. }
  940. // Inserts a character at the given position
  941. public void InsertCharAtCaret(char ch, bool move_caret) {
  942. LineTag tag;
  943. caret.line.text.Insert(caret.pos, ch);
  944. caret.tag.length++;
  945. if (caret.tag.next != null) {
  946. tag = caret.tag.next;
  947. while (tag != null) {
  948. tag.start++;
  949. tag = tag.next;
  950. }
  951. }
  952. caret.line.Grow(1);
  953. caret.line.recalc = true;
  954. UpdateView(caret.line, caret.pos);
  955. if (move_caret) {
  956. caret.pos++;
  957. UpdateCaret();
  958. }
  959. }
  960. // Inserts a character at the given position; it will not delete past line limits
  961. public void DeleteChar(LineTag tag, int pos, bool forward) {
  962. Line line;
  963. bool streamline;
  964. streamline = false;
  965. line = tag.line;
  966. if ((pos == 0 && forward == false) || (pos == line.text.Length && forward == true)) {
  967. return;
  968. }
  969. if (forward) {
  970. line.text.Remove(pos, 1);
  971. tag.length--;
  972. if (tag.length == 0) {
  973. streamline = true;
  974. }
  975. } else {
  976. pos--;
  977. line.text.Remove(pos, 1);
  978. if (pos >= (tag.start - 1)) {
  979. tag.length--;
  980. if (tag.length == 0) {
  981. streamline = true;
  982. }
  983. } else if (tag.previous != null) {
  984. tag.previous.length--;
  985. if (tag.previous.length == 0) {
  986. streamline = true;
  987. }
  988. }
  989. }
  990. tag = tag.next;
  991. while (tag != null) {
  992. tag.start--;
  993. tag = tag.next;
  994. }
  995. line.recalc = true;
  996. line.Streamline();
  997. UpdateView(line, pos);
  998. }
  999. // Combine two lines
  1000. public void Combine(int FirstLine, int SecondLine) {
  1001. Combine(GetLine(FirstLine), GetLine(SecondLine));
  1002. }
  1003. public void Combine(Line first, Line second) {
  1004. LineTag last;
  1005. int shift;
  1006. // Combine the two tag chains into one
  1007. last = first.tags;
  1008. while (last.next != null) {
  1009. last = last.next;
  1010. }
  1011. last.next = second.tags;
  1012. last.next.previous = last;
  1013. shift = last.start + last.length - 1;
  1014. // Fix up references within the chain
  1015. last = last.next;
  1016. while (last != null) {
  1017. last.line = first;
  1018. last.start += shift;
  1019. last = last.next;
  1020. }
  1021. // Combine both lines' strings
  1022. first.text.Insert(first.text.Length, second.text.ToString());
  1023. first.Grow(first.text.Length);
  1024. // Remove the reference to our (now combined) tags from the doomed line
  1025. second.tags = null;
  1026. // Renumber lines
  1027. DecrementLines(first.line_no + 2); // first.line_no + 1 will be deleted, so we need to start renumbering one later
  1028. // Mop up
  1029. first.recalc = true;
  1030. first.height = 0; // This forces RecalcDocument/UpdateView to redraw from this line on
  1031. first.Streamline();
  1032. #if Debug
  1033. Line check_first;
  1034. Line check_second;
  1035. check_first = GetLine(first.line_no);
  1036. check_second = GetLine(check_first.line_no + 1);
  1037. Console.WriteLine("Pre-delete: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
  1038. #endif
  1039. this.Delete(second);
  1040. #if Debug
  1041. check_first = GetLine(first.line_no);
  1042. check_second = GetLine(check_first.line_no + 1);
  1043. Console.WriteLine("Post-delete Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
  1044. #endif
  1045. }
  1046. // Split the line at the position into two
  1047. public void Split(int LineNo, int pos) {
  1048. Line line;
  1049. LineTag tag;
  1050. line = GetLine(LineNo);
  1051. tag = LineTag.FindTag(line, pos);
  1052. Split(line, tag, pos);
  1053. }
  1054. public void Split(Line line, int pos) {
  1055. LineTag tag;
  1056. tag = LineTag.FindTag(line, pos);
  1057. Split(line, tag, pos);
  1058. }
  1059. public void Split(Line line, LineTag tag, int pos) {
  1060. LineTag new_tag;
  1061. Line new_line;
  1062. // cover the easy case first
  1063. if (pos == line.text.Length) {
  1064. Add(line.line_no + 1, "", tag.font, tag.color);
  1065. return;
  1066. }
  1067. // We need to move the rest of the text into the new line
  1068. Add(line.line_no + 1, line.text.ToString(pos, line.text.Length - pos), tag.font, tag.color);
  1069. // Now transfer our tags from this line to the next
  1070. new_line = GetLine(line.line_no + 1);
  1071. line.recalc = true;
  1072. if ((tag.start - 1) == pos) {
  1073. int shift;
  1074. // We can simply break the chain and move the tag into the next line
  1075. if (tag == line.tags) {
  1076. new_tag = new LineTag(line, 1, 0);
  1077. new_tag.font = tag.font;
  1078. new_tag.color = tag.color;
  1079. line.tags = new_tag;
  1080. }
  1081. if (tag.previous != null) {
  1082. tag.previous.next = null;
  1083. }
  1084. new_line.tags = tag;
  1085. tag.previous = null;
  1086. tag.line = new_line;
  1087. // Walk the list and correct the start location of the tags we just bumped into the next line
  1088. shift = tag.start - 1;
  1089. new_tag = tag;
  1090. while (new_tag != null) {
  1091. new_tag.start -= shift;
  1092. new_tag.line = new_line;
  1093. new_tag = new_tag.next;
  1094. }
  1095. } else {
  1096. int shift;
  1097. new_tag = new LineTag(new_line, 1, tag.start - 1 + tag.length - pos);
  1098. new_tag.next = tag.next;
  1099. new_tag.font = tag.font;
  1100. new_tag.color = tag.color;
  1101. new_line.tags = new_tag;
  1102. if (new_tag.next != null) {
  1103. new_tag.next.previous = new_tag;
  1104. }
  1105. tag.next = null;
  1106. tag.length = pos - tag.start + 1;
  1107. shift = pos;
  1108. new_tag = new_tag.next;
  1109. while (new_tag != null) {
  1110. new_tag.start -= shift;
  1111. new_tag.line = new_line;
  1112. new_tag = new_tag.next;
  1113. }
  1114. }
  1115. line.text.Remove(pos, line.text.Length - pos);
  1116. }
  1117. // Adds a line of text, with given font.
  1118. // Bumps any line at that line number that already exists down
  1119. public void Add(int LineNo, string Text, Font font, Brush color) {
  1120. Line add;
  1121. Line line;
  1122. int line_no;
  1123. if (LineNo<1 || Text == null) {
  1124. if (LineNo<1) {
  1125. throw new ArgumentNullException("LineNo", "Line numbers must be positive");
  1126. } else {
  1127. throw new ArgumentNullException("Text", "Cannot insert NULL line");
  1128. }
  1129. }
  1130. add = new Line(LineNo, Text, font, color);
  1131. line = document;
  1132. while (line != sentinel) {
  1133. add.parent = line;
  1134. line_no = line.line_no;
  1135. if (LineNo > line_no) {
  1136. line = line.right;
  1137. } else if (LineNo < line_no) {
  1138. line = line.left;
  1139. } else {
  1140. // Bump existing line numbers; walk all nodes to the right of this one and increment line_no
  1141. IncrementLines(line.line_no);
  1142. line = line.left;
  1143. }
  1144. }
  1145. add.left = sentinel;
  1146. add.right = sentinel;
  1147. if (add.parent != null) {
  1148. if (LineNo > add.parent.line_no) {
  1149. add.parent.right = add;
  1150. } else {
  1151. add.parent.left = add;
  1152. }
  1153. } else {
  1154. // Root node
  1155. document = add;
  1156. }
  1157. RebalanceAfterAdd(add);
  1158. lines++;
  1159. }
  1160. public virtual void Clear() {
  1161. lines = 0;
  1162. document = sentinel;
  1163. }
  1164. public virtual object Clone() {
  1165. Document clone;
  1166. clone = new Document(null);
  1167. clone.lines = this.lines;
  1168. clone.document = (Line)document.Clone();
  1169. return clone;
  1170. }
  1171. public void Delete(int LineNo) {
  1172. if (LineNo>lines) {
  1173. return;
  1174. }
  1175. Delete(GetLine(LineNo));
  1176. }
  1177. public void Delete(Line line1) {
  1178. Line line2;// = new Line();
  1179. Line line3;
  1180. if ((line1.left == sentinel) || (line1.right == sentinel)) {
  1181. line3 = line1;
  1182. } else {
  1183. line3 = line1.right;
  1184. while (line3.left != sentinel) {
  1185. line3 = line3.left;
  1186. }
  1187. }
  1188. if (line3.left != sentinel) {
  1189. line2 = line3.left;
  1190. } else {
  1191. line2 = line3.right;
  1192. }
  1193. line2.parent = line3.parent;
  1194. if (line3.parent != null) {
  1195. if(line3 == line3.parent.left) {
  1196. line3.parent.left = line2;
  1197. } else {
  1198. line3.parent.right = line2;
  1199. }
  1200. } else {
  1201. document = line2;
  1202. }
  1203. if (line3 != line1) {
  1204. LineTag tag;
  1205. line1.ascent = line3.ascent;
  1206. line1.height = line3.height;
  1207. line1.line_no = line3.line_no;
  1208. line1.recalc = line3.recalc;
  1209. line1.space = line3.space;
  1210. line1.tags = line3.tags;
  1211. line1.text = line3.text;
  1212. line1.widths = line3.widths;
  1213. line1.Y = line3.Y;
  1214. tag = line1.tags;
  1215. while (tag != null) {
  1216. tag.line = line1;
  1217. tag = tag.next;
  1218. }
  1219. }
  1220. if (line3.color == LineColor.Black)
  1221. RebalanceAfterDelete(line2);
  1222. this.lines--;
  1223. last_found = sentinel;
  1224. }
  1225. // Set our selection markers
  1226. public void SetSelection(Line start, int start_pos, Line end, int end_pos) {
  1227. selection_start_line = start;
  1228. selection_start_pos = start_pos;
  1229. selection_end_line = end;
  1230. selection_end_pos = end_pos;
  1231. }
  1232. public void SetSelection(Line start, int start_pos) {
  1233. selection_start_line = start;
  1234. selection_start_pos = start_pos;
  1235. selection_end_line = start;
  1236. selection_end_pos = start_pos;
  1237. }
  1238. // Give it a Line number and it returns the Line object at with that line number
  1239. public Line GetLine(int LineNo) {
  1240. Line line = document;
  1241. while (line != sentinel) {
  1242. if (LineNo == line.line_no) {
  1243. return line;
  1244. } else if (LineNo < line.line_no) {
  1245. line = line.left;
  1246. } else {
  1247. line = line.right;
  1248. }
  1249. }
  1250. return null;
  1251. }
  1252. // Give it a Y pixel coordinate and it returns the Line covering that Y coordinate
  1253. ///
  1254. public Line GetLineByPixel(int y, bool exact) {
  1255. Line line = document;
  1256. Line last = null;
  1257. while (line != sentinel) {
  1258. last = line;
  1259. if ((y >= line.Y) && (y < (line.Y+line.height))) {
  1260. return line;
  1261. } else if (y < line.Y) {
  1262. line = line.left;
  1263. } else {
  1264. line = line.right;
  1265. }
  1266. }
  1267. if (exact) {
  1268. return null;
  1269. }
  1270. return last;
  1271. }
  1272. // Give it x/y pixel coordinates and it returns the Tag at that position; optionally the char position is returned in index
  1273. public LineTag FindTag(int x, int y, out int index, bool exact) {
  1274. Line line;
  1275. LineTag tag;
  1276. line = GetLineByPixel(y, exact);
  1277. if (line == null) {
  1278. index = 0;
  1279. return null;
  1280. }
  1281. tag = line.tags;
  1282. while (true) {
  1283. if (x >= tag.X && x < (tag.X+tag.width)) {
  1284. int end;
  1285. end = tag.start + tag.length - 1;
  1286. for (int pos = tag.start; pos < end; pos++) {
  1287. if (x < line.widths[pos]) {
  1288. index = pos;
  1289. return tag;
  1290. }
  1291. }
  1292. index=end;
  1293. return tag;
  1294. }
  1295. if (tag.next != null) {
  1296. tag = tag.next;
  1297. } else {
  1298. if (exact) {
  1299. index = 0;
  1300. return null;
  1301. }
  1302. index = line.text.Length;
  1303. return tag;
  1304. }
  1305. }
  1306. }
  1307. // Give it x/y pixel coordinates and it returns the Tag at that position; optionally the char position is returned in index
  1308. public LineTag FindCursor(int x, int y, out int index) {
  1309. Line line;
  1310. LineTag tag;
  1311. line = GetLineByPixel(y, false);
  1312. tag = line.tags;
  1313. while (true) {
  1314. if (x >= tag.X && x < (tag.X+tag.width)) {
  1315. int end;
  1316. end = tag.start + tag.length - 1;
  1317. for (int pos = tag.start-1; pos < end; pos++) {
  1318. // When clicking on a character, we position the cursor to whatever edge
  1319. // of the character the click was closer
  1320. if (x < (line.widths[pos] + ((line.widths[pos+1]-line.widths[pos])/2))) {
  1321. index = pos;
  1322. return tag;
  1323. }
  1324. }
  1325. index=end;
  1326. return tag;
  1327. }
  1328. if (tag.next != null) {
  1329. tag = tag.next;
  1330. } else {
  1331. index = line.text.Length;
  1332. return tag;
  1333. }
  1334. }
  1335. }
  1336. // Calculate formatting for the whole document
  1337. public bool RecalculateDocument(Graphics g) {
  1338. return RecalculateDocument(g, 1, this.lines, false);
  1339. }
  1340. // Calculate formatting starting at a certain line
  1341. public bool RecalculateDocument(Graphics g, int start) {
  1342. return RecalculateDocument(g, start, this.lines, false);
  1343. }
  1344. // Calculate formatting within two given line numbers
  1345. public bool RecalculateDocument(Graphics g, int start, int end) {
  1346. return RecalculateDocument(g, start, end, false);
  1347. }
  1348. // With optimize on, returns true if line heights changed
  1349. public bool RecalculateDocument(Graphics g, int start, int end, bool optimize) {
  1350. Line line;
  1351. int line_no;
  1352. int Y;
  1353. Y = GetLine(start).Y;
  1354. line_no = start;
  1355. if (optimize) {
  1356. bool changed;
  1357. changed = false;
  1358. while (line_no <= end) {
  1359. line = GetLine(line_no++);
  1360. line.Y = Y;
  1361. if (line.recalc) {
  1362. if (line.RecalculateLine(g)) {
  1363. changed = true;
  1364. // If the height changed, all subsequent lines change
  1365. end = this.lines;
  1366. }
  1367. }
  1368. Y += line.height;
  1369. }
  1370. return changed;
  1371. } else {
  1372. while (line_no <= end) {
  1373. line = GetLine(line_no++);
  1374. line.Y = Y;
  1375. line.RecalculateLine(g);
  1376. Y += line.height;
  1377. }
  1378. return true;
  1379. }
  1380. }
  1381. public bool SetCursor(int x, int y) {
  1382. return true;
  1383. }
  1384. public int Size() {
  1385. return lines;
  1386. }
  1387. #endregion // Public Methods
  1388. #region Administrative
  1389. public IEnumerator GetEnumerator() {
  1390. // FIXME
  1391. return null;
  1392. }
  1393. public override bool Equals(object obj) {
  1394. if (obj == null) {
  1395. return false;
  1396. }
  1397. if (!(obj is Document)) {
  1398. return false;
  1399. }
  1400. if (obj == this) {
  1401. return true;
  1402. }
  1403. if (ToString().Equals(((Document)obj).ToString())) {
  1404. return true;
  1405. }
  1406. return false;
  1407. }
  1408. public override int GetHashCode() {
  1409. return document_id;
  1410. }
  1411. public override string ToString() {
  1412. return "document " + this.document_id;
  1413. }
  1414. #endregion // Administrative
  1415. }
  1416. public class LineTag {
  1417. #region Local Variables;
  1418. // Payload; formatting
  1419. internal Font font; // System.Drawing.Font object for this tag
  1420. internal Brush color; // System.Drawing.Brush object
  1421. // Payload; text
  1422. internal int start; // start, in chars; index into Line.text
  1423. internal int length; // length, in chars
  1424. internal bool r_to_l; // Which way is the font
  1425. // Drawing support
  1426. internal int height; // Height in pixels of the text this tag describes
  1427. internal int X; // X location of the text this tag describes
  1428. internal float width; // Width in pixels of the text this tag describes
  1429. internal int ascent; // Ascent of the font for this tag
  1430. internal int shift; // Shift down for this tag, to stay on baseline
  1431. // Administrative
  1432. internal Line line; // The line we're on
  1433. internal LineTag next; // Next tag on the same line
  1434. internal LineTag previous; // Previous tag on the same line
  1435. #endregion;
  1436. #region Constructors
  1437. public LineTag(Line line, int start, int length) {
  1438. this.line = line;
  1439. this.start = start;
  1440. this.length = length;
  1441. this.X = 0;
  1442. this.width = 0;
  1443. }
  1444. #endregion // Constructors
  1445. #region Public Methods
  1446. //
  1447. // Applies 'font' to characters starting at 'start' for 'length' chars
  1448. // Removes any previous tags overlapping the same area
  1449. // returns true if lineheight has changed
  1450. //
  1451. public static bool FormatText(Line line, int start, int length, Font font, Brush color) {
  1452. LineTag tag;
  1453. LineTag start_tag;
  1454. LineTag end_tag;
  1455. int end;
  1456. int state;
  1457. int left;
  1458. bool retval = false; // Assume line-height doesn't change
  1459. // Too simple?
  1460. if (font.Height != line.height) {
  1461. retval = true;
  1462. }
  1463. line.recalc = true; // This forces recalculation of the line in RecalculateDocument
  1464. // A little sanity, not sure if it's needed, might be able to remove for speed
  1465. if (length > line.text.Length) {
  1466. length = line.text.Length;
  1467. }
  1468. tag = line.tags;
  1469. end = start + length;
  1470. state = 0;
  1471. // Common special case
  1472. if ((start == 1) && (length == tag.length)) {
  1473. tag.ascent = 0;
  1474. tag.font = font;
  1475. tag.color = color;
  1476. return retval;
  1477. }
  1478. start_tag = FindTag(line, start);
  1479. end_tag = FindTag(line, end);
  1480. tag = new LineTag(line, start, length);
  1481. tag.font = font;
  1482. tag.color = color;
  1483. if (start == 1) {
  1484. line.tags = tag;
  1485. }
  1486. if (start_tag.start == start) {
  1487. tag.next = start_tag;
  1488. tag.previous = start_tag.previous;
  1489. if (start_tag.previous != null) {
  1490. start_tag.previous.next = tag;
  1491. }
  1492. start_tag.previous = tag;
  1493. } else {
  1494. // Insert ourselves 'in the middle'
  1495. if ((start_tag.next != null) && (start_tag.next.start < end)) {
  1496. tag.next = start_tag.next;
  1497. } else {
  1498. tag.next = new LineTag(line, start_tag.start, start_tag.length);
  1499. tag.next.font = start_tag.font;
  1500. tag.next.color = start_tag.color;
  1501. if (start_tag.next != null) {
  1502. tag.next.next = start_tag.next;
  1503. tag.next.next.previous = tag.next;
  1504. }
  1505. }
  1506. tag.next.previous = tag;
  1507. start_tag.length = start - start_tag.start;
  1508. tag.previous = start_tag;
  1509. start_tag.next = tag;
  1510. #if crap
  1511. if (tag.next.start > (tag.start + tag.length)) {
  1512. tag.next.length += tag.next.start - (tag.start + tag.length);
  1513. tag.next.start = tag.start + tag.length;
  1514. }
  1515. #endif
  1516. }
  1517. // Elimination loop
  1518. tag = tag.next;
  1519. while ((tag != null) && (tag.start < end)) {
  1520. if ((tag.start + tag.length) <= end) {
  1521. // remove the tag
  1522. tag.previous.next = tag.next;
  1523. if (tag.next != null) {
  1524. tag.next.previous = tag.previous;
  1525. }
  1526. tag = tag.previous;
  1527. } else {
  1528. // Adjust the length of the tag
  1529. tag.length = (tag.start + tag.length) - end;
  1530. tag.start = end;
  1531. }
  1532. tag = tag.next;
  1533. }
  1534. return retval;
  1535. }
  1536. //
  1537. // Finds the tag that describes the character at position 'pos' on 'line'
  1538. //
  1539. public static LineTag FindTag(Line line, int pos) {
  1540. LineTag tag = line.tags;
  1541. // Beginning of line is a bit special
  1542. if (pos == 0) {
  1543. return tag;
  1544. }
  1545. while (tag != null) {
  1546. if ((tag.start <= pos) && (pos < (tag.start+tag.length))) {
  1547. return tag;
  1548. }
  1549. tag = tag.next;
  1550. }
  1551. return null;
  1552. }
  1553. //
  1554. // Combines 'this' tag with 'other' tag.
  1555. //
  1556. public bool Combine(LineTag other) {
  1557. if (!this.Equals(other)) {
  1558. return false;
  1559. }
  1560. this.width += other.width;
  1561. this.length += other.length;
  1562. this.next = other.next;
  1563. if (this.next != null) {
  1564. this.next.previous = this;
  1565. }
  1566. return true;
  1567. }
  1568. //
  1569. // Remove 'this' tag ; to be called when formatting is to be removed
  1570. //
  1571. public bool Remove() {
  1572. if ((this.start == 1) && (this.next == null)) {
  1573. // We cannot remove the only tag
  1574. return false;
  1575. }
  1576. if (this.start != 1) {
  1577. this.previous.length += this.length;
  1578. this.previous.width = -1;
  1579. this.previous.next = this.next;
  1580. this.next.previous = this.previous;
  1581. } else {
  1582. this.next.start = 1;
  1583. this.next.length += this.length;
  1584. this.next.width = -1;
  1585. this.line.tags = this.next;
  1586. this.next.previous = null;
  1587. }
  1588. return true;
  1589. }
  1590. //
  1591. // Checks if 'this' tag describes the same formatting options as 'obj'
  1592. //
  1593. public override bool Equals(object obj) {
  1594. LineTag other;
  1595. if (obj == null) {
  1596. return false;
  1597. }
  1598. if (!(obj is LineTag)) {
  1599. return false;
  1600. }
  1601. if (obj == this) {
  1602. return true;
  1603. }
  1604. other = (LineTag)obj;
  1605. if (this.font.Equals(other.font) && this.color.Equals(other.color)) { // FIXME add checking for things like link or type later
  1606. return true;
  1607. }
  1608. return false;
  1609. }
  1610. public override string ToString() {
  1611. return "Tag starts at index " + this.start + "length " + this.length + " text: " + this.line.Text.Substring(this.start-1, this.length) + "Font " + this.font.ToString();
  1612. }
  1613. #endregion // Public Methods
  1614. }
  1615. }