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

📄 linenumberview.cs

📁 c#精彩编程百例(源代码)
💻 CS
字号:
//  LineNumberView.cs
//  Copyright (C) 2000 Mike Krueger
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;

namespace SharpDevelop.Gui.Edit.Text {
	
	/// <summary>
	/// This class views the line numbers and folding markers.
	/// </summary>
	public class LineNumberView : UserControl
	{
		TextAreaPainter  textarea;
//		GraphicsBuffer buffer;
		
		public LineNumberView(TextAreaPainter textarea)
		{
//			buffer = GraphicsBuffer.CreateOptimal();
			this.textarea = textarea;
			Visible       = true;
			textarea.Buffer.FoldChanged += new EventHandler(RepaintMeEvent);
			textarea.Buffer.LineCountChanged += new EventHandler(LineCountChange);
			ResizeRedraw  = false;
		}
		
		void LineCountChange(object sender, EventArgs e)
		{
			int y1 = (int)(lastline * textarea.FontHeight);
			int y2 = (int)(textarea.Buffer.Length* textarea.FontHeight);
			Invalidate(new Rectangle(0, Math.Min(y1, y2), Width, Math.Max(y1, y2)));
			Update();
			RepaintMeEvent(sender, e);
//			Refresh();
		}
		
		void RepaintMeEvent(object sender, EventArgs e)
		{
			int ll = lastline;
			Invalidate(new Rectangle(Width - 13, 0,  13, Height));
			Update();
			lastline = ll;
		}
		
		int lastline = -1;
		
		void DrawFoldState(Graphics g, Rectangle rectangle, bool open)
		{
			
			g.FillRectangle(new SolidBrush(textarea.Buffer.Syntax.FoldMarker.BackgroundColor), rectangle);
			g.DrawRectangle(new Pen(new SolidBrush(textarea.Buffer.Syntax.FoldMarker.Color)), rectangle);
			g.DrawLine(new Pen(new SolidBrush(textarea.Buffer.Syntax.FoldLine.BackgroundColor)), rectangle.X + 2, rectangle.Y + rectangle.Height / 2, rectangle.X + rectangle.Width - 2, rectangle.Y + rectangle.Height / 2);
			if (!open) {
				g.DrawLine(new Pen(new SolidBrush(textarea.Buffer.Syntax.FoldLine.BackgroundColor)), rectangle.X + rectangle.Width / 2, rectangle.Y + 2, 
					                                             rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height - 2);
			}
		}
		
		protected override void OnClick(EventArgs e)
		{
			Point mousepos  = PointToClient(Control.MousePosition);
			int   clickline = (int)(mousepos.Y / textarea.FontHeight);
			int   realline  = textarea.Buffer.Foldings.GetPhysicalLine(clickline);
			if (textarea.Buffer[realline].foldon && realline + 1 < textarea.Buffer.Length) {
				if (textarea.Buffer[realline + 1].Visible) {
					textarea.Buffer.Foldings.FoldPos(new Point(0, realline));
					textarea.Caret.CheckCaretPos();
					Refresh();
					textarea.Refresh();
				} else {
					textarea.Buffer.Foldings.UnFold(realline + 1);
					textarea.Caret.CheckCaretPos();
					Refresh();
					textarea.Refresh();
				}
			}
			
		}
		
		protected override void OnPaint(PaintEventArgs pe)
		{
//			Graphics  drawTo     =  buffer.RequestBuffer(pe.Graphics, Width, Height);
			Graphics  drawTo     =  pe.Graphics;
			
			if (textarea.Options.UseAntiAliasFont) {
				drawTo.CompositingQuality = CompositingQuality.AssumeLinear;
				drawTo.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
				drawTo.SmoothingMode  = SmoothingMode.AntiAlias;
				drawTo.InterpolationMode = InterpolationMode.High;
			}
			
			Color color = Enabled ? textarea.Buffer.Syntax.LineNumberColor.BackgroundColor : SystemColors.InactiveBorder;
			drawTo.FillRectangle(new SolidBrush(color), pe.ClipRectangle);
			
			if (Enabled) {
				int realline  = textarea.Buffer.Foldings.GetPhysicalLine((int)(pe.ClipRectangle.Y / textarea.FontHeight));
				for (int line = (int)(pe.ClipRectangle.Y / textarea.FontHeight); textarea.FontHeight * (line - 1) <= pe.ClipRectangle.Y + pe.ClipRectangle.Height; ++line) {
					if (realline >= textarea.Buffer.Length) 
						break;
					if (!textarea.Buffer[realline].Visible) {
						if (textarea.Buffer.Foldings.foldings[realline] != null) {
							realline += (int)textarea.Buffer.Foldings.foldings[realline];
						} else {
							while (!textarea.Buffer[realline].Visible)
								++realline;
						}
					}
					
//					if (textarea.FontHeight * (line + 1) >= pe.ClipRectangle.Y) 
					{
						int number = line + 1;
						string text = "";
						bool drawfoldstate = false;
						
						if (textarea.Options.ShowLineNumbers && number <= textarea.Buffer.Length ) {
							lastline = number;
							text = String.Format("{0} ", realline + 1); 
							
							while (text.Length < 4) 
								text = " " + text;
							drawTo.DrawString(text,
									textarea.Options.DefaultFont,
	//						         textarea.Buffer.Syntax.LineNumberColor.Font,
							         new SolidBrush(textarea.Buffer.Syntax.LineNumberColor.Color), 
							         new PointF(0, textarea.FontHeight * line)
							         );
						}
						
						if (realline + 1 < textarea.Buffer.Length && textarea.Buffer[realline].foldon) {
							drawfoldstate = true;
							DrawFoldState(drawTo,new Rectangle(Width - 9, 
						                                          (int)(textarea.FontHeight * line + (textarea.FontHeight - 8) / 2), 
						                                          8, 
						                                          8),
						                         textarea.Buffer[realline + 1].Visible);
							drawTo.DrawLine(new Pen(new SolidBrush(textarea.Buffer.Syntax.FoldLine.Color)), 
								       Width - 12, 
								       (int)(textarea.FontHeight * line + textarea.FontHeight / 2), 
								       Width - 9, 
								       (int)(textarea.FontHeight * line + textarea.FontHeight / 2));
						}
						
						/*
						if (textarea.Buffer[realline].FoldLevel > 0)
						{
							bool last  = realline + 1 >= textarea.Buffer.Length || textarea.Buffer[realline + 1].FoldLevel == 0; 
							bool first = realline <= 0 || textarea.Buffer[realline - 1].FoldLevel == 0;
							
							if (first) {
							drawTo.DrawLine(new Pen(new SolidBrush(textarea.Buffer.Syntax.FoldLine.Color)), 
								       Width - 12, 
								       (int)(textarea.FontHeight * line), 
								       Width - 12, 
								       (int)(textarea.FontHeight * line + textarea.FontHeight / 2));
							} else
							if (last) {
								drawTo.DrawLine(new Pen(new SolidBrush(textarea.Buffer.Syntax.FoldLine.Color)), 
									       Width - 12, 
									       (int)(textarea.FontHeight * line + textarea.FontHeight / 2), 
									       Width - 12, 
									       (int)(textarea.FontHeight * line + textarea.FontHeight + 1));
							} else
							drawTo.DrawLine(new Pen(new SolidBrush(textarea.Buffer.Syntax.FoldLine.Color)), 
								       Width - 12, 
								       (int)(textarea.FontHeight * line), 
								       Width - 12, 
								       (int)(textarea.FontHeight * line + textarea.FontHeight + 1));
						}*/
						
						if (realline < textarea.Buffer.Length && textarea.Buffer[realline].foldoff) {
							if (!drawfoldstate)
							drawTo.DrawLine(new Pen(new SolidBrush(textarea.Buffer.Syntax.FoldLine.Color)), 
								       Width - 12, 
								       (int)(textarea.FontHeight * line + textarea.FontHeight / 2), 
								       Width - 3, 
								       (int)(textarea.FontHeight * line + textarea.FontHeight / 2));
							
						}
					}
					++realline;
				}
			}
//			buffer.PaintBuffer(pe.Graphics, 0, 0);
		}
		
		protected override void OnPaintBackground(PaintEventArgs pe)
		{}
	}
}

⌨️ 快捷键说明

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