Debian Codegraph @

demos/gtk-demo/links.c

raw
1 /* Links
2 *
3 * GtkLabel can show hyperlinks. The default action is to call
4 * gtk_show_uri_on_window() 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 gint response_id,
13 gpointer data)
14 {
15 gtk_widget_destroy (dialog);
16 }
17
18 static gboolean
19 activate_link (GtkWidget *label,
20 const gchar *uri,
21 gpointer data)
22 {
23 if (g_strcmp0 (uri, "keynav") == 0)
24 {
25 GtkWidget *dialog;
26 GtkWidget *parent;
27
28 parent = gtk_widget_get_toplevel (label);
29 dialog = gtk_message_dialog_new_with_markup (GTK_WINDOW (parent),
30 GTK_DIALOG_DESTROY_WITH_PARENT,
31 GTK_MESSAGE_INFO,
32 GTK_BUTTONS_OK,
33 "The term <i>keynav</i> is a shorthand for "
34 "keyboard navigation and refers to the process of using "
35 "a program (exclusively) via keyboard input.");
36 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
37
38 gtk_window_present (GTK_WINDOW (dialog));
39 g_signal_connect (dialog, "response", G_CALLBACK (response_cb), NULL);
40
41 return TRUE;
42 }
43
44 return FALSE;
45 }
46
47 GtkWidget *
48 do_links (GtkWidget *do_widget)
49 {
50 static GtkWidget *window = NULL;
51 GtkWidget *label;
52
53 if (!window)
54 {
55 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
56 gtk_window_set_screen (GTK_WINDOW (window),
57 gtk_widget_get_screen (do_widget));
58 gtk_window_set_title (GTK_WINDOW (window), "Links");
59 gtk_container_set_border_width (GTK_CONTAINER (window), 12);
60 g_signal_connect (window, "destroy",
61 G_CALLBACK (gtk_widget_destroyed), &window);
62
63 label = gtk_label_new ("Some <a href=\"http://en.wikipedia.org/wiki/Text\""
64 "title=\"plain text\">text</a> may be marked up\n"
65 "as hyperlinks, which can be clicked\n"
66 "or activated via <a href=\"keynav\">keynav</a>\n"
67 "and they work fine with other markup, like when\n"
68 "searching on <a href=\"http://www.google.com/\">"
69 "<span color=\"#0266C8\">G</span><span color=\"#F90101\">o</span>"
70 "<span color=\"#F2B50F\">o</span><span color=\"#0266C8\">g</span>"
71 "<span color=\"#00933B\">l</span><span color=\"#F90101\">e</span>"
72 "</a>.");
73 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
74 g_signal_connect (label, "activate-link", G_CALLBACK (activate_link), NULL);
75 gtk_container_add (GTK_CONTAINER (window), label);
76 gtk_widget_show (label);
77 }
78
79 if (!gtk_widget_get_visible (window))
80 gtk_widget_show (window);
81 else
82 gtk_widget_destroy (window);
83
84 return window;
85 }