TextControl.cs 79 KB

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