TextControl.cs 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951
  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-2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. //
  26. // NOT COMPLETE
  27. // There's still plenty of things missing, I've got most of it planned, just hadn't had
  28. // the time to write it all yet.
  29. // Stuff missing (in no particular order):
  30. // - Align text after RecalculateLine
  31. // - Implement tag types to support wrapping (ie have a 'newline' tag), for images, etc.
  32. // - Implement CaretPgUp/PgDown
  33. // - Finish selection calculations (invalidate only changed, more ways to select)
  34. // - Implement C&P
  35. #undef Debug
  36. using System;
  37. using System.Collections;
  38. using System.Drawing;
  39. using System.Drawing.Text;
  40. using System.Text;
  41. namespace System.Windows.Forms {
  42. internal enum LineColor {
  43. Red = 0,
  44. Black = 1
  45. }
  46. internal enum CaretDirection {
  47. CharForward, // Move a char to the right
  48. CharBack, // Move a char to the left
  49. LineUp, // Move a line up
  50. LineDown, // Move a line down
  51. Home, // Move to the beginning of the line
  52. End, // Move to the end of the line
  53. PgUp, // Move one page up
  54. PgDn, // Move one page down
  55. CtrlHome, // Move to the beginning of the document
  56. CtrlEnd, // Move to the end of the document
  57. WordBack, // Move to the beginning of the previous word (or beginning of line)
  58. WordForward // Move to the beginning of the next word (or end of line)
  59. }
  60. // Being cloneable should allow for nice line and document copies...
  61. internal class Line : ICloneable, IComparable {
  62. #region Local Variables
  63. // Stuff that matters for our line
  64. internal StringBuilder text; // Characters for the line
  65. internal float[] widths; // Width of each character; always one larger than text.Length
  66. internal int space; // Number of elements in text and widths
  67. internal int line_no; // Line number
  68. internal LineTag tags; // Tags describing the text
  69. internal int Y; // Baseline
  70. internal int height; // Height of the line (height of tallest tag)
  71. internal int ascent; // Ascent of the line (ascent of the tallest tag)
  72. internal HorizontalAlignment alignment; // Alignment of the line
  73. internal int align_shift; // Pixel shift caused by the alignment
  74. internal bool soft_break; // Tag is 'broken soft' and continuation from previous line
  75. // Stuff that's important for the tree
  76. internal Line parent; // Our parent line
  77. internal Line left; // Line with smaller line number
  78. internal Line right; // Line with higher line number
  79. internal LineColor color; // We're doing a black/red tree. this is the node color
  80. internal int DEFAULT_TEXT_LEN; //
  81. internal static StringFormat string_format; // For calculating widths/heights
  82. internal bool recalc; // Line changed
  83. #endregion // Local Variables
  84. #region Constructors
  85. internal Line() {
  86. color = LineColor.Red;
  87. left = null;
  88. right = null;
  89. parent = null;
  90. text = null;
  91. recalc = true;
  92. soft_break = false;
  93. alignment = HorizontalAlignment.Left;
  94. if (string_format == null) {
  95. string_format = new StringFormat(StringFormat.GenericTypographic);
  96. string_format.Trimming = StringTrimming.None;
  97. string_format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
  98. }
  99. }
  100. internal Line(int LineNo, string Text, Font font, Brush color) : this() {
  101. space = Text.Length > DEFAULT_TEXT_LEN ? Text.Length+1 : DEFAULT_TEXT_LEN;
  102. text = new StringBuilder(Text, space);
  103. line_no = LineNo;
  104. widths = new float[space + 1];
  105. tags = new LineTag(this, 1, text.Length);
  106. tags.font = font;
  107. tags.color = color;
  108. }
  109. internal Line(int LineNo, string Text, HorizontalAlignment align, Font font, Brush color) : this() {
  110. space = Text.Length > DEFAULT_TEXT_LEN ? Text.Length+1 : DEFAULT_TEXT_LEN;
  111. text = new StringBuilder(Text, space);
  112. line_no = LineNo;
  113. alignment = align;
  114. widths = new float[space + 1];
  115. tags = new LineTag(this, 1, text.Length);
  116. tags.font = font;
  117. tags.color = color;
  118. }
  119. internal Line(int LineNo, string Text, LineTag tag) : this() {
  120. space = Text.Length > DEFAULT_TEXT_LEN ? Text.Length+1 : DEFAULT_TEXT_LEN;
  121. text = new StringBuilder(Text, space);
  122. line_no = LineNo;
  123. widths = new float[space + 1];
  124. tags = tag;
  125. }
  126. #endregion // Constructors
  127. #region Internal Properties
  128. internal int Height {
  129. get {
  130. return height;
  131. }
  132. set {
  133. height = value;
  134. }
  135. }
  136. internal int LineNo {
  137. get {
  138. return line_no;
  139. }
  140. set {
  141. line_no = value;
  142. }
  143. }
  144. internal string Text {
  145. get {
  146. return text.ToString();
  147. }
  148. set {
  149. text = new StringBuilder(value, value.Length > DEFAULT_TEXT_LEN ? value.Length : DEFAULT_TEXT_LEN);
  150. }
  151. }
  152. internal HorizontalAlignment Alignment {
  153. get {
  154. return alignment;
  155. }
  156. set {
  157. if (alignment != value) {
  158. alignment = value;
  159. recalc = true;
  160. }
  161. }
  162. }
  163. #if no
  164. internal StringBuilder Text {
  165. get {
  166. return text;
  167. }
  168. set {
  169. text = value;
  170. }
  171. }
  172. #endif
  173. #endregion // Internal Properties
  174. #region Internal Methods
  175. // Make sure we always have enoughs space in text and widths
  176. internal void Grow(int minimum) {
  177. int length;
  178. float[] new_widths;
  179. length = text.Length;
  180. if ((length + minimum) > space) {
  181. // We need to grow; double the size
  182. if ((length + minimum) > (space * 2)) {
  183. new_widths = new float[length + minimum * 2 + 1];
  184. space = length + minimum * 2;
  185. } else {
  186. new_widths = new float[space * 2 + 1];
  187. space *= 2;
  188. }
  189. widths.CopyTo(new_widths, 0);
  190. widths = new_widths;
  191. }
  192. }
  193. internal void Streamline() {
  194. LineTag current;
  195. LineTag next;
  196. current = this.tags;
  197. next = current.next;
  198. // Catch what the loop below wont; eliminate 0 length
  199. // tags, but only if there are other tags after us
  200. while ((current.length == 0) && (next != null)) {
  201. tags = next;
  202. current = next;
  203. next = current.next;
  204. }
  205. if (next == null) {
  206. return;
  207. }
  208. while (next != null) {
  209. // Take out 0 length tags
  210. if (next.length == 0) {
  211. current.next = next.next;
  212. if (current.next != null) {
  213. current.next.previous = current;
  214. }
  215. next = current.next;
  216. continue;
  217. }
  218. if (current.Combine(next)) {
  219. next = current.next;
  220. continue;
  221. }
  222. current = current.next;
  223. next = current.next;
  224. }
  225. }
  226. /// <summary> Find the tag on a line based on the character position</summary>
  227. internal LineTag FindTag(int pos) {
  228. LineTag tag;
  229. if (pos == 0) {
  230. return tags;
  231. }
  232. tag = this.tags;
  233. if (pos > text.Length) {
  234. pos = text.Length;
  235. }
  236. while (tag != null) {
  237. if ((tag.start <= pos) && (pos < (tag.start + tag.length))) {
  238. return tag;
  239. }
  240. tag = tag.next;
  241. }
  242. return null;
  243. }
  244. /// <summary>
  245. /// Go through all tags on a line and recalculate all size-related values;
  246. /// returns true if lineheight changed
  247. /// </summary>
  248. internal bool RecalculateLine(Graphics g, Document doc) {
  249. LineTag tag;
  250. int pos;
  251. int len;
  252. SizeF size;
  253. float w;
  254. int prev_height;
  255. bool retval;
  256. bool wrapped;
  257. Line line;
  258. int wrap_pos;
  259. float wrap_width;
  260. pos = 0;
  261. len = this.text.Length;
  262. tag = this.tags;
  263. prev_height = this.height; // For drawing optimization calculations
  264. this.height = 0; // Reset line height
  265. this.ascent = 0; // Reset the ascent for the line
  266. tag.shift = 0;
  267. tag.width = 0;
  268. widths[0] = 0;
  269. this.recalc = false;
  270. retval = false;
  271. wrapped = false;
  272. wrap_pos = 0;
  273. wrap_width = 0;
  274. while (pos < len) {
  275. size = g.MeasureString(this.text.ToString(pos, 1), tag.font, 10000, string_format);
  276. w = size.Width;
  277. if (Char.IsWhiteSpace(text[pos])) {
  278. wrap_pos = pos + 1;
  279. wrap_width = tag.width + w;
  280. }
  281. if (doc.wrap) {
  282. if ((widths[pos] + w) + 27 > doc.viewport_width) {
  283. pos = wrap_pos;
  284. tag.width = wrap_width;
  285. doc.Split(this, tag, pos, true);
  286. len = this.text.Length;
  287. retval = true;
  288. wrapped = true;
  289. }
  290. }
  291. // Contract all soft lines that follow back into our line
  292. if (!wrapped) {
  293. tag.width += w;
  294. pos++;
  295. widths[pos] = widths[pos-1] + w;
  296. if (pos == len) {
  297. line = doc.GetLine(this.line_no + 1);
  298. if ((line != null) && (line.soft_break)) {
  299. // Pull the previous line back into this one
  300. doc.Combine(this.line_no, this.line_no + 1);
  301. len = this.text.Length;
  302. retval = true;
  303. }
  304. }
  305. }
  306. if (pos == (tag.start-1 + tag.length)) {
  307. // We just found the end of our current tag
  308. tag.height = (int)tag.font.Height;
  309. // Check if we're the tallest on the line (so far)
  310. if (tag.height > this.height) {
  311. this.height = tag.height; // Yep; make sure the line knows
  312. }
  313. if (tag.ascent == 0) {
  314. int descent;
  315. XplatUI.GetFontMetrics(g, tag.font, out tag.ascent, out descent);
  316. }
  317. if (tag.ascent > this.ascent) {
  318. LineTag t;
  319. // We have a tag that has a taller ascent than the line;
  320. t = tags;
  321. while (t != tag) {
  322. t.shift = tag.ascent - t.ascent;
  323. t = t.next;
  324. }
  325. // Save on our line
  326. this.ascent = tag.ascent;
  327. } else {
  328. tag.shift = this.ascent - tag.ascent;
  329. }
  330. // Update our horizontal starting pixel position
  331. if (tag.previous == null) {
  332. tag.X = 0;
  333. } else {
  334. tag.X = tag.previous.X + (int)tag.previous.width;
  335. }
  336. tag = tag.next;
  337. if (tag != null) {
  338. tag.width = 0;
  339. tag.shift = 0;
  340. wrap_pos = pos;
  341. wrap_width = tag.width;
  342. }
  343. }
  344. }
  345. if (this.height == 0) {
  346. this.height = tags.font.Height;
  347. tag.height = this.height;
  348. }
  349. if (prev_height != this.height) {
  350. retval = true;
  351. }
  352. return retval;
  353. }
  354. #endregion // Internal Methods
  355. #region Administrative
  356. public int CompareTo(object obj) {
  357. if (obj == null) {
  358. return 1;
  359. }
  360. if (! (obj is Line)) {
  361. throw new ArgumentException("Object is not of type Line", "obj");
  362. }
  363. if (line_no < ((Line)obj).line_no) {
  364. return -1;
  365. } else if (line_no > ((Line)obj).line_no) {
  366. return 1;
  367. } else {
  368. return 0;
  369. }
  370. }
  371. public object Clone() {
  372. Line clone;
  373. clone = new Line();
  374. clone.text = text;
  375. if (left != null) {
  376. clone.left = (Line)left.Clone();
  377. }
  378. if (left != null) {
  379. clone.left = (Line)left.Clone();
  380. }
  381. return clone;
  382. }
  383. internal object CloneLine() {
  384. Line clone;
  385. clone = new Line();
  386. clone.text = text;
  387. return clone;
  388. }
  389. public override bool Equals(object obj) {
  390. if (obj == null) {
  391. return false;
  392. }
  393. if (!(obj is Line)) {
  394. return false;
  395. }
  396. if (obj == this) {
  397. return true;
  398. }
  399. if (line_no == ((Line)obj).line_no) {
  400. return true;
  401. }
  402. return false;
  403. }
  404. public override int GetHashCode() {
  405. return base.GetHashCode ();
  406. }
  407. public override string ToString() {
  408. return "Line " + line_no;
  409. }
  410. #endregion // Administrative
  411. }
  412. internal class Document : ICloneable, IEnumerable {
  413. #region Structures
  414. internal struct Marker {
  415. internal Line line;
  416. internal LineTag tag;
  417. internal int pos;
  418. internal int height;
  419. }
  420. #endregion Structures
  421. #region Local Variables
  422. private Line document;
  423. private int lines;
  424. private Line sentinel;
  425. private Line last_found;
  426. private int document_id;
  427. private Random random = new Random();
  428. internal bool multiline;
  429. internal bool wrap;
  430. internal Marker caret;
  431. internal Marker selection_start;
  432. internal Marker selection_end;
  433. internal bool selection_visible;
  434. internal int viewport_x;
  435. internal int viewport_y; // The visible area of the document
  436. internal int viewport_width;
  437. internal int viewport_height;
  438. internal int document_x; // Width of the document
  439. internal int document_y; // Height of the document
  440. internal Control owner; // Who's owning us?
  441. #endregion // Local Variables
  442. #region Constructors
  443. internal Document(Control owner) {
  444. lines = 0;
  445. this.owner = owner;
  446. multiline = true;
  447. // Tree related stuff
  448. sentinel = new Line();
  449. sentinel.color = LineColor.Black;
  450. document = sentinel;
  451. last_found = sentinel;
  452. // We always have a blank line
  453. Add(1, "", owner.Font, new SolidBrush(owner.ForeColor));
  454. this.RecalculateDocument(owner.CreateGraphics());
  455. PositionCaret(0, 0);
  456. lines=1;
  457. selection_visible = false;
  458. selection_start.line = this.document;
  459. selection_start.pos = 0;
  460. selection_end.line = this.document;
  461. selection_end.pos = 0;
  462. viewport_x = 0;
  463. viewport_y = -2;
  464. // Default selection is empty
  465. document_id = random.Next();
  466. }
  467. #endregion
  468. #region Internal Properties
  469. internal Line Root {
  470. get {
  471. return document;
  472. }
  473. set {
  474. document = value;
  475. }
  476. }
  477. internal int Lines {
  478. get {
  479. return lines;
  480. }
  481. }
  482. internal Line CaretLine {
  483. get {
  484. return caret.line;
  485. }
  486. }
  487. internal int CaretPosition {
  488. get {
  489. return caret.pos;
  490. }
  491. }
  492. internal Point Caret {
  493. get {
  494. return new Point((int)caret.tag.line.widths[caret.pos] + caret.line.align_shift, caret.line.Y);
  495. }
  496. }
  497. internal LineTag CaretTag {
  498. get {
  499. return caret.tag;
  500. }
  501. }
  502. internal int ViewPortX {
  503. get {
  504. return viewport_x;
  505. }
  506. set {
  507. viewport_x = value;
  508. }
  509. }
  510. internal int ViewPortY {
  511. get {
  512. return viewport_y;
  513. }
  514. set {
  515. viewport_y = value;
  516. }
  517. }
  518. internal int ViewPortWidth {
  519. get {
  520. return viewport_width;
  521. }
  522. set {
  523. viewport_width = value;
  524. }
  525. }
  526. internal int ViewPortHeight {
  527. get {
  528. return viewport_height;
  529. }
  530. set {
  531. viewport_height = value;
  532. }
  533. }
  534. internal int Width {
  535. get {
  536. return this.document_x;
  537. }
  538. }
  539. internal int Height {
  540. get {
  541. return this.document_y;
  542. }
  543. }
  544. internal bool Wrap {
  545. get {
  546. return wrap;
  547. }
  548. set {
  549. wrap = value;
  550. // FIXME - force recalc/redisplay
  551. }
  552. }
  553. #endregion // Internal Properties
  554. #region Private Methods
  555. // For debugging
  556. internal void DumpTree(Line line, bool with_tags) {
  557. Console.Write("Line {0}, Y: {1} Text {2}", line.line_no, line.Y, line.text != null ? line.text.ToString() : "undefined");
  558. if (line.left == sentinel) {
  559. Console.Write(", left = sentinel");
  560. } else if (line.left == null) {
  561. Console.Write(", left = NULL");
  562. }
  563. if (line.right == sentinel) {
  564. Console.Write(", right = sentinel");
  565. } else if (line.right == null) {
  566. Console.Write(", right = NULL");
  567. }
  568. Console.WriteLine("");
  569. if (with_tags) {
  570. LineTag tag;
  571. int count;
  572. tag = line.tags;
  573. count = 1;
  574. Console.Write(" Tags: ");
  575. while (tag != null) {
  576. Console.Write("{0} <{1}>-<{2}> ", count++, tag.start, tag.length);
  577. if (tag.line != line) {
  578. Console.Write("BAD line link");
  579. throw new Exception("Bad line link in tree");
  580. }
  581. tag = tag.next;
  582. if (tag != null) {
  583. Console.Write(", ");
  584. }
  585. }
  586. Console.WriteLine("");
  587. }
  588. if (line.left != null) {
  589. if (line.left != sentinel) {
  590. DumpTree(line.left, with_tags);
  591. }
  592. } else {
  593. if (line != sentinel) {
  594. throw new Exception("Left should not be NULL");
  595. }
  596. }
  597. if (line.right != null) {
  598. if (line.right != sentinel) {
  599. DumpTree(line.right, with_tags);
  600. }
  601. } else {
  602. if (line != sentinel) {
  603. throw new Exception("Right should not be NULL");
  604. }
  605. }
  606. }
  607. private void DecrementLines(int line_no) {
  608. int current;
  609. current = line_no;
  610. while (current <= lines) {
  611. GetLine(current).line_no--;
  612. current++;
  613. }
  614. return;
  615. }
  616. private void IncrementLines(int line_no) {
  617. int current;
  618. current = this.lines;
  619. while (current >= line_no) {
  620. GetLine(current).line_no++;
  621. current--;
  622. }
  623. return;
  624. }
  625. private void RebalanceAfterAdd(Line line1) {
  626. Line line2;
  627. while ((line1 != document) && (line1.parent.color == LineColor.Red)) {
  628. if (line1.parent == line1.parent.parent.left) {
  629. line2 = line1.parent.parent.right;
  630. if ((line2 != null) && (line2.color == LineColor.Red)) {
  631. line1.parent.color = LineColor.Black;
  632. line2.color = LineColor.Black;
  633. line1.parent.parent.color = LineColor.Red;
  634. line1 = line1.parent.parent;
  635. } else {
  636. if (line1 == line1.parent.right) {
  637. line1 = line1.parent;
  638. RotateLeft(line1);
  639. }
  640. line1.parent.color = LineColor.Black;
  641. line1.parent.parent.color = LineColor.Red;
  642. RotateRight(line1.parent.parent);
  643. }
  644. } else {
  645. line2 = line1.parent.parent.left;
  646. if ((line2 != null) && (line2.color == LineColor.Red)) {
  647. line1.parent.color = LineColor.Black;
  648. line2.color = LineColor.Black;
  649. line1.parent.parent.color = LineColor.Red;
  650. line1 = line1.parent.parent;
  651. } else {
  652. if (line1 == line1.parent.left) {
  653. line1 = line1.parent;
  654. RotateRight(line1);
  655. }
  656. line1.parent.color = LineColor.Black;
  657. line1.parent.parent.color = LineColor.Red;
  658. RotateLeft(line1.parent.parent);
  659. }
  660. }
  661. }
  662. document.color = LineColor.Black;
  663. }
  664. private void RebalanceAfterDelete(Line line1) {
  665. Line line2;
  666. while ((line1 != document) && (line1.color == LineColor.Black)) {
  667. if (line1 == line1.parent.left) {
  668. line2 = line1.parent.right;
  669. if (line2.color == LineColor.Red) {
  670. line2.color = LineColor.Black;
  671. line1.parent.color = LineColor.Red;
  672. RotateLeft(line1.parent);
  673. line2 = line1.parent.right;
  674. }
  675. if ((line2.left.color == LineColor.Black) && (line2.right.color == LineColor.Black)) {
  676. line2.color = LineColor.Red;
  677. line1 = line1.parent;
  678. } else {
  679. if (line2.right.color == LineColor.Black) {
  680. line2.left.color = LineColor.Black;
  681. line2.color = LineColor.Red;
  682. RotateRight(line2);
  683. line2 = line1.parent.right;
  684. }
  685. line2.color = line1.parent.color;
  686. line1.parent.color = LineColor.Black;
  687. line2.right.color = LineColor.Black;
  688. RotateLeft(line1.parent);
  689. line1 = document;
  690. }
  691. } else {
  692. line2 = line1.parent.left;
  693. if (line2.color == LineColor.Red) {
  694. line2.color = LineColor.Black;
  695. line1.parent.color = LineColor.Red;
  696. RotateRight(line1.parent);
  697. line2 = line1.parent.left;
  698. }
  699. if ((line2.right.color == LineColor.Black) && (line2.left.color == LineColor.Black)) {
  700. line2.color = LineColor.Red;
  701. line1 = line1.parent;
  702. } else {
  703. if (line2.left.color == LineColor.Black) {
  704. line2.right.color = LineColor.Black;
  705. line2.color = LineColor.Red;
  706. RotateLeft(line2);
  707. line2 = line1.parent.left;
  708. }
  709. line2.color = line1.parent.color;
  710. line1.parent.color = LineColor.Black;
  711. line2.left.color = LineColor.Black;
  712. RotateRight(line1.parent);
  713. line1 = document;
  714. }
  715. }
  716. }
  717. line1.color = LineColor.Black;
  718. }
  719. private void RotateLeft(Line line1) {
  720. Line line2 = line1.right;
  721. line1.right = line2.left;
  722. if (line2.left != sentinel) {
  723. line2.left.parent = line1;
  724. }
  725. if (line2 != sentinel) {
  726. line2.parent = line1.parent;
  727. }
  728. if (line1.parent != null) {
  729. if (line1 == line1.parent.left) {
  730. line1.parent.left = line2;
  731. } else {
  732. line1.parent.right = line2;
  733. }
  734. } else {
  735. document = line2;
  736. }
  737. line2.left = line1;
  738. if (line1 != sentinel) {
  739. line1.parent = line2;
  740. }
  741. }
  742. private void RotateRight(Line line1) {
  743. Line line2 = line1.left;
  744. line1.left = line2.right;
  745. if (line2.right != sentinel) {
  746. line2.right.parent = line1;
  747. }
  748. if (line2 != sentinel) {
  749. line2.parent = line1.parent;
  750. }
  751. if (line1.parent != null) {
  752. if (line1 == line1.parent.right) {
  753. line1.parent.right = line2;
  754. } else {
  755. line1.parent.left = line2;
  756. }
  757. } else {
  758. document = line2;
  759. }
  760. line2.right = line1;
  761. if (line1 != sentinel) {
  762. line1.parent = line2;
  763. }
  764. }
  765. internal void UpdateView(Line line, int pos) {
  766. if (RecalculateDocument(owner.CreateGraphics(), line.line_no, line.line_no, true)) {
  767. // Lineheight changed, invalidate the rest of the document
  768. if ((line.Y - viewport_y) >=0 ) {
  769. // We formatted something that's in view, only draw parts of the screen
  770. owner.Invalidate(new Rectangle(0, line.Y - viewport_y, viewport_width, owner.Height - line.Y - viewport_y));
  771. } else {
  772. // The tag was above the visible area, draw everything
  773. owner.Invalidate();
  774. }
  775. } else {
  776. owner.Invalidate(new Rectangle((int)line.widths[pos] - viewport_x - 1, line.Y - viewport_y, viewport_width, line.height));
  777. }
  778. }
  779. // Update display from line, down line_count lines; pos is unused, but required for the signature
  780. internal void UpdateView(Line line, int line_count, int pos) {
  781. if (RecalculateDocument(owner.CreateGraphics(), line.line_no, line.line_no + line_count - 1, true)) {
  782. // Lineheight changed, invalidate the rest of the document
  783. if ((line.Y - viewport_y) >=0 ) {
  784. // We formatted something that's in view, only draw parts of the screen
  785. owner.Invalidate(new Rectangle(0, line.Y - viewport_y, viewport_width, owner.Height - line.Y - viewport_y));
  786. } else {
  787. // The tag was above the visible area, draw everything
  788. owner.Invalidate();
  789. }
  790. } else {
  791. Line end_line;
  792. end_line = GetLine(line.line_no + line_count -1);
  793. if (end_line == null) {
  794. end_line = line;
  795. }
  796. owner.Invalidate(new Rectangle(0 - viewport_x, line.Y - viewport_y, (int)line.widths[line.text.Length], end_line.Y + end_line.height));
  797. }
  798. }
  799. #endregion // Private Methods
  800. #region Internal Methods
  801. // Clear the document and reset state
  802. internal void Empty() {
  803. document = sentinel;
  804. last_found = sentinel;
  805. lines = 0;
  806. // We always have a blank line
  807. Add(1, "", owner.Font, new SolidBrush(owner.ForeColor));
  808. this.RecalculateDocument(owner.CreateGraphics());
  809. PositionCaret(0, 0);
  810. selection_visible = false;
  811. selection_start.line = this.document;
  812. selection_start.pos = 0;
  813. selection_end.line = this.document;
  814. selection_end.pos = 0;
  815. viewport_x = 0;
  816. viewport_y = 0;
  817. document_x = 0;
  818. document_y = 0;
  819. }
  820. internal void PositionCaret(Line line, int pos) {
  821. caret.tag = line.FindTag(pos);
  822. caret.line = line;
  823. caret.pos = pos;
  824. caret.height = caret.tag.height;
  825. XplatUI.DestroyCaret(owner.Handle);
  826. XplatUI.CreateCaret(owner.Handle, 2, caret.height);
  827. XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.align_shift - viewport_x, caret.line.Y + caret.tag.shift - viewport_y);
  828. if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
  829. }
  830. internal void PositionCaret(int x, int y) {
  831. caret.tag = FindCursor(x, y, out caret.pos);
  832. caret.line = caret.tag.line;
  833. caret.height = caret.tag.height;
  834. XplatUI.DestroyCaret(owner.Handle);
  835. XplatUI.CreateCaret(owner.Handle, 2, caret.height);
  836. XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.align_shift - viewport_x, caret.line.Y + caret.tag.shift - viewport_y);
  837. if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
  838. }
  839. internal void CaretHasFocus() {
  840. if (caret.tag != null) {
  841. XplatUI.CreateCaret(owner.Handle, 2, caret.height);
  842. XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.align_shift - viewport_x, caret.line.Y + caret.tag.shift - viewport_y);
  843. XplatUI.CaretVisible(owner.Handle, true);
  844. }
  845. }
  846. internal void CaretLostFocus() {
  847. XplatUI.DestroyCaret(owner.Handle);
  848. }
  849. internal void AlignCaret() {
  850. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  851. caret.height = caret.tag.height;
  852. XplatUI.CreateCaret(owner.Handle, 2, caret.height);
  853. XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.align_shift - viewport_x, caret.line.Y + caret.tag.shift - viewport_y);
  854. XplatUI.CaretVisible(owner.Handle, true);
  855. if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
  856. }
  857. internal void UpdateCaret() {
  858. if (caret.tag.height != caret.height) {
  859. caret.height = caret.tag.height;
  860. XplatUI.CreateCaret(owner.Handle, 2, caret.height);
  861. }
  862. XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.align_shift - viewport_x, caret.line.Y + caret.tag.shift - viewport_y);
  863. XplatUI.CaretVisible(owner.Handle, true);
  864. if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
  865. }
  866. internal void DisplayCaret() {
  867. XplatUI.CaretVisible(owner.Handle, true);
  868. }
  869. internal void HideCaret() {
  870. XplatUI.CaretVisible(owner.Handle, false);
  871. }
  872. internal void MoveCaret(CaretDirection direction) {
  873. switch(direction) {
  874. case CaretDirection.CharForward: {
  875. caret.pos++;
  876. if (caret.pos > caret.line.text.Length) {
  877. if (multiline) {
  878. // Go into next line
  879. if (caret.line.line_no < this.lines) {
  880. caret.line = GetLine(caret.line.line_no+1);
  881. caret.pos = 0;
  882. caret.tag = caret.line.tags;
  883. } else {
  884. caret.pos--;
  885. }
  886. } else {
  887. // Single line; we stay where we are
  888. caret.pos--;
  889. }
  890. } else {
  891. if ((caret.tag.start - 1 + caret.tag.length) < caret.pos) {
  892. caret.tag = caret.tag.next;
  893. }
  894. }
  895. UpdateCaret();
  896. return;
  897. }
  898. case CaretDirection.CharBack: {
  899. if (caret.pos > 0) {
  900. // caret.pos--; // folded into the if below
  901. if (--caret.pos > 0) {
  902. if (caret.tag.start > caret.pos) {
  903. caret.tag = caret.tag.previous;
  904. }
  905. }
  906. } else {
  907. if (caret.line.line_no > 1) {
  908. caret.line = GetLine(caret.line.line_no - 1);
  909. caret.pos = caret.line.text.Length;
  910. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  911. }
  912. }
  913. UpdateCaret();
  914. return;
  915. }
  916. case CaretDirection.WordForward: {
  917. int len;
  918. len = caret.line.text.Length;
  919. if (caret.pos < len) {
  920. while ((caret.pos < len) && (caret.line.text.ToString(caret.pos, 1) != " ")) {
  921. caret.pos++;
  922. }
  923. if (caret.pos < len) {
  924. // Skip any whitespace
  925. while ((caret.pos < len) && (caret.line.text.ToString(caret.pos, 1) == " ")) {
  926. caret.pos++;
  927. }
  928. }
  929. } else {
  930. if (caret.line.line_no < this.lines) {
  931. caret.line = GetLine(caret.line.line_no+1);
  932. caret.pos = 0;
  933. caret.tag = caret.line.tags;
  934. }
  935. }
  936. UpdateCaret();
  937. return;
  938. }
  939. case CaretDirection.WordBack: {
  940. if (caret.pos > 0) {
  941. caret.pos--;
  942. while ((caret.pos > 0) && (caret.line.text.ToString(caret.pos, 1) == " ")) {
  943. caret.pos--;
  944. }
  945. while ((caret.pos > 0) && (caret.line.text.ToString(caret.pos, 1) != " ")) {
  946. caret.pos--;
  947. }
  948. if (caret.line.text.ToString(caret.pos, 1) == " ") {
  949. if (caret.pos != 0) {
  950. caret.pos++;
  951. } else {
  952. caret.line = GetLine(caret.line.line_no - 1);
  953. caret.pos = caret.line.text.Length;
  954. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  955. }
  956. }
  957. } else {
  958. if (caret.line.line_no > 1) {
  959. caret.line = GetLine(caret.line.line_no - 1);
  960. caret.pos = caret.line.text.Length;
  961. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  962. }
  963. }
  964. UpdateCaret();
  965. return;
  966. }
  967. case CaretDirection.LineUp: {
  968. if (caret.line.line_no > 1) {
  969. int pixel;
  970. pixel = (int)caret.line.widths[caret.pos];
  971. PositionCaret(pixel, GetLine(caret.line.line_no - 1).Y);
  972. XplatUI.CaretVisible(owner.Handle, true);
  973. }
  974. return;
  975. }
  976. case CaretDirection.LineDown: {
  977. if (caret.line.line_no < lines) {
  978. int pixel;
  979. pixel = (int)caret.line.widths[caret.pos];
  980. PositionCaret(pixel, GetLine(caret.line.line_no + 1).Y);
  981. XplatUI.CaretVisible(owner.Handle, true);
  982. }
  983. return;
  984. }
  985. case CaretDirection.Home: {
  986. if (caret.pos > 0) {
  987. caret.pos = 0;
  988. caret.tag = caret.line.tags;
  989. UpdateCaret();
  990. }
  991. return;
  992. }
  993. case CaretDirection.End: {
  994. if (caret.pos < caret.line.text.Length) {
  995. caret.pos = caret.line.text.Length;
  996. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  997. UpdateCaret();
  998. }
  999. return;
  1000. }
  1001. case CaretDirection.PgUp: {
  1002. return;
  1003. }
  1004. case CaretDirection.PgDn: {
  1005. return;
  1006. }
  1007. case CaretDirection.CtrlHome: {
  1008. caret.line = GetLine(1);
  1009. caret.pos = 0;
  1010. caret.tag = caret.line.tags;
  1011. UpdateCaret();
  1012. return;
  1013. }
  1014. case CaretDirection.CtrlEnd: {
  1015. caret.line = GetLine(lines);
  1016. caret.pos = 0;
  1017. caret.tag = caret.line.tags;
  1018. UpdateCaret();
  1019. return;
  1020. }
  1021. }
  1022. }
  1023. // Draw the document
  1024. internal void Draw(Graphics g, Rectangle clip) {
  1025. Line line; // Current line being drawn
  1026. LineTag tag; // Current tag being drawn
  1027. int start; // First line to draw
  1028. int end; // Last line to draw
  1029. string s; // String representing the current line
  1030. int line_no; //
  1031. Brush hilight;
  1032. Brush hilight_text;
  1033. // First, figure out from what line to what line we need to draw
  1034. start = GetLineByPixel(clip.Top + viewport_y, false).line_no;
  1035. end = GetLineByPixel(clip.Bottom + viewport_y, false).line_no;
  1036. Console.WriteLine("Starting drawing at line {0}, ending at line {1} (clip-bottom:{2})", start, end, clip.Bottom);
  1037. // Now draw our elements; try to only draw those that are visible
  1038. line_no = start;
  1039. #if Debug
  1040. DateTime n = DateTime.Now;
  1041. Console.WriteLine("Started drawing: {0}s {1}ms", n.Second, n.Millisecond);
  1042. #endif
  1043. hilight = ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorHilight);
  1044. hilight_text = ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorHilightText);
  1045. while (line_no <= end) {
  1046. line = GetLine(line_no);
  1047. tag = line.tags;
  1048. s = line.text.ToString();
  1049. while (tag != null) {
  1050. if (((tag.X + tag.width) > (clip.Left - viewport_x)) || (tag.X < (clip.Right - viewport_x))) {
  1051. // Check for selection
  1052. if ((!selection_visible) || (!owner.has_focus) || (line_no < selection_start.line.line_no) || (line_no > selection_end.line.line_no)) {
  1053. // regular drawing
  1054. g.DrawString(s.Substring(tag.start-1, tag.length), tag.font, tag.color, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
  1055. } else {
  1056. // we might have to draw our selection
  1057. if ((line_no != selection_start.line.line_no) && (line_no != selection_end.line.line_no)) {
  1058. g.FillRectangle(hilight, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, line.widths[tag.start + tag.length - 1], tag.height);
  1059. g.DrawString(s.Substring(tag.start-1, tag.length), tag.font, hilight_text, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
  1060. } else {
  1061. bool highlight;
  1062. bool partial;
  1063. highlight = false;
  1064. partial = false;
  1065. // Check the partial drawings first
  1066. if ((selection_start.tag == tag) && (selection_end.tag == tag)) {
  1067. partial = true;
  1068. // First, the regular part
  1069. g.DrawString(s.Substring(tag.start - 1, selection_start.pos - tag.start + 1), tag.font, tag.color, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
  1070. // Now the highlight
  1071. g.FillRectangle(hilight, line.widths[selection_start.pos] + line.align_shift, line.Y + tag.shift - viewport_y, line.widths[selection_end.pos] - line.widths[selection_start.pos], tag.height);
  1072. g.DrawString(s.Substring(selection_start.pos, selection_end.pos - selection_start.pos), tag.font, hilight_text, line.widths[selection_start.pos] + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
  1073. // And back to the regular
  1074. g.DrawString(s.Substring(selection_end.pos, tag.length - selection_end.pos), tag.font, tag.color, line.widths[selection_end.pos] + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
  1075. } else if (selection_start.tag == tag) {
  1076. partial = true;
  1077. // The highlighted part
  1078. g.FillRectangle(hilight, line.widths[selection_start.pos] + line.align_shift, line.Y + tag.shift - viewport_y, line.widths[tag.start + tag.length - 1] - line.widths[selection_start.pos], tag.height);
  1079. g.DrawString(s.Substring(selection_start.pos, tag.length - selection_start.pos), tag.font, hilight_text, line.widths[selection_start.pos] + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
  1080. // The regular part
  1081. g.DrawString(s.Substring(tag.start - 1, selection_start.pos - tag.start + 1), tag.font, tag.color, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
  1082. } else if (selection_end.tag == tag) {
  1083. partial = true;
  1084. // The highlighted part
  1085. g.FillRectangle(hilight, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, line.widths[selection_end.pos], tag.height);
  1086. g.DrawString(s.Substring(tag.start - 1, selection_end.pos - tag.start + 1), tag.font, hilight_text, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
  1087. // The regular part
  1088. g.DrawString(s.Substring(selection_end.pos, tag.length - selection_end.pos), tag.font, tag.color, line.widths[selection_end.pos] + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
  1089. } else {
  1090. // no partially selected tags here, simple checks...
  1091. if (selection_start.line == line) {
  1092. if ((tag.start + tag.length - 1) > selection_start.pos) {
  1093. highlight = true;
  1094. }
  1095. }
  1096. if (selection_end.line == line) {
  1097. if ((tag.start + tag.length - 1) < selection_start.pos) {
  1098. highlight = true;
  1099. }
  1100. }
  1101. }
  1102. if (!partial) {
  1103. if (highlight) {
  1104. g.FillRectangle(hilight, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, line.widths[tag.start + tag.length - 1], tag.height);
  1105. g.DrawString(s.Substring(tag.start-1, tag.length), tag.font, hilight_text, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
  1106. } else {
  1107. g.DrawString(s.Substring(tag.start-1, tag.length), tag.font, tag.color, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
  1108. }
  1109. }
  1110. }
  1111. }
  1112. }
  1113. tag = tag.next;
  1114. }
  1115. line_no++;
  1116. }
  1117. #if Debug
  1118. n = DateTime.Now;
  1119. Console.WriteLine("Finished drawing: {0}s {1}ms", n.Second, n.Millisecond);
  1120. #endif
  1121. }
  1122. // Inserts a character at the given position
  1123. internal void InsertString(Line line, int pos, string s) {
  1124. InsertString(line.FindTag(pos), pos, s);
  1125. }
  1126. // Inserts a string at the given position
  1127. internal void InsertString(LineTag tag, int pos, string s) {
  1128. Line line;
  1129. int len;
  1130. len = s.Length;
  1131. line = tag.line;
  1132. line.text.Insert(pos, s);
  1133. tag.length += len;
  1134. tag = tag.next;
  1135. while (tag != null) {
  1136. tag.start += len;
  1137. tag = tag.next;
  1138. }
  1139. line.Grow(len);
  1140. line.recalc = true;
  1141. UpdateView(line, pos);
  1142. }
  1143. // Inserts a string at the caret position
  1144. internal void InsertStringAtCaret(string s, bool move_caret) {
  1145. LineTag tag;
  1146. int len;
  1147. len = s.Length;
  1148. caret.line.text.Insert(caret.pos, s);
  1149. caret.tag.length += len;
  1150. if (caret.tag.next != null) {
  1151. tag = caret.tag.next;
  1152. while (tag != null) {
  1153. tag.start += len;
  1154. tag = tag.next;
  1155. }
  1156. }
  1157. caret.line.Grow(len);
  1158. caret.line.recalc = true;
  1159. UpdateView(caret.line, caret.pos);
  1160. if (move_caret) {
  1161. caret.pos += len;
  1162. UpdateCaret();
  1163. }
  1164. }
  1165. // Inserts a character at the given position
  1166. internal void InsertChar(Line line, int pos, char ch) {
  1167. InsertChar(line.FindTag(pos), pos, ch);
  1168. }
  1169. // Inserts a character at the given position
  1170. internal void InsertChar(LineTag tag, int pos, char ch) {
  1171. Line line;
  1172. line = tag.line;
  1173. line.text.Insert(pos, ch);
  1174. tag.length++;
  1175. tag = tag.next;
  1176. while (tag != null) {
  1177. tag.start++;
  1178. tag = tag.next;
  1179. }
  1180. line.Grow(1);
  1181. line.recalc = true;
  1182. UpdateView(line, pos);
  1183. }
  1184. // Inserts a character at the current caret position
  1185. internal void InsertCharAtCaret(char ch, bool move_caret) {
  1186. LineTag tag;
  1187. caret.line.text.Insert(caret.pos, ch);
  1188. caret.tag.length++;
  1189. if (caret.tag.next != null) {
  1190. tag = caret.tag.next;
  1191. while (tag != null) {
  1192. tag.start++;
  1193. tag = tag.next;
  1194. }
  1195. }
  1196. caret.line.Grow(1);
  1197. caret.line.recalc = true;
  1198. UpdateView(caret.line, caret.pos);
  1199. if (move_caret) {
  1200. caret.pos++;
  1201. UpdateCaret();
  1202. }
  1203. }
  1204. // Inserts n characters at the given position; it will not delete past line limits
  1205. internal void DeleteChars(LineTag tag, int pos, int count) {
  1206. Line line;
  1207. bool streamline;
  1208. streamline = false;
  1209. line = tag.line;
  1210. if (pos == line.text.Length) {
  1211. return;
  1212. }
  1213. line.text.Remove(pos, count);
  1214. // Check if we're crossing tag boundaries
  1215. if ((pos + count) > (tag.start + tag.length)) {
  1216. int left;
  1217. // We have to delete cross tag boundaries
  1218. streamline = true;
  1219. left = count;
  1220. left -= pos - tag.start;
  1221. tag.length -= pos - tag.start;
  1222. tag = tag.next;
  1223. while ((tag != null) && (left > 0)) {
  1224. if (tag.length > left) {
  1225. tag.length -= left;
  1226. left = 0;
  1227. } else {
  1228. left -= tag.length;
  1229. tag.length = 0;
  1230. tag = tag.next;
  1231. }
  1232. }
  1233. } else {
  1234. // We got off easy, same tag
  1235. tag.length -= count;
  1236. }
  1237. tag = tag.next;
  1238. while (tag != null) {
  1239. tag.start -= count;
  1240. tag = tag.next;
  1241. }
  1242. line.recalc = true;
  1243. if (streamline) {
  1244. line.Streamline();
  1245. }
  1246. UpdateView(line, pos);
  1247. }
  1248. // Deletes a character at or after the given position (depending on forward); it will not delete past line limits
  1249. internal void DeleteChar(LineTag tag, int pos, bool forward) {
  1250. Line line;
  1251. bool streamline;
  1252. streamline = false;
  1253. line = tag.line;
  1254. if ((pos == 0 && forward == false) || (pos == line.text.Length && forward == true)) {
  1255. return;
  1256. }
  1257. if (forward) {
  1258. line.text.Remove(pos, 1);
  1259. tag.length--;
  1260. if (tag.length == 0) {
  1261. streamline = true;
  1262. }
  1263. } else {
  1264. pos--;
  1265. line.text.Remove(pos, 1);
  1266. if (pos >= (tag.start - 1)) {
  1267. tag.length--;
  1268. if (tag.length == 0) {
  1269. streamline = true;
  1270. }
  1271. } else if (tag.previous != null) {
  1272. tag.previous.length--;
  1273. if (tag.previous.length == 0) {
  1274. streamline = true;
  1275. }
  1276. }
  1277. }
  1278. tag = tag.next;
  1279. while (tag != null) {
  1280. tag.start--;
  1281. tag = tag.next;
  1282. }
  1283. line.recalc = true;
  1284. if (streamline) {
  1285. line.Streamline();
  1286. }
  1287. UpdateView(line, pos);
  1288. }
  1289. // Combine two lines
  1290. internal void Combine(int FirstLine, int SecondLine) {
  1291. Combine(GetLine(FirstLine), GetLine(SecondLine));
  1292. }
  1293. internal void Combine(Line first, Line second) {
  1294. LineTag last;
  1295. int shift;
  1296. // Combine the two tag chains into one
  1297. last = first.tags;
  1298. while (last.next != null) {
  1299. last = last.next;
  1300. }
  1301. last.next = second.tags;
  1302. last.next.previous = last;
  1303. shift = last.start + last.length - 1;
  1304. // Fix up references within the chain
  1305. last = last.next;
  1306. while (last != null) {
  1307. last.line = first;
  1308. last.start += shift;
  1309. last = last.next;
  1310. }
  1311. // Combine both lines' strings
  1312. first.text.Insert(first.text.Length, second.text.ToString());
  1313. first.Grow(first.text.Length);
  1314. // Remove the reference to our (now combined) tags from the doomed line
  1315. second.tags = null;
  1316. // Renumber lines
  1317. DecrementLines(first.line_no + 2); // first.line_no + 1 will be deleted, so we need to start renumbering one later
  1318. // Mop up
  1319. first.recalc = true;
  1320. first.height = 0; // This forces RecalcDocument/UpdateView to redraw from this line on
  1321. first.Streamline();
  1322. #if Debug
  1323. Line check_first;
  1324. Line check_second;
  1325. check_first = GetLine(first.line_no);
  1326. check_second = GetLine(check_first.line_no + 1);
  1327. Console.WriteLine("Pre-delete: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
  1328. #endif
  1329. this.Delete(second);
  1330. #if Debug
  1331. check_first = GetLine(first.line_no);
  1332. check_second = GetLine(check_first.line_no + 1);
  1333. Console.WriteLine("Post-delete Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
  1334. #endif
  1335. }
  1336. // Split the line at the position into two
  1337. internal void Split(int LineNo, int pos) {
  1338. Line line;
  1339. LineTag tag;
  1340. line = GetLine(LineNo);
  1341. tag = LineTag.FindTag(line, pos);
  1342. Split(line, tag, pos, false);
  1343. }
  1344. internal void Split(Line line, int pos) {
  1345. LineTag tag;
  1346. tag = LineTag.FindTag(line, pos);
  1347. Split(line, tag, pos, false);
  1348. }
  1349. internal void Split(Line line, LineTag tag, int pos, bool soft) {
  1350. LineTag new_tag;
  1351. Line new_line;
  1352. bool move_caret;
  1353. move_caret = false;
  1354. // Adjust selection and cursors
  1355. if (soft && (caret.line == line) && (caret.pos >= pos)) {
  1356. move_caret = true;
  1357. }
  1358. // cover the easy case first
  1359. if (pos == line.text.Length) {
  1360. Add(line.line_no + 1, "", line.alignment, tag.font, tag.color);
  1361. if (soft) {
  1362. if (move_caret) {
  1363. caret.line = GetLine(line.line_no + 1);
  1364. caret.line.soft_break = true;
  1365. caret.tag = selection_start.line.tags;
  1366. caret.pos = 0;
  1367. } else {
  1368. GetLine(line.line_no + 1).soft_break = true;
  1369. }
  1370. }
  1371. return;
  1372. }
  1373. // We need to move the rest of the text into the new line
  1374. Add(line.line_no + 1, line.text.ToString(pos, line.text.Length - pos), line.alignment, tag.font, tag.color);
  1375. // Now transfer our tags from this line to the next
  1376. new_line = GetLine(line.line_no + 1);
  1377. line.recalc = true;
  1378. if ((tag.start - 1) == pos) {
  1379. int shift;
  1380. // We can simply break the chain and move the tag into the next line
  1381. if (tag == line.tags) {
  1382. new_tag = new LineTag(line, 1, 0);
  1383. new_tag.font = tag.font;
  1384. new_tag.color = tag.color;
  1385. line.tags = new_tag;
  1386. }
  1387. if (tag.previous != null) {
  1388. tag.previous.next = null;
  1389. }
  1390. new_line.tags = tag;
  1391. tag.previous = null;
  1392. tag.line = new_line;
  1393. // Walk the list and correct the start location of the tags we just bumped into the next line
  1394. shift = tag.start - 1;
  1395. new_tag = tag;
  1396. while (new_tag != null) {
  1397. new_tag.start -= shift;
  1398. new_tag.line = new_line;
  1399. new_tag = new_tag.next;
  1400. }
  1401. } else {
  1402. int shift;
  1403. new_tag = new LineTag(new_line, 1, tag.start - 1 + tag.length - pos);
  1404. new_tag.next = tag.next;
  1405. new_tag.font = tag.font;
  1406. new_tag.color = tag.color;
  1407. new_line.tags = new_tag;
  1408. if (new_tag.next != null) {
  1409. new_tag.next.previous = new_tag;
  1410. }
  1411. tag.next = null;
  1412. tag.length = pos - tag.start + 1;
  1413. shift = pos;
  1414. new_tag = new_tag.next;
  1415. while (new_tag != null) {
  1416. new_tag.start -= shift;
  1417. new_tag.line = new_line;
  1418. new_tag = new_tag.next;
  1419. }
  1420. }
  1421. if (soft) {
  1422. if (move_caret) {
  1423. caret.line = new_line;
  1424. caret.pos = caret.pos - pos;
  1425. caret.tag = caret.line.FindTag(caret.pos);
  1426. }
  1427. new_line.soft_break = true;
  1428. }
  1429. line.text.Remove(pos, line.text.Length - pos);
  1430. }
  1431. // Adds a line of text, with given font.
  1432. // Bumps any line at that line number that already exists down
  1433. internal void Add(int LineNo, string Text, Font font, Brush color) {
  1434. Add(LineNo, Text, HorizontalAlignment.Left, font, color);
  1435. }
  1436. internal void Add(int LineNo, string Text, HorizontalAlignment align, Font font, Brush color) {
  1437. Line add;
  1438. Line line;
  1439. int line_no;
  1440. if (LineNo<1 || Text == null) {
  1441. if (LineNo<1) {
  1442. throw new ArgumentNullException("LineNo", "Line numbers must be positive");
  1443. } else {
  1444. throw new ArgumentNullException("Text", "Cannot insert NULL line");
  1445. }
  1446. }
  1447. add = new Line(LineNo, Text, align, font, color);
  1448. line = document;
  1449. while (line != sentinel) {
  1450. add.parent = line;
  1451. line_no = line.line_no;
  1452. if (LineNo > line_no) {
  1453. line = line.right;
  1454. } else if (LineNo < line_no) {
  1455. line = line.left;
  1456. } else {
  1457. // Bump existing line numbers; walk all nodes to the right of this one and increment line_no
  1458. IncrementLines(line.line_no);
  1459. line = line.left;
  1460. }
  1461. }
  1462. add.left = sentinel;
  1463. add.right = sentinel;
  1464. if (add.parent != null) {
  1465. if (LineNo > add.parent.line_no) {
  1466. add.parent.right = add;
  1467. } else {
  1468. add.parent.left = add;
  1469. }
  1470. } else {
  1471. // Root node
  1472. document = add;
  1473. }
  1474. RebalanceAfterAdd(add);
  1475. lines++;
  1476. }
  1477. internal virtual void Clear() {
  1478. lines = 0;
  1479. document = sentinel;
  1480. }
  1481. public virtual object Clone() {
  1482. Document clone;
  1483. clone = new Document(null);
  1484. clone.lines = this.lines;
  1485. clone.document = (Line)document.Clone();
  1486. return clone;
  1487. }
  1488. internal void Delete(int LineNo) {
  1489. if (LineNo>lines) {
  1490. return;
  1491. }
  1492. Delete(GetLine(LineNo));
  1493. }
  1494. internal void Delete(Line line1) {
  1495. Line line2;// = new Line();
  1496. Line line3;
  1497. if ((line1.left == sentinel) || (line1.right == sentinel)) {
  1498. line3 = line1;
  1499. } else {
  1500. line3 = line1.right;
  1501. while (line3.left != sentinel) {
  1502. line3 = line3.left;
  1503. }
  1504. }
  1505. if (line3.left != sentinel) {
  1506. line2 = line3.left;
  1507. } else {
  1508. line2 = line3.right;
  1509. }
  1510. line2.parent = line3.parent;
  1511. if (line3.parent != null) {
  1512. if(line3 == line3.parent.left) {
  1513. line3.parent.left = line2;
  1514. } else {
  1515. line3.parent.right = line2;
  1516. }
  1517. } else {
  1518. document = line2;
  1519. }
  1520. if (line3 != line1) {
  1521. LineTag tag;
  1522. line1.ascent = line3.ascent;
  1523. line1.height = line3.height;
  1524. line1.line_no = line3.line_no;
  1525. line1.recalc = line3.recalc;
  1526. line1.space = line3.space;
  1527. line1.tags = line3.tags;
  1528. line1.text = line3.text;
  1529. line1.widths = line3.widths;
  1530. line1.Y = line3.Y;
  1531. line1.soft_break = line3.soft_break;
  1532. tag = line1.tags;
  1533. while (tag != null) {
  1534. tag.line = line1;
  1535. tag = tag.next;
  1536. }
  1537. }
  1538. if (line3.color == LineColor.Black)
  1539. RebalanceAfterDelete(line2);
  1540. this.lines--;
  1541. last_found = sentinel;
  1542. }
  1543. // Set our selection markers
  1544. internal void Invalidate(Line start, int start_pos, Line end, int end_pos) {
  1545. Line l1;
  1546. Line l2;
  1547. int p1;
  1548. int p2;
  1549. // figure out what's before what so the logic below is straightforward
  1550. if (start.line_no < end.line_no) {
  1551. l1 = start;
  1552. p1 = start_pos;
  1553. l2 = end;
  1554. p2 = end_pos;
  1555. } else if (start.line_no > end.line_no) {
  1556. l1 = end;
  1557. p1 = end_pos;
  1558. l2 = start;
  1559. p2 = start_pos;
  1560. } else {
  1561. if (start_pos < end_pos) {
  1562. l1 = start;
  1563. p1 = start_pos;
  1564. l2 = end;
  1565. p2 = end_pos;
  1566. } else {
  1567. l1 = end;
  1568. p1 = end_pos;
  1569. l2 = start;
  1570. p2 = start_pos;
  1571. }
  1572. owner.Invalidate(new Rectangle((int)l1.widths[p1] - viewport_x, l1.Y - viewport_y, (int)l2.widths[p2] - (int)l1.widths[p1], l1.height));
  1573. return;
  1574. }
  1575. // Three invalidates:
  1576. // First line from start
  1577. owner.Invalidate(new Rectangle((int)l1.widths[p1] - viewport_x, l1.Y - viewport_y, viewport_width, l1.height));
  1578. // lines inbetween
  1579. if ((l1.line_no + 1) < l2.line_no) {
  1580. int y;
  1581. y = GetLine(l1.line_no + 1).Y;
  1582. owner.Invalidate(new Rectangle(0 - viewport_x, y - viewport_y, viewport_width, GetLine(l2.line_no - 1).Y - y - viewport_y));
  1583. }
  1584. // Last line to end
  1585. owner.Invalidate(new Rectangle((int)l1.widths[p1] - viewport_x, l1.Y - viewport_y, viewport_width, l1.height));
  1586. }
  1587. // It's nothing short of pathetic to always invalidate the whole control
  1588. // I will find time to finish the optimization and make it invalidate deltas only
  1589. internal void SetSelectionToCaret(bool start) {
  1590. if (start) {
  1591. selection_start.line = caret.line;
  1592. selection_start.tag = caret.tag;
  1593. selection_start.pos = caret.pos;
  1594. // start always also selects end
  1595. selection_end.line = caret.line;
  1596. selection_end.tag = caret.tag;
  1597. selection_end.pos = caret.pos;
  1598. } else {
  1599. if (caret.line.line_no <= selection_end.line.line_no) {
  1600. if ((caret.line != selection_end.line) || (caret.pos < selection_end.pos)) {
  1601. selection_start.line = caret.line;
  1602. selection_start.tag = caret.tag;
  1603. selection_start.pos = caret.pos;
  1604. } else {
  1605. selection_end.line = caret.line;
  1606. selection_end.tag = caret.tag;
  1607. selection_end.pos = caret.pos;
  1608. }
  1609. } else {
  1610. selection_end.line = caret.line;
  1611. selection_end.tag = caret.tag;
  1612. selection_end.pos = caret.pos;
  1613. }
  1614. }
  1615. if ((selection_start.line == selection_end.line) && (selection_start.pos == selection_end.pos)) {
  1616. selection_visible = false;
  1617. } else {
  1618. selection_visible = true;
  1619. }
  1620. owner.Invalidate();
  1621. }
  1622. #if buggy
  1623. internal void SetSelection(Line start, int start_pos, Line end, int end_pos) {
  1624. // Ok, this is ugly, bad and slow, but I don't have time right now to optimize it.
  1625. if (selection_visible) {
  1626. // Try to only invalidate what's changed so we don't redraw the whole thing
  1627. if ((start != selection_start.line) || (start_pos != selection_start.pos) && ((end == selection_end.line) && (end_pos == selection_end.pos))) {
  1628. Invalidate(start, start_pos, selection_start.line, selection_start.pos);
  1629. } else if ((end != selection_end.line) || (end_pos != selection_end.pos) && ((start == selection_start.line) && (start_pos == selection_start.pos))) {
  1630. Invalidate(end, end_pos, selection_end.line, selection_end.pos);
  1631. } else {
  1632. // both start and end changed
  1633. Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
  1634. }
  1635. }
  1636. selection_start.line = start;
  1637. selection_start.pos = start_pos;
  1638. if (start != null) {
  1639. selection_start.tag = LineTag.FindTag(start, start_pos);
  1640. }
  1641. selection_end.line = end;
  1642. selection_end.pos = end_pos;
  1643. if (end != null) {
  1644. selection_end.tag = LineTag.FindTag(end, end_pos);
  1645. }
  1646. if (((start == end) && (start_pos == end_pos)) || start == null || end == null) {
  1647. selection_visible = false;
  1648. } else {
  1649. selection_visible = true;
  1650. }
  1651. }
  1652. #endif
  1653. // Make sure that start is always before end
  1654. private void FixupSelection() {
  1655. if (selection_start.line.line_no > selection_end.line.line_no) {
  1656. Line line;
  1657. int pos;
  1658. LineTag tag;
  1659. line = selection_start.line;
  1660. tag = selection_start.tag;
  1661. pos = selection_start.pos;
  1662. selection_start.line = selection_end.line;
  1663. selection_start.tag = selection_end.tag;
  1664. selection_start.pos = selection_end.pos;
  1665. selection_end.line = line;
  1666. selection_end.tag = tag;
  1667. selection_end.pos = pos;
  1668. return;
  1669. }
  1670. if ((selection_start.line == selection_end.line) && (selection_start.pos > selection_end.pos)) {
  1671. int pos;
  1672. pos = selection_start.pos;
  1673. selection_start.pos = selection_end.pos;
  1674. selection_end.pos = pos;
  1675. Console.WriteLine("flipped: sel start: {0} end: {1}", selection_start.pos, selection_end.pos);
  1676. return;
  1677. }
  1678. return;
  1679. }
  1680. internal void SetSelectionStart(Line start, int start_pos) {
  1681. selection_start.line = start;
  1682. selection_start.pos = start_pos;
  1683. selection_start.tag = LineTag.FindTag(start, start_pos);
  1684. FixupSelection();
  1685. if ((selection_end.line != selection_start.line) || (selection_end.pos != selection_start.pos)) {
  1686. selection_visible = true;
  1687. }
  1688. if (selection_visible) {
  1689. Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
  1690. }
  1691. }
  1692. internal void SetSelectionEnd(Line end, int end_pos) {
  1693. selection_end.line = end;
  1694. selection_end.pos = end_pos;
  1695. selection_end.tag = LineTag.FindTag(end, end_pos);
  1696. FixupSelection();
  1697. if ((selection_end.line != selection_start.line) || (selection_end.pos != selection_start.pos)) {
  1698. selection_visible = true;
  1699. }
  1700. if (selection_visible) {
  1701. Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
  1702. }
  1703. }
  1704. internal void SetSelection(Line start, int start_pos) {
  1705. if (selection_visible) {
  1706. Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
  1707. }
  1708. selection_start.line = start;
  1709. selection_start.pos = start_pos;
  1710. selection_start.tag = LineTag.FindTag(start, start_pos);
  1711. selection_end.line = start;
  1712. selection_end.tag = selection_start.tag;
  1713. selection_end.pos = start_pos;
  1714. selection_visible = false;
  1715. }
  1716. internal void InvalidateSelectionArea() {
  1717. // implement me
  1718. }
  1719. // Return the current selection, as string
  1720. internal string GetSelection() {
  1721. // We return String.Empty if there is no selection
  1722. if ((selection_start.pos == selection_end.pos) && (selection_start.line == selection_end.line)) {
  1723. return string.Empty;
  1724. }
  1725. if (!multiline || (selection_start.line == selection_end.line)) {
  1726. return selection_start.line.text.ToString(selection_start.pos, selection_end.pos - selection_start.pos);
  1727. } else {
  1728. StringBuilder sb;
  1729. int i;
  1730. int start;
  1731. int end;
  1732. sb = new StringBuilder();
  1733. start = selection_start.line.line_no;
  1734. end = selection_end.line.line_no;
  1735. sb.Append(selection_start.line.text.ToString(selection_start.pos, selection_start.line.text.Length - selection_start.pos) + Environment.NewLine);
  1736. if ((start + 1) < end) {
  1737. for (i = start + 1; i < end; i++) {
  1738. sb.Append(GetLine(i).text.ToString() + Environment.NewLine);
  1739. }
  1740. }
  1741. sb.Append(selection_end.line.text.ToString(0, selection_end.pos));
  1742. return sb.ToString();
  1743. }
  1744. }
  1745. internal void ReplaceSelection(string s) {
  1746. // The easiest is to break the lines where the selection tags are and delete those lines
  1747. if ((selection_start.pos == selection_end.pos) && (selection_start.line == selection_end.line)) {
  1748. // Nothing to delete, simply insert
  1749. InsertString(selection_start.tag, selection_start.pos, s);
  1750. }
  1751. if (!multiline || (selection_start.line == selection_end.line)) {
  1752. DeleteChars(selection_start.tag, selection_start.pos, selection_end.pos - selection_start.pos);
  1753. // The tag might have been removed, we need to recalc it
  1754. selection_start.tag = selection_start.line.FindTag(selection_start.pos);
  1755. InsertString(selection_start.tag, selection_start.pos, s);
  1756. } else {
  1757. int i;
  1758. int start;
  1759. int end;
  1760. int base_line;
  1761. string[] ins;
  1762. int insert_lines;
  1763. start = selection_start.line.line_no;
  1764. end = selection_end.line.line_no;
  1765. // Delete first line
  1766. DeleteChars(selection_start.tag, selection_start.pos, selection_end.pos - selection_start.pos);
  1767. start++;
  1768. if (start < end) {
  1769. for (i = end - 1; i >= start; i--) {
  1770. Delete(i);
  1771. }
  1772. }
  1773. // Delete last line
  1774. DeleteChars(selection_end.line.tags, 0, selection_end.pos);
  1775. ins = s.Split(new char[] {'\n'});
  1776. for (int j = 0; j < ins.Length; j++) {
  1777. if (ins[j].EndsWith("\r")) {
  1778. ins[j] = ins[j].Substring(0, ins[j].Length - 1);
  1779. }
  1780. }
  1781. insert_lines = ins.Length;
  1782. // Bump the text at insertion point a line down if we're inserting more than one line
  1783. if (insert_lines > 1) {
  1784. Split(selection_start.line, selection_start.pos);
  1785. // Reminder of start line is now in startline+1
  1786. // if the last line does not end with a \n we will insert the last line in front of the just moved text
  1787. if (s.EndsWith("\n")) {
  1788. insert_lines--; // We don't want to insert the last line as part of the loop anymore
  1789. InsertString(GetLine(selection_start.line.line_no + 1), 0, ins[insert_lines - 1]);
  1790. }
  1791. }
  1792. // Insert the first line
  1793. InsertString(selection_start.line, selection_start.pos, ins[0]);
  1794. if (insert_lines > 1) {
  1795. base_line = selection_start.line.line_no + 1;
  1796. for (i = 1; i < insert_lines; i++) {
  1797. Add(base_line + i, ins[i], selection_start.line.alignment, selection_start.tag.font, selection_start.tag.color);
  1798. }
  1799. }
  1800. }
  1801. selection_end.line = selection_start.line;
  1802. selection_end.pos = selection_start.pos;
  1803. selection_end.tag = selection_start.tag;
  1804. selection_visible = false;
  1805. PositionCaret(selection_start.line, selection_start.pos);
  1806. InvalidateSelectionArea();
  1807. }
  1808. internal void CharIndexToLineTag(int index, out Line line_out, out LineTag tag_out, out int pos) {
  1809. Line line;
  1810. LineTag tag;
  1811. int i;
  1812. int chars;
  1813. int start;
  1814. chars = 0;
  1815. for (i = 1; i < lines; i++) {
  1816. line = GetLine(i);
  1817. start = chars;
  1818. chars += line.text.Length + 2; // We count the trailing \n as a char
  1819. if (index <= chars) {
  1820. // we found the line
  1821. tag = line.tags;
  1822. while (tag != null) {
  1823. if (index < (start + tag.start + tag.length)) {
  1824. line_out = line;
  1825. tag_out = tag;
  1826. pos = index - start;
  1827. return;
  1828. }
  1829. if (tag.next == null) {
  1830. Line next_line;
  1831. next_line = GetLine(line.line_no + 1);
  1832. if (next_line != null) {
  1833. line_out = next_line;
  1834. tag_out = next_line.tags;
  1835. pos = 0;
  1836. return;
  1837. } else {
  1838. line_out = line;
  1839. tag_out = tag;
  1840. pos = line_out.text.Length;
  1841. return;
  1842. }
  1843. }
  1844. tag = tag.next;
  1845. }
  1846. }
  1847. }
  1848. line_out = GetLine(lines);
  1849. tag = line_out.tags;
  1850. while (tag.next != null) {
  1851. tag = tag.next;
  1852. }
  1853. tag_out = tag;
  1854. pos = line_out.text.Length;
  1855. }
  1856. internal int LineTagToCharIndex(Line line, int pos) {
  1857. int i;
  1858. int length;
  1859. // Count first and last line
  1860. length = 0;
  1861. // Count the lines in the middle
  1862. for (i = 1; i < line.line_no; i++) {
  1863. length += GetLine(i).text.Length + 2; // Add one for the \n at the end of the line
  1864. }
  1865. length += pos;
  1866. return length;
  1867. }
  1868. internal int SelectionLength() {
  1869. if ((selection_start.pos == selection_end.pos) && (selection_start.line == selection_end.line)) {
  1870. return 0;
  1871. }
  1872. if (!multiline || (selection_start.line == selection_end.line)) {
  1873. return selection_end.pos - selection_start.pos;
  1874. } else {
  1875. int i;
  1876. int start;
  1877. int end;
  1878. int length;
  1879. // Count first and last line
  1880. length = selection_start.line.text.Length - selection_start.pos + selection_end.pos;
  1881. // Count the lines in the middle
  1882. start = selection_start.line.line_no + 1;
  1883. end = selection_end.line.line_no;
  1884. if (start < end) {
  1885. for (i = start; i < end; i++) {
  1886. length += GetLine(i).text.Length;
  1887. }
  1888. }
  1889. return length;
  1890. }
  1891. }
  1892. /// <summary>Give it a Line number and it returns the Line object at with that line number</summary>
  1893. internal Line GetLine(int LineNo) {
  1894. Line line = document;
  1895. while (line != sentinel) {
  1896. if (LineNo == line.line_no) {
  1897. return line;
  1898. } else if (LineNo < line.line_no) {
  1899. line = line.left;
  1900. } else {
  1901. line = line.right;
  1902. }
  1903. }
  1904. return null;
  1905. }
  1906. /// <summary>Retrieve the previous tag; walks line boundaries</summary>
  1907. internal LineTag PreviousTag(LineTag tag) {
  1908. Line l;
  1909. if (tag.previous != null) {
  1910. return tag.previous;
  1911. }
  1912. // Next line
  1913. if (tag.line.line_no == 1) {
  1914. return null;
  1915. }
  1916. l = GetLine(tag.line.line_no - 1);
  1917. if (l != null) {
  1918. LineTag t;
  1919. t = l.tags;
  1920. while (t.next != null) {
  1921. t = t.next;
  1922. }
  1923. return t;
  1924. }
  1925. return null;
  1926. }
  1927. /// <summary>Retrieve the next tag; walks line boundaries</summary>
  1928. internal LineTag NextTag(LineTag tag) {
  1929. Line l;
  1930. if (tag.next != null) {
  1931. return tag.next;
  1932. }
  1933. // Next line
  1934. l = GetLine(tag.line.line_no + 1);
  1935. if (l != null) {
  1936. return l.tags;
  1937. }
  1938. return null;
  1939. }
  1940. /// <summary>Give it a Y pixel coordinate and it returns the Line covering that Y coordinate</summary>
  1941. internal Line GetLineByPixel(int y, bool exact) {
  1942. Line line = document;
  1943. Line last = null;
  1944. while (line != sentinel) {
  1945. last = line;
  1946. if ((y >= line.Y) && (y < (line.Y+line.height))) {
  1947. return line;
  1948. } else if (y < line.Y) {
  1949. line = line.left;
  1950. } else {
  1951. line = line.right;
  1952. }
  1953. }
  1954. if (exact) {
  1955. return null;
  1956. }
  1957. return last;
  1958. }
  1959. // Give it x/y pixel coordinates and it returns the Tag at that position; optionally the char position is returned in index
  1960. internal LineTag FindTag(int x, int y, out int index, bool exact) {
  1961. Line line;
  1962. LineTag tag;
  1963. line = GetLineByPixel(y, exact);
  1964. if (line == null) {
  1965. index = 0;
  1966. return null;
  1967. }
  1968. tag = line.tags;
  1969. // Alignment adjustment
  1970. x += line.align_shift;
  1971. while (true) {
  1972. if (x >= tag.X && x < (tag.X+tag.width)) {
  1973. int end;
  1974. end = tag.start + tag.length - 1;
  1975. for (int pos = tag.start; pos < end; pos++) {
  1976. if (x < line.widths[pos]) {
  1977. index = pos;
  1978. return tag;
  1979. }
  1980. }
  1981. index=end;
  1982. return tag;
  1983. }
  1984. if (tag.next != null) {
  1985. tag = tag.next;
  1986. } else {
  1987. if (exact) {
  1988. index = 0;
  1989. return null;
  1990. }
  1991. index = line.text.Length;
  1992. return tag;
  1993. }
  1994. }
  1995. }
  1996. // Give it x/y pixel coordinates and it returns the Tag at that position; optionally the char position is returned in index
  1997. internal LineTag FindCursor(int x, int y, out int index) {
  1998. Line line;
  1999. LineTag tag;
  2000. line = GetLineByPixel(y, false);
  2001. tag = line.tags;
  2002. // Adjust for alignment
  2003. x += line.align_shift;
  2004. while (true) {
  2005. if (x >= tag.X && x < (tag.X+tag.width)) {
  2006. int end;
  2007. end = tag.start + tag.length - 1;
  2008. for (int pos = tag.start-1; pos < end; pos++) {
  2009. // When clicking on a character, we position the cursor to whatever edge
  2010. // of the character the click was closer
  2011. if (x < (line.widths[pos] + ((line.widths[pos+1]-line.widths[pos])/2))) {
  2012. index = pos;
  2013. return tag;
  2014. }
  2015. }
  2016. index=end;
  2017. return tag;
  2018. }
  2019. if (tag.next != null) {
  2020. tag = tag.next;
  2021. } else {
  2022. index = line.text.Length;
  2023. return tag;
  2024. }
  2025. }
  2026. }
  2027. internal void FormatText(Line start_line, int start_pos, Line end_line, int end_pos, Font font, Brush color) {
  2028. Line l;
  2029. // First, format the first line
  2030. LineTag.FormatText(start_line, start_pos, start_line.text.Length - start_pos, font, color);
  2031. // Format last line
  2032. if (end_line != start_line) {
  2033. LineTag.FormatText(end_line, 0, end_pos, font, color);
  2034. }
  2035. // Now all the lines inbetween
  2036. for (int i = start_line.line_no + 1; i < end_line.line_no - 1; i++) {
  2037. l = GetLine(i);
  2038. LineTag.FormatText(l, 0, l.text.Length, font, color);
  2039. }
  2040. }
  2041. internal void RecalculateAlignments() {
  2042. Line line;
  2043. int line_no;
  2044. line_no = 1;
  2045. while (line_no <= lines) {
  2046. line = GetLine(line_no);
  2047. if (line != null && line.alignment != HorizontalAlignment.Left) {
  2048. if (line.alignment == HorizontalAlignment.Center) {
  2049. line.align_shift = (viewport_width - (int)line.widths[line.text.Length]) / 2;
  2050. } else {
  2051. line.align_shift = viewport_width - (int)line.widths[line.text.Length];
  2052. }
  2053. }
  2054. line_no++;
  2055. }
  2056. return;
  2057. }
  2058. /// <summary>Calculate formatting for the whole document</summary>
  2059. internal bool RecalculateDocument(Graphics g) {
  2060. return RecalculateDocument(g, 1, this.lines, false);
  2061. }
  2062. /// <summary>Calculate formatting starting at a certain line</summary>
  2063. internal bool RecalculateDocument(Graphics g, int start) {
  2064. return RecalculateDocument(g, start, this.lines, false);
  2065. }
  2066. /// <summary>Calculate formatting within two given line numbers</summary>
  2067. internal bool RecalculateDocument(Graphics g, int start, int end) {
  2068. return RecalculateDocument(g, start, end, false);
  2069. }
  2070. /// <summary>With optimize on, returns true if line heights changed</summary>
  2071. internal bool RecalculateDocument(Graphics g, int start, int end, bool optimize) {
  2072. Line line;
  2073. int line_no;
  2074. int Y;
  2075. int new_width;
  2076. Y = GetLine(start).Y;
  2077. line_no = start;
  2078. new_width = 0;
  2079. if (optimize) {
  2080. bool changed;
  2081. bool alignment_recalc;
  2082. changed = false;
  2083. alignment_recalc = true;
  2084. while (line_no <= end) {
  2085. line = GetLine(line_no++);
  2086. line.Y = Y;
  2087. if (line.recalc) {
  2088. if (line.RecalculateLine(g, this)) {
  2089. changed = true;
  2090. // If the height changed, all subsequent lines change
  2091. end = this.lines;
  2092. }
  2093. if (line.widths[line.text.Length] > new_width) {
  2094. new_width = (int)line.widths[line.text.Length];
  2095. }
  2096. // Calculate alignment
  2097. if (line.alignment != HorizontalAlignment.Left) {
  2098. if (line.alignment == HorizontalAlignment.Center) {
  2099. line.align_shift = (viewport_width - (int)line.widths[line.text.Length]) / 2;
  2100. } else {
  2101. line.align_shift = viewport_width - (int)line.widths[line.text.Length];
  2102. }
  2103. }
  2104. }
  2105. Y += line.height;
  2106. if (line_no > lines) {
  2107. break;
  2108. }
  2109. }
  2110. if (document_x < new_width) {
  2111. document_x = new_width;
  2112. alignment_recalc = true;
  2113. if (WidthChanged != null) {
  2114. WidthChanged(this, null);
  2115. }
  2116. }
  2117. if (alignment_recalc) {
  2118. RecalculateAlignments();
  2119. }
  2120. line = GetLine(lines);
  2121. if (document_y != line.Y + line.height) {
  2122. document_y = line.Y + line.height;
  2123. if (HeightChanged != null) {
  2124. HeightChanged(this, null);
  2125. }
  2126. }
  2127. return changed;
  2128. } else {
  2129. int shift;
  2130. shift = 0;
  2131. while (line_no <= (end + shift)) {
  2132. line = GetLine(line_no++);
  2133. line.Y = Y;
  2134. shift = this.lines;
  2135. line.RecalculateLine(g, this);
  2136. if (this.lines > shift) {
  2137. shift = this.lines - shift;
  2138. } else {
  2139. shift = 0;
  2140. }
  2141. if (line.widths[line.text.Length] > new_width) {
  2142. new_width = (int)line.widths[line.text.Length];
  2143. }
  2144. // Calculate alignment
  2145. if (line.alignment != HorizontalAlignment.Left) {
  2146. if (line.alignment == HorizontalAlignment.Center) {
  2147. line.align_shift = (viewport_width - (int)line.widths[line.text.Length]) / 2;
  2148. } else {
  2149. line.align_shift = viewport_width - (int)line.widths[line.text.Length];
  2150. }
  2151. }
  2152. Y += line.height;
  2153. if (line_no > lines) {
  2154. break;
  2155. }
  2156. }
  2157. if (document_x != new_width) {
  2158. document_x = new_width;
  2159. if (WidthChanged != null) {
  2160. WidthChanged(this, null);
  2161. }
  2162. }
  2163. RecalculateAlignments();
  2164. line = GetLine(lines);
  2165. if (document_y != line.Y + line.height) {
  2166. document_y = line.Y + line.height;
  2167. if (HeightChanged != null) {
  2168. HeightChanged(this, null);
  2169. }
  2170. }
  2171. return true;
  2172. }
  2173. }
  2174. internal bool SetCursor(int x, int y) {
  2175. return true;
  2176. }
  2177. internal int Size() {
  2178. return lines;
  2179. }
  2180. #endregion // Internal Methods
  2181. #region Events
  2182. internal event EventHandler CaretMoved;
  2183. internal event EventHandler WidthChanged;
  2184. internal event EventHandler HeightChanged;
  2185. #endregion // Events
  2186. #region Administrative
  2187. public IEnumerator GetEnumerator() {
  2188. // FIXME
  2189. return null;
  2190. }
  2191. public override bool Equals(object obj) {
  2192. if (obj == null) {
  2193. return false;
  2194. }
  2195. if (!(obj is Document)) {
  2196. return false;
  2197. }
  2198. if (obj == this) {
  2199. return true;
  2200. }
  2201. if (ToString().Equals(((Document)obj).ToString())) {
  2202. return true;
  2203. }
  2204. return false;
  2205. }
  2206. public override int GetHashCode() {
  2207. return document_id;
  2208. }
  2209. public override string ToString() {
  2210. return "document " + this.document_id;
  2211. }
  2212. #endregion // Administrative
  2213. }
  2214. internal class LineTag {
  2215. #region Local Variables;
  2216. // Payload; formatting
  2217. internal Font font; // System.Drawing.Font object for this tag
  2218. internal Brush color; // System.Drawing.Brush object
  2219. // Payload; text
  2220. internal int start; // start, in chars; index into Line.text
  2221. internal int length; // length, in chars
  2222. internal bool r_to_l; // Which way is the font
  2223. // Drawing support
  2224. internal int height; // Height in pixels of the text this tag describes
  2225. internal int X; // X location of the text this tag describes
  2226. internal float width; // Width in pixels of the text this tag describes
  2227. internal int ascent; // Ascent of the font for this tag
  2228. internal int shift; // Shift down for this tag, to stay on baseline
  2229. // Administrative
  2230. internal Line line; // The line we're on
  2231. internal LineTag next; // Next tag on the same line
  2232. internal LineTag previous; // Previous tag on the same line
  2233. #endregion;
  2234. #region Constructors
  2235. internal LineTag(Line line, int start, int length) {
  2236. this.line = line;
  2237. this.start = start;
  2238. this.length = length;
  2239. this.X = 0;
  2240. this.width = 0;
  2241. }
  2242. #endregion // Constructors
  2243. #region Internal Methods
  2244. /// <summary>Applies 'font' to characters starting at 'start' for 'length' chars;
  2245. /// Removes any previous tags overlapping the same area;
  2246. /// returns true if lineheight has changed</summary>
  2247. internal static bool FormatText(Line line, int start, int length, Font font, Brush color) {
  2248. LineTag tag;
  2249. LineTag start_tag;
  2250. int end;
  2251. bool retval = false; // Assume line-height doesn't change
  2252. // Too simple?
  2253. if (font.Height != line.height) {
  2254. retval = true;
  2255. }
  2256. line.recalc = true; // This forces recalculation of the line in RecalculateDocument
  2257. // A little sanity, not sure if it's needed, might be able to remove for speed
  2258. if (length > line.text.Length) {
  2259. length = line.text.Length;
  2260. }
  2261. tag = line.tags;
  2262. end = start + length;
  2263. // Common special case
  2264. if ((start == 1) && (length == tag.length)) {
  2265. tag.ascent = 0;
  2266. tag.font = font;
  2267. tag.color = color;
  2268. return retval;
  2269. }
  2270. start_tag = FindTag(line, start);
  2271. tag = new LineTag(line, start, length);
  2272. tag.font = font;
  2273. tag.color = color;
  2274. if (start == 1) {
  2275. line.tags = tag;
  2276. }
  2277. if (start_tag.start == start) {
  2278. tag.next = start_tag;
  2279. tag.previous = start_tag.previous;
  2280. if (start_tag.previous != null) {
  2281. start_tag.previous.next = tag;
  2282. }
  2283. start_tag.previous = tag;
  2284. } else {
  2285. // Insert ourselves 'in the middle'
  2286. if ((start_tag.next != null) && (start_tag.next.start < end)) {
  2287. tag.next = start_tag.next;
  2288. } else {
  2289. tag.next = new LineTag(line, start_tag.start, start_tag.length);
  2290. tag.next.font = start_tag.font;
  2291. tag.next.color = start_tag.color;
  2292. if (start_tag.next != null) {
  2293. tag.next.next = start_tag.next;
  2294. tag.next.next.previous = tag.next;
  2295. }
  2296. }
  2297. tag.next.previous = tag;
  2298. start_tag.length = start - start_tag.start;
  2299. tag.previous = start_tag;
  2300. start_tag.next = tag;
  2301. #if nope
  2302. if (tag.next.start > (tag.start + tag.length)) {
  2303. tag.next.length += tag.next.start - (tag.start + tag.length);
  2304. tag.next.start = tag.start + tag.length;
  2305. }
  2306. #endif
  2307. }
  2308. // Elimination loop
  2309. tag = tag.next;
  2310. while ((tag != null) && (tag.start < end)) {
  2311. if ((tag.start + tag.length) <= end) {
  2312. // remove the tag
  2313. tag.previous.next = tag.next;
  2314. if (tag.next != null) {
  2315. tag.next.previous = tag.previous;
  2316. }
  2317. tag = tag.previous;
  2318. } else {
  2319. // Adjust the length of the tag
  2320. tag.length = (tag.start + tag.length) - end;
  2321. tag.start = end;
  2322. }
  2323. tag = tag.next;
  2324. }
  2325. return retval;
  2326. }
  2327. /// <summary>Finds the tag that describes the character at position 'pos' on 'line'</summary>
  2328. internal static LineTag FindTag(Line line, int pos) {
  2329. LineTag tag = line.tags;
  2330. // Beginning of line is a bit special
  2331. if (pos == 0) {
  2332. return tag;
  2333. }
  2334. while (tag != null) {
  2335. if ((tag.start <= pos) && (pos < (tag.start+tag.length))) {
  2336. return tag;
  2337. }
  2338. tag = tag.next;
  2339. }
  2340. return null;
  2341. }
  2342. /// <summary>Combines 'this' tag with 'other' tag</summary>
  2343. internal bool Combine(LineTag other) {
  2344. if (!this.Equals(other)) {
  2345. return false;
  2346. }
  2347. this.width += other.width;
  2348. this.length += other.length;
  2349. this.next = other.next;
  2350. if (this.next != null) {
  2351. this.next.previous = this;
  2352. }
  2353. return true;
  2354. }
  2355. /// <summary>Remove 'this' tag ; to be called when formatting is to be removed</summary>
  2356. internal bool Remove() {
  2357. if ((this.start == 1) && (this.next == null)) {
  2358. // We cannot remove the only tag
  2359. return false;
  2360. }
  2361. if (this.start != 1) {
  2362. this.previous.length += this.length;
  2363. this.previous.width = -1;
  2364. this.previous.next = this.next;
  2365. this.next.previous = this.previous;
  2366. } else {
  2367. this.next.start = 1;
  2368. this.next.length += this.length;
  2369. this.next.width = -1;
  2370. this.line.tags = this.next;
  2371. this.next.previous = null;
  2372. }
  2373. return true;
  2374. }
  2375. /// <summary>Checks if 'this' tag describes the same formatting options as 'obj'</summary>
  2376. public override bool Equals(object obj) {
  2377. LineTag other;
  2378. if (obj == null) {
  2379. return false;
  2380. }
  2381. if (!(obj is LineTag)) {
  2382. return false;
  2383. }
  2384. if (obj == this) {
  2385. return true;
  2386. }
  2387. other = (LineTag)obj;
  2388. if (this.font.Equals(other.font) && this.color.Equals(other.color)) { // FIXME add checking for things like link or type later
  2389. return true;
  2390. }
  2391. return false;
  2392. }
  2393. public override int GetHashCode() {
  2394. return base.GetHashCode ();
  2395. }
  2396. public override string ToString() {
  2397. return "Tag starts at index " + this.start + "length " + this.length + " text: " + this.line.Text.Substring(this.start-1, this.length) + "Font " + this.font.ToString();
  2398. }
  2399. #endregion // Internal Methods
  2400. }
  2401. }