TextControl.cs 115 KB

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