Code Coverage Statistics for Source File

c:\Tools\SD3\src\Libraries\ICSharpCode.TextEditor\Project\Src\Gui\GutterMargin.cs

Sequence Point Coverage
N/A
0 of 0
Branch Coverage
N/A
0 of 0
Lines
158
Highlight: Uncovered Code Covered Code
L V Source
1
// <file>
2
//     <copyright see="prj:///doc/copyright.txt"/>
3
//     <license see="prj:///doc/license.txt"/>
4
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
5
//     <version>$Revision: 2680 $</version>
6
// </file>
7
8
using System;
9
using System.Drawing;
10
using System.IO;
11
using System.Reflection;
12
using System.Windows.Forms;
13
14
using ICSharpCode.TextEditor.Document;
15
16
namespace ICSharpCode.TextEditor
17
{
18
	/// <summary>
19
	/// This class views the line numbers and folding markers.
20
	/// </summary>
21
	public class GutterMargin : AbstractMargin, IDisposable
22
	{
23
		StringFormat numberStringFormat = (StringFormat)StringFormat.GenericTypographic.Clone();
24
		
25
		public static Cursor RightLeftCursor;
26
		
27
		static GutterMargin()
28
		{
29
			Stream cursorStream = Assembly.GetCallingAssembly().GetManifestResourceStream("ICSharpCode.TextEditor.Resources.RightArrow.cur");
30
			RightLeftCursor = new Cursor(cursorStream);
31
			cursorStream.Close();
32
		}
33
		
34
		public void Dispose()
35
		{
36
			numberStringFormat.Dispose();
37
		}
38
		
39
		public override Cursor Cursor {
40
			get {
41
				return RightLeftCursor;
42
			}
43
		}
44
		
45
		public override Size Size {
46
			get {
47
				return new Size((int)(textArea.TextView.WideSpaceWidth
48
				                      * Math.Max(3, (int)Math.Log10(textArea.Document.TotalNumberOfLines) + 1)),
49
				                -1);
50
			}
51
		}
52
		
53
		public override bool IsVisible {
54
			get {
55
				return textArea.TextEditorProperties.ShowLineNumbers;
56
			}
57
		}
58
		
59
		public GutterMargin(TextArea textArea) : base(textArea)
60
		{
61
			numberStringFormat.LineAlignment = StringAlignment.Far;
62
			numberStringFormat.FormatFlags   = StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.FitBlackBox |
63
				StringFormatFlags.NoWrap | StringFormatFlags.NoClip;
64
		}
65
		
66
		public override void Paint(Graphics g, Rectangle rect)
67
		{
68
			if (rect.Width <= 0 || rect.Height <= 0) {
69
				return;
70
			}
71
			HighlightColor lineNumberPainterColor = textArea.Document.HighlightingStrategy.GetColorFor("LineNumbers");
72
			int fontHeight = textArea.TextView.FontHeight;
73
			Brush fillBrush = textArea.Enabled ? BrushRegistry.GetBrush(lineNumberPainterColor.BackgroundColor) : SystemBrushes.InactiveBorder;
74
			Brush drawBrush = BrushRegistry.GetBrush(lineNumberPainterColor.Color);
75
			for (int y = 0; y < (DrawingPosition.Height + textArea.TextView.VisibleLineDrawingRemainder) / fontHeight + 1; ++y) {
76
				int ypos = drawingPosition.Y + fontHeight * y  - textArea.TextView.VisibleLineDrawingRemainder;
77
				Rectangle backgroundRectangle = new Rectangle(drawingPosition.X, ypos, drawingPosition.Width, fontHeight);
78
				if (rect.IntersectsWith(backgroundRectangle)) {
79
					g.FillRectangle(fillBrush, backgroundRectangle);
80
					int curLine = textArea.Document.GetFirstLogicalLine(textArea.Document.GetVisibleLine(textArea.TextView.FirstVisibleLine) + y);
81
					
82
					if (curLine < textArea.Document.TotalNumberOfLines) {
83
						g.DrawString((curLine + 1).ToString(),
84
						             lineNumberPainterColor.GetFont(TextEditorProperties.FontContainer),
85
						             drawBrush,
86
						             backgroundRectangle,
87
						             numberStringFormat);
88
					}
89
				}
90
			}
91
		}
92
93
		public override void HandleMouseDown(Point mousepos, MouseButtons mouseButtons)
94
		{
95
			TextLocation selectionStartPos;
96
97
			textArea.SelectionManager.selectFrom.where = WhereFrom.Gutter;
98
			int realline = textArea.TextView.GetLogicalLine(mousepos.Y);
99
			if (realline >= 0 && realline < textArea.Document.TotalNumberOfLines) {
100
				// shift-select
101
				if((Control.ModifierKeys & Keys.Shift) != 0) {
102
					if(!textArea.SelectionManager.HasSomethingSelected && realline != textArea.Caret.Position.Y) {
103
						if (realline >= textArea.Caret.Position.Y)
104
						{ // at or below starting selection, place the cursor on the next line
105
							// nothing is selected so make a new selection from cursor
106
							selectionStartPos = textArea.Caret.Position;
107
							// whole line selection - start of line to start of next line
108
							if (realline < textArea.Document.TotalNumberOfLines - 1)
109
							{
110
								textArea.SelectionManager.SetSelection(new DefaultSelection(textArea.Document, selectionStartPos, new TextLocation(0, realline + 1)));
111
								textArea.Caret.Position = new TextLocation(0, realline + 1);
112
							}
113
							else
114
							{
115
								textArea.SelectionManager.SetSelection(new DefaultSelection(textArea.Document, selectionStartPos, new TextLocation(textArea.Document.GetLineSegment(realline).Length + 1, realline)));
116
								textArea.Caret.Position = new TextLocation(textArea.Document.GetLineSegment(realline).Length + 1, realline);
117
							}
118
						}
119
						else
120
						{ // prior lines to starting selection, place the cursor on the same line as the new selection
121
							// nothing is selected so make a new selection from cursor
122
							selectionStartPos = textArea.Caret.Position;
123
							// whole line selection - start of line to start of next line
124
							textArea.SelectionManager.SetSelection(new DefaultSelection(textArea.Document, selectionStartPos, new TextLocation(selectionStartPos.X, selectionStartPos.Y)));
125
							textArea.SelectionManager.ExtendSelection(new TextLocation(selectionStartPos.X, selectionStartPos.Y), new TextLocation(0, realline));
126
							textArea.Caret.Position = new TextLocation(0, realline);
127
						}
128
					}
129
					else
130
					{
131
						// let MouseMove handle a shift-click in a gutter
132
						MouseEventArgs e = new MouseEventArgs(mouseButtons, 1, mousepos.X, mousepos.Y, 0);
133
						textArea.RaiseMouseMove(e);
134
					}
135
				} else { // this is a new selection with no shift-key
136
					// sync the textareamousehandler mouse location
137
					// (fixes problem with clicking out into a menu then back to the gutter whilst
138
					// there is a selection)
139
					textArea.mousepos = mousepos;
140
141
					selectionStartPos = new TextLocation(0, realline);
142
					textArea.SelectionManager.ClearSelection();
143
					// whole line selection - start of line to start of next line
144
					if (realline < textArea.Document.TotalNumberOfLines - 1)
145
					{
146
						textArea.SelectionManager.SetSelection(new DefaultSelection(textArea.Document, selectionStartPos, new TextLocation(selectionStartPos.X, selectionStartPos.Y + 1)));
147
						textArea.Caret.Position = new TextLocation(selectionStartPos.X, selectionStartPos.Y + 1);
148
					}
149
					else
150
					{
151
						textArea.SelectionManager.SetSelection(new DefaultSelection(textArea.Document, new TextLocation(0, realline), new TextLocation(textArea.Document.GetLineSegment(realline).Length + 1, selectionStartPos.Y)));
152
						textArea.Caret.Position = new TextLocation(textArea.Document.GetLineSegment(realline).Length + 1, selectionStartPos.Y);
153
					}
154
				}
155
			}
156
		}
157
	}
158
}