TextControl.cs 125 KB

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