3 /* Global tree of all windows */
4 static GNode *root_window = NULL;
8 * @win: A window, or #NULL.
9 * @rockptr: Return location for the next window's rock, or #NULL.
11 * Iterates over the list of windows; if @win is #NULL, it returns the first
12 * window, otherwise the next window after @win. If there are no more, it
13 * returns #NULL. The window's rock is stored in @rockptr. If you don't want
14 * the rocks to be returned, you may set @rockptr to #NULL.
16 * The order in which windows are returned is arbitrary. The root window is
17 * not necessarily first, nor is it necessarily last. The order may change
18 * every time you create or destroy a window, invalidating the iteration.
20 * Returns: the next window, or #NULL if there are no more.
23 glk_window_iterate(winid_t win, glui32 *rockptr)
28 retnode = root_window;
31 GNode *node = win->window_node;
32 if( G_NODE_IS_LEAF(node) )
34 while(node && node->next == NULL)
42 retnode = g_node_first_child(node);
44 winid_t retval = retnode? (winid_t)retnode->data : NULL;
46 /* Store the window's rock in rockptr */
48 *rockptr = glk_window_get_rock(retval);
54 * glk_window_get_rock:
57 * Returns the window @win's rock value. Pair windows always have rock 0.
59 * Returns: A rock value.
62 glk_window_get_rock(winid_t win)
64 g_return_val_if_fail(win != NULL, 0);
69 * glk_window_get_type:
72 * Returns the window @win's type, one of #wintype_Blank, #wintype_Pair,
73 * #wintype_TextBuffer, #wintype_TextGrid, or #wintype_Graphics.
75 * Returns: The window's type.
78 glk_window_get_type(winid_t win)
80 g_return_val_if_fail(win != NULL, 0);
85 * glk_window_get_parent:
88 * Returns the window @win's parent window. If @win is the root window, this
89 * returns #NULL, since the root window has no parent. Remember that the parent
90 * of every window is a pair window; other window types are always childless.
95 glk_window_get_parent(winid_t win)
97 g_return_val_if_fail(win != NULL, NULL);
98 /* Value will also be NULL if win is the root window */
99 return (winid_t)win->window_node->parent->data;
103 * glk_window_get_sibling:
106 * Returns the other child of the window @win's parent. If @win is the
107 * root window, this returns #NULL.
109 * Returns: A window, or NULL.
112 glk_window_get_sibling(winid_t win)
114 g_return_val_if_fail(win != NULL, NULL);
116 if(G_NODE_IS_ROOT(win->window_node))
118 if(win->window_node->next)
119 return (winid_t)win->window_node->next;
120 return (winid_t)win->window_node->prev;
124 * glk_window_get_root:
126 * Returns the root window. If there are no windows, this returns #NULL.
128 * Returns: A window, or #NULL.
131 glk_window_get_root()
133 if(root_window == NULL)
135 return (winid_t)root_window->data;
140 * @split: The window to split to create the new window. Must be 0 if there
141 * are no windows yet.
142 * @method: Position of the new window and method of size computation. One of
143 * #winmethod_Above, #winmethod_Below, #winmethod_Left, or #winmethod_Right
144 * OR'ed with #winmethod_Fixed or #winmethod_Proportional. If @wintype is
145 * #wintype_Blank, then #winmethod_Fixed is not allowed.
146 * @size: Size of the new window, in percentage points if @method is
147 * #winmethod_Proportional, otherwise in characters if @wintype is
148 * #wintype_TextBuffer or #wintype_TextGrid, or pixels if @wintype is
150 * @wintype: Type of the new window. One of #wintype_Blank, #wintype_TextGrid,
151 * #wintype_TextBuffer, or #wintype_Graphics.
152 * @rock: The new window's rock value.
154 * If there are no windows, create a new root window. @split must be 0, and
155 * @method and @size are ignored. Otherwise, split window @split into two, with
156 * position, size, and type specified by @method, @size, and @wintype. See the
157 * Glk documentation for the window placement algorithm.
159 * Returns: the new window.
162 glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype,
165 extern GtkBuilder *builder;
169 g_warning("glk_window_open: splitting of windows not implemented");
173 if(root_window != NULL)
175 g_warning("glk_window_open: there is already a window");
178 /* We only create one window and don't support any more than that */
179 winid_t new_window = g_new0(struct glk_window_struct, 1);
180 root_window = g_node_new(new_window);
182 new_window->rock = rock;
183 new_window->window_type = wintype;
185 GtkBox *vbox = GTK_BOX( gtk_builder_get_object(builder, "vbox") );
188 error_dialog(NULL, NULL, "Could not find vbox");
196 /* A blank window will be a label without any text */
197 GtkWidget *window = gtk_label_new("");
198 gtk_box_pack_end(vbox, window, TRUE, TRUE, 0);
199 gtk_widget_show(window);
201 new_window->widget = window;
202 /* You can print to a blank window's stream, but it does nothing */
203 new_window->window_stream = window_stream_new(new_window);
204 new_window->echo_stream = NULL;
208 case wintype_TextBuffer:
210 GtkWidget *scroll_window = gtk_scrolled_window_new(NULL, NULL);
211 GtkWidget *window = gtk_text_view_new();
212 gtk_container_add( GTK_CONTAINER(scroll_window), window );
213 gtk_box_pack_end(vbox, scroll_window, TRUE, TRUE, 0);
214 gtk_widget_show_all(scroll_window);
216 new_window->widget = window;
217 new_window->window_stream = window_stream_new(new_window);
218 new_window->echo_stream = NULL;
219 new_window->input_request_type = INPUT_REQUEST_NONE;
220 new_window->line_input_buffer = NULL;
221 new_window->line_input_buffer_unicode = NULL;
226 g_warning("glk_window_open: unsupported window type");
231 new_window->window_node = root_window;
240 * Erases the window @win.
243 glk_window_clear(winid_t win)
245 g_return_if_fail(win != NULL);
253 case wintype_TextBuffer:
254 /* delete all text in the window */
256 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
257 GTK_TEXT_VIEW(current_stream->window->widget) );
258 GtkTextIter start, end;
259 gtk_text_buffer_get_bounds(buffer, &start, &end);
260 gtk_text_buffer_delete(buffer, &start, &end);
265 g_warning("glk_window_clear: unsupported window type");
273 * Sets the current stream to @win's window stream.
276 glk_set_window(winid_t win)
278 glk_stream_set_current( glk_window_get_stream(win) );
282 * glk_window_get_stream:
285 * Gets the stream associated with @win.
287 * Returns: The window stream.
289 strid_t glk_window_get_stream(winid_t win)
291 g_return_val_if_fail(win != NULL, NULL);
292 return win->window_stream;
296 * glk_window_set_echo_stream:
298 * @str: A stream to attach to the window, or #NULL.
300 * Attaches the stream @str to @win as a second stream. Any text printed to the
301 * window is also echoed to this second stream, which is called the window's
304 * Effectively, any call to glk_put_char() (or the other output commands) which
305 * is directed to @win's window stream, is replicated to @win's echo stream.
306 * This also goes for the style commands such as glk_set_style().
308 * Note that the echoing is one-way. You can still print text directly to the
309 * echo stream, and it will go wherever the stream is bound, but it does not
310 * back up and appear in the window.
312 * It is illegal to set a window's echo stream to be its own window stream,
313 * which would create an infinite loop. It is similarly illegal to create a
314 * longer loop (two or more windows echoing to each other.)
316 * You can reset a window to stop echoing by setting @str to #NULL.
319 glk_window_set_echo_stream(winid_t win, strid_t str)
321 g_return_if_fail(win != NULL);
323 /* Test for an infinite loop */
326 next_str != NULL && next_str->stream_type == STREAM_TYPE_WINDOW;
327 next_str = next_str->window->echo_stream)
329 if(next_str == win->window_stream)
331 g_warning("glk_window_set_echo_stream: Infinite loop detected");
332 win->echo_stream = NULL;
337 win->echo_stream = str;
341 * glk_window_get_echo_stream:
344 * Returns the echo stream of window @win. If the window has no echo stream (as
345 * is initially the case) then this returns #NULL.
347 * Returns: A stream, or #NULL.
350 glk_window_get_echo_stream(winid_t win)
352 g_return_val_if_fail(win != NULL, NULL);
353 return win->echo_stream;