string AdjustColors(string markup) { StringBuilder result = new StringBuilder(); int idx = markup.IndexOf("foreground=\""); int offset = 0; Gdk.Color baseColor; // This is a workaround for Bug 559804 - Strings in search result pad are near-invisible // On mac it's not possible to get the white background color with the Base or Background // methods. If this bug is fixed or a better work around is found - remove this hack. if (Platform.IsMac) { { baseColor = treeviewSearchResults.Style.Light(treeviewSearchResults.State); } } else { baseColor = treeviewSearchResults.Style.Base(treeviewSearchResults.State); } while (idx > 0) { idx += "foreground=\"".Length; result.Append(markup.Substring(offset, idx - offset)); if (idx + 7 >= markup.Length) { offset = idx; break; } offset = idx + 7; string colorStr = markup.Substring(idx, 7); Gdk.Color color = Gdk.Color.Zero; if (Gdk.Color.Parse(colorStr, ref color)) { colorStr = SyntaxMode.ColorToPangoMarkup(AdjustColor(baseColor, color)); } result.Append(colorStr); idx = markup.IndexOf("foreground=\"", idx); } result.Append(markup.Substring(offset, markup.Length - offset)); return(result.ToString()); }
// 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; } int lineNr = doc.OffsetToLineNumber(searchResult.Offset); DocumentLine line = doc.GetLine(lineNr); if (line == null) { textRenderer.Markup = "Invalid line number " + lineNr + " from offset: " + searchResult.Offset; return; } bool isSelected = treeviewSearchResults.Selection.IterIsSelected(iter); int indent = line.GetIndentation(doc).Length; var data = new Mono.TextEditor.TextEditorData(doc); data.ColorStyle = highlightStyle; string markup = doc.SyntaxMode != null? data.GetMarkup(line.Offset + indent, line.Length - indent, true, !isSelected, false) : GLib.Markup.EscapeText(doc.GetTextAt(line.Offset, line.Length)); if (!isSelected) { int col = searchResult.Offset - line.Offset - indent; string tag; int pos1 = FindPosition(markup, col, out tag); int pos2 = FindPosition(markup, col + searchResult.Length, out tag); if (pos1 >= 0 && pos2 >= 0) { markup = tag.StartsWith("span") ? markup.Insert(pos2, "</span></span><" + tag + ">") : markup.Insert(pos2, "</span>"); Color searchColor = Mono.TextEditor.Highlighting.ColorScheme.ToGdkColor(highlightStyle.SearchTextBg); double b1 = Mono.TextEditor.HslColor.Brightness(searchColor); double b2 = Mono.TextEditor.HslColor.Brightness(AdjustColor(Style.Base(StateType.Normal), highlightStyle.Default.Color)); double delta = Math.Abs(b1 - b2); if (delta < 0.1) { Mono.TextEditor.HslColor color1 = highlightStyle.SearchTextBg; if (color1.L + 0.5 > 1.0) { color1.L -= 0.5; } else { color1.L += 0.5; } searchColor = color1; } markup = markup.Insert(pos1, "<span background=\"" + SyntaxMode.ColorToPangoMarkup(searchColor) + "\">"); } } string markupText = AdjustColors(markup.Replace("\t", new string (' ', TextEditorOptions.DefaultOptions.TabSize))); try { textRenderer.Markup = markupText; } catch (Exception e) { LoggingService.LogError("Error whil setting the text renderer markup to: " + markup, e); } }
void ResultTextDataFunc(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter) { if (TreeIter.Zero.Equals(iter)) { return; } CellRendererText textRenderer = (CellRendererText)cell; SearchResult searchResult = (SearchResult)store.GetValue(iter, SearchResultColumn); if (searchResult == null) { return; } Mono.TextEditor.Document doc = GetDocument(searchResult); int lineNr = doc.OffsetToLineNumber(searchResult.Offset); LineSegment line = doc.GetLine(lineNr); bool isSelected = treeviewSearchResults.Selection.IterIsSelected(iter); string markup; if (doc.SyntaxMode != null) { markup = doc.SyntaxMode.GetMarkup(doc, new TextEditorOptions(), highlightStyle, line.Offset, line.EditableLength, true, !isSelected, false); } else { markup = GLib.Markup.EscapeText(doc.GetTextAt(line.Offset, line.EditableLength)); } if (!isSelected) { int col = searchResult.Offset - line.Offset; string tag; int pos1 = FindPosition(markup, col, out tag); int pos2 = FindPosition(markup, col + searchResult.Length, out tag); if (pos1 >= 0 && pos2 >= 0) { if (tag.StartsWith("span")) { markup = markup.Insert(pos2, "</span></span><" + tag + ">"); } else { markup = markup.Insert(pos2, "</span>"); } Gdk.Color searchColor = highlightStyle.SearchTextBg; double b1 = HslColor.Brightness(searchColor); double b2 = HslColor.Brightness(AdjustColor(Style.Base(StateType.Normal), highlightStyle.Default.Color)); double delta = Math.Abs(b1 - b2); if (delta < 0.1) { HslColor color1 = highlightStyle.SearchTextBg; if (color1.L + 0.5 > 1.0) { color1.L -= 0.5; } else { color1.L += 0.5; } searchColor = color1; } markup = markup.Insert(pos1, "<span background=\"" + SyntaxMode.ColorToPangoMarkup(searchColor) + "\">"); } } string markupText = AdjustColors(markup.Replace("\t", new string (' ', TextEditorOptions.DefaultOptions.TabSize))); try { textRenderer.Markup = markupText; } catch (Exception e) { LoggingService.LogError("Error whil setting the text renderer markup to: " + markup, e); } }