Alexandria.Engines.GoldBox.Resources.Script.Browse C# (CSharp) 메소드

Browse() 공개 메소드

public Browse ( Action progressUpdateCallback = null ) : Control
progressUpdateCallback Action
리턴 System.Windows.Forms.Control
        public override System.Windows.Forms.Control Browse(Action<double> progressUpdateCallback = null)
        {
            RichTextBuilder builder = new RichTextBuilder();
            bool indentNext = false;

            //builder.Append(@"{\rtf\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\par ");
            builder.UnderlineStyle = RichTextUnderlineStyle.Double;

            foreach (ScriptInstruction instruction in Instructions) {
                string codeName = CodeAddressName(instruction.Address);

                if (codeName != null)
                    builder.AppendFormat("\n==============================\n{0}\n==============================\n", codeName);

                if (instruction.IsTarget)
                    builder.IsUnderlined = true;
                builder.AppendFormat("{0:X}h", instruction.Address);
                if (instruction.IsTarget)
                    builder.IsUnderlined = false;
                builder.Append(":\t");

                if (indentNext)
                    builder.Append("\t");
                instruction.ToRichText(builder);
                //builder.Append(instruction.ToString());
                builder.Append("\n");
                if (instruction.IsBranch && !indentNext)
                    builder.Append("\n");
                indentNext = instruction.IsTest;
            }

            builder.Append(ExceptionEnd);
            var output = builder.ToString();

            BetterRichTextBox box = new BetterRichTextBox() {
                ReadOnly = false,
                Multiline = true,
                //ScrollBars = ScrollBars.Vertical,
                Rtf = output,
                WordWrap = false,
            };

            Font normal = box.Font;
            var underlined = new Font(normal, FontStyle.Underline);
            bool recurse = false;

            box.SelectionChanged += (object sender, EventArgs args) => {
                if (recurse)
                    return;

                try {
                    Point scrollPosition = box.ScrollPosition;
                    int resetStart = box.SelectionStart, resetLength = box.SelectionLength;
                    box.BeginUpdate();

                    recurse = true;
                    var start = box.SelectionStart;
                    string text = box.Text;

                    if (!IsIdentifier(text, start) && !IsIdentifier(text, start - 1))
                        return;
                    while (IsIdentifier(text, start - 1))
                        start--;

                    int end = box.SelectionStart;
                    while (IsIdentifier(text, end))
                        end++;

                    string searchText = text.Substring(start, end - start);

                    box.SelectAll();
                    //box.SelectionFont = normal;
                    box.SelectionBackColor = Color.Transparent;

                    start = 0;
                    while (true) {
                        start = text.IndexOf(searchText, start);
                        if (start < 0)
                            break;

                        if (!IsIdentifier(text, start - 1) && !IsIdentifier(text, start + searchText.Length)) {
                            box.Select(start, searchText.Length);
                            //box.SelectionFont = underlined;
                            box.SelectionBackColor = Color.Yellow;
                        }

                        start += searchText.Length;
                    }

                    box.Select(resetStart, resetLength);
                    box.ScrollPosition = scrollPosition;
                } finally {
                    box.EndUpdate();
                    box.Invalidate();
                    recurse = false;
                }
            };

            box.LinkClicked += (sender, args) => {
                throw new Exception();
            };

            return box;
        }