SourceCodeDebugControl.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace MoonSharp.Debugger
  10. {
  11. public partial class SourceCodeDebugControl : UserControl
  12. {
  13. private string[] m_SourceCode;
  14. private bool[] m_BreakPoints;
  15. int m_Line = 0;
  16. int m_XOffs = 0;
  17. int m_ActiveLine = -1;
  18. int m_CursorLine = 0;
  19. Brush m_CursorBrush = new SolidBrush(Color.FromArgb(64, Color.White));
  20. public int ActiveLine
  21. {
  22. get { return m_ActiveLine; }
  23. set
  24. {
  25. m_CursorLine = m_ActiveLine = value;
  26. ScrollToIncludeLine(m_ActiveLine);
  27. Invalidate();
  28. }
  29. }
  30. private void ScrollToIncludeLine(int line)
  31. {
  32. if (line == m_Line)
  33. return;
  34. if (line < m_Line)
  35. m_Line = line;
  36. if (line > m_Line + (this.GetHeight() / this.Font.Height))
  37. m_Line = line;
  38. }
  39. private int GetHeight()
  40. {
  41. return this.Height - horizScroll.Height;
  42. }
  43. public int CursorLine
  44. {
  45. get { return m_CursorLine; }
  46. set { m_CursorLine = value; Invalidate(); }
  47. }
  48. public SourceCodeDebugControl()
  49. {
  50. InitializeComponent();
  51. }
  52. public string[] SourceCode
  53. {
  54. get { return m_SourceCode; }
  55. set
  56. {
  57. if (value == null)
  58. {
  59. m_SourceCode = null;
  60. m_BreakPoints = null;
  61. }
  62. else
  63. {
  64. m_SourceCode = value.Select(s => s.Replace("\t", " ")).ToArray();
  65. m_BreakPoints = new bool[m_SourceCode.Length];
  66. }
  67. OnSourceCodeChanged();
  68. }
  69. }
  70. private void OnSourceCodeChanged()
  71. {
  72. m_Line = 0;
  73. if (vertScroll != null)
  74. {
  75. vertScroll.Value = 0;
  76. vertScroll.Maximum = (m_SourceCode != null) ? m_SourceCode.Length : 10;
  77. horizScroll.Maximum = 300;
  78. horizScroll.Value = 0;
  79. m_CursorLine = 0;
  80. m_ActiveLine = -1;
  81. }
  82. Invalidate();
  83. }
  84. protected override bool DoubleBuffered
  85. {
  86. get { return true; }
  87. set { base.DoubleBuffered = true; }
  88. }
  89. protected override void OnPaintBackground(PaintEventArgs e)
  90. {
  91. }
  92. protected override void OnPaint(PaintEventArgs e)
  93. {
  94. base.OnPaintBackground(e);
  95. if (m_SourceCode == null)
  96. return;
  97. int H = this.Font.Height;
  98. int W = (int)e.Graphics.MeasureString("X", this.Font).Width;
  99. int Y = 0;
  100. for (int i = m_Line; i < m_SourceCode.Length && H < this.GetHeight(); i++, Y += H)
  101. {
  102. if (m_ActiveLine == i)
  103. {
  104. e.Graphics.FillRectangle(Brushes.DarkCyan, 0, Y, Width, H);
  105. }
  106. if (m_BreakPoints[i])
  107. {
  108. e.Graphics.FillEllipse(Brushes.Red, 5, Y + 5, 10, 10);
  109. e.Graphics.DrawEllipse(Pens.DarkRed, 5, Y + 5, 10, 10);
  110. // e.Graphics.FillRectangle(Brushes.DarkRed, 0, Y, Width, H);
  111. }
  112. if (i == m_CursorLine)
  113. e.Graphics.FillRectangle(m_CursorBrush, -1, Y, Width + 1, H);
  114. if (m_ActiveLine == i)
  115. {
  116. e.Graphics.FillRectangle(Brushes.Aqua, 3, Y + 8, 14, 6);
  117. e.Graphics.DrawRectangle(Pens.DarkBlue, 3, Y + 8, 14, 6);
  118. }
  119. string str = m_SourceCode[i];
  120. if (m_XOffs != 0)
  121. {
  122. if (m_XOffs >= str.Length)
  123. continue;
  124. else
  125. str = str.Substring(m_XOffs);
  126. }
  127. e.Graphics.DrawString(str, this.Font, Brushes.Gainsboro, 20, Y);
  128. }
  129. }
  130. private void vertScroll_Scroll(object sender, ScrollEventArgs e)
  131. {
  132. m_Line = Math.Min(m_SourceCode.Length - 1, Math.Max(0, e.NewValue));
  133. Invalidate();
  134. }
  135. private void vertScroll_ValueChanged(object sender, EventArgs e)
  136. {
  137. m_Line = Math.Min(m_SourceCode.Length - 1, Math.Max(0, vertScroll.Value));
  138. Invalidate();
  139. }
  140. private void horizScroll_Scroll(object sender, ScrollEventArgs e)
  141. {
  142. m_XOffs = horizScroll.Value;
  143. Invalidate();
  144. }
  145. private void horizScroll_ValueChanged(object sender, EventArgs e)
  146. {
  147. m_XOffs = horizScroll.Value;
  148. Invalidate();
  149. }
  150. public void SetBreakpoint(int i, bool val)
  151. {
  152. m_BreakPoints[i] = val;
  153. }
  154. private void SourceCodeDebugControl_MouseClick(object sender, MouseEventArgs e)
  155. {
  156. int Y = e.Y / this.Font.Height;
  157. Y += m_Line;
  158. m_CursorLine = Y;
  159. Invalidate();
  160. }
  161. private void SourceCodeDebugControl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
  162. {
  163. if (e.KeyCode == Keys.Up)
  164. m_CursorLine = Math.Max(0, m_CursorLine - 1);
  165. if (e.KeyCode == Keys.Down)
  166. m_CursorLine = Math.Min(m_SourceCode.Length - 1, m_CursorLine + 1);
  167. if (e.KeyCode == Keys.F9)
  168. m_BreakPoints[m_CursorLine] = !m_BreakPoints[m_CursorLine];
  169. }
  170. }
  171. }