Fixed freeing data at stop & start. Previously the root window was set to NULL when...
[rodin/chimara.git] / libchimara / hyperlink.c
1 #include "hyperlink.h"
2 #include "chimara-glk-private.h"
3 #include "magic.h"
4
5 extern GPrivate *glk_data_key;
6
7 /**
8  * glk_set_hyperlink:
9  * @linkval: Set to nonzero to initiate hyperlink mode. Set to zero to disengage.
10  *
11  * Use this function to create hyperlinks in a textbuffer. It sets the current stream
12  * to hyperlink mode, after which text will become a hyperlink until hyperlink mode
13  * is turned off. If the current stream does not write to a textbuffer window, this function
14  * does nothing.
15  *
16  * You can request hyperlink events with glk_request_hyperlink_event() to react
17  * to clicks on the link.
18  */
19 void 
20 glk_set_hyperlink(glui32 linkval)
21 {
22         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
23         g_return_if_fail(glk_data->current_stream != NULL);
24         glk_set_hyperlink_stream(glk_data->current_stream, linkval);
25 }
26
27 /**
28  * glk_set_hyperlink_stream:
29  * @str: The stream to set the hyperlink mode on.
30  * @linkval: Set to nonzero to initiate hyperlink mode. Set to zero to disengage.
31  *
32  * Use this function to create hyperlinks in a textbuffer. It sets a stream to a textbuffer
33  * window to hyperlink mode, after which text will become a hyperlink until hyperlink mode
34  * is turned off. Calling this function on a stream that does not write to a textbuffer does
35  * nothing.
36  *
37  * You can request hyperlink events with glk_request_hyperlink_event() to react
38  * to clicks on the link.
39  */
40 void 
41 glk_set_hyperlink_stream(strid_t str, glui32 linkval)
42 {
43         g_return_if_fail(str != NULL);
44         g_return_if_fail(str->window != NULL);
45         g_return_if_fail(str->window->type == wintype_TextBuffer);
46
47         flush_window_buffer(str->window);
48
49         if(linkval == 0) {
50                 /* Turn off hyperlink mode */
51                 str->hyperlink_mode = FALSE;
52                 str->window->current_hyperlink = NULL;
53                 return;
54         }
55
56         /* Check whether a tag with the needed value already exists */
57         hyperlink_t *new_hyperlink = g_hash_table_lookup(str->window->hyperlinks, &linkval);
58         if(new_hyperlink == NULL) {
59                 /* Create a new hyperlink with the requested value */
60                 new_hyperlink = g_new0(struct hyperlink, 1);
61                 new_hyperlink->value = linkval;
62                 new_hyperlink->tag = gtk_text_tag_new(NULL);
63                 new_hyperlink->event_handler = g_signal_connect( new_hyperlink->tag, "event", G_CALLBACK(on_hyperlink_clicked), new_hyperlink );
64                 g_signal_handler_block(new_hyperlink->tag, new_hyperlink->event_handler);
65                 new_hyperlink->window = str->window;
66
67                 /* Add the new tag to the tag table of the textbuffer */
68                 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(str->window->widget) );
69                 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(textbuffer);
70                 gtk_text_tag_table_add(tags, new_hyperlink->tag);
71
72                 printf("inserting link %d\n", linkval);
73
74                 gint *linkval_pointer = g_new0(gint, 1);
75                 *linkval_pointer = linkval;
76                 g_hash_table_insert(str->window->hyperlinks, linkval_pointer, new_hyperlink);
77         }
78
79         str->hyperlink_mode = TRUE;
80         str->window->current_hyperlink = new_hyperlink;
81 }
82
83 /* Internal function used to iterate over all the hyperlinks, unblocking the event handler */
84 void
85 hyperlink_unblock_event_handler(gpointer key, gpointer value, gpointer user_data)
86 {
87         hyperlink_t *link = (hyperlink_t *) value;
88         g_signal_handler_unblock(link->tag, link->event_handler);
89         printf("unblocking link %d\n", link->value);
90 }
91
92 /* Internal function used to iterate over all the hyperlinks, blocking the event handler */
93 void
94 hyperlink_block_event_handler(gpointer key, gpointer value, gpointer user_data)
95 {
96         hyperlink_t *link = (hyperlink_t *) value;
97         g_signal_handler_block(link->tag, link->event_handler);
98 }
99
100 void 
101 glk_request_hyperlink_event(winid_t win)
102 {
103         VALID_WINDOW(win, return);
104         g_return_if_fail(win != NULL);
105         g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
106
107         g_hash_table_foreach(win->hyperlinks, hyperlink_unblock_event_handler, NULL);
108
109 }
110
111 void 
112 glk_cancel_hyperlink_event(winid_t win)
113 {
114         VALID_WINDOW(win, return);
115         g_return_if_fail(win != NULL);
116         g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
117
118         g_hash_table_foreach(win->hyperlinks, hyperlink_block_event_handler, NULL);
119 }
120
121 gboolean
122 on_hyperlink_clicked(GtkTextTag *tag, GObject *object, GdkEvent *event, GtkTextIter *iter, hyperlink_t *link)
123 {
124         ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(link->window->widget, CHIMARA_TYPE_GLK));
125         g_assert(glk);
126
127         if(event->type == GDK_BUTTON_PRESS) {
128                 event_throw(glk, evtype_Hyperlink, link->window, link->value, 0);
129         }
130
131         return FALSE;
132 }