TextControl.cs 122 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892
  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-2006 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. // NOTE:
  34. // selection_start.pos and selection_end.pos are 0-based
  35. // selection_start.pos = first selected char
  36. // selection_end.pos = first NOT-selected char
  37. //
  38. // FormatText methods are 1-based (as are all tags, LineTag.Start is 1 for
  39. // the first character on a line; the reason is that 0 is the position
  40. // *before* the first character on a line
  41. #undef Debug
  42. using System;
  43. using System.Collections;
  44. using System.Drawing;
  45. using System.Drawing.Text;
  46. using System.Text;
  47. namespace System.Windows.Forms {
  48. internal enum LineColor {
  49. Red = 0,
  50. Black = 1
  51. }
  52. internal enum CaretSelection {
  53. Position, // Selection=Caret
  54. Word, // Selection=Word under caret
  55. Line // Selection=Line under caret
  56. }
  57. internal class FontDefinition {
  58. internal String face;
  59. internal int size;
  60. internal FontStyle add_style;
  61. internal FontStyle remove_style;
  62. internal Color color;
  63. internal Font font_obj;
  64. internal FontDefinition() {
  65. face = null;
  66. size = 0;
  67. color = Color.Empty;
  68. }
  69. }
  70. internal enum CaretDirection {
  71. CharForward, // Move a char to the right
  72. CharBack, // Move a char to the left
  73. LineUp, // Move a line up
  74. LineDown, // Move a line down
  75. Home, // Move to the beginning of the line
  76. End, // Move to the end of the line
  77. PgUp, // Move one page up
  78. PgDn, // Move one page down
  79. CtrlPgUp, // Move caret to the first visible char in the viewport
  80. CtrlPgDn, // Move caret to the last visible char in the viewport
  81. CtrlHome, // Move to the beginning of the document
  82. CtrlEnd, // Move to the end of the document
  83. WordBack, // Move to the beginning of the previous word (or beginning of line)
  84. WordForward, // Move to the beginning of the next word (or end of line)
  85. SelectionStart, // Move to the beginning of the current selection
  86. SelectionEnd // Move to the end of the current selection
  87. }
  88. // Being cloneable should allow for nice line and document copies...
  89. internal class Line : ICloneable, IComparable {
  90. #region Local Variables
  91. // Stuff that matters for our line
  92. internal StringBuilder text; // Characters for the line
  93. internal float[] widths; // Width of each character; always one larger than text.Length
  94. internal int space; // Number of elements in text and widths
  95. internal int line_no; // Line number
  96. internal LineTag tags; // Tags describing the text
  97. internal int Y; // Baseline
  98. internal int height; // Height of the line (height of tallest tag)
  99. internal int ascent; // Ascent of the line (ascent of the tallest tag)
  100. internal HorizontalAlignment alignment; // Alignment of the line
  101. internal int align_shift; // Pixel shift caused by the alignment
  102. internal bool soft_break; // Tag is 'broken soft' and continuation from previous line
  103. internal int indent; // Left indent for the first line
  104. internal int hanging_indent; // Hanging indent (left indent for all but the first line)
  105. internal int right_indent; // Right indent for all lines
  106. // Stuff that's important for the tree
  107. internal Line parent; // Our parent line
  108. internal Line left; // Line with smaller line number
  109. internal Line right; // Line with higher line number
  110. internal LineColor color; // We're doing a black/red tree. this is the node color
  111. internal int DEFAULT_TEXT_LEN; //
  112. internal static StringFormat string_format; // For calculating widths/heights
  113. internal bool recalc; // Line changed
  114. #endregion // Local Variables
  115. #region Constructors
  116. internal Line() {
  117. color = LineColor.Red;
  118. left = null;
  119. right = null;
  120. parent = null;
  121. text = null;
  122. recalc = true;
  123. soft_break = false;
  124. alignment = HorizontalAlignment.Left;
  125. if (string_format == null) {
  126. string_format = new StringFormat(StringFormat.GenericTypographic);
  127. string_format.Trimming = StringTrimming.None;
  128. string_format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
  129. }
  130. }
  131. internal Line(int LineNo, string Text, Font font, Brush color) : this() {
  132. space = Text.Length > DEFAULT_TEXT_LEN ? Text.Length+1 : DEFAULT_TEXT_LEN;
  133. text = new StringBuilder(Text, space);
  134. line_no = LineNo;
  135. widths = new float[space + 1];
  136. tags = new LineTag(this, 1, text.Length);
  137. tags.font = font;
  138. tags.color = color;
  139. }
  140. internal Line(int LineNo, string Text, HorizontalAlignment align, Font font, Brush color) : this() {
  141. space = Text.Length > DEFAULT_TEXT_LEN ? Text.Length+1 : DEFAULT_TEXT_LEN;
  142. text = new StringBuilder(Text, space);
  143. line_no = LineNo;
  144. alignment = align;
  145. widths = new float[space + 1];
  146. tags = new LineTag(this, 1, text.Length);
  147. tags.font = font;
  148. tags.color = color;
  149. }
  150. internal Line(int LineNo, string Text, LineTag tag) : this() {
  151. space = Text.Length > DEFAULT_TEXT_LEN ? Text.Length+1 : DEFAULT_TEXT_LEN;
  152. text = new StringBuilder(Text, space);
  153. line_no = LineNo;
  154. widths = new float[space + 1];
  155. tags = tag;
  156. }
  157. #endregion // Constructors
  158. #region Internal Properties
  159. internal int Indent {
  160. get {
  161. return indent;
  162. }
  163. set {
  164. indent = value;
  165. recalc = true;
  166. }
  167. }
  168. internal int HangingIndent {
  169. get {
  170. return hanging_indent;
  171. }
  172. set {
  173. hanging_indent = value;
  174. recalc = true;
  175. }
  176. }
  177. internal int RightIndent {
  178. get {
  179. return right_indent;
  180. }
  181. set {
  182. right_indent = value;
  183. recalc = true;
  184. }
  185. }
  186. internal int Height {
  187. get {
  188. return height;
  189. }
  190. set {
  191. height = value;
  192. }
  193. }
  194. internal int LineNo {
  195. get {
  196. return line_no;
  197. }
  198. set {
  199. line_no = value;
  200. }
  201. }
  202. internal string Text {
  203. get {
  204. return text.ToString();
  205. }
  206. set {
  207. text = new StringBuilder(value, value.Length > DEFAULT_TEXT_LEN ? value.Length : DEFAULT_TEXT_LEN);
  208. }
  209. }
  210. internal HorizontalAlignment Alignment {
  211. get {
  212. return alignment;
  213. }
  214. set {
  215. if (alignment != value) {
  216. alignment = value;
  217. recalc = true;
  218. }
  219. }
  220. }
  221. #if no
  222. internal StringBuilder Text {
  223. get {
  224. return text;
  225. }
  226. set {
  227. text = value;
  228. }
  229. }
  230. #endif
  231. #endregion // Internal Properties
  232. #region Internal Methods
  233. // Make sure we always have enoughs space in text and widths
  234. internal void Grow(int minimum) {
  235. int length;
  236. float[] new_widths;
  237. length = text.Length;
  238. if ((length + minimum) > space) {
  239. // We need to grow; double the size
  240. if ((length + minimum) > (space * 2)) {
  241. new_widths = new float[length + minimum * 2 + 1];
  242. space = length + minimum * 2;
  243. } else {
  244. new_widths = new float[space * 2 + 1];
  245. space *= 2;
  246. }
  247. widths.CopyTo(new_widths, 0);
  248. widths = new_widths;
  249. }
  250. }
  251. internal void Streamline(int lines) {
  252. LineTag current;
  253. LineTag next;
  254. current = this.tags;
  255. next = current.next;
  256. // Catch what the loop below wont; eliminate 0 length
  257. // tags, but only if there are other tags after us
  258. while ((current.length == 0) && (next != null)) {
  259. tags = next;
  260. tags.previous = null;
  261. current = next;
  262. next = current.next;
  263. }
  264. if (next == null) {
  265. return;
  266. }
  267. while (next != null) {
  268. // Take out 0 length tags unless it's the last tag in the document
  269. if (next.length == 0) {
  270. if ((next.next != null) || (line_no != lines)) {
  271. current.next = next.next;
  272. if (current.next != null) {
  273. current.next.previous = current;
  274. }
  275. next = current.next;
  276. continue;
  277. }
  278. }
  279. if (current.Combine(next)) {
  280. next = current.next;
  281. continue;
  282. }
  283. current = current.next;
  284. next = current.next;
  285. }
  286. }
  287. /// <summary> Find the tag on a line based on the character position, pos is 0-based</summary>
  288. internal LineTag FindTag(int pos) {
  289. LineTag tag;
  290. if (pos == 0) {
  291. return tags;
  292. }
  293. tag = this.tags;
  294. if (pos >= text.Length) {
  295. pos = text.Length - 1;
  296. }
  297. while (tag != null) {
  298. if (((tag.start - 1) <= pos) && (pos < (tag.start + tag.length - 1))) {
  299. return tag;
  300. }
  301. tag = tag.next;
  302. }
  303. return null;
  304. }
  305. /// <summary>
  306. /// Recalculate a single line using the same char for every character in the line
  307. /// </summary>
  308. internal bool RecalculatePasswordLine(Graphics g, Document doc) {
  309. LineTag tag;
  310. int pos;
  311. int len;
  312. float w;
  313. bool ret;
  314. int descent;
  315. pos = 0;
  316. len = this.text.Length;
  317. tag = this.tags;
  318. ascent = 0;
  319. tag.shift = 0;
  320. tag.width = 0;
  321. this.recalc = false;
  322. widths[0] = indent;
  323. tag.X = indent;
  324. w = g.MeasureString(doc.password_char, tags.font, 10000, string_format).Width;
  325. if (this.height != (int)tag.font.Height) {
  326. ret = true;
  327. } else {
  328. ret = false;
  329. }
  330. this.height = (int)tag.font.Height;
  331. tag.height = this.height;
  332. XplatUI.GetFontMetrics(g, tag.font, out tag.ascent, out descent);
  333. this.ascent = tag.ascent;
  334. while (pos < len) {
  335. tag.width += w;
  336. pos++;
  337. widths[pos] = widths[pos-1] + w;
  338. }
  339. return ret;
  340. }
  341. /// <summary>
  342. /// Go through all tags on a line and recalculate all size-related values;
  343. /// returns true if lineheight changed
  344. /// </summary>
  345. internal bool RecalculateLine(Graphics g, Document doc) {
  346. LineTag tag;
  347. int pos;
  348. int len;
  349. SizeF size;
  350. float w;
  351. int prev_height;
  352. bool retval;
  353. bool wrapped;
  354. Line line;
  355. int wrap_pos;
  356. float wrap_width;
  357. pos = 0;
  358. len = this.text.Length;
  359. tag = this.tags;
  360. prev_height = this.height; // For drawing optimization calculations
  361. this.height = 0; // Reset line height
  362. this.ascent = 0; // Reset the ascent for the line
  363. tag.shift = 0;
  364. tag.width = 0;
  365. if (this.soft_break) {
  366. widths[0] = hanging_indent;
  367. } else {
  368. widths[0] = indent;
  369. }
  370. this.recalc = false;
  371. retval = false;
  372. wrapped = false;
  373. wrap_pos = 0;
  374. wrap_width = 0;
  375. while (pos < len) {
  376. size = g.MeasureString(this.text.ToString(pos, 1), tag.font, 10000, string_format);
  377. while (tag.length == 0) { // We should always have tags after a tag.length==0 unless len==0
  378. tag.width = 0;
  379. tag.ascent = 0;
  380. if (tag.previous != null) {
  381. tag.X = tag.previous.X;
  382. } else {
  383. tag.X = (int)widths[pos];
  384. }
  385. tag = tag.next;
  386. tag.width = 0;
  387. tag.shift = 0;
  388. }
  389. w = size.Width;
  390. if (Char.IsWhiteSpace(text[pos])) {
  391. wrap_pos = pos + 1;
  392. wrap_width = tag.width + w;
  393. }
  394. if (doc.wrap) {
  395. if ((wrap_pos > 0) && (wrap_pos != len) && (widths[pos] + w) + 27 > (doc.viewport_width - this.right_indent)) {
  396. pos = wrap_pos;
  397. tag.width = wrap_width;
  398. doc.Split(this, tag, pos, true);
  399. len = this.text.Length;
  400. retval = true;
  401. wrapped = true;
  402. }
  403. }
  404. // Contract all soft lines that follow back into our line
  405. if (!wrapped) {
  406. tag.width += w;
  407. pos++;
  408. widths[pos] = widths[pos-1] + w;
  409. if (pos == len) {
  410. line = doc.GetLine(this.line_no + 1);
  411. if ((line != null) && (line.soft_break)) {
  412. // Pull the previous line back into this one
  413. doc.Combine(this.line_no, this.line_no + 1);
  414. len = this.text.Length;
  415. retval = true;
  416. }
  417. }
  418. }
  419. if (pos == (tag.start-1 + tag.length)) {
  420. // We just found the end of our current tag
  421. tag.height = (int)tag.font.Height;
  422. // Check if we're the tallest on the line (so far)
  423. if (tag.height > this.height) {
  424. this.height = tag.height; // Yep; make sure the line knows
  425. }
  426. if (tag.ascent == 0) {
  427. int descent;
  428. XplatUI.GetFontMetrics(g, tag.font, out tag.ascent, out descent);
  429. }
  430. if (tag.ascent > this.ascent) {
  431. LineTag t;
  432. // We have a tag that has a taller ascent than the line;
  433. t = tags;
  434. while (t != tag) {
  435. t.shift = tag.ascent - t.ascent;
  436. t = t.next;
  437. }
  438. // Save on our line
  439. this.ascent = tag.ascent;
  440. } else {
  441. tag.shift = this.ascent - tag.ascent;
  442. }
  443. // Update our horizontal starting pixel position
  444. if (tag.previous == null) {
  445. tag.X = (int)widths[0];
  446. } else {
  447. tag.X = tag.previous.X + (int)tag.previous.width;
  448. }
  449. tag = tag.next;
  450. if (tag != null) {
  451. tag.width = 0;
  452. tag.shift = 0;
  453. wrap_pos = pos;
  454. wrap_width = tag.width;
  455. }
  456. }
  457. }
  458. if (this.height == 0) {
  459. this.height = tags.font.Height;
  460. tag.height = this.height;
  461. }
  462. if (prev_height != this.height) {
  463. retval = true;
  464. }
  465. return retval;
  466. }
  467. #endregion // Internal Methods
  468. #region Administrative
  469. public int CompareTo(object obj) {
  470. if (obj == null) {
  471. return 1;
  472. }
  473. if (! (obj is Line)) {
  474. throw new ArgumentException("Object is not of type Line", "obj");
  475. }
  476. if (line_no < ((Line)obj).line_no) {
  477. return -1;
  478. } else if (line_no > ((Line)obj).line_no) {
  479. return 1;
  480. } else {
  481. return 0;
  482. }
  483. }
  484. public object Clone() {
  485. Line clone;
  486. clone = new Line();
  487. clone.text = text;
  488. if (left != null) {
  489. clone.left = (Line)left.Clone();
  490. }
  491. if (left != null) {
  492. clone.left = (Line)left.Clone();
  493. }
  494. return clone;
  495. }
  496. internal object CloneLine() {
  497. Line clone;
  498. clone = new Line();
  499. clone.text = text;
  500. return clone;
  501. }
  502. public override bool Equals(object obj) {
  503. if (obj == null) {
  504. return false;
  505. }
  506. if (!(obj is Line)) {
  507. return false;
  508. }
  509. if (obj == this) {
  510. return true;
  511. }
  512. if (line_no == ((Line)obj).line_no) {
  513. return true;
  514. }
  515. return false;
  516. }
  517. public override int GetHashCode() {
  518. return base.GetHashCode ();
  519. }
  520. public override string ToString() {
  521. return "Line " + line_no;
  522. }
  523. #endregion // Administrative
  524. }
  525. internal class Document : ICloneable, IEnumerable {
  526. #region Structures
  527. // FIXME - go through code and check for places where
  528. // we do explicit comparisons instead of using the compare overloads
  529. internal struct Marker {
  530. internal Line line;
  531. internal LineTag tag;
  532. internal int pos;
  533. internal int height;
  534. public static bool operator<(Marker lhs, Marker rhs) {
  535. if (lhs.line.line_no < rhs.line.line_no) {
  536. return true;
  537. }
  538. if (lhs.line.line_no == rhs.line.line_no) {
  539. if (lhs.pos < rhs.pos) {
  540. return true;
  541. }
  542. }
  543. return false;
  544. }
  545. public static bool operator>(Marker lhs, Marker rhs) {
  546. if (lhs.line.line_no > rhs.line.line_no) {
  547. return true;
  548. }
  549. if (lhs.line.line_no == rhs.line.line_no) {
  550. if (lhs.pos > rhs.pos) {
  551. return true;
  552. }
  553. }
  554. return false;
  555. }
  556. public static bool operator==(Marker lhs, Marker rhs) {
  557. if ((lhs.line.line_no == rhs.line.line_no) && (lhs.pos == rhs.pos)) {
  558. return true;
  559. }
  560. return false;
  561. }
  562. public static bool operator!=(Marker lhs, Marker rhs) {
  563. if ((lhs.line.line_no != rhs.line.line_no) || (lhs.pos != rhs.pos)) {
  564. return true;
  565. }
  566. return false;
  567. }
  568. public void Combine(Line move_to_line, int move_to_line_length) {
  569. line = move_to_line;
  570. pos += move_to_line_length;
  571. tag = LineTag.FindTag(line, pos);
  572. }
  573. // This is for future use, right now Document.Split does it by hand, with some added shortcut logic
  574. public void Split(Line move_to_line, int split_at) {
  575. line = move_to_line;
  576. pos -= split_at;
  577. tag = LineTag.FindTag(line, pos);
  578. }
  579. public override bool Equals(object obj) {
  580. return this==(Marker)obj;
  581. }
  582. public override int GetHashCode() {
  583. return base.GetHashCode ();
  584. }
  585. public override string ToString() {
  586. return "Marker Line " + line + ", Position " + pos;
  587. }
  588. }
  589. #endregion Structures
  590. #region Local Variables
  591. private Line document;
  592. private int lines;
  593. private Line sentinel;
  594. private Line last_found;
  595. private int document_id;
  596. private Random random = new Random();
  597. internal string password_char;
  598. private StringBuilder password_cache;
  599. private bool calc_pass;
  600. private int char_count;
  601. private bool no_recalc;
  602. private bool recalc_pending;
  603. private int recalc_start;
  604. private int recalc_end;
  605. private bool recalc_optimize;
  606. internal bool multiline;
  607. internal bool wrap;
  608. internal UndoClass undo;
  609. internal Marker caret;
  610. internal Marker selection_start;
  611. internal Marker selection_end;
  612. internal bool selection_visible;
  613. internal Marker selection_anchor;
  614. internal Marker selection_prev;
  615. internal bool selection_end_anchor;
  616. internal int viewport_x;
  617. internal int viewport_y; // The visible area of the document
  618. internal int viewport_width;
  619. internal int viewport_height;
  620. internal int document_x; // Width of the document
  621. internal int document_y; // Height of the document
  622. internal Rectangle invalid;
  623. internal int crlf_size; // 1 or 2, depending on whether we use \r\n or just \n
  624. internal TextBoxBase owner; // Who's owning us?
  625. static internal int caret_width = 1;
  626. static internal int caret_shift = 1;
  627. #endregion // Local Variables
  628. #region Constructors
  629. internal Document(TextBoxBase owner) {
  630. lines = 0;
  631. this.owner = owner;
  632. multiline = true;
  633. password_char = "";
  634. calc_pass = false;
  635. no_recalc = false;
  636. recalc_pending = false;
  637. // Tree related stuff
  638. sentinel = new Line();
  639. sentinel.color = LineColor.Black;
  640. document = sentinel;
  641. last_found = sentinel;
  642. // We always have a blank line
  643. owner.HandleCreated += new EventHandler(owner_HandleCreated);
  644. owner.VisibleChanged += new EventHandler(owner_VisibleChanged);
  645. Add(1, "", owner.Font, ThemeEngine.Current.ResPool.GetSolidBrush(owner.ForeColor));
  646. lines=1;
  647. undo = new UndoClass(this);
  648. selection_visible = false;
  649. selection_start.line = this.document;
  650. selection_start.pos = 0;
  651. selection_start.tag = selection_start.line.tags;
  652. selection_end.line = this.document;
  653. selection_end.pos = 0;
  654. selection_end.tag = selection_end.line.tags;
  655. selection_anchor.line = this.document;
  656. selection_anchor.pos = 0;
  657. selection_anchor.tag = selection_anchor.line.tags;
  658. viewport_x = 0;
  659. viewport_y = 0;
  660. crlf_size = 2;
  661. // Default selection is empty
  662. document_id = random.Next();
  663. }
  664. #endregion
  665. #region Internal Properties
  666. internal Line Root {
  667. get {
  668. return document;
  669. }
  670. set {
  671. document = value;
  672. }
  673. }
  674. internal int Lines {
  675. get {
  676. return lines;
  677. }
  678. }
  679. internal Line CaretLine {
  680. get {
  681. return caret.line;
  682. }
  683. }
  684. internal int CaretPosition {
  685. get {
  686. return caret.pos;
  687. }
  688. }
  689. internal Point Caret {
  690. get {
  691. return new Point((int)caret.tag.line.widths[caret.pos] + caret.line.align_shift, caret.line.Y);
  692. }
  693. }
  694. internal LineTag CaretTag {
  695. get {
  696. return caret.tag;
  697. }
  698. set {
  699. caret.tag = value;
  700. }
  701. }
  702. internal int CRLFSize {
  703. get {
  704. return crlf_size;
  705. }
  706. set {
  707. crlf_size = value;
  708. }
  709. }
  710. internal string PasswordChar {
  711. get {
  712. return password_char;
  713. }
  714. set {
  715. password_char = value;
  716. if ((password_char.Length != 0) && (password_char[0] != '\0')) {
  717. char ch;
  718. calc_pass = true;
  719. ch = value[0];
  720. password_cache = new StringBuilder(1024);
  721. for (int i = 0; i < 1024; i++) {
  722. password_cache.Append(ch);
  723. }
  724. } else {
  725. calc_pass = false;
  726. password_cache = null;
  727. }
  728. }
  729. }
  730. internal int ViewPortX {
  731. get {
  732. return viewport_x;
  733. }
  734. set {
  735. viewport_x = value;
  736. }
  737. }
  738. internal int Length {
  739. get {
  740. return char_count + lines - 1; // Add \n for each line but the last
  741. }
  742. }
  743. private int CharCount {
  744. get {
  745. return char_count;
  746. }
  747. set {
  748. char_count = value;
  749. if (LengthChanged != null) {
  750. LengthChanged(this, EventArgs.Empty);
  751. }
  752. }
  753. }
  754. ///<summary>Setting NoRecalc to true will prevent the document from being recalculated.
  755. ///This ensures that coordinates of added text are predictable after adding the text even with wrapped view</summary>
  756. internal bool NoRecalc {
  757. get {
  758. return no_recalc;
  759. }
  760. set {
  761. no_recalc = value;
  762. if (!no_recalc && recalc_pending) {
  763. RecalculateDocument(owner.CreateGraphicsInternal(), recalc_start, recalc_end, recalc_optimize);
  764. recalc_pending = false;
  765. }
  766. }
  767. }
  768. internal int ViewPortY {
  769. get {
  770. return viewport_y;
  771. }
  772. set {
  773. viewport_y = value;
  774. }
  775. }
  776. internal int ViewPortWidth {
  777. get {
  778. return viewport_width;
  779. }
  780. set {
  781. viewport_width = value;
  782. }
  783. }
  784. internal int ViewPortHeight {
  785. get {
  786. return viewport_height;
  787. }
  788. set {
  789. viewport_height = value;
  790. }
  791. }
  792. internal int Width {
  793. get {
  794. return this.document_x;
  795. }
  796. }
  797. internal int Height {
  798. get {
  799. return this.document_y;
  800. }
  801. }
  802. internal bool SelectionVisible {
  803. get {
  804. return selection_visible;
  805. }
  806. }
  807. internal bool Wrap {
  808. get {
  809. return wrap;
  810. }
  811. set {
  812. wrap = value;
  813. }
  814. }
  815. #endregion // Internal Properties
  816. #region Private Methods
  817. // For debugging
  818. internal int DumpTree(Line line, bool with_tags) {
  819. int total;
  820. total = 1;
  821. Console.Write("Line {0} [# {1}], Y: {1} Text {2}", line.line_no, line.GetHashCode(), line.Y, line.text != null ? line.text.ToString() : "undefined");
  822. if (line.left == sentinel) {
  823. Console.Write(", left = sentinel");
  824. } else if (line.left == null) {
  825. Console.Write(", left = NULL");
  826. }
  827. if (line.right == sentinel) {
  828. Console.Write(", right = sentinel");
  829. } else if (line.right == null) {
  830. Console.Write(", right = NULL");
  831. }
  832. Console.WriteLine("");
  833. if (with_tags) {
  834. LineTag tag;
  835. int count;
  836. int length;
  837. tag = line.tags;
  838. count = 1;
  839. length = 0;
  840. Console.Write(" Tags: ");
  841. while (tag != null) {
  842. Console.Write("{0} <{1}>-<{2}> ", count++, tag.start, tag.length);
  843. length += tag.length;
  844. if (tag.line != line) {
  845. Console.Write("BAD line link");
  846. throw new Exception("Bad line link in tree");
  847. }
  848. tag = tag.next;
  849. if (tag != null) {
  850. Console.Write(", ");
  851. }
  852. }
  853. if (length > line.text.Length) {
  854. throw new Exception(String.Format("Length of tags more than length of text on line (expected {0} calculated {1})", line.text.Length, length));
  855. } else if (length < line.text.Length) {
  856. throw new Exception(String.Format("Length of tags less than length of text on line (expected {0} calculated {1})", line.text.Length, length));
  857. }
  858. Console.WriteLine("");
  859. }
  860. if (line.left != null) {
  861. if (line.left != sentinel) {
  862. total += DumpTree(line.left, with_tags);
  863. }
  864. } else {
  865. if (line != sentinel) {
  866. throw new Exception("Left should not be NULL");
  867. }
  868. }
  869. if (line.right != null) {
  870. if (line.right != sentinel) {
  871. total += DumpTree(line.right, with_tags);
  872. }
  873. } else {
  874. if (line != sentinel) {
  875. throw new Exception("Right should not be NULL");
  876. }
  877. }
  878. for (int i = 1; i <= this.lines; i++) {
  879. if (GetLine(i) == null) {
  880. throw new Exception(String.Format("Hole in line order, missing {0}", i));
  881. }
  882. }
  883. if (line == this.Root) {
  884. if (total < this.lines) {
  885. throw new Exception(String.Format("Not enough nodes in tree, found {0}, expected {1}", total, this.lines));
  886. } else if (total > this.lines) {
  887. throw new Exception(String.Format("Too many nodes in tree, found {0}, expected {1}", total, this.lines));
  888. }
  889. }
  890. return total;
  891. }
  892. private void DecrementLines(int line_no) {
  893. int current;
  894. current = line_no;
  895. while (current <= lines) {
  896. GetLine(current).line_no--;
  897. current++;
  898. }
  899. return;
  900. }
  901. private void IncrementLines(int line_no) {
  902. int current;
  903. current = this.lines;
  904. while (current >= line_no) {
  905. GetLine(current).line_no++;
  906. current--;
  907. }
  908. return;
  909. }
  910. private void RebalanceAfterAdd(Line line1) {
  911. Line line2;
  912. while ((line1 != document) && (line1.parent.color == LineColor.Red)) {
  913. if (line1.parent == line1.parent.parent.left) {
  914. line2 = line1.parent.parent.right;
  915. if ((line2 != null) && (line2.color == LineColor.Red)) {
  916. line1.parent.color = LineColor.Black;
  917. line2.color = LineColor.Black;
  918. line1.parent.parent.color = LineColor.Red;
  919. line1 = line1.parent.parent;
  920. } else {
  921. if (line1 == line1.parent.right) {
  922. line1 = line1.parent;
  923. RotateLeft(line1);
  924. }
  925. line1.parent.color = LineColor.Black;
  926. line1.parent.parent.color = LineColor.Red;
  927. RotateRight(line1.parent.parent);
  928. }
  929. } else {
  930. line2 = line1.parent.parent.left;
  931. if ((line2 != null) && (line2.color == LineColor.Red)) {
  932. line1.parent.color = LineColor.Black;
  933. line2.color = LineColor.Black;
  934. line1.parent.parent.color = LineColor.Red;
  935. line1 = line1.parent.parent;
  936. } else {
  937. if (line1 == line1.parent.left) {
  938. line1 = line1.parent;
  939. RotateRight(line1);
  940. }
  941. line1.parent.color = LineColor.Black;
  942. line1.parent.parent.color = LineColor.Red;
  943. RotateLeft(line1.parent.parent);
  944. }
  945. }
  946. }
  947. document.color = LineColor.Black;
  948. }
  949. private void RebalanceAfterDelete(Line line1) {
  950. Line line2;
  951. while ((line1 != document) && (line1.color == LineColor.Black)) {
  952. if (line1 == line1.parent.left) {
  953. line2 = line1.parent.right;
  954. if (line2.color == LineColor.Red) {
  955. line2.color = LineColor.Black;
  956. line1.parent.color = LineColor.Red;
  957. RotateLeft(line1.parent);
  958. line2 = line1.parent.right;
  959. }
  960. if ((line2.left.color == LineColor.Black) && (line2.right.color == LineColor.Black)) {
  961. line2.color = LineColor.Red;
  962. line1 = line1.parent;
  963. } else {
  964. if (line2.right.color == LineColor.Black) {
  965. line2.left.color = LineColor.Black;
  966. line2.color = LineColor.Red;
  967. RotateRight(line2);
  968. line2 = line1.parent.right;
  969. }
  970. line2.color = line1.parent.color;
  971. line1.parent.color = LineColor.Black;
  972. line2.right.color = LineColor.Black;
  973. RotateLeft(line1.parent);
  974. line1 = document;
  975. }
  976. } else {
  977. line2 = line1.parent.left;
  978. if (line2.color == LineColor.Red) {
  979. line2.color = LineColor.Black;
  980. line1.parent.color = LineColor.Red;
  981. RotateRight(line1.parent);
  982. line2 = line1.parent.left;
  983. }
  984. if ((line2.right.color == LineColor.Black) && (line2.left.color == LineColor.Black)) {
  985. line2.color = LineColor.Red;
  986. line1 = line1.parent;
  987. } else {
  988. if (line2.left.color == LineColor.Black) {
  989. line2.right.color = LineColor.Black;
  990. line2.color = LineColor.Red;
  991. RotateLeft(line2);
  992. line2 = line1.parent.left;
  993. }
  994. line2.color = line1.parent.color;
  995. line1.parent.color = LineColor.Black;
  996. line2.left.color = LineColor.Black;
  997. RotateRight(line1.parent);
  998. line1 = document;
  999. }
  1000. }
  1001. }
  1002. line1.color = LineColor.Black;
  1003. }
  1004. private void RotateLeft(Line line1) {
  1005. Line line2 = line1.right;
  1006. line1.right = line2.left;
  1007. if (line2.left != sentinel) {
  1008. line2.left.parent = line1;
  1009. }
  1010. if (line2 != sentinel) {
  1011. line2.parent = line1.parent;
  1012. }
  1013. if (line1.parent != null) {
  1014. if (line1 == line1.parent.left) {
  1015. line1.parent.left = line2;
  1016. } else {
  1017. line1.parent.right = line2;
  1018. }
  1019. } else {
  1020. document = line2;
  1021. }
  1022. line2.left = line1;
  1023. if (line1 != sentinel) {
  1024. line1.parent = line2;
  1025. }
  1026. }
  1027. private void RotateRight(Line line1) {
  1028. Line line2 = line1.left;
  1029. line1.left = line2.right;
  1030. if (line2.right != sentinel) {
  1031. line2.right.parent = line1;
  1032. }
  1033. if (line2 != sentinel) {
  1034. line2.parent = line1.parent;
  1035. }
  1036. if (line1.parent != null) {
  1037. if (line1 == line1.parent.right) {
  1038. line1.parent.right = line2;
  1039. } else {
  1040. line1.parent.left = line2;
  1041. }
  1042. } else {
  1043. document = line2;
  1044. }
  1045. line2.right = line1;
  1046. if (line1 != sentinel) {
  1047. line1.parent = line2;
  1048. }
  1049. }
  1050. internal void UpdateView(Line line, int pos) {
  1051. if (!owner.IsHandleCreated) {
  1052. return;
  1053. }
  1054. if (no_recalc) {
  1055. recalc_start = line.line_no;
  1056. recalc_end = line.line_no;
  1057. recalc_optimize = true;
  1058. recalc_pending = true;
  1059. return;
  1060. }
  1061. // Optimize invalidation based on Line alignment
  1062. if (RecalculateDocument(owner.CreateGraphicsInternal(), line.line_no, line.line_no, true)) {
  1063. // Lineheight changed, invalidate the rest of the document
  1064. if ((line.Y - viewport_y) >=0 ) {
  1065. // We formatted something that's in view, only draw parts of the screen
  1066. owner.Invalidate(new Rectangle(0, line.Y - viewport_y, viewport_width, owner.Height - line.Y - viewport_y));
  1067. } else {
  1068. // The tag was above the visible area, draw everything
  1069. owner.Invalidate();
  1070. }
  1071. } else {
  1072. switch(line.alignment) {
  1073. case HorizontalAlignment.Left: {
  1074. owner.Invalidate(new Rectangle((int)line.widths[pos] - viewport_x - 1, line.Y - viewport_y, viewport_width, line.height + 1));
  1075. break;
  1076. }
  1077. case HorizontalAlignment.Center: {
  1078. owner.Invalidate(new Rectangle(0, line.Y - viewport_y, viewport_width, line.height + 1));
  1079. break;
  1080. }
  1081. case HorizontalAlignment.Right: {
  1082. owner.Invalidate(new Rectangle(0, line.Y - viewport_y, (int)line.widths[pos + 1] - viewport_x + line.align_shift, line.height + 1));
  1083. break;
  1084. }
  1085. }
  1086. }
  1087. }
  1088. // Update display from line, down line_count lines; pos is unused, but required for the signature
  1089. internal void UpdateView(Line line, int line_count, int pos) {
  1090. if (!owner.IsHandleCreated) {
  1091. return;
  1092. }
  1093. if (no_recalc) {
  1094. recalc_start = line.line_no;
  1095. recalc_end = line.line_no + line_count - 1;
  1096. recalc_optimize = true;
  1097. recalc_pending = true;
  1098. return;
  1099. }
  1100. if (RecalculateDocument(owner.CreateGraphicsInternal(), line.line_no, line.line_no + line_count - 1, true)) {
  1101. // Lineheight changed, invalidate the rest of the document
  1102. if ((line.Y - viewport_y) >=0 ) {
  1103. // We formatted something that's in view, only draw parts of the screen
  1104. //blah Console.WriteLine("TextControl.cs(981) Invalidate called in UpdateView(line, line_count, pos)");
  1105. owner.Invalidate(new Rectangle(0, line.Y - viewport_y, viewport_width, owner.Height - line.Y - viewport_y));
  1106. } else {
  1107. // The tag was above the visible area, draw everything
  1108. //blah Console.WriteLine("TextControl.cs(985) Invalidate called in UpdateView(line, line_count, pos)");
  1109. owner.Invalidate();
  1110. }
  1111. } else {
  1112. Line end_line;
  1113. end_line = GetLine(line.line_no + line_count -1);
  1114. if (end_line == null) {
  1115. end_line = line;
  1116. }
  1117. //blah Console.WriteLine("TextControl.cs(996) Invalidate called in UpdateView(line, line_count, pos)");
  1118. owner.Invalidate(new Rectangle(0 - viewport_x, line.Y - viewport_y, (int)line.widths[line.text.Length], end_line.Y + end_line.height));
  1119. }
  1120. }
  1121. #endregion // Private Methods
  1122. #region Internal Methods
  1123. // Clear the document and reset state
  1124. internal void Empty() {
  1125. document = sentinel;
  1126. last_found = sentinel;
  1127. lines = 0;
  1128. // We always have a blank line
  1129. Add(1, "", owner.Font, ThemeEngine.Current.ResPool.GetSolidBrush(owner.ForeColor));
  1130. this.RecalculateDocument(owner.CreateGraphicsInternal());
  1131. PositionCaret(0, 0);
  1132. selection_visible = false;
  1133. selection_start.line = this.document;
  1134. selection_start.pos = 0;
  1135. selection_start.tag = selection_start.line.tags;
  1136. selection_end.line = this.document;
  1137. selection_end.pos = 0;
  1138. selection_end.tag = selection_end.line.tags;
  1139. char_count = 0;
  1140. viewport_x = 0;
  1141. viewport_y = 0;
  1142. document_x = 0;
  1143. document_y = 0;
  1144. }
  1145. internal void PositionCaret(Line line, int pos) {
  1146. if (!owner.IsHandleCreated) {
  1147. return;
  1148. }
  1149. undo.RecordCursor();
  1150. caret.tag = line.FindTag(pos);
  1151. caret.line = line;
  1152. caret.pos = pos;
  1153. caret.height = caret.tag.height;
  1154. if (owner.Focused) {
  1155. 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 + caret_shift);
  1156. }
  1157. if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
  1158. }
  1159. internal void PositionCaret(int x, int y) {
  1160. if (!owner.IsHandleCreated) {
  1161. return;
  1162. }
  1163. undo.RecordCursor();
  1164. caret.tag = FindCursor(x, y, out caret.pos);
  1165. caret.line = caret.tag.line;
  1166. caret.height = caret.tag.height;
  1167. if (owner.Focused) {
  1168. 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 + caret_shift);
  1169. }
  1170. if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
  1171. }
  1172. internal void CaretHasFocus() {
  1173. if ((caret.tag != null) && owner.IsHandleCreated) {
  1174. XplatUI.CreateCaret(owner.Handle, caret_width, caret.height);
  1175. 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 + caret_shift);
  1176. if (!selection_visible) {
  1177. XplatUI.CaretVisible(owner.Handle, true);
  1178. } else {
  1179. XplatUI.CaretVisible(owner.Handle, false);
  1180. }
  1181. }
  1182. }
  1183. internal void CaretLostFocus() {
  1184. if (!owner.IsHandleCreated) {
  1185. return;
  1186. }
  1187. XplatUI.DestroyCaret(owner.Handle);
  1188. }
  1189. internal void AlignCaret() {
  1190. if (!owner.IsHandleCreated) {
  1191. return;
  1192. }
  1193. undo.RecordCursor();
  1194. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  1195. caret.height = caret.tag.height;
  1196. if (owner.Focused) {
  1197. XplatUI.CreateCaret(owner.Handle, caret_width, caret.height);
  1198. 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 + caret_shift);
  1199. XplatUI.CaretVisible(owner.Handle, true);
  1200. }
  1201. if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
  1202. }
  1203. internal void UpdateCaret() {
  1204. if (!owner.IsHandleCreated || caret.tag == null) {
  1205. return;
  1206. }
  1207. undo.RecordCursor();
  1208. if (caret.tag.height != caret.height) {
  1209. caret.height = caret.tag.height;
  1210. if (owner.Focused) {
  1211. XplatUI.CreateCaret(owner.Handle, caret_width, caret.height);
  1212. }
  1213. }
  1214. if (owner.Focused) {
  1215. 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 + caret_shift);
  1216. XplatUI.CaretVisible(owner.Handle, true);
  1217. }
  1218. if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
  1219. }
  1220. internal void DisplayCaret() {
  1221. if (!owner.IsHandleCreated) {
  1222. return;
  1223. }
  1224. if (owner.Focused) {
  1225. XplatUI.CaretVisible(owner.Handle, true);
  1226. }
  1227. }
  1228. internal void HideCaret() {
  1229. if (!owner.IsHandleCreated) {
  1230. return;
  1231. }
  1232. if (owner.Focused) {
  1233. XplatUI.CaretVisible(owner.Handle, false);
  1234. }
  1235. }
  1236. internal void MoveCaret(CaretDirection direction) {
  1237. // FIXME should we use IsWordSeparator to detect whitespace, instead
  1238. // of looking for actual spaces in the Word move cases?
  1239. switch(direction) {
  1240. case CaretDirection.CharForward: {
  1241. caret.pos++;
  1242. if (caret.pos > caret.line.text.Length) {
  1243. if (multiline) {
  1244. // Go into next line
  1245. if (caret.line.line_no < this.lines) {
  1246. caret.line = GetLine(caret.line.line_no+1);
  1247. caret.pos = 0;
  1248. caret.tag = caret.line.tags;
  1249. } else {
  1250. caret.pos--;
  1251. }
  1252. } else {
  1253. // Single line; we stay where we are
  1254. caret.pos--;
  1255. }
  1256. } else {
  1257. if ((caret.tag.start - 1 + caret.tag.length) < caret.pos) {
  1258. caret.tag = caret.tag.next;
  1259. }
  1260. }
  1261. UpdateCaret();
  1262. return;
  1263. }
  1264. case CaretDirection.CharBack: {
  1265. if (caret.pos > 0) {
  1266. // caret.pos--; // folded into the if below
  1267. if (--caret.pos > 0) {
  1268. if (caret.tag.start > caret.pos) {
  1269. caret.tag = caret.tag.previous;
  1270. }
  1271. }
  1272. } else {
  1273. if (caret.line.line_no > 1) {
  1274. caret.line = GetLine(caret.line.line_no - 1);
  1275. caret.pos = caret.line.text.Length;
  1276. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  1277. }
  1278. }
  1279. UpdateCaret();
  1280. return;
  1281. }
  1282. case CaretDirection.WordForward: {
  1283. int len;
  1284. len = caret.line.text.Length;
  1285. if (caret.pos < len) {
  1286. while ((caret.pos < len) && (caret.line.text[caret.pos] != ' ')) {
  1287. caret.pos++;
  1288. }
  1289. if (caret.pos < len) {
  1290. // Skip any whitespace
  1291. while ((caret.pos < len) && (caret.line.text[caret.pos] == ' ')) {
  1292. caret.pos++;
  1293. }
  1294. }
  1295. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  1296. } else {
  1297. if (caret.line.line_no < this.lines) {
  1298. caret.line = GetLine(caret.line.line_no + 1);
  1299. caret.pos = 0;
  1300. caret.tag = caret.line.tags;
  1301. }
  1302. }
  1303. UpdateCaret();
  1304. return;
  1305. }
  1306. case CaretDirection.WordBack: {
  1307. if (caret.pos > 0) {
  1308. caret.pos--;
  1309. while ((caret.pos > 0) && (caret.line.text[caret.pos] == ' ')) {
  1310. caret.pos--;
  1311. }
  1312. while ((caret.pos > 0) && (caret.line.text[caret.pos] != ' ')) {
  1313. caret.pos--;
  1314. }
  1315. if (caret.line.text.ToString(caret.pos, 1) == " ") {
  1316. if (caret.pos != 0) {
  1317. caret.pos++;
  1318. } else {
  1319. caret.line = GetLine(caret.line.line_no - 1);
  1320. caret.pos = caret.line.text.Length;
  1321. }
  1322. }
  1323. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  1324. } else {
  1325. if (caret.line.line_no > 1) {
  1326. caret.line = GetLine(caret.line.line_no - 1);
  1327. caret.pos = caret.line.text.Length;
  1328. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  1329. }
  1330. }
  1331. UpdateCaret();
  1332. return;
  1333. }
  1334. case CaretDirection.LineUp: {
  1335. if (caret.line.line_no > 1) {
  1336. int pixel;
  1337. pixel = (int)caret.line.widths[caret.pos];
  1338. PositionCaret(pixel, GetLine(caret.line.line_no - 1).Y);
  1339. if (!owner.IsHandleCreated) {
  1340. return;
  1341. }
  1342. XplatUI.CaretVisible(owner.Handle, true);
  1343. }
  1344. return;
  1345. }
  1346. case CaretDirection.LineDown: {
  1347. if (caret.line.line_no < lines) {
  1348. int pixel;
  1349. pixel = (int)caret.line.widths[caret.pos];
  1350. PositionCaret(pixel, GetLine(caret.line.line_no + 1).Y);
  1351. if (!owner.IsHandleCreated) {
  1352. return;
  1353. }
  1354. XplatUI.CaretVisible(owner.Handle, true);
  1355. }
  1356. return;
  1357. }
  1358. case CaretDirection.Home: {
  1359. if (caret.pos > 0) {
  1360. caret.pos = 0;
  1361. caret.tag = caret.line.tags;
  1362. UpdateCaret();
  1363. }
  1364. return;
  1365. }
  1366. case CaretDirection.End: {
  1367. if (caret.pos < caret.line.text.Length) {
  1368. caret.pos = caret.line.text.Length;
  1369. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  1370. UpdateCaret();
  1371. }
  1372. return;
  1373. }
  1374. case CaretDirection.PgUp: {
  1375. int index;
  1376. int new_y;
  1377. Line line;
  1378. if ((viewport_y - viewport_height) < 0) {
  1379. // We're just placing the caret at the end of the document, no scrolling needed
  1380. owner.vscroll.Value = 0;
  1381. line = GetLine(1);
  1382. PositionCaret(line, 0);
  1383. XplatUI.CaretVisible(owner.Handle, true);
  1384. owner.Invalidate();
  1385. return;
  1386. }
  1387. new_y = caret.line.Y - viewport_height;
  1388. if (new_y < 0) {
  1389. line = GetLine(1);
  1390. PositionCaret(line, 0);
  1391. } else {
  1392. line = FindTag((int)caret.line.widths[caret.pos], caret.line.Y - viewport_height, out index, false).line;
  1393. if (caret.pos > 0) {
  1394. PositionCaret(line, index);
  1395. } else {
  1396. PositionCaret(line, 0);
  1397. }
  1398. }
  1399. // Line up to fill line starts
  1400. new_y = viewport_y - viewport_height;
  1401. line = FindTag(0, new_y, out index, false).line;
  1402. if (line != null) {
  1403. owner.vscroll.Value = line.Y;
  1404. } else {
  1405. owner.vscroll.Value = new_y;
  1406. }
  1407. XplatUI.CaretVisible(owner.Handle, true);
  1408. owner.Invalidate();
  1409. return;
  1410. }
  1411. case CaretDirection.PgDn: {
  1412. int index;
  1413. int new_y;
  1414. Line line;
  1415. if ((viewport_y + viewport_height) > document_y) {
  1416. // We're just placing the caret at the end of the document, no scrolling needed
  1417. owner.vscroll.Value = owner.vscroll.Maximum;
  1418. line = GetLine(lines);
  1419. PositionCaret(line, line.Text.Length);
  1420. XplatUI.CaretVisible(owner.Handle, true);
  1421. owner.Invalidate();
  1422. return;
  1423. }
  1424. new_y = caret.line.Y + viewport_height;
  1425. if (new_y > document_y) {
  1426. line = GetLine(lines);
  1427. PositionCaret(line, line.text.Length);
  1428. } else {
  1429. line = FindTag((int)caret.line.widths[caret.pos], caret.line.Y + viewport_height, out index, false).line;
  1430. if (caret.pos > 0) {
  1431. PositionCaret(line, index);
  1432. } else {
  1433. PositionCaret(line, 0);
  1434. }
  1435. }
  1436. // Line up to fill line starts
  1437. new_y = viewport_y + viewport_height;
  1438. line = FindTag(0, new_y, out index, false).line;
  1439. if (line != null) {
  1440. if (line.Y > owner.vscroll.Maximum) {
  1441. owner.vscroll.Value = owner.vscroll.Maximum;
  1442. } else {
  1443. owner.vscroll.Value = line.Y;
  1444. }
  1445. } else {
  1446. owner.vscroll.Value = new_y;
  1447. }
  1448. XplatUI.CaretVisible(owner.Handle, true);
  1449. owner.Invalidate();
  1450. return;
  1451. }
  1452. case CaretDirection.CtrlPgUp: {
  1453. PositionCaret(0, viewport_y);
  1454. if (!owner.IsHandleCreated) {
  1455. return;
  1456. }
  1457. XplatUI.CaretVisible(owner.Handle, true);
  1458. return;
  1459. }
  1460. case CaretDirection.CtrlPgDn: {
  1461. Line line;
  1462. LineTag tag;
  1463. int index;
  1464. tag = FindTag(0, viewport_y + viewport_height, out index, false);
  1465. if (tag.line.line_no > 1) {
  1466. line = GetLine(tag.line.line_no - 1);
  1467. } else {
  1468. line = tag.line;
  1469. }
  1470. PositionCaret(line, line.Text.Length);
  1471. if (!owner.IsHandleCreated) {
  1472. return;
  1473. }
  1474. XplatUI.CaretVisible(owner.Handle, true);
  1475. return;
  1476. }
  1477. case CaretDirection.CtrlHome: {
  1478. caret.line = GetLine(1);
  1479. caret.pos = 0;
  1480. caret.tag = caret.line.tags;
  1481. UpdateCaret();
  1482. return;
  1483. }
  1484. case CaretDirection.CtrlEnd: {
  1485. caret.line = GetLine(lines);
  1486. caret.pos = caret.line.text.Length;
  1487. caret.tag = LineTag.FindTag(caret.line, caret.pos);
  1488. UpdateCaret();
  1489. return;
  1490. }
  1491. case CaretDirection.SelectionStart: {
  1492. caret.line = selection_start.line;
  1493. caret.pos = selection_start.pos;
  1494. caret.tag = selection_start.tag;
  1495. UpdateCaret();
  1496. return;
  1497. }
  1498. case CaretDirection.SelectionEnd: {
  1499. caret.line = selection_end.line;
  1500. caret.pos = selection_end.pos;
  1501. caret.tag = selection_end.tag;
  1502. UpdateCaret();
  1503. return;
  1504. }
  1505. }
  1506. }
  1507. // Draw the document
  1508. internal void Draw(Graphics g, Rectangle clip) {
  1509. Line line; // Current line being drawn
  1510. LineTag tag; // Current tag being drawn
  1511. int start; // First line to draw
  1512. int end; // Last line to draw
  1513. StringBuilder text; // String representing the current line
  1514. int line_no; //
  1515. Brush disabled;
  1516. Brush hilight;
  1517. Brush hilight_text;
  1518. // First, figure out from what line to what line we need to draw
  1519. start = GetLineByPixel(clip.Top + viewport_y, false).line_no;
  1520. end = GetLineByPixel(clip.Bottom + viewport_y, false).line_no;
  1521. //Console.WriteLine("Starting drawing at line {0}, ending at line {1} (clip-bottom:{2})", start, end, clip.Bottom);
  1522. // Now draw our elements; try to only draw those that are visible
  1523. line_no = start;
  1524. #if Debug
  1525. DateTime n = DateTime.Now;
  1526. Console.WriteLine("Started drawing: {0}s {1}ms", n.Second, n.Millisecond);
  1527. #endif
  1528. disabled = ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorGrayText);
  1529. hilight = ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorHighlight);
  1530. hilight_text = ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorHighlightText);
  1531. while (line_no <= end) {
  1532. line = GetLine(line_no);
  1533. #if not
  1534. if (owner.backcolor_set || (owner.Enabled && !owner.read_only)) {
  1535. g.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(owner.BackColor), new Rectangle(clip.Left, line.Y - viewport_y, clip.Width, line.Y - viewport_y + line.Height));
  1536. } else {
  1537. g.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorControl), new Rectangle(clip.Left, line.Y - viewport_y, clip.Width, line.Y - viewport_y + line.Height));
  1538. }
  1539. #endif
  1540. tag = line.tags;
  1541. if (!calc_pass) {
  1542. text = line.text;
  1543. } else {
  1544. // This fails if there's a password > 1024 chars...
  1545. text = this.password_cache;
  1546. }
  1547. while (tag != null) {
  1548. if (tag.length == 0) {
  1549. tag = tag.next;
  1550. continue;
  1551. }
  1552. if (((tag.X + tag.width) > (clip.Left - viewport_x)) || (tag.X < (clip.Right - viewport_x))) {
  1553. // Check for selection
  1554. if ((!selection_visible) || (!owner.ShowSelection) || (line_no < selection_start.line.line_no) || (line_no > selection_end.line.line_no)) {
  1555. // regular drawing, no selection to deal with
  1556. //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);
  1557. if (owner.is_enabled) {
  1558. g.DrawString(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);
  1559. } else {
  1560. Color a;
  1561. Color b;
  1562. a = ((SolidBrush)tag.color).Color;
  1563. b = ThemeEngine.Current.ColorWindowText;
  1564. if ((a.R == b.R) && (a.G == b.G) && (a.B == b.B)) {
  1565. g.DrawString(text.ToString(tag.start-1, tag.length), tag.font, disabled, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
  1566. } else {
  1567. g.DrawString(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);
  1568. }
  1569. }
  1570. } else {
  1571. // we might have to draw our selection
  1572. if ((line_no != selection_start.line.line_no) && (line_no != selection_end.line.line_no)) {
  1573. // Special case, whole line is selected, draw this tag selected
  1574. g.FillRectangle(
  1575. hilight, // Brush
  1576. tag.X + line.align_shift - viewport_x, // X
  1577. line.Y + tag.shift - viewport_y, // Y
  1578. line.widths[tag.start + tag.length - 1], // width
  1579. tag.height // Height
  1580. );
  1581. g.DrawString(
  1582. //s.Substring(tag.start-1, tag.length), // String
  1583. text.ToString(tag.start-1, tag.length), // String
  1584. tag.font, // Font
  1585. hilight_text, // Brush
  1586. tag.X + line.align_shift - viewport_x, // X
  1587. line.Y + tag.shift - viewport_y, // Y
  1588. StringFormat.GenericTypographic);
  1589. } else {
  1590. bool highlight;
  1591. bool partial;
  1592. highlight = false;
  1593. partial = false;
  1594. // One or more, but not all tags on the line are selected
  1595. if ((selection_start.tag == tag) && (selection_end.tag == tag)) {
  1596. // Single tag selected, draw "normalSELECTEDnormal"
  1597. partial = true;
  1598. // First, the regular part
  1599. g.DrawString(
  1600. //s.Substring(tag.start - 1, selection_start.pos - tag.start + 1), // String
  1601. text.ToString(tag.start - 1, selection_start.pos - tag.start + 1), // String
  1602. tag.font, // Font
  1603. tag.color, // Brush
  1604. tag.X + line.align_shift - viewport_x, // X
  1605. line.Y + tag.shift - viewport_y, // Y
  1606. StringFormat.GenericTypographic);
  1607. // Now the highlight
  1608. g.FillRectangle(
  1609. hilight, // Brush
  1610. line.widths[selection_start.pos] + line.align_shift, // X
  1611. line.Y + tag.shift - viewport_y, // Y
  1612. line.widths[selection_end.pos] - line.widths[selection_start.pos], // Width
  1613. tag.height); // Height
  1614. g.DrawString(
  1615. //s.Substring(selection_start.pos, selection_end.pos - selection_start.pos), // String
  1616. text.ToString(selection_start.pos, selection_end.pos - selection_start.pos), // String
  1617. tag.font, // Font
  1618. hilight_text, // Brush
  1619. line.widths[selection_start.pos] + line.align_shift - viewport_x, // X
  1620. line.Y + tag.shift - viewport_y, // Y
  1621. StringFormat.GenericTypographic);
  1622. // And back to the regular
  1623. g.DrawString(
  1624. //s.Substring(selection_end.pos, tag.start + tag.length - selection_end.pos - 1), // String
  1625. text.ToString(selection_end.pos, tag.start + tag.length - selection_end.pos - 1), // String
  1626. tag.font, // Font
  1627. tag.color, // Brush
  1628. line.widths[selection_end.pos] + line.align_shift - viewport_x, // X
  1629. line.Y + tag.shift - viewport_y, // Y
  1630. StringFormat.GenericTypographic);
  1631. } else if (selection_start.tag == tag) {
  1632. partial = true;
  1633. // The highlighted part
  1634. g.FillRectangle(
  1635. hilight,
  1636. line.widths[selection_start.pos] + line.align_shift,
  1637. line.Y + tag.shift - viewport_y,
  1638. line.widths[tag.start + tag.length - 1] - line.widths[selection_start.pos],
  1639. tag.height);
  1640. g.DrawString(
  1641. //s.Substring(selection_start.pos, tag.start + tag.length - selection_start.pos - 1), // String
  1642. text.ToString(selection_start.pos, tag.start + tag.length - selection_start.pos - 1), // String
  1643. tag.font, // Font
  1644. hilight_text, // Brush
  1645. line.widths[selection_start.pos] + line.align_shift - viewport_x, // X
  1646. line.Y + tag.shift - viewport_y, // Y
  1647. StringFormat.GenericTypographic);
  1648. // The regular part
  1649. g.DrawString(
  1650. //s.Substring(tag.start - 1, selection_start.pos - tag.start + 1), // String
  1651. text.ToString(tag.start - 1, selection_start.pos - tag.start + 1), // String
  1652. tag.font, // Font
  1653. tag.color, // Brush
  1654. tag.X + line.align_shift - viewport_x, // X
  1655. line.Y + tag.shift - viewport_y, // Y
  1656. StringFormat.GenericTypographic);
  1657. } else if (selection_end.tag == tag) {
  1658. partial = true;
  1659. // The highlighted part
  1660. g.FillRectangle(
  1661. hilight,
  1662. tag.X + line.align_shift - viewport_x,
  1663. line.Y + tag.shift - viewport_y,
  1664. line.widths[selection_end.pos] - line.widths[tag.start - 1],
  1665. tag.height);
  1666. g.DrawString(
  1667. //s.Substring(tag.start - 1, selection_end.pos - tag.start + 1), // String
  1668. text.ToString(tag.start - 1, selection_end.pos - tag.start + 1), // String
  1669. tag.font, // Font
  1670. hilight_text, // Brush
  1671. tag.X + line.align_shift - viewport_x, // X
  1672. line.Y + tag.shift - viewport_y, // Y
  1673. StringFormat.GenericTypographic);
  1674. // The regular part
  1675. g.DrawString(
  1676. //s.Substring(selection_end.pos, tag.start + tag.length - selection_end.pos - 1), // String
  1677. text.ToString(selection_end.pos, tag.start + tag.length - selection_end.pos - 1), // String
  1678. tag.font, // Font
  1679. tag.color, // Brush
  1680. line.widths[selection_end.pos] + line.align_shift - viewport_x, // X
  1681. line.Y + tag.shift - viewport_y, // Y
  1682. StringFormat.GenericTypographic);
  1683. } else {
  1684. // no partially selected tags here, simple checks...
  1685. if (selection_start.line == line) {
  1686. int begin;
  1687. int stop;
  1688. begin = tag.start - 1;
  1689. stop = tag.start + tag.length - 1;
  1690. if (selection_end.line == line) {
  1691. if ((begin >= selection_start.pos) && (stop < selection_end.pos)) {
  1692. highlight = true;
  1693. }
  1694. } else {
  1695. if (stop > selection_start.pos) {
  1696. highlight = true;
  1697. }
  1698. }
  1699. } else if (selection_end.line == line) {
  1700. if ((tag.start - 1) < selection_end.pos) {
  1701. highlight = true;
  1702. }
  1703. }
  1704. }
  1705. if (!partial) {
  1706. if (highlight) {
  1707. g.FillRectangle(
  1708. hilight,
  1709. tag.X + line.align_shift - viewport_x,
  1710. line.Y + tag.shift - viewport_y,
  1711. line.widths[tag.start + tag.length - 1] - line.widths[tag.start - 1],
  1712. tag.height);
  1713. g.DrawString(
  1714. //s.Substring(tag.start-1, tag.length), // String
  1715. text.ToString(tag.start-1, tag.length), // String
  1716. tag.font, // Font
  1717. hilight_text, // Brush
  1718. tag.X + line.align_shift - viewport_x, // X
  1719. line.Y + tag.shift - viewport_y, // Y
  1720. StringFormat.GenericTypographic);
  1721. } else {
  1722. g.DrawString(
  1723. //s.Substring(tag.start-1, tag.length), // String
  1724. text.ToString(tag.start-1, tag.length), // String
  1725. tag.font, // Font
  1726. tag.color, // Brush
  1727. tag.X + line.align_shift - viewport_x, // X
  1728. line.Y + tag.shift - viewport_y, // Y
  1729. StringFormat.GenericTypographic);
  1730. }
  1731. }
  1732. }
  1733. }
  1734. }
  1735. tag = tag.next;
  1736. }
  1737. line_no++;
  1738. }
  1739. #if Debug
  1740. n = DateTime.Now;
  1741. Console.WriteLine("Finished drawing: {0}s {1}ms", n.Second, n.Millisecond);
  1742. #endif
  1743. }
  1744. internal void Insert(Line line, int pos, string s) {
  1745. Insert(line, null, pos, false, s);
  1746. }
  1747. // Insert multi-line text at the given position; use formatting at insertion point for inserted text
  1748. internal void Insert(Line line, LineTag tag, int pos, bool update_caret, string s) {
  1749. int i;
  1750. int base_line;
  1751. string[] ins;
  1752. int insert_lines;
  1753. // The formatting at the insertion point is used for the inserted text
  1754. if (tag == null) {
  1755. tag = LineTag.FindTag(line, pos);
  1756. }
  1757. base_line = line.line_no;
  1758. ins = s.Split(new char[] {'\n'});
  1759. for (int j = 0; j < ins.Length; j++) {
  1760. if (ins[j].EndsWith("\r")) {
  1761. ins[j] = ins[j].Substring(0, ins[j].Length - 1);
  1762. }
  1763. }
  1764. insert_lines = ins.Length;
  1765. // Bump the text at insertion point a line down if we're inserting more than one line
  1766. if (insert_lines > 1) {
  1767. Split(line, pos);
  1768. // Remainder of start line is now in base_line + 1
  1769. }
  1770. // Insert the first line
  1771. InsertString(tag, pos, ins[0]);
  1772. if (insert_lines > 1) {
  1773. for (i = 1; i < insert_lines; i++) {
  1774. Add(base_line + i, ins[i], line.alignment, tag.font, tag.color);
  1775. }
  1776. if (!s.EndsWith("\n\n")) {
  1777. this.Combine(base_line + insert_lines - 1, base_line + insert_lines);
  1778. }
  1779. }
  1780. UpdateView(line, insert_lines + 1, pos);
  1781. if (update_caret && owner.IsHandleCreated) {
  1782. // Move caret to the end of the inserted text
  1783. if (insert_lines > 1) {
  1784. PositionCaret(GetLine(line.line_no + insert_lines - 1), ins[ins.Length - 1].Length);
  1785. } else {
  1786. PositionCaret(line, pos + ins[0].Length);
  1787. }
  1788. XplatUI.CaretVisible(owner.Handle, true);
  1789. }
  1790. }
  1791. // Inserts a character at the given position
  1792. internal void InsertString(Line line, int pos, string s) {
  1793. InsertString(line.FindTag(pos), pos, s);
  1794. }
  1795. // Inserts a string at the given position
  1796. internal void InsertString(LineTag tag, int pos, string s) {
  1797. Line line;
  1798. int len;
  1799. len = s.Length;
  1800. CharCount += len;
  1801. line = tag.line;
  1802. line.text.Insert(pos, s);
  1803. tag.length += len;
  1804. tag = tag.next;
  1805. while (tag != null) {
  1806. tag.start += len;
  1807. tag = tag.next;
  1808. }
  1809. line.Grow(len);
  1810. line.recalc = true;
  1811. UpdateView(line, pos);
  1812. }
  1813. // Inserts a string at the caret position
  1814. internal void InsertStringAtCaret(string s, bool move_caret) {
  1815. LineTag tag;
  1816. int len;
  1817. len = s.Length;
  1818. CharCount += len;
  1819. caret.line.text.Insert(caret.pos, s);
  1820. caret.tag.length += len;
  1821. if (caret.tag.next != null) {
  1822. tag = caret.tag.next;
  1823. while (tag != null) {
  1824. tag.start += len;
  1825. tag = tag.next;
  1826. }
  1827. }
  1828. caret.line.Grow(len);
  1829. caret.line.recalc = true;
  1830. UpdateView(caret.line, caret.pos);
  1831. if (move_caret) {
  1832. caret.pos += len;
  1833. UpdateCaret();
  1834. }
  1835. }
  1836. // Inserts a character at the given position
  1837. internal void InsertChar(Line line, int pos, char ch) {
  1838. InsertChar(line.FindTag(pos), pos, ch);
  1839. }
  1840. // Inserts a character at the given position
  1841. internal void InsertChar(LineTag tag, int pos, char ch) {
  1842. Line line;
  1843. CharCount++;
  1844. line = tag.line;
  1845. line.text.Insert(pos, ch);
  1846. tag.length++;
  1847. tag = tag.next;
  1848. while (tag != null) {
  1849. tag.start++;
  1850. tag = tag.next;
  1851. }
  1852. line.Grow(1);
  1853. line.recalc = true;
  1854. UpdateView(line, pos);
  1855. }
  1856. // Inserts a character at the current caret position
  1857. internal void InsertCharAtCaret(char ch, bool move_caret) {
  1858. LineTag tag;
  1859. CharCount++;
  1860. caret.line.text.Insert(caret.pos, ch);
  1861. caret.tag.length++;
  1862. if (caret.tag.next != null) {
  1863. tag = caret.tag.next;
  1864. while (tag != null) {
  1865. tag.start++;
  1866. tag = tag.next;
  1867. }
  1868. }
  1869. caret.line.Grow(1);
  1870. caret.line.recalc = true;
  1871. UpdateView(caret.line, caret.pos);
  1872. if (move_caret) {
  1873. caret.pos++;
  1874. UpdateCaret();
  1875. SetSelectionToCaret(true);
  1876. }
  1877. }
  1878. // Deletes n characters at the given position; it will not delete past line limits
  1879. // pos is 0-based
  1880. internal void DeleteChars(LineTag tag, int pos, int count) {
  1881. Line line;
  1882. bool streamline;
  1883. streamline = false;
  1884. line = tag.line;
  1885. CharCount -= count;
  1886. if (pos == line.text.Length) {
  1887. return;
  1888. }
  1889. line.text.Remove(pos, count);
  1890. // Make sure the tag points to the right spot
  1891. while ((tag != null) && (tag.start + tag.length - 1) <= pos) {
  1892. tag = tag.next;
  1893. }
  1894. if (tag == null) {
  1895. return;
  1896. }
  1897. // Check if we're crossing tag boundaries
  1898. if ((pos + count) > (tag.start + tag.length - 1)) {
  1899. int left;
  1900. // We have to delete cross tag boundaries
  1901. streamline = true;
  1902. left = count;
  1903. left -= tag.start + tag.length - pos - 1;
  1904. tag.length -= tag.start + tag.length - pos - 1;
  1905. tag = tag.next;
  1906. while ((tag != null) && (left > 0)) {
  1907. tag.start -= count - left;
  1908. if (tag.length > left) {
  1909. tag.length -= left;
  1910. left = 0;
  1911. } else {
  1912. left -= tag.length;
  1913. tag.length = 0;
  1914. tag = tag.next;
  1915. }
  1916. }
  1917. } else {
  1918. // We got off easy, same tag
  1919. tag.length -= count;
  1920. if (tag.length == 0) {
  1921. streamline = true;
  1922. }
  1923. }
  1924. // Adjust the start point of any tags following
  1925. if (tag != null) {
  1926. tag = tag.next;
  1927. while (tag != null) {
  1928. tag.start -= count;
  1929. tag = tag.next;
  1930. }
  1931. }
  1932. line.recalc = true;
  1933. if (streamline) {
  1934. line.Streamline(lines);
  1935. }
  1936. UpdateView(line, pos);
  1937. }
  1938. // Deletes a character at or after the given position (depending on forward); it will not delete past line limits
  1939. internal void DeleteChar(LineTag tag, int pos, bool forward) {
  1940. Line line;
  1941. bool streamline;
  1942. CharCount--;
  1943. streamline = false;
  1944. line = tag.line;
  1945. if ((pos == 0 && forward == false) || (pos == line.text.Length && forward == true)) {
  1946. return;
  1947. }
  1948. if (forward) {
  1949. line.text.Remove(pos, 1);
  1950. while ((tag != null) && (tag.start + tag.length - 1) <= pos) {
  1951. tag = tag.next;
  1952. }
  1953. if (tag == null) {
  1954. return;
  1955. }
  1956. tag.length--;
  1957. if (tag.length == 0) {
  1958. streamline = true;
  1959. }
  1960. } else {
  1961. pos--;
  1962. line.text.Remove(pos, 1);
  1963. if (pos >= (tag.start - 1)) {
  1964. tag.length--;
  1965. if (tag.length == 0) {
  1966. streamline = true;
  1967. }
  1968. } else if (tag.previous != null) {
  1969. tag.previous.length--;
  1970. if (tag.previous.length == 0) {
  1971. streamline = true;
  1972. }
  1973. }
  1974. }
  1975. tag = tag.next;
  1976. while (tag != null) {
  1977. tag.start--;
  1978. tag = tag.next;
  1979. }
  1980. line.recalc = true;
  1981. if (streamline) {
  1982. line.Streamline(lines);
  1983. }
  1984. UpdateView(line, pos);
  1985. }
  1986. // Combine two lines
  1987. internal void Combine(int FirstLine, int SecondLine) {
  1988. Combine(GetLine(FirstLine), GetLine(SecondLine));
  1989. }
  1990. internal void Combine(Line first, Line second) {
  1991. LineTag last;
  1992. int shift;
  1993. // Combine the two tag chains into one
  1994. last = first.tags;
  1995. while (last.next != null) {
  1996. last = last.next;
  1997. }
  1998. last.next = second.tags;
  1999. last.next.previous = last;
  2000. shift = last.start + last.length - 1;
  2001. // Fix up references within the chain
  2002. last = last.next;
  2003. while (last != null) {
  2004. last.line = first;
  2005. last.start += shift;
  2006. last = last.next;
  2007. }
  2008. // Combine both lines' strings
  2009. first.text.Insert(first.text.Length, second.text.ToString());
  2010. first.Grow(first.text.Length);
  2011. // Remove the reference to our (now combined) tags from the doomed line
  2012. second.tags = null;
  2013. // Renumber lines
  2014. DecrementLines(first.line_no + 2); // first.line_no + 1 will be deleted, so we need to start renumbering one later
  2015. // Mop up
  2016. first.recalc = true;
  2017. first.height = 0; // This forces RecalcDocument/UpdateView to redraw from this line on
  2018. first.Streamline(lines);
  2019. // Update Caret, Selection, etc
  2020. if (caret.line == second) {
  2021. caret.Combine(first, shift);
  2022. }
  2023. if (selection_anchor.line == second) {
  2024. selection_anchor.Combine(first, shift);
  2025. }
  2026. if (selection_start.line == second) {
  2027. selection_start.Combine(first, shift);
  2028. }
  2029. if (selection_end.line == second) {
  2030. selection_end.Combine(first, shift);
  2031. }
  2032. #if Debug
  2033. Line check_first;
  2034. Line check_second;
  2035. check_first = GetLine(first.line_no);
  2036. check_second = GetLine(check_first.line_no + 1);
  2037. Console.WriteLine("Pre-delete: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
  2038. #endif
  2039. this.Delete(second);
  2040. #if Debug
  2041. check_first = GetLine(first.line_no);
  2042. check_second = GetLine(check_first.line_no + 1);
  2043. Console.WriteLine("Post-delete Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
  2044. #endif
  2045. }
  2046. // Split the line at the position into two
  2047. internal void Split(int LineNo, int pos) {
  2048. Line line;
  2049. LineTag tag;
  2050. line = GetLine(LineNo);
  2051. tag = LineTag.FindTag(line, pos);
  2052. Split(line, tag, pos, false);
  2053. }
  2054. internal void Split(Line line, int pos) {
  2055. LineTag tag;
  2056. tag = LineTag.FindTag(line, pos);
  2057. Split(line, tag, pos, false);
  2058. }
  2059. ///<summary>Split line at given tag and position into two lines</summary>
  2060. ///<param name="soft">True if the split should be marked as 'soft', indicating that it can be contracted
  2061. ///if more space becomes available on previous line</param>
  2062. internal void Split(Line line, LineTag tag, int pos, bool soft) {
  2063. LineTag new_tag;
  2064. Line new_line;
  2065. bool move_caret;
  2066. bool move_sel_start;
  2067. bool move_sel_end;
  2068. move_caret = false;
  2069. move_sel_start = false;
  2070. move_sel_end = false;
  2071. // Adjust selection and cursors
  2072. if (soft && (caret.line == line) && (caret.pos >= pos)) {
  2073. move_caret = true;
  2074. }
  2075. if (selection_start.line == line && selection_start.pos > pos) {
  2076. move_sel_start = true;
  2077. }
  2078. if (selection_end.line == line && selection_end.pos > pos) {
  2079. move_sel_end = true;
  2080. }
  2081. // cover the easy case first
  2082. if (pos == line.text.Length) {
  2083. Add(line.line_no + 1, "", line.alignment, tag.font, tag.color);
  2084. new_line = GetLine(line.line_no + 1);
  2085. if (soft) {
  2086. if (move_caret) {
  2087. caret.line = new_line;
  2088. caret.line.soft_break = true;
  2089. caret.tag = new_line.tags;
  2090. caret.pos = 0;
  2091. } else {
  2092. new_line.soft_break = true;
  2093. }
  2094. }
  2095. if (move_sel_start) {
  2096. selection_start.line = new_line;
  2097. selection_start.pos = 0;
  2098. selection_start.tag = new_line.tags;
  2099. }
  2100. if (move_sel_end) {
  2101. selection_end.line = new_line;
  2102. selection_end.pos = 0;
  2103. selection_end.tag = new_line.tags;
  2104. }
  2105. return;
  2106. }
  2107. // We need to move the rest of the text into the new line
  2108. Add(line.line_no + 1, line.text.ToString(pos, line.text.Length - pos), line.alignment, tag.font, tag.color);
  2109. // Now transfer our tags from this line to the next
  2110. new_line = GetLine(line.line_no + 1);
  2111. line.recalc = true;
  2112. new_line.recalc = true;
  2113. if ((tag.start - 1) == pos) {
  2114. int shift;
  2115. // We can simply break the chain and move the tag into the next line
  2116. if (tag == line.tags) {
  2117. new_tag = new LineTag(line, 1, 0);
  2118. new_tag.font = tag.font;
  2119. new_tag.color = tag.color;
  2120. line.tags = new_tag;
  2121. }
  2122. if (tag.previous != null) {
  2123. tag.previous.next = null;
  2124. }
  2125. new_line.tags = tag;
  2126. tag.previous = null;
  2127. tag.line = new_line;
  2128. // Walk the list and correct the start location of the tags we just bumped into the next line
  2129. shift = tag.start - 1;
  2130. new_tag = tag;
  2131. while (new_tag != null) {
  2132. new_tag.start -= shift;
  2133. new_tag.line = new_line;
  2134. new_tag = new_tag.next;
  2135. }
  2136. } else {
  2137. int shift;
  2138. new_tag = new LineTag(new_line, 1, tag.start - 1 + tag.length - pos);
  2139. new_tag.next = tag.next;
  2140. new_tag.font = tag.font;
  2141. new_tag.color = tag.color;
  2142. new_line.tags = new_tag;
  2143. if (new_tag.next != null) {
  2144. new_tag.next.previous = new_tag;
  2145. }
  2146. tag.next = null;
  2147. tag.length = pos - tag.start + 1;
  2148. shift = pos;
  2149. new_tag = new_tag.next;
  2150. while (new_tag != null) {
  2151. new_tag.start -= shift;
  2152. new_tag.line = new_line;
  2153. new_tag = new_tag.next;
  2154. }
  2155. }
  2156. if (soft) {
  2157. if (move_caret) {
  2158. caret.line = new_line;
  2159. caret.pos = caret.pos - pos;
  2160. caret.tag = caret.line.FindTag(caret.pos);
  2161. }
  2162. new_line.soft_break = true;
  2163. }
  2164. if (move_sel_start) {
  2165. selection_start.line = new_line;
  2166. selection_start.pos = selection_start.pos - pos;
  2167. selection_start.tag = new_line.FindTag(selection_start.pos);
  2168. }
  2169. if (move_sel_end) {
  2170. selection_end.line = new_line;
  2171. selection_end.pos = selection_end.pos - pos;
  2172. selection_end.tag = new_line.FindTag(selection_end.pos);
  2173. }
  2174. CharCount -= line.text.Length - pos;
  2175. line.text.Remove(pos, line.text.Length - pos);
  2176. }
  2177. // Adds a line of text, with given font.
  2178. // Bumps any line at that line number that already exists down
  2179. internal void Add(int LineNo, string Text, Font font, Brush color) {
  2180. Add(LineNo, Text, HorizontalAlignment.Left, font, color);
  2181. }
  2182. internal void Add(int LineNo, string Text, HorizontalAlignment align, Font font, Brush color) {
  2183. Line add;
  2184. Line line;
  2185. int line_no;
  2186. CharCount += Text.Length;
  2187. if (LineNo<1 || Text == null) {
  2188. if (LineNo<1) {
  2189. throw new ArgumentNullException("LineNo", "Line numbers must be positive");
  2190. } else {
  2191. throw new ArgumentNullException("Text", "Cannot insert NULL line");
  2192. }
  2193. }
  2194. add = new Line(LineNo, Text, align, font, color);
  2195. line = document;
  2196. while (line != sentinel) {
  2197. add.parent = line;
  2198. line_no = line.line_no;
  2199. if (LineNo > line_no) {
  2200. line = line.right;
  2201. } else if (LineNo < line_no) {
  2202. line = line.left;
  2203. } else {
  2204. // Bump existing line numbers; walk all nodes to the right of this one and increment line_no
  2205. IncrementLines(line.line_no);
  2206. line = line.left;
  2207. }
  2208. }
  2209. add.left = sentinel;
  2210. add.right = sentinel;
  2211. if (add.parent != null) {
  2212. if (LineNo > add.parent.line_no) {
  2213. add.parent.right = add;
  2214. } else {
  2215. add.parent.left = add;
  2216. }
  2217. } else {
  2218. // Root node
  2219. document = add;
  2220. }
  2221. RebalanceAfterAdd(add);
  2222. lines++;
  2223. }
  2224. internal virtual void Clear() {
  2225. lines = 0;
  2226. CharCount = 0;
  2227. document = sentinel;
  2228. }
  2229. public virtual object Clone() {
  2230. Document clone;
  2231. clone = new Document(null);
  2232. clone.lines = this.lines;
  2233. clone.document = (Line)document.Clone();
  2234. return clone;
  2235. }
  2236. internal void Delete(int LineNo) {
  2237. Line line;
  2238. if (LineNo>lines) {
  2239. return;
  2240. }
  2241. line = GetLine(LineNo);
  2242. CharCount -= line.text.Length;
  2243. DecrementLines(LineNo + 1);
  2244. Delete(line);
  2245. }
  2246. internal void Delete(Line line1) {
  2247. Line line2;// = new Line();
  2248. Line line3;
  2249. if ((line1.left == sentinel) || (line1.right == sentinel)) {
  2250. line3 = line1;
  2251. } else {
  2252. line3 = line1.right;
  2253. while (line3.left != sentinel) {
  2254. line3 = line3.left;
  2255. }
  2256. }
  2257. if (line3.left != sentinel) {
  2258. line2 = line3.left;
  2259. } else {
  2260. line2 = line3.right;
  2261. }
  2262. line2.parent = line3.parent;
  2263. if (line3.parent != null) {
  2264. if(line3 == line3.parent.left) {
  2265. line3.parent.left = line2;
  2266. } else {
  2267. line3.parent.right = line2;
  2268. }
  2269. } else {
  2270. document = line2;
  2271. }
  2272. if (line3 != line1) {
  2273. LineTag tag;
  2274. if (selection_start.line == line3) {
  2275. selection_start.line = line1;
  2276. }
  2277. if (selection_end.line == line3) {
  2278. selection_end.line = line1;
  2279. }
  2280. if (selection_anchor.line == line3) {
  2281. selection_anchor.line = line1;
  2282. }
  2283. if (caret.line == line3) {
  2284. caret.line = line1;
  2285. }
  2286. line1.alignment = line3.alignment;
  2287. line1.ascent = line3.ascent;
  2288. line1.hanging_indent = line3.hanging_indent;
  2289. line1.height = line3.height;
  2290. line1.indent = line3.indent;
  2291. line1.line_no = line3.line_no;
  2292. line1.recalc = line3.recalc;
  2293. line1.right_indent = line3.right_indent;
  2294. line1.soft_break = line3.soft_break;
  2295. line1.space = line3.space;
  2296. line1.tags = line3.tags;
  2297. line1.text = line3.text;
  2298. line1.widths = line3.widths;
  2299. line1.Y = line3.Y;
  2300. tag = line1.tags;
  2301. while (tag != null) {
  2302. tag.line = line1;
  2303. tag = tag.next;
  2304. }
  2305. }
  2306. if (line3.color == LineColor.Black)
  2307. RebalanceAfterDelete(line2);
  2308. this.lines--;
  2309. last_found = sentinel;
  2310. }
  2311. // Invalidate a section of the document to trigger redraw
  2312. internal void Invalidate(Line start, int start_pos, Line end, int end_pos) {
  2313. Line l1;
  2314. Line l2;
  2315. int p1;
  2316. int p2;
  2317. if ((start == end) && (start_pos == end_pos)) {
  2318. return;
  2319. }
  2320. if (end_pos == -1) {
  2321. end_pos = end.text.Length;
  2322. }
  2323. // figure out what's before what so the logic below is straightforward
  2324. if (start.line_no < end.line_no) {
  2325. l1 = start;
  2326. p1 = start_pos;
  2327. l2 = end;
  2328. p2 = end_pos;
  2329. } else if (start.line_no > end.line_no) {
  2330. l1 = end;
  2331. p1 = end_pos;
  2332. l2 = start;
  2333. p2 = start_pos;
  2334. } else {
  2335. if (start_pos < end_pos) {
  2336. l1 = start;
  2337. p1 = start_pos;
  2338. l2 = end;
  2339. p2 = end_pos;
  2340. } else {
  2341. l1 = end;
  2342. p1 = end_pos;
  2343. l2 = start;
  2344. p2 = start_pos;
  2345. }
  2346. #if Debug
  2347. Console.WriteLine("Invaliding from {0}:{1} to {2}:{3}", l1.line_no, p1, l2.line_no, p2);
  2348. #endif
  2349. owner.Invalidate(
  2350. new Rectangle(
  2351. (int)l1.widths[p1] + l1.align_shift - viewport_x,
  2352. l1.Y - viewport_y,
  2353. (int)l2.widths[p2] - (int)l1.widths[p1] + 1,
  2354. l1.height
  2355. )
  2356. );
  2357. return;
  2358. }
  2359. #if Debug
  2360. Console.WriteLine("Invaliding from {0}:{1} to {2}:{3} Start => x={4}, y={5}, {6}x{7}", l1.line_no, p1, l2.line_no, p2, (int)l1.widths[p1] + l1.align_shift - viewport_x, l1.Y - viewport_y, viewport_width, l1.height);
  2361. #endif
  2362. // Three invalidates:
  2363. // First line from start
  2364. owner.Invalidate(new Rectangle((int)l1.widths[p1] + l1.align_shift - viewport_x, l1.Y - viewport_y, viewport_width, l1.height));
  2365. // lines inbetween
  2366. if ((l1.line_no + 1) < l2.line_no) {
  2367. int y;
  2368. y = GetLine(l1.line_no + 1).Y;
  2369. owner.Invalidate(new Rectangle(0, y - viewport_y, viewport_width, GetLine(l2.line_no).Y - y - viewport_y));
  2370. #if Debug
  2371. Console.WriteLine("Invaliding from {0}:{1} to {2}:{3} Middle => x={4}, y={5}, {6}x{7}", l1.line_no, p1, l2.line_no, p2, 0, y - viewport_y, viewport_width, GetLine(l2.line_no).Y - y - viewport_y);
  2372. #endif
  2373. }
  2374. // Last line to end
  2375. owner.Invalidate(new Rectangle((int)l2.widths[0] + l2.align_shift - viewport_x, l2.Y - viewport_y, (int)l2.widths[p2] + 1, l2.height));
  2376. #if Debug
  2377. Console.WriteLine("Invaliding from {0}:{1} to {2}:{3} End => x={4}, y={5}, {6}x{7}", l1.line_no, p1, l2.line_no, p2, (int)l2.widths[0] + l2.align_shift - viewport_x, l2.Y - viewport_y, (int)l2.widths[p2] + 1, l2.height);
  2378. #endif
  2379. }
  2380. /// <summary>Select text around caret</summary>
  2381. internal void ExpandSelection(CaretSelection mode, bool to_caret) {
  2382. if (to_caret) {
  2383. // We're expanding the selection to the caret position
  2384. switch(mode) {
  2385. case CaretSelection.Line: {
  2386. // Invalidate the selection delta
  2387. if (caret > selection_prev) {
  2388. Invalidate(selection_prev.line, 0, caret.line, caret.line.text.Length);
  2389. } else {
  2390. Invalidate(selection_prev.line, selection_prev.line.text.Length, caret.line, 0);
  2391. }
  2392. if (caret.line.line_no <= selection_anchor.line.line_no) {
  2393. selection_start.line = caret.line;
  2394. selection_start.tag = caret.line.tags;
  2395. selection_start.pos = 0;
  2396. selection_end.line = selection_anchor.line;
  2397. selection_end.tag = selection_anchor.tag;
  2398. selection_end.pos = selection_anchor.pos;
  2399. selection_end_anchor = true;
  2400. } else {
  2401. selection_start.line = selection_anchor.line;
  2402. selection_start.pos = selection_anchor.height;
  2403. selection_start.tag = selection_anchor.line.FindTag(selection_anchor.height);
  2404. selection_end.line = caret.line;
  2405. selection_end.tag = caret.line.tags;
  2406. selection_end.pos = caret.line.text.Length;
  2407. selection_end_anchor = false;
  2408. }
  2409. selection_prev.line = caret.line;
  2410. selection_prev.tag = caret.tag;
  2411. selection_prev.pos = caret.pos;
  2412. break;
  2413. }
  2414. case CaretSelection.Word: {
  2415. int start_pos;
  2416. int end_pos;
  2417. start_pos = FindWordSeparator(caret.line, caret.pos, false);
  2418. end_pos = FindWordSeparator(caret.line, caret.pos, true);
  2419. // Invalidate the selection delta
  2420. if (caret > selection_prev) {
  2421. Invalidate(selection_prev.line, selection_prev.pos, caret.line, end_pos);
  2422. } else {
  2423. Invalidate(selection_prev.line, selection_prev.pos, caret.line, start_pos);
  2424. }
  2425. if (caret < selection_anchor) {
  2426. selection_start.line = caret.line;
  2427. selection_start.tag = caret.line.FindTag(start_pos);
  2428. selection_start.pos = start_pos;
  2429. selection_end.line = selection_anchor.line;
  2430. selection_end.tag = selection_anchor.tag;
  2431. selection_end.pos = selection_anchor.pos;
  2432. selection_prev.line = caret.line;
  2433. selection_prev.tag = caret.tag;
  2434. selection_prev.pos = start_pos;
  2435. selection_end_anchor = true;
  2436. } else {
  2437. selection_start.line = selection_anchor.line;
  2438. selection_start.pos = selection_anchor.height;
  2439. selection_start.tag = selection_anchor.line.FindTag(selection_anchor.height);
  2440. selection_end.line = caret.line;
  2441. selection_end.tag = caret.line.FindTag(end_pos);
  2442. selection_end.pos = end_pos;
  2443. selection_prev.line = caret.line;
  2444. selection_prev.tag = caret.tag;
  2445. selection_prev.pos = end_pos;
  2446. selection_end_anchor = false;
  2447. }
  2448. break;
  2449. }
  2450. case CaretSelection.Position: {
  2451. SetSelectionToCaret(false);
  2452. return;
  2453. }
  2454. }
  2455. } else {
  2456. // We're setting the selection 'around' the caret position
  2457. switch(mode) {
  2458. case CaretSelection.Line: {
  2459. this.Invalidate(caret.line, 0, caret.line, caret.line.text.Length);
  2460. selection_start.line = caret.line;
  2461. selection_start.tag = caret.line.tags;
  2462. selection_start.pos = 0;
  2463. selection_end.line = caret.line;
  2464. selection_end.pos = caret.line.text.Length;
  2465. selection_end.tag = caret.line.FindTag(selection_end.pos);
  2466. selection_anchor.line = selection_end.line;
  2467. selection_anchor.tag = selection_end.tag;
  2468. selection_anchor.pos = selection_end.pos;
  2469. selection_anchor.height = 0;
  2470. selection_prev.line = caret.line;
  2471. selection_prev.tag = caret.tag;
  2472. selection_prev.pos = caret.pos;
  2473. this.selection_end_anchor = true;
  2474. break;
  2475. }
  2476. case CaretSelection.Word: {
  2477. int start_pos;
  2478. int end_pos;
  2479. start_pos = FindWordSeparator(caret.line, caret.pos, false);
  2480. end_pos = FindWordSeparator(caret.line, caret.pos, true);
  2481. this.Invalidate(selection_start.line, start_pos, caret.line, end_pos);
  2482. selection_start.line = caret.line;
  2483. selection_start.tag = caret.line.FindTag(start_pos);
  2484. selection_start.pos = start_pos;
  2485. selection_end.line = caret.line;
  2486. selection_end.tag = caret.line.FindTag(end_pos);
  2487. selection_end.pos = end_pos;
  2488. selection_anchor.line = selection_end.line;
  2489. selection_anchor.tag = selection_end.tag;
  2490. selection_anchor.pos = selection_end.pos;
  2491. selection_anchor.height = start_pos;
  2492. selection_prev.line = caret.line;
  2493. selection_prev.tag = caret.tag;
  2494. selection_prev.pos = caret.pos;
  2495. this.selection_end_anchor = true;
  2496. break;
  2497. }
  2498. }
  2499. }
  2500. if (selection_start == selection_end) {
  2501. selection_visible = false;
  2502. } else {
  2503. selection_visible = true;
  2504. }
  2505. }
  2506. internal void SetSelectionToCaret(bool start) {
  2507. if (start) {
  2508. // Invalidate old selection; selection is being reset to empty
  2509. this.Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
  2510. selection_start.line = caret.line;
  2511. selection_start.tag = caret.tag;
  2512. selection_start.pos = caret.pos;
  2513. // start always also selects end
  2514. selection_end.line = caret.line;
  2515. selection_end.tag = caret.tag;
  2516. selection_end.pos = caret.pos;
  2517. selection_anchor.line = caret.line;
  2518. selection_anchor.tag = caret.tag;
  2519. selection_anchor.pos = caret.pos;
  2520. } else {
  2521. // Invalidate from previous end to caret (aka new end)
  2522. if (selection_end_anchor) {
  2523. if (selection_start != caret) {
  2524. this.Invalidate(selection_start.line, selection_start.pos, caret.line, caret.pos);
  2525. }
  2526. } else {
  2527. if (selection_end != caret) {
  2528. this.Invalidate(selection_end.line, selection_end.pos, caret.line, caret.pos);
  2529. }
  2530. }
  2531. if (caret < selection_anchor) {
  2532. selection_start.line = caret.line;
  2533. selection_start.tag = caret.tag;
  2534. selection_start.pos = caret.pos;
  2535. selection_end.line = selection_anchor.line;
  2536. selection_end.tag = selection_anchor.tag;
  2537. selection_end.pos = selection_anchor.pos;
  2538. selection_end_anchor = true;
  2539. } else {
  2540. selection_start.line = selection_anchor.line;
  2541. selection_start.tag = selection_anchor.tag;
  2542. selection_start.pos = selection_anchor.pos;
  2543. selection_end.line = caret.line;
  2544. selection_end.tag = caret.tag;
  2545. selection_end.pos = caret.pos;
  2546. selection_end_anchor = false;
  2547. }
  2548. }
  2549. if (selection_start == selection_end) {
  2550. selection_visible = false;
  2551. } else {
  2552. selection_visible = true;
  2553. }
  2554. }
  2555. internal void SetSelection(Line start, int start_pos, Line end, int end_pos) {
  2556. if (selection_visible) {
  2557. Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
  2558. }
  2559. if ((end.line_no < start.line_no) || ((end == start) && (end_pos <= start_pos))) {
  2560. selection_start.line = end;
  2561. selection_start.tag = LineTag.FindTag(end, end_pos);
  2562. selection_start.pos = end_pos;
  2563. selection_end.line = start;
  2564. selection_end.tag = LineTag.FindTag(start, start_pos);
  2565. selection_end.pos = start_pos;
  2566. selection_end_anchor = true;
  2567. } else {
  2568. selection_start.line = start;
  2569. selection_start.tag = LineTag.FindTag(start, start_pos);
  2570. selection_start.pos = start_pos;
  2571. selection_end.line = end;
  2572. selection_end.tag = LineTag.FindTag(end, end_pos);
  2573. selection_end.pos = end_pos;
  2574. selection_end_anchor = false;
  2575. }
  2576. selection_anchor.line = start;
  2577. selection_anchor.tag = selection_start.tag;
  2578. selection_anchor.pos = start_pos;
  2579. if (((start == end) && (start_pos == end_pos)) || start == null || end == null) {
  2580. selection_visible = false;
  2581. } else {
  2582. selection_visible = true;
  2583. }
  2584. }
  2585. internal void SetSelectionStart(Line start, int start_pos) {
  2586. // Invalidate from the previous to the new start pos
  2587. Invalidate(selection_start.line, selection_start.pos, start, start_pos);
  2588. selection_start.line = start;
  2589. selection_start.pos = start_pos;
  2590. selection_start.tag = LineTag.FindTag(start, start_pos);
  2591. selection_anchor.line = start;
  2592. selection_anchor.pos = start_pos;
  2593. selection_anchor.tag = selection_start.tag;
  2594. selection_end_anchor = false;
  2595. if ((selection_end.line != selection_start.line) || (selection_end.pos != selection_start.pos)) {
  2596. selection_visible = true;
  2597. // This could be calculated better
  2598. Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
  2599. }
  2600. }
  2601. internal void SetSelectionStart(int character_index) {
  2602. Line line;
  2603. LineTag tag;
  2604. int pos;
  2605. if (character_index < 0) {
  2606. return;
  2607. }
  2608. CharIndexToLineTag(character_index, out line, out tag, out pos);
  2609. SetSelectionStart(line, pos);
  2610. }
  2611. internal void SetSelectionEnd(Line end, int end_pos) {
  2612. if ((end.line_no < selection_anchor.line.line_no) || ((end == selection_anchor.line) && (end_pos <= selection_anchor.pos))) {
  2613. selection_start.line = end;
  2614. selection_start.tag = LineTag.FindTag(end, end_pos);
  2615. selection_start.pos = end_pos;
  2616. selection_end.line = selection_anchor.line;
  2617. selection_end.tag = selection_anchor.tag;
  2618. selection_end.pos = selection_anchor.pos;
  2619. selection_end_anchor = true;
  2620. } else {
  2621. selection_start.line = selection_anchor.line;
  2622. selection_start.tag = selection_anchor.tag;
  2623. selection_start.pos = selection_anchor.pos;
  2624. selection_end.line = end;
  2625. selection_end.tag = LineTag.FindTag(end, end_pos);
  2626. selection_end.pos = end_pos;
  2627. selection_end_anchor = false;
  2628. }
  2629. if ((selection_end.line != selection_start.line) || (selection_end.pos != selection_start.pos)) {
  2630. selection_visible = true;
  2631. Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
  2632. }
  2633. }
  2634. internal void SetSelectionEnd(int character_index) {
  2635. Line line;
  2636. LineTag tag;
  2637. int pos;
  2638. if (character_index < 0) {
  2639. return;
  2640. }
  2641. CharIndexToLineTag(character_index, out line, out tag, out pos);
  2642. SetSelectionEnd(line, pos);
  2643. }
  2644. internal void SetSelection(Line start, int start_pos) {
  2645. if (selection_visible) {
  2646. Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
  2647. }
  2648. selection_start.line = start;
  2649. selection_start.pos = start_pos;
  2650. selection_start.tag = LineTag.FindTag(start, start_pos);
  2651. selection_end.line = start;
  2652. selection_end.tag = selection_start.tag;
  2653. selection_end.pos = start_pos;
  2654. selection_anchor.line = start;
  2655. selection_anchor.tag = selection_start.tag;
  2656. selection_anchor.pos = start_pos;
  2657. selection_end_anchor = false;
  2658. selection_visible = false;
  2659. }
  2660. internal void InvalidateSelectionArea() {
  2661. Invalidate (selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
  2662. }
  2663. // Return the current selection, as string
  2664. internal string GetSelection() {
  2665. // We return String.Empty if there is no selection
  2666. if ((selection_start.pos == selection_end.pos) && (selection_start.line == selection_end.line)) {
  2667. return string.Empty;
  2668. }
  2669. if (!multiline || (selection_start.line == selection_end.line)) {
  2670. return selection_start.line.text.ToString(selection_start.pos, selection_end.pos - selection_start.pos);
  2671. } else {
  2672. StringBuilder sb;
  2673. int i;
  2674. int start;
  2675. int end;
  2676. sb = new StringBuilder();
  2677. start = selection_start.line.line_no;
  2678. end = selection_end.line.line_no;
  2679. sb.Append(selection_start.line.text.ToString(selection_start.pos, selection_start.line.text.Length - selection_start.pos) + Environment.NewLine);
  2680. if ((start + 1) < end) {
  2681. for (i = start + 1; i < end; i++) {
  2682. sb.Append(GetLine(i).text.ToString() + Environment.NewLine);
  2683. }
  2684. }
  2685. sb.Append(selection_end.line.text.ToString(0, selection_end.pos));
  2686. return sb.ToString();
  2687. }
  2688. }
  2689. internal void ReplaceSelection(string s) {
  2690. int i;
  2691. // First, delete any selected text
  2692. if ((selection_start.pos != selection_end.pos) || (selection_start.line != selection_end.line)) {
  2693. if (!multiline || (selection_start.line == selection_end.line)) {
  2694. undo.RecordDeleteChars(selection_start.line, selection_start.pos + 1, selection_end.pos - selection_start.pos);
  2695. DeleteChars(selection_start.tag, selection_start.pos, selection_end.pos - selection_start.pos);
  2696. // The tag might have been removed, we need to recalc it
  2697. selection_start.tag = selection_start.line.FindTag(selection_start.pos);
  2698. } else {
  2699. int start;
  2700. int end;
  2701. start = selection_start.line.line_no;
  2702. end = selection_end.line.line_no;
  2703. undo.RecordDelete(selection_start.line, selection_start.pos + 1, selection_end.line, selection_end.pos);
  2704. // Delete first line
  2705. DeleteChars(selection_start.tag, selection_start.pos, selection_start.line.text.Length - selection_start.pos);
  2706. // Delete last line
  2707. DeleteChars(selection_end.line.tags, 0, selection_end.pos);
  2708. start++;
  2709. if (start < end) {
  2710. for (i = end - 1; i >= start; i--) {
  2711. Delete(i);
  2712. }
  2713. }
  2714. // BIG FAT WARNING - selection_end.line might be stale due
  2715. // to the above Delete() call. DONT USE IT before hitting the end of this method!
  2716. // Join start and end
  2717. Combine(selection_start.line.line_no, start);
  2718. }
  2719. }
  2720. Insert(selection_start.line, null, selection_start.pos, true, s);
  2721. selection_end.line = selection_start.line;
  2722. selection_end.pos = selection_start.pos;
  2723. selection_end.tag = selection_start.tag;
  2724. selection_visible = false;
  2725. }
  2726. internal void CharIndexToLineTag(int index, out Line line_out, out LineTag tag_out, out int pos) {
  2727. Line line;
  2728. LineTag tag;
  2729. int i;
  2730. int chars;
  2731. int start;
  2732. chars = 0;
  2733. for (i = 1; i < lines; i++) {
  2734. line = GetLine(i);
  2735. start = chars;
  2736. chars += line.text.Length + crlf_size;
  2737. if (index <= chars) {
  2738. // we found the line
  2739. tag = line.tags;
  2740. while (tag != null) {
  2741. if (index < (start + tag.start + tag.length)) {
  2742. line_out = line;
  2743. tag_out = tag;
  2744. pos = index - start;
  2745. return;
  2746. }
  2747. if (tag.next == null) {
  2748. Line next_line;
  2749. next_line = GetLine(line.line_no + 1);
  2750. if (next_line != null) {
  2751. line_out = next_line;
  2752. tag_out = next_line.tags;
  2753. pos = 0;
  2754. return;
  2755. } else {
  2756. line_out = line;
  2757. tag_out = tag;
  2758. pos = line_out.text.Length;
  2759. return;
  2760. }
  2761. }
  2762. tag = tag.next;
  2763. }
  2764. }
  2765. }
  2766. line_out = GetLine(lines);
  2767. tag = line_out.tags;
  2768. while (tag.next != null) {
  2769. tag = tag.next;
  2770. }
  2771. tag_out = tag;
  2772. pos = line_out.text.Length;
  2773. }
  2774. internal int LineTagToCharIndex(Line line, int pos) {
  2775. int i;
  2776. int length;
  2777. // Count first and last line
  2778. length = 0;
  2779. // Count the lines in the middle
  2780. for (i = 1; i < line.line_no; i++) {
  2781. length += GetLine(i).text.Length + crlf_size;
  2782. }
  2783. length += pos;
  2784. return length;
  2785. }
  2786. internal int SelectionLength() {
  2787. if ((selection_start.pos == selection_end.pos) && (selection_start.line == selection_end.line)) {
  2788. return 0;
  2789. }
  2790. if (!multiline || (selection_start.line == selection_end.line)) {
  2791. return selection_end.pos - selection_start.pos;
  2792. } else {
  2793. int i;
  2794. int start;
  2795. int end;
  2796. int length;
  2797. // Count first and last line
  2798. length = selection_start.line.text.Length - selection_start.pos + selection_end.pos + crlf_size;
  2799. // Count the lines in the middle
  2800. start = selection_start.line.line_no + 1;
  2801. end = selection_end.line.line_no;
  2802. if (start < end) {
  2803. for (i = start; i < end; i++) {
  2804. length += GetLine(i).text.Length + crlf_size;
  2805. }
  2806. }
  2807. return length;
  2808. }
  2809. }
  2810. /// <summary>Give it a Line number and it returns the Line object at with that line number</summary>
  2811. internal Line GetLine(int LineNo) {
  2812. Line line = document;
  2813. while (line != sentinel) {
  2814. if (LineNo == line.line_no) {
  2815. return line;
  2816. } else if (LineNo < line.line_no) {
  2817. line = line.left;
  2818. } else {
  2819. line = line.right;
  2820. }
  2821. }
  2822. return null;
  2823. }
  2824. /// <summary>Retrieve the previous tag; walks line boundaries</summary>
  2825. internal LineTag PreviousTag(LineTag tag) {
  2826. Line l;
  2827. if (tag.previous != null) {
  2828. return tag.previous;
  2829. }
  2830. // Next line
  2831. if (tag.line.line_no == 1) {
  2832. return null;
  2833. }
  2834. l = GetLine(tag.line.line_no - 1);
  2835. if (l != null) {
  2836. LineTag t;
  2837. t = l.tags;
  2838. while (t.next != null) {
  2839. t = t.next;
  2840. }
  2841. return t;
  2842. }
  2843. return null;
  2844. }
  2845. /// <summary>Retrieve the next tag; walks line boundaries</summary>
  2846. internal LineTag NextTag(LineTag tag) {
  2847. Line l;
  2848. if (tag.next != null) {
  2849. return tag.next;
  2850. }
  2851. // Next line
  2852. l = GetLine(tag.line.line_no + 1);
  2853. if (l != null) {
  2854. return l.tags;
  2855. }
  2856. return null;
  2857. }
  2858. internal Line ParagraphStart(Line line) {
  2859. while (line.soft_break) {
  2860. line = GetLine(line.line_no - 1);
  2861. }
  2862. return line;
  2863. }
  2864. internal Line ParagraphEnd(Line line) {
  2865. Line l;
  2866. while (line.soft_break) {
  2867. l = GetLine(line.line_no + 1);
  2868. if ((l == null) || (!l.soft_break)) {
  2869. break;
  2870. }
  2871. line = l;
  2872. }
  2873. return line;
  2874. }
  2875. /// <summary>Give it a Y pixel coordinate and it returns the Line covering that Y coordinate</summary>
  2876. internal Line GetLineByPixel(int y, bool exact) {
  2877. Line line = document;
  2878. Line last = null;
  2879. while (line != sentinel) {
  2880. last = line;
  2881. if ((y >= line.Y) && (y < (line.Y+line.height))) {
  2882. return line;
  2883. } else if (y < line.Y) {
  2884. line = line.left;
  2885. } else {
  2886. line = line.right;
  2887. }
  2888. }
  2889. if (exact) {
  2890. return null;
  2891. }
  2892. return last;
  2893. }
  2894. // Give it x/y pixel coordinates and it returns the Tag at that position; optionally the char position is returned in index
  2895. internal LineTag FindTag(int x, int y, out int index, bool exact) {
  2896. Line line;
  2897. LineTag tag;
  2898. line = GetLineByPixel(y, exact);
  2899. if (line == null) {
  2900. index = 0;
  2901. return null;
  2902. }
  2903. tag = line.tags;
  2904. // Alignment adjustment
  2905. x += line.align_shift;
  2906. while (true) {
  2907. if (x >= tag.X && x < (tag.X+tag.width)) {
  2908. int end;
  2909. end = tag.start + tag.length - 1;
  2910. for (int pos = tag.start; pos < end; pos++) {
  2911. if (x < line.widths[pos]) {
  2912. index = pos;
  2913. return tag;
  2914. }
  2915. }
  2916. index=end;
  2917. return tag;
  2918. }
  2919. if (tag.next != null) {
  2920. tag = tag.next;
  2921. } else {
  2922. if (exact) {
  2923. index = 0;
  2924. return null;
  2925. }
  2926. index = line.text.Length;
  2927. return tag;
  2928. }
  2929. }
  2930. }
  2931. // Give it x/y pixel coordinates and it returns the Tag at that position; optionally the char position is returned in index
  2932. internal LineTag FindCursor(int x, int y, out int index) {
  2933. Line line;
  2934. LineTag tag;
  2935. line = GetLineByPixel(y, false);
  2936. tag = line.tags;
  2937. // Adjust for alignment
  2938. x += line.align_shift;
  2939. while (true) {
  2940. if (x >= tag.X && x < (tag.X+tag.width)) {
  2941. int end;
  2942. end = tag.start + tag.length - 1;
  2943. for (int pos = tag.start-1; pos < end; pos++) {
  2944. // When clicking on a character, we position the cursor to whatever edge
  2945. // of the character the click was closer
  2946. if (x < (line.widths[pos] + ((line.widths[pos+1]-line.widths[pos])/2))) {
  2947. index = pos;
  2948. return tag;
  2949. }
  2950. }
  2951. index=end;
  2952. return tag;
  2953. }
  2954. if (tag.next != null) {
  2955. tag = tag.next;
  2956. } else {
  2957. index = line.text.Length;
  2958. return tag;
  2959. }
  2960. }
  2961. }
  2962. /// <summary>Format area of document in specified font and color</summary>
  2963. /// <param name="start_pos">1-based start position on start_line</param>
  2964. /// <param name="end_pos">1-based end position on end_line </param>
  2965. internal void FormatText(Line start_line, int start_pos, Line end_line, int end_pos, Font font, Brush color) {
  2966. Line l;
  2967. // First, format the first line
  2968. if (start_line != end_line) {
  2969. // First line
  2970. LineTag.FormatText(start_line, start_pos, start_line.text.Length - start_pos + 1, font, color);
  2971. // Format last line
  2972. LineTag.FormatText(end_line, 1, end_pos, font, color);
  2973. // Now all the lines inbetween
  2974. for (int i = start_line.line_no + 1; i < end_line.line_no; i++) {
  2975. l = GetLine(i);
  2976. LineTag.FormatText(l, 1, l.text.Length, font, color);
  2977. }
  2978. } else {
  2979. // Special case, single line
  2980. LineTag.FormatText(start_line, start_pos, end_pos - start_pos, font, color);
  2981. }
  2982. }
  2983. /// <summary>Re-format areas of the document in specified font and color</summary>
  2984. /// <param name="start_pos">1-based start position on start_line</param>
  2985. /// <param name="end_pos">1-based end position on end_line </param>
  2986. /// <param name="font">Font specifying attributes</param>
  2987. /// <param name="color">Color (or NULL) to apply</param>
  2988. /// <param name="apply">Attributes from font and color to apply</param>
  2989. internal void FormatText(Line start_line, int start_pos, Line end_line, int end_pos, FontDefinition attributes) {
  2990. Line l;
  2991. // First, format the first line
  2992. if (start_line != end_line) {
  2993. // First line
  2994. LineTag.FormatText(start_line, start_pos, start_line.text.Length - start_pos + 1, attributes);
  2995. // Format last line
  2996. LineTag.FormatText(end_line, 1, end_pos - 1, attributes);
  2997. // Now all the lines inbetween
  2998. for (int i = start_line.line_no + 1; i < end_line.line_no; i++) {
  2999. l = GetLine(i);
  3000. LineTag.FormatText(l, 1, l.text.Length, attributes);
  3001. }
  3002. } else {
  3003. // Special case, single line
  3004. LineTag.FormatText(start_line, start_pos, end_pos - start_pos, attributes);
  3005. }
  3006. }
  3007. internal void RecalculateAlignments() {
  3008. Line line;
  3009. int line_no;
  3010. line_no = 1;
  3011. while (line_no <= lines) {
  3012. line = GetLine(line_no);
  3013. if (line != null && line.alignment != HorizontalAlignment.Left) {
  3014. if (line.alignment == HorizontalAlignment.Center) {
  3015. line.align_shift = (viewport_width - (int)line.widths[line.text.Length]) / 2;
  3016. } else {
  3017. line.align_shift = viewport_width - (int)line.widths[line.text.Length] - 1;
  3018. }
  3019. }
  3020. line_no++;
  3021. }
  3022. return;
  3023. }
  3024. /// <summary>Calculate formatting for the whole document</summary>
  3025. internal bool RecalculateDocument(Graphics g) {
  3026. return RecalculateDocument(g, 1, this.lines, false);
  3027. }
  3028. /// <summary>Calculate formatting starting at a certain line</summary>
  3029. internal bool RecalculateDocument(Graphics g, int start) {
  3030. return RecalculateDocument(g, start, this.lines, false);
  3031. }
  3032. /// <summary>Calculate formatting within two given line numbers</summary>
  3033. internal bool RecalculateDocument(Graphics g, int start, int end) {
  3034. return RecalculateDocument(g, start, end, false);
  3035. }
  3036. /// <summary>With optimize on, returns true if line heights changed</summary>
  3037. internal bool RecalculateDocument(Graphics g, int start, int end, bool optimize) {
  3038. Line line;
  3039. int line_no;
  3040. int Y;
  3041. int new_width;
  3042. bool changed;
  3043. int shift;
  3044. if (no_recalc) {
  3045. recalc_pending = true;
  3046. recalc_start = start;
  3047. recalc_end = end;
  3048. recalc_optimize = optimize;
  3049. return false;
  3050. }
  3051. Y = GetLine(start).Y;
  3052. line_no = start;
  3053. new_width = 0;
  3054. shift = this.lines;
  3055. if (!optimize) {
  3056. changed = true; // We always return true if we run non-optimized
  3057. } else {
  3058. changed = false;
  3059. }
  3060. while (line_no <= (end + this.lines - shift)) {
  3061. line = GetLine(line_no++);
  3062. line.Y = Y;
  3063. if (!calc_pass) {
  3064. if (!optimize) {
  3065. line.RecalculateLine(g, this);
  3066. } else {
  3067. if (line.recalc && line.RecalculateLine(g, this)) {
  3068. changed = true;
  3069. // If the height changed, all subsequent lines change
  3070. end = this.lines;
  3071. shift = this.lines;
  3072. }
  3073. }
  3074. } else {
  3075. if (!optimize) {
  3076. line.RecalculatePasswordLine(g, this);
  3077. } else {
  3078. if (line.recalc && line.RecalculatePasswordLine(g, this)) {
  3079. changed = true;
  3080. // If the height changed, all subsequent lines change
  3081. end = this.lines;
  3082. shift = this.lines;
  3083. }
  3084. }
  3085. }
  3086. if (line.widths[line.text.Length] > new_width) {
  3087. new_width = (int)line.widths[line.text.Length];
  3088. }
  3089. // Calculate alignment
  3090. if (line.alignment != HorizontalAlignment.Left) {
  3091. if (line.alignment == HorizontalAlignment.Center) {
  3092. line.align_shift = (viewport_width - (int)line.widths[line.text.Length]) / 2;
  3093. } else {
  3094. line.align_shift = viewport_width - (int)line.widths[line.text.Length] - 1;
  3095. }
  3096. }
  3097. Y += line.height;
  3098. if (line_no > lines) {
  3099. break;
  3100. }
  3101. }
  3102. if (document_x != new_width) {
  3103. document_x = new_width;
  3104. if (WidthChanged != null) {
  3105. WidthChanged(this, null);
  3106. }
  3107. }
  3108. RecalculateAlignments();
  3109. line = GetLine(lines);
  3110. if (document_y != line.Y + line.height) {
  3111. document_y = line.Y + line.height;
  3112. if (HeightChanged != null) {
  3113. HeightChanged(this, null);
  3114. }
  3115. }
  3116. UpdateCaret();
  3117. return changed;
  3118. }
  3119. internal int Size() {
  3120. return lines;
  3121. }
  3122. private void owner_HandleCreated(object sender, EventArgs e) {
  3123. RecalculateDocument(owner.CreateGraphicsInternal());
  3124. PositionCaret(0, 0);
  3125. }
  3126. private void owner_VisibleChanged(object sender, EventArgs e) {
  3127. if (owner.Visible) {
  3128. RecalculateDocument(owner.CreateGraphicsInternal());
  3129. }
  3130. }
  3131. internal static bool IsWordSeparator(char ch) {
  3132. switch(ch) {
  3133. case ' ':
  3134. case '\t':
  3135. case '(':
  3136. case ')': {
  3137. return true;
  3138. }
  3139. default: {
  3140. return false;
  3141. }
  3142. }
  3143. }
  3144. internal int FindWordSeparator(Line line, int pos, bool forward) {
  3145. int len;
  3146. len = line.text.Length;
  3147. if (forward) {
  3148. for (int i = pos + 1; i < len; i++) {
  3149. if (IsWordSeparator(line.Text[i])) {
  3150. return i + 1;
  3151. }
  3152. }
  3153. return len;
  3154. } else {
  3155. for (int i = pos - 1; i > 0; i--) {
  3156. if (IsWordSeparator(line.Text[i - 1])) {
  3157. return i;
  3158. }
  3159. }
  3160. return 0;
  3161. }
  3162. }
  3163. /* Search document for text */
  3164. internal bool FindChars(char[] chars, Marker start, Marker end, out Marker result) {
  3165. Line line;
  3166. int line_no;
  3167. int pos;
  3168. int line_len;
  3169. // Search for occurence of any char in the chars array
  3170. result = new Marker();
  3171. line = start.line;
  3172. line_no = start.line.line_no;
  3173. pos = start.pos;
  3174. while (line_no <= end.line.line_no) {
  3175. line_len = line.text.Length;
  3176. while (pos < line_len) {
  3177. for (int i = 0; i < chars.Length; i++) {
  3178. if (line.text[pos] == chars[i]) {
  3179. // Special case
  3180. if ((line.line_no == end.line.line_no) && (pos >= end.pos)) {
  3181. return false;
  3182. }
  3183. result.line = line;
  3184. result.pos = pos;
  3185. return true;
  3186. }
  3187. }
  3188. pos++;
  3189. }
  3190. pos = 0;
  3191. line_no++;
  3192. line = GetLine(line_no);
  3193. }
  3194. return false;
  3195. }
  3196. // This version does not build one big string for searching, instead it handles
  3197. // line-boundaries, which is faster and less memory intensive
  3198. // FIXME - Depending on culture stuff we might have to create a big string and use culturespecific
  3199. // search stuff and change it to accept and return positions instead of Markers (which would match
  3200. // RichTextBox behaviour better but would be inconsistent with the rest of TextControl)
  3201. internal bool Find(string search, Marker start, Marker end, out Marker result, RichTextBoxFinds options) {
  3202. Marker last;
  3203. string search_string;
  3204. Line line;
  3205. int line_no;
  3206. int pos;
  3207. int line_len;
  3208. int current;
  3209. bool word;
  3210. bool word_option;
  3211. bool ignore_case;
  3212. bool reverse;
  3213. char c;
  3214. result = new Marker();
  3215. word_option = ((options & RichTextBoxFinds.WholeWord) != 0);
  3216. ignore_case = ((options & RichTextBoxFinds.MatchCase) == 0);
  3217. reverse = ((options & RichTextBoxFinds.Reverse) != 0);
  3218. line = start.line;
  3219. line_no = start.line.line_no;
  3220. pos = start.pos;
  3221. current = 0;
  3222. // Prep our search string, lowercasing it if we do case-independent matching
  3223. if (ignore_case) {
  3224. StringBuilder sb;
  3225. sb = new StringBuilder(search);
  3226. for (int i = 0; i < sb.Length; i++) {
  3227. sb[i] = Char.ToLower(sb[i]);
  3228. }
  3229. search_string = sb.ToString();
  3230. } else {
  3231. search_string = search;
  3232. }
  3233. // We need to check if the character before our start position is a wordbreak
  3234. if (word_option) {
  3235. if (line_no == 1) {
  3236. if ((pos == 0) || (IsWordSeparator(line.text[pos - 1]))) {
  3237. word = true;
  3238. } else {
  3239. word = false;
  3240. }
  3241. } else {
  3242. if (pos > 0) {
  3243. if (IsWordSeparator(line.text[pos - 1])) {
  3244. word = true;
  3245. } else {
  3246. word = false;
  3247. }
  3248. } else {
  3249. // Need to check the end of the previous line
  3250. Line prev_line;
  3251. prev_line = GetLine(line_no - 1);
  3252. if (prev_line.soft_break) {
  3253. if (IsWordSeparator(prev_line.text[prev_line.text.Length - 1])) {
  3254. word = true;
  3255. } else {
  3256. word = false;
  3257. }
  3258. } else {
  3259. word = true;
  3260. }
  3261. }
  3262. }
  3263. } else {
  3264. word = false;
  3265. }
  3266. // To avoid duplication of this loop with reverse logic, we search
  3267. // through the document, remembering the last match and when returning
  3268. // report that last remembered match
  3269. last = new Marker();
  3270. last.height = -1; // Abused - we use it to track change
  3271. while (line_no <= end.line.line_no) {
  3272. if (line_no != end.line.line_no) {
  3273. line_len = line.text.Length;
  3274. } else {
  3275. line_len = end.pos;
  3276. }
  3277. while (pos < line_len) {
  3278. if (word_option && (current == search_string.Length)) {
  3279. if (IsWordSeparator(line.text[pos])) {
  3280. if (!reverse) {
  3281. goto FindFound;
  3282. } else {
  3283. last = result;
  3284. current = 0;
  3285. }
  3286. } else {
  3287. current = 0;
  3288. }
  3289. }
  3290. if (ignore_case) {
  3291. c = Char.ToLower(line.text[pos]);
  3292. } else {
  3293. c = line.text[pos];
  3294. }
  3295. if (c == search_string[current]) {
  3296. if (current == 0) {
  3297. result.line = line;
  3298. result.pos = pos;
  3299. }
  3300. if (!word_option || (word_option && (word || (current > 0)))) {
  3301. current++;
  3302. }
  3303. if (!word_option && (current == search_string.Length)) {
  3304. if (!reverse) {
  3305. goto FindFound;
  3306. } else {
  3307. last = result;
  3308. current = 0;
  3309. }
  3310. }
  3311. } else {
  3312. current = 0;
  3313. }
  3314. pos++;
  3315. if (!word_option) {
  3316. continue;
  3317. }
  3318. if (IsWordSeparator(c)) {
  3319. word = true;
  3320. } else {
  3321. word = false;
  3322. }
  3323. }
  3324. if (word_option) {
  3325. // Mark that we just saw a word boundary
  3326. if (!line.soft_break) {
  3327. word = true;
  3328. }
  3329. if (current == search_string.Length) {
  3330. if (word) {
  3331. if (!reverse) {
  3332. goto FindFound;
  3333. } else {
  3334. last = result;
  3335. current = 0;
  3336. }
  3337. } else {
  3338. current = 0;
  3339. }
  3340. }
  3341. }
  3342. pos = 0;
  3343. line_no++;
  3344. line = GetLine(line_no);
  3345. }
  3346. if (reverse) {
  3347. if (last.height != -1) {
  3348. result = last;
  3349. return true;
  3350. }
  3351. }
  3352. return false;
  3353. FindFound:
  3354. if (!reverse) {
  3355. // if ((line.line_no == end.line.line_no) && (pos >= end.pos)) {
  3356. // return false;
  3357. // }
  3358. return true;
  3359. }
  3360. result = last;
  3361. return true;
  3362. }
  3363. /* Marker stuff */
  3364. internal void GetMarker(out Marker mark, bool start) {
  3365. mark = new Marker();
  3366. if (start) {
  3367. mark.line = GetLine(1);
  3368. mark.tag = mark.line.tags;
  3369. mark.pos = 0;
  3370. } else {
  3371. mark.line = GetLine(lines);
  3372. mark.tag = mark.line.tags;
  3373. while (mark.tag.next != null) {
  3374. mark.tag = mark.tag.next;
  3375. }
  3376. mark.pos = mark.line.text.Length;
  3377. }
  3378. }
  3379. #endregion // Internal Methods
  3380. #region Events
  3381. internal event EventHandler CaretMoved;
  3382. internal event EventHandler WidthChanged;
  3383. internal event EventHandler HeightChanged;
  3384. internal event EventHandler LengthChanged;
  3385. #endregion // Events
  3386. #region Administrative
  3387. public IEnumerator GetEnumerator() {
  3388. // FIXME
  3389. return null;
  3390. }
  3391. public override bool Equals(object obj) {
  3392. if (obj == null) {
  3393. return false;
  3394. }
  3395. if (!(obj is Document)) {
  3396. return false;
  3397. }
  3398. if (obj == this) {
  3399. return true;
  3400. }
  3401. if (ToString().Equals(((Document)obj).ToString())) {
  3402. return true;
  3403. }
  3404. return false;
  3405. }
  3406. public override int GetHashCode() {
  3407. return document_id;
  3408. }
  3409. public override string ToString() {
  3410. return "document " + this.document_id;
  3411. }
  3412. #endregion // Administrative
  3413. }
  3414. internal class LineTag {
  3415. #region Local Variables;
  3416. // Payload; formatting
  3417. internal Font font; // System.Drawing.Font object for this tag
  3418. internal Brush color; // System.Drawing.Brush object
  3419. // Payload; text
  3420. internal int start; // start, in chars; index into Line.text
  3421. internal int length; // length, in chars
  3422. internal bool r_to_l; // Which way is the font
  3423. // Drawing support
  3424. internal int height; // Height in pixels of the text this tag describes
  3425. internal int X; // X location of the text this tag describes
  3426. internal float width; // Width in pixels of the text this tag describes
  3427. internal int ascent; // Ascent of the font for this tag
  3428. internal int shift; // Shift down for this tag, to stay on baseline
  3429. // Administrative
  3430. internal Line line; // The line we're on
  3431. internal LineTag next; // Next tag on the same line
  3432. internal LineTag previous; // Previous tag on the same line
  3433. #endregion;
  3434. #region Constructors
  3435. internal LineTag(Line line, int start, int length) {
  3436. this.line = line;
  3437. this.start = start;
  3438. this.length = length;
  3439. this.X = 0;
  3440. this.width = 0;
  3441. }
  3442. #endregion // Constructors
  3443. #region Internal Methods
  3444. ///<summary>Break a tag into two with identical attributes; pos is 1-based; returns tag starting at &gt;pos&lt; or null if end-of-line</summary>
  3445. internal LineTag Break(int pos) {
  3446. LineTag new_tag;
  3447. // Sanity
  3448. if (pos == this.start) {
  3449. return this;
  3450. } else if (pos >= (start + length)) {
  3451. return null;
  3452. }
  3453. new_tag = new LineTag(line, pos, start + length - pos);
  3454. new_tag.color = color;
  3455. new_tag.font = font;
  3456. this.length -= new_tag.length;
  3457. new_tag.next = this.next;
  3458. this.next = new_tag;
  3459. new_tag.previous = this;
  3460. if (new_tag.next != null) {
  3461. new_tag.next.previous = new_tag;
  3462. }
  3463. return new_tag;
  3464. }
  3465. ///<summary>Create new font and brush from existing font and given new attributes. Returns true if fontheight changes</summary>
  3466. internal static bool GenerateTextFormat(Font font_from, Brush color_from, FontDefinition attributes, out Font new_font, out Brush new_color) {
  3467. float size;
  3468. string face;
  3469. FontStyle style;
  3470. GraphicsUnit unit;
  3471. if (attributes.font_obj == null) {
  3472. size = font_from.SizeInPoints;
  3473. unit = font_from.Unit;
  3474. face = font_from.Name;
  3475. style = font_from.Style;
  3476. if (attributes.face != null) {
  3477. face = attributes.face;
  3478. }
  3479. if (attributes.size != 0) {
  3480. size = attributes.size;
  3481. }
  3482. style |= attributes.add_style;
  3483. style &= ~attributes.remove_style;
  3484. // Create new font
  3485. new_font = new Font(face, size, style, unit);
  3486. } else {
  3487. new_font = attributes.font_obj;
  3488. }
  3489. // Create 'new' color brush
  3490. if (attributes.color != Color.Empty) {
  3491. new_color = new SolidBrush(attributes.color);
  3492. } else {
  3493. new_color = color_from;
  3494. }
  3495. if (new_font.Height == font_from.Height) {
  3496. return false;
  3497. }
  3498. return true;
  3499. }
  3500. /// <summary>Applies 'font' and 'brush' to characters starting at 'start' for 'length' chars;
  3501. /// Removes any previous tags overlapping the same area;
  3502. /// returns true if lineheight has changed</summary>
  3503. /// <param name="start">1-based character position on line</param>
  3504. internal static bool FormatText(Line line, int start, int length, Font font, Brush color) {
  3505. LineTag tag;
  3506. LineTag start_tag;
  3507. int end;
  3508. bool retval = false; // Assume line-height doesn't change
  3509. // Too simple?
  3510. if (font.Height != line.height) {
  3511. retval = true;
  3512. }
  3513. line.recalc = true; // This forces recalculation of the line in RecalculateDocument
  3514. // A little sanity, not sure if it's needed, might be able to remove for speed
  3515. if (length > line.text.Length) {
  3516. length = line.text.Length;
  3517. }
  3518. tag = line.tags;
  3519. end = start + length;
  3520. // Common special case
  3521. if ((start == 1) && (length == tag.length)) {
  3522. tag.ascent = 0;
  3523. tag.font = font;
  3524. tag.color = color;
  3525. return retval;
  3526. }
  3527. //Console.WriteLine("Finding tag for {0} {1}", line, start);
  3528. start_tag = FindTag(line, start);
  3529. if (start_tag == null) { // FIXME - is there a better way to handle this, or do we even need it?
  3530. throw new Exception(String.Format("Could not find start_tag in document at line {0} position {1}", line.line_no, start));
  3531. }
  3532. tag = new LineTag(line, start, length);
  3533. tag.font = font;
  3534. tag.color = color;
  3535. if (start == 1) {
  3536. line.tags = tag;
  3537. }
  3538. //Console.WriteLine("Start tag: '{0}'", start_tag!=null ? start_tag.ToString() : "NULL");
  3539. if (start_tag.start == start) {
  3540. tag.next = start_tag;
  3541. tag.previous = start_tag.previous;
  3542. if (start_tag.previous != null) {
  3543. start_tag.previous.next = tag;
  3544. }
  3545. start_tag.previous = tag;
  3546. } else {
  3547. // Insert ourselves 'in the middle'
  3548. if ((start_tag.next != null) && (start_tag.next.start < end)) {
  3549. tag.next = start_tag.next;
  3550. } else {
  3551. tag.next = new LineTag(line, start_tag.start, start_tag.length);
  3552. tag.next.font = start_tag.font;
  3553. tag.next.color = start_tag.color;
  3554. if (start_tag.next != null) {
  3555. tag.next.next = start_tag.next;
  3556. tag.next.next.previous = tag.next;
  3557. }
  3558. }
  3559. tag.next.previous = tag;
  3560. start_tag.length = start - start_tag.start;
  3561. tag.previous = start_tag;
  3562. start_tag.next = tag;
  3563. #if nope
  3564. if (tag.next.start > (tag.start + tag.length)) {
  3565. tag.next.length += tag.next.start - (tag.start + tag.length);
  3566. tag.next.start = tag.start + tag.length;
  3567. }
  3568. #endif
  3569. }
  3570. // Elimination loop
  3571. tag = tag.next;
  3572. while ((tag != null) && (tag.start < end)) {
  3573. if ((tag.start + tag.length) <= end) {
  3574. // remove the tag
  3575. tag.previous.next = tag.next;
  3576. if (tag.next != null) {
  3577. tag.next.previous = tag.previous;
  3578. }
  3579. tag = tag.previous;
  3580. } else {
  3581. // Adjust the length of the tag
  3582. tag.length = (tag.start + tag.length) - end;
  3583. tag.start = end;
  3584. }
  3585. tag = tag.next;
  3586. }
  3587. return retval;
  3588. }
  3589. /// <summary>Applies font attributes specified to characters starting at 'start' for 'length' chars;
  3590. /// Breaks tags at start and end point, keeping middle tags with altered attributes.
  3591. /// Returns true if lineheight has changed</summary>
  3592. /// <param name="start">1-based character position on line</param>
  3593. internal static bool FormatText(Line line, int start, int length, FontDefinition attributes) {
  3594. LineTag tag;
  3595. LineTag start_tag;
  3596. LineTag end_tag;
  3597. bool retval = false; // Assume line-height doesn't change
  3598. line.recalc = true; // This forces recalculation of the line in RecalculateDocument
  3599. // A little sanity, not sure if it's needed, might be able to remove for speed
  3600. if (length > line.text.Length) {
  3601. length = line.text.Length;
  3602. }
  3603. tag = line.tags;
  3604. // Common special case
  3605. if ((start == 1) && (length == tag.length)) {
  3606. tag.ascent = 0;
  3607. GenerateTextFormat(tag.font, tag.color, attributes, out tag.font, out tag.color);
  3608. return retval;
  3609. }
  3610. start_tag = FindTag(line, start);
  3611. if (start_tag == null) {
  3612. if (length == 0) {
  3613. // We are 'starting' after all valid tags; create a new tag with the right attributes
  3614. start_tag = FindTag(line, line.text.Length - 1);
  3615. start_tag.next = new LineTag(line, line.text.Length + 1, 0);
  3616. start_tag.next.font = start_tag.font;
  3617. start_tag.next.color = start_tag.color;
  3618. start_tag.next.previous = start_tag;
  3619. start_tag = start_tag.next;
  3620. } else {
  3621. throw new Exception(String.Format("Could not find start_tag in document at line {0} position {1}", line.line_no, start));
  3622. }
  3623. } else {
  3624. start_tag = start_tag.Break(start);
  3625. }
  3626. end_tag = FindTag(line, start + length);
  3627. if (end_tag != null) {
  3628. end_tag = end_tag.Break(start + length);
  3629. }
  3630. // start_tag or end_tag might be null; we're cool with that
  3631. // we now walk from start_tag to end_tag, applying new attributes
  3632. tag = start_tag;
  3633. while ((tag != null) && tag != end_tag) {
  3634. if (LineTag.GenerateTextFormat(tag.font, tag.color, attributes, out tag.font, out tag.color)) {
  3635. retval = true;
  3636. }
  3637. tag = tag.next;
  3638. }
  3639. return retval;
  3640. }
  3641. /// <summary>Finds the tag that describes the character at position 'pos' on 'line'</summary>
  3642. internal static LineTag FindTag(Line line, int pos) {
  3643. LineTag tag = line.tags;
  3644. // Beginning of line is a bit special
  3645. if (pos == 0) {
  3646. return tag;
  3647. }
  3648. while (tag != null) {
  3649. if ((tag.start <= pos) && (pos < (tag.start+tag.length))) {
  3650. return tag;
  3651. }
  3652. tag = tag.next;
  3653. }
  3654. return null;
  3655. }
  3656. /// <summary>Combines 'this' tag with 'other' tag</summary>
  3657. internal bool Combine(LineTag other) {
  3658. if (!this.Equals(other)) {
  3659. return false;
  3660. }
  3661. this.width += other.width;
  3662. this.length += other.length;
  3663. this.next = other.next;
  3664. if (this.next != null) {
  3665. this.next.previous = this;
  3666. }
  3667. return true;
  3668. }
  3669. /// <summary>Remove 'this' tag ; to be called when formatting is to be removed</summary>
  3670. internal bool Remove() {
  3671. if ((this.start == 1) && (this.next == null)) {
  3672. // We cannot remove the only tag
  3673. return false;
  3674. }
  3675. if (this.start != 1) {
  3676. this.previous.length += this.length;
  3677. this.previous.width = -1;
  3678. this.previous.next = this.next;
  3679. this.next.previous = this.previous;
  3680. } else {
  3681. this.next.start = 1;
  3682. this.next.length += this.length;
  3683. this.next.width = -1;
  3684. this.line.tags = this.next;
  3685. this.next.previous = null;
  3686. }
  3687. return true;
  3688. }
  3689. /// <summary>Checks if 'this' tag describes the same formatting options as 'obj'</summary>
  3690. public override bool Equals(object obj) {
  3691. LineTag other;
  3692. if (obj == null) {
  3693. return false;
  3694. }
  3695. if (!(obj is LineTag)) {
  3696. return false;
  3697. }
  3698. if (obj == this) {
  3699. return true;
  3700. }
  3701. other = (LineTag)obj;
  3702. if (this.font.Equals(other.font) && this.color.Equals(other.color)) { // FIXME add checking for things like link or type later
  3703. return true;
  3704. }
  3705. return false;
  3706. }
  3707. public override int GetHashCode() {
  3708. return base.GetHashCode ();
  3709. }
  3710. public override string ToString() {
  3711. return "Tag starts at index " + this.start + "length " + this.length + " text: " + this.line.Text.Substring(this.start-1, this.length) + "Font " + this.font.ToString();
  3712. }
  3713. #endregion // Internal Methods
  3714. }
  3715. internal class UndoClass {
  3716. internal enum ActionType {
  3717. InsertChar,
  3718. InsertString,
  3719. DeleteChar,
  3720. DeleteChars,
  3721. CursorMove,
  3722. Mark,
  3723. }
  3724. internal class Action {
  3725. internal ActionType type;
  3726. internal int line_no;
  3727. internal int pos;
  3728. internal object data;
  3729. }
  3730. #region Local Variables
  3731. private Document document;
  3732. private Stack undo_actions;
  3733. private Stack redo_actions;
  3734. private int caret_line;
  3735. private int caret_pos;
  3736. #endregion // Local Variables
  3737. #region Constructors
  3738. internal UndoClass(Document doc) {
  3739. document = doc;
  3740. undo_actions = new Stack(50);
  3741. redo_actions = new Stack(50);
  3742. }
  3743. #endregion // Constructors
  3744. #region Properties
  3745. [MonoTODO("Change this to be configurable")]
  3746. internal int UndoLevels {
  3747. get {
  3748. return undo_actions.Count;
  3749. }
  3750. }
  3751. [MonoTODO("Change this to be configurable")]
  3752. internal int RedoLevels {
  3753. get {
  3754. return redo_actions.Count;
  3755. }
  3756. }
  3757. [MonoTODO("Come up with good naming and localization")]
  3758. internal string UndoName {
  3759. get {
  3760. Action action;
  3761. action = (Action)undo_actions.Peek();
  3762. switch(action.type) {
  3763. case ActionType.InsertChar: {
  3764. Locale.GetText("Insert character");
  3765. break;
  3766. }
  3767. case ActionType.DeleteChar: {
  3768. Locale.GetText("Delete character");
  3769. break;
  3770. }
  3771. case ActionType.InsertString: {
  3772. Locale.GetText("Insert string");
  3773. break;
  3774. }
  3775. case ActionType.DeleteChars: {
  3776. Locale.GetText("Delete string");
  3777. break;
  3778. }
  3779. case ActionType.CursorMove: {
  3780. Locale.GetText("Cursor move");
  3781. break;
  3782. }
  3783. }
  3784. return null;
  3785. }
  3786. }
  3787. internal string RedoName() {
  3788. return null;
  3789. }
  3790. #endregion // Properties
  3791. #region Internal Methods
  3792. internal void Clear() {
  3793. undo_actions.Clear();
  3794. redo_actions.Clear();
  3795. }
  3796. internal void Undo() {
  3797. Action action;
  3798. if (undo_actions.Count == 0) {
  3799. return;
  3800. }
  3801. action = (Action)undo_actions.Pop();
  3802. // Put onto redo stack
  3803. redo_actions.Push(action);
  3804. // Do the thing
  3805. switch(action.type) {
  3806. case ActionType.InsertChar: {
  3807. // FIXME - implement me
  3808. break;
  3809. }
  3810. case ActionType.DeleteChars: {
  3811. this.Insert(document.GetLine(action.line_no), action.pos, (Line)action.data);
  3812. Undo(); // Grab the cursor location
  3813. break;
  3814. }
  3815. case ActionType.CursorMove: {
  3816. document.caret.line = document.GetLine(action.line_no);
  3817. if (document.caret.line == null) {
  3818. Undo();
  3819. break;
  3820. }
  3821. document.caret.tag = document.caret.line.FindTag(action.pos);
  3822. document.caret.pos = action.pos;
  3823. document.caret.height = document.caret.tag.height;
  3824. if (document.owner.IsHandleCreated) {
  3825. XplatUI.DestroyCaret(document.owner.Handle);
  3826. XplatUI.CreateCaret(document.owner.Handle, 2, document.caret.height);
  3827. XplatUI.SetCaretPos(document.owner.Handle, (int)document.caret.tag.line.widths[document.caret.pos] + document.caret.line.align_shift - document.viewport_x, document.caret.line.Y + document.caret.tag.shift - document.viewport_y + Document.caret_shift);
  3828. XplatUI.CaretVisible(document.owner.Handle, true);
  3829. }
  3830. // FIXME - enable call
  3831. //if (document.CaretMoved != null) document.CaretMoved(this, EventArgs.Empty);
  3832. break;
  3833. }
  3834. }
  3835. }
  3836. internal void Redo() {
  3837. if (redo_actions.Count == 0) {
  3838. return;
  3839. }
  3840. }
  3841. #endregion // Internal Methods
  3842. #region Private Methods
  3843. // pos = 1-based
  3844. public void RecordDeleteChars(Line line, int pos, int length) {
  3845. RecordDelete(line, pos, line, pos + length - 1);
  3846. }
  3847. // start_pos, end_pos = 1 based
  3848. public void RecordDelete(Line start_line, int start_pos, Line end_line, int end_pos) {
  3849. Line l;
  3850. Action a;
  3851. l = Duplicate(start_line, start_pos, end_line, end_pos);
  3852. a = new Action();
  3853. a.type = ActionType.DeleteChars;
  3854. a.data = l;
  3855. a.line_no = start_line.line_no;
  3856. a.pos = start_pos - 1;
  3857. undo_actions.Push(a);
  3858. RecordCursor();
  3859. }
  3860. public void RecordCursor() {
  3861. if (document.caret.line == null) {
  3862. return;
  3863. }
  3864. RecordCursor(document.caret.line, document.caret.pos);
  3865. }
  3866. public void RecordCursor(Line line, int pos) {
  3867. Action a;
  3868. if ((line.line_no == caret_line) && (pos == caret_pos)) {
  3869. return;
  3870. }
  3871. caret_line = line.line_no;
  3872. caret_pos = pos;
  3873. a = new Action();
  3874. a.type = ActionType.CursorMove;
  3875. a.line_no = line.line_no;
  3876. a.pos = pos;
  3877. undo_actions.Push(a);
  3878. }
  3879. // start_pos = 1-based
  3880. // end_pos = 1-based
  3881. public Line Duplicate(Line start_line, int start_pos, Line end_line, int end_pos) {
  3882. Line ret;
  3883. Line line;
  3884. Line current;
  3885. LineTag tag;
  3886. LineTag current_tag;
  3887. int start;
  3888. int end;
  3889. int tag_start;
  3890. int tag_length;
  3891. line = new Line();
  3892. ret = line;
  3893. for (int i = start_line.line_no; i <= end_line.line_no; i++) {
  3894. current = document.GetLine(i);
  3895. if (start_line.line_no == i) {
  3896. start = start_pos;
  3897. } else {
  3898. start = 1;
  3899. }
  3900. if (end_line.line_no == i) {
  3901. end = end_pos;
  3902. } else {
  3903. end = current.text.Length;
  3904. }
  3905. // Text for the tag
  3906. line.text = new StringBuilder(current.text.ToString(start - 1, end - start + 1));
  3907. // Copy tags from start to start+length onto new line
  3908. current_tag = current.FindTag(start - 1);
  3909. while ((current_tag != null) && (current_tag.start < end)) {
  3910. if ((current_tag.start <= start) && (start < (current_tag.start + current_tag.length))) {
  3911. // start tag is within this tag
  3912. tag_start = start;
  3913. } else {
  3914. tag_start = current_tag.start;
  3915. }
  3916. if (end < (current_tag.start + current_tag.length)) {
  3917. tag_length = end - tag_start + 1;
  3918. } else {
  3919. tag_length = current_tag.start + current_tag.length - tag_start;
  3920. }
  3921. tag = new LineTag(line, tag_start - start + 1, tag_length);
  3922. tag.color = current_tag.color;
  3923. tag.font = current_tag.font;
  3924. current_tag = current_tag.next;
  3925. // Add the new tag to the line
  3926. if (line.tags == null) {
  3927. line.tags = tag;
  3928. } else {
  3929. LineTag tail;
  3930. tail = line.tags;
  3931. while (tail.next != null) {
  3932. tail = tail.next;
  3933. }
  3934. tail.next = tag;
  3935. tag.previous = tail;
  3936. }
  3937. }
  3938. if ((i + 1) <= end_line.line_no) {
  3939. line.soft_break = current.soft_break;
  3940. // Chain them (we use right/left as next/previous)
  3941. line.right = new Line();
  3942. line.right.left = line;
  3943. line = line.right;
  3944. }
  3945. }
  3946. return ret;
  3947. }
  3948. // Insert multi-line text at the given position; use formatting at insertion point for inserted text
  3949. internal void Insert(Line line, int pos, Line insert) {
  3950. Line current;
  3951. LineTag tag;
  3952. int offset;
  3953. int lines;
  3954. Line first;
  3955. // Handle special case first
  3956. if (insert.right == null) {
  3957. // Single line insert
  3958. document.Split(line, pos);
  3959. if (insert.tags == null) {
  3960. return; // Blank line
  3961. }
  3962. //Insert our tags at the end
  3963. tag = line.tags;
  3964. while (tag.next != null) {
  3965. tag = tag.next;
  3966. }
  3967. offset = tag.start + tag.length - 1;
  3968. tag.next = insert.tags;
  3969. line.text.Insert(offset, insert.text.ToString());
  3970. // Adjust start locations
  3971. tag = tag.next;
  3972. while (tag != null) {
  3973. tag.start += offset;
  3974. tag.line = line;
  3975. tag = tag.next;
  3976. }
  3977. // Put it back together
  3978. document.Combine(line.line_no, line.line_no + 1);
  3979. document.UpdateView(line, pos);
  3980. return;
  3981. }
  3982. first = line;
  3983. lines = 1;
  3984. current = insert;
  3985. while (current != null) {
  3986. if (current == insert) {
  3987. // Inserting the first line we split the line (and make space)
  3988. document.Split(line, pos);
  3989. //Insert our tags at the end of the line
  3990. tag = line.tags;
  3991. if (tag != null) {
  3992. while (tag.next != null) {
  3993. tag = tag.next;
  3994. }
  3995. offset = tag.start + tag.length - 1;
  3996. tag.next = current.tags;
  3997. tag.next.previous = tag;
  3998. tag = tag.next;
  3999. } else {
  4000. offset = 0;
  4001. line.tags = current.tags;
  4002. line.tags.previous = null;
  4003. tag = line.tags;
  4004. }
  4005. } else {
  4006. document.Split(line.line_no, 0);
  4007. offset = 0;
  4008. line.tags = current.tags;
  4009. line.tags.previous = null;
  4010. tag = line.tags;
  4011. }
  4012. // Adjust start locations and line pointers
  4013. while (tag != null) {
  4014. tag.start += offset;
  4015. tag.line = line;
  4016. tag = tag.next;
  4017. }
  4018. line.text.Insert(offset, current.text.ToString());
  4019. line.Grow(line.text.Length);
  4020. line.recalc = true;
  4021. line = document.GetLine(line.line_no + 1);
  4022. // FIXME? Test undo of line-boundaries
  4023. if ((current.right == null) && (current.tags.length != 0)) {
  4024. document.Combine(line.line_no - 1, line.line_no);
  4025. }
  4026. current = current.right;
  4027. lines++;
  4028. }
  4029. // Recalculate our document
  4030. document.UpdateView(first, lines, pos);
  4031. return;
  4032. }
  4033. #endregion // Private Methods
  4034. }
  4035. }