| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952 |
- // 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-2005 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 {
- internal enum LineColor {
- Red = 0,
- Black = 1
- }
- 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
- 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...
- internal 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
- internal bool soft_break; // Tag is 'broken soft' and continuation from previous line
- // Stuff that's important for the tree
- internal Line parent; // Our parent line
- internal Line left; // Line with smaller line number
- internal 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
- internal Line() {
- color = LineColor.Red;
- left = null;
- right = null;
- parent = null;
- text = null;
- recalc = true;
- soft_break = false;
- alignment = HorizontalAlignment.Left;
- if (string_format == null) {
- string_format = new StringFormat(StringFormat.GenericTypographic);
- string_format.Trimming = StringTrimming.None;
- string_format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
- }
- }
- internal 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;
- }
- internal 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;
- }
- internal 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 Internal Properties
- internal int Height {
- get {
- return height;
- }
- set {
- height = value;
- }
- }
- internal int LineNo {
- get {
- return line_no;
- }
- set {
- line_no = value;
- }
- }
- internal string Text {
- get {
- return text.ToString();
- }
- set {
- text = new StringBuilder(value, value.Length > DEFAULT_TEXT_LEN ? value.Length : DEFAULT_TEXT_LEN);
- }
- }
- internal HorizontalAlignment Alignment {
- get {
- return alignment;
- }
- set {
- if (alignment != value) {
- alignment = value;
- recalc = true;
- }
- }
- }
- #if no
- internal StringBuilder Text {
- get {
- return text;
- }
- set {
- text = value;
- }
- }
- #endif
- #endregion // Internal Properties
- #region Internal Methods
- // Make sure we always have enoughs space in text and widths
- internal 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;
- }
- }
- internal 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;
- }
- }
- /// <summary> Find the tag on a line based on the character position</summary>
- internal 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;
- }
- /// <summary>
- /// Go through all tags on a line and recalculate all size-related values;
- /// returns true if lineheight changed
- /// </summary>
- internal bool RecalculateLine(Graphics g, Document doc) {
- LineTag tag;
- int pos;
- int len;
- SizeF size;
- float w;
- int prev_height;
- bool retval;
- bool wrapped;
- Line line;
- int wrap_pos;
- float wrap_width;
- 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;
- retval = false;
- wrapped = false;
- wrap_pos = 0;
- wrap_width = 0;
- while (pos < len) {
- size = g.MeasureString(this.text.ToString(pos, 1), tag.font, 10000, string_format);
- w = size.Width;
- if (Char.IsWhiteSpace(text[pos])) {
- wrap_pos = pos + 1;
- wrap_width = tag.width + w;
- }
- if (doc.wrap) {
- if ((widths[pos] + w) + 27 > doc.viewport_width) {
- pos = wrap_pos;
- tag.width = wrap_width;
- doc.Split(this, tag, pos, true);
- len = this.text.Length;
- retval = true;
- wrapped = true;
- }
- }
- // Contract all soft lines that follow back into our line
- if (!wrapped) {
- tag.width += w;
- pos++;
- widths[pos] = widths[pos-1] + w;
- if (pos == len) {
- line = doc.GetLine(this.line_no + 1);
- if ((line != null) && (line.soft_break)) {
- // Pull the previous line back into this one
- doc.Combine(this.line_no, this.line_no + 1);
- len = this.text.Length;
- retval = true;
- }
- }
- }
- 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;
- wrap_pos = pos;
- wrap_width = tag.width;
- }
- }
- }
- if (this.height == 0) {
- this.height = tags.font.Height;
- tag.height = this.height;
- }
- if (prev_height != this.height) {
- retval = true;
- }
- return retval;
- }
- #endregion // Internal 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;
- }
- internal 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 int GetHashCode() {
- return base.GetHashCode ();
- }
- public override string ToString() {
- return "Line " + line_no;
- }
- #endregion // Administrative
- }
- internal 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 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 viewport_width;
- internal int viewport_height;
- 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
- internal 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;
- viewport_x = -2;
- viewport_y = -2;
- // Default selection is empty
- document_id = random.Next();
- }
- #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.align_shift, caret.line.Y);
- }
- }
- internal LineTag CaretTag {
- get {
- return caret.tag;
- }
- }
- internal int ViewPortX {
- get {
- return viewport_x;
- }
- set {
- viewport_x = value;
- }
- }
- 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 Wrap {
- get {
- return wrap;
- }
- set {
- wrap = value;
- // FIXME - force recalc/redisplay
- }
- }
- #endregion // Internal 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;
- }
- }
- internal void UpdateView(Line line, int pos) {
- 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, viewport_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, viewport_width, line.height));
- }
- }
- // 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 (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, viewport_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 Internal Methods
- // Clear the document and reset state
- internal 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;
- }
- internal 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);
- if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
- }
- internal void PositionCaret(int x, int y) {
- caret.tag = FindCursor(x, 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);
- if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
- }
- internal 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);
- }
- }
- internal void CaretLostFocus() {
- XplatUI.DestroyCaret(owner.Handle);
- }
- internal 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);
- if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
- }
- internal 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);
- if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
- }
- internal void DisplayCaret() {
- XplatUI.CaretVisible(owner.Handle, true);
- }
- internal void HideCaret() {
- XplatUI.CaretVisible(owner.Handle, false);
- }
- internal 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) {
- 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
- 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
- string s; // String representing the current line
- int line_no; //
- Brush hilight;
- Brush hilight_text;
- // 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;
- Console.WriteLine("Starting drawing at line {0}, ending at line {1} (clip-bottom:{2})", start, end, clip.Bottom);
- // 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
- hilight = ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorHilight);
- hilight_text = ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorHilightText);
- 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) || (!owner.has_focus) || (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(hilight, 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, hilight_text, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
- } else {
- 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(hilight, 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, hilight_text, 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(hilight, line.widths[selection_start.pos] + line.align_shift, line.Y + tag.shift - viewport_y, line.widths[tag.start + tag.length - 1] - line.widths[selection_start.pos], tag.height);
- g.DrawString(s.Substring(selection_start.pos, tag.length - selection_start.pos), tag.font, hilight_text, 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(hilight, 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, hilight_text, 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(hilight, 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, hilight_text, 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
- 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;
- 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
- internal 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
- 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;
- 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
- internal 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
- internal 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
- internal 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
- internal void Combine(int FirstLine, int SecondLine) {
- Combine(GetLine(FirstLine), GetLine(SecondLine));
- }
- internal 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
- internal void Split(int LineNo, int pos) {
- Line line;
- LineTag tag;
- line = GetLine(LineNo);
- tag = LineTag.FindTag(line, pos);
- Split(line, tag, pos, false);
- }
- internal void Split(Line line, int pos) {
- LineTag tag;
- tag = LineTag.FindTag(line, pos);
- Split(line, tag, pos, false);
- }
- internal void Split(Line line, LineTag tag, int pos, bool soft) {
- LineTag new_tag;
- Line new_line;
- bool move_caret;
- move_caret = false;
- // Adjust selection and cursors
- if (soft && (caret.line == line) && (caret.pos >= pos)) {
- move_caret = true;
- }
- // cover the easy case first
- if (pos == line.text.Length) {
- Add(line.line_no + 1, "", line.alignment, tag.font, tag.color);
- if (soft) {
- if (move_caret) {
- caret.line = GetLine(line.line_no + 1);
- caret.line.soft_break = true;
- caret.tag = selection_start.line.tags;
- caret.pos = 0;
- } else {
- GetLine(line.line_no + 1).soft_break = true;
- }
- }
- 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;
- }
- }
- if (soft) {
- if (move_caret) {
- caret.line = new_line;
- caret.pos = caret.pos - pos;
- caret.tag = caret.line.FindTag(caret.pos);
- }
- new_line.soft_break = true;
- }
- 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, Brush color) {
- Add(LineNo, Text, HorizontalAlignment.Left, font, color);
- }
- internal 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++;
- }
- internal 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;
- }
- internal void Delete(int LineNo) {
- if (LineNo>lines) {
- return;
- }
- Delete(GetLine(LineNo));
- }
- 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;
- 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;
- line1.soft_break = line3.soft_break;
- 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
- internal 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, 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 - viewport_x, y - viewport_y, viewport_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, viewport_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
- internal 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
- internal 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;
- }
- internal 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);
- }
- }
- internal 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);
- }
- }
- 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_visible = false;
- }
- internal void InvalidateSelectionArea() {
- // implement me
- }
- // 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 (!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) + 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) {
- // 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'});
- for (int j = 0; j < ins.Length; j++) {
- if (ins[j].EndsWith("\r")) {
- ins[j] = ins[j].Substring(0, ins[j].Length - 1);
- }
- }
- 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;
- PositionCaret(selection_start.line, selection_start.pos);
- InvalidateSelectionArea();
- }
- 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 + 2; // We count the trailing \n as a char
- if (index <= chars) {
- // we found the line
- tag = line.tags;
- while (tag != null) {
- if (index < (start + tag.start + tag.length)) {
- line_out = line;
- tag_out = 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 = next_line.tags;
- pos = 0;
- return;
- } else {
- line_out = line;
- tag_out = 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 + 2; // Add one for the \n at the end of the line
- }
- length += pos;
- return length;
- }
- internal 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;
- // 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;
- }
-
- }
- /// <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;
- }
- /// <summary>Give it a Y pixel coordinate and it returns the Line covering that Y coordinate</summary>
- internal 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
- 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.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
- internal 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;
- }
- }
- }
- internal void FormatText(Line start_line, int start_pos, Line end_line, int end_pos, Font font, Brush color) {
- Line l;
- // First, format the first line
- LineTag.FormatText(start_line, start_pos, start_line.text.Length - start_pos, font, color);
- // Format last line
- if (end_line != start_line) {
- LineTag.FormatText(end_line, 0, end_pos, font, color);
- }
- // Now all the lines inbetween
- for (int i = start_line.line_no + 1; i < end_line.line_no - 1; i++) {
- l = GetLine(i);
- LineTag.FormatText(l, 0, l.text.Length, font, color);
- }
- }
- internal 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 = (viewport_width - (int)line.widths[line.text.Length]) / 2;
- } else {
- line.align_shift = viewport_width - (int)line.widths[line.text.Length];
- }
- }
- 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 Y;
- int new_width;
- Y = GetLine(start).Y;
- line_no = start;
- new_width = 0;
- if (optimize) {
- bool changed;
- bool alignment_recalc;
- changed = false;
- alignment_recalc = true;
- while (line_no <= end) {
- line = GetLine(line_no++);
- line.Y = Y;
- if (line.recalc) {
- if (line.RecalculateLine(g, this)) {
- changed = true;
- // If the height changed, all subsequent lines change
- end = 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];
- }
- }
- }
- Y += line.height;
- if (line_no > lines) {
- break;
- }
- }
- if (document_x < new_width) {
- document_x = new_width;
- alignment_recalc = true;
- if (WidthChanged != null) {
- WidthChanged(this, null);
- }
- }
- if (alignment_recalc) {
- RecalculateAlignments();
- }
- line = GetLine(lines);
- if (document_y != line.Y + line.height) {
- document_y = line.Y + line.height;
- if (HeightChanged != null) {
- HeightChanged(this, null);
- }
- }
- return changed;
- } else {
- int shift;
- shift = 0;
- while (line_no <= (end + shift)) {
- line = GetLine(line_no++);
- line.Y = Y;
- shift = this.lines;
- line.RecalculateLine(g, this);
- if (this.lines > shift) {
- shift = this.lines - shift;
- } else {
- shift = 0;
- }
- 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];
- }
- }
- Y += line.height;
- 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);
- }
- }
- return true;
- }
- }
- internal bool SetCursor(int x, int y) {
- return true;
- }
- internal int Size() {
- return lines;
- }
- #endregion // Internal Methods
- #region Events
- internal event EventHandler CaretMoved;
- internal event EventHandler WidthChanged;
- internal event EventHandler HeightChanged;
- #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 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
- // 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
- internal 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 Internal Methods
- /// <summary>Applies 'font' to characters starting at 'start' for 'length' chars;
- /// Removes any previous tags overlapping the same area;
- /// returns true if lineheight has changed</summary>
- internal static bool FormatText(Line line, int start, int length, Font font, Brush color) {
- LineTag tag;
- LineTag start_tag;
- int end;
- 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;
- // 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);
- 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;
- }
- /// <summary>Finds the tag that describes the character at position 'pos' on 'line'</summary>
- internal 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;
- }
- /// <summary>Combines 'this' tag with 'other' tag</summary>
- internal 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;
- }
- /// <summary>Remove 'this' tag ; to be called when formatting is to be removed</summary>
- internal 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;
- }
- /// <summary>Checks if 'this' tag describes the same formatting options as 'obj'</summary>
- 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 int GetHashCode() {
- return base.GetHashCode ();
- }
- 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 // Internal Methods
- }
- }
|