Code Coverage Statistics for Source File

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

Sequence Point Coverage
N/A
0 of 0
Branch Coverage
N/A
0 of 0
Lines
214
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: 2681 $</version>
6
// </file>
7
8
using System;
9
using System.Drawing;
10
using System.Windows.Forms;
11
12
namespace ICSharpCode.TextEditor.Gui.CompletionWindow
13
{
14
	/// <summary>
15
	/// Description of AbstractCompletionWindow.
16
	/// </summary>
17
	public abstract class AbstractCompletionWindow : System.Windows.Forms.Form
18
	{
19
		protected TextEditorControl control;
20
		protected Size              drawingSize;
21
		Rectangle workingScreen;
22
		Form parentForm;
23
		
24
		protected AbstractCompletionWindow(Form parentForm, TextEditorControl control)
25
		{
26
			workingScreen = Screen.GetWorkingArea(parentForm);
27
//			SetStyle(ControlStyles.Selectable, false);
28
			this.parentForm = parentForm;
29
			this.control  = control;
30
			
31
			SetLocation();
32
			StartPosition   = FormStartPosition.Manual;
33
			FormBorderStyle = FormBorderStyle.None;
34
			ShowInTaskbar   = false;
35
			MinimumSize     = new Size(1, 1);
36
			Size            = new Size(1, 1);
37
		}
38
		
39
		protected virtual void SetLocation()
40
		{
41
			TextArea textArea = control.ActiveTextAreaControl.TextArea;
42
			TextLocation caretPos  = textArea.Caret.Position;
43
			
44
			int xpos = textArea.TextView.GetDrawingXPos(caretPos.Y, caretPos.X);
45
			int rulerHeight = textArea.TextEditorProperties.ShowHorizontalRuler ? textArea.TextView.FontHeight : 0;
46
			Point pos = new Point(textArea.TextView.DrawingPosition.X + xpos,
47
			                      textArea.TextView.DrawingPosition.Y + (textArea.Document.GetVisibleLine(caretPos.Y)) * textArea.TextView.FontHeight 
48
			                      - textArea.TextView.TextArea.VirtualTop.Y + textArea.TextView.FontHeight + rulerHeight);
49
			
50
			Point location = control.ActiveTextAreaControl.PointToScreen(pos);
51
			
52
			// set bounds
53
			Rectangle bounds = new Rectangle(location, drawingSize);
54
			
55
			if (!workingScreen.Contains(bounds)) {
56
				if (bounds.Right > workingScreen.Right) {
57
					bounds.X = workingScreen.Right - bounds.Width;
58
				}
59
				if (bounds.Left < workingScreen.Left) {
60
					bounds.X = workingScreen.Left;
61
				}
62
				if (bounds.Top < workingScreen.Top) {
63
					bounds.Y = workingScreen.Top;
64
				}
65
				if (bounds.Bottom > workingScreen.Bottom) {
66
					bounds.Y = bounds.Y - bounds.Height - control.ActiveTextAreaControl.TextArea.TextView.FontHeight;
67
					if (bounds.Bottom > workingScreen.Bottom) {
68
						bounds.Y = workingScreen.Bottom - bounds.Height;
69
					}
70
				}
71
			}
72
			Bounds = bounds;
73
		}
74
		
75
		protected override CreateParams CreateParams {
76
			get {
77
				CreateParams p = base.CreateParams;
78
				AddShadowToWindow(p);
79
				return p;
80
			}
81
		}
82
		
83
		static int shadowStatus;
84
		
85
		/// <summary>
86
		/// Adds a shadow to the create params if it is supported by the operating system.
87
		/// </summary>
88
		public static void AddShadowToWindow(CreateParams createParams)
89
		{
90
			if (shadowStatus == 0) {
91
				// Test OS version
92
				shadowStatus = -1; // shadow not supported
93
				if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
94
					Version ver = Environment.OSVersion.Version;
95
					if (ver.Major > 5 || ver.Major == 5 && ver.Minor >= 1) {
96
						shadowStatus = 1;
97
					}
98
				}
99
			}
100
			if (shadowStatus == 1) {
101
				createParams.ClassStyle |= 0x00020000; // set CS_DROPSHADOW
102
			}
103
		}
104
		
105
		protected override bool ShowWithoutActivation {
106
			get {
107
				return true;
108
			}
109
		}
110
		
111
		protected void ShowCompletionWindow()
112
		{
113
			Owner = parentForm;
114
			Enabled = true;
115
			this.Show();
116
			
117
			control.Focus();
118
			
119
			if (parentForm != null) {
120
				parentForm.LocationChanged += new EventHandler(this.ParentFormLocationChanged);
121
			}
122
			
123
			control.ActiveTextAreaControl.VScrollBar.ValueChanged     += new EventHandler(ParentFormLocationChanged);
124
			control.ActiveTextAreaControl.HScrollBar.ValueChanged     += new EventHandler(ParentFormLocationChanged);
125
			control.ActiveTextAreaControl.TextArea.DoProcessDialogKey += new DialogKeyProcessor(ProcessTextAreaKey);
126
			control.ActiveTextAreaControl.Caret.PositionChanged       += new EventHandler(CaretOffsetChanged);
127
			control.ActiveTextAreaControl.TextArea.LostFocus          += new EventHandler(this.TextEditorLostFocus);
128
			control.Resize += new EventHandler(ParentFormLocationChanged);
129
			
130
			foreach (Control c in Controls) {
131
				c.MouseMove += ControlMouseMove;
132
			}
133
		}
134
		
135
		void ParentFormLocationChanged(object sender, EventArgs e)
136
		{
137
			SetLocation();
138
		}
139
		
140
		public virtual bool ProcessKeyEvent(char ch)
141
		{
142
			return false;
143
		}
144
		
145
		protected virtual bool ProcessTextAreaKey(Keys keyData)
146
		{
147
			if (!Visible) {
148
				return false;
149
			}
150
			switch (keyData) {
151
				case Keys.Escape:
152
					Close();
153
					return true;
154
			}
155
			return false;
156
		}
157
		
158
		protected virtual void CaretOffsetChanged(object sender, EventArgs e)
159
		{
160
		}
161
		
162
		protected void TextEditorLostFocus(object sender, EventArgs e)
163
		{
164
			if (!control.ActiveTextAreaControl.TextArea.Focused && !this.ContainsFocus) {
165
				Close();
166
			}
167
		}
168
		
169
		protected override void OnClosed(EventArgs e)
170
		{
171
			base.OnClosed(e);
172
			
173
			// take out the inserted methods
174
			parentForm.LocationChanged -= new EventHandler(ParentFormLocationChanged);
175
			
176
			foreach (Control c in Controls) {
177
				c.MouseMove -= ControlMouseMove;
178
			}
179
			
180
			if (control.ActiveTextAreaControl.VScrollBar != null) {
181
				control.ActiveTextAreaControl.VScrollBar.ValueChanged -= new EventHandler(ParentFormLocationChanged);
182
			}
183
			if (control.ActiveTextAreaControl.HScrollBar != null) {
184
				control.ActiveTextAreaControl.HScrollBar.ValueChanged -= new EventHandler(ParentFormLocationChanged);
185
			}
186
			
187
			control.ActiveTextAreaControl.TextArea.LostFocus          -= new EventHandler(this.TextEditorLostFocus);
188
			control.ActiveTextAreaControl.Caret.PositionChanged       -= new EventHandler(CaretOffsetChanged);
189
			control.ActiveTextAreaControl.TextArea.DoProcessDialogKey -= new DialogKeyProcessor(ProcessTextAreaKey);
190
			control.Resize -= new EventHandler(ParentFormLocationChanged);
191
			Dispose();
192
		}
193
		
194
		protected override void OnMouseMove(MouseEventArgs e)
195
		{
196
			base.OnMouseMove(e);
197
			ControlMouseMove(this, e);
198
		}
199
		
200
		/// <summary>
201
		/// Invoked when the mouse moves over this form or any child control.
202
		/// Shows the mouse cursor on the text area if it has been hidden.
203
		/// </summary>
204
		/// <remarks>
205
		/// Derived classes should attach this handler to the MouseMove event
206
		/// of all created controls which are not added to the Controls
207
		/// collection.
208
		/// </remarks>
209
		protected void ControlMouseMove(object sender, MouseEventArgs e)
210
		{
211
			control.ActiveTextAreaControl.TextArea.ShowHiddenCursor(false);
212
		}
213
	}
214
}