TextControl.cs 122 KB

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