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

« back to all changes in this revision

Viewing changes to src/core/Mono.Texteditor/Mono.TextEditor/TextSegmentMarker.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
//
 
2
// TextSegmentMarker.cs
 
3
//
 
4
// Author:
 
5
//       Mike Krüger <mkrueger@xamarin.com>
 
6
//
 
7
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using Cairo;
 
28
using Mono.TextEditor.Highlighting;
 
29
 
 
30
namespace Mono.TextEditor
 
31
{
 
32
        public class TextSegmentMarker : TreeSegment
 
33
        {
 
34
 
 
35
                public virtual TextLineMarkerFlags Flags {
 
36
                        get;
 
37
                        set;
 
38
                }
 
39
                
 
40
                
 
41
                bool isVisible = true;
 
42
                public virtual bool IsVisible {
 
43
                        get { return isVisible; }
 
44
                        set { isVisible = value; }
 
45
                }
 
46
                
 
47
                public TextSegmentMarker (int offset, int length) : base (offset, length)
 
48
                {
 
49
                }
 
50
 
 
51
                public TextSegmentMarker (TextSegment textSegment) : base (textSegment)
 
52
                {
 
53
                }
 
54
                
 
55
                public virtual void Draw (TextEditor editor, Cairo.Context cr, Pango.Layout layout, bool selected, int startOffset, int endOffset, double y, double startXPos, double endXPos)
 
56
                {
 
57
                }
 
58
                
 
59
                public virtual ChunkStyle GetStyle (ChunkStyle baseStyle)
 
60
                {
 
61
                        return baseStyle;
 
62
                }
 
63
        }
 
64
 
 
65
        public interface IChunkMarker
 
66
        {
 
67
                void ChangeForeColor (TextEditor editor, Chunk chunk, ref Cairo.Color color);
 
68
        }
 
69
 
 
70
        public class UnderlineTextSegmentMarker : TextSegmentMarker
 
71
        {
 
72
                public UnderlineTextSegmentMarker (Cairo.Color color, TextSegment textSegment) : base (textSegment)
 
73
                {
 
74
                        this.Color = color;
 
75
                        this.Wave = true;
 
76
                }
 
77
 
 
78
                public UnderlineTextSegmentMarker (string colorName, TextSegment textSegment) : base (textSegment)
 
79
                {
 
80
                        this.ColorName = colorName;
 
81
                        this.Wave = true;
 
82
                }
 
83
 
 
84
                public string ColorName { get; set; }
 
85
                public Cairo.Color Color { get; set; }
 
86
                public bool Wave { get; set; }
 
87
                
 
88
                public override void Draw (TextEditor editor, Cairo.Context cr, Pango.Layout layout, bool selected, int startOffset, int endOffset, double y, double startXPos, double endXPos)
 
89
                {
 
90
                        int markerStart = Segment.Offset;
 
91
                        int markerEnd = Segment.EndOffset;
 
92
                        if (markerEnd < startOffset || markerStart > endOffset) 
 
93
                                return; 
 
94
                        
 
95
                        
 
96
                        if (editor.IsSomethingSelected) {
 
97
                                var range = editor.SelectionRange;
 
98
                                if (range.Contains (markerStart)) {
 
99
                                        int end = System.Math.Min (markerEnd, range.EndOffset);
 
100
                                        InternalDraw (markerStart, end, editor, cr, layout, true, startOffset, endOffset, y, startXPos, endXPos);
 
101
                                        InternalDraw (range.EndOffset, markerEnd, editor, cr, layout, false, startOffset, endOffset, y, startXPos, endXPos);
 
102
                                        return;
 
103
                                }
 
104
                                if (range.Contains (markerEnd)) {
 
105
                                        InternalDraw (markerStart, range.Offset, editor, cr, layout, false, startOffset, endOffset, y, startXPos, endXPos);
 
106
                                        InternalDraw (range.Offset, markerEnd, editor, cr, layout, true, startOffset, endOffset, y, startXPos, endXPos);
 
107
                                        return;
 
108
                                }
 
109
                                if (markerStart <= range.Offset && range.EndOffset <= markerEnd) {
 
110
                                        InternalDraw (markerStart, range.Offset, editor, cr, layout, false, startOffset, endOffset, y, startXPos, endXPos);
 
111
                                        InternalDraw (range.Offset, range.EndOffset, editor, cr, layout, true, startOffset, endOffset, y, startXPos, endXPos);
 
112
                                        InternalDraw (range.EndOffset, markerEnd, editor, cr, layout, false, startOffset, endOffset, y, startXPos, endXPos);
 
113
                                        return;
 
114
                                }
 
115
                                
 
116
                        }
 
117
                        
 
118
                        InternalDraw (markerStart, markerEnd, editor, cr, layout, false, startOffset, endOffset, y, startXPos, endXPos);
 
119
                }
 
120
                
 
121
                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)
 
122
                {
 
123
                        if (markerStart >= markerEnd)
 
124
                                return;
 
125
                        double @from;
 
126
                        double to;
 
127
                        if (markerStart < startOffset && endOffset < markerEnd) {
 
128
                                @from = startXPos;
 
129
                                to = endXPos;
 
130
                        } else {
 
131
                                int start = startOffset < markerStart ? markerStart : startOffset;
 
132
                                int end = endOffset < markerEnd ? endOffset : markerEnd;
 
133
                                int /*lineNr,*/ x_pos;
 
134
                                
 
135
                                x_pos = layout.IndexToPos (start - startOffset).X;
 
136
                                @from = startXPos + (int)(x_pos / Pango.Scale.PangoScale);
 
137
                                
 
138
                                x_pos = layout.IndexToPos (end - startOffset).X;
 
139
                                
 
140
                                to = startXPos + (int)(x_pos / Pango.Scale.PangoScale);
 
141
                        }
 
142
                        @from = System.Math.Max (@from, editor.TextViewMargin.XOffset);
 
143
                        to = System.Math.Max (to, editor.TextViewMargin.XOffset);
 
144
                        if (@from >= to) {
 
145
                                return;
 
146
                        }
 
147
                        double height = editor.LineHeight / 5;
 
148
                        if (selected) {
 
149
                                cr.Color = editor.ColorStyle.SelectedText.Foreground;
 
150
                        } else {
 
151
                                cr.Color = ColorName == null ? Color : editor.ColorStyle.GetChunkStyle (ColorName).Foreground;
 
152
                        }
 
153
                        if (Wave) {     
 
154
                                Pango.CairoHelper.ShowErrorUnderline (cr, @from, y + editor.LineHeight - height, to - @from, height);
 
155
                        } else {
 
156
                                cr.LineWidth = 1;
 
157
                                cr.MoveTo (@from, y + editor.LineHeight - 1.5);
 
158
                                cr.LineTo (to, y + editor.LineHeight - 1.5);
 
159
                                cr.Stroke ();
 
160
                        }
 
161
                }
 
162
        }
 
163
}