Exemplo n.º 1
0
        /// <summary>
        /// Very simple syntax highlighting
        /// </summary>
        /// <param name="targetTextBox"></param>
        /// <param name="s"></param>
        internal void PrintWithSyntaxHighlighting(EfficientTextBox targetTextBox, string s)
        {
            if (s.Trim().StartsWith("[Example("))
            {
                return;
            }
            if (s.Trim().StartsWith("//"))
            {
                targetTextBox.SelectionColor = Color.Green;
                targetTextBox.AppendText(s + Environment.NewLine);
                targetTextBox.SelectionColor = Color.Black;
                return;
            }
            targetTextBox.SelectionBackColor = Color.White;
            if (s.Contains("//highlight"))
            {
                s = s.Replace("//highlight", "");
                //                targetTextBox.SelectionFont = boldFont;
                targetTextBox.SelectionBackColor = Color.Yellow;
            }
            //            else targetTextBox.SelectionFont = normalFont;
            MatchCollection mc  = reg.Matches(s);
            int             ind = 0;

            foreach (Match m in mc)
            {
                targetTextBox.AppendText(s.Substring(ind, m.Index - ind));
                ind = m.Index + m.Length;
                string word     = s.Substring(m.Index, m.Length);
                bool   reserved = IsReservedWord(word);
                if (reserved)
                {
                    targetTextBox.SelectionColor = Color.Blue;
                }
                if (IsKnownTypeName(word))
                {
                    targetTextBox.SelectionColor = Color.DarkCyan;
                }
                targetTextBox.AppendText(word);
                targetTextBox.SelectionColor = Color.Black;
            }
            if (ind < s.Length)
            {
                targetTextBox.AppendText(s.Substring(ind));
            }
            targetTextBox.AppendText(Environment.NewLine);
            //sourceTextBox.AppendText(s + Environment.NewLine);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Selection changed handler
        /// </summary>
        protected void OnSelectionChanged()
        {
            Type exampleClass = SelectedExample;

            if (exampleClass == null)
            {
                return;
            }
            SuspendLayout();
            exampleSummaryBox.Clear();
            exampleSummaryBox.SelectionColor = Color.DarkBlue;
            exampleSummaryBox.SelectionFont  = new Font(FontFamily.GenericSansSerif, 11f, FontStyle.Bold);
            exampleSummaryBox.AppendText(exampleClass.Name + Environment.NewLine);
            //label1.Text = "'" + tutorialClass.Name + "' source code";
            ExampleAttribute exa = GetExampleAttribute(exampleClass);

            exampleSummaryBox.SelectionFont  = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Regular);
            exampleSummaryBox.SelectionColor = Color.FromArgb(0, 0, 100);

            string desc = "";

            if (exa != null)
            {
                desc = exa.Description;
            }
            exampleSummaryBox.AppendText(desc);
            exampleSummaryBox.Size    = exampleSummaryBox.GetPreferredSize(exampleSummaryBox.Size);
            exampleSummaryBox.Height += 10;
            exampleSummaryBox.Refresh();
            string filename = GetSourceCodeFilename(exampleClass);

            DoubleBuffered = true;
            //sourceTextBox.SuspendLayout();
            try
            {
                if (filename == null)
                {
                    sourceTextBox.Text = "Example source code was not found.  " + Environment.NewLine +
                                         "Go to the properties of the source file in Visual Studio and set 'Copy To Output Directory' to 'Copy if newer'.";
                }
                else
                {
                    RichTextBox tempBox = new RichTextBox();
                    tempBox.Font          = sourceTextBox.Font;
                    tempBox.SelectionTabs = sourceTextBox.SelectionTabs;
                    EfficientTextBox etb      = new EfficientTextBox(tempBox);
                    StreamReader     sr       = new StreamReader(filename);
                    bool             isHeader = true;
                    while (true)
                    {
                        string line = sr.ReadLine();
                        if (line == null)
                        {
                            break;
                        }
                        if (isHeader)
                        {
                            string trimmed = line.TrimStart();
                            if (trimmed.Length == 0 || trimmed.StartsWith("// "))
                            {
                                continue;
                            }
                            else
                            {
                                isHeader = false;
                            }
                        }
                        PrintWithSyntaxHighlighting(etb, line);
                    }
                    etb.Flush();
                    sr.Close();
                    sourceTextBox.Rtf = tempBox.Rtf;
                }
                //sourceTextBox.ResumeLayout(true);
            }
            finally
            {
                ResumeLayout(true);
            }
            //this.PerformLayout();
        }