PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/UiException/Controls/CodeBox.cs

#
C# | 227 lines | 155 code | 46 blank | 26 comment | 8 complexity | d972a505ed61b6f2fbd1dda0f3cdc99f MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // This is free software licensed under the NUnit license. You may
  3. // obtain a copy of the license at http://nunit.org
  4. // ****************************************************************
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Drawing;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. using NUnit.UiException.CodeFormatters;
  12. using System.Diagnostics;
  13. /// This control could have been replaced by a standard RichTextBox control, but
  14. /// it turned out that RichTextBox:
  15. /// - was hard to configure
  16. /// - was hard to set the viewport
  17. /// - doesn't use double buffer optimization
  18. /// - scrolls text one line at a time without be configurable.
  19. ///
  20. /// CodeBox has been written to address these specific issues in order to display
  21. /// C# source code where exceptions occured.
  22. namespace NUnit.UiException.Controls
  23. {
  24. /// <summary>
  25. /// A control that implements ICodeView.
  26. /// </summary>
  27. public class CodeBox : UserControl, ICodeView
  28. {
  29. protected CodeRenderingContext _workingContext;
  30. protected FormattedCode _formattedCode;
  31. private IFormatterCatalog _formatter;
  32. private ICodeRenderer _renderer;
  33. private string _language;
  34. private bool _showCurrentLine;
  35. private int _currentLine;
  36. public CodeBox() :
  37. this(new GeneralCodeFormatter(), new DefaultCodeRenderer()) { }
  38. #region ICodeView Members
  39. public override string Text
  40. {
  41. get { return (_formattedCode.Text); }
  42. set
  43. {
  44. if (value == null)
  45. value = "";
  46. SizeF docSize;
  47. _formattedCode = _formatter.Format(value, _language);
  48. docSize = _renderer.GetDocumentSize(
  49. _formattedCode, _workingContext.Graphics, _workingContext.Font);
  50. AutoScrollMinSize = new Size((int)docSize.Width, (int)docSize.Height);
  51. Invalidate();
  52. return;
  53. }
  54. }
  55. public string Language
  56. {
  57. get { return (_language); }
  58. set
  59. {
  60. if (value == null)
  61. value = "";
  62. if (_language == value)
  63. return;
  64. _language = value;
  65. Text = Text;
  66. }
  67. }
  68. public int CurrentLine
  69. {
  70. get { return (_currentLine); }
  71. set
  72. {
  73. float y = _renderer.LineIndexToYCoordinate(value,
  74. _workingContext.Graphics, _workingContext.Font);
  75. y -= Height / 2;
  76. _currentLine = value;
  77. AutoScrollPosition = new Point(0, (int)y);
  78. Invalidate();
  79. }
  80. }
  81. public IFormatterCatalog Formatter
  82. {
  83. get { return (_formatter); }
  84. }
  85. #endregion
  86. /// <summary>
  87. /// Gets or sets a value telling whether or not displaying a special
  88. /// feature for the current line at drawing time.
  89. /// </summary>
  90. public bool ShowCurrentLine
  91. {
  92. get { return (_showCurrentLine); }
  93. set { _showCurrentLine = value; }
  94. }
  95. /// <summary>
  96. /// If ShowCurrentLine is set, this set the current line's background color.
  97. /// </summary>
  98. public Color CurrentLineBackColor
  99. {
  100. get { return (_workingContext.CurrentLineBackColor); }
  101. set {
  102. _workingContext.CurrentLineBackColor = value;
  103. Invalidate();
  104. }
  105. }
  106. /// <summary>
  107. /// If ShowCurrentLine is set, this set current line's foreground color.
  108. /// </summary>
  109. public Color CurrentLineForeColor
  110. {
  111. get { return (_workingContext.CurrentLineForeColor); }
  112. set {
  113. _workingContext.CurrentLineForeColor = value;
  114. Invalidate();
  115. }
  116. }
  117. protected CodeBox(IFormatterCatalog formatter, ICodeRenderer renderer)
  118. {
  119. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  120. SetStyle(ControlStyles.UserPaint, true);
  121. DoubleBuffered = true;
  122. _formatter = formatter;
  123. _formattedCode = FormattedCode.Empty;
  124. _renderer = renderer;
  125. _currentLine = -1;
  126. _showCurrentLine = false;
  127. _language = "";
  128. this.Font = new Font(FontFamily.GenericMonospace, 8);
  129. this.BackColor = Color.White;
  130. createGraphics();
  131. AutoScroll = true;
  132. return;
  133. }
  134. protected override void OnMouseHover(EventArgs e)
  135. {
  136. base.OnMouseHover(e);
  137. Focus();
  138. }
  139. protected override void OnPaint(PaintEventArgs e)
  140. {
  141. Graphics backup;
  142. base.OnPaint(e);
  143. backup = _workingContext.Graphics;
  144. _workingContext.Graphics = e.Graphics;
  145. _workingContext.CurrentLine = (_showCurrentLine ? _currentLine : -1);
  146. _renderer.DrawToGraphics(_formattedCode, _workingContext,
  147. new Rectangle(-AutoScrollPosition.X, -AutoScrollPosition.Y, Width, Height));
  148. _workingContext.Graphics = backup;
  149. return;
  150. }
  151. protected override void OnFontChanged(EventArgs e)
  152. {
  153. base.OnFontChanged(e);
  154. if (_workingContext != null)
  155. {
  156. _workingContext.Font = Font;
  157. Text = Text;
  158. }
  159. return;
  160. }
  161. private void createGraphics()
  162. {
  163. Graphics gCurrent = CreateGraphics();
  164. Image img = new Bitmap(10, 10, gCurrent);
  165. Graphics gImg = Graphics.FromImage(img);
  166. gCurrent.Dispose();
  167. _workingContext = new CodeRenderingContext();
  168. _workingContext.Graphics = gImg;
  169. _workingContext.Font = Font;
  170. _workingContext.CurrentLine = -1;
  171. _workingContext.BackgroundColor = Color.White;
  172. _workingContext.CurrentLineBackColor = Color.Red;
  173. _workingContext.CurrentLineForeColor = Color.White;
  174. _workingContext.CodeColor = Color.Black;
  175. _workingContext.CommentColor = Color.Green;
  176. _workingContext.KeywordColor = Color.Blue;
  177. _workingContext.StringColor = Color.Red;
  178. return;
  179. }
  180. }
  181. }