Commit 9100a6de authored by Alberto Fanjul's avatar Alberto Fanjul

Configurable color for links

parent 2ea23c0c
......@@ -21,4 +21,4 @@ A port in vala to https://gitlab.gnome.org/GNOME/gtk/blob/gtk-3-24/demos/gtk-dem
## Example
./TextViewHyperLinks --custom-link '!([0-9]+),https://gitlab.gnome.org/GNOME/gitg/merge_requests/\\1,orange' --custom-link '#([0-9]+),https://gitlab.gnome.org/GNOME/gitg/issues/\\1,green' --text "this is a text with an url https://gitlab.gnome.org/GNOME/gitg/issues/152 and a hidden link #152 and hidden MR !21 and documentation http://example.com/doc?param=1&chapter=4 for sure'
./TextViewHyperLinks --custom-link '!([0-9]+),https://gitlab.gnome.org/GNOME/gitg/merge_requests/\\1,orange' --custom-link '#([0-9]+),https://gitlab.gnome.org/GNOME/gitg/issues/\\1,green' --text "this is a text with an url https://gitlab.gnome.org/GNOME/gitg/issues/152 and a hidden link #152 and hidden MR !21 and documentation http://example.com/doc?param=1&chapter=4 for sure' --link-color "#0000ee"
public class TextViewHyperLinksApp : Gtk.Application {
private static string? text = null;
private static string? link_color = null;
[CCode (array_length = false, array_null_terminated = true)]
private static string[]? custom_links = null;
private const GLib.OptionEntry[] options = {
{ "text", 0, 0, OptionArg.STRING, ref text, "Provide text to parse for urls", "TEXT" },
{ "custom-link", 0, 0, OptionArg.STRING_ARRAY, ref custom_links, "Provide pattern to transform custom links", "REGEX,REPLACEMENT,COLOR" },
{ "text", 0, 0, OptionArg.STRING, ref text, "Text to parse for urls", "TEXT" },
{ "link-color", 0, 0, OptionArg.STRING, ref link_color, "Color name or #rgb for links", "COLOR" },
{ "custom-link", 0, 0, OptionArg.STRING_ARRAY, ref custom_links, "Pattern to transform custom links", "REGEX,REPLACEMENT,COLOR" },
{ null }
};
......@@ -15,13 +17,16 @@ public class TextViewHyperLinksApp : Gtk.Application {
}
protected override void activate () {
new TextViewHyperLinksWindow (this, text, custom_links).show_all ();
if (link_color == null) {
link_color = " #ffffffff";
}
new TextViewHyperLinksWindow (this, text, custom_links, link_color).show_all ();
}
}
class TextViewHyperLinksWindow: Gtk.ApplicationWindow {
internal TextViewHyperLinksWindow(TextViewHyperLinksApp app, string? text, string[]? custom_links) {
internal TextViewHyperLinksWindow(TextViewHyperLinksApp app, string? text, string[]? custom_links, string link_color) {
Object (application: app, title: "TextView Example");
this.set_default_size (220, 200);
......@@ -42,11 +47,16 @@ class TextViewHyperLinksWindow: Gtk.ApplicationWindow {
}
buffer.text = text;
apply_link_tags(buffer, "https?://[a-zA-Z/&#.0-9=?]+", null, "blue");
apply_link_tags(buffer, "https?://[a-zA-Z/&#.0-9=?]+", null, link_color);
foreach (string custom_link in custom_links)
{
string[] params = custom_link.split(",");
apply_link_tags(buffer, params[0], params[1], params[2]);
string color = link_color;
if (params.length == 3)
{
color = params[2];
}
apply_link_tags(buffer, params[0], params[1], color);
}
var scrolled_window = new Gtk.ScrolledWindow (null, null);
......@@ -185,7 +195,7 @@ public void follow_if_link(Gtk.Widget texview, Gtk.TextIter iter)
}
public void apply_link_tags(Gtk.TextBuffer buffer, string regex_str, string? replacement, string color)
public void apply_link_tags(Gtk.TextBuffer buffer, string regex_str, string? replacement, string link_color)
{
try {
GLib.MatchInfo matchInfo;
......@@ -202,7 +212,17 @@ public void apply_link_tags(Gtk.TextBuffer buffer, string regex_str, string? rep
buffer.get_iter_at_offset(out start, start_pos);
buffer.get_iter_at_offset(out end, end_pos);
var tag = buffer.create_tag(null, "foreground", color, "underline", Pango.Underline.SINGLE);
var tag = buffer.create_tag(null, "underline", Pango.Underline.SINGLE);
if (link_color.contains("#"))
{
var rgba = Gdk.RGBA();
rgba.parse(link_color);
tag.foreground_rgba = rgba;
}
else
{
tag.foreground = link_color;
}
tag.set_data("type", "url");
if (replacement != null)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment