4
// Mike KrĆ¼ger <mkrueger@novell.com>
6
// Copyright (c) 2008 Novell, Inc (http://www.novell.com)
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:
15
// The above copyright notice and this permission notice shall be included in
16
// all copies or substantial portions of the Software.
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
29
namespace Mono.TextEditor
31
public interface IExtendingTextMarker
33
double GetLineHeight (TextEditor editor);
34
void Draw (TextEditor editor, Cairo.Context cr, int lineNr, Cairo.Rectangle lineArea);
37
public interface IActionTextMarker
40
/// true, if the mouse press was handled - false otherwise.
42
bool MousePressed (TextEditor editor, MarginMouseEventArgs args);
44
void MouseHover (TextEditor editor, MarginMouseEventArgs args, TextMarkerHoverResult result);
47
public class TextMarkerHoverResult
49
public Gdk.Cursor Cursor { get; set; }
50
public string TooltipMarkup { get; set; }
54
public enum TextMarkerFlags
60
public class TextMarker
62
DocumentLine lineSegment;
64
public DocumentLine LineSegment {
73
public virtual TextMarkerFlags Flags {
79
bool isVisible = true;
80
public virtual bool IsVisible {
81
get { return isVisible; }
82
set { isVisible = value; }
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)
93
public virtual ChunkStyle GetStyle (ChunkStyle baseStyle)
105
public class UrlMarker : TextMarker, IDisposable
121
public int StartColumn {
127
public int EndColumn {
133
public UrlType UrlType {
139
public UrlMarker (TextDocument doc, DocumentLine line, string url, UrlType urlType, string style, int startColumn, int endColumn)
144
this.urlType = urlType;
146
this.startColumn = startColumn;
147
this.endColumn = endColumn;
148
doc.LineChanged += HandleDocLineChanged;
151
void HandleDocLineChanged (object sender, LineEventArgs e)
154
doc.RemoveMarker (this);
157
public void Dispose ()
159
doc.LineChanged -= HandleDocLineChanged;
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)
164
int markerStart = line.Offset + startColumn;
165
int markerEnd = line.Offset + endColumn;
167
if (markerEnd < startOffset || markerStart > endOffset)
173
if (markerStart < startOffset && endOffset < markerEnd) {
177
int start = startOffset < markerStart ? markerStart : startOffset;
178
int end = endOffset < markerEnd ? endOffset : markerEnd;
179
int x_pos = layout.IndexToPos (start - startOffset).X;
181
@from = startXPos + (int)(x_pos / Pango.Scale.PangoScale);
183
x_pos = layout.IndexToPos (end - startOffset).X;
185
to = startXPos + (int)(x_pos / Pango.Scale.PangoScale);
188
@from = System.Math.Max (@from, editor.TextViewMargin.XOffset);
189
to = System.Math.Max (to, editor.TextViewMargin.XOffset);
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);
197
/// A specialized text marker interface to draw icons in the bookmark margin.
199
public interface IIconBarMarker
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);
208
/// A specialized interface to draw text backgrounds.
210
public interface IBackgroundMarker
213
/// Draws the backround of a line part.
216
/// true, when the text view should draw the text, false when the text view should not draw the text.
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);
221
public class LineBackgroundMarker: TextMarker, IBackgroundMarker
225
public LineBackgroundMarker (Cairo.Color color)
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)
233
if (selectionStart > 0)
236
cr.Rectangle (startXPos, y, endXPos - startXPos, editor.LineHeight);
242
public class UnderlineMarker: TextMarker
244
protected UnderlineMarker ()
247
public UnderlineMarker (string colorName, int start, int end)
249
this.ColorName = colorName;
250
this.StartCol = start;
254
public UnderlineMarker (Cairo.Color color, int start, int end)
257
this.StartCol = start;
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; }
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)
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)
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);
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);
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);
298
InternalDraw (markerStart, markerEnd, editor, cr, layout, false, startOffset, endOffset, y, startXPos, endXPos);
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)
303
if (markerStart >= markerEnd)
307
if (markerStart < startOffset && endOffset < markerEnd) {
311
int start = startOffset < markerStart ? markerStart : startOffset;
312
int end = endOffset < markerEnd ? endOffset : markerEnd;
313
int /*lineNr,*/ x_pos;
315
x_pos = layout.IndexToPos (start - startOffset).X;
316
@from = startXPos + (int)(x_pos / Pango.Scale.PangoScale);
318
x_pos = layout.IndexToPos (end - startOffset).X;
320
to = startXPos + (int)(x_pos / Pango.Scale.PangoScale);
322
@from = System.Math.Max (@from, editor.TextViewMargin.XOffset);
323
to = System.Math.Max (to, editor.TextViewMargin.XOffset);
327
double height = editor.LineHeight / 5;
329
cr.Color = editor.ColorStyle.Selection.CairoColor;
331
cr.Color = ColorName == null ? Color : editor.ColorStyle.GetColorFromDefinition (ColorName);
334
Pango.CairoHelper.ShowErrorUnderline (cr, @from, y + editor.LineHeight - height, to - @from, height);
337
cr.MoveTo (@from, y + editor.LineHeight - 1.5);
338
cr.LineTo (to, y + editor.LineHeight - 1.5);
344
public class StyleTextMarker: TextMarker
347
public enum StyleFlag {
356
Cairo.Color backColor;
366
IncludedStyles |= StyleFlag.Italic;
370
public virtual StyleFlag IncludedStyles {
375
public virtual Cairo.Color Color {
381
IncludedStyles |= StyleFlag.Color;
391
IncludedStyles |= StyleFlag.Bold;
395
public virtual Cairo.Color BackgroundColor {
401
IncludedStyles |= StyleFlag.BackgroundColor;
405
protected virtual ChunkStyle CreateStyle (ChunkStyle baseStyle, Cairo.Color color, Cairo.Color bgColor)
407
ChunkStyle style = new ChunkStyle (baseStyle);
408
if ((IncludedStyles & StyleFlag.Color) != 0)
409
style.CairoColor = color;
411
if ((IncludedStyles & StyleFlag.BackgroundColor) != 0) {
412
style.ChunkProperties &= ~ChunkProperties.TransparentBackground;
413
style.CairoBackgroundColor = bgColor;
416
if ((IncludedStyles & StyleFlag.Bold) != 0)
417
style.ChunkProperties |= ChunkProperties.Bold;
419
if ((IncludedStyles & StyleFlag.Italic) != 0)
420
style.ChunkProperties |= ChunkProperties.Italic;
424
public override ChunkStyle GetStyle (ChunkStyle baseStyle)
426
if (baseStyle == null || IncludedStyles == StyleFlag.None)
429
return CreateStyle (baseStyle, Color, BackgroundColor);