PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/UiException/Controls/DefaultErrorListRenderer.cs

#
C# | 337 lines | 253 code | 69 blank | 15 comment | 24 complexity | a41ae21a17bd022e8aba7ddfbda29c06 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.Text;
  8. using System.Drawing;
  9. using NUnit.UiException.Properties;
  10. using System.Diagnostics;
  11. namespace NUnit.UiException.Controls
  12. {
  13. /// <summary>
  14. /// Implements IErrorListRenderer.
  15. /// </summary>
  16. public class DefaultErrorListRenderer :
  17. IErrorListRenderer
  18. {
  19. //private static readonly int ITEM_HEIGHT = 54;
  20. private static readonly int TEXT_MARGIN_X = 16;
  21. private Font _font;
  22. private Font _fontUnderlined;
  23. private int _itemHeight;
  24. private Brush _brushBlue;
  25. private Brush _brushGray;
  26. private float _offsetLine;
  27. private Rectangle _rectListShadow;
  28. private Rectangle _rectListBackground;
  29. private Rectangle _rectItemGray;
  30. private Rectangle _rectItemWhite;
  31. private Rectangle _rectSelectionMiddle;
  32. private Rectangle _rectIconDll;
  33. private Rectangle _rectIconCSharp;
  34. private Rectangle _rectIconArrow;
  35. // private Rectangle _rectShadow;
  36. private PaintData _paintData;
  37. public DefaultErrorListRenderer()
  38. {
  39. this.Font = new Font(FontFamily.GenericSansSerif, 8.25f);
  40. //_fontUnderlined = new Font(_font, FontStyle.Underline);
  41. //_itemHeight = _font.Height * 4 + 6;
  42. _brushBlue = new SolidBrush(Color.FromArgb(0, 43, 114));
  43. _brushGray = new SolidBrush(Color.FromArgb(64, 64, 64));
  44. _rectListShadow = new Rectangle(0, 0, 48, 9);
  45. _rectListBackground = new Rectangle(0, 10, 48, 48);
  46. _rectItemGray = new Rectangle(71, 0, 9, 54);
  47. _rectItemWhite = new Rectangle(60, 0, 9, 54);
  48. _rectSelectionMiddle = new Rectangle(49, 0, 9, 54);
  49. _rectIconDll = new Rectangle(1, 59, 16, 15);
  50. _rectIconCSharp = new Rectangle(18, 59, 14, 15);
  51. _rectIconArrow = new Rectangle(35, 60, 9, 5);
  52. // _rectShadow = new Rectangle(49, 60, 4, 8);
  53. _paintData = new PaintData();
  54. return;
  55. }
  56. public Font Font
  57. {
  58. get { return (_font); }
  59. set
  60. {
  61. _fontUnderlined = _font = value;
  62. if (_font.FontFamily.IsStyleAvailable(FontStyle.Underline))
  63. _fontUnderlined = new Font(_font, FontStyle.Underline);
  64. _itemHeight = _font.Height * 4 + 6;
  65. }
  66. }
  67. #region IErrorListRenderer Membres
  68. public void DrawToGraphics(ErrorItemCollection items,
  69. ErrorItem selected, Graphics g, Rectangle viewport)
  70. {
  71. SizeF sizeLineSource;
  72. int last;
  73. int i;
  74. UiExceptionHelper.CheckNotNull(items, "items");
  75. UiExceptionHelper.CheckNotNull(g, "g");
  76. if (!_paintData.Equals(items, selected, viewport))
  77. {
  78. _paintData.Dispose();
  79. _paintData = new PaintData(items, selected, viewport, g);
  80. PaintBackground(Resources.ImageErrorList, _paintData.WorkingGraphics,
  81. _rectListBackground, viewport);
  82. sizeLineSource = g.MeasureString("Line 9999", _font);
  83. _offsetLine = viewport.Width - sizeLineSource.Width;
  84. last = LastIndexVisible(items.Count, viewport);
  85. for (i = FirstIndexVisible(items.Count, viewport); i <= last; ++i)
  86. DrawItem(items[i], i, selected == items[i], i == items.Count - 1, false,
  87. _paintData.WorkingGraphics, viewport);
  88. //_paintData.WorkingGraphics.DrawImage(Resources.ErrorList,
  89. //new Rectangle(0, 0, viewport.Width, _rectShadow.Height),
  90. //_rectShadow, GraphicsUnit.Pixel);
  91. }
  92. _paintData.PaintTo(g);
  93. return;
  94. }
  95. public void DrawItem(ErrorItem item, int index, bool hovered, bool selected, Graphics g, Rectangle viewport)
  96. {
  97. DrawItem(item, index, selected, false, hovered, g, viewport);
  98. }
  99. public Size GetDocumentSize(ErrorItemCollection items, Graphics g)
  100. {
  101. SizeF current;
  102. float w;
  103. _paintData = new PaintData();
  104. if (items.Count == 0)
  105. return (new Size());
  106. w = 0;
  107. foreach (ErrorItem item in items)
  108. {
  109. current = MeasureItem(g, item);
  110. w = Math.Max(w, current.Width);
  111. }
  112. return (new Size((int)w, items.Count * _itemHeight));
  113. }
  114. public ErrorItem ItemAt(ErrorItemCollection items, Graphics g, Point point)
  115. {
  116. int idx = point.Y / _itemHeight;
  117. if (items == null || point.Y < 0 || idx >= items.Count)
  118. return (null);
  119. return (items[idx]);
  120. }
  121. #endregion
  122. protected bool IsDirty(ErrorItemCollection items, ErrorItem selection, Rectangle viewport)
  123. {
  124. return (!_paintData.Equals(items, selection, viewport));
  125. }
  126. protected SizeF MeasureItem(Graphics g, ErrorItem item)
  127. {
  128. SizeF sizeMethod;
  129. SizeF sizeClass;
  130. SizeF sizeFile;
  131. UiExceptionHelper.CheckNotNull(g, "g");
  132. UiExceptionHelper.CheckNotNull(item, "item");
  133. sizeClass = g.MeasureString(item.ClassName, _font);
  134. sizeMethod = g.MeasureString(item.MethodName, _font);
  135. sizeFile = g.MeasureString(item.FileName, _font);
  136. return (new SizeF(
  137. Math.Max(sizeClass.Width, Math.Max(sizeMethod.Width, sizeFile.Width)) + TEXT_MARGIN_X,
  138. _itemHeight));
  139. }
  140. private void DrawItem(ErrorItem item, int index, bool selected, bool last, bool hover, Graphics g, Rectangle viewport)
  141. {
  142. Rectangle src;
  143. Font font;
  144. int x = -viewport.X;
  145. int y = _itemHeight * index - viewport.Y;
  146. src = (index % 2 == 0) ? _rectItemWhite : _rectItemGray ;
  147. font = (hover == true) ? _fontUnderlined : _font;
  148. g.DrawImage(Resources.ImageErrorList,
  149. new Rectangle(0, y, viewport.Width, _itemHeight), src,
  150. GraphicsUnit.Pixel);
  151. if (selected)
  152. {
  153. g.DrawImage(Resources.ImageErrorList,
  154. new Rectangle(0, y + 1, viewport.Width, _itemHeight ),
  155. _rectSelectionMiddle, GraphicsUnit.Pixel);
  156. }
  157. if (item.HasSourceAttachment)
  158. {
  159. g.DrawImage(Resources.ImageErrorList, new Rectangle(x + 1, y + 2 + font.Height, 14, 15),
  160. _rectIconCSharp, GraphicsUnit.Pixel);
  161. g.DrawImage(Resources.ImageErrorList,
  162. new Rectangle(TEXT_MARGIN_X - 3 + x, y + 5 + 2 * font.Height, 9, 5),
  163. _rectIconArrow, GraphicsUnit.Pixel);
  164. g.DrawString(String.Format("Line {0}", item.LineNumber),
  165. font, _brushGray, _offsetLine, y + 2);
  166. g.DrawString(item.ClassName, font, _brushBlue, x + TEXT_MARGIN_X, y + 2 + font.Height);
  167. g.DrawString(item.BaseMethodName + "()", font, _brushBlue,
  168. x + TEXT_MARGIN_X + 5, y + 2 + 2 * font.Height);
  169. g.DrawString(item.FileName, font, _brushGray,
  170. x + TEXT_MARGIN_X, y + 2 + 3 * _font.Height);
  171. }
  172. else
  173. {
  174. g.DrawImage(Resources.ImageErrorList, new Rectangle(x + 1, y + 2 + font.Height, 16, 15),
  175. _rectIconDll, GraphicsUnit.Pixel);
  176. g.DrawString("N/A", font, _brushGray, _offsetLine, y + 2);
  177. g.DrawString(item.ClassName, font, _brushGray,
  178. x + TEXT_MARGIN_X, y + 2 + font.Height);
  179. g.DrawString(item.BaseMethodName + "()", font, _brushGray,
  180. x + TEXT_MARGIN_X, y + 2 + 2 * font.Height);
  181. }
  182. if (!last)
  183. return;
  184. PaintTile(Resources.ImageErrorList, g, _rectListShadow,
  185. new Rectangle(0, y + _itemHeight, viewport.Width, 9));
  186. return;
  187. }
  188. private static void PaintBackground(Image img, Graphics g, Rectangle bkg, Rectangle viewport)
  189. {
  190. Rectangle destTile;
  191. int x;
  192. int y;
  193. int startY;
  194. int startX;
  195. startY = -viewport.Y % viewport.Height;
  196. startX = -viewport.X % viewport.Width;
  197. for (y = startY; y < viewport.Height; y += bkg.Height)
  198. for (x = startX; x < viewport.Width; x += bkg.Width)
  199. {
  200. destTile = new Rectangle(x, y, bkg.Width, bkg.Height);
  201. g.DrawImage(img, destTile, bkg, GraphicsUnit.Pixel);
  202. }
  203. return;
  204. }
  205. private static void PaintTile(Image tile, Graphics g, Rectangle src, Rectangle dst)
  206. {
  207. Rectangle destTile;
  208. int x;
  209. int y;
  210. for (y = dst.Top; y < dst.Bottom; y += src.Height)
  211. for (x = dst.Left; x < dst.Right; x += src.Width)
  212. {
  213. destTile = new Rectangle(x, y, src.Width, src.Height);
  214. g.DrawImage(tile, destTile, src, GraphicsUnit.Pixel);
  215. }
  216. return;
  217. }
  218. private int FirstIndexVisible(int count, Rectangle viewport)
  219. {
  220. return (Math.Max(0, viewport.Y / _itemHeight));
  221. }
  222. private int LastIndexVisible(int count, Rectangle viewport)
  223. {
  224. return (Math.Min(count - 1,
  225. FirstIndexVisible(count, viewport) + 1 + viewport.Height / _itemHeight));
  226. }
  227. class PaintData
  228. {
  229. public Graphics WorkingGraphics;
  230. private ErrorItem _firstItem;
  231. private ErrorItem selection;
  232. private Rectangle viewport;
  233. private Image _workingImage;
  234. public PaintData() { }
  235. public PaintData(ErrorItemCollection items, ErrorItem item, Rectangle rectangle, Graphics g)
  236. {
  237. if (item == null)
  238. item = new ErrorItem();
  239. selection = item;
  240. _firstItem = ((items.Count > 0) ? items[0] : null);
  241. viewport = rectangle;
  242. _workingImage = new Bitmap(rectangle.Width, rectangle.Height, g);
  243. WorkingGraphics = Graphics.FromImage(_workingImage);
  244. return;
  245. }
  246. public void Dispose()
  247. {
  248. if (_workingImage != null)
  249. {
  250. _workingImage.Dispose();
  251. WorkingGraphics.Dispose();
  252. }
  253. return;
  254. }
  255. public void PaintTo(Graphics g)
  256. {
  257. g.DrawImage(_workingImage, 0, 0);
  258. }
  259. public bool Equals(ErrorItemCollection items, ErrorItem item, Rectangle rectangle)
  260. {
  261. ErrorItem first = ((items.Count > 0) ? items[0] : null);
  262. return (viewport.Equals(rectangle) &&
  263. object.ReferenceEquals(item, selection) &&
  264. object.ReferenceEquals(first, _firstItem));
  265. }
  266. }
  267. }
  268. }