BioCore  5.0.0
A Windows .NET microscopy library and application for viewing, editing, and annotating images.
Loading...
Searching...
No Matches
CodeView.cs
Go to the documentation of this file.
1using System.ComponentModel;
2
3namespace BioCore
4{
5 public partial class CodeView : UserControl
6 {
7 private ScrollTextBox textBox = new ScrollTextBox();
8 private ScrollTextBox lineBox = new ScrollTextBox();
9 private int tabSize = 15;
10 /* Initializing the textbox and the linebox. */
11 public CodeView()
12 {
13 InitializeComponent();
14 textBox.Dock = DockStyle.Fill;
15 textBox.MouseWheel += new MouseEventHandler(Code_MouseWheel);
16 textBox.TextChanged += new EventHandler(textBox_TextChanged);
17 textBox.VScroll += new EventHandler(textBox_Scroll);
18 textBox.FontChanged += new EventHandler(textBox_FontChanged);
19 textBox.WordWrap = false;
20 textBox.AcceptsTab = true;
21 panel.Controls.Add(textBox);
22 lineBox.Dock = DockStyle.Fill;
23 lineBox.ScrollBars = RichTextBoxScrollBars.None;
24 panel2.Controls.Add(lineBox);
25 //MouseWheel += new MouseEventHandler(Code_MouseWheel);
26 textBox.SelectionTabs = new int[] { tabSize, tabSize * 2, tabSize * 3, tabSize * 4, tabSize * 5, tabSize * 6 };
28 }
29 /* A property of a RichTextBox. */
30 public RichTextBox TextBox
31 {
32 get
33 {
34 return textBox;
35 }
36 }
37
38 /* A property of the textbox. */
39 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
40 public bool WordWrap
41 {
42 get
43 {
44 return textBox.WordWrap;
45 }
46 set
47 {
48 textBox.WordWrap = value;
49 }
50 }
52 public void UpdateScroll()
53 {
54 lineBox.VerticalScrollPosition = textBox.VerticalScrollPosition;
55 }
56
61 public class ScrollTextBox : RichTextBox
62 {
63
64 /* Creating a new instance of the ScrollTextBox class. */
66 {
67 _components = new System.ComponentModel.Container();
68 // Calculate width of "W" to set as the small horizontal increment
69 OnFontChanged(null);
70 }
71
72 [System.ComponentModel.DefaultValue(0)
73 , System.ComponentModel.Category("Appearance")
74 , System.ComponentModel.Description
75 ("Gets or sets the vertical scroll bar's position"
76 )
77 ]
84 {
85 set { SetScroll(value, Win32.WM_VSCROLL, Win32.SB_VERT); }
86 get { return GetScroll(Win32.SB_VERT); }
87 }
88
89 [System.ComponentModel.DefaultValue(0)
90 , System.ComponentModel.Category("Appearance")
91 , System.ComponentModel.Description
92 ("Gets or sets the horizontal scroll bar's position"
93 )
94 ]
104 {
105 set { SetScroll(value, Win32.WM_HSCROLL, Win32.SB_HORZ); }
106 get { return GetScroll(Win32.SB_HORZ); }
107 }
108
109 [System.ComponentModel.Description
110 ("Fired when the scroll bar's vertical position changes"
111 )
112 , System.ComponentModel.Category("Property Changed")
113 ]
114 /* Creating an event handler for the ScrollChanged event. */
115 public event System.Windows.Forms.ScrollEventHandler ScrollChanged;
116
117 // Fire scroll event if the scroll-bars are moved
118 protected override void WndProc
119 (ref Message message
120 )
121 {
122 base.WndProc(ref message);
123 if (message.Msg == Win32.WM_VSCROLL
124 || message.Msg == Win32.WM_HSCROLL
125 || message.Msg == Win32.WM_MOUSEWHEEL
126 ) TryFireScrollEvent();
127 }
128
129 // Key-down includes navigation keys, but also typing and pasting can
130 // cause a scroll event
131 protected override void OnKeyDown
132 (System.Windows.Forms.KeyEventArgs e
133 )
134 {
135 base.OnKeyDown(e);
136 TryFireScrollEvent();
137 }
138
139 protected override void OnKeyUp
140 (System.Windows.Forms.KeyEventArgs e
141 )
142 {
143 base.OnKeyUp(e);
144 TryFireScrollEvent();
145 }
146
147 // Resizing can alter the word-wrap or fit the content causing the text
148 // to scroll
149 protected override void OnResize
150 (System.EventArgs e
151 )
152 {
153 base.OnResize(e);
154 TryFireScrollEvent();
155 }
156
157 // Clicking an empty space can move the carot to the beginning of the
158 // line causing a scroll event
159 protected override void OnMouseDown
160 (System.Windows.Forms.MouseEventArgs e
161 )
162 {
163 base.OnMouseDown(e);
164 TryFireScrollEvent();
165 }
166
167 protected override void OnMouseUp
168 (System.Windows.Forms.MouseEventArgs e
169 )
170 {
171 base.OnMouseUp(e);
172 TryFireScrollEvent();
173 }
174
175 // If the mouse button is down, a text selection is probably being done
176 // where dragging the selection can caues scroll events
177 protected override void OnMouseMove
178 (System.Windows.Forms.MouseEventArgs e
179 )
180 {
181 base.OnMouseMove(e);
182 if (e.Button != System.Windows.Forms.MouseButtons.None)
183 TryFireScrollEvent();
184 }
185
186 // Changing the size of the font can cause a scroll event. Also, when
187 // scrolling horizontally, the event will notify whether the scroll
188 // was a large or small change. For vertical, small increments are 1
189 // line, but for horizontal, it is several pixels. To guess what a
190 // small increment is, get the width of the W character and anything
191 // smaller than that will be represented as a small increment
192 protected override void OnFontChanged
193 (System.EventArgs e
194 )
195 {
196 base.OnFontChanged(e);
197 using (System.Drawing.Graphics graphics = this.CreateGraphics())
198 _fontWidth = (int)graphics.MeasureString("W", this.Font).Width;
199 TryFireScrollEvent();
200 }
201
206 protected override void Dispose
207 (bool disposing
208 )
209 {
210 if (disposing && (_components != null))
211 _components.Dispose();
212 base.Dispose(disposing);
213 }
214
221 private void SetScroll
222 (int value
223 , uint windowsMessage
224 , int scrollBarMessage
225 )
226 {
227 Win32.SetScrollPos
228 ((System.IntPtr)this.Handle
229 , scrollBarMessage
230 , value
231 , true
232 );
233 Win32.PostMessage
234 ((System.IntPtr)this.Handle
235 , windowsMessage
236 , 4 + 0x10000 * value
237 , 0
238 );
239 }
240
246 private int GetScroll
247 (int scrollBarMessage
248 )
249 {
250 return Win32.GetScrollPos
251 ((System.IntPtr)this.Handle
252 , scrollBarMessage
253 );
254 }
255
256 // Fire both horizontal and vertical scroll events seperately, one
257 // after the other. These first test if a scroll actually occurred
258 // and won't fire if there was no actual movement
259 private void TryFireScrollEvent()
260 { // Don't do anything if there is no event handler
261 if (ScrollChanged == null)
262 return;
263 TryFireHorizontalScrollEvent();
264 TryFireVerticalScrollEvent();
265 }
266
272 private void TryFireHorizontalScrollEvent()
273 {
274
275 // Don't do anything if there is no event handler
276 if (ScrollChanged == null)
277 return;
278
279 int lastScrollPosition = _lastHorizontalScrollPosition;
280 int scrollPosition = HorizontalScrollPosition;
281
282 // Don't do anything if there was no change in position
283 if (scrollPosition == lastScrollPosition)
284 return;
285
286 _lastHorizontalScrollPosition = scrollPosition;
287
289 (this
290 , new System.Windows.Forms.ScrollEventArgs
291 (scrollPosition < lastScrollPosition - _fontWidth
292 ? System.Windows.Forms.ScrollEventType.LargeDecrement
293 : scrollPosition > lastScrollPosition + _fontWidth
294 ? System.Windows.Forms.ScrollEventType.LargeIncrement
295 : scrollPosition < lastScrollPosition
296 ? System.Windows.Forms.ScrollEventType.SmallDecrement
297 : System.Windows.Forms.ScrollEventType.SmallIncrement
298 , lastScrollPosition
299 , scrollPosition
300 , System.Windows.Forms.ScrollOrientation.HorizontalScroll
301 )
302 );
303
304 }
305
309 private void TryFireVerticalScrollEvent()
310 {
311 // Don't do anything if there is no event handler
312 if (ScrollChanged == null)
313 return;
314 int lastScrollPosition = _lastVerticalScrollPosition;
315 int scrollPosition = VerticalScrollPosition;
316
317 // Don't do anything if there was no change in position
318 if (scrollPosition == lastScrollPosition)
319 return;
320
321 _lastVerticalScrollPosition = scrollPosition;
322
324 (this
325 , new System.Windows.Forms.ScrollEventArgs
326 (scrollPosition < lastScrollPosition - 1
327 ? System.Windows.Forms.ScrollEventType.LargeDecrement
328 : scrollPosition > lastScrollPosition + 1
329 ? System.Windows.Forms.ScrollEventType.LargeIncrement
330 : scrollPosition < lastScrollPosition
331 ? System.Windows.Forms.ScrollEventType.SmallDecrement
332 : System.Windows.Forms.ScrollEventType.SmallIncrement
333 , lastScrollPosition
334 , scrollPosition
335 , System.Windows.Forms.ScrollOrientation.VerticalScroll
336 )
337 );
338
339 }
340
341 private int _lastVerticalScrollPosition;
342 private int _lastHorizontalScrollPosition;
343 private int _fontWidth;
344 private System.ComponentModel.IContainer _components = null;
345
346 /* It's a class that allows you to set the scroll position of a control. */
347 private static class Win32
348 {
349
350 public const uint WM_HSCROLL = 0x114;
351 public const uint WM_VSCROLL = 0x115;
352 public const uint WM_MOUSEWHEEL = 0x20A;
353 public const int SB_VERT = 0x1;
354 public const int SB_HORZ = 0x0;
355
356 [System.Runtime.InteropServices.DllImport("user32.dll")]
357 public static extern int SetScrollPos
358 (System.IntPtr hWnd
359 , int nBar
360 , int nPos
361 , bool bRedraw
362 );
363 [System.Runtime.InteropServices.DllImport("user32.dll")]
364 public static extern int GetScrollPos
365 (System.IntPtr hWnd
366 , int nBar
367 );
368
369 [System.Runtime.InteropServices.DllImport
370 ("User32.Dll"
371 , EntryPoint = "PostMessageA")
372 ]
373 public static extern bool PostMessage
374 (System.IntPtr hWnd
375 , uint msg
376 , int wParam
377 , int lParam
378 );
379
380 }
381
382 }
387 private void Code_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
388 {
389 UpdateScroll();
390 }
395 private void textBox_Scroll(object sender, EventArgs e)
396 {
397 UpdateScroll();
398 }
399
404 private void textBox_TextChanged(object sender, EventArgs e)
405 {
406 lineBox.Text = "";
407 for (int i = 0; i < textBox.Lines.Length; i++)
408 {
409 lineBox.Text += (i + 1).ToString() + Environment.NewLine;
410 }
411 UpdateScroll();
412 }
413
418 private void textBox_FontChanged(object sender, EventArgs e)
419 {
420 lineBox.Font = textBox.Font;
421 }
422
427 private void CodeView_Resize(object sender, EventArgs e)
428 {
429 UpdateScroll();
430 }
431 }
432}
TextBox with support for getting and setting the vertical scroll bar position, as well as listening t...
Definition CodeView.cs:62
override void Dispose(bool disposing)
Definition CodeView.cs:207
override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
Definition CodeView.cs:132
override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
Definition CodeView.cs:178
override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
Definition CodeView.cs:160
override void OnFontChanged(System.EventArgs e)
Definition CodeView.cs:193
override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
Definition CodeView.cs:168
override void WndProc(ref Message message)
Definition CodeView.cs:119
override void OnResize(System.EventArgs e)
Definition CodeView.cs:150
override void OnKeyUp(System.Windows.Forms.KeyEventArgs e)
Definition CodeView.cs:140
System.Windows.Forms.ScrollEventHandler ScrollChanged
Definition CodeView.cs:115
RichTextBox TextBox
Definition CodeView.cs:31
void UpdateScroll()
The lineBox's vertical scroll position is set to the textBox's vertical scroll position.
Definition CodeView.cs:52