From 85d194e349858b3cbbb4dd2a8ca007428378c56c Mon Sep 17 00:00:00 2001 From: fiaxh Date: Tue, 18 Sep 2018 21:28:56 +0200 Subject: Use Label instead of TextView for message display --- main/src/ui/util/helper.vala | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) (limited to 'main/src/ui/util') diff --git a/main/src/ui/util/helper.vala b/main/src/ui/util/helper.vala index 4e9e942d..06715ad6 100644 --- a/main/src/ui/util/helper.vala +++ b/main/src/ui/util/helper.vala @@ -138,13 +138,39 @@ public static bool is_24h_format() { return settings_format == "24h" || p_format == " "; } -// Workaround GTK TextView issues -public static void force_alloc_width(Widget widget, int width) { - Allocation alloc = Allocation(); - widget.get_preferred_width(out alloc.width, null); - widget.get_preferred_height(out alloc.height, null); - alloc.width = width; - widget.size_allocate(alloc); +public static string make_word_bold_markup(string s, string word) { + string ret = s; + int elongated_by = 0; + Regex highlight_regex = new Regex("\\b" + Regex.escape_string(word.down()) + "\\b"); + MatchInfo match_info; + string markup_text_bak = s.down(); + highlight_regex.match(markup_text_bak, 0, out match_info); + for (; match_info.matches(); match_info.next()) { + int start, end; + match_info.fetch_pos(0, out start, out end); + ret = ret[0:start+elongated_by] + "" + ret[start+elongated_by:end+elongated_by] + "" + ret[end+elongated_by:ret.length]; + elongated_by += 7; + } + markup_text_bak += ""; // We need markup_text_bak to live until here because url_regex.match does not copy the string + return ret; +} + +public static string make_link_markup(string s) { + string ret = s; + Regex url_regex = new Regex("""(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))"""); + int elongated_by = 0; + MatchInfo match_info; + string markup_text_bak = ret.down(); + url_regex.match(markup_text_bak, 0, out match_info); + for (; match_info.matches(); match_info.next()) { + int start, end; + match_info.fetch_pos(0, out start, out end); + string link = ret[start+elongated_by:end+elongated_by]; + ret = ret[0:start+elongated_by] + "" + link + "" + ret[end+elongated_by:ret.length]; + elongated_by += 15 + link.length; + } + markup_text_bak += ""; // We need markup_text_bak to live until here because url_regex.match does not copy the string + return ret; } } -- cgit v1.2.3-54-g00ecf