All pastes #22601 Raw Edit

Miscellany

public text v1 · immutable
#22601 ·published 2005-09-10 21:51 UTC
rendered paste body
using System;

using MonoDevelop.Services;
using Pango;

namespace MonoDevelop.SourceEditor.Gui.Rulers
{
	public class LineNumberRuler : AbstractRuler
	{
		public LineNumberRuler () : base ()
		{
			
		}
		
		public override Gtk.TextView TextView
		{
			get
			{ return base.TextView; }
			set
			{
				base.TextView = value;
				TextView.MoveCursor += new Gtk.MoveCursorHandler (CursorMoved);
			}
		}
		
		void CursorMoved (object sender, Gtk.MoveCursorArgs e)
		{
			currentLine = TextView.Buffer.GetIterAtMark (TextView.Buffer.InsertMark).Line + 1; 
		}		
	
		public override int Width
		{
			get
			{
				if (layout == null || TextView == null)
					return 16;
				layout.SetMarkup (TextView.Buffer.LineCount.ToString ());
				int w, h;
				layout.GetPixelSize (out w, out h);
				layout.Width = w;
				return w + 4;
			}
		}
		
		Gdk.GC gc;
		Gdk.Color back = new Gdk.Color (0xff, 0xff, 0xff);
		Gdk.Color fore = new Gdk.Color (0, 0, 0);
		Gdk.Color highlight_color = new Gdk.Color (0xff, 0, 0);  
		Layout layout;
		
		public override void OnExpose (object sender, Gtk.ExposeEventArgs e)
		{
			if (!NeedRefresh)
				return;
			if (gc == null)
			{
				gc = new Gdk.GC (DrawingWindow);
				
													
				Gdk.Colormap colormap = Gdk.Colormap.System;
				colormap.AllocColor (ref back, true, true);
				colormap.AllocColor (ref fore, true, true);
				colormap.AllocColor (ref highlight_color, true, true);
				
				gc.Background = back;
				gc.Foreground = fore;
				DrawingWindow.Background = back; // for *visual* performance we should set the right background color
				
				layout = new Pango.Layout (TextView.PangoContext);
				layout.Alignment = Pango.Alignment.Center;
				layout.FontDescription = FontDescription.FromString ("Monospace 10 Light");
			}
			
			SetBackground (back);
			int lastY = -99;
			for (int i = FirstVisibleLine; i <= LastVisibleLine; i++)
			{
				Gdk.Rectangle rect = GetLineRectangle (i - 1);
				layout.SetText (i.ToString ());
				if (rect.Y != lastY)  // this handles the overlapping when collapsing lines.
				{
					int x = 0, y = rect.Y;
					switch (layout.Alignment)
					{
						case Pango.Alignment.Center:
							x = rect.X + rect.Width / 2;
							break;
						case Pango.Alignment.Left:
							x = rect.X;
							break;
						case Pango.Alignment.Right:
							x = rect.Width;
							break;
					}
					if (i==currentLine && HighlightLine)
					{
						layout.FontDescription = FontDescription.FromString ("Monospace 11 Heavy");
						gc.Foreground = highlight_color;
					}	
					DrawingWindow.DrawLayout (gc, x, y, layout);
					if (i==currentLine && HighlightLine)
					{	// re set the defaults
						layout.FontDescription = FontDescription.FromString ("Monospace 10 Light");
						gc.Foreground = fore;
					}	
					lastY = rect.Y;
				}
			}
		}

		Pango.Alignment TextAlignment
		{
			get
			{ return layout.Alignment; }
			set
			{ layout.Alignment = value; }
		}
		
		int currentLine = 0;
		bool highlightLine = true;
		bool HighlightLine
		{
			get
			{ return highlightLine; }
			set
			{ highlightLine = value; }
		}
	}
}