TextControl.cs 124 KB

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