TextControl.cs 125 KB

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