TextControl.cs 124 KB

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