Beginning work on graphic windows. Hopelessly broken still....
[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  * This call sets the current link value in the current output stream. See
12  * glk_set_hyperlink_stream().
13  */
14 void 
15 glk_set_hyperlink(glui32 linkval)
16 {
17         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
18         g_return_if_fail(glk_data->current_stream != NULL);
19         glk_set_hyperlink_stream(glk_data->current_stream, linkval);
20 }
21
22 /**
23  * glk_set_hyperlink_stream:
24  * @str: The stream to set the hyperlink mode on.
25  * @linkval: Set to non-zero to initiate hyperlink mode. Set to zero to 
26  * disengage.
27  *
28  * This call sets the current link value on the specified output stream. A link
29  * value is any non-zero integer; zero indicates no link. Subsequent text output
30  * is considered to make up the body of the link, which continues until the link
31  * value is changed (or set to zero).
32  *
33  * Note that it is almost certainly useless to change the link value of a stream
34  * twice with no intervening text. The result will be a zero-length link, which 
35  * the player probably cannot see or select; the library may optimize it out 
36  * entirely.
37  *
38  * Setting the link value of a stream to the value it already has, has no 
39  * effect.
40  *
41  * If the library supports images, they take on the current link value as they 
42  * are output, just as text does. The player can select an image in a link just 
43  * as he does text. (This includes margin-aligned images, which can lead to some 
44  * peculiar situations, since a right-margin image may not appear directly 
45  * adjacent to the text it was output with.)
46  * 
47  * The library will attempt to display links in some distinctive way (and it 
48  * will do this whether or not hyperlink input has actually been requested for 
49  * the window). Naturally, blue underlined text is most likely. Link images may 
50  * not be distinguished from non-link images, so it is best not to use a 
51  * particular image both ways. 
52  */
53 void 
54 glk_set_hyperlink_stream(strid_t str, glui32 linkval)
55 {
56         g_return_if_fail(str != NULL);
57         g_return_if_fail(str->type == STREAM_TYPE_WINDOW);
58         g_return_if_fail(str->window != NULL);
59         g_return_if_fail(str->window->type == wintype_TextBuffer || str->window->type == wintype_TextGrid);
60
61         flush_window_buffer(str->window);
62
63         if(linkval == 0) {
64                 /* Turn off hyperlink mode */
65                 str->hyperlink_mode = FALSE;
66                 str->window->current_hyperlink = NULL;
67                 return;
68         }
69
70         /* Check whether a tag with the needed value already exists */
71         hyperlink_t *new_hyperlink = g_hash_table_lookup(str->window->hyperlinks, &linkval);
72         if(new_hyperlink == NULL) {
73                 /* Create a new hyperlink with the requested value */
74                 new_hyperlink = g_new0(struct hyperlink, 1);
75                 new_hyperlink->value = linkval;
76                 new_hyperlink->tag = gtk_text_tag_new(NULL);
77                 new_hyperlink->event_handler = g_signal_connect( new_hyperlink->tag, "event", G_CALLBACK(on_hyperlink_clicked), new_hyperlink );
78                 g_signal_handler_block(new_hyperlink->tag, new_hyperlink->event_handler);
79                 new_hyperlink->window = str->window;
80
81                 /* Add the new tag to the tag table of the textbuffer */
82                 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(str->window->widget) );
83                 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(textbuffer);
84                 gtk_text_tag_table_add(tags, new_hyperlink->tag);
85
86                 gint *linkval_pointer = g_new0(gint, 1);
87                 *linkval_pointer = linkval;
88                 g_hash_table_insert(str->window->hyperlinks, linkval_pointer, new_hyperlink);
89         }
90
91         str->hyperlink_mode = TRUE;
92         str->window->current_hyperlink = new_hyperlink;
93 }
94
95 /* Internal function used to iterate over all the hyperlinks, unblocking the event handler */
96 void
97 hyperlink_unblock_event_handler(gpointer key, gpointer value, gpointer user_data)
98 {
99         hyperlink_t *link = (hyperlink_t *) value;
100         g_signal_handler_unblock(link->tag, link->event_handler);
101 }
102
103 /* Internal function used to iterate over all the hyperlinks, blocking the event handler */
104 void
105 hyperlink_block_event_handler(gpointer key, gpointer value, gpointer user_data)
106 {
107         hyperlink_t *link = (hyperlink_t *) value;
108         g_signal_handler_block(link->tag, link->event_handler);
109 }
110
111 /**
112  * glk_request_hyperlink_event:
113  * @win: The window to request a hyperlink event on.
114  *
115  * This call works like glk_request_char_event(), glk_request_line_event() and
116  * glk_request_mouse_event(). A pending request on a window remains pending 
117  * until the player selects a link, or the request is cancelled.
118  *
119  * A window can have hyperlink input and mouse, character, or line input pending
120  * at the same time. However, if hyperlink and mouse input are requested at the
121  * same time, the library may not provide an intuitive way for the player to 
122  * distingish which a mouse click represents. Therefore, this combination should
123  * be avoided.
124  *
125  * When a link is selected in a window with a pending request, glk_select() will
126  * return an event of type %evtype_Hyperlink. In the event structure, @win tells
127  * what window the event came from, and @val1 gives the (non-zero) link value.
128  * 
129  * If no hyperlink request is pending in a window, the library will ignore 
130  * attempts to select a link. No %evtype_Hyperlink event will be generated 
131  * unless it has been requested. 
132  */
133 void 
134 glk_request_hyperlink_event(winid_t win)
135 {
136         VALID_WINDOW(win, return);
137         g_return_if_fail(win != NULL);
138         g_return_if_fail(win->type == wintype_TextBuffer || win->type == wintype_TextGrid);
139
140         g_hash_table_foreach(win->hyperlinks, hyperlink_unblock_event_handler, NULL);
141
142 }
143
144 /**
145  * glk_cancel_hyperlink_event:
146  * @win: The window in which to cancel the hyperlink event request.
147  *
148  * This call works like glk_cancel_char_event(), glk_cancel_line_event(), and
149  * glk_cancel_mouse_event(). See glk_request_hyperlink_event().
150  */
151 void 
152 glk_cancel_hyperlink_event(winid_t win)
153 {
154         VALID_WINDOW(win, return);
155         g_return_if_fail(win != NULL);
156         g_return_if_fail(win->type == wintype_TextBuffer || win->type == wintype_TextGrid);
157
158         g_hash_table_foreach(win->hyperlinks, hyperlink_block_event_handler, NULL);
159 }
160
161 gboolean
162 on_hyperlink_clicked(GtkTextTag *tag, GObject *object, GdkEvent *event, GtkTextIter *iter, hyperlink_t *link)
163 {
164         ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(link->window->widget, CHIMARA_TYPE_GLK));
165         g_assert(glk);
166
167         if(event->type == GDK_BUTTON_PRESS) {
168                 event_throw(glk, evtype_Hyperlink, link->window, link->value, 0);
169         }
170
171         return FALSE;
172 }