// 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);
}
}