Commit 2ea23c0c authored by Alberto Fanjul's avatar Alberto Fanjul

Custom links

parent 95d5d99c
...@@ -19,6 +19,6 @@ A port in vala to https://gitlab.gnome.org/GNOME/gtk/blob/gtk-3-24/demos/gtk-dem ...@@ -19,6 +19,6 @@ A port in vala to https://gitlab.gnome.org/GNOME/gtk/blob/gtk-3-24/demos/gtk-dem
ninja -C build ninja -C build
./build/TextViewHyperLinks ./build/TextViewHyperLinks
## Arguments ## Example
If you want to test different texts, you can pass a `--text` argument. ./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'
public class TextViewHyperLinksApp : Gtk.Application { public class TextViewHyperLinksApp : Gtk.Application {
private static string? text; private static string? text = null;
[CCode (array_length = false, array_null_terminated = true)]
private static string[]? custom_links = null;
private const GLib.OptionEntry[] options = { private const GLib.OptionEntry[] options = {
{ "text", 0, 0, OptionArg.STRING, ref text, "Provide text to parse for urls", "TEXT" }, { "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" },
{ null } { null }
}; };
...@@ -12,13 +15,13 @@ public class TextViewHyperLinksApp : Gtk.Application { ...@@ -12,13 +15,13 @@ public class TextViewHyperLinksApp : Gtk.Application {
} }
protected override void activate () { protected override void activate () {
new TextViewHyperLinksWindow (this, text).show_all (); new TextViewHyperLinksWindow (this, text, custom_links).show_all ();
} }
} }
class TextViewHyperLinksWindow: Gtk.ApplicationWindow { class TextViewHyperLinksWindow: Gtk.ApplicationWindow {
internal TextViewHyperLinksWindow(TextViewHyperLinksApp app, string? text) { internal TextViewHyperLinksWindow(TextViewHyperLinksApp app, string? text, string[]? custom_links) {
Object (application: app, title: "TextView Example"); Object (application: app, title: "TextView Example");
this.set_default_size (220, 200); this.set_default_size (220, 200);
...@@ -33,17 +36,18 @@ class TextViewHyperLinksWindow: Gtk.ApplicationWindow { ...@@ -33,17 +36,18 @@ class TextViewHyperLinksWindow: Gtk.ApplicationWindow {
textview.set_wrap_mode (Gtk.WrapMode.WORD); //sets line wrapping textview.set_wrap_mode (Gtk.WrapMode.WORD); //sets line wrapping
if (text != null) if (text == null)
{ {
message("not is null"); text = "";
buffer.text = text;
} else {
buffer.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";
} }
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, "blue");
apply_link_tags(buffer, "#([0-9]+)", "https://gitlab.gnome.org/GNOME/gitg/issues/\\1", "green"); foreach (string custom_link in custom_links)
apply_link_tags(buffer, "!([0-9]+)", "https://gitlab.gnome.org/GNOME/gitg/merge_requests/\\1", "orange"); {
string[] params = custom_link.split(",");
apply_link_tags(buffer, params[0], params[1], params[2]);
}
var scrolled_window = new Gtk.ScrolledWindow (null, null); var scrolled_window = new Gtk.ScrolledWindow (null, null);
scrolled_window.set_policy (Gtk.PolicyType.AUTOMATIC, scrolled_window.set_policy (Gtk.PolicyType.AUTOMATIC,
...@@ -66,7 +70,7 @@ private bool on_motion_notify_event (Gtk.Widget widget, Gdk.EventMotion evt) ...@@ -66,7 +70,7 @@ private bool on_motion_notify_event (Gtk.Widget widget, Gdk.EventMotion evt)
int x, y; int x, y;
Gtk.TextView textview = ((Gtk.TextView)widget); Gtk.TextView textview = ((Gtk.TextView)widget);
textview.window_to_buffer_coords (Gtk.TextWindowType.WIDGET, textview.window_to_buffer_coords (Gtk.TextWindowType.WIDGET,
(int)evt.x, (int)evt.y, out x, out y); (int)evt.x, (int)evt.y, out x, out y);
...@@ -76,7 +80,7 @@ private bool on_motion_notify_event (Gtk.Widget widget, Gdk.EventMotion evt) ...@@ -76,7 +80,7 @@ private bool on_motion_notify_event (Gtk.Widget widget, Gdk.EventMotion evt)
if (textview.get_iter_at_location (out iter, x, y)) if (textview.get_iter_at_location (out iter, x, y))
{ {
var tags = iter.get_tags (); var tags = iter.get_tags ();
foreach (Gtk.TextTag tag in tags) foreach (Gtk.TextTag tag in tags)
{ {
if (tag.get_data<string>("type") == "url") if (tag.get_data<string>("type") == "url")
{ {
......
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