TextControl.cs 122 KB

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