PageRenderTime 16ms CodeModel.GetById 7ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/UiException/Controls/CodeRenderingContext.cs

#
C# | 226 lines | 176 code | 43 blank | 7 comment | 0 complexity | 0f866da46f9d85d8bdf4d86024da52c7 MD5 | raw file
  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
  6using System;
  7using System.Collections.Generic;
  8using System.Text;
  9using System.Drawing;
 10using NUnit.UiException.CodeFormatters;
 11
 12namespace NUnit.UiException.Controls
 13{
 14    /// <summary>
 15    /// Encapsulates basic colors settings to format a text according a language.
 16    /// </summary>
 17    public class CodeRenderingContext
 18    {
 19        private static readonly int INDEX_CODE = 0;
 20        private static readonly int INDEX_KEYWORD = 1;
 21        private static readonly int INDEX_COMMENT = 2;
 22        private static readonly int INDEX_STRING = 3;
 23        private static readonly int INDEX_BACKGROUND = 4;
 24        private static readonly int INDEX_CURRBACK = 5;
 25        private static readonly int INDEX_CURRFORE = 6;
 26
 27        private Graphics _graphics;
 28        private Font _font;
 29
 30        private int _currentLine;
 31
 32        private ColorMaterial[] _colors;
 33
 34        public CodeRenderingContext()
 35        {
 36            _colors = new ColorMaterial[] 
 37            {
 38                new ColorMaterial(Color.Black),        // code color
 39                new ColorMaterial(Color.Blue),         // keyword color
 40                new ColorMaterial(Color.Green),        // comment color
 41                new ColorMaterial(Color.Red),          // string color
 42
 43                new ColorMaterial(Color.White),        // background
 44                new ColorMaterial(Color.Red),          // current line back color
 45                new ColorMaterial(Color.White),        // current line fore color                
 46            };
 47
 48            return;
 49        }
 50
 51        public Graphics Graphics {
 52            get { return (_graphics); }
 53            set { _graphics = value; }
 54        }       
 55
 56        public Font Font {
 57            get { return (_font); }
 58            set { _font = value; }
 59        }
 60
 61        public int CurrentLine {
 62            get { return (_currentLine); }
 63            set { _currentLine = value; }
 64        }
 65
 66        public Color BackgroundColor {
 67            get { return (_colors[INDEX_BACKGROUND].Color); }
 68            set
 69            {
 70                _colors[INDEX_BACKGROUND].Dispose();
 71                _colors[INDEX_BACKGROUND] = new ColorMaterial(value);
 72            }
 73        }
 74
 75        public Color CurrentLineBackColor {
 76            get { return (_colors[INDEX_CURRBACK].Color); }
 77            set {
 78                _colors[INDEX_CURRBACK].Dispose();
 79                _colors[INDEX_CURRBACK] = new ColorMaterial(value);
 80            }
 81        }
 82
 83        public Color CurrentLineForeColor {
 84            get { return (_colors[INDEX_CURRFORE].Color); }
 85            set {
 86                _colors[INDEX_CURRFORE].Dispose();
 87                _colors[INDEX_CURRFORE] = new ColorMaterial(value);
 88            }
 89        }
 90
 91        public Color KeywordColor {
 92            get { return (_colors[INDEX_KEYWORD].Color); }
 93            set {
 94                _colors[INDEX_KEYWORD].Dispose();
 95                _colors[INDEX_KEYWORD] = new ColorMaterial(value);
 96            }
 97        }
 98
 99        public Color CommentColor {
100            get { return (_colors[INDEX_COMMENT].Color); }
101            set {
102                _colors[INDEX_COMMENT].Dispose();
103                _colors[INDEX_COMMENT] = new ColorMaterial(value);
104            }
105        }
106
107        public Color CodeColor {
108            get { return (_colors[INDEX_CODE].Color); }
109            set {
110                _colors[INDEX_CODE].Dispose();
111                _colors[INDEX_CODE] = new ColorMaterial(value);
112            }
113        }
114
115        public Color StringColor {
116            get { return (_colors[INDEX_STRING].Color); }
117            set {
118                _colors[INDEX_STRING].Dispose();
119                _colors[INDEX_STRING] = new ColorMaterial(value);
120            }
121        }
122
123        public Color this[ClassificationTag tag]
124        {
125            get
126            {
127                int idx = (int)tag;
128                return (_colors[idx].Color);
129            }
130        }    
131
132        public Brush GetBrush(ClassificationTag tag) {
133            return (_colors[(int)tag].Brush);
134        }
135      
136        public Pen GetPen(ClassificationTag tag) {
137            return (_colors[(int)tag].Pen);
138        }
139
140        #region Brushes
141
142        public Brush BackgroundBrush {
143            get { return (_colors[INDEX_BACKGROUND].Brush); }
144        }
145
146        public Brush CurrentLineBackBrush {
147            get { return (_colors[INDEX_CURRBACK].Brush); }
148        }
149
150        public Brush CurrentLineForeBrush {
151            get { return (_colors[INDEX_CURRFORE].Brush); }
152        }
153
154        public Brush KeywordBrush {
155            get { return (_colors[INDEX_KEYWORD].Brush); }
156        }
157
158        public Brush CommentBrush {
159            get { return (_colors[INDEX_COMMENT].Brush); }
160        }
161
162        public Brush CodeBrush {
163            get { return (_colors[INDEX_CODE].Brush); }
164        }
165
166        public Brush StringBrush {
167            get { return (_colors[INDEX_STRING].Brush); }
168        }
169
170        #endregion
171
172        #region Pens
173
174        public Pen BackgroundPen {
175            get { return (_colors[INDEX_BACKGROUND].Pen); }
176        }
177
178        public Pen CurrentLineBackPen {
179            get { return (_colors[INDEX_CURRBACK].Pen); }
180        }
181
182        public Pen CurrentLineForePen {
183            get { return (_colors[INDEX_CURRFORE].Pen); }
184        }
185
186        public Pen KeywordPen {
187            get { return (_colors[INDEX_KEYWORD].Pen); }
188        }
189
190        public Pen CommentPen {
191            get { return (_colors[INDEX_COMMENT].Pen); }
192        }
193
194        public Pen CodePen {
195            get { return (_colors[INDEX_CODE].Pen); }
196        }
197
198        public Pen StringPen {
199            get { return (_colors[INDEX_STRING].Pen); }
200        }
201
202        #endregion
203
204        class ColorMaterial
205        {
206            public Color Color;
207            public Brush Brush;
208            public Pen Pen;
209
210            public ColorMaterial(Color color)
211            {
212                this.Color = color;
213                this.Brush = new SolidBrush(color);
214                this.Pen = new Pen(color);
215
216                return;
217            }
218
219            public void Dispose()
220            {
221                this.Brush.Dispose();
222                this.Pen.Dispose();
223            }
224        }
225    }
226}