protected override LayoutWrapper RenderLine(long line)
        {
            Pango.Layout layout = new Pango.Layout(Editor.PangoContext);
            layout.FontDescription = Editor.Options.Font;
            StringBuilder sb          = new StringBuilder();
            long          startOffset = line * Editor.BytesInRow;
            long          endOffset   = System.Math.Min(startOffset + Editor.BytesInRow, Data.Length);

            byte[] lineBytes = Data.GetBytes(startOffset, (int)(endOffset - startOffset));
            for (int i = 0; i < lineBytes.Length; i++)
            {
                byte b  = lineBytes[i];
                char ch = (char)b;
                if (b < 128 && (Char.IsLetterOrDigit(ch) || Char.IsPunctuation(ch)))
                {
                    sb.Append(ch);
                }
                else
                {
                    sb.Append(".");
                }
            }

            layout.SetText(sb.ToString());
            char[] lineChars = layout.Text.ToCharArray();
            Margin.LayoutWrapper result = new LayoutWrapper(layout);
            uint curIndex = 0, byteIndex = 0;

            if (Data.IsSomethingSelected)
            {
                ISegment selection = Data.MainSelection.Segment;
                HandleSelection(selection.Offset, selection.EndOffset, startOffset, endOffset, null, delegate(long start, long end)
                {
                    Pango.AttrForeground selectedForeground = new Pango.AttrForeground(Style.Selection.Red,
                                                                                       Style.Selection.Green,
                                                                                       Style.Selection.Blue);
                    selectedForeground.StartIndex = TranslateToUTF8Index(lineChars, (uint)(start - startOffset), ref curIndex, ref byteIndex);
                    selectedForeground.EndIndex   = TranslateToUTF8Index(lineChars, (uint)(end - startOffset), ref curIndex, ref byteIndex);

                    result.Add(selectedForeground);

                    Pango.AttrBackground attrBackground = new Pango.AttrBackground(Style.SelectionBg.Red,
                                                                                   Style.SelectionBg.Green,
                                                                                   Style.SelectionBg.Blue);
                    attrBackground.StartIndex = selectedForeground.StartIndex;
                    attrBackground.EndIndex   = selectedForeground.EndIndex;
                    result.Add(attrBackground);
                });
            }
            result.SetAttributes();
            return(result);
        }
        protected override LayoutWrapper RenderLine(long line)
        {
            Pango.Layout layout = new Pango.Layout(Editor.PangoContext);
            layout.FontDescription = Editor.Options.Font;
            layout.Tabs            = tabArray;
            StringBuilder sb          = new StringBuilder();
            long          startOffset = line * Editor.BytesInRow;
            long          endOffset   = System.Math.Min(startOffset + Editor.BytesInRow, Data.Length);

            byte[] lineBytes = Data.GetBytes(startOffset, (int)(endOffset - startOffset));
            for (int i = 0; i < lineBytes.Length; i++)
            {
                sb.Append(string.Format("{0:X2}", lineBytes[i]));
                if (i % Editor.Options.GroupBytes == 0)
                {
                    sb.Append("\t");
                }
            }

            layout.SetText(sb.ToString());
            char[] lineChars = sb.ToString().ToCharArray();
            Margin.LayoutWrapper result = new LayoutWrapper(layout);
            uint curIndex = 0, byteIndex = 0;

            if (Data.IsSomethingSelected)
            {
                ISegment selection = Data.MainSelection.Segment;
                HandleSelection(selection.Offset, selection.EndOffset, startOffset, endOffset, null, delegate(long start, long end)
                {
                    Pango.AttrForeground selectedForeground = new Pango.AttrForeground(Style.Selection.Red,
                                                                                       Style.Selection.Green,
                                                                                       Style.Selection.Blue);
                    selectedForeground.StartIndex = TranslateToUTF8Index(lineChars, TranslateColumn(start - startOffset), ref curIndex, ref byteIndex);
                    selectedForeground.EndIndex   = TranslateToUTF8Index(lineChars, TranslateColumn(end - startOffset) - 1, ref curIndex, ref byteIndex);
                    result.Add(selectedForeground);

                    Pango.AttrBackground attrBackground = new Pango.AttrBackground(Style.SelectionBg.Red,
                                                                                   Style.SelectionBg.Green,
                                                                                   Style.SelectionBg.Blue);
                    attrBackground.StartIndex = selectedForeground.StartIndex;
                    attrBackground.EndIndex   = selectedForeground.EndIndex;
                    result.Add(attrBackground);
                });
            }
            result.SetAttributes();
            return(result);
        }
		public double ColumnToX (DocumentLine line, int column)
		{
			column--;
			// calculate virtual indentation
			if (column > 0 && line.Length == 0 && textEditor.GetTextEditorData ().HasIndentationTracker) {
				using (var l = PangoUtil.CreateLayout (textEditor, textEditor.GetTextEditorData ().IndentationTracker.GetIndentationString (line.Offset))) {
					l.Alignment = Pango.Alignment.Left;
					l.FontDescription = textEditor.Options.Font;
					l.Tabs = tabArray;

					Pango.Rectangle ink_rect, logical_rect;
					l.GetExtents (out ink_rect, out logical_rect);
					return (logical_rect.Width + Pango.Scale.PangoScale - 1) / Pango.Scale.PangoScale;
				}
			}
			if (line == null || line.Length == 0 || column < 0)
				return 0;
			int logicalRulerColumn = line.GetLogicalColumn (textEditor.GetTextEditorData (), textEditor.Options.RulerColumn);
			int lineOffset = line.Offset;
			StringBuilder textBuilder = new StringBuilder ();
			ISyntaxMode mode = Document.SyntaxMode != null && textEditor.Options.EnableSyntaxHighlighting ? Document.SyntaxMode : new SyntaxMode (Document);
			var startChunk = GetCachedChunks (mode, Document, textEditor.ColorStyle, line, lineOffset, line.Length);
			foreach (Chunk chunk in startChunk) {
				try {
					textBuilder.Append (Document.GetTextAt (chunk));
				} catch (Exception e) {
					Console.WriteLine (e);
					return 0;
				}
			}
			string lineText = textBuilder.ToString ();
			char[] lineChars = lineText.ToCharArray ();
			
			bool containsPreedit = textEditor.ContainsPreedit (lineOffset, line.Length);
			uint preeditLength = 0;

			if (containsPreedit) {
				lineText = lineText.Insert (textEditor.preeditOffset - lineOffset, textEditor.preeditString);
				preeditLength = (uint)textEditor.preeditString.Length;
			}
			if (column < lineText.Length)
				lineText = lineText.Substring (0, column);

			var layout = PangoUtil.CreateLayout (textEditor, lineText);
			layout.Alignment = Pango.Alignment.Left;
			layout.FontDescription = textEditor.Options.Font;
			layout.Tabs = tabArray;

			int startOffset = lineOffset, endOffset = lineOffset + line.Length;
			uint curIndex = 0, byteIndex = 0;
			uint curChunkIndex = 0, byteChunkIndex = 0;
			List<Pango.Attribute> attributes = new List<Pango.Attribute> ();
			uint oldEndIndex = 0;

			Cairo.Color curFgColor = textEditor.ColorStyle.PlainText.Foreground;
			Cairo.Color curBgColor = textEditor.ColorStyle.PlainText.Background;
			var curWeight = Xwt.Drawing.FontWeight.Normal;
			var curStyle = Xwt.Drawing.FontStyle.Normal;

			foreach (Chunk chunk in startChunk) {
				ChunkStyle chunkStyle = chunk != null ? textEditor.ColorStyle.GetChunkStyle (chunk) : null;

				foreach (TextLineMarker marker in line.Markers)
					chunkStyle = marker.GetStyle (chunkStyle);

				if (chunkStyle != null) {
					startOffset = chunk.Offset;
					endOffset = chunk.EndOffset;

					uint startIndex = (uint)(oldEndIndex);
					uint endIndex = (uint)(startIndex + chunk.Length);
					oldEndIndex = endIndex;

					if (containsPreedit) {
						if (textEditor.preeditOffset < startOffset)
							startIndex += preeditLength;
						if (textEditor.preeditOffset < endOffset)
							endIndex += preeditLength;
					}

					HandleSelection (lineOffset, logicalRulerColumn, - 1, -1, chunk.Offset, chunk.EndOffset, delegate(int start, int end) {
						var color = textEditor.ColorStyle.GetForeground (chunkStyle);
						var si = TranslateToUTF8Index (lineChars, (uint)(startIndex + start - chunk.Offset), ref curIndex, ref byteIndex);
						var ei = TranslateToUTF8Index (lineChars, (uint)(startIndex + end - chunk.Offset), ref curIndex, ref byteIndex);
						if (!color.Equals (curFgColor)) {
							curFgColor = color;
							var foreGround = new Pango.AttrForeground (
								(ushort)(color.R * ushort.MaxValue),
								(ushort)(color.G * ushort.MaxValue),
								(ushort)(color.B * ushort.MaxValue));
							foreGround.StartIndex = si;
							foreGround.EndIndex = ei;
							attributes.Add (foreGround);
						}
						if (!chunkStyle.TransparentBackground) {
							color = chunkStyle.Background;
							if (!color.Equals (curBgColor)) {
								var background = new Pango.AttrBackground (
									(ushort)(color.R * ushort.MaxValue),
									(ushort)(color.G * ushort.MaxValue),
									(ushort)(color.B * ushort.MaxValue));
								background.StartIndex = si;
								background.EndIndex = ei;
								attributes.Add (background);
							}
						}
					}, delegate(int start, int end) {
						Pango.AttrForeground selectedForeground;
						if (!SelectionColor.TransparentForeground) {
							var color = SelectionColor.Foreground;
							if (color.Equals (curFgColor))
								return;
							curFgColor = color;
							selectedForeground = new Pango.AttrForeground (
								(ushort)(color.R * ushort.MaxValue),
								(ushort)(color.G * ushort.MaxValue),
								(ushort)(color.B * ushort.MaxValue));
						} else {
							var color = ColorStyle.GetForeground (chunkStyle);
							if (color.Equals (curFgColor))
								return;
							curFgColor = color;
							selectedForeground = new Pango.AttrForeground (
								(ushort)(color.R * ushort.MaxValue),
								(ushort)(color.G * ushort.MaxValue),
								(ushort)(color.B * ushort.MaxValue));
						} 
						selectedForeground.StartIndex = TranslateToUTF8Index (lineChars, (uint)(startIndex + start - chunk.Offset), ref curIndex, ref byteIndex);
						selectedForeground.EndIndex = TranslateToUTF8Index (lineChars, (uint)(startIndex + end - chunk.Offset), ref curIndex, ref byteIndex);
						attributes.Add (selectedForeground);
					});

					var translatedStartIndex = TranslateToUTF8Index (lineChars, (uint)startIndex, ref curChunkIndex, ref byteChunkIndex);
					var translatedEndIndex = TranslateToUTF8Index (lineChars, (uint)endIndex, ref curChunkIndex, ref byteChunkIndex);

					if (chunkStyle.FontWeight != curWeight) {
						curWeight = chunkStyle.FontWeight;
						var attrWeight = new Pango.AttrWeight ((Pango.Weight)chunkStyle.FontWeight);
						attrWeight.StartIndex = translatedStartIndex;
						attrWeight.EndIndex = translatedEndIndex;
						attributes.Add (attrWeight);
					}

					if (chunkStyle.FontStyle != curStyle) {
						curStyle = chunkStyle.FontStyle;
						Pango.AttrStyle attrStyle = new Pango.AttrStyle ((Pango.Style)chunkStyle.FontStyle);
						attrStyle.StartIndex = translatedStartIndex;
						attrStyle.EndIndex = translatedEndIndex;
						attributes.Add (attrStyle);
					}

					if (chunkStyle.Underline) {
						var attrUnderline = new Pango.AttrUnderline (Pango.Underline.Single);
						attrUnderline.StartIndex = translatedStartIndex;
						attrUnderline.EndIndex = translatedEndIndex;
						attributes.Add (attrUnderline);
					}
				}
			}
			Pango.AttrList attributeList = new Pango.AttrList ();
			attributes.ForEach (attr => attributeList.Insert (attr));
			layout.Attributes = attributeList;
			Pango.Rectangle inkrect, logicalrect;
			layout.GetExtents (out inkrect, out logicalrect);
			attributes.ForEach (attr => attr.Dispose ());
			attributeList.Dispose ();
			layout.Dispose ();
			return (logicalrect.Width + Pango.Scale.PangoScale - 1) / Pango.Scale.PangoScale;
		}
Esempio n. 4
0
//		void ResultLineDataFunc (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
//		{
//			if (TreeIter.Zero.Equals (iter))
//				return;
//			var lineRenderer = (CellRendererText)cell;
//			var searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
//			if (searchResult == null)
//				return;
//
//			Document doc = GetDocument (searchResult);
//			int lineNr = doc.OffsetToLineNumber (searchResult.Offset) + 1;
//			bool didRead = (bool)store.GetValue (iter, DidReadColumn);
//			lineRenderer.Markup = MarkupText (lineNr.ToString (), didRead);
//		}
//
        void ResultTextDataFunc(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
        {
            if (TreeIter.Zero.Equals(iter))
            {
                return;
            }
            var textRenderer = (CellRendererText)cell;
            var searchResult = (SearchResult)store.GetValue(iter, SearchResultColumn);

            if (searchResult == null || searchResult.Offset < 0)
            {
                textRenderer.Markup = "Invalid search result";
                return;
            }

            var doc = GetDocument(searchResult);

            if (doc == null)
            {
                textRenderer.Markup = "Can't create document for:" + searchResult.FileName;
                return;
            }
            bool isSelected = treeviewSearchResults.Selection.IterIsSelected(iter);

            if (searchResult.Markup == null)
            {
                if (searchResult.LineNumber <= 0)
                {
                    searchResult.LineNumber = doc.OffsetToLineNumber(searchResult.Offset);
                }
                DocumentLine line = doc.GetLine(searchResult.LineNumber);
                if (line == null)
                {
                    textRenderer.Markup = "Invalid line number " + searchResult.LineNumber + " from offset: " + searchResult.Offset;
                    return;
                }
                int indent = line.GetIndentation(doc).Length;
                var data   = new Mono.TextEditor.TextEditorData(doc);
                data.ColorStyle = highlightStyle;
                var lineText = doc.GetTextAt(line.Offset + indent, line.Length - indent);
                int col      = searchResult.Offset - line.Offset - indent;
                // search result contained part of the indent.
                if (col + searchResult.Length < lineText.Length)
                {
                    lineText = doc.GetTextAt(line.Offset, line.Length);
                }

                var markup = doc.SyntaxMode != null?
                             data.GetMarkup(line.Offset + indent, line.Length - indent, true, !isSelected, false) :
                                 GLib.Markup.EscapeText(lineText);

                searchResult.Markup = AdjustColors(markup.Replace("\t", new string (' ', TextEditorOptions.DefaultOptions.TabSize)));

                uint start;
                uint end;
                try {
                    start = (uint)TextViewMargin.TranslateIndexToUTF8(lineText, col);
                    end   = (uint)TextViewMargin.TranslateIndexToUTF8(lineText, col + searchResult.Length);
                } catch (Exception e) {
                    LoggingService.LogError("Exception while translating index to utf8 (column was:" + col + " search result length:" + searchResult.Length + " line text:" + lineText + ")", e);
                    return;
                }
                searchResult.StartIndex = start;
                searchResult.EndIndex   = end;
            }


            try {
                textRenderer.Markup = searchResult.Markup;

                if (!isSelected)
                {
                    var    searchColor = highlightStyle.SearchResult.Color;
                    double b1          = Mono.TextEditor.HslColor.Brightness(searchColor);
                    double b2          = Mono.TextEditor.HslColor.Brightness(AdjustColor(Style.Base(StateType.Normal), (Mono.TextEditor.HslColor)highlightStyle.PlainText.Foreground));
                    double delta       = Math.Abs(b1 - b2);
                    if (delta < 0.1)
                    {
                        Mono.TextEditor.HslColor color1 = highlightStyle.SearchResult.Color;
                        if (color1.L + 0.5 > 1.0)
                        {
                            color1.L -= 0.5;
                        }
                        else
                        {
                            color1.L += 0.5;
                        }
                        searchColor = color1;
                    }
                    var attr = new Pango.AttrBackground((ushort)(searchColor.R * ushort.MaxValue), (ushort)(searchColor.G * ushort.MaxValue), (ushort)(searchColor.B * ushort.MaxValue));
                    attr.StartIndex = searchResult.StartIndex;
                    attr.EndIndex   = searchResult.EndIndex;

                    using (var list = textRenderer.Attributes.Copy()) {
                        list.Insert(attr);
                        textRenderer.Attributes = list;
                    }
                }
            } catch (Exception e) {
                LoggingService.LogError("Error whil setting the text renderer markup to: " + searchResult.Markup, e);
            }
        }
Esempio n. 5
0
		public double ColumnToX (LineSegment line, int column)
		{
			column--;
			if (line == null || line.Length == 0 || column < 0)
				return 0;
			int logicalRulerColumn = line.GetLogicalColumn (textEditor.GetTextEditorData (), textEditor.Options.RulerColumn);
			int lineOffset = line.Offset;
			StringBuilder textBuilder = new StringBuilder ();
			ISyntaxMode mode = Document.SyntaxMode != null && textEditor.Options.EnableSyntaxHighlighting ? Document.SyntaxMode : new SyntaxMode (Document);
			var startChunk = GetCachedChunks (mode, Document, textEditor.ColorStyle, line, lineOffset, line.Length);
			foreach (Chunk chunk in startChunk) {
				try {
					textBuilder.Append (Document.GetTextAt (chunk));
				} catch (Exception e) {
					Console.WriteLine (e);
					return 0;
				}
			}
			string lineText = textBuilder.ToString ();
			char[] lineChars = lineText.ToCharArray ();
			
			bool containsPreedit = textEditor.ContainsPreedit (lineOffset, line.Length);
			uint preeditLength = 0;

			if (containsPreedit) {
				lineText = lineText.Insert (textEditor.preeditOffset - lineOffset, textEditor.preeditString);
				preeditLength = (uint)textEditor.preeditString.Length;
			}
			if (column < lineText.Length)
				lineText = lineText.Substring (0, column);

			var layout = PangoUtil.CreateLayout (textEditor, lineText);
			layout.Alignment = Pango.Alignment.Left;
			layout.FontDescription = textEditor.Options.Font;
			layout.Tabs = tabArray;

			int startOffset = lineOffset, endOffset = lineOffset + line.Length;
			uint curIndex = 0, byteIndex = 0;
			uint curChunkIndex = 0, byteChunkIndex = 0;
			List<Pango.Attribute> attributes = new List<Pango.Attribute> ();
			uint oldEndIndex = 0;
			foreach (Chunk chunk in startChunk) {
				ChunkStyle chunkStyle = chunk != null ? textEditor.ColorStyle.GetChunkStyle (chunk) : null;

				foreach (TextMarker marker in line.Markers)
					chunkStyle = marker.GetStyle (chunkStyle);

				if (chunkStyle != null) {
					startOffset = chunk.Offset;
					endOffset = chunk.EndOffset;

					uint startIndex = (uint)(oldEndIndex);
					uint endIndex = (uint)(startIndex + chunk.Length);
					oldEndIndex = endIndex;

					if (containsPreedit) {
						if (textEditor.preeditOffset < startOffset)
							startIndex += preeditLength;
						if (textEditor.preeditOffset < endOffset)
							endIndex += preeditLength;
					}

					HandleSelection (lineOffset, logicalRulerColumn, - 1, -1, chunk.Offset, chunk.EndOffset, delegate(int start, int end) {
						Pango.AttrForeground foreGround = new Pango.AttrForeground (chunkStyle.Color.Red, chunkStyle.Color.Green, chunkStyle.Color.Blue);
						foreGround.StartIndex = TranslateToUTF8Index (lineChars, (uint)(startIndex + start - chunk.Offset), ref curIndex, ref byteIndex);
						foreGround.EndIndex = TranslateToUTF8Index (lineChars, (uint)(startIndex + end - chunk.Offset), ref curIndex, ref byteIndex);
						attributes.Add (foreGround);
						if (!chunkStyle.TransparentBackround) {
							var background = new Pango.AttrBackground (chunkStyle.BackgroundColor.Red, chunkStyle.BackgroundColor.Green, chunkStyle.BackgroundColor.Blue);
							background.StartIndex = foreGround.StartIndex;
							background.EndIndex = foreGround.EndIndex;
							attributes.Add (background);
						}
					}, delegate(int start, int end) {
						Pango.AttrForeground selectedForeground = new Pango.AttrForeground (SelectionColor.Color.Red, SelectionColor.Color.Green, SelectionColor.Color.Blue);
						selectedForeground.StartIndex = TranslateToUTF8Index (lineChars, (uint)(startIndex + start - chunk.Offset), ref curIndex, ref byteIndex);
						selectedForeground.EndIndex = TranslateToUTF8Index (lineChars, (uint)(startIndex + end - chunk.Offset), ref curIndex, ref byteIndex);
						attributes.Add (selectedForeground);
					});

					var translatedStartIndex = TranslateToUTF8Index (lineChars, (uint)startIndex, ref curChunkIndex, ref byteChunkIndex);
					var translatedEndIndex = TranslateToUTF8Index (lineChars, (uint)endIndex, ref curChunkIndex, ref byteChunkIndex);

					if (chunkStyle.Bold) {
						Pango.AttrWeight attrWeight = new Pango.AttrWeight (Pango.Weight.Bold);
						attrWeight.StartIndex = translatedStartIndex;
						attrWeight.EndIndex = translatedEndIndex;
						attributes.Add (attrWeight);
					}

					if (chunkStyle.Italic) {
						Pango.AttrStyle attrStyle = new Pango.AttrStyle (Pango.Style.Italic);
						attrStyle.StartIndex = translatedStartIndex;
						attrStyle.EndIndex = translatedEndIndex;
						attributes.Add (attrStyle);
					}

					if (chunkStyle.Underline) {
						Pango.AttrUnderline attrUnderline = new Pango.AttrUnderline (Pango.Underline.Single);
						attrUnderline.StartIndex = translatedStartIndex;
						attrUnderline.EndIndex = translatedEndIndex;
						attributes.Add (attrUnderline);
					}
				}
			}
			Pango.AttrList attributeList = new Pango.AttrList ();
			attributes.ForEach (attr => attributeList.Insert (attr));
			layout.Attributes = attributeList;
			Pango.Rectangle ink_rect, logical_rect;
			layout.GetExtents (out ink_rect, out logical_rect);
			attributes.ForEach (attr => attr.Dispose ());
			attributeList.Dispose ();
			layout.Dispose ();
			return (logical_rect.Width + Pango.Scale.PangoScale - 1) / Pango.Scale.PangoScale;
		}
//		void ResultLineDataFunc (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
//		{
//			if (TreeIter.Zero.Equals (iter))
//				return;
//			var lineRenderer = (CellRendererText)cell;
//			var searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
//			if (searchResult == null)
//				return;
//			
//			Document doc = GetDocument (searchResult);
//			int lineNr = doc.OffsetToLineNumber (searchResult.Offset) + 1;
//			bool didRead = (bool)store.GetValue (iter, DidReadColumn);
//			lineRenderer.Markup = MarkupText (lineNr.ToString (), didRead);
//		}
//		
		void ResultTextDataFunc (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
		{
			if (TreeIter.Zero.Equals (iter))
				return;
			var textRenderer = (CellRendererText)cell;
			var searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
			if (searchResult == null || searchResult.Offset < 0) {
				textRenderer.Markup = "Invalid search result";
				return;
			}
			
			var doc = GetDocument (searchResult);
			if (doc == null) {
				textRenderer.Markup = "Can't create document for:" + searchResult.FileName;
				return;
			}
			bool isSelected = treeviewSearchResults.Selection.IterIsSelected (iter);

			if (searchResult.Markup == null) {
				if (searchResult.LineNumber <= 0)
					searchResult.LineNumber = doc.OffsetToLineNumber (searchResult.Offset); 
				DocumentLine line = doc.GetLine (searchResult.LineNumber );
				if (line == null) {
					textRenderer.Markup = "Invalid line number " + searchResult.LineNumber + " from offset: " + searchResult.Offset;
					return;
				}
				int indent = line.GetIndentation (doc).Length;
				var data = new Mono.TextEditor.TextEditorData (doc);
				data.ColorStyle = highlightStyle;
				var lineText = doc.GetTextAt (line.Offset + indent, line.Length - indent);
				int col = searchResult.Offset - line.Offset - indent;
				// search result contained part of the indent.
				if (col + searchResult.Length < lineText.Length)
					lineText = doc.GetTextAt (line.Offset, line.Length);

				var markup = doc.SyntaxMode != null ?
				data.GetMarkup (line.Offset + indent, line.Length - indent, true, !isSelected, false) :
				GLib.Markup.EscapeText (lineText);
				searchResult.Markup = AdjustColors (markup.Replace ("\t", new string (' ', TextEditorOptions.DefaultOptions.TabSize)));

				uint start;
				uint end;
				try {
					start = (uint)TextViewMargin.TranslateIndexToUTF8 (lineText, col);
					end = (uint)TextViewMargin.TranslateIndexToUTF8 (lineText, Math.Min (lineText.Length, col + searchResult.Length));
				} catch (Exception e) {
					LoggingService.LogError ("Exception while translating index to utf8 (column was:" +col + " search result length:" + searchResult.Length + " line text:" + lineText + ")", e);
					return;
				}
				searchResult.StartIndex = start;
				searchResult.EndIndex = end;
			}


			try {
				textRenderer.Markup = searchResult.Markup;

				if (!isSelected) {
					var searchColor = searchResult.GetBackgroundMarkerColor (highlightStyle).Color;
					double b1 = Mono.TextEditor.HslColor.Brightness (searchColor);
					double b2 = Mono.TextEditor.HslColor.Brightness (AdjustColor (Style.Base (StateType.Normal), (Mono.TextEditor.HslColor)highlightStyle.PlainText.Foreground));
					double delta = Math.Abs (b1 - b2);
					if (delta < 0.1) {
						Mono.TextEditor.HslColor color1 = highlightStyle.SearchResult.Color;
						if (color1.L + 0.5 > 1.0) {
							color1.L -= 0.5;
						} else {
							color1.L += 0.5;
						}
						searchColor = color1;
					}
					var attr = new Pango.AttrBackground ((ushort)(searchColor.R * ushort.MaxValue), (ushort)(searchColor.G * ushort.MaxValue), (ushort)(searchColor.B * ushort.MaxValue));
					attr.StartIndex = searchResult.StartIndex;
					attr.EndIndex = searchResult.EndIndex;

					using (var list = textRenderer.Attributes.Copy ()) {
						list.Insert (attr);
						textRenderer.Attributes = list;
					}
				}
			} catch (Exception e) {
				LoggingService.LogError ("Error whil setting the text renderer markup to: " + searchResult.Markup, e);
			}
		}
		protected override LayoutWrapper RenderLine (long line)
		{
			Pango.Layout layout = new Pango.Layout (Editor.PangoContext);
			layout.FontDescription = Editor.Options.Font;
			layout.Tabs = tabArray;
			StringBuilder sb = new StringBuilder ();
			long startOffset = line * Editor.BytesInRow;
			long endOffset   = System.Math.Min (startOffset + Editor.BytesInRow, Data.Length);
			byte[] lineBytes = Data.GetBytes (startOffset, (int)(endOffset - startOffset));
			for (int i = 0; i < lineBytes.Length; i++) {
				sb.Append (string.Format ("{0:X2}", lineBytes[i]));
				if (i % Editor.Options.GroupBytes == 0)
					sb.Append ("\t");
			}
			
			layout.SetText (sb.ToString ());
			char[] lineChars = sb.ToString ().ToCharArray ();
			Margin.LayoutWrapper result = new LayoutWrapper (layout);
			uint curIndex = 0, byteIndex = 0;
			if (Data.IsSomethingSelected) {
				ISegment selection = Data.MainSelection.Segment;
				HandleSelection (selection.Offset, selection.EndOffset, startOffset, endOffset, null, delegate(long start, long end) {
					Pango.AttrForeground selectedForeground = new Pango.AttrForeground (Style.Selection.Red, 
					                                                                    Style.Selection.Green, 
					                                                                    Style.Selection.Blue);
					selectedForeground.StartIndex = TranslateToUTF8Index (lineChars, TranslateColumn (start - startOffset), ref curIndex, ref byteIndex);
					selectedForeground.EndIndex = TranslateToUTF8Index (lineChars, TranslateColumn (end - startOffset) - 1, ref curIndex, ref byteIndex);
					result.Add (selectedForeground);
					
					Pango.AttrBackground attrBackground = new Pango.AttrBackground (Style.SelectionBg.Red, 
					                                                                Style.SelectionBg.Green, 
					                                                                Style.SelectionBg.Blue);
					attrBackground.StartIndex = selectedForeground.StartIndex;
					attrBackground.EndIndex = selectedForeground.EndIndex;
					result.Add (attrBackground);

				});
			}
			result.SetAttributes ();
			return result;
		}
		protected override LayoutWrapper RenderLine (long line)
		{
			Pango.Layout layout = new Pango.Layout (Editor.PangoContext);
			layout.FontDescription = Editor.Options.Font;
			StringBuilder sb = new StringBuilder ();
			long startOffset = line * Editor.BytesInRow;
			long endOffset   = System.Math.Min (startOffset + Editor.BytesInRow, Data.Length);
			byte[] lineBytes = Data.GetBytes (startOffset, (int)(endOffset - startOffset));
			for (int i = 0; i < lineBytes.Length; i++) {
				byte b = lineBytes[i];
				char ch = (char)b;
				if (b < 128 && (Char.IsLetterOrDigit (ch) || Char.IsPunctuation (ch))) {
					sb.Append (ch);
				} else {
					sb.Append (".");
				}
			}
			
			layout.SetText (sb.ToString ());
			char[] lineChars = layout.Text.ToCharArray ();
			Margin.LayoutWrapper result = new LayoutWrapper (layout);
			uint curIndex = 0, byteIndex = 0;
			if (Data.IsSomethingSelected) {
				ISegment selection = Data.MainSelection.Segment;
				HandleSelection (selection.Offset, selection.EndOffset, startOffset, endOffset, null, delegate(long start, long end) {
					Pango.AttrForeground selectedForeground = new Pango.AttrForeground (Style.Selection.Red, 
					                                                                    Style.Selection.Green, 
					                                                                    Style.Selection.Blue);
					selectedForeground.StartIndex = TranslateToUTF8Index (lineChars, (uint)(start - startOffset), ref curIndex, ref byteIndex);
					selectedForeground.EndIndex = TranslateToUTF8Index (lineChars, (uint)(end - startOffset), ref curIndex, ref byteIndex);
					
					result.Add (selectedForeground);
					
					Pango.AttrBackground attrBackground = new Pango.AttrBackground (Style.SelectionBg.Red, 
					                                                                Style.SelectionBg.Green, 
					                                                                Style.SelectionBg.Blue);
					attrBackground.StartIndex = selectedForeground.StartIndex;
					attrBackground.EndIndex = selectedForeground.EndIndex;
					result.Add (attrBackground);

				});
			}
			result.SetAttributes ();
			return result;
		}