2 #include "chimara-glk-private.h"
4 extern GPrivate *glk_data_key;
8 * @linkval: Set to nonzero to initiate hyperlink mode. Set to zero to disengage.
10 * Use this function to create hyperlinks in a textbuffer. It sets the current stream
11 * to hyperlink mode, after which text will become a hyperlink until hyperlink mode
12 * is turned off. If the current stream does not write to a textbuffer window, this function
15 * You can request hyperlink events with glk_request_hyperlink_event() to react
16 * to clicks on the link.
19 glk_set_hyperlink(glui32 linkval)
21 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
22 g_return_if_fail(glk_data->current_stream != NULL);
23 glk_set_hyperlink_stream(glk_data->current_stream, linkval);
27 * glk_set_hyperlink_stream:
28 * @str: The stream to set the hyperlink mode on.
29 * @linkval: Set to nonzero to initiate hyperlink mode. Set to zero to disengage.
31 * Use this function to create hyperlinks in a textbuffer. It sets a stream to a textbuffer
32 * window to hyperlink mode, after which text will become a hyperlink until hyperlink mode
33 * is turned off. Calling this function on a stream that does not write to a textbuffer does
36 * You can request hyperlink events with glk_request_hyperlink_event() to react
37 * to clicks on the link.
40 glk_set_hyperlink_stream(strid_t str, glui32 linkval)
42 g_return_if_fail(str != NULL);
43 g_return_if_fail(str->window != NULL);
44 g_return_if_fail(str->window->type == wintype_TextBuffer);
46 flush_window_buffer(str->window);
49 /* Turn off hyperlink mode */
50 str->hyperlink_mode = FALSE;
51 str->window->current_hyperlink = NULL;
55 /* Check whether a tag with the needed value already exists */
56 hyperlink_t *new_hyperlink = g_hash_table_lookup(str->window->hyperlinks, &linkval);
57 if(new_hyperlink == NULL) {
58 /* Create a new hyperlink with the requested value */
59 new_hyperlink = g_new0(struct hyperlink, 1);
60 new_hyperlink->value = linkval;
61 new_hyperlink->tag = gtk_text_tag_new(NULL);
62 new_hyperlink->event_handler = g_signal_connect( new_hyperlink->tag, "event", G_CALLBACK(on_hyperlink_clicked), new_hyperlink );
63 g_signal_handler_block(new_hyperlink->tag, new_hyperlink->event_handler);
64 new_hyperlink->window = str->window;
66 /* Add the new tag to the tag table of the textbuffer */
67 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(str->window->widget) );
68 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(textbuffer);
69 gtk_text_tag_table_add(tags, new_hyperlink->tag);
71 printf("inserting link %d\n", linkval);
73 gint *linkval_pointer = g_new0(gint, 1);
74 *linkval_pointer = linkval;
75 g_hash_table_insert(str->window->hyperlinks, linkval_pointer, new_hyperlink);
78 str->hyperlink_mode = TRUE;
79 str->window->current_hyperlink = new_hyperlink;
82 /* Internal function used to iterate over all the hyperlinks, unblocking the event handler */
84 hyperlink_unblock_event_handler(gpointer key, gpointer value, gpointer user_data)
86 hyperlink_t *link = (hyperlink_t *) value;
87 g_signal_handler_unblock(link->tag, link->event_handler);
88 printf("unblocking link %d\n", link->value);
91 /* Internal function used to iterate over all the hyperlinks, blocking the event handler */
93 hyperlink_block_event_handler(gpointer key, gpointer value, gpointer user_data)
95 hyperlink_t *link = (hyperlink_t *) value;
96 g_signal_handler_block(link->tag, link->event_handler);
100 glk_request_hyperlink_event(winid_t win)
102 VALID_WINDOW(win, return);
103 g_return_if_fail(win != NULL);
104 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
106 g_hash_table_foreach(win->hyperlinks, hyperlink_unblock_event_handler, NULL);
111 glk_cancel_hyperlink_event(winid_t win)
113 VALID_WINDOW(win, return);
114 g_return_if_fail(win != NULL);
115 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
117 g_hash_table_foreach(win->hyperlinks, hyperlink_block_event_handler, NULL);
121 on_hyperlink_clicked(GtkTextTag *tag, GObject *object, GdkEvent *event, GtkTextIter *iter, hyperlink_t *link)
123 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(link->window->widget, CHIMARA_TYPE_GLK));
126 if(event->type == GDK_BUTTON_PRESS) {
127 event_throw(glk, evtype_Hyperlink, link->window, link->value, 0);