1 | /* Links |
2 | * |
3 | * GtkLabel can show hyperlinks. The default action is to call |
4 | * gtk_show_uri() on their URI, but it is possible to override |
5 | * this with a custom handler. |
6 | */ |
7 | |
8 | #include <gtk/gtk.h> |
9 | |
10 | static void |
11 | response_cb (GtkWidget *dialog, |
12 | int response_id, |
13 | gpointer data) |
14 | { |
15 | gtk_window_destroy (GTK_WINDOW (dialog)); |
16 | } |
17 | |
18 | static gboolean |
19 | activate_link (GtkWidget *label, |
20 | const char *uri, |
21 | gpointer data) |
22 | { |
23 | if (g_strcmp0 (str1: uri, str2: "keynav" ) == 0) |
24 | { |
25 | GtkWidget *dialog; |
26 | GtkWidget *parent; |
27 | |
28 | parent = GTK_WIDGET (gtk_widget_get_root (label)); |
29 | dialog = gtk_message_dialog_new_with_markup (GTK_WINDOW (parent), |
30 | flags: GTK_DIALOG_DESTROY_WITH_PARENT, |
31 | type: GTK_MESSAGE_INFO, |
32 | buttons: GTK_BUTTONS_OK, |
33 | message_format: "Keyboard navigation" ); |
34 | gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (dialog), |
35 | message_format: "The term <i>keynav</i> is a shorthand for " |
36 | "keyboard navigation and refers to the process of using " |
37 | "a program (exclusively) via keyboard input." ); |
38 | gtk_window_set_modal (GTK_WINDOW (dialog), TRUE); |
39 | |
40 | gtk_window_present (GTK_WINDOW (dialog)); |
41 | g_signal_connect (dialog, "response" , G_CALLBACK (response_cb), NULL); |
42 | |
43 | return TRUE; |
44 | } |
45 | |
46 | return FALSE; |
47 | } |
48 | |
49 | GtkWidget * |
50 | do_links (GtkWidget *do_widget) |
51 | { |
52 | static GtkWidget *window = NULL; |
53 | GtkWidget *label; |
54 | |
55 | if (!window) |
56 | { |
57 | window = gtk_window_new (); |
58 | gtk_window_set_display (GTK_WINDOW (window), |
59 | display: gtk_widget_get_display (widget: do_widget)); |
60 | gtk_window_set_title (GTK_WINDOW (window), title: "Links" ); |
61 | gtk_window_set_resizable (GTK_WINDOW (window), FALSE); |
62 | g_object_add_weak_pointer (G_OBJECT (window), weak_pointer_location: (gpointer *)&window); |
63 | |
64 | label = gtk_label_new (str: "Some <a href=\"http://en.wikipedia.org/wiki/Text\"" |
65 | "title=\"plain text\">text</a> may be marked up " |
66 | "as hyperlinks, which can be clicked " |
67 | "or activated via <a href=\"keynav\">keynav</a> " |
68 | "and they work fine with other markup, like when " |
69 | "searching on <a href=\"http://www.google.com/\">" |
70 | "<span color=\"#0266C8\">G</span><span color=\"#F90101\">o</span>" |
71 | "<span color=\"#F2B50F\">o</span><span color=\"#0266C8\">g</span>" |
72 | "<span color=\"#00933B\">l</span><span color=\"#F90101\">e</span>" |
73 | "</a>." ); |
74 | gtk_label_set_use_markup (GTK_LABEL (label), TRUE); |
75 | gtk_label_set_max_width_chars (GTK_LABEL (label), n_chars: 40); |
76 | gtk_label_set_wrap (GTK_LABEL (label), TRUE); |
77 | gtk_label_set_wrap_mode (GTK_LABEL (label), wrap_mode: PANGO_WRAP_WORD); |
78 | g_signal_connect (label, "activate-link" , G_CALLBACK (activate_link), NULL); |
79 | gtk_widget_set_margin_start (widget: label, margin: 20); |
80 | gtk_widget_set_margin_end (widget: label, margin: 20); |
81 | gtk_widget_set_margin_top (widget: label, margin: 20); |
82 | gtk_widget_set_margin_bottom (widget: label, margin: 20); |
83 | gtk_window_set_child (GTK_WINDOW (window), child: label); |
84 | gtk_widget_show (widget: label); |
85 | } |
86 | |
87 | if (!gtk_widget_get_visible (widget: window)) |
88 | gtk_widget_show (widget: window); |
89 | else |
90 | gtk_window_destroy (GTK_WINDOW (window)); |
91 | |
92 | return window; |
93 | } |
94 | |