| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644 |
- // 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 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 to support wrapping (ie have a 'newline' tag), for images, etc.
- // - Wrap and recalculate lines
- // - Implement CaretPgUp/PgDown
- // - Finish selection calculations (invalidate only changed, more ways to select)
- // - Implement C&P
- #undef Debug
- using System;
- using System.Collections;
- using System.Drawing;
- using System.Drawing.Text;
- using System.Text;
- namespace System.Windows.Forms {
- public enum LineColor {
- Red = 0,
- Black = 1
- }
- public 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
- 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)
- }
- // Being cloneable should allow for nice line and document copies...
- public class Line : ICloneable, IComparable {
- #region Local Variables
- // Stuff that matters for our line
- internal StringBuilder text; // Characters for the line
- internal float[] widths; // Width of each character; always one larger than text.Length
- internal int space; // Number of elements in text and widths
- internal int line_no; // Line number
- internal LineTag tags; // Tags describing the text
- internal int Y; // Baseline
- internal int height; // Height of the line (height of tallest tag)
- internal int ascent; // Ascent of the line (ascent of the tallest tag)
- internal HorizontalAlignment alignment; // Alignment of the line
- internal int align_shift; // Pixel shift caused by the alignment
- // Stuff that's important for the tree
- internal Line parent; // Our parent line
- public Line left; // Line with smaller line number
- public Line right; // Line with higher line number
- internal LineColor color; // We're doing a black/red tree. this is the node color
- internal int DEFAULT_TEXT_LEN; //
- internal static StringFormat string_format; // For calculating widths/heights
- internal bool recalc; // Line changed
- #endregion // Local Variables
- #region Constructors
- public Line() {
- color = LineColor.Red;
- left = null;
- right = null;
- parent = null;
- text = null;
- recalc = true;
- alignment = HorizontalAlignment.Left;
- if (string_format == null) {
- string_format = new StringFormat(StringFormat.GenericTypographic);
- string_format.Trimming = StringTrimming.None;
- string_format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
- }
- }
- public Line(int LineNo, string Text, Font font, Brush color) : this() {
- space = Text.Length > DEFAULT_TEXT_LEN ? Text.Length+1 : DEFAULT_TEXT_LEN;
- text = new StringBuilder(Text, space);
- line_no = LineNo;
- widths = new float[space + 1];
- tags = new LineTag(this, 1, text.Length);
- tags.font = font;
- tags.color = color;
- }
- public Line(int LineNo, string Text, HorizontalAlignment align, Font font, Brush color) : this() {
- space = Text.Length > DEFAULT_TEXT_LEN ? Text.Length+1 : DEFAULT_TEXT_LEN;
- text = new StringBuilder(Text, space);
- line_no = LineNo;
- alignment = align;
- widths = new float[space + 1];
- tags = new LineTag(this, 1, text.Length);
- tags.font = font;
- tags.color = color;
- }
- public Line(int LineNo, string Text, LineTag tag) : this() {
- space = Text.Length > DEFAULT_TEXT_LEN ? Text.Length+1 : DEFAULT_TEXT_LEN;
- text = new StringBuilder(Text, space);
- line_no = LineNo;
- widths = new float[space + 1];
- tags = tag;
- }
- #endregion // Constructors
- #region Public Properties
- public int Height {
- get {
- return height;
- }
- set {
- height = value;
- }
- }
- public int LineNo {
- get {
- return line_no;
- }
- set {
- line_no = value;
- }
- }
- public string Text {
- get {
- return text.ToString();
- }
- set {
- text = new StringBuilder(value, value.Length > DEFAULT_TEXT_LEN ? value.Length : DEFAULT_TEXT_LEN);
- }
- }
- public HorizontalAlignment Alignment {
- get {
- return alignment;
- }
- set {
- if (alignment != value) {
- alignment = value;
- recalc = true;
- }
- }
- }
- #if no
- public StringBuilder Text {
- get {
- return text;
- }
- set {
- text = value;
- }
- }
- #endif
- #endregion // Public Properties
- #region Public Methods
- // Make sure we always have enoughs space in text and widths
- public void Grow(int minimum) {
- int length;
- float[] new_widths;
- length = text.Length;
- if ((length + minimum) > space) {
- // We need to grow; double the size
- if ((length + minimum) > (space * 2)) {
- new_widths = new float[length + minimum * 2 + 1];
- space = length + minimum * 2;
- } else {
- new_widths = new float[space * 2 + 1];
- space *= 2;
- }
- widths.CopyTo(new_widths, 0);
- widths = new_widths;
- }
- }
- public void Streamline() {
- LineTag current;
- LineTag next;
- current = this.tags;
- next = current.next;
- // Catch what the loop below wont; eliminate 0 length
- // tags, but only if there are other tags after us
- while ((current.length == 0) && (next != null)) {
- tags = next;
- current = next;
- next = current.next;
- }
-
- if (next == null) {
- return;
- }
- while (next != null) {
- // Take out 0 length tags
- if (next.length == 0) {
- current.next = next.next;
- if (current.next != null) {
- current.next.previous = current;
- }
- next = current.next;
- continue;
- }
- if (current.Combine(next)) {
- next = current.next;
- continue;
- }
- current = current.next;
- next = current.next;
- }
- }
- // Find the tag on a line based on the character position
- public LineTag FindTag(int pos) {
- LineTag tag;
- if (pos == 0) {
- return tags;
- }
- tag = this.tags;
- if (pos > text.Length) {
- pos = text.Length;
- }
- while (tag != null) {
- if ((tag.start <= pos) && (pos < (tag.start + tag.length))) {
- return tag;
- }
- tag = tag.next;
- }
- return null;
- }
- //
- // Go through all tags on a line and recalculate all size-related values
- // returns true if lineheight changed
- //
- public bool RecalculateLine(Graphics g) {
- LineTag tag;
- int pos;
- int len;
- SizeF size;
- float w;
- int prev_height;
- pos = 0;
- len = this.text.Length;
- tag = this.tags;
- prev_height = this.height; // For drawing optimization calculations
- this.height = 0; // Reset line height
- this.ascent = 0; // Reset the ascent for the line
- tag.shift = 0;
- tag.width = 0;
- widths[0] = 0;
- this.recalc = false;
- while (pos < len) {
- size = g.MeasureString(this.text.ToString(pos, 1), tag.font, 10000, string_format);
- w = size.Width;
- tag.width += w;
- pos++;
- widths[pos] = widths[pos-1] + w;
- if (pos == (tag.start-1 + tag.length)) {
- // We just found the end of our current tag
- tag.height = (int)tag.font.Height;
- // Check if we're the tallest on the line (so far)
- if (tag.height > this.height) {
- this.height = tag.height; // Yep; make sure the line knows
- }
- if (tag.ascent == 0) {
- int descent;
- XplatUI.GetFontMetrics(g, tag.font, out tag.ascent, out descent);
- }
- if (tag.ascent > this.ascent) {
- LineTag t;
- // We have a tag that has a taller ascent than the line;
- t = tags;
- while (t != tag) {
- t.shift = tag.ascent - t.ascent;
- t = t.next;
- }
- // Save on our line
- this.ascent = tag.ascent;
- } else {
- tag.shift = this.ascent - tag.ascent;
- }
- // Update our horizontal starting pixel position
- if (tag.previous == null) {
- tag.X = 0;
- } else {
- tag.X = tag.previous.X + (int)tag.previous.width;
- }
- tag = tag.next;
- if (tag != null) {
- tag.width = 0;
- tag.shift = 0;
- }
- }
- }
- if (this.height == 0) {
- this.height = tags.font.Height;
- tag.height = this.height;
- }
- if (prev_height != this.height) {
- return true;
- }
- return false;
- }
- #endregion // Public Methods
- #region Administrative
- public int CompareTo(object obj) {
- if (obj == null) {
- return 1;
- }
- if (! (obj is Line)) {
- throw new ArgumentException("Object is not of type Line", "obj");
- }
- if (line_no < ((Line)obj).line_no) {
- return -1;
- } else if (line_no > ((Line)obj).line_no) {
- return 1;
- } else {
- return 0;
- }
- }
- public object Clone() {
- Line clone;
- clone = new Line();
- clone.text = text;
- if (left != null) {
- clone.left = (Line)left.Clone();
- }
- if (left != null) {
- clone.left = (Line)left.Clone();
- }
- return clone;
- }
- public object CloneLine() {
- Line clone;
- clone = new Line();
- clone.text = text;
- return clone;
- }
- public override bool Equals(object obj) {
- if (obj == null) {
- return false;
- }
- if (!(obj is Line)) {
- return false;
- }
- if (obj == this) {
- return true;
- }
- if (line_no == ((Line)obj).line_no) {
- return true;
- }
- return false;
- }
- public override string ToString() {
- return "Line " + line_no;
- }
- #endregion // Administrative
- }
- public class Document : ICloneable, IEnumerable {
- #region Structures
- internal struct Marker {
- internal Line line;
- internal LineTag tag;
- internal int pos;
- internal int height;
- }
- #endregion Structures
- #region Local Variables
- private Line document;
- private int lines;
- private static Line sentinel;
- private Line last_found;
- private int document_id;
- private Random random = new Random();
- internal bool multiline;
- internal bool wrap;
- internal Marker caret;
- internal Marker selection_start;
- internal Marker selection_end;
- internal bool selection_visible;
- internal int viewport_x;
- internal int viewport_y; // The visible area of the document
- internal int document_x; // Width of the document
- internal int document_y; // Height of the document
- internal Control owner; // Who's owning us?
- #endregion // Local Variables
- #region Constructors
- public Document(Control owner) {
- lines = 0;
- this.owner = owner;
- multiline = true;
- // Tree related stuff
- sentinel = new Line();
- sentinel.color = LineColor.Black;
- document = sentinel;
- last_found = sentinel;
- // We always have a blank line
- Add(1, "", owner.Font, new SolidBrush(owner.ForeColor));
- this.RecalculateDocument(owner.CreateGraphics());
- PositionCaret(0, 0);
- lines=1;
- selection_visible = false;
- selection_start.line = this.document;
- selection_start.pos = 0;
- selection_end.line = this.document;
- selection_end.pos = 0;
- // Default selection is empty
- document_id = random.Next();
- }
- #endregion
- #region Public Properties
- public Line Root {
- get {
- return document;
- }
- set {
- document = value;
- }
- }
- public int Lines {
- get {
- return lines;
- }
- }
- public Line CaretLine {
- get {
- return caret.line;
- }
- }
- public int CaretPosition {
- get {
- return caret.pos;
- }
- }
- public LineTag CaretTag {
- get {
- return caret.tag;
- }
- }
- public int ViewPortX {
- get {
- return viewport_x;
- }
- set {
- viewport_x = value;
- }
- }
- public int ViewPortY {
- get {
- return viewport_y;
- }
- set {
- viewport_y = value;
- }
- }
- public int Width {
- get {
- return this.document_x;
- }
- }
- public int Height {
- get {
- return this.document_y;
- }
- }
- #endregion // Public Properties
- #region Private Methods
- // For debugging
- internal void DumpTree(Line line, bool with_tags) {
- Console.Write("Line {0}, Y: {1} Text {2}", line.line_no, line.Y, 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;
- tag = line.tags;
- count = 1;
- Console.Write(" Tags: ");
- while (tag != null) {
- Console.Write("{0} <{1}>-<{2}> ", count++, tag.start, 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(", ");
- }
- }
- Console.WriteLine("");
- }
- if (line.left != null) {
- if (line.left != sentinel) {
- 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) {
- DumpTree(line.right, with_tags);
- }
- } else {
- if (line != sentinel) {
- throw new Exception("Right should not be NULL");
- }
- }
- }
- 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;
- }
- }
- public void UpdateView(Line line, int pos) {
- int prev_width;
- // This is an optimization; we need to invalidate
- prev_width = (int)line.widths[line.text.Length];
- if (RecalculateDocument(owner.CreateGraphics(), 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, owner.Width, owner.Height - line.Y - viewport_y));
- } else {
- // The tag was above the visible area, draw everything
- owner.Invalidate();
- }
- } else {
- owner.Invalidate(new Rectangle((int)line.widths[pos] - viewport_x - 1, line.Y - viewport_y, (int)owner.Width, line.height));
- }
- }
- // Update display from line, down line_count lines; pos is unused, but required for the signature
- public void UpdateView(Line line, int line_count, int pos) {
- int prev_width;
- // This is an optimization; we need to invalidate
- prev_width = (int)line.widths[line.text.Length];
- if (RecalculateDocument(owner.CreateGraphics(), line.line_no, line.line_no + line_count - 1, 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, owner.Width, owner.Height - line.Y - viewport_y));
- } else {
- // The tag was above the visible area, draw everything
- owner.Invalidate();
- }
- } else {
- Line end_line;
- end_line = GetLine(line.line_no + line_count -1);
- if (end_line == null) {
- end_line = line;
- }
- owner.Invalidate(new Rectangle(0 - viewport_x, line.Y - viewport_y, (int)line.widths[line.text.Length], end_line.Y + end_line.height));
- }
- }
- #endregion // Private Methods
- #region Public Methods
- // Clear the document and reset state
- public void Empty() {
- document = sentinel;
- last_found = sentinel;
- lines = 0;
- // We always have a blank line
- Add(1, "", owner.Font, new SolidBrush(owner.ForeColor));
- this.RecalculateDocument(owner.CreateGraphics());
- PositionCaret(0, 0);
- selection_visible = false;
- selection_start.line = this.document;
- selection_start.pos = 0;
- selection_end.line = this.document;
- selection_end.pos = 0;
- viewport_x = 0;
- viewport_y = 0;
- document_x = 0;
- document_y = 0;
- }
- public void PositionCaret(Line line, int pos) {
- caret.tag = line.FindTag(pos);
- caret.line = line;
- caret.pos = pos;
- caret.height = caret.tag.height;
- XplatUI.DestroyCaret(owner.Handle);
- XplatUI.CreateCaret(owner.Handle, 2, caret.height);
- XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.align_shift - viewport_x, caret.line.Y + caret.tag.shift - viewport_y);
- }
- public void PositionCaret(int x, int y) {
- caret.tag = FindCursor(x + viewport_x, y + viewport_y, out caret.pos);
- caret.line = caret.tag.line;
- caret.height = caret.tag.height;
- XplatUI.DestroyCaret(owner.Handle);
- XplatUI.CreateCaret(owner.Handle, 2, caret.height);
- XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.align_shift - viewport_x, caret.line.Y + caret.tag.shift - viewport_y);
- }
- public void CaretHasFocus() {
- if (caret.tag != null) {
- XplatUI.CreateCaret(owner.Handle, 2, caret.height);
- XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.align_shift - viewport_x, caret.line.Y + caret.tag.shift - viewport_y);
- XplatUI.CaretVisible(owner.Handle, true);
- }
- }
- public void CaretLostFocus() {
- XplatUI.DestroyCaret(owner.Handle);
- }
- public void AlignCaret() {
- caret.tag = LineTag.FindTag(caret.line, caret.pos);
- caret.height = caret.tag.height;
- XplatUI.CreateCaret(owner.Handle, 2, caret.height);
- XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.align_shift - viewport_x, caret.line.Y + caret.tag.shift - viewport_y);
- XplatUI.CaretVisible(owner.Handle, true);
- }
- public void UpdateCaret() {
- if (caret.tag.height != caret.height) {
- caret.height = caret.tag.height;
- XplatUI.CreateCaret(owner.Handle, 2, caret.height);
- }
- XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.align_shift - viewport_x, caret.line.Y + caret.tag.shift - viewport_y);
- XplatUI.CaretVisible(owner.Handle, true);
- }
- public void DisplayCaret() {
- XplatUI.CaretVisible(owner.Handle, true);
- }
- public void HideCaret() {
- XplatUI.CaretVisible(owner.Handle, false);
- }
- public void MoveCaret(CaretDirection direction) {
- switch(direction) {
- case CaretDirection.CharForward: {
- caret.pos++;
- if (caret.pos > caret.line.text.Length) {
- if (multiline) {
- // 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.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) {
- 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.WordForward: {
- int len;
- len = caret.line.text.Length;
- if (caret.pos < len) {
- while ((caret.pos < len) && (caret.line.text.ToString(caret.pos, 1) != " ")) {
- caret.pos++;
- }
- if (caret.pos < len) {
- // Skip any whitespace
- while ((caret.pos < len) && (caret.line.text.ToString(caret.pos, 1) == " ")) {
- 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) {
- int len;
- len = caret.line.text.Length;
- caret.pos--;
- while ((caret.pos > 0) && (caret.line.text.ToString(caret.pos, 1) == " ")) {
- caret.pos--;
- }
- while ((caret.pos > 0) && (caret.line.text.ToString(caret.pos, 1) != " ")) {
- 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);
- XplatUI.CaretVisible(owner.Handle, true);
- }
- 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);
- XplatUI.CaretVisible(owner.Handle, true);
- }
- 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.text.Length) {
- caret.pos = caret.line.text.Length;
- caret.tag = LineTag.FindTag(caret.line, caret.pos);
- UpdateCaret();
- }
- return;
- }
- case CaretDirection.PgUp: {
- return;
- }
- case CaretDirection.PgDn: {
- 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 = 0;
- caret.tag = caret.line.tags;
- UpdateCaret();
- return;
- }
- }
- }
- // Draw the document
- public 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
- string s; // String representing the current line
- int line_no; //
- // First, figure out from what line to what line we need to draw
- start = GetLineByPixel(clip.Top - viewport_y, false).line_no;
- end = GetLineByPixel(clip.Bottom - viewport_y, false).line_no;
- // Now draw our elements; try to only draw those that are visible
- line_no = start;
- #if Debug
- DateTime n = DateTime.Now;
- Console.WriteLine("Started drawing: {0}s {1}ms", n.Second, n.Millisecond);
- #endif
- while (line_no <= end) {
- line = GetLine(line_no);
- tag = line.tags;
- s = line.text.ToString();
- while (tag != null) {
- if (((tag.X + tag.width) > (clip.Left - viewport_x)) || (tag.X < (clip.Right - viewport_x))) {
- // Check for selection
- if ((!selection_visible) || (line_no < selection_start.line.line_no) || (line_no > selection_end.line.line_no)) {
- // regular drawing
- g.DrawString(s.Substring(tag.start-1, tag.length), tag.font, tag.color, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
- } else {
- // we might have to draw our selection
- if ((line_no != selection_start.line.line_no) && (line_no != selection_end.line.line_no)) {
- g.FillRectangle(tag.color, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, line.widths[tag.start + tag.length - 1], tag.height);
- g.DrawString(s.Substring(tag.start-1, tag.length), tag.font, ThemeEngine.Current.ResPool.GetSolidBrush(owner.BackColor), tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
- } else {
- int middle;
- bool highlight;
- bool partial;
- highlight = false;
- partial = false;
- // Check the partial drawings first
- if ((selection_start.tag == tag) && (selection_end.tag == tag)) {
- partial = true;
- // First, the regular part
- g.DrawString(s.Substring(tag.start - 1, selection_start.pos - tag.start + 1), tag.font, tag.color, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
- // Now the highlight
- g.FillRectangle(tag.color, line.widths[selection_start.pos] + line.align_shift, line.Y + tag.shift - viewport_y, line.widths[selection_end.pos] - line.widths[selection_start.pos], tag.height);
- g.DrawString(s.Substring(selection_start.pos, selection_end.pos - selection_start.pos), tag.font, ThemeEngine.Current.ResPool.GetSolidBrush(owner.BackColor), line.widths[selection_start.pos] + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
- // And back to the regular
- g.DrawString(s.Substring(selection_end.pos, tag.length - selection_end.pos), tag.font, tag.color, line.widths[selection_end.pos] + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
- } else if (selection_start.tag == tag) {
- partial = true;
- // The highlighted part
- g.FillRectangle(tag.color, line.widths[selection_start.pos] + line.align_shift, line.Y + tag.shift - viewport_y, line.widths[tag.start + tag.length - 1], tag.height);
- g.DrawString(s.Substring(selection_start.pos, tag.length - selection_start.pos), tag.font, ThemeEngine.Current.ResPool.GetSolidBrush(owner.BackColor), line.widths[selection_start.pos] + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
- // The regular part
- g.DrawString(s.Substring(tag.start - 1, selection_start.pos - tag.start + 1), tag.font, tag.color, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
- } else if (selection_end.tag == tag) {
- partial = true;
- // The highlighted part
- g.FillRectangle(tag.color, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, line.widths[selection_end.pos], tag.height);
- g.DrawString(s.Substring(tag.start - 1, selection_end.pos - tag.start + 1), tag.font, ThemeEngine.Current.ResPool.GetSolidBrush(owner.BackColor), tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
- // The regular part
- g.DrawString(s.Substring(selection_end.pos, tag.length - selection_end.pos), tag.font, tag.color, line.widths[selection_end.pos] + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
- } else {
- // no partially selected tags here, simple checks...
- if (selection_start.line == line) {
- if ((tag.start + tag.length - 1) > selection_start.pos) {
- highlight = true;
- }
- }
- if (selection_end.line == line) {
- if ((tag.start + tag.length - 1) < selection_start.pos) {
- highlight = true;
- }
- }
- }
- if (!partial) {
- if (highlight) {
- g.FillRectangle(tag.color, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, line.widths[tag.start + tag.length - 1], tag.height);
- g.DrawString(s.Substring(tag.start-1, tag.length), tag.font, ThemeEngine.Current.ResPool.GetSolidBrush(owner.BackColor), tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
- } else {
- g.DrawString(s.Substring(tag.start-1, tag.length), tag.font, tag.color, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
- }
- }
- }
- }
- }
- tag = tag.next;
- }
- line_no++;
- }
- #if Debug
- n = DateTime.Now;
- Console.WriteLine("Finished drawing: {0}s {1}ms", n.Second, n.Millisecond);
- #endif
- }
- // Inserts a character at the given position
- public void InsertString(Line line, int pos, string s) {
- InsertString(line.FindTag(pos), pos, s);
- }
- // Inserts a string at the given position
- public void InsertString(LineTag tag, int pos, string s) {
- Line line;
- int len;
- len = s.Length;
- line = tag.line;
- line.text.Insert(pos, s);
- tag.length += len;
- 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
- public void InsertStringAtCaret(string s, bool move_caret) {
- LineTag tag;
- int len;
- len = s.Length;
- caret.line.text.Insert(caret.pos, s);
- caret.tag.length += len;
-
- if (caret.tag.next != null) {
- tag = caret.tag.next;
- while (tag != null) {
- tag.start += len;
- tag = tag.next;
- }
- }
- caret.line.Grow(len);
- caret.line.recalc = true;
- UpdateView(caret.line, caret.pos);
- if (move_caret) {
- caret.pos += len;
- UpdateCaret();
- }
- }
- // Inserts a character at the given position
- public void InsertChar(Line line, int pos, char ch) {
- InsertChar(line.FindTag(pos), pos, ch);
- }
- // Inserts a character at the given position
- public void InsertChar(LineTag tag, int pos, char ch) {
- Line line;
- line = tag.line;
- line.text.Insert(pos, ch);
- tag.length++;
- tag = tag.next;
- while (tag != null) {
- tag.start++;
- tag = tag.next;
- }
- line.Grow(1);
- line.recalc = true;
- UpdateView(line, pos);
- }
- // Inserts a character at the current caret position
- public void InsertCharAtCaret(char ch, bool move_caret) {
- LineTag tag;
- 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;
- UpdateView(caret.line, caret.pos);
- if (move_caret) {
- caret.pos++;
- UpdateCaret();
- }
- }
- // Inserts n characters at the given position; it will not delete past line limits
- public void DeleteChars(LineTag tag, int pos, int count) {
- Line line;
- bool streamline;
- streamline = false;
- line = tag.line;
- if (pos == line.text.Length) {
- return;
- }
- line.text.Remove(pos, count);
- // Check if we're crossing tag boundaries
- if ((pos + count) > (tag.start + tag.length)) {
- int left;
- // We have to delete cross tag boundaries
- streamline = true;
- left = count;
- left -= pos - tag.start;
- tag.length -= pos - tag.start;
- tag = tag.next;
- while ((tag != null) && (left > 0)) {
- if (tag.length > left) {
- tag.length -= left;
- left = 0;
- } else {
- left -= tag.length;
- tag.length = 0;
-
- tag = tag.next;
- }
- }
- } else {
- // We got off easy, same tag
- tag.length -= count;
- }
- tag = tag.next;
- while (tag != null) {
- tag.start -= count;
- tag = tag.next;
- }
- line.recalc = true;
- if (streamline) {
- line.Streamline();
- }
- UpdateView(line, pos);
- }
- // Deletes a character at or after the given position (depending on forward); it will not delete past line limits
- public void DeleteChar(LineTag tag, int pos, bool forward) {
- Line line;
- bool streamline;
- streamline = false;
- line = tag.line;
- if ((pos == 0 && forward == false) || (pos == line.text.Length && forward == true)) {
- return;
- }
- if (forward) {
- line.text.Remove(pos, 1);
- 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;
- }
- }
- }
- tag = tag.next;
- while (tag != null) {
- tag.start--;
- tag = tag.next;
- }
- line.recalc = true;
- if (streamline) {
- line.Streamline();
- }
- UpdateView(line, pos);
- }
- // Combine two lines
- public void Combine(int FirstLine, int SecondLine) {
- Combine(GetLine(FirstLine), GetLine(SecondLine));
- }
- public void Combine(Line first, Line second) {
- LineTag last;
- int shift;
- // Combine the two tag chains into one
- last = first.tags;
- while (last.next != null) {
- last = last.next;
- }
- last.next = second.tags;
- last.next.previous = last;
- shift = last.start + last.length - 1;
- // 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();
- #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
- public void Split(int LineNo, int pos) {
- Line line;
- LineTag tag;
- line = GetLine(LineNo);
- tag = LineTag.FindTag(line, pos);
- Split(line, tag, pos);
- }
- public void Split(Line line, int pos) {
- LineTag tag;
- tag = LineTag.FindTag(line, pos);
- Split(line, tag, pos);
- }
- public void Split(Line line, LineTag tag, int pos) {
- LineTag new_tag;
- Line new_line;
- // cover the easy case first
- if (pos == line.text.Length) {
- Add(line.line_no + 1, "", line.alignment, tag.font, tag.color);
- 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);
- // Now transfer our tags from this line to the next
- new_line = GetLine(line.line_no + 1);
- 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, 0);
- new_tag.font = tag.font;
- new_tag.color = tag.color;
- 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, tag.start - 1 + tag.length - pos);
- new_tag.next = tag.next;
- new_tag.font = tag.font;
- new_tag.color = tag.color;
- new_line.tags = new_tag;
- if (new_tag.next != null) {
- new_tag.next.previous = new_tag;
- }
- tag.next = null;
- tag.length = pos - tag.start + 1;
- 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;
- }
- }
- 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
- public void Add(int LineNo, string Text, Font font, Brush color) {
- Add(LineNo, Text, HorizontalAlignment.Left, font, color);
- }
- public void Add(int LineNo, string Text, HorizontalAlignment align, Font font, Brush color) {
- Line add;
- Line line;
- int line_no;
- 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(LineNo, Text, align, font, color);
- 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++;
- }
- public virtual void Clear() {
- lines = 0;
- document = sentinel;
- }
- public virtual object Clone() {
- Document clone;
- clone = new Document(null);
- clone.lines = this.lines;
- clone.document = (Line)document.Clone();
- return clone;
- }
- public void Delete(int LineNo) {
- if (LineNo>lines) {
- return;
- }
- Delete(GetLine(LineNo));
- }
- public 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;
- line1.ascent = line3.ascent;
- line1.height = line3.height;
- line1.line_no = line3.line_no;
- line1.recalc = line3.recalc;
- line1.space = line3.space;
- line1.tags = line3.tags;
- line1.text = line3.text;
- line1.widths = line3.widths;
- line1.Y = line3.Y;
- tag = line1.tags;
- while (tag != null) {
- tag.line = line1;
- tag = tag.next;
- }
- }
- if (line3.color == LineColor.Black)
- RebalanceAfterDelete(line2);
- this.lines--;
- last_found = sentinel;
- }
- // Set our selection markers
- public void Invalidate(Line start, int start_pos, Line end, int end_pos) {
- Line l1;
- Line l2;
- int p1;
- int p2;
-
- // 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;
- }
- owner.Invalidate(new Rectangle((int)l1.widths[p1] - viewport_x, l1.Y - viewport_y, (int)l2.widths[p2] - (int)l1.widths[p1], l1.height));
- return;
- }
- // Three invalidates:
- // First line from start
- owner.Invalidate(new Rectangle((int)l1.widths[p1] - viewport_x, l1.Y - viewport_y, owner.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 - viewport_x, y - viewport_y, owner.Width, GetLine(l2.line_no - 1).Y - y - viewport_y));
- }
- // Last line to end
- owner.Invalidate(new Rectangle((int)l1.widths[p1] - viewport_x, l1.Y - viewport_y, owner.Width, l1.height));
- }
- // It's nothing short of pathetic to always invalidate the whole control
- // I will find time to finish the optimization and make it invalidate deltas only
- public void SetSelectionToCaret(bool start) {
- if (start) {
- 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;
- } else {
- if (caret.line.line_no <= selection_end.line.line_no) {
- if ((caret.line != selection_end.line) || (caret.pos < selection_end.pos)) {
- selection_start.line = caret.line;
- selection_start.tag = caret.tag;
- selection_start.pos = caret.pos;
- } else {
- selection_end.line = caret.line;
- selection_end.tag = caret.tag;
- selection_end.pos = caret.pos;
- }
- } else {
- selection_end.line = caret.line;
- selection_end.tag = caret.tag;
- selection_end.pos = caret.pos;
- }
- }
- if ((selection_start.line == selection_end.line) && (selection_start.pos == selection_end.pos)) {
- selection_visible = false;
- } else {
- selection_visible = true;
- }
- owner.Invalidate();
- }
- #if buggy
- public void SetSelection(Line start, int start_pos, Line end, int end_pos) {
- // Ok, this is ugly, bad and slow, but I don't have time right now to optimize it.
- if (selection_visible) {
- // Try to only invalidate what's changed so we don't redraw the whole thing
- if ((start != selection_start.line) || (start_pos != selection_start.pos) && ((end == selection_end.line) && (end_pos == selection_end.pos))) {
- Invalidate(start, start_pos, selection_start.line, selection_start.pos);
- } else if ((end != selection_end.line) || (end_pos != selection_end.pos) && ((start == selection_start.line) && (start_pos == selection_start.pos))) {
- Invalidate(end, end_pos, selection_end.line, selection_end.pos);
- } else {
- // both start and end changed
- Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
- }
- }
- selection_start.line = start;
- selection_start.pos = start_pos;
- if (start != null) {
- selection_start.tag = LineTag.FindTag(start, start_pos);
- }
- selection_end.line = end;
- selection_end.pos = end_pos;
- if (end != null) {
- selection_end.tag = LineTag.FindTag(end, end_pos);
- }
- if (((start == end) && (start_pos == end_pos)) || start == null || end == null) {
- selection_visible = false;
- } else {
- selection_visible = true;
-
- }
- }
- #endif
- // Make sure that start is always before end
- private void FixupSelection() {
- if (selection_start.line.line_no > selection_end.line.line_no) {
- Line line;
- int pos;
- LineTag tag;
- line = selection_start.line;
- tag = selection_start.tag;
- pos = selection_start.pos;
- selection_start.line = selection_end.line;
- selection_start.tag = selection_end.tag;
- selection_start.pos = selection_end.pos;
-
- selection_end.line = line;
- selection_end.tag = tag;
- selection_end.pos = pos;
- return;
- }
- if ((selection_start.line == selection_end.line) && (selection_start.pos > selection_end.pos)) {
- int pos;
- pos = selection_start.pos;
- selection_start.pos = selection_end.pos;
- selection_end.pos = pos;
- Console.WriteLine("flipped: sel start: {0} end: {1}", selection_start.pos, selection_end.pos);
- return;
- }
- return;
- }
- public void SetSelectionStart(Line start, int start_pos) {
- selection_start.line = start;
- selection_start.pos = start_pos;
- selection_start.tag = LineTag.FindTag(start, start_pos);
- FixupSelection();
- if ((selection_end.line != selection_start.line) || (selection_end.pos != selection_start.pos)) {
- selection_visible = true;
- }
- if (selection_visible) {
- Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
- }
- }
- public void SetSelectionEnd(Line end, int end_pos) {
- selection_end.line = end;
- selection_end.pos = end_pos;
- selection_end.tag = LineTag.FindTag(end, end_pos);
- FixupSelection();
- if ((selection_end.line != selection_start.line) || (selection_end.pos != selection_start.pos)) {
- selection_visible = true;
- }
- if (selection_visible) {
- Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
- }
- }
- public 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_visible = false;
- }
- public void InvalidateSelectionArea() {
- // implement me
- }
- // Return the current selection, as string
- public 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 (!multiline || (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) + "\n");
- if ((start + 1) < end) {
- for (i = start + 1; i < end; i++) {
- sb.Append(GetLine(i).text.ToString() + "\n");
- }
- }
- sb.Append(selection_end.line.text.ToString(0, selection_end.pos));
- return sb.ToString();
- }
- }
- public void ReplaceSelection(string s) {
- // The easiest is to break the lines where the selection tags are and delete those lines
- if ((selection_start.pos == selection_end.pos) && (selection_start.line == selection_end.line)) {
- // Nothing to delete, simply insert
- InsertString(selection_start.tag, selection_start.pos, s);
- }
- if (!multiline || (selection_start.line == selection_end.line)) {
- 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);
- InsertString(selection_start.tag, selection_start.pos, s);
- } else {
- int i;
- int start;
- int end;
- int base_line;
- string[] ins;
- int insert_lines;
- start = selection_start.line.line_no;
- end = selection_end.line.line_no;
- // Delete first line
- DeleteChars(selection_start.tag, selection_start.pos, selection_end.pos - selection_start.pos);
- start++;
- if (start < end) {
- for (i = end - 1; i >= start; i++) {
- Delete(i);
- }
- }
- // Delete last line
- DeleteChars(selection_end.line.tags, 0, selection_end.pos);
- ins = s.Split(new char[] {'\n'});
- insert_lines = ins.Length;
- // Bump the text at insertion point a line down if we're inserting more than one line
- if (insert_lines > 1) {
- Split(selection_start.line, selection_start.pos);
- // Reminder of start line is now in startline+1
- // if the last line does not end with a \n we will insert the last line in front of the just moved text
- if (s.EndsWith("\n")) {
- insert_lines--; // We don't want to insert the last line as part of the loop anymore
- InsertString(GetLine(selection_start.line.line_no + 1), 0, ins[insert_lines - 1]);
- }
- }
- // Insert the first line
- InsertString(selection_start.line, selection_start.pos, ins[0]);
- if (insert_lines > 1) {
- base_line = selection_start.line.line_no + 1;
- for (i = 1; i < insert_lines; i++) {
- Add(base_line + i, ins[i], selection_start.line.alignment, selection_start.tag.font, selection_start.tag.color);
- }
- }
- }
- selection_end.line = selection_start.line;
- selection_end.pos = selection_start.pos;
- selection_end.tag = selection_start.tag;
- selection_visible = false;
- InvalidateSelectionArea();
- }
- public int SelectionLength() {
- if ((selection_start.pos == selection_end.pos) && (selection_start.line == selection_end.line)) {
- return 0;
- }
- if (!multiline || (selection_start.line == selection_end.line)) {
- return selection_end.pos - selection_start.pos;
- } else {
- int i;
- int start;
- int end;
- int length;
- Line line;
- // Count first and last line
- length = selection_start.line.text.Length - selection_start.pos + selection_end.pos;
- // 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++) {
- length += GetLine(i).text.Length;
- }
- }
- return length;
- }
-
- }
- // Give it a Line number and it returns the Line object at with that line number
- public 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;
- }
- // Give it a Y pixel coordinate and it returns the Line covering that Y coordinate
- ///
- public Line GetLineByPixel(int y, bool exact) {
- Line line = document;
- Line last = null;
- while (line != sentinel) {
- last = line;
- if ((y >= line.Y) && (y < (line.Y+line.height))) {
- return line;
- } else if (y < line.Y) {
- 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
- public 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.align_shift;
- 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 tag;
- }
- }
- index=end;
- return tag;
- }
- if (tag.next != null) {
- tag = tag.next;
- } else {
- if (exact) {
- index = 0;
- return null;
- }
- index = line.text.Length;
- return tag;
- }
- }
- }
- // Give it x/y pixel coordinates and it returns the Tag at that position; optionally the char position is returned in index
- public LineTag FindCursor(int x, int y, out int index) {
- Line line;
- LineTag tag;
- line = GetLineByPixel(y, false);
- tag = line.tags;
- // Adjust for alignment
- x += line.align_shift;
- while (true) {
- if (x >= tag.X && x < (tag.X+tag.width)) {
- int end;
- end = tag.start + tag.length - 1;
- 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.widths[pos] + ((line.widths[pos+1]-line.widths[pos])/2))) {
- index = pos;
- return tag;
- }
- }
- index=end;
- return tag;
- }
- if (tag.next != null) {
- tag = tag.next;
- } else {
- index = line.text.Length;
- return tag;
- }
- }
- }
- public void RecalculateAlignments() {
- Line line;
- int line_no;
- line_no = 1;
- while (line_no <= lines) {
- line = GetLine(line_no);
- if (line.alignment != HorizontalAlignment.Left) {
- if (line.alignment == HorizontalAlignment.Center) {
- line.align_shift = (document_x - (int)line.widths[line.text.Length]) / 2;
- } else {
- line.align_shift = document_x - (int)line.widths[line.text.Length];
- }
- }
- line_no++;
- }
- return;
- }
- // Calculate formatting for the whole document
- public bool RecalculateDocument(Graphics g) {
- return RecalculateDocument(g, 1, this.lines, false);
- }
- // Calculate formatting starting at a certain line
- public bool RecalculateDocument(Graphics g, int start) {
- return RecalculateDocument(g, start, this.lines, false);
- }
- // Calculate formatting within two given line numbers
- public bool RecalculateDocument(Graphics g, int start, int end) {
- return RecalculateDocument(g, start, end, false);
- }
- // With optimize on, returns true if line heights changed
- public bool RecalculateDocument(Graphics g, int start, int end, bool optimize) {
- Line line;
- int line_no;
- int Y;
- Y = GetLine(start).Y;
- line_no = start;
- if (optimize) {
- bool changed;
- bool alignment_recalc;
- changed = false;
- alignment_recalc = false;
- while (line_no <= end) {
- line = GetLine(line_no++);
- line.Y = Y;
- if (line.recalc) {
- if (line.RecalculateLine(g)) {
- changed = true;
- // If the height changed, all subsequent lines change
- end = this.lines;
- }
- if (line.widths[line.text.Length] > this.document_x) {
- this.document_x = (int)line.widths[line.text.Length];
- alignment_recalc = true;
- }
- // Calculate alignment
- if (line.alignment != HorizontalAlignment.Left) {
- if (line.alignment == HorizontalAlignment.Center) {
- line.align_shift = (document_x - (int)line.widths[line.text.Length]) / 2;
- } else {
- line.align_shift = document_x - (int)line.widths[line.text.Length];
- }
- }
- }
- Y += line.height;
- }
- RecalculateAlignments();
- return changed;
- } else {
- while (line_no <= end) {
- line = GetLine(line_no++);
- line.Y = Y;
- line.RecalculateLine(g);
- if (line.widths[line.text.Length] > this.document_x) {
- this.document_x = (int)line.widths[line.text.Length];
- }
- // Calculate alignment
- if (line.alignment != HorizontalAlignment.Left) {
- if (line.alignment == HorizontalAlignment.Center) {
- line.align_shift = (document_x - (int)line.widths[line.text.Length]) / 2;
- } else {
- line.align_shift = document_x - (int)line.widths[line.text.Length];
- }
- }
- Y += line.height;
- }
- RecalculateAlignments();
- return true;
- }
- }
- public bool SetCursor(int x, int y) {
- return true;
- }
- public int Size() {
- return lines;
- }
- #endregion // Public Methods
- #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
- }
- public class LineTag {
- #region Local Variables;
- // Payload; formatting
- internal Font font; // System.Drawing.Font object for this tag
- internal Brush color; // System.Drawing.Brush object
- // Payload; text
- internal int start; // start, in chars; index into Line.text
- internal int length; // length, in chars
- internal bool r_to_l; // Which way is the font
- // Drawing support
- internal int height; // Height in pixels of the text this tag describes
- internal int X; // X location of the text this tag describes
- internal float width; // Width in pixels of the text this tag describes
- internal int ascent; // Ascent of the font for this tag
- internal int shift; // Shift down for this tag, to stay on baseline
- internal int soft_break; // Tag is 'broken soft' and continues in the next line
- // Administrative
- internal Line line; // The line we're on
- internal LineTag next; // Next tag on the same line
- internal LineTag previous; // Previous tag on the same line
- #endregion;
- #region Constructors
- public LineTag(Line line, int start, int length) {
- this.line = line;
- this.start = start;
- this.length = length;
- this.X = 0;
- this.width = 0;
- }
- #endregion // Constructors
- #region Public Methods
- //
- // Applies 'font' to characters starting at 'start' for 'length' chars
- // Removes any previous tags overlapping the same area
- // returns true if lineheight has changed
- //
- public static bool FormatText(Line line, int start, int length, Font font, Brush color) {
- LineTag tag;
- LineTag start_tag;
- LineTag end_tag;
- int end;
- int state;
- int left;
- bool retval = false; // Assume line-height doesn't change
- // Too simple?
- if (font.Height != line.height) {
- retval = true;
- }
- line.recalc = true; // This forces recalculation of the line in RecalculateDocument
- // A little sanity, not sure if it's needed, might be able to remove for speed
- if (length > line.text.Length) {
- length = line.text.Length;
- }
- tag = line.tags;
- end = start + length;
- state = 0;
- // Common special case
- if ((start == 1) && (length == tag.length)) {
- tag.ascent = 0;
- tag.font = font;
- tag.color = color;
- return retval;
- }
- start_tag = FindTag(line, start);
- end_tag = FindTag(line, end);
- tag = new LineTag(line, start, length);
- tag.font = font;
- tag.color = color;
- if (start == 1) {
- line.tags = tag;
- }
- if (start_tag.start == start) {
- tag.next = start_tag;
- tag.previous = start_tag.previous;
- if (start_tag.previous != null) {
- start_tag.previous.next = tag;
- }
- start_tag.previous = tag;
- } else {
- // Insert ourselves 'in the middle'
- if ((start_tag.next != null) && (start_tag.next.start < end)) {
- tag.next = start_tag.next;
- } else {
- tag.next = new LineTag(line, start_tag.start, start_tag.length);
- tag.next.font = start_tag.font;
- tag.next.color = start_tag.color;
- if (start_tag.next != null) {
- tag.next.next = start_tag.next;
- tag.next.next.previous = tag.next;
- }
- }
- tag.next.previous = tag;
- start_tag.length = start - start_tag.start;
- tag.previous = start_tag;
- start_tag.next = tag;
- #if nope
- if (tag.next.start > (tag.start + tag.length)) {
- tag.next.length += tag.next.start - (tag.start + tag.length);
- tag.next.start = tag.start + tag.length;
- }
- #endif
- }
- // Elimination loop
- tag = tag.next;
- while ((tag != null) && (tag.start < end)) {
- if ((tag.start + tag.length) <= end) {
- // remove the tag
- tag.previous.next = tag.next;
- if (tag.next != null) {
- tag.next.previous = tag.previous;
- }
- tag = tag.previous;
- } else {
- // Adjust the length of the tag
- tag.length = (tag.start + tag.length) - end;
- tag.start = end;
- }
- tag = tag.next;
- }
- return retval;
- }
- //
- // Finds the tag that describes the character at position 'pos' on 'line'
- //
- public static LineTag FindTag(Line line, int pos) {
- LineTag tag = line.tags;
- // Beginning of line is a bit special
- if (pos == 0) {
- return tag;
- }
- while (tag != null) {
- if ((tag.start <= pos) && (pos < (tag.start+tag.length))) {
- return tag;
- }
- tag = tag.next;
- }
- return null;
- }
- //
- // Combines 'this' tag with 'other' tag.
- //
- public bool Combine(LineTag other) {
- if (!this.Equals(other)) {
- return false;
- }
- this.width += other.width;
- this.length += other.length;
- this.next = other.next;
- if (this.next != null) {
- this.next.previous = this;
- }
- return true;
- }
- //
- // Remove 'this' tag ; to be called when formatting is to be removed
- //
- public bool Remove() {
- if ((this.start == 1) && (this.next == null)) {
- // We cannot remove the only tag
- return false;
- }
- if (this.start != 1) {
- this.previous.length += this.length;
- this.previous.width = -1;
- this.previous.next = this.next;
- this.next.previous = this.previous;
- } else {
- this.next.start = 1;
- this.next.length += this.length;
- this.next.width = -1;
- this.line.tags = this.next;
- this.next.previous = null;
- }
- return true;
- }
- //
- // Checks if 'this' tag describes the same formatting options as 'obj'
- //
- public override bool Equals(object obj) {
- LineTag other;
- if (obj == null) {
- return false;
- }
- if (!(obj is LineTag)) {
- return false;
- }
- if (obj == this) {
- return true;
- }
- other = (LineTag)obj;
- if (this.font.Equals(other.font) && this.color.Equals(other.color)) { // FIXME add checking for things like link or type later
- return true;
- }
- return false;
- }
- public override string ToString() {
- return "Tag starts at index " + this.start + "length " + this.length + " text: " + this.line.Text.Substring(this.start-1, this.length) + "Font " + this.font.ToString();
- }
- #endregion // Public Methods
- }
- }
|