~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/Mono.Texteditor/Mono.TextEditor/TextMarker.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// TextMarker.cs
2
 
//
3
 
// Author:
4
 
//   Mike KrĆ¼ger <mkrueger@novell.com>
5
 
//
6
 
// Copyright (c) 2008 Novell, Inc (http://www.novell.com)
7
 
//
8
 
// Permission is hereby granted, free of charge, to any person obtaining a copy
9
 
// of this software and associated documentation files (the "Software"), to deal
10
 
// in the Software without restriction, including without limitation the rights
11
 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 
// copies of the Software, and to permit persons to whom the Software is
13
 
// furnished to do so, subject to the following conditions:
14
 
//
15
 
// The above copyright notice and this permission notice shall be included in
16
 
// all copies or substantial portions of the Software.
17
 
//
18
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 
// THE SOFTWARE.
25
 
 
26
 
using System;
27
 
using Gdk;
28
 
 
29
 
namespace Mono.TextEditor
30
 
{
31
 
        public interface IExtendingTextMarker 
32
 
        {
33
 
                double GetLineHeight (TextEditor editor);
34
 
                void Draw (TextEditor editor, Cairo.Context cr, int lineNr, Cairo.Rectangle lineArea);
35
 
        }
36
 
        
37
 
        public interface IActionTextMarker
38
 
        {
39
 
                /// <returns>
40
 
                /// true, if the mouse press was handled - false otherwise.
41
 
                /// </returns>
42
 
                bool MousePressed (TextEditor editor, MarginMouseEventArgs args);
43
 
                
44
 
                void MouseHover (TextEditor editor, MarginMouseEventArgs args, TextMarkerHoverResult result);
45
 
        }
46
 
        
47
 
        public class TextMarkerHoverResult 
48
 
        {
49
 
                public Gdk.Cursor Cursor { get; set; }
50
 
                public string TooltipMarkup { get; set; }
51
 
        }
52
 
        
53
 
        [Flags]
54
 
        public enum TextMarkerFlags
55
 
        {
56
 
                None           = 0,
57
 
                DrawsSelection = 1
58
 
        }
59
 
        
60
 
        public class TextMarker
61
 
        {
62
 
                DocumentLine lineSegment;
63
 
                
64
 
                public DocumentLine LineSegment {
65
 
                        get {
66
 
                                return lineSegment;
67
 
                        }
68
 
                        set {
69
 
                                lineSegment = value;
70
 
                        }
71
 
                }
72
 
                
73
 
                public virtual TextMarkerFlags Flags {
74
 
                        get;
75
 
                        set;
76
 
                }
77
 
                
78
 
                
79
 
                bool isVisible = true;
80
 
                public virtual bool IsVisible {
81
 
                        get { return isVisible; }
82
 
                        set { isVisible = value; }
83
 
                }
84
 
 
85
 
                public TextMarker ()
86
 
                {
87
 
                }
88
 
                
89
 
                public virtual void Draw (TextEditor editor, Cairo.Context cr, Pango.Layout layout, bool selected, int startOffset, int endOffset, double y, double startXPos, double endXPos)
90
 
                {
91
 
                }
92
 
                
93
 
                public virtual ChunkStyle GetStyle (ChunkStyle baseStyle)
94
 
                {
95
 
                        return baseStyle;
96
 
                }
97
 
        }
98
 
        
99
 
        public enum UrlType {
100
 
                Unknown,
101
 
                Url,
102
 
                Email
103
 
        }
104
 
        
105
 
        public class UrlMarker : TextMarker, IDisposable
106
 
        {
107
 
                string url;
108
 
                string style;
109
 
                int startColumn;
110
 
                int endColumn;
111
 
                DocumentLine line;
112
 
                UrlType urlType;
113
 
                TextDocument doc;
114
 
                
115
 
                public string Url {
116
 
                        get {
117
 
                                return url;
118
 
                        }
119
 
                }
120
 
 
121
 
                public int StartColumn {
122
 
                        get {
123
 
                                return startColumn;
124
 
                        }
125
 
                }
126
 
 
127
 
                public int EndColumn {
128
 
                        get {
129
 
                                return endColumn;
130
 
                        }
131
 
                }
132
 
 
133
 
                public UrlType UrlType {
134
 
                        get {
135
 
                                return urlType;
136
 
                        }
137
 
                }
138
 
                
139
 
                public UrlMarker (TextDocument doc, DocumentLine line, string url, UrlType urlType, string style, int startColumn, int endColumn)
140
 
                {
141
 
                        this.doc = doc;
142
 
                        this.line = line;
143
 
                        this.url = url;
144
 
                        this.urlType = urlType;
145
 
                        this.style = style;
146
 
                        this.startColumn = startColumn;
147
 
                        this.endColumn = endColumn;
148
 
                        doc.LineChanged += HandleDocLineChanged;
149
 
                }
150
 
                
151
 
                void HandleDocLineChanged (object sender, LineEventArgs e)
152
 
                {
153
 
                        if (line == e.Line)
154
 
                                doc.RemoveMarker (this);
155
 
                }
156
 
                
157
 
                public void Dispose ()
158
 
                {
159
 
                        doc.LineChanged -= HandleDocLineChanged;
160
 
                }
161
 
                
162
 
                public override void Draw (TextEditor editor, Cairo.Context cr, Pango.Layout layout, bool selected, int startOffset, int endOffset, double y, double startXPos, double endXPos)
163
 
                {
164
 
                        int markerStart = line.Offset + startColumn;
165
 
                        int markerEnd = line.Offset + endColumn;
166
 
        
167
 
                        if (markerEnd < startOffset || markerStart > endOffset) 
168
 
                                return; 
169
 
        
170
 
                        double @from;
171
 
                        double to;
172
 
        
173
 
                        if (markerStart < startOffset && endOffset < markerEnd) {
174
 
                                @from = startXPos;
175
 
                                to = endXPos;
176
 
                        } else {
177
 
                                int start = startOffset < markerStart ? markerStart : startOffset;
178
 
                                int end = endOffset < markerEnd ? endOffset : markerEnd;
179
 
                                int x_pos = layout.IndexToPos (start - startOffset).X;
180
 
        
181
 
                                @from = startXPos + (int)(x_pos / Pango.Scale.PangoScale);
182
 
        
183
 
                                x_pos = layout.IndexToPos (end - startOffset).X;
184
 
        
185
 
                                to = startXPos + (int)(x_pos / Pango.Scale.PangoScale);
186
 
                        }
187
 
        
188
 
                        @from = System.Math.Max (@from, editor.TextViewMargin.XOffset);
189
 
                        to = System.Math.Max (to, editor.TextViewMargin.XOffset);
190
 
                        if (@from < to) {
191
 
                                cr.DrawLine (selected ? editor.ColorStyle.Selection.CairoColor : editor.ColorStyle.GetChunkStyle (style).CairoColor, @from + 0.5, y + editor.LineHeight - 1.5, to + 0.5, y + editor.LineHeight - 1.5);
192
 
                        }
193
 
                }
194
 
        }
195
 
        
196
 
        /// <summary>
197
 
        /// A specialized text marker interface to draw icons in the bookmark margin.
198
 
        /// </summary>
199
 
        public interface IIconBarMarker
200
 
        {
201
 
                void DrawIcon (TextEditor editor, Cairo.Context cr, DocumentLine line, int lineNumber, double xPos, double yPos, double width, double height);
202
 
                void MousePress (MarginMouseEventArgs args);
203
 
                void MouseRelease (MarginMouseEventArgs args);
204
 
                void MouseHover (MarginMouseEventArgs args);
205
 
        }
206
 
        
207
 
        /// <summary>
208
 
        /// A specialized interface to draw text backgrounds.
209
 
        /// </summary>
210
 
        public interface IBackgroundMarker
211
 
        {
212
 
                /// <summary>
213
 
                /// Draws the backround of a line part.
214
 
                /// </summary>
215
 
                /// <returns>
216
 
                /// true, when the text view should draw the text, false when the text view should not draw the text.
217
 
                /// </returns>
218
 
                bool DrawBackground (TextEditor Editor, Cairo.Context cr, TextViewMargin.LayoutWrapper layout, int selectionStart, int selectionEnd, int startOffset, int endOffset, double y, double startXPos, double endXPos, ref bool drawBg);
219
 
        }
220
 
        
221
 
        public class LineBackgroundMarker: TextMarker, IBackgroundMarker
222
 
        {
223
 
                Cairo.Color color;
224
 
                
225
 
                public LineBackgroundMarker (Cairo.Color color)
226
 
                {
227
 
                        this.color = color;
228
 
                }
229
 
                
230
 
                public bool DrawBackground (TextEditor editor, Cairo.Context cr, TextViewMargin.LayoutWrapper layout, int selectionStart, int selectionEnd, int startOffset, int endOffset, double y, double startXPos, double endXPos, ref bool drawBg)
231
 
                {
232
 
                        drawBg = false;
233
 
                        if (selectionStart > 0)
234
 
                                return true;
235
 
                        cr.Color = color;
236
 
                        cr.Rectangle (startXPos, y, endXPos - startXPos, editor.LineHeight);
237
 
                        cr.Fill ();
238
 
                        return true;
239
 
                }
240
 
        }
241
 
        
242
 
        public class UnderlineMarker: TextMarker
243
 
        {
244
 
                protected UnderlineMarker ()
245
 
                {}
246
 
                
247
 
                public UnderlineMarker (string colorName, int start, int end)
248
 
                {
249
 
                        this.ColorName = colorName;
250
 
                        this.StartCol = start;
251
 
                        this.EndCol = end;
252
 
                        this.Wave = true;
253
 
                }
254
 
                public UnderlineMarker (Cairo.Color color, int start, int end)
255
 
                {
256
 
                        this.Color = color;
257
 
                        this.StartCol = start;
258
 
                        this.EndCol = end;
259
 
                        this.Wave = false;
260
 
                }
261
 
                
262
 
                public string ColorName { get; set; }
263
 
                public Cairo.Color Color { get; set; }
264
 
                public int StartCol { get; set; }
265
 
                public int EndCol { get; set; }
266
 
                public bool Wave { get; set; }
267
 
                
268
 
                public override void Draw (TextEditor editor, Cairo.Context cr, Pango.Layout layout, bool selected, int startOffset, int endOffset, double y, double startXPos, double endXPos)
269
 
                {
270
 
                        int markerStart = LineSegment.Offset + System.Math.Max (StartCol - 1, 0);
271
 
                        int markerEnd = LineSegment.Offset + (EndCol < 1 ? LineSegment.Length : EndCol - 1);
272
 
                        if (markerEnd < startOffset || markerStart > endOffset) 
273
 
                                return; 
274
 
        
275
 
                        
276
 
                        if (editor.IsSomethingSelected) {
277
 
                                var range = editor.SelectionRange;
278
 
                                if (range.Contains (markerStart)) {
279
 
                                        int end = System.Math.Min (markerEnd, range.EndOffset);
280
 
                                        InternalDraw (markerStart, end, editor, cr, layout, true, startOffset, endOffset, y, startXPos, endXPos);
281
 
                                        InternalDraw (range.EndOffset, markerEnd, editor, cr, layout, false, startOffset, endOffset, y, startXPos, endXPos);
282
 
                                        return;
283
 
                                }
284
 
                                if (range.Contains (markerEnd)) {
285
 
                                        InternalDraw (markerStart, range.Offset, editor, cr, layout, false, startOffset, endOffset, y, startXPos, endXPos);
286
 
                                        InternalDraw (range.Offset, markerEnd, editor, cr, layout, true, startOffset, endOffset, y, startXPos, endXPos);
287
 
                                        return;
288
 
                                }
289
 
                                if (markerStart <= range.Offset && range.EndOffset <= markerEnd) {
290
 
                                        InternalDraw (markerStart, range.Offset, editor, cr, layout, false, startOffset, endOffset, y, startXPos, endXPos);
291
 
                                        InternalDraw (range.Offset, range.EndOffset, editor, cr, layout, true, startOffset, endOffset, y, startXPos, endXPos);
292
 
                                        InternalDraw (range.EndOffset, markerEnd, editor, cr, layout, false, startOffset, endOffset, y, startXPos, endXPos);
293
 
                                        return;
294
 
                                }
295
 
                                
296
 
                        }
297
 
                        
298
 
                        InternalDraw (markerStart, markerEnd, editor, cr, layout, false, startOffset, endOffset, y, startXPos, endXPos);
299
 
                }
300
 
                
301
 
                void InternalDraw (int markerStart, int markerEnd, TextEditor editor, Cairo.Context cr, Pango.Layout layout, bool selected, int startOffset, int endOffset, double y, double startXPos, double endXPos)
302
 
                {
303
 
                        if (markerStart >= markerEnd)
304
 
                                return;
305
 
                        double @from;
306
 
                        double to;
307
 
                        if (markerStart < startOffset && endOffset < markerEnd) {
308
 
                                @from = startXPos;
309
 
                                to = endXPos;
310
 
                        } else {
311
 
                                int start = startOffset < markerStart ? markerStart : startOffset;
312
 
                                int end = endOffset < markerEnd ? endOffset : markerEnd;
313
 
                                int /*lineNr,*/ x_pos;
314
 
                                
315
 
                                x_pos = layout.IndexToPos (start - startOffset).X;
316
 
                                @from = startXPos + (int)(x_pos / Pango.Scale.PangoScale);
317
 
        
318
 
                                x_pos = layout.IndexToPos (end - startOffset).X;
319
 
        
320
 
                                to = startXPos + (int)(x_pos / Pango.Scale.PangoScale);
321
 
                        }
322
 
                        @from = System.Math.Max (@from, editor.TextViewMargin.XOffset);
323
 
                        to = System.Math.Max (to, editor.TextViewMargin.XOffset);
324
 
                        if (@from >= to) {
325
 
                                return;
326
 
                        }
327
 
                        double height = editor.LineHeight / 5;
328
 
                        if (selected) {
329
 
                                cr.Color = editor.ColorStyle.Selection.CairoColor;
330
 
                        } else {
331
 
                                cr.Color = ColorName == null ? Color : editor.ColorStyle.GetColorFromDefinition (ColorName);
332
 
                        }
333
 
                        if (Wave) {     
334
 
                                Pango.CairoHelper.ShowErrorUnderline (cr, @from, y + editor.LineHeight - height, to - @from, height);
335
 
                        } else {
336
 
                                cr.LineWidth = 1;
337
 
                                cr.MoveTo (@from, y + editor.LineHeight - 1.5);
338
 
                                cr.LineTo (to, y + editor.LineHeight - 1.5);
339
 
                                cr.Stroke ();
340
 
                        }
341
 
                }
342
 
        }
343
 
 
344
 
        public class StyleTextMarker: TextMarker
345
 
        {
346
 
                [Flags]
347
 
                public enum StyleFlag {
348
 
                        None = 0,
349
 
                        Color = 1,
350
 
                        BackgroundColor = 2,
351
 
                        Bold = 4,
352
 
                        Italic = 8
353
 
                }
354
 
                
355
 
                Cairo.Color color;
356
 
                Cairo.Color backColor;
357
 
                bool bold;
358
 
                bool italic;
359
 
                
360
 
                public bool Italic {
361
 
                        get {
362
 
                                return italic;
363
 
                        }
364
 
                        set {
365
 
                                italic = value;
366
 
                                IncludedStyles |= StyleFlag.Italic;
367
 
                        }
368
 
                }
369
 
                
370
 
                public virtual StyleFlag IncludedStyles {
371
 
                        get;
372
 
                        set;
373
 
                }
374
 
                
375
 
                public virtual Cairo.Color Color {
376
 
                        get {
377
 
                                return color;
378
 
                        }
379
 
                        set {
380
 
                                color = value;
381
 
                                IncludedStyles |= StyleFlag.Color;
382
 
                        }
383
 
                }
384
 
                
385
 
                public bool Bold {
386
 
                        get {
387
 
                                return bold;
388
 
                        }
389
 
                        set {
390
 
                                bold = value;
391
 
                                IncludedStyles |= StyleFlag.Bold;
392
 
                        }
393
 
                }
394
 
                
395
 
                public virtual Cairo.Color BackgroundColor {
396
 
                        get {
397
 
                                return backColor;
398
 
                        }
399
 
                        set {
400
 
                                backColor = value;
401
 
                                IncludedStyles |= StyleFlag.BackgroundColor;
402
 
                        }
403
 
                }
404
 
                
405
 
                protected virtual ChunkStyle CreateStyle (ChunkStyle baseStyle, Cairo.Color color, Cairo.Color bgColor)
406
 
                {
407
 
                        ChunkStyle style = new ChunkStyle (baseStyle);
408
 
                        if ((IncludedStyles & StyleFlag.Color) != 0)
409
 
                                style.CairoColor = color;
410
 
                        
411
 
                        if ((IncludedStyles & StyleFlag.BackgroundColor) != 0) {
412
 
                                style.ChunkProperties &= ~ChunkProperties.TransparentBackground;
413
 
                                style.CairoBackgroundColor = bgColor;
414
 
                        }
415
 
                        
416
 
                        if ((IncludedStyles & StyleFlag.Bold) != 0)
417
 
                                style.ChunkProperties |= ChunkProperties.Bold;
418
 
                        
419
 
                        if ((IncludedStyles & StyleFlag.Italic) != 0)
420
 
                                style.ChunkProperties |= ChunkProperties.Italic;
421
 
                        return style;
422
 
                }
423
 
                
424
 
                public override ChunkStyle GetStyle (ChunkStyle baseStyle)
425
 
                {
426
 
                        if (baseStyle == null || IncludedStyles == StyleFlag.None)
427
 
                                return baseStyle;
428
 
                        
429
 
                        return CreateStyle (baseStyle, Color, BackgroundColor);
430
 
                }
431
 
        }
432
 
}