TextControl.cs 127 KB

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