TextControl.cs 124 KB

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