TextControl.cs 130 KB

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