Oeps, nog enkele bugs
[projects/chimara/chimara.git] / src / window.c
1 #include "window.h"
2
3 /* Global tree of all windows */
4 static GNode *root_window = NULL;
5
6 /**
7  * glk_window_iterate:
8  * @win: A window, or #NULL.
9  * @rockptr: Return location for the next window's rock, or #NULL.
10  *
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.
15  *
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.
19  *
20  * Returns: the next window, or #NULL if there are no more.
21  */
22 winid_t
23 glk_window_iterate(winid_t win, glui32 *rockptr)
24 {
25         GNode *retnode;
26         
27         if(win == NULL)
28                 retnode = root_window;
29         else
30         {
31                 GNode *node = win->window_node;
32                 if( G_NODE_IS_LEAF(node) )
33                 {
34                         while(node && node->next == NULL)
35                                 node = node->parent;
36                         if(node)
37                                 retnode = node->next;
38                         else
39                                 retnode = NULL;
40                 }
41                 else
42                         retnode = g_node_first_child(node);
43         }
44         winid_t retval = retnode? (winid_t)retnode->data : NULL;
45                 
46         /* Store the window's rock in rockptr */
47         if(retval && rockptr)
48                 *rockptr = glk_window_get_rock(retval);
49                 
50         return retval;
51 }
52
53 /**
54  * glk_window_get_rock:
55  * @win: A window.
56  * 
57  * Returns the window @win's rock value. Pair windows always have rock 0.
58  *
59  * Returns: A rock value.
60  */
61 glui32
62 glk_window_get_rock(winid_t win)
63 {
64         g_return_val_if_fail(win != NULL, 0);
65         return win->rock;
66 }
67
68 /**
69  * glk_window_get_type:
70  * @win: A window.
71  *
72  * Returns the window @win's type, one of #wintype_Blank, #wintype_Pair,
73  * #wintype_TextBuffer, #wintype_TextGrid, or #wintype_Graphics.
74  *
75  * Returns: The window's type.
76  */
77 glui32
78 glk_window_get_type(winid_t win)
79 {
80         g_return_val_if_fail(win != NULL, 0);
81         return win->window_type;
82 }
83
84 /**
85  * glk_window_get_parent:
86  * @win: A window.
87  *
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.
91  *
92  * Returns: A window.
93  */
94 winid_t
95 glk_window_get_parent(winid_t win)
96 {
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;
100 }
101
102 /**
103  * glk_window_get_sibling:
104  * @win: A window.
105  *
106  * Returns the other child of the window @win's parent. If @win is the
107  * root window, this returns #NULL.
108  *
109  * Returns: A window, or NULL.
110  */
111 winid_t
112 glk_window_get_sibling(winid_t win)
113 {
114         g_return_val_if_fail(win != NULL, NULL);
115         
116         if(G_NODE_IS_ROOT(win->window_node))
117                 return NULL;
118         if(win->window_node->next)
119                 return (winid_t)win->window_node->next;
120         return (winid_t)win->window_node->prev;
121 }
122
123 /**
124  * glk_window_get_root:
125  * 
126  * Returns the root window. If there are no windows, this returns #NULL.
127  *
128  * Returns: A window, or #NULL.
129  */
130 winid_t
131 glk_window_get_root()
132 {
133         if(root_window == NULL)
134                 return NULL;
135         return (winid_t)root_window->data;
136 }
137
138 /**
139  * glk_window_open:
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
149  * #wintype_Graphics.
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.
153  *
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.
158  *
159  * Returns: the new window.
160  */
161 winid_t
162 glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype, 
163                 glui32 rock)
164 {
165         extern GtkBuilder *builder;
166
167         if(split)
168         {
169                 g_warning("glk_window_open: splitting of windows not implemented");
170                 return NULL;
171         }
172
173         if(root_window != NULL)
174         {
175                 g_warning("glk_window_open: there is already a window");
176                 return NULL;
177         }
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);
181
182         new_window->rock = rock;
183         new_window->window_type = wintype;
184
185         GtkBox *vbox = GTK_BOX( gtk_builder_get_object(builder, "vbox") );                      
186         if(vbox == NULL)
187         {
188                 error_dialog(NULL, NULL, "Could not find vbox");
189                 return NULL;
190         }
191
192         switch(wintype)
193         {
194                 case wintype_Blank:
195                 {
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);
200                         
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;
205                 }
206                         break;
207                         
208                 case wintype_TextBuffer:
209                 {
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);
215
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;
222                 }
223                         break;
224                         
225                 default:
226                         g_warning("glk_window_open: unsupported window type");
227                         g_free(new_window);
228                         return NULL;
229         }
230
231         new_window->window_node = root_window;
232
233         return new_window;
234 }
235
236 /**
237  * glk_window_clear:
238  * @win: A window.
239  *
240  * Erases the window @win.
241  */
242 void
243 glk_window_clear(winid_t win)
244 {
245         g_return_if_fail(win != NULL);
246         
247         switch(win->window_type)
248         {
249                 case wintype_Blank:
250                         /* do nothing */
251                         break;
252                         
253                 case wintype_TextBuffer:
254                         /* delete all text in the window */
255                 {
256                         GtkTextBuffer *buffer = 
257                                 gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
258                         GtkTextIter start, end;
259                         gtk_text_buffer_get_bounds(buffer, &start, &end);
260                         gtk_text_buffer_delete(buffer, &start, &end);
261                 }
262                         break;
263                         
264                 default:
265                         g_warning("glk_window_clear: unsupported window type");
266         }
267 }
268
269 /**
270  * glk_set_window:
271  * @win: A window.
272  *
273  * Sets the current stream to @win's window stream.
274  */
275 void
276 glk_set_window(winid_t win)
277 {
278         glk_stream_set_current( glk_window_get_stream(win) );
279 }
280
281 /**
282  * glk_window_get_stream:
283  * @win: A window.
284  *
285  * Gets the stream associated with @win.
286  *
287  * Returns: The window stream.
288  */
289 strid_t glk_window_get_stream(winid_t win)
290 {
291         g_return_val_if_fail(win != NULL, NULL);
292         return win->window_stream;
293 }
294
295 /**
296  * glk_window_set_echo_stream:
297  * @win: A window.
298  * @str: A stream to attach to the window, or #NULL.
299  *
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
302  * "echo stream."
303  *
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().
307  *
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.
311  *
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.)
315  *
316  * You can reset a window to stop echoing by setting @str to #NULL.
317  */
318 void
319 glk_window_set_echo_stream(winid_t win, strid_t str)
320 {
321         g_return_if_fail(win != NULL);
322         
323         /* Test for an infinite loop */
324         strid_t next_str;
325         for(next_str = str;
326                 next_str != NULL && next_str->stream_type == STREAM_TYPE_WINDOW;
327                 next_str = next_str->window->echo_stream)
328         {
329                 if(next_str == win->window_stream)
330                 {
331                         g_warning("glk_window_set_echo_stream: Infinite loop detected");
332                         win->echo_stream = NULL;
333                         return;
334                 }
335         }
336         
337         win->echo_stream = str; 
338 }
339
340 /**
341  * glk_window_get_echo_stream:
342  * @win: A window.
343  *
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.
346  *
347  * Returns: A stream, or #NULL.
348  */
349 strid_t
350 glk_window_get_echo_stream(winid_t win)
351 {
352         g_return_val_if_fail(win != NULL, NULL);
353         return win->echo_stream;
354 }
355