| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366 |
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- // Copyright (c) 2004-2006 Novell, Inc. (http://www.novell.com)
- //
- // Authors:
- // Peter Bartok [email protected]
- //
- //
- // NOT COMPLETE
- // There's still plenty of things missing, I've got most of it planned, just hadn't had
- // the time to write it all yet.
- // Stuff missing (in no particular order):
- // - Align text after RecalculateLine
- // - Implement tag types for hotlinks, etc.
- // - Implement CaretPgUp/PgDown
- // NOTE:
- // selection_start.pos and selection_end.pos are 0-based
- // selection_start.pos = first selected char
- // selection_end.pos = first NOT-selected char
- //
- // FormatText methods are 1-based (as are all tags, LineTag.Start is 1 for
- // the first character on a line; the reason is that 0 is the position
- // *before* the first character on a line
- #undef Debug
- using System;
- using System.Collections;
- using System.Drawing;
- using System.Drawing.Text;
- using System.Text;
- using RTF=System.Windows.Forms.RTF;
- namespace System.Windows.Forms {
- internal enum LineColor {
- Red = 0,
- Black = 1
- }
- internal enum CaretSelection {
- Position, // Selection=Caret
- Word, // Selection=Word under caret
- Line // Selection=Line under caret
- }
- [Flags]
- internal enum FormatSpecified {
- None,
- BackColor = 2,
- Font = 4,
- Color = 8,
- }
- internal enum CaretDirection {
- CharForward, // Move a char to the right
- CharBack, // Move a char to the left
- LineUp, // Move a line up
- LineDown, // Move a line down
- Home, // Move to the beginning of the line
- End, // Move to the end of the line
- PgUp, // Move one page up
- PgDn, // Move one page down
- CtrlPgUp, // Move caret to the first visible char in the viewport
- CtrlPgDn, // Move caret to the last visible char in the viewport
- CtrlHome, // Move to the beginning of the document
- CtrlEnd, // Move to the end of the document
- WordBack, // Move to the beginning of the previous word (or beginning of line)
- WordForward, // Move to the beginning of the next word (or end of line)
- SelectionStart, // Move to the beginning of the current selection
- SelectionEnd, // Move to the end of the current selection
- CharForwardNoWrap, // Move a char forward, but don't wrap onto the next line
- CharBackNoWrap // Move a char backward, but don't wrap onto the previous line
- }
- internal enum LineEnding {
- Wrap, // line wraps to the next line
- Limp, // \r
- Hard, // \r\n
- Soft, // \r\r\n
- Rich, // \n
- None
- }
-
- internal class Document : ICloneable, IEnumerable {
- #region Structures
- // FIXME - go through code and check for places where
- // we do explicit comparisons instead of using the compare overloads
- internal struct Marker {
- internal Line line;
- internal LineTag tag;
- internal int pos;
- internal int height;
- public static bool operator<(Marker lhs, Marker rhs) {
- if (lhs.line.line_no < rhs.line.line_no) {
- return true;
- }
- if (lhs.line.line_no == rhs.line.line_no) {
- if (lhs.pos < rhs.pos) {
- return true;
- }
- }
- return false;
- }
- public static bool operator>(Marker lhs, Marker rhs) {
- if (lhs.line.line_no > rhs.line.line_no) {
- return true;
- }
- if (lhs.line.line_no == rhs.line.line_no) {
- if (lhs.pos > rhs.pos) {
- return true;
- }
- }
- return false;
- }
- public static bool operator==(Marker lhs, Marker rhs) {
- if ((lhs.line.line_no == rhs.line.line_no) && (lhs.pos == rhs.pos)) {
- return true;
- }
- return false;
- }
- public static bool operator!=(Marker lhs, Marker rhs) {
- if ((lhs.line.line_no != rhs.line.line_no) || (lhs.pos != rhs.pos)) {
- return true;
- }
- return false;
- }
- public void Combine(Line move_to_line, int move_to_line_length) {
- line = move_to_line;
- pos += move_to_line_length;
- tag = LineTag.FindTag(line, pos);
- }
- // This is for future use, right now Document.Split does it by hand, with some added shortcut logic
- public void Split(Line move_to_line, int split_at) {
- line = move_to_line;
- pos -= split_at;
- tag = LineTag.FindTag(line, pos);
- }
- public override bool Equals(object obj) {
- return this==(Marker)obj;
- }
- public override int GetHashCode() {
- return base.GetHashCode ();
- }
- public override string ToString() {
- return "Marker Line " + line + ", Position " + pos;
- }
- }
- #endregion Structures
- #region Local Variables
- private Line document;
- private int lines;
- private Line sentinel;
- private int document_id;
- private Random random = new Random();
- internal string password_char;
- private StringBuilder password_cache;
- private bool calc_pass;
- private int char_count;
- // For calculating widths/heights
- public static readonly StringFormat string_format = new StringFormat (StringFormat.GenericTypographic);
- private int recalc_suspended;
- private bool recalc_pending;
- private int recalc_start = 1; // This starts at one, since lines are 1 based
- private int recalc_end;
- private bool recalc_optimize;
- private int update_suspended;
- private bool update_pending;
- private int update_start = 1;
- internal bool multiline;
- internal HorizontalAlignment alignment;
- internal bool wrap;
- internal UndoManager undo;
- internal Marker caret;
- internal Marker selection_start;
- internal Marker selection_end;
- internal bool selection_visible;
- internal Marker selection_anchor;
- internal Marker selection_prev;
- internal bool selection_end_anchor;
- internal int viewport_x;
- internal int viewport_y; // The visible area of the document
- internal int viewport_width;
- internal int viewport_height;
- internal int document_x; // Width of the document
- internal int document_y; // Height of the document
- internal Rectangle invalid;
- internal int crlf_size; // 1 or 2, depending on whether we use \r\n or just \n
- internal TextBoxBase owner; // Who's owning us?
- static internal int caret_width = 1;
- static internal int caret_shift = 1;
- internal int left_margin = 2; // A left margin for all lines
- internal int top_margin = 2;
- internal int right_margin = 2;
- #endregion // Local Variables
- #region Constructors
- internal Document (TextBoxBase owner)
- {
- lines = 0;
- this.owner = owner;
- multiline = true;
- password_char = "";
- calc_pass = false;
- recalc_pending = false;
- // Tree related stuff
- sentinel = new Line (this, LineEnding.None);
- sentinel.color = LineColor.Black;
- document = sentinel;
- // We always have a blank line
- owner.HandleCreated += new EventHandler(owner_HandleCreated);
- owner.VisibleChanged += new EventHandler(owner_VisibleChanged);
- Add (1, String.Empty, owner.Font, ThemeEngine.Current.ResPool.GetSolidBrush (owner.ForeColor), LineEnding.None);
- undo = new UndoManager (this);
- selection_visible = false;
- selection_start.line = this.document;
- selection_start.pos = 0;
- selection_start.tag = selection_start.line.tags;
- selection_end.line = this.document;
- selection_end.pos = 0;
- selection_end.tag = selection_end.line.tags;
- selection_anchor.line = this.document;
- selection_anchor.pos = 0;
- selection_anchor.tag = selection_anchor.line.tags;
- caret.line = this.document;
- caret.pos = 0;
- caret.tag = caret.line.tags;
- viewport_x = 0;
- viewport_y = 0;
- crlf_size = 2;
- // Default selection is empty
- document_id = random.Next();
- string_format.Trimming = StringTrimming.None;
- string_format.FormatFlags = StringFormatFlags.DisplayFormatControl;
- UpdateMargins ();
- }
- #endregion
- #region Internal Properties
- internal Line Root {
- get {
- return document;
- }
- set {
- document = value;
- }
- }
- internal int Lines {
- get {
- return lines;
- }
- }
- internal Line CaretLine {
- get {
- return caret.line;
- }
- }
- internal int CaretPosition {
- get {
- return caret.pos;
- }
- }
- internal Point Caret {
- get {
- return new Point((int)caret.tag.line.widths[caret.pos] + caret.line.X, caret.line.Y);
- }
- }
- internal LineTag CaretTag {
- get {
- return caret.tag;
- }
- set {
- caret.tag = value;
- }
- }
- internal int CRLFSize {
- get {
- return crlf_size;
- }
- set {
- crlf_size = value;
- }
- }
- internal string PasswordChar {
- get {
- return password_char;
- }
- set {
- password_char = value;
- PasswordCache.Length = 0;
- if ((password_char.Length != 0) && (password_char[0] != '\0')) {
- calc_pass = true;
- } else {
- calc_pass = false;
- }
- }
- }
- private StringBuilder PasswordCache {
- get {
- if (password_cache == null)
- password_cache = new StringBuilder();
- return password_cache;
- }
- }
- internal int ViewPortX {
- get {
- return viewport_x;
- }
- set {
- viewport_x = value;
- }
- }
- internal int Length {
- get {
- return char_count + lines - 1; // Add \n for each line but the last
- }
- }
- private int CharCount {
- get {
- return char_count;
- }
- set {
- char_count = value;
- if (LengthChanged != null) {
- LengthChanged(this, EventArgs.Empty);
- }
- }
- }
- internal int ViewPortY {
- get {
- return viewport_y;
- }
- set {
- viewport_y = value;
- }
- }
- internal int ViewPortWidth {
- get {
- return viewport_width;
- }
- set {
- viewport_width = value;
- }
- }
- internal int ViewPortHeight {
- get {
- return viewport_height;
- }
- set {
- viewport_height = value;
- }
- }
- internal int Width {
- get {
- return this.document_x;
- }
- }
- internal int Height {
- get {
- return this.document_y;
- }
- }
- internal bool SelectionVisible {
- get {
- return selection_visible;
- }
- }
- internal bool Wrap {
- get {
- return wrap;
- }
- set {
- wrap = value;
- }
- }
- #endregion // Internal Properties
- #region Private Methods
- internal void UpdateMargins ()
- {
- switch (owner.actual_border_style) {
- case BorderStyle.None:
- left_margin = 0;
- top_margin = 0;
- right_margin = 1;
- break;
- case BorderStyle.FixedSingle:
- left_margin = 2;
- top_margin = 2;
- right_margin = 3;
- break;
- case BorderStyle.Fixed3D:
- left_margin = 1;
- top_margin = 1;
- right_margin = 2;
- break;
- }
- }
- internal void SuspendRecalc ()
- {
- recalc_suspended++;
- }
- internal void ResumeRecalc (bool immediate_update)
- {
- if (recalc_suspended > 0)
- recalc_suspended--;
- if (immediate_update && recalc_suspended == 0 && recalc_pending) {
- RecalculateDocument (owner.CreateGraphicsInternal(), recalc_start, recalc_end, recalc_optimize);
- recalc_pending = false;
- }
- }
- internal void SuspendUpdate ()
- {
- update_suspended++;
- }
- internal void ResumeUpdate (bool immediate_update)
- {
- if (update_suspended > 0)
- update_suspended--;
- if (immediate_update && update_suspended == 0 && update_pending) {
- UpdateView (GetLine (update_start), 0);
- update_pending = false;
- }
- }
- // For debugging
- internal int DumpTree(Line line, bool with_tags) {
- int total;
- total = 1;
- Console.Write("Line {0} [# {1}], Y: {2}, ending style: {3}, Text: '{4}'",
- line.line_no, line.GetHashCode(), line.Y, line.ending,
- line.text != null ? line.text.ToString() : "undefined");
- if (line.left == sentinel) {
- Console.Write(", left = sentinel");
- } else if (line.left == null) {
- Console.Write(", left = NULL");
- }
- if (line.right == sentinel) {
- Console.Write(", right = sentinel");
- } else if (line.right == null) {
- Console.Write(", right = NULL");
- }
- Console.WriteLine("");
- if (with_tags) {
- LineTag tag;
- int count;
- int length;
- tag = line.tags;
- count = 1;
- length = 0;
- Console.Write(" Tags: ");
- while (tag != null) {
- Console.Write("{0} <{1}>-<{2}>", count++, tag.start, tag.End
- /*line.text.ToString (tag.start - 1, tag.length)*/);
- length += tag.Length;
- if (tag.line != line) {
- Console.Write("BAD line link");
- throw new Exception("Bad line link in tree");
- }
- tag = tag.next;
- if (tag != null) {
- Console.Write(", ");
- }
- }
- if (length > line.text.Length) {
- throw new Exception(String.Format("Length of tags more than length of text on line (expected {0} calculated {1})", line.text.Length, length));
- } else if (length < line.text.Length) {
- throw new Exception(String.Format("Length of tags less than length of text on line (expected {0} calculated {1})", line.text.Length, length));
- }
- Console.WriteLine("");
- }
- if (line.left != null) {
- if (line.left != sentinel) {
- total += DumpTree(line.left, with_tags);
- }
- } else {
- if (line != sentinel) {
- throw new Exception("Left should not be NULL");
- }
- }
- if (line.right != null) {
- if (line.right != sentinel) {
- total += DumpTree(line.right, with_tags);
- }
- } else {
- if (line != sentinel) {
- throw new Exception("Right should not be NULL");
- }
- }
- for (int i = 1; i <= this.lines; i++) {
- if (GetLine(i) == null) {
- throw new Exception(String.Format("Hole in line order, missing {0}", i));
- }
- }
- if (line == this.Root) {
- if (total < this.lines) {
- throw new Exception(String.Format("Not enough nodes in tree, found {0}, expected {1}", total, this.lines));
- } else if (total > this.lines) {
- throw new Exception(String.Format("Too many nodes in tree, found {0}, expected {1}", total, this.lines));
- }
- }
- return total;
- }
- private void SetSelectionVisible (bool value)
- {
- selection_visible = value;
- // cursor and selection are enemies, we can't have both in the same room at the same time
- if (owner.IsHandleCreated && !owner.show_caret_w_selection)
- XplatUI.CaretVisible (owner.Handle, !selection_visible);
- }
- private void DecrementLines(int line_no) {
- int current;
- current = line_no;
- while (current <= lines) {
- GetLine(current).line_no--;
- current++;
- }
- return;
- }
- private void IncrementLines(int line_no) {
- int current;
- current = this.lines;
- while (current >= line_no) {
- GetLine(current).line_no++;
- current--;
- }
- return;
- }
- private void RebalanceAfterAdd(Line line1) {
- Line line2;
- while ((line1 != document) && (line1.parent.color == LineColor.Red)) {
- if (line1.parent == line1.parent.parent.left) {
- line2 = line1.parent.parent.right;
- if ((line2 != null) && (line2.color == LineColor.Red)) {
- line1.parent.color = LineColor.Black;
- line2.color = LineColor.Black;
- line1.parent.parent.color = LineColor.Red;
- line1 = line1.parent.parent;
- } else {
- if (line1 == line1.parent.right) {
- line1 = line1.parent;
- RotateLeft(line1);
- }
- line1.parent.color = LineColor.Black;
- line1.parent.parent.color = LineColor.Red;
- RotateRight(line1.parent.parent);
- }
- } else {
- line2 = line1.parent.parent.left;
- if ((line2 != null) && (line2.color == LineColor.Red)) {
- line1.parent.color = LineColor.Black;
- line2.color = LineColor.Black;
- line1.parent.parent.color = LineColor.Red;
- line1 = line1.parent.parent;
- } else {
- if (line1 == line1.parent.left) {
- line1 = line1.parent;
- RotateRight(line1);
- }
- line1.parent.color = LineColor.Black;
- line1.parent.parent.color = LineColor.Red;
- RotateLeft(line1.parent.parent);
- }
- }
- }
- document.color = LineColor.Black;
- }
- private void RebalanceAfterDelete(Line line1) {
- Line line2;
- while ((line1 != document) && (line1.color == LineColor.Black)) {
- if (line1 == line1.parent.left) {
- line2 = line1.parent.right;
- if (line2.color == LineColor.Red) {
- line2.color = LineColor.Black;
- line1.parent.color = LineColor.Red;
- RotateLeft(line1.parent);
- line2 = line1.parent.right;
- }
- if ((line2.left.color == LineColor.Black) && (line2.right.color == LineColor.Black)) {
- line2.color = LineColor.Red;
- line1 = line1.parent;
- } else {
- if (line2.right.color == LineColor.Black) {
- line2.left.color = LineColor.Black;
- line2.color = LineColor.Red;
- RotateRight(line2);
- line2 = line1.parent.right;
- }
- line2.color = line1.parent.color;
- line1.parent.color = LineColor.Black;
- line2.right.color = LineColor.Black;
- RotateLeft(line1.parent);
- line1 = document;
- }
- } else {
- line2 = line1.parent.left;
- if (line2.color == LineColor.Red) {
- line2.color = LineColor.Black;
- line1.parent.color = LineColor.Red;
- RotateRight(line1.parent);
- line2 = line1.parent.left;
- }
- if ((line2.right.color == LineColor.Black) && (line2.left.color == LineColor.Black)) {
- line2.color = LineColor.Red;
- line1 = line1.parent;
- } else {
- if (line2.left.color == LineColor.Black) {
- line2.right.color = LineColor.Black;
- line2.color = LineColor.Red;
- RotateLeft(line2);
- line2 = line1.parent.left;
- }
- line2.color = line1.parent.color;
- line1.parent.color = LineColor.Black;
- line2.left.color = LineColor.Black;
- RotateRight(line1.parent);
- line1 = document;
- }
- }
- }
- line1.color = LineColor.Black;
- }
- private void RotateLeft(Line line1) {
- Line line2 = line1.right;
- line1.right = line2.left;
- if (line2.left != sentinel) {
- line2.left.parent = line1;
- }
- if (line2 != sentinel) {
- line2.parent = line1.parent;
- }
- if (line1.parent != null) {
- if (line1 == line1.parent.left) {
- line1.parent.left = line2;
- } else {
- line1.parent.right = line2;
- }
- } else {
- document = line2;
- }
- line2.left = line1;
- if (line1 != sentinel) {
- line1.parent = line2;
- }
- }
- private void RotateRight(Line line1) {
- Line line2 = line1.left;
- line1.left = line2.right;
- if (line2.right != sentinel) {
- line2.right.parent = line1;
- }
- if (line2 != sentinel) {
- line2.parent = line1.parent;
- }
- if (line1.parent != null) {
- if (line1 == line1.parent.right) {
- line1.parent.right = line2;
- } else {
- line1.parent.left = line2;
- }
- } else {
- document = line2;
- }
- line2.right = line1;
- if (line1 != sentinel) {
- line1.parent = line2;
- }
- }
- internal void UpdateView(Line line, int pos) {
- if (!owner.IsHandleCreated) {
- return;
- }
- if (update_suspended > 0) {
- update_start = Math.Min (update_start, line.line_no);
- // update_end = Math.Max (update_end, line.line_no);
- // recalc_optimize = true;
- update_pending = true;
- return;
- }
- // Optimize invalidation based on Line alignment
- if (RecalculateDocument(owner.CreateGraphicsInternal(), line.line_no, line.line_no, true)) {
- // Lineheight changed, invalidate the rest of the document
- if ((line.Y - viewport_y) >=0 ) {
- // We formatted something that's in view, only draw parts of the screen
- owner.Invalidate(new Rectangle(0, line.Y - viewport_y, viewport_width, owner.Height - line.Y - viewport_y));
- } else {
- // The tag was above the visible area, draw everything
- owner.Invalidate();
- }
- } else {
- switch(line.alignment) {
- case HorizontalAlignment.Left: {
- owner.Invalidate(new Rectangle(line.X + (int)line.widths[pos] - viewport_x - 1, line.Y - viewport_y, viewport_width, line.height + 1));
- break;
- }
- case HorizontalAlignment.Center: {
- owner.Invalidate(new Rectangle(line.X, line.Y - viewport_y, viewport_width, line.height + 1));
- break;
- }
- case HorizontalAlignment.Right: {
- owner.Invalidate(new Rectangle(line.X, line.Y - viewport_y, (int)line.widths[pos + 1] - viewport_x + line.X, line.height + 1));
- break;
- }
- }
- }
- }
- // Update display from line, down line_count lines; pos is unused, but required for the signature
- internal void UpdateView(Line line, int line_count, int pos) {
- if (!owner.IsHandleCreated) {
- return;
- }
- if (recalc_suspended > 0) {
- recalc_start = Math.Min (recalc_start, line.line_no);
- recalc_end = Math.Max (recalc_end, line.line_no + line_count);
- recalc_optimize = true;
- recalc_pending = true;
- return;
- }
- int start_line_top = line.Y;
- int end_line_bottom;
- Line end_line;
- end_line = GetLine (line.line_no + line_count);
- if (end_line == null)
- end_line = GetLine (lines);
- end_line_bottom = end_line.Y + end_line.height;
-
- if (RecalculateDocument(owner.CreateGraphicsInternal(), line.line_no, line.line_no + line_count, true)) {
- // Lineheight changed, invalidate the rest of the document
- if ((line.Y - viewport_y) >=0 ) {
- // We formatted something that's in view, only draw parts of the screen
- owner.Invalidate(new Rectangle(0, line.Y - viewport_y, viewport_width, owner.Height - line.Y - viewport_y));
- } else {
- // The tag was above the visible area, draw everything
- owner.Invalidate();
- }
- } else {
- int x = 0 - viewport_x;
- int w = viewport_width;
- int y = Math.Min (start_line_top - viewport_y, line.Y - viewport_y);
- int h = Math.Max (end_line_bottom - y, end_line.Y + end_line.height - y);
- owner.Invalidate (new Rectangle (x, y, w, h));
- }
- }
- #endregion // Private Methods
- #region Internal Methods
- // Clear the document and reset state
- internal void Empty() {
- document = sentinel;
- lines = 0;
- // We always have a blank line
- Add (1, String.Empty, owner.Font, ThemeEngine.Current.ResPool.GetSolidBrush (owner.ForeColor), LineEnding.None);
-
- this.RecalculateDocument(owner.CreateGraphicsInternal());
- PositionCaret(0, 0);
- SetSelectionVisible (false);
- selection_start.line = this.document;
- selection_start.pos = 0;
- selection_start.tag = selection_start.line.tags;
- selection_end.line = this.document;
- selection_end.pos = 0;
- selection_end.tag = selection_end.line.tags;
- char_count = 0;
- viewport_x = 0;
- viewport_y = 0;
- document_x = 0;
- document_y = 0;
- if (owner.IsHandleCreated)
- owner.Invalidate ();
- }
- internal void PositionCaret(Line line, int pos) {
- caret.tag = line.FindTag (pos);
- MoveCaretToTextTag ();
- caret.line = line;
- caret.pos = pos;
- if (owner.IsHandleCreated) {
- if (owner.Focused) {
- if (caret.height != caret.tag.height)
- XplatUI.CreateCaret (owner.Handle, caret_width, caret.height);
- XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.X - viewport_x, caret.line.Y + caret.tag.shift - viewport_y + caret_shift);
- }
- if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
- }
- // We set this at the end because we use the heights to determine whether or
- // not we need to recreate the caret
- caret.height = caret.tag.height;
- }
- internal void PositionCaret(int x, int y) {
- if (!owner.IsHandleCreated) {
- return;
- }
- caret.tag = FindCursor(x, y, out caret.pos);
- MoveCaretToTextTag ();
-
- caret.line = caret.tag.line;
- caret.height = caret.tag.height;
- if (owner.ShowSelection && (!selection_visible || owner.show_caret_w_selection)) {
- XplatUI.CreateCaret (owner.Handle, caret_width, caret.height);
- XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.X - viewport_x, caret.line.Y + caret.tag.shift - viewport_y + caret_shift);
- }
- if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
- }
- internal void CaretHasFocus() {
- if ((caret.tag != null) && owner.IsHandleCreated) {
- XplatUI.CreateCaret(owner.Handle, caret_width, caret.height);
- XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.X - viewport_x, caret.line.Y + caret.tag.shift - viewport_y + caret_shift);
- DisplayCaret ();
- }
- if (owner.IsHandleCreated && SelectionLength () > 0) {
- InvalidateSelectionArea ();
- }
- }
- internal void CaretLostFocus() {
- if (!owner.IsHandleCreated) {
- return;
- }
- XplatUI.DestroyCaret(owner.Handle);
- }
- internal void AlignCaret() {
- if (!owner.IsHandleCreated) {
- return;
- }
- caret.tag = LineTag.FindTag (caret.line, caret.pos);
- MoveCaretToTextTag ();
- caret.height = caret.tag.height;
- if (owner.Focused) {
- XplatUI.CreateCaret(owner.Handle, caret_width, caret.height);
- XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.X - viewport_x, caret.line.Y + caret.tag.shift - viewport_y + caret_shift);
- DisplayCaret ();
- }
- if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
- }
- internal void UpdateCaret() {
- if (!owner.IsHandleCreated || caret.tag == null) {
- return;
- }
- MoveCaretToTextTag ();
- if (caret.tag.height != caret.height) {
- caret.height = caret.tag.height;
- if (owner.Focused) {
- XplatUI.CreateCaret(owner.Handle, caret_width, caret.height);
- }
- }
- if (owner.Focused) {
- XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.X - viewport_x, caret.line.Y + caret.tag.shift - viewport_y + caret_shift);
- DisplayCaret ();
- }
-
- if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
- }
- internal void DisplayCaret() {
- if (!owner.IsHandleCreated) {
- return;
- }
- if (owner.ShowSelection && (!selection_visible || owner.show_caret_w_selection)) {
- XplatUI.CaretVisible(owner.Handle, true);
- }
- }
- internal void HideCaret() {
- if (!owner.IsHandleCreated) {
- return;
- }
- if (owner.Focused) {
- XplatUI.CaretVisible(owner.Handle, false);
- }
- }
-
- internal void MoveCaretToTextTag ()
- {
- if (caret.tag == null || caret.tag.IsTextTag)
- return;
-
- if (caret.pos < caret.tag.start) {
- caret.tag = caret.tag.previous;
- } else {
- caret.tag = caret.tag.next;
- }
- }
- internal void MoveCaret(CaretDirection direction) {
- // FIXME should we use IsWordSeparator to detect whitespace, instead
- // of looking for actual spaces in the Word move cases?
- bool nowrap = false;
- switch(direction) {
- case CaretDirection.CharForwardNoWrap:
- nowrap = true;
- goto case CaretDirection.CharForward;
- case CaretDirection.CharForward: {
- caret.pos++;
- if (caret.pos > caret.line.TextLengthWithoutEnding ()) {
- if (!nowrap) {
- // Go into next line
- if (caret.line.line_no < this.lines) {
- caret.line = GetLine(caret.line.line_no+1);
- caret.pos = 0;
- caret.tag = caret.line.tags;
- } else {
- caret.pos--;
- }
- } else {
- // Single line; we stay where we are
- caret.pos--;
- }
- } else {
- if ((caret.tag.start - 1 + caret.tag.Length) < caret.pos) {
- caret.tag = caret.tag.next;
- }
- }
- UpdateCaret();
- return;
- }
- case CaretDirection.CharBackNoWrap:
- nowrap = true;
- goto case CaretDirection.CharBack;
- case CaretDirection.CharBack: {
- if (caret.pos > 0) {
- // caret.pos--; // folded into the if below
-
- if (--caret.pos > 0) {
- if (caret.tag.start > caret.pos) {
- caret.tag = caret.tag.previous;
- }
- }
- } else {
- if (caret.line.line_no > 1 && !nowrap) {
- caret.line = GetLine(caret.line.line_no - 1);
- caret.pos = caret.line.TextLengthWithoutEnding ();
- caret.tag = LineTag.FindTag(caret.line, caret.pos);
- }
- }
- UpdateCaret();
- return;
- }
- case CaretDirection.WordForward: {
- int len;
- len = caret.line.text.Length;
- if (caret.pos < len) {
- while ((caret.pos < len) && (caret.line.text[caret.pos] != ' ')) {
- caret.pos++;
- }
- if (caret.pos < len) {
- // Skip any whitespace
- while ((caret.pos < len) && (caret.line.text[caret.pos] == ' ')) {
- caret.pos++;
- }
- }
- caret.tag = LineTag.FindTag(caret.line, caret.pos);
- } else {
- if (caret.line.line_no < this.lines) {
- caret.line = GetLine(caret.line.line_no + 1);
- caret.pos = 0;
- caret.tag = caret.line.tags;
- }
- }
- UpdateCaret();
- return;
- }
- case CaretDirection.WordBack: {
- if (caret.pos > 0) {
- caret.pos--;
- while ((caret.pos > 0) && (caret.line.text[caret.pos] == ' ')) {
- caret.pos--;
- }
- while ((caret.pos > 0) && (caret.line.text[caret.pos] != ' ')) {
- caret.pos--;
- }
- if (caret.line.text.ToString(caret.pos, 1) == " ") {
- if (caret.pos != 0) {
- caret.pos++;
- } else {
- caret.line = GetLine(caret.line.line_no - 1);
- caret.pos = caret.line.text.Length;
- }
- }
- caret.tag = LineTag.FindTag(caret.line, caret.pos);
- } else {
- if (caret.line.line_no > 1) {
- caret.line = GetLine(caret.line.line_no - 1);
- caret.pos = caret.line.text.Length;
- caret.tag = LineTag.FindTag(caret.line, caret.pos);
- }
- }
- UpdateCaret();
- return;
- }
- case CaretDirection.LineUp: {
- if (caret.line.line_no > 1) {
- int pixel;
- pixel = (int)caret.line.widths[caret.pos];
- PositionCaret(pixel, GetLine(caret.line.line_no - 1).Y);
- DisplayCaret ();
- }
- return;
- }
- case CaretDirection.LineDown: {
- if (caret.line.line_no < lines) {
- int pixel;
- pixel = (int)caret.line.widths[caret.pos];
- PositionCaret(pixel, GetLine(caret.line.line_no + 1).Y);
- DisplayCaret ();
- }
- return;
- }
- case CaretDirection.Home: {
- if (caret.pos > 0) {
- caret.pos = 0;
- caret.tag = caret.line.tags;
- UpdateCaret();
- }
- return;
- }
- case CaretDirection.End: {
- if (caret.pos < caret.line.TextLengthWithoutEnding ()) {
- caret.pos = caret.line.TextLengthWithoutEnding ();
- caret.tag = LineTag.FindTag(caret.line, caret.pos);
- UpdateCaret();
- }
- return;
- }
- case CaretDirection.PgUp: {
- if (viewport_y == 0 && owner.richtext) {
- owner.vscroll.Value = 0;
- Line line = GetLine (1);
- PositionCaret (line, 0);
- }
- int y_offset = caret.line.Y + caret.line.height - 1 - viewport_y;
- int index;
- LineTag top = FindCursor ((int) caret.line.widths [caret.pos],
- viewport_y - viewport_height, out index);
- owner.vscroll.Value = Math.Min (top.line.Y, owner.vscroll.Maximum - viewport_height);
- PositionCaret ((int) caret.line.widths [caret.pos], y_offset + viewport_y);
- return;
- }
- case CaretDirection.PgDn: {
- if (viewport_y + viewport_height >= document_y && owner.richtext) {
- owner.vscroll.Value = owner.vscroll.Maximum - viewport_height + 1;
- Line line = GetLine (lines);
- PositionCaret (line, line.Text.Length);
- }
- int y_offset = caret.line.Y - viewport_y;
- int index;
- LineTag top = FindCursor ((int) caret.line.widths [caret.pos],
- viewport_y + viewport_height, out index);
- owner.vscroll.Value = Math.Min (top.line.Y, owner.vscroll.Maximum - viewport_height);
- PositionCaret ((int) caret.line.widths [caret.pos], y_offset + viewport_y);
-
- return;
- }
- case CaretDirection.CtrlPgUp: {
- PositionCaret(0, viewport_y);
- DisplayCaret ();
- return;
- }
- case CaretDirection.CtrlPgDn: {
- Line line;
- LineTag tag;
- int index;
- tag = FindTag(0, viewport_y + viewport_height, out index, false);
- if (tag.line.line_no > 1) {
- line = GetLine(tag.line.line_no - 1);
- } else {
- line = tag.line;
- }
- PositionCaret(line, line.Text.Length);
- DisplayCaret ();
- return;
- }
- case CaretDirection.CtrlHome: {
- caret.line = GetLine(1);
- caret.pos = 0;
- caret.tag = caret.line.tags;
- UpdateCaret();
- return;
- }
- case CaretDirection.CtrlEnd: {
- caret.line = GetLine(lines);
- caret.pos = caret.line.TextLengthWithoutEnding ();
- caret.tag = LineTag.FindTag(caret.line, caret.pos);
- UpdateCaret();
- return;
- }
- case CaretDirection.SelectionStart: {
- caret.line = selection_start.line;
- caret.pos = selection_start.pos;
- caret.tag = selection_start.tag;
- UpdateCaret();
- return;
- }
- case CaretDirection.SelectionEnd: {
- caret.line = selection_end.line;
- caret.pos = selection_end.pos;
- caret.tag = selection_end.tag;
- UpdateCaret();
- return;
- }
- }
- }
- internal void DumpDoc ()
- {
- Console.WriteLine ("<doc lines='{0}'>", lines);
- for (int i = 1; i <= lines ; i++) {
- Line line = GetLine (i);
- Console.WriteLine ("<line no='{0}' ending='{1}'>", line.line_no, line.ending);
- LineTag tag = line.tags;
- while (tag != null) {
- Console.Write ("\t<tag type='{0}' span='{1}->{2}' font='{3}' color='{4}'>",
- tag.GetType (), tag.start, tag.Length, tag.font, tag.color.Color);
- Console.Write (tag.Text ());
- Console.WriteLine ("</tag>");
- tag = tag.next;
- }
- Console.WriteLine ("</line>");
- }
- Console.WriteLine ("</doc>");
- }
- internal void Draw (Graphics g, Rectangle clip)
- {
- Line line; // Current line being drawn
- LineTag tag; // Current tag being drawn
- int start; // First line to draw
- int end; // Last line to draw
- StringBuilder text; // String representing the current line
- int line_no;
- Brush tag_brush;
- Brush current_brush;
- Brush disabled_brush;
- Brush readonly_brush;
- Brush hilight;
- Brush hilight_text;
- // First, figure out from what line to what line we need to draw
- if (multiline) {
- start = GetLineByPixel(clip.Top + viewport_y, false).line_no;
- end = GetLineByPixel(clip.Bottom + viewport_y, false).line_no;
- } else {
- start = GetLineByPixel(clip.Left + viewport_x, false).line_no;
- end = GetLineByPixel(clip.Right + viewport_x, false).line_no;
- }
- ///
- /// We draw the single border ourself
- ///
- if (owner.actual_border_style == BorderStyle.FixedSingle) {
- ControlPaint.DrawBorder (g, owner.ClientRectangle, Color.Black, ButtonBorderStyle.Solid);
- }
- /// Make sure that we aren't drawing one more line then we need to
- line = GetLine (end - 1);
- if (line != null && clip.Bottom == line.Y + line.height + viewport_y)
- end--;
- line_no = start;
- #if Debug
- DateTime n = DateTime.Now;
- Console.WriteLine ("Started drawing: {0}s {1}ms", n.Second, n.Millisecond);
- Console.WriteLine ("CLIP: {0}", clip);
- Console.WriteLine ("S: {0}", GetLine (start).text);
- Console.WriteLine ("E: {0}", GetLine (end).text);
- #endif
- disabled_brush = ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorGrayText);
- readonly_brush = ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorControlText);
- hilight = ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorHighlight);
- hilight_text = ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorHighlightText);
- // Non multiline selection can be handled outside of the loop
- if (!multiline && selection_visible && owner.ShowSelection) {
- g.FillRectangle (hilight,
- selection_start.line.widths [selection_start.pos] +
- selection_start.line.X - viewport_x,
- selection_start.line.Y,
- (selection_end.line.X + selection_end.line.widths [selection_end.pos]) -
- (selection_start.line.X + selection_start.line.widths [selection_start.pos]),
- selection_start.line.height);
- }
- while (line_no <= end) {
- line = GetLine (line_no);
- float line_y = line.Y - viewport_y;
-
- tag = line.tags;
- if (!calc_pass) {
- text = line.text;
- } else {
- if (PasswordCache.Length < line.text.Length)
- PasswordCache.Append(Char.Parse(password_char), line.text.Length - PasswordCache.Length);
- else if (PasswordCache.Length > line.text.Length)
- PasswordCache.Remove(line.text.Length, PasswordCache.Length - line.text.Length);
- text = PasswordCache;
- }
- int line_selection_start = text.Length + 1;
- int line_selection_end = text.Length + 1;
- if (selection_visible && owner.ShowSelection &&
- (line_no >= selection_start.line.line_no) &&
- (line_no <= selection_end.line.line_no)) {
- if (line_no == selection_start.line.line_no)
- line_selection_start = selection_start.pos + 1;
- else
- line_selection_start = 1;
- if (line_no == selection_end.line.line_no)
- line_selection_end = selection_end.pos + 1;
- else
- line_selection_end = text.Length + 1;
- if (line_selection_end == line_selection_start) {
- // There isn't really selection
- line_selection_start = text.Length + 1;
- line_selection_end = line_selection_start;
- } else if (multiline) {
- // lets draw some selection baby!! (non multiline selection is drawn outside the loop)
- g.FillRectangle (hilight,
- line.widths [line_selection_start - 1] + line.X - viewport_x,
- line_y, line.widths [line_selection_end - 1] - line.widths [line_selection_start - 1],
- line.height);
- }
- }
- current_brush = line.tags.color;
- while (tag != null) {
- // Skip empty tags
- if (tag.Length == 0) {
- tag = tag.next;
- continue;
- }
- if (((tag.X + tag.Width) < (clip.Left - viewport_x)) && (tag.X > (clip.Right - viewport_x))) {
- tag = tag.next;
- continue;
- }
- if (tag.back_color != Color.Empty) {
- g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (tag.back_color), tag.X + line.X - viewport_x,
- line_y + tag.shift, tag.Width, line.height);
- }
- tag_brush = tag.color;
- current_brush = tag_brush;
- if (!owner.Enabled) {
- Color a = ((SolidBrush) tag.color).Color;
- Color b = ThemeEngine.Current.ColorWindowText;
- if ((a.R == b.R) && (a.G == b.G) && (a.B == b.B)) {
- tag_brush = disabled_brush;
- }
- } else if (owner.read_only && !owner.backcolor_set) {
- tag_brush = readonly_brush;
- }
- int tag_pos = tag.start;
- current_brush = tag_brush;
- while (tag_pos < tag.start + tag.Length) {
- int old_tag_pos = tag_pos;
- if (tag_pos >= line_selection_start && tag_pos < line_selection_end) {
- current_brush = hilight_text;
- tag_pos = Math.Min (tag.End, line_selection_end);
- } else if (tag_pos < line_selection_start) {
- current_brush = tag_brush;
- tag_pos = Math.Min (tag.End, line_selection_start);
- } else {
- current_brush = tag_brush;
- tag_pos = tag.End;
- }
- tag.Draw (g, current_brush,
- line.X - viewport_x,
- line_y + tag.shift,
- old_tag_pos - 1, Math.Min (tag.start + tag.Length, tag_pos) - 1,
- text.ToString() );
- }
- tag = tag.next;
- }
- line.DrawEnding (g, line_y);
- line_no++;
- }
- }
- internal int GetLineEnding (string line, int start, out LineEnding ending)
- {
- int res;
- res = line.IndexOf ('\r', start);
- if (res != -1) {
- if (res + 2 < line.Length && line [res + 1] == '\r' && line [res + 2] == '\n') {
- ending = LineEnding.Soft;
- return res;
- }
- if (res + 1 < line.Length && line [res + 1] == '\n') {
- ending = LineEnding.Hard;
- return res;
- }
- ending = LineEnding.Limp;
- return res;
- }
- res = line.IndexOf ('\n', start);
- if (res != -1) {
- ending = LineEnding.Rich;
- return res;
- }
- ending = LineEnding.Wrap;
- return line.Length;
- }
- internal int LineEndingLength (LineEnding ending)
- {
- int res = 0;
- switch (ending) {
- case LineEnding.Limp:
- case LineEnding.Rich:
- res = 1;
- break;
- case LineEnding.Hard:
- res = 2;
- break;
- case LineEnding.Soft:
- res = 3;
- break;
- }
- return res;
- }
- internal string LineEndingToString (LineEnding ending)
- {
- string res = String.Empty;
- switch (ending) {
- case LineEnding.Limp:
- res = "\r";
- break;
- case LineEnding.Hard:
- res = "\r\n";
- break;
- case LineEnding.Soft:
- res = "\r\r\n";
- break;
- case LineEnding.Rich:
- res = "\n";
- break;
- }
- return res;
- }
-
- // Insert multi-line text at the given position; use formatting at insertion point for inserted text
- internal void Insert(Line line, int pos, bool update_caret, string s) {
- int break_index;
- int base_line;
- int old_line_count;
- int count = 1;
- LineEnding ending;
- LineTag tag = LineTag.FindTag (line, pos);
-
- SuspendRecalc ();
-
- base_line = line.line_no;
- old_line_count = lines;
- break_index = GetLineEnding (s, 0, out ending);
- // Bump the text at insertion point a line down if we're inserting more than one line
- if (break_index != s.Length) {
- Split (line, pos);
- line.ending = ending;
- // Remainder of start line is now in base_line + 1
- }
- InsertString (line, pos, s.Substring (0, break_index + LineEndingLength (ending)));
-
- break_index += LineEndingLength (ending);
- while (break_index < s.Length) {
- int next_break = GetLineEnding (s, break_index, out ending);
- string line_text = s.Substring (break_index, next_break - break_index +
- LineEndingLength (ending));
- Add (base_line + count, line_text, line.alignment, tag.font, tag.color, ending);
- Line last = GetLine (base_line + count);
- last.ending = ending;
- count++;
- break_index = next_break + LineEndingLength (ending);
- }
- ResumeRecalc (true);
- UpdateView(line, lines - old_line_count + 1, pos);
- if (update_caret) {
- // Move caret to the end of the inserted text
- Line l = GetLine (line.line_no + lines - old_line_count);
- PositionCaret(l, l.text.Length);
- DisplayCaret ();
- }
- }
- // Inserts a character at the given position
- internal void InsertString(Line line, int pos, string s) {
- InsertString(line.FindTag(pos), pos, s);
- }
- // Inserts a string at the given position
- internal void InsertString(LineTag tag, int pos, string s) {
- Line line;
- int len;
- len = s.Length;
- CharCount += len;
- line = tag.line;
- line.text.Insert(pos, s);
- tag = tag.next;
- while (tag != null) {
- tag.start += len;
- tag = tag.next;
- }
- line.Grow(len);
- line.recalc = true;
- UpdateView(line, pos);
- }
- // Inserts a string at the caret position
- internal void InsertStringAtCaret(string s, bool move_caret) {
- InsertString (caret.tag, caret.pos, s);
- UpdateView(caret.line, caret.pos);
- if (move_caret) {
- caret.pos += s.Length;
- UpdateCaret();
- }
- }
- // Inserts a character at the given position
- internal void InsertChar(Line line, int pos, char ch) {
- InsertChar(line.FindTag(pos), pos, ch);
- }
- // Inserts a character at the given position
- internal void InsertChar(LineTag tag, int pos, char ch) {
- Line line;
- CharCount++;
- line = tag.line;
- line.text.Insert(pos, ch);
- tag = tag.next;
- while (tag != null) {
- tag.start++;
- tag = tag.next;
- }
- line.Grow(1);
- line.recalc = true;
- undo.RecordTyping (line, pos, ch);
- UpdateView(line, pos);
- }
- // Inserts a character at the current caret position
- internal void InsertCharAtCaret(char ch, bool move_caret) {
- /*
- LineTag tag;
- CharCount++;
- caret.line.text.Insert(caret.pos, ch);
- caret.tag.length++;
-
- if (caret.tag.next != null) {
- tag = caret.tag.next;
- while (tag != null) {
- tag.start++;
- tag = tag.next;
- }
- }
- caret.line.Grow(1);
- caret.line.recalc = true;
- */
- InsertChar (caret.tag, caret.pos, ch);
- UpdateView(caret.line, caret.pos);
- if (move_caret) {
- caret.pos++;
- UpdateCaret();
- SetSelectionToCaret(true);
- }
- }
-
- internal void InsertPicture (Line line, int pos, RTF.Picture picture)
- {
- //LineTag next_tag;
- LineTag tag;
- int len;
- len = 1;
- // Just a place holder basically
- line.text.Insert (pos, "I");
- PictureTag picture_tag = new PictureTag (line, pos + 1, picture);
- tag = LineTag.FindTag (line, pos);
- picture_tag.CopyFormattingFrom (tag);
- /*next_tag = */tag.Break (pos + 1);
- picture_tag.previous = tag;
- picture_tag.next = tag.next;
- tag.next = picture_tag;
- //
- // Picture tags need to be surrounded by text tags
- //
- if (picture_tag.next == null) {
- picture_tag.next = new LineTag (line, pos + 1);
- picture_tag.next.CopyFormattingFrom (tag);
- picture_tag.next.previous = picture_tag;
- }
- tag = picture_tag.next;
- while (tag != null) {
- tag.start += len;
- tag = tag.next;
- }
- line.Grow (len);
- line.recalc = true;
- UpdateView (line, pos);
- }
- internal void DeleteMultiline (Line start_line, int pos, int length)
- {
- Marker start = new Marker ();
- Marker end = new Marker ();
- int start_index = LineTagToCharIndex (start_line, pos);
- start.line = start_line;
- start.pos = pos;
- start.tag = LineTag.FindTag (start_line, pos);
- CharIndexToLineTag (start_index + length, out end.line,
- out end.tag, out end.pos);
- SuspendUpdate ();
- if (start.line == end.line) {
- DeleteChars (start.tag, pos, end.pos - pos);
- } else {
- // Delete first and last lines
- DeleteChars (start.tag, start.pos, start.line.text.Length - start.pos);
- DeleteChars (end.line.tags, 0, end.pos);
- int current = start.line.line_no + 1;
- if (current < end.line.line_no) {
- for (int i = end.line.line_no - 1; i >= current; i--) {
- Delete (i);
- }
- }
- // BIG FAT WARNING - selection_end.line might be stale due
- // to the above Delete() call. DONT USE IT before hitting the end of this method!
- // Join start and end
- Combine (start.line.line_no, current);
- }
- ResumeUpdate (true);
- }
-
- // Deletes n characters at the given position; it will not delete past line limits
- // pos is 0-based
- internal void DeleteChars(LineTag tag, int pos, int count) {
- Line line;
- bool streamline;
- streamline = false;
- line = tag.line;
- CharCount -= count;
- if (pos == line.text.Length) {
- return;
- }
- line.text.Remove(pos, count);
- // Make sure the tag points to the right spot
- while ((tag != null) && (tag.End) < pos) {
- tag = tag.next;
- }
- if (tag == null) {
- goto Cleanup;
- }
- // Check if we're crossing tag boundaries
- if ((pos + count) > (tag.start + tag.Length - 1)) {
- int left;
- // We have to delete cross tag boundaries
- streamline = true;
- left = count;
- left -= tag.start + tag.Length - pos - 1;
- tag = tag.next;
- while ((tag != null) && (left > 0)) {
- tag.start -= count - left;
- if (tag.Length > left) {
- left = 0;
- } else {
- left -= tag.Length;
- tag = tag.next;
- }
- }
- } else {
- // We got off easy, same tag
- if (tag.Length == 0) {
- streamline = true;
- }
- }
- // Delete empty orphaned tags at the end
- LineTag walk = tag;
- while (walk != null && walk.next != null && walk.next.Length == 0) {
- LineTag t = walk;
- walk.next = walk.next.next;
- if (walk.next != null)
- walk.next.previous = t;
- walk = walk.next;
- }
- // Adjust the start point of any tags following
- if (tag != null) {
- tag = tag.next;
- while (tag != null) {
- tag.start -= count;
- tag = tag.next;
- }
- }
- line.recalc = true;
- if (streamline) {
- line.Streamline(lines);
- }
- Cleanup:
- if (pos >= line.TextLengthWithoutEnding ()) {
- LineEnding ending = line.ending;
- GetLineEnding (line.text.ToString (), 0, out ending);
- if (ending != line.ending) {
- line.ending = ending;
- if (!multiline) {
- UpdateView (line, lines, pos);
- owner.Invalidate ();
- return;
- }
- }
- }
- if (!multiline) {
- UpdateView (line, lines, pos);
- owner.Invalidate ();
- } else
- UpdateView(line, pos);
- }
- // Deletes a character at or after the given position (depending on forward); it will not delete past line limits
- internal void DeleteChar(LineTag tag, int pos, bool forward) {
- Line line;
- bool streamline;
- CharCount--;
- streamline = false;
- line = tag.line;
- if ((pos == 0 && forward == false) || (pos == line.text.Length && forward == true)) {
- return;
- }
- if (forward) {
- line.text.Remove(pos, 1);
- while ((tag != null) && (tag.start + tag.Length - 1) <= pos) {
- tag = tag.next;
- }
- if (tag == null) {
- goto Cleanup;
- }
- // tag.length--;
- if (tag.Length == 0) {
- streamline = true;
- }
- } else {
- pos--;
- line.text.Remove(pos, 1);
- if (pos >= (tag.start - 1)) {
- // tag.length--;
- if (tag.Length == 0) {
- streamline = true;
- }
- } else if (tag.previous != null) {
- // tag.previous.length--;
- if (tag.previous.Length == 0) {
- streamline = true;
- }
- }
- }
- // Delete empty orphaned tags at the end
- LineTag walk = tag;
- while (walk != null && walk.next != null && walk.next.Length == 0) {
- LineTag t = walk;
- walk.next = walk.next.next;
- if (walk.next != null)
- walk.next.previous = t;
- walk = walk.next;
- }
- tag = tag.next;
- while (tag != null) {
- tag.start--;
- tag = tag.next;
- }
- line.recalc = true;
- if (streamline) {
- line.Streamline(lines);
- }
- Cleanup:
- if (pos >= line.TextLengthWithoutEnding ()) {
- LineEnding ending = line.ending;
- GetLineEnding (line.text.ToString (), 0, out ending);
- if (ending != line.ending) {
- line.ending = ending;
- if (!multiline) {
- UpdateView (line, lines, pos);
- owner.Invalidate ();
- return;
- }
- }
- }
- if (!multiline) {
- UpdateView (line, lines, pos);
- owner.Invalidate ();
- } else
- UpdateView(line, pos);
- }
- // Combine two lines
- internal void Combine(int FirstLine, int SecondLine) {
- Combine(GetLine(FirstLine), GetLine(SecondLine));
- }
- internal void Combine(Line first, Line second) {
- LineTag last;
- int shift;
- // strip the ending off of the first lines text
- first.text.Length = first.text.Length - LineEndingLength (first.ending);
- // Combine the two tag chains into one
- last = first.tags;
- // Maintain the line ending style
- first.ending = second.ending;
- while (last.next != null) {
- last = last.next;
- }
- // need to get the shift before setting the next tag since that effects length
- shift = last.start + last.Length - 1;
- last.next = second.tags;
- last.next.previous = last;
- // Fix up references within the chain
- last = last.next;
- while (last != null) {
- last.line = first;
- last.start += shift;
- last = last.next;
- }
- // Combine both lines' strings
- first.text.Insert(first.text.Length, second.text.ToString());
- first.Grow(first.text.Length);
- // Remove the reference to our (now combined) tags from the doomed line
- second.tags = null;
- // Renumber lines
- DecrementLines(first.line_no + 2); // first.line_no + 1 will be deleted, so we need to start renumbering one later
- // Mop up
- first.recalc = true;
- first.height = 0; // This forces RecalcDocument/UpdateView to redraw from this line on
- first.Streamline(lines);
- // Update Caret, Selection, etc
- if (caret.line == second) {
- caret.Combine(first, shift);
- }
- if (selection_anchor.line == second) {
- selection_anchor.Combine(first, shift);
- }
- if (selection_start.line == second) {
- selection_start.Combine(first, shift);
- }
- if (selection_end.line == second) {
- selection_end.Combine(first, shift);
- }
- #if Debug
- Line check_first;
- Line check_second;
- check_first = GetLine(first.line_no);
- check_second = GetLine(check_first.line_no + 1);
- Console.WriteLine("Pre-delete: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
- #endif
- this.Delete(second);
- #if Debug
- check_first = GetLine(first.line_no);
- check_second = GetLine(check_first.line_no + 1);
- Console.WriteLine("Post-delete Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
- #endif
- }
- // Split the line at the position into two
- internal void Split(int LineNo, int pos) {
- Line line;
- LineTag tag;
- line = GetLine(LineNo);
- tag = LineTag.FindTag(line, pos);
- Split(line, tag, pos);
- }
- internal void Split(Line line, int pos) {
- LineTag tag;
- tag = LineTag.FindTag(line, pos);
- Split(line, tag, pos);
- }
- ///<summary>Split line at given tag and position into two lines</summary>
- ///if more space becomes available on previous line</param>
- internal void Split(Line line, LineTag tag, int pos) {
- LineTag new_tag;
- Line new_line;
- bool move_caret;
- bool move_sel_start;
- bool move_sel_end;
- move_caret = false;
- move_sel_start = false;
- move_sel_end = false;
- // Adjust selection and cursors
- if (caret.line == line && caret.pos >= pos) {
- move_caret = true;
- }
- if (selection_start.line == line && selection_start.pos > pos) {
- move_sel_start = true;
- }
- if (selection_end.line == line && selection_end.pos > pos) {
- move_sel_end = true;
- }
- // cover the easy case first
- if (pos == line.text.Length) {
- Add (line.line_no + 1, String.Empty, line.alignment, tag.font, tag.color, line.ending);
- new_line = GetLine (line.line_no + 1);
-
- if (move_caret) {
- caret.line = new_line;
- caret.tag = new_line.tags;
- caret.pos = 0;
- }
- if (move_sel_start) {
- selection_start.line = new_line;
- selection_start.pos = 0;
- selection_start.tag = new_line.tags;
- }
- if (move_sel_end) {
- selection_end.line = new_line;
- selection_end.pos = 0;
- selection_end.tag = new_line.tags;
- }
- return;
- }
- // We need to move the rest of the text into the new line
- Add (line.line_no + 1, line.text.ToString (pos, line.text.Length - pos), line.alignment, tag.font, tag.color, line.ending);
- // Now transfer our tags from this line to the next
- new_line = GetLine(line.line_no + 1);
- line.recalc = true;
- new_line.recalc = true;
- if ((tag.start - 1) == pos) {
- int shift;
- // We can simply break the chain and move the tag into the next line
- if (tag == line.tags) {
- new_tag = new LineTag(line, 1);
- new_tag.CopyFormattingFrom (tag);
- line.tags = new_tag;
- }
- if (tag.previous != null) {
- tag.previous.next = null;
- }
- new_line.tags = tag;
- tag.previous = null;
- tag.line = new_line;
- // Walk the list and correct the start location of the tags we just bumped into the next line
- shift = tag.start - 1;
- new_tag = tag;
- while (new_tag != null) {
- new_tag.start -= shift;
- new_tag.line = new_line;
- new_tag = new_tag.next;
- }
- } else {
- int shift;
- new_tag = new LineTag (new_line, 1);
- new_tag.next = tag.next;
- new_tag.CopyFormattingFrom (tag);
- new_line.tags = new_tag;
- if (new_tag.next != null) {
- new_tag.next.previous = new_tag;
- }
- tag.next = null;
- shift = pos;
- new_tag = new_tag.next;
- while (new_tag != null) {
- new_tag.start -= shift;
- new_tag.line = new_line;
- new_tag = new_tag.next;
- }
- }
- if (move_caret) {
- caret.line = new_line;
- caret.pos = caret.pos - pos;
- caret.tag = caret.line.FindTag(caret.pos);
- }
- if (move_sel_start) {
- selection_start.line = new_line;
- selection_start.pos = selection_start.pos - pos;
- selection_start.tag = new_line.FindTag(selection_start.pos);
- }
- if (move_sel_end) {
- selection_end.line = new_line;
- selection_end.pos = selection_end.pos - pos;
- selection_end.tag = new_line.FindTag(selection_end.pos);
- }
- CharCount -= line.text.Length - pos;
- line.text.Remove(pos, line.text.Length - pos);
- }
- // Adds a line of text, with given font.
- // Bumps any line at that line number that already exists down
- internal void Add (int LineNo, string Text, Font font, SolidBrush color, LineEnding ending)
- {
- Add (LineNo, Text, alignment, font, color, ending);
- }
- internal void Add (int LineNo, string Text, HorizontalAlignment align, Font font, SolidBrush color, LineEnding ending)
- {
- Line add;
- Line line;
- int line_no;
- CharCount += Text.Length;
- if (LineNo<1 || Text == null) {
- if (LineNo<1) {
- throw new ArgumentNullException("LineNo", "Line numbers must be positive");
- } else {
- throw new ArgumentNullException("Text", "Cannot insert NULL line");
- }
- }
- add = new Line (this, LineNo, Text, align, font, color, ending);
- line = document;
- while (line != sentinel) {
- add.parent = line;
- line_no = line.line_no;
- if (LineNo > line_no) {
- line = line.right;
- } else if (LineNo < line_no) {
- line = line.left;
- } else {
- // Bump existing line numbers; walk all nodes to the right of this one and increment line_no
- IncrementLines(line.line_no);
- line = line.left;
- }
- }
- add.left = sentinel;
- add.right = sentinel;
- if (add.parent != null) {
- if (LineNo > add.parent.line_no) {
- add.parent.right = add;
- } else {
- add.parent.left = add;
- }
- } else {
- // Root node
- document = add;
- }
- RebalanceAfterAdd(add);
- lines++;
- }
- internal virtual void Clear() {
- lines = 0;
- CharCount = 0;
- document = sentinel;
- }
- public virtual object Clone() {
- Document clone;
- clone = new Document(null);
- clone.lines = this.lines;
- clone.document = (Line)document.Clone();
- return clone;
- }
- internal void Delete(int LineNo) {
- Line line;
- if (LineNo>lines) {
- return;
- }
- line = GetLine(LineNo);
- CharCount -= line.text.Length;
- DecrementLines(LineNo + 1);
- Delete(line);
- }
- internal void Delete(Line line1) {
- Line line2;// = new Line();
- Line line3;
- if ((line1.left == sentinel) || (line1.right == sentinel)) {
- line3 = line1;
- } else {
- line3 = line1.right;
- while (line3.left != sentinel) {
- line3 = line3.left;
- }
- }
- if (line3.left != sentinel) {
- line2 = line3.left;
- } else {
- line2 = line3.right;
- }
- line2.parent = line3.parent;
- if (line3.parent != null) {
- if(line3 == line3.parent.left) {
- line3.parent.left = line2;
- } else {
- line3.parent.right = line2;
- }
- } else {
- document = line2;
- }
- if (line3 != line1) {
- LineTag tag;
- if (selection_start.line == line3) {
- selection_start.line = line1;
- }
- if (selection_end.line == line3) {
- selection_end.line = line1;
- }
- if (selection_anchor.line == line3) {
- selection_anchor.line = line1;
- }
- if (caret.line == line3) {
- caret.line = line1;
- }
- line1.alignment = line3.alignment;
- line1.ascent = line3.ascent;
- line1.hanging_indent = line3.hanging_indent;
- line1.height = line3.height;
- line1.indent = line3.indent;
- line1.line_no = line3.line_no;
- line1.recalc = line3.recalc;
- line1.right_indent = line3.right_indent;
- line1.ending = line3.ending;
- line1.space = line3.space;
- line1.tags = line3.tags;
- line1.text = line3.text;
- line1.widths = line3.widths;
- line1.offset = line3.offset;
- tag = line1.tags;
- while (tag != null) {
- tag.line = line1;
- tag = tag.next;
- }
- }
- if (line3.color == LineColor.Black)
- RebalanceAfterDelete(line2);
- this.lines--;
- }
- // Invalidate a section of the document to trigger redraw
- internal void Invalidate(Line start, int start_pos, Line end, int end_pos) {
- Line l1;
- Line l2;
- int p1;
- int p2;
- if ((start == end) && (start_pos == end_pos)) {
- return;
- }
- if (end_pos == -1) {
- end_pos = end.text.Length;
- }
-
- // figure out what's before what so the logic below is straightforward
- if (start.line_no < end.line_no) {
- l1 = start;
- p1 = start_pos;
- l2 = end;
- p2 = end_pos;
- } else if (start.line_no > end.line_no) {
- l1 = end;
- p1 = end_pos;
- l2 = start;
- p2 = start_pos;
- } else {
- if (start_pos < end_pos) {
- l1 = start;
- p1 = start_pos;
- l2 = end;
- p2 = end_pos;
- } else {
- l1 = end;
- p1 = end_pos;
- l2 = start;
- p2 = start_pos;
- }
- int endpoint = (int) l1.widths [p2];
- if (p2 == l1.text.Length + 1) {
- endpoint = (int) viewport_width;
- }
- #if Debug
- Console.WriteLine("Invaliding backwards from {0}:{1} to {2}:{3} {4}",
- l1.line_no, p1, l2.line_no, p2,
- new Rectangle(
- (int)l1.widths[p1] + l1.X - viewport_x,
- l1.Y - viewport_y,
- (int)l1.widths[p2],
- l1.height
- )
- );
- #endif
- owner.Invalidate(new Rectangle (
- (int)l1.widths[p1] + l1.X - viewport_x,
- l1.Y - viewport_y,
- endpoint - (int)l1.widths[p1] + 1,
- l1.height));
- return;
- }
- #if Debug
- Console.WriteLine("Invaliding from {0}:{1} to {2}:{3} Start => x={4}, y={5}, {6}x{7}", l1.line_no, p1, l2.line_no, p2, (int)l1.widths[p1] + l1.X - viewport_x, l1.Y - viewport_y, viewport_width, l1.height);
- Console.WriteLine ("invalidate start line: {0} position: {1}", l1.text, p1);
- #endif
- // Three invalidates:
- // First line from start
- owner.Invalidate(new Rectangle((int)l1.widths[p1] + l1.X - viewport_x, l1.Y - viewport_y, viewport_width, l1.height));
-
- // lines inbetween
- if ((l1.line_no + 1) < l2.line_no) {
- int y;
- y = GetLine(l1.line_no + 1).Y;
- owner.Invalidate(new Rectangle(0, y - viewport_y, viewport_width, l2.Y - y));
- #if Debug
- Console.WriteLine("Invaliding from {0}:{1} to {2}:{3} Middle => x={4}, y={5}, {6}x{7}", l1.line_no, p1, l2.line_no, p2, 0, y - viewport_y, viewport_width, l2.Y - y);
- #endif
- }
-
- // Last line to end
- owner.Invalidate(new Rectangle((int)l2.widths[0] + l2.X - viewport_x, l2.Y - viewport_y, (int)l2.widths[p2] + 1, l2.height));
- #if Debug
- Console.WriteLine("Invaliding from {0}:{1} to {2}:{3} End => x={4}, y={5}, {6}x{7}", l1.line_no, p1, l2.line_no, p2, (int)l2.widths[0] + l2.X - viewport_x, l2.Y - viewport_y, (int)l2.widths[p2] + 1, l2.height);
- #endif
- }
- /// <summary>Select text around caret</summary>
- internal void ExpandSelection(CaretSelection mode, bool to_caret) {
- if (to_caret) {
- // We're expanding the selection to the caret position
- switch(mode) {
- case CaretSelection.Line: {
- // Invalidate the selection delta
- if (caret > selection_prev) {
- Invalidate(selection_prev.line, 0, caret.line, caret.line.text.Length);
- } else {
- Invalidate(selection_prev.line, selection_prev.line.text.Length, caret.line, 0);
- }
- if (caret.line.line_no <= selection_anchor.line.line_no) {
- selection_start.line = caret.line;
- selection_start.tag = caret.line.tags;
- selection_start.pos = 0;
- selection_end.line = selection_anchor.line;
- selection_end.tag = selection_anchor.tag;
- selection_end.pos = selection_anchor.pos;
- selection_end_anchor = true;
- } else {
- selection_start.line = selection_anchor.line;
- selection_start.pos = selection_anchor.height;
- selection_start.tag = selection_anchor.line.FindTag(selection_anchor.height);
- selection_end.line = caret.line;
- selection_end.tag = caret.line.tags;
- selection_end.pos = caret.line.text.Length;
- selection_end_anchor = false;
- }
- selection_prev.line = caret.line;
- selection_prev.tag = caret.tag;
- selection_prev.pos = caret.pos;
- break;
- }
- case CaretSelection.Word: {
- int start_pos;
- int end_pos;
- start_pos = FindWordSeparator(caret.line, caret.pos, false);
- end_pos = FindWordSeparator(caret.line, caret.pos, true);
-
- // Invalidate the selection delta
- if (caret > selection_prev) {
- Invalidate(selection_prev.line, selection_prev.pos, caret.line, end_pos);
- } else {
- Invalidate(selection_prev.line, selection_prev.pos, caret.line, start_pos);
- }
- if (caret < selection_anchor) {
- selection_start.line = caret.line;
- selection_start.tag = caret.line.FindTag(start_pos);
- selection_start.pos = start_pos;
- selection_end.line = selection_anchor.line;
- selection_end.tag = selection_anchor.tag;
- selection_end.pos = selection_anchor.pos;
- selection_prev.line = caret.line;
- selection_prev.tag = caret.tag;
- selection_prev.pos = start_pos;
- selection_end_anchor = true;
- } else {
- selection_start.line = selection_anchor.line;
- selection_start.pos = selection_anchor.height;
- selection_start.tag = selection_anchor.line.FindTag(selection_anchor.height);
- selection_end.line = caret.line;
- selection_end.tag = caret.line.FindTag(end_pos);
- selection_end.pos = end_pos;
- selection_prev.line = caret.line;
- selection_prev.tag = caret.tag;
- selection_prev.pos = end_pos;
- selection_end_anchor = false;
- }
- break;
- }
- case CaretSelection.Position: {
- SetSelectionToCaret(false);
- return;
- }
- }
- } else {
- // We're setting the selection 'around' the caret position
- switch(mode) {
- case CaretSelection.Line: {
- this.Invalidate(caret.line, 0, caret.line, caret.line.text.Length);
- selection_start.line = caret.line;
- selection_start.tag = caret.line.tags;
- selection_start.pos = 0;
- selection_end.line = caret.line;
- selection_end.pos = caret.line.text.Length;
- selection_end.tag = caret.line.FindTag(selection_end.pos);
- selection_anchor.line = selection_end.line;
- selection_anchor.tag = selection_end.tag;
- selection_anchor.pos = selection_end.pos;
- selection_anchor.height = 0;
- selection_prev.line = caret.line;
- selection_prev.tag = caret.tag;
- selection_prev.pos = caret.pos;
- this.selection_end_anchor = true;
- break;
- }
- case CaretSelection.Word: {
- int start_pos;
- int end_pos;
- start_pos = FindWordSeparator(caret.line, caret.pos, false);
- end_pos = FindWordSeparator(caret.line, caret.pos, true);
- this.Invalidate(selection_start.line, start_pos, caret.line, end_pos);
- selection_start.line = caret.line;
- selection_start.tag = caret.line.FindTag(start_pos);
- selection_start.pos = start_pos;
- selection_end.line = caret.line;
- selection_end.tag = caret.line.FindTag(end_pos);
- selection_end.pos = end_pos;
- selection_anchor.line = selection_end.line;
- selection_anchor.tag = selection_end.tag;
- selection_anchor.pos = selection_end.pos;
- selection_anchor.height = start_pos;
- selection_prev.line = caret.line;
- selection_prev.tag = caret.tag;
- selection_prev.pos = caret.pos;
- this.selection_end_anchor = true;
- break;
- }
- }
- }
- SetSelectionVisible (!(selection_start == selection_end));
- }
- internal void SetSelectionToCaret(bool start) {
- if (start) {
- // Invalidate old selection; selection is being reset to empty
- this.Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
- selection_start.line = caret.line;
- selection_start.tag = caret.tag;
- selection_start.pos = caret.pos;
- // start always also selects end
- selection_end.line = caret.line;
- selection_end.tag = caret.tag;
- selection_end.pos = caret.pos;
- selection_anchor.line = caret.line;
- selection_anchor.tag = caret.tag;
- selection_anchor.pos = caret.pos;
- } else {
- // Invalidate from previous end to caret (aka new end)
- if (selection_end_anchor) {
- if (selection_start != caret) {
- this.Invalidate(selection_start.line, selection_start.pos, caret.line, caret.pos);
- }
- } else {
- if (selection_end != caret) {
- this.Invalidate(selection_end.line, selection_end.pos, caret.line, caret.pos);
- }
- }
- if (caret < selection_anchor) {
- selection_start.line = caret.line;
- selection_start.tag = caret.tag;
- selection_start.pos = caret.pos;
- selection_end.line = selection_anchor.line;
- selection_end.tag = selection_anchor.tag;
- selection_end.pos = selection_anchor.pos;
- selection_end_anchor = true;
- } else {
- selection_start.line = selection_anchor.line;
- selection_start.tag = selection_anchor.tag;
- selection_start.pos = selection_anchor.pos;
- selection_end.line = caret.line;
- selection_end.tag = caret.tag;
- selection_end.pos = caret.pos;
- selection_end_anchor = false;
- }
- }
- SetSelectionVisible (!(selection_start == selection_end));
- }
- internal void SetSelection(Line start, int start_pos, Line end, int end_pos) {
- if (selection_visible) {
- Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
- }
- if ((end.line_no < start.line_no) || ((end == start) && (end_pos <= start_pos))) {
- selection_start.line = end;
- selection_start.tag = LineTag.FindTag(end, end_pos);
- selection_start.pos = end_pos;
- selection_end.line = start;
- selection_end.tag = LineTag.FindTag(start, start_pos);
- selection_end.pos = start_pos;
- selection_end_anchor = true;
- } else {
- selection_start.line = start;
- selection_start.tag = LineTag.FindTag(start, start_pos);
- selection_start.pos = start_pos;
- selection_end.line = end;
- selection_end.tag = LineTag.FindTag(end, end_pos);
- selection_end.pos = end_pos;
- selection_end_anchor = false;
- }
- selection_anchor.line = start;
- selection_anchor.tag = selection_start.tag;
- selection_anchor.pos = start_pos;
- if (((start == end) && (start_pos == end_pos)) || start == null || end == null) {
- SetSelectionVisible (false);
- } else {
- SetSelectionVisible (true);
- Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
- }
- }
- internal void SetSelectionStart(Line start, int start_pos, bool invalidate) {
- // Invalidate from the previous to the new start pos
- if (invalidate)
- Invalidate(selection_start.line, selection_start.pos, start, start_pos);
- selection_start.line = start;
- selection_start.pos = start_pos;
- selection_start.tag = LineTag.FindTag(start, start_pos);
- selection_anchor.line = start;
- selection_anchor.pos = start_pos;
- selection_anchor.tag = selection_start.tag;
- selection_end_anchor = false;
-
- if ((selection_end.line != selection_start.line) || (selection_end.pos != selection_start.pos)) {
- SetSelectionVisible (true);
- } else {
- SetSelectionVisible (false);
- }
- if (invalidate)
- Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
- }
- internal void SetSelectionStart(int character_index, bool invalidate) {
- Line line;
- LineTag tag;
- int pos;
- if (character_index < 0) {
- return;
- }
- CharIndexToLineTag(character_index, out line, out tag, out pos);
- SetSelectionStart(line, pos, invalidate);
- }
- internal void SetSelectionEnd(Line end, int end_pos, bool invalidate) {
- if (end == selection_end.line && end_pos == selection_start.pos) {
- selection_anchor.line = selection_start.line;
- selection_anchor.tag = selection_start.tag;
- selection_anchor.pos = selection_start.pos;
- selection_end.line = selection_start.line;
- selection_end.tag = selection_start.tag;
- selection_end.pos = selection_start.pos;
- selection_end_anchor = false;
- } else if ((end.line_no < selection_anchor.line.line_no) || ((end == selection_anchor.line) && (end_pos <= selection_anchor.pos))) {
- selection_start.line = end;
- selection_start.tag = LineTag.FindTag(end, end_pos);
- selection_start.pos = end_pos;
- selection_end.line = selection_anchor.line;
- selection_end.tag = selection_anchor.tag;
- selection_end.pos = selection_anchor.pos;
- selection_end_anchor = true;
- } else {
- selection_start.line = selection_anchor.line;
- selection_start.tag = selection_anchor.tag;
- selection_start.pos = selection_anchor.pos;
- selection_end.line = end;
- selection_end.tag = LineTag.FindTag(end, end_pos);
- selection_end.pos = end_pos;
- selection_end_anchor = false;
- }
- if ((selection_end.line != selection_start.line) || (selection_end.pos != selection_start.pos)) {
- SetSelectionVisible (true);
- if (invalidate)
- Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
- } else {
- SetSelectionVisible (false);
- // ?? Do I need to invalidate here, tests seem to work without it, but I don't think they should :-s
- }
- }
- internal void SetSelectionEnd(int character_index, bool invalidate) {
- Line line;
- LineTag tag;
- int pos;
- if (character_index < 0) {
- return;
- }
- CharIndexToLineTag(character_index, out line, out tag, out pos);
- SetSelectionEnd(line, pos, invalidate);
- }
- internal void SetSelection(Line start, int start_pos) {
- if (selection_visible) {
- Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
- }
- selection_start.line = start;
- selection_start.pos = start_pos;
- selection_start.tag = LineTag.FindTag(start, start_pos);
- selection_end.line = start;
- selection_end.tag = selection_start.tag;
- selection_end.pos = start_pos;
- selection_anchor.line = start;
- selection_anchor.tag = selection_start.tag;
- selection_anchor.pos = start_pos;
- selection_end_anchor = false;
- SetSelectionVisible (false);
- }
- internal void InvalidateSelectionArea() {
- Invalidate (selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
- }
- // Return the current selection, as string
- internal string GetSelection() {
- // We return String.Empty if there is no selection
- if ((selection_start.pos == selection_end.pos) && (selection_start.line == selection_end.line)) {
- return string.Empty;
- }
- if (selection_start.line == selection_end.line) {
- return selection_start.line.text.ToString (selection_start.pos, selection_end.pos - selection_start.pos);
- } else {
- StringBuilder sb;
- int i;
- int start;
- int end;
- sb = new StringBuilder();
- start = selection_start.line.line_no;
- end = selection_end.line.line_no;
- sb.Append(selection_start.line.text.ToString(selection_start.pos, selection_start.line.text.Length - selection_start.pos) + Environment.NewLine);
- if ((start + 1) < end) {
- for (i = start + 1; i < end; i++) {
- sb.Append(GetLine(i).text.ToString() + Environment.NewLine);
- }
- }
- sb.Append(selection_end.line.text.ToString(0, selection_end.pos));
- return sb.ToString();
- }
- }
- internal void ReplaceSelection(string s, bool select_new) {
- int i;
- int selection_pos_on_line = selection_start.pos;
- int selection_start_pos = LineTagToCharIndex (selection_start.line, selection_start.pos);
- SuspendRecalc ();
- // First, delete any selected text
- if ((selection_start.pos != selection_end.pos) || (selection_start.line != selection_end.line)) {
- if (selection_start.line == selection_end.line) {
- undo.RecordDeleteString (selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
- DeleteChars(selection_start.tag, selection_start.pos, selection_end.pos - selection_start.pos);
- // The tag might have been removed, we need to recalc it
- selection_start.tag = selection_start.line.FindTag(selection_start.pos);
- } else {
- int start;
- int end;
- start = selection_start.line.line_no;
- end = selection_end.line.line_no;
- undo.RecordDeleteString (selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
- InvalidateSelectionArea ();
- // Delete first line
- DeleteChars(selection_start.tag, selection_start.pos, selection_start.line.text.Length - selection_start.pos);
- selection_start.line.recalc = true;
- // Delete last line
- DeleteChars(selection_end.line.tags, 0, selection_end.pos);
- start++;
- if (start < end) {
- for (i = end - 1; i >= start; i--) {
- Delete(i);
- }
- }
- // BIG FAT WARNING - selection_end.line might be stale due
- // to the above Delete() call. DONT USE IT before hitting the end of this method!
- // Join start and end
- Combine(selection_start.line.line_no, start);
- }
- }
- Insert(selection_start.line, selection_start.pos, false, s);
- undo.RecordInsertString (selection_start.line, selection_start.pos, s);
- ResumeRecalc (false);
- if (!select_new) {
- CharIndexToLineTag(selection_start_pos + s.Length, out selection_start.line,
- out selection_start.tag, out selection_start.pos);
- selection_end.line = selection_start.line;
- selection_end.pos = selection_start.pos;
- selection_end.tag = selection_start.tag;
- selection_anchor.line = selection_start.line;
- selection_anchor.pos = selection_start.pos;
- selection_anchor.tag = selection_start.tag;
- SetSelectionVisible (false);
- } else {
- CharIndexToLineTag(selection_start_pos, out selection_start.line,
- out selection_start.tag, out selection_start.pos);
- CharIndexToLineTag(selection_start_pos + s.Length, out selection_end.line,
- out selection_end.tag, out selection_end.pos);
- selection_anchor.line = selection_start.line;
- selection_anchor.pos = selection_start.pos;
- selection_anchor.tag = selection_start.tag;
- SetSelectionVisible (true);
- }
- PositionCaret (selection_start.line, selection_start.pos);
- UpdateView (selection_start.line, selection_pos_on_line);
- }
- internal void CharIndexToLineTag(int index, out Line line_out, out LineTag tag_out, out int pos) {
- Line line;
- LineTag tag;
- int i;
- int chars;
- int start;
- chars = 0;
- for (i = 1; i <= lines; i++) {
- line = GetLine(i);
- start = chars;
- chars += line.text.Length;
- if (index <= chars) {
- // we found the line
- tag = line.tags;
- while (tag != null) {
- if (index < (start + tag.start + tag.Length - 1)) {
- line_out = line;
- tag_out = LineTag.GetFinalTag (tag);
- pos = index - start;
- return;
- }
- if (tag.next == null) {
- Line next_line;
- next_line = GetLine(line.line_no + 1);
- if (next_line != null) {
- line_out = next_line;
- tag_out = LineTag.GetFinalTag (next_line.tags);
- pos = 0;
- return;
- } else {
- line_out = line;
- tag_out = LineTag.GetFinalTag (tag);
- pos = line_out.text.Length;
- return;
- }
- }
- tag = tag.next;
- }
- }
- }
- line_out = GetLine(lines);
- tag = line_out.tags;
- while (tag.next != null) {
- tag = tag.next;
- }
- tag_out = tag;
- pos = line_out.text.Length;
- }
- internal int LineTagToCharIndex(Line line, int pos) {
- int i;
- int length;
- // Count first and last line
- length = 0;
- // Count the lines in the middle
- for (i = 1; i < line.line_no; i++) {
- length += GetLine(i).text.Length;
- }
- length += pos;
- return length;
- }
- internal int SelectionLength() {
- if ((selection_start.pos == selection_end.pos) && (selection_start.line == selection_end.line)) {
- return 0;
- }
- if (selection_start.line == selection_end.line) {
- return selection_end.pos - selection_start.pos;
- } else {
- int i;
- int start;
- int end;
- int length;
- // Count first and last line
- length = selection_start.line.text.Length - selection_start.pos + selection_end.pos + crlf_size;
- // Count the lines in the middle
- start = selection_start.line.line_no + 1;
- end = selection_end.line.line_no;
- if (start < end) {
- for (i = start; i < end; i++) {
- Line line = GetLine (i);
- length += line.text.Length + LineEndingLength (line.ending);
- }
- }
- return length;
- }
-
- }
- /// <summary>Give it a Line number and it returns the Line object at with that line number</summary>
- internal Line GetLine(int LineNo) {
- Line line = document;
- while (line != sentinel) {
- if (LineNo == line.line_no) {
- return line;
- } else if (LineNo < line.line_no) {
- line = line.left;
- } else {
- line = line.right;
- }
- }
- return null;
- }
- /// <summary>Retrieve the previous tag; walks line boundaries</summary>
- internal LineTag PreviousTag(LineTag tag) {
- Line l;
- if (tag.previous != null) {
- return tag.previous;
- }
- // Next line
- if (tag.line.line_no == 1) {
- return null;
- }
- l = GetLine(tag.line.line_no - 1);
- if (l != null) {
- LineTag t;
- t = l.tags;
- while (t.next != null) {
- t = t.next;
- }
- return t;
- }
- return null;
- }
- /// <summary>Retrieve the next tag; walks line boundaries</summary>
- internal LineTag NextTag(LineTag tag) {
- Line l;
- if (tag.next != null) {
- return tag.next;
- }
- // Next line
- l = GetLine(tag.line.line_no + 1);
- if (l != null) {
- return l.tags;
- }
- return null;
- }
- internal Line ParagraphStart(Line line) {
- while (line.ending == LineEnding.Wrap) {
- line = GetLine(line.line_no - 1);
- }
- return line;
- }
- internal Line ParagraphEnd(Line line) {
- Line l;
-
- while (line.ending == LineEnding.Wrap) {
- l = GetLine(line.line_no + 1);
- if ((l == null) || (l.ending != LineEnding.Wrap)) {
- break;
- }
- line = l;
- }
- return line;
- }
- /// <summary>Give it a pixel offset coordinate and it returns the Line covering that are (offset
- /// is either X or Y depending on if we are multiline
- /// </summary>
- internal Line GetLineByPixel (int offset, bool exact)
- {
- Line line = document;
- Line last = null;
- if (multiline) {
- while (line != sentinel) {
- last = line;
- if ((offset >= line.Y) && (offset < (line.Y+line.height))) {
- return line;
- } else if (offset < line.Y) {
- line = line.left;
- } else {
- line = line.right;
- }
- }
- } else {
- while (line != sentinel) {
- last = line;
- if ((offset >= line.X) && (offset < (line.X + line.Width)))
- return line;
- else if (offset < line.X)
- line = line.left;
- else
- line = line.right;
- }
- }
- if (exact) {
- return null;
- }
- return last;
- }
- // Give it x/y pixel coordinates and it returns the Tag at that position; optionally the char position is returned in index
- internal LineTag FindTag(int x, int y, out int index, bool exact) {
- Line line;
- LineTag tag;
- line = GetLineByPixel(y, exact);
- if (line == null) {
- index = 0;
- return null;
- }
- tag = line.tags;
- // Alignment adjustment
- x += line.X;
- while (true) {
- if (x >= tag.X && x < (tag.X+tag.Width)) {
- int end;
- end = tag.start + tag.Length - 1;
- for (int pos = tag.start; pos < end; pos++) {
- if (x < line.widths[pos]) {
- index = pos;
- return LineTag.GetFinalTag (tag);
- }
- }
- index=end;
- return LineTag.GetFinalTag (tag);
- }
- if (tag.next != null) {
- tag = tag.next;
- } else {
- if (exact) {
- index = 0;
- return null;
- }
- index = line.text.Length;
- return LineTag.GetFinalTag (tag);
- }
- }
- }
- // Give it x/y pixel coordinates and it returns the Tag at that position; optionally the char position is returned in index
- internal LineTag FindCursor(int x, int y, out int index) {
- Line line;
- LineTag tag;
- line = GetLineByPixel(multiline ? y : x, false);
- tag = line.tags;
- /// Special case going leftwards of the first tag
- if (x < tag.X) {
- index = 0;
- return LineTag.GetFinalTag (tag);
- }
- while (true) {
- if (x >= tag.X && x < (tag.X+tag.Width)) {
- int end;
- end = tag.TextEnd;
- for (int pos = tag.start - 1; pos < end; pos++) {
- // When clicking on a character, we position the cursor to whatever edge
- // of the character the click was closer
- if (x < (line.X + line.widths[pos] + ((line.widths[pos+1]-line.widths[pos])/2))) {
- index = pos;
- return LineTag.GetFinalTag (tag);
- }
- }
- index=end;
- return LineTag.GetFinalTag (tag);
- }
- if (tag.next != null) {
- tag = tag.next;
- } else {
- index = line.TextLengthWithoutEnding ();
- return LineTag.GetFinalTag (tag);
- }
- }
- }
- /// <summary>Format area of document in specified font and color</summary>
- /// <param name="start_pos">1-based start position on start_line</param>
- /// <param name="end_pos">1-based end position on end_line </param>
- internal void FormatText (Line start_line, int start_pos, Line end_line, int end_pos, Font font,
- SolidBrush color, Color back_color, FormatSpecified specified)
- {
- Line l;
- // First, format the first line
- if (start_line != end_line) {
- // First line
- LineTag.FormatText(start_line, start_pos, start_line.text.Length - start_pos + 1, font, color, back_color, specified);
- // Format last line
- LineTag.FormatText(end_line, 1, end_pos, font, color, back_color, specified);
- // Now all the lines inbetween
- for (int i = start_line.line_no + 1; i < end_line.line_no; i++) {
- l = GetLine(i);
- LineTag.FormatText(l, 1, l.text.Length, font, color, back_color, specified);
- }
- } else {
- // Special case, single line
- LineTag.FormatText(start_line, start_pos, end_pos - start_pos, font, color, back_color, specified);
- }
- }
- internal void RecalculateAlignments ()
- {
- Line line;
- int line_no;
- line_no = 1;
- while (line_no <= lines) {
- line = GetLine(line_no);
- if (line != null) {
- switch (line.alignment) {
- case HorizontalAlignment.Left:
- line.align_shift = 0;
- break;
- case HorizontalAlignment.Center:
- line.align_shift = (viewport_width - (int)line.widths[line.text.Length]) / 2;
- break;
- case HorizontalAlignment.Right:
- line.align_shift = viewport_width - (int)line.widths[line.text.Length] - right_margin;
- break;
- }
- }
- line_no++;
- }
- return;
- }
- /// <summary>Calculate formatting for the whole document</summary>
- internal bool RecalculateDocument(Graphics g) {
- return RecalculateDocument(g, 1, this.lines, false);
- }
- /// <summary>Calculate formatting starting at a certain line</summary>
- internal bool RecalculateDocument(Graphics g, int start) {
- return RecalculateDocument(g, start, this.lines, false);
- }
- /// <summary>Calculate formatting within two given line numbers</summary>
- internal bool RecalculateDocument(Graphics g, int start, int end) {
- return RecalculateDocument(g, start, end, false);
- }
- /// <summary>With optimize on, returns true if line heights changed</summary>
- internal bool RecalculateDocument(Graphics g, int start, int end, bool optimize) {
- Line line;
- int line_no;
- int offset;
- int new_width;
- bool changed;
- int shift;
- if (recalc_suspended > 0) {
- recalc_pending = true;
- recalc_start = Math.Min (recalc_start, start);
- recalc_end = Math.Max (recalc_end, end);
- recalc_optimize = optimize;
- return false;
- }
- // Fixup the positions, they can go kinda nuts
- start = Math.Max (start, 1);
- end = Math.Min (end, lines);
- offset = GetLine(start).offset;
- line_no = start;
- new_width = 0;
- shift = this.lines;
- if (!optimize) {
- changed = true; // We always return true if we run non-optimized
- } else {
- changed = false;
- }
- while (line_no <= (end + this.lines - shift)) {
- line = GetLine(line_no++);
- line.offset = offset;
- if (!calc_pass) {
- if (!optimize) {
- line.RecalculateLine(g, this);
- } else {
- if (line.recalc && line.RecalculateLine(g, this)) {
- changed = true;
- // If the height changed, all subsequent lines change
- end = this.lines;
- shift = this.lines;
- }
- }
- } else {
- if (!optimize) {
- line.RecalculatePasswordLine(g, this);
- } else {
- if (line.recalc && line.RecalculatePasswordLine(g, this)) {
- changed = true;
- // If the height changed, all subsequent lines change
- end = this.lines;
- shift = this.lines;
- }
- }
- }
- if (line.widths[line.text.Length] > new_width) {
- new_width = (int)line.widths[line.text.Length];
- }
- // Calculate alignment
- if (line.alignment != HorizontalAlignment.Left) {
- if (line.alignment == HorizontalAlignment.Center) {
- line.align_shift = (viewport_width - (int)line.widths[line.text.Length]) / 2;
- } else {
- line.align_shift = viewport_width - (int)line.widths[line.text.Length] - 1;
- }
- }
- if (multiline)
- offset += line.height;
- else
- offset += (int) line.widths [line.text.Length];
- if (line_no > lines) {
- break;
- }
- }
- if (document_x != new_width) {
- document_x = new_width;
- if (WidthChanged != null) {
- WidthChanged(this, null);
- }
- }
- RecalculateAlignments();
- line = GetLine(lines);
- if (document_y != line.Y + line.height) {
- document_y = line.Y + line.height;
- if (HeightChanged != null) {
- HeightChanged(this, null);
- }
- }
- UpdateCaret();
- return changed;
- }
- internal int Size() {
- return lines;
- }
- private void owner_HandleCreated(object sender, EventArgs e) {
- RecalculateDocument(owner.CreateGraphicsInternal());
- AlignCaret();
- }
- private void owner_VisibleChanged(object sender, EventArgs e) {
- if (owner.Visible) {
- RecalculateDocument(owner.CreateGraphicsInternal());
- }
- }
- internal static bool IsWordSeparator (char ch)
- {
- switch (ch) {
- case ' ':
- case '\t':
- case '(':
- case ')':
- case '\r':
- case '\n':
- return true;
- default:
- return false;
- }
- }
- internal int FindWordSeparator(Line line, int pos, bool forward) {
- int len;
- len = line.text.Length;
- if (forward) {
- for (int i = pos + 1; i < len; i++) {
- if (IsWordSeparator(line.Text[i])) {
- return i + 1;
- }
- }
- return len;
- } else {
- for (int i = pos - 1; i > 0; i--) {
- if (IsWordSeparator(line.Text[i - 1])) {
- return i;
- }
- }
- return 0;
- }
- }
- /* Search document for text */
- internal bool FindChars(char[] chars, Marker start, Marker end, out Marker result) {
- Line line;
- int line_no;
- int pos;
- int line_len;
- // Search for occurence of any char in the chars array
- result = new Marker();
- line = start.line;
- line_no = start.line.line_no;
- pos = start.pos;
- while (line_no <= end.line.line_no) {
- line_len = line.text.Length;
- while (pos < line_len) {
- for (int i = 0; i < chars.Length; i++) {
- if (line.text[pos] == chars[i]) {
- // Special case
- if ((line.line_no == end.line.line_no) && (pos >= end.pos)) {
- return false;
- }
- result.line = line;
- result.pos = pos;
- return true;
- }
- }
- pos++;
- }
- pos = 0;
- line_no++;
- line = GetLine(line_no);
- }
- return false;
- }
- // This version does not build one big string for searching, instead it handles
- // line-boundaries, which is faster and less memory intensive
- // FIXME - Depending on culture stuff we might have to create a big string and use culturespecific
- // search stuff and change it to accept and return positions instead of Markers (which would match
- // RichTextBox behaviour better but would be inconsistent with the rest of TextControl)
- internal bool Find(string search, Marker start, Marker end, out Marker result, RichTextBoxFinds options) {
- Marker last;
- string search_string;
- Line line;
- int line_no;
- int pos;
- int line_len;
- int current;
- bool word;
- bool word_option;
- bool ignore_case;
- bool reverse;
- char c;
- result = new Marker();
- word_option = ((options & RichTextBoxFinds.WholeWord) != 0);
- ignore_case = ((options & RichTextBoxFinds.MatchCase) == 0);
- reverse = ((options & RichTextBoxFinds.Reverse) != 0);
- line = start.line;
- line_no = start.line.line_no;
- pos = start.pos;
- current = 0;
- // Prep our search string, lowercasing it if we do case-independent matching
- if (ignore_case) {
- StringBuilder sb;
- sb = new StringBuilder(search);
- for (int i = 0; i < sb.Length; i++) {
- sb[i] = Char.ToLower(sb[i]);
- }
- search_string = sb.ToString();
- } else {
- search_string = search;
- }
- // We need to check if the character before our start position is a wordbreak
- if (word_option) {
- if (line_no == 1) {
- if ((pos == 0) || (IsWordSeparator(line.text[pos - 1]))) {
- word = true;
- } else {
- word = false;
- }
- } else {
- if (pos > 0) {
- if (IsWordSeparator(line.text[pos - 1])) {
- word = true;
- } else {
- word = false;
- }
- } else {
- // Need to check the end of the previous line
- Line prev_line;
- prev_line = GetLine(line_no - 1);
- if (prev_line.ending == LineEnding.Wrap) {
- if (IsWordSeparator(prev_line.text[prev_line.text.Length - 1])) {
- word = true;
- } else {
- word = false;
- }
- } else {
- word = true;
- }
- }
- }
- } else {
- word = false;
- }
- // To avoid duplication of this loop with reverse logic, we search
- // through the document, remembering the last match and when returning
- // report that last remembered match
- last = new Marker();
- last.height = -1; // Abused - we use it to track change
- while (line_no <= end.line.line_no) {
- if (line_no != end.line.line_no) {
- line_len = line.text.Length;
- } else {
- line_len = end.pos;
- }
- while (pos < line_len) {
- if (word_option && (current == search_string.Length)) {
- if (IsWordSeparator(line.text[pos])) {
- if (!reverse) {
- goto FindFound;
- } else {
- last = result;
- current = 0;
- }
- } else {
- current = 0;
- }
- }
- if (ignore_case) {
- c = Char.ToLower(line.text[pos]);
- } else {
- c = line.text[pos];
- }
- if (c == search_string[current]) {
-
- if (current == 0) {
- result.line = line;
- result.pos = pos;
- }
- if (!word_option || (word_option && (word || (current > 0)))) {
- current++;
- }
- if (!word_option && (current == search_string.Length)) {
- if (!reverse) {
- goto FindFound;
- } else {
- last = result;
- current = 0;
- }
- }
- } else {
- current = 0;
- }
- pos++;
- if (!word_option) {
- continue;
- }
- if (IsWordSeparator(c)) {
- word = true;
- } else {
- word = false;
- }
- }
- if (word_option) {
- // Mark that we just saw a word boundary
- if (line.ending != LineEnding.Wrap || line.line_no == lines - 1) {
- word = true;
- }
- if (current == search_string.Length) {
- if (word) {
- if (!reverse) {
- goto FindFound;
- } else {
- last = result;
- current = 0;
- }
- } else {
- current = 0;
- }
- }
- }
- pos = 0;
- line_no++;
- line = GetLine(line_no);
- }
- if (reverse) {
- if (last.height != -1) {
- result = last;
- return true;
- }
- }
- return false;
- FindFound:
- if (!reverse) {
- // if ((line.line_no == end.line.line_no) && (pos >= end.pos)) {
- // return false;
- // }
- return true;
- }
- result = last;
- return true;
- }
- /* Marker stuff */
- internal void GetMarker(out Marker mark, bool start) {
- mark = new Marker();
- if (start) {
- mark.line = GetLine(1);
- mark.tag = mark.line.tags;
- mark.pos = 0;
- } else {
- mark.line = GetLine(lines);
- mark.tag = mark.line.tags;
- while (mark.tag.next != null) {
- mark.tag = mark.tag.next;
- }
- mark.pos = mark.line.text.Length;
- }
- }
- #endregion // Internal Methods
- #region Events
- internal event EventHandler CaretMoved;
- internal event EventHandler WidthChanged;
- internal event EventHandler HeightChanged;
- internal event EventHandler LengthChanged;
- #endregion // Events
- #region Administrative
- public IEnumerator GetEnumerator() {
- // FIXME
- return null;
- }
- public override bool Equals(object obj) {
- if (obj == null) {
- return false;
- }
- if (!(obj is Document)) {
- return false;
- }
- if (obj == this) {
- return true;
- }
- if (ToString().Equals(((Document)obj).ToString())) {
- return true;
- }
- return false;
- }
- public override int GetHashCode() {
- return document_id;
- }
- public override string ToString() {
- return "document " + this.document_id;
- }
- #endregion // Administrative
- }
- internal class PictureTag : LineTag {
- internal RTF.Picture picture;
- internal PictureTag (Line line, int start, RTF.Picture picture) : base (line, start)
- {
- this.picture = picture;
- }
- public override bool IsTextTag {
- get { return false; }
- }
- internal override SizeF SizeOfPosition (Graphics dc, int pos)
- {
- return picture.Size;
- }
- internal override int MaxHeight ()
- {
- return (int) (picture.Height + 0.5F);
- }
- internal override void Draw (Graphics dc, Brush brush, float xoff, float y, int start, int end)
- {
- picture.DrawImage (dc, xoff + line.widths [start], y, false);
- }
- internal override void Draw (Graphics dc, Brush brush, float xoff, float y, int start, int end, string text)
- {
- picture.DrawImage (dc, xoff + + line.widths [start], y, false);
- }
- public override string Text ()
- {
- return "I";
- }
- }
- internal class UndoManager {
- internal enum ActionType {
- Typing,
- // This is basically just cut & paste
- InsertString,
- DeleteString,
- UserActionBegin,
- UserActionEnd
- }
- internal class Action {
- internal ActionType type;
- internal int line_no;
- internal int pos;
- internal object data;
- }
- #region Local Variables
- private Document document;
- private Stack undo_actions;
- private Stack redo_actions;
- //private int caret_line;
- //private int caret_pos;
- // When performing an action, we lock the queue, so that the action can't be undone
- private bool locked;
- #endregion // Local Variables
- #region Constructors
- internal UndoManager (Document document)
- {
- this.document = document;
- undo_actions = new Stack (50);
- redo_actions = new Stack (50);
- }
- #endregion // Constructors
- #region Properties
- internal bool CanUndo {
- get { return undo_actions.Count > 0; }
- }
- internal bool CanRedo {
- get { return redo_actions.Count > 0; }
- }
- internal string UndoActionName {
- get {
- foreach (Action action in undo_actions) {
- if (action.type == ActionType.UserActionBegin)
- return (string) action.data;
- if (action.type == ActionType.Typing)
- return Locale.GetText ("Typing");
- }
- return String.Empty;
- }
- }
- internal string RedoActionName {
- get {
- foreach (Action action in redo_actions) {
- if (action.type == ActionType.UserActionBegin)
- return (string) action.data;
- if (action.type == ActionType.Typing)
- return Locale.GetText ("Typing");
- }
- return String.Empty;
- }
- }
- #endregion // Properties
- #region Internal Methods
- internal void Clear ()
- {
- undo_actions.Clear();
- redo_actions.Clear();
- }
- internal void Undo ()
- {
- Action action;
- bool user_action_finished = false;
- if (undo_actions.Count == 0)
- return;
- // Nuke the redo queue
- redo_actions.Clear ();
- locked = true;
- do {
- Line start;
- action = (Action) undo_actions.Pop ();
- // Put onto redo stack
- redo_actions.Push(action);
- // Do the thing
- switch(action.type) {
- case ActionType.UserActionBegin:
- user_action_finished = true;
- break;
- case ActionType.UserActionEnd:
- // noop
- break;
- case ActionType.InsertString:
- start = document.GetLine (action.line_no);
- document.SuspendUpdate ();
- document.DeleteMultiline (start, action.pos, ((string) action.data).Length + 1);
- document.PositionCaret (start, action.pos);
- document.SetSelectionToCaret (true);
- document.ResumeUpdate (true);
- break;
- case ActionType.Typing:
- start = document.GetLine (action.line_no);
- document.SuspendUpdate ();
- document.DeleteMultiline (start, action.pos, ((StringBuilder) action.data).Length);
- document.PositionCaret (start, action.pos);
- document.SetSelectionToCaret (true);
- document.ResumeUpdate (true);
- // This is an open ended operation, so only a single typing operation can be undone at once
- user_action_finished = true;
- break;
- case ActionType.DeleteString:
- start = document.GetLine (action.line_no);
- document.SuspendUpdate ();
- Insert (start, action.pos, (Line) action.data, true);
- document.ResumeUpdate (true);
- break;
- }
- } while (!user_action_finished && undo_actions.Count > 0);
- locked = false;
- }
- internal void Redo ()
- {
- Action action;
- bool user_action_finished = false;
- if (redo_actions.Count == 0)
- return;
- // You can't undo anything after redoing
- undo_actions.Clear ();
- locked = true;
- do {
- Line start;
- int start_index;
- action = (Action) redo_actions.Pop ();
- switch (action.type) {
- case ActionType.UserActionBegin:
- // Noop
- break;
- case ActionType.UserActionEnd:
- user_action_finished = true;
- break;
- case ActionType.InsertString:
- start = document.GetLine (action.line_no);
- document.SuspendUpdate ();
- start_index = document.LineTagToCharIndex (start, action.pos);
- document.InsertString (start, action.pos, (string) action.data);
- document.CharIndexToLineTag (start_index + ((string) action.data).Length,
- out document.caret.line, out document.caret.tag,
- out document.caret.pos);
- document.UpdateCaret ();
- document.SetSelectionToCaret (true);
- document.ResumeUpdate (true);
- break;
- case ActionType.Typing:
- start = document.GetLine (action.line_no);
- document.SuspendUpdate ();
- start_index = document.LineTagToCharIndex (start, action.pos);
- document.InsertString (start, action.pos, ((StringBuilder) action.data).ToString ());
- document.CharIndexToLineTag (start_index + ((StringBuilder) action.data).Length,
- out document.caret.line, out document.caret.tag,
- out document.caret.pos);
- document.UpdateCaret ();
- document.SetSelectionToCaret (true);
- document.ResumeUpdate (true);
- // This is an open ended operation, so only a single typing operation can be undone at once
- user_action_finished = true;
- break;
- case ActionType.DeleteString:
- start = document.GetLine (action.line_no);
- document.SuspendUpdate ();
- document.DeleteMultiline (start, action.pos, ((Line) action.data).text.Length);
- document.PositionCaret (start, action.pos);
- document.SetSelectionToCaret (true);
- document.ResumeUpdate (true);
- break;
- }
- } while (!user_action_finished && redo_actions.Count > 0);
- locked = false;
- }
- #endregion // Internal Methods
- #region Private Methods
- public void BeginUserAction (string name)
- {
- if (locked)
- return;
- Action ua = new Action ();
- ua.type = ActionType.UserActionBegin;
- ua.data = name;
- undo_actions.Push (ua);
- }
- public void EndUserAction ()
- {
- if (locked)
- return;
- Action ua = new Action ();
- ua.type = ActionType.UserActionEnd;
- undo_actions.Push (ua);
- }
- // start_pos, end_pos = 1 based
- public void RecordDeleteString (Line start_line, int start_pos, Line end_line, int end_pos)
- {
- if (locked)
- return;
- Action a = new Action ();
- // We cant simply store the string, because then formatting would be lost
- a.type = ActionType.DeleteString;
- a.line_no = start_line.line_no;
- a.pos = start_pos;
- a.data = Duplicate (start_line, start_pos, end_line, end_pos);
- undo_actions.Push(a);
- }
- public void RecordInsertString (Line line, int pos, string str)
- {
- if (locked || str.Length == 0)
- return;
- Action a = new Action ();
- a.type = ActionType.InsertString;
- a.data = str;
- a.line_no = line.line_no;
- a.pos = pos;
- undo_actions.Push (a);
- }
- public void RecordTyping (Line line, int pos, char ch)
- {
- if (locked)
- return;
- Action a = null;
- if (undo_actions.Count > 0)
- a = (Action) undo_actions.Peek ();
- if (a == null || a.type != ActionType.Typing) {
- a = new Action ();
- a.type = ActionType.Typing;
- a.data = new StringBuilder ();
- a.line_no = line.line_no;
- a.pos = pos;
- undo_actions.Push (a);
- }
- StringBuilder data = (StringBuilder) a.data;
- data.Append (ch);
- }
- // start_pos = 1-based
- // end_pos = 1-based
- public Line Duplicate(Line start_line, int start_pos, Line end_line, int end_pos)
- {
- Line ret;
- Line line;
- Line current;
- LineTag tag;
- LineTag current_tag;
- int start;
- int end;
- int tag_start;
- line = new Line (start_line.document, start_line.ending);
- ret = line;
- for (int i = start_line.line_no; i <= end_line.line_no; i++) {
- current = document.GetLine(i);
- if (start_line.line_no == i) {
- start = start_pos;
- } else {
- start = 0;
- }
- if (end_line.line_no == i) {
- end = end_pos;
- } else {
- end = current.text.Length;
- }
- if (end_pos == 0)
- continue;
- // Text for the tag
- line.text = new StringBuilder (current.text.ToString (start, end - start));
- // Copy tags from start to start+length onto new line
- current_tag = current.FindTag (start);
- while ((current_tag != null) && (current_tag.start <= end)) {
- if ((current_tag.start <= start) && (start < (current_tag.start + current_tag.Length))) {
- // start tag is within this tag
- tag_start = start;
- } else {
- tag_start = current_tag.start;
- }
- tag = new LineTag(line, tag_start - start + 1);
- tag.CopyFormattingFrom (current_tag);
- current_tag = current_tag.next;
- // Add the new tag to the line
- if (line.tags == null) {
- line.tags = tag;
- } else {
- LineTag tail;
- tail = line.tags;
- while (tail.next != null) {
- tail = tail.next;
- }
- tail.next = tag;
- tag.previous = tail;
- }
- }
- if ((i + 1) <= end_line.line_no) {
- line.ending = current.ending;
- // Chain them (we use right/left as next/previous)
- line.right = new Line (start_line.document, start_line.ending);
- line.right.left = line;
- line = line.right;
- }
- }
- return ret;
- }
- // Insert multi-line text at the given position; use formatting at insertion point for inserted text
- internal void Insert(Line line, int pos, Line insert, bool select)
- {
- Line current;
- LineTag tag;
- int offset;
- int lines;
- Line first;
- // Handle special case first
- if (insert.right == null) {
- // Single line insert
- document.Split(line, pos);
- if (insert.tags == null) {
- return; // Blank line
- }
- //Insert our tags at the end
- tag = line.tags;
- while (tag.next != null) {
- tag = tag.next;
- }
- offset = tag.start + tag.Length - 1;
- tag.next = insert.tags;
- line.text.Insert(offset, insert.text.ToString());
- // Adjust start locations
- tag = tag.next;
- while (tag != null) {
- tag.start += offset;
- tag.line = line;
- tag = tag.next;
- }
- // Put it back together
- document.Combine(line.line_no, line.line_no + 1);
- if (select) {
- document.SetSelectionStart (line, pos, false);
- document.SetSelectionEnd (line, pos + insert.text.Length, false);
- }
- document.UpdateView(line, pos);
- return;
- }
- first = line;
- lines = 1;
- current = insert;
- while (current != null) {
- if (current == insert) {
- // Inserting the first line we split the line (and make space)
- document.Split(line.line_no, pos);
- //Insert our tags at the end of the line
- tag = line.tags;
-
- if (tag != null && tag.Length != 0) {
- while (tag.next != null) {
- tag = tag.next;
- }
- offset = tag.start + tag.Length - 1;
- tag.next = current.tags;
- tag.next.previous = tag;
- tag = tag.next;
- } else {
- offset = 0;
- line.tags = current.tags;
- line.tags.previous = null;
- tag = line.tags;
- }
- line.ending = current.ending;
- } else {
- document.Split(line.line_no, 0);
- offset = 0;
- line.tags = current.tags;
- line.tags.previous = null;
- line.ending = current.ending;
- tag = line.tags;
- }
- // Adjust start locations and line pointers
- while (tag != null) {
- tag.start += offset - 1;
- tag.line = line;
- tag = tag.next;
- }
- line.text.Insert(offset, current.text.ToString());
- line.Grow(line.text.Length);
- line.recalc = true;
- line = document.GetLine(line.line_no + 1);
- // FIXME? Test undo of line-boundaries
- if ((current.right == null) && (current.tags.Length != 0)) {
- document.Combine(line.line_no - 1, line.line_no);
- }
- current = current.right;
- lines++;
- }
- // Recalculate our document
- document.UpdateView(first, lines, pos);
- return;
- }
- #endregion // Private Methods
- }
- }
|