TextControl.cs 129 KB

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