TextControl.cs 124 KB

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