TextControl.cs 122 KB

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