⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 painter_gdi.cs

📁 Fireball.CodeEditor is an source code editor control derived from the best compona SyntaxBox Control
💻 CS
📖 第 1 页 / 共 3 页
字号:
//ORIGINAL LGPL SOURCE CODE FINDED ON COMPONA LGPL SOURCE CODE

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Fireball.Drawing.GDI;
using Fireball.Syntax;
using Fireball.Windows.Forms.CodeEditor.TextDraw;
using Fireball.Globalization;
using System.Globalization;

namespace Fireball.Windows.Forms.CodeEditor.Painter
{
	/// <summary>
	/// Painter class that uses GDI32 to render the content of a SyntaxBoxControl
	/// </summary>
    public class Painter_GDI : IPainter
    {
        private int FirstSpanRow = 0;
        private int LastSpanRow = 0;
        private bool SpanFound = false;

        private int LastRow = 0;
        private int yOffset = 0;
        private EditViewControl Control;
        private RenderItems GFX = new RenderItems();
        private int ResizeCount = 0;

        private Word BracketStart, BracketEnd;

        /// <summary>
        /// Implementation of the IPainter Resize method
        /// </summary>
        public void Resize()
        {
            ResizeCount++;
            this.InitGraphics();
            //	Console.WriteLine ("painterresize {0} {1}",ResizeCount,Control.Name);

        }

        /// <summary>
        /// Painter_GDI constructor.
        /// </summary>
        /// <param name="control">The control that will use the Painter</param>
        public Painter_GDI(EditViewControl control)
        {
            Control = control;
            InitGraphics();

        }

        /// <summary>
        /// Implementation of the IPainter MeasureString method
        /// </summary>
        /// <param name="s">String to measure</param>
        /// <returns>Size of the string in pixels</returns>
        public Size MeasureString(string s)
        {
            //try
            //{
            GFX.StringBuffer.Font = GFX.FontNormal;
            return GFX.StringBuffer.MeasureTabbedString(s, Control.TabSize);
            //}
            //catch
            //{
            //    return new Size(0, 0);
            //}
        }

        /// <summary>
        /// Implementation of the IPainter InitGraphics method.
        /// Initializes GDI32 backbuffers and brushes.
        /// </summary>
        public void InitGraphics()
        {
            
                if (GFX.BackgroundBrush != null)
                    GFX.BackgroundBrush.Dispose();

                if (GFX.GutterMarginBrush != null)
                    GFX.GutterMarginBrush.Dispose();

                if (GFX.LineNumberMarginBrush != null)
                    GFX.LineNumberMarginBrush.Dispose();

                if (GFX.HighLightLineBrush != null)
                    GFX.HighLightLineBrush.Dispose();

                if (GFX.LineNumberMarginBorderBrush != null)
                    GFX.LineNumberMarginBorderBrush.Dispose();

                if (GFX.GutterMarginBorderBrush != null)
                    GFX.GutterMarginBorderBrush.Dispose();

                if (GFX.OutlineBrush != null)
                    GFX.OutlineBrush.Dispose();


                GFX.BackgroundBrush = new GDIBrush(Control.BackColor);
                GFX.GutterMarginBrush = new GDIBrush(Control.GutterMarginColor);
                GFX.LineNumberMarginBrush = new GDIBrush(Control.LineNumberBackColor);
                GFX.HighLightLineBrush = new GDIBrush(Control.HighLightedLineColor);
                GFX.LineNumberMarginBorderBrush = new GDIBrush
                    (Control.LineNumberBorderColor);
                GFX.GutterMarginBorderBrush = new GDIBrush
                    (Control.GutterMarginBorderColor);
                GFX.OutlineBrush = new GDIBrush(Control.OutlineColor);


                if (GFX.FontNormal != null)
                    GFX.FontNormal.Dispose();

                if (GFX.FontBold != null)
                    GFX.FontBold.Dispose();

                if (GFX.FontItalic != null)
                    GFX.FontItalic.Dispose();

                if (GFX.FontBoldItalic != null)
                    GFX.FontBoldItalic.Dispose();

                if (GFX.FontUnderline != null)
                    GFX.FontUnderline.Dispose();

                if (GFX.FontBoldUnderline != null)
                    GFX.FontBoldUnderline.Dispose();

                if (GFX.FontItalicUnderline != null)
                    GFX.FontItalicUnderline.Dispose();

                if (GFX.FontBoldItalicUnderline != null)
                    GFX.FontBoldItalicUnderline.Dispose();


                //	string font="courier new";
                string font = Control.FontName;
                float fontsize = Control.FontSize;
                GFX.FontNormal = new GDIFont(font, fontsize, false, false, false, false)
                    ;
                GFX.FontBold = new GDIFont(font, fontsize, true, false, false, false);
                GFX.FontItalic = new GDIFont(font, fontsize, false, true, false, false);
                GFX.FontBoldItalic = new GDIFont(font, fontsize, true, true, false,
                                                 false);
                GFX.FontUnderline = new GDIFont(font, fontsize, false, false, true,
                                                false);
                GFX.FontBoldUnderline = new GDIFont(font, fontsize, true, false, true,
                                                    false);
                GFX.FontItalicUnderline = new GDIFont(font, fontsize, false, true, true,
                                                      false);
                GFX.FontBoldItalicUnderline = new GDIFont(font, fontsize, true, true,
                                                          true, false);

                this.InitIMEWindow();

   

            //			try
            //			{

            if (Control != null)
            {
                if (Control.IsHandleCreated)
                {
                    if (GFX.StringBuffer != null)
                        GFX.StringBuffer.Dispose();

                    if (GFX.SelectionBuffer != null)
                        GFX.SelectionBuffer.Dispose();

                    if (GFX.BackBuffer != null)
                        GFX.BackBuffer.Dispose();

                    GFX.StringBuffer = new GDISurface(1, 1, Control, true);
                    GFX.StringBuffer.Font = GFX.FontNormal;
                    int h = GFX.StringBuffer.MeasureTabbedString("ABC", 0).Height +
                        this.Control._CodeEditor.RowPadding;
                    GFX.BackBuffer = new GDISurface(Control.ClientWidth, h, Control, true)
                        ;
                    GFX.BackBuffer.Font = GFX.FontNormal;

                    GFX.SelectionBuffer = new GDISurface(Control.ClientWidth, h, Control,
                                                         true);
                    GFX.SelectionBuffer.Font = GFX.FontNormal;

                    Control.View.RowHeight = GFX.BackBuffer.MeasureTabbedString("ABC", 0)
                        .Height + this.Control._CodeEditor.RowPadding;
                    Control.View.CharWidth = GFX.BackBuffer.MeasureTabbedString(" ", 0)
                        .Width;

                }
                else
                {
                    //		System.Windows.Forms.MessageBox.Show ("no handle");

                }
            }
            //			}
            //			catch
            //			{
            //			}
        }

        private void InitIMEWindow()
        {
            if (this.Control.IMEWindow != null)
                this.Control.IMEWindow.SetFont(Control.FontName, Control.FontSize);
        }


        private Size MeasureRow(Row xtr, int Count, int OffsetX)
        {
            int width = 0;
            int taborig = -Control.View.FirstVisibleColumn * Control.View.CharWidth
                + Control.View.TextMargin;
            int xpos = Control.View.TextMargin - Control.View.ClientAreaStart;
            if (xtr.InQueue)
            {
                SetStringFont(false, false, false);
                int Padd = Math.Max(Count - xtr.Text.Length, 0);
                string PaddStr = new String(' ', Padd);
                string TotStr = xtr.Text + PaddStr;
                width = GFX.StringBuffer.MeasureTabbedString(TotStr.Substring(0, Count),
                                                             Control.PixelTabSize).Width;
            }
            else
            {
                int CharNo = 0;
                int TotWidth = 0;
                int CharPos = 0;
                foreach (Word w in xtr.FormattedWords)
                {
                    if (w.Type == WordType.xtWord && w.Style != null)
                        SetStringFont(w.Style.Bold, w.Style.Italic, w.Style.Underline);
                    else
                        SetStringFont(false, false, false);

                    if (w.Text.Length + CharNo >= Count || w ==
                        xtr.FormattedWords[xtr.FormattedWords.Count - 1])
                    {
                        CharPos = Count - CharNo;
                        int MaxChars = Math.Min(CharPos, w.Text.Length);
                        TotWidth += GFX.StringBuffer.DrawTabbedString(w.Text.Substring(0,
                                                                                       MaxChars), xpos + TotWidth, 0, taborig, Control.PixelTabSize)
                            .Width;
                        width = TotWidth;
                        break;
                    }
                    else
                    {
                        TotWidth += GFX.StringBuffer.DrawTabbedString(w.Text, xpos +
                            TotWidth, 0, taborig, Control.PixelTabSize).Width;
                        CharNo += w.Text.Length;
                    }
                }

                SetStringFont(false, false, false);
                int Padd = Math.Max(Count - xtr.Text.Length, 0);
                string PaddStr = new String(' ', Padd);
                width += GFX.StringBuffer.DrawTabbedString(PaddStr, xpos + TotWidth, 0,
                                                           taborig, Control.PixelTabSize).Width;


            }


            return new Size(width, 0);

            //	return GFX.BackBuffer.MeasureTabbedString (xtr.Text.Substring (0,Count),Control.PixelTabSize);
        }

        /// <summary>
        /// Implementation of the IPainter MeasureRow method.
        /// </summary>
        /// <param name="xtr">Row to measure</param>
        /// <param name="Count">Last char index</param>
        /// <returns>The size of the row in pixels</returns>
        public Size MeasureRow(Row xtr, int Count)
        {
            return MeasureRow(xtr, Count, 0);
        }

        /// <summary>
        /// Implementation of the IPainter RenderAll method.
        /// </summary>
        public void RenderAll()
        {
            //
            Control.View.RowHeight = GFX.BackBuffer.MeasureString("ABC").Height;
            Control.View.CharWidth = GFX.BackBuffer.MeasureString(" ").Width;


            Control.InitVars();

            Graphics g = Control.CreateGraphics();

            RenderAll(g);

            g.Dispose();
        }

        private void SetBrackets()
        {
            Segment CurrentSegment = null;
            this.BracketEnd = null;
            this.BracketStart = null;

            Word CurrWord = Control.Caret.CurrentWord;
            if (CurrWord != null)
            {
                CurrentSegment = CurrWord.Segment;
                if (CurrentSegment != null)
                {
                    if (CurrWord == CurrentSegment.StartWord || CurrWord ==
                        CurrentSegment.EndWord)
                    {
                        if (CurrentSegment.EndWord != null)
                        {
                            //	if(w!=null)
                            //	{
                            this.BracketEnd = CurrentSegment.EndWord;
                            this.BracketStart = CurrentSegment.StartWord;
                            //	}
                        }
                    }

                    //ROGER ST諨A H腞!!!

                    //try
                    //{
                    if (CurrWord == null || CurrWord.Pattern == null)
                        return;

                    if (CurrWord.Pattern.BracketType == BracketType.EndBracket)
                    {
                        Word w = this.Control.Document.GetStartBracketWord(CurrWord,
                                                                           CurrWord.Pattern.MatchingBracket, CurrWord.Segment);
                        this.BracketEnd = CurrWord;
                        this.BracketStart = w;
                    }
                    if (CurrWord.Pattern.BracketType == BracketType.StartBracket)
                    {
                        Word w = this.Control.Document.GetEndBracketWord(CurrWord,
                                                                         CurrWord.Pattern.MatchingBracket, CurrWord.Segment);

                        //	if(w!=null)
                        //	{
                        this.BracketEnd = w;
                        this.BracketStart = CurrWord;
                        //	}
                    }
                    //}
                    //catch
                    //{
                    //    //	System.Windows.Forms.MessageBox.Show (x.Message + "\n\n\n" + x.StackTrace);
                    //}
                }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -