TextControl.cs 125 KB

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