first.c compilet en draait!
[projects/chimara/chimara.git] / src / window.c
index be7c20d97496224b207852a989d6f600666dc37d..c8d793c4e3396989a6eacd20138f2f22a3a1bb31 100644 (file)
@@ -65,6 +65,76 @@ glk_window_get_rock(winid_t win)
        return win->rock;
 }
 
+/**
+ * glk_window_get_type:
+ * @win: A window.
+ *
+ * Returns the window @win's type, one of #wintype_Blank, #wintype_Pair,
+ * #wintype_TextBuffer, #wintype_TextGrid, or #wintype_Graphics.
+ *
+ * Returns: The window's type.
+ */
+glui32
+glk_window_get_type(winid_t win)
+{
+       g_return_val_if_fail(win != NULL, 0);
+       return win->window_type;
+}
+
+/**
+ * glk_window_get_parent:
+ * @win: A window.
+ *
+ * Returns the window @win's parent window. If @win is the root window, this
+ * returns #NULL, since the root window has no parent. Remember that the parent
+ * of every window is a pair window; other window types are always childless.
+ *
+ * Returns: A window.
+ */
+winid_t
+glk_window_get_parent(winid_t win)
+{
+       g_return_val_if_fail(win != NULL, NULL);
+       /* Value will also be NULL if win is the root window */
+       return (winid_t)win->window_node->parent->data;
+}
+
+/**
+ * glk_window_get_sibling:
+ * @win: A window.
+ *
+ * Returns the other child of the window @win's parent. If @win is the
+ * root window, this returns #NULL.
+ *
+ * Returns: A window, or NULL.
+ */
+winid_t
+glk_window_get_sibling(winid_t win)
+{
+       g_return_val_if_fail(win != NULL, NULL);
+       
+       if(G_NODE_IS_ROOT(win->window_node))
+               return NULL;
+       if(win->window_node->next)
+               return (winid_t)win->window_node->next;
+       return (winid_t)win->window_node->prev;
+}
+
+/**
+ * glk_window_get_root:
+ * 
+ * Returns the root window. If there are no windows, this returns #NULL.
+ *
+ * Returns: A window, or #NULL.
+ */
+winid_t
+glk_window_get_root()
+{
+       if(root_window == NULL)
+               return NULL;
+       return (winid_t)root_window->data;
+}
+
 /**
  * glk_window_open:
  * @split: The window to split to create the new window. Must be 0 if there
@@ -112,10 +182,13 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype,
        new_window->rock = rock;
        new_window->window_type = wintype;
 
+       gdk_threads_enter();
+
        GtkBox *vbox = GTK_BOX( gtk_builder_get_object(builder, "vbox") );                      
        if(vbox == NULL)
        {
                error_dialog(NULL, NULL, "Could not find vbox");
+               gdk_threads_leave();
                return NULL;
        }
 
@@ -133,11 +206,17 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype,
                        new_window->window_stream = window_stream_new(new_window);
                        new_window->echo_stream = NULL;
                }
-                       break;  
+                       break;
+                       
                case wintype_TextBuffer:
                {
                        GtkWidget *scroll_window = gtk_scrolled_window_new(NULL, NULL);
                        GtkWidget *window = gtk_text_view_new();
+                       GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(window) );
+
+                       gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW(window), GTK_WRAP_WORD_CHAR );
+                       gtk_text_view_set_editable( GTK_TEXT_VIEW(window), FALSE );
+
                        gtk_container_add( GTK_CONTAINER(scroll_window), window );
                        gtk_box_pack_end(vbox, scroll_window, TRUE, TRUE, 0);
                        gtk_widget_show_all(scroll_window);
@@ -148,19 +227,100 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype,
                        new_window->input_request_type = INPUT_REQUEST_NONE;
                        new_window->line_input_buffer = NULL;
                        new_window->line_input_buffer_unicode = NULL;
+
+                       /* Connect signal handlers */
+                       new_window->keypress_handler = g_signal_connect( G_OBJECT(window), "key-press-event", G_CALLBACK(on_window_key_press_event), new_window );
+                       g_signal_handler_block( G_OBJECT(window), new_window->keypress_handler );
+
+                       new_window->insert_text_handler = g_signal_connect_after( G_OBJECT(buffer), "insert-text", G_CALLBACK(after_window_insert_text), new_window );
+                       g_signal_handler_block( G_OBJECT(buffer), new_window->insert_text_handler );
+
+                       /* Create an editable tag to indicate editable parts of the window (for line input) */
+                       gtk_text_buffer_create_tag(buffer, "uneditable", "editable", FALSE, "editable-set", TRUE, NULL);
+
+                       /* Mark the position where the user will input text */
+                       GtkTextIter end_iter;
+                       gtk_text_buffer_get_end_iter(buffer, &end_iter);
+                       gtk_text_buffer_create_mark(buffer, "input_position", &end_iter, TRUE);
                }
                        break;
+                       
                default:
                        g_warning("glk_window_open: unsupported window type");
                        g_free(new_window);
+                       gdk_threads_leave();
                        return NULL;
        }
 
        new_window->window_node = root_window;
 
+       gdk_threads_leave();
+
        return new_window;
 }
 
+void
+glk_window_close(winid_t win, stream_result_t *result)
+{
+       g_return_if_fail(win != NULL);
+
+       switch(win->window_type)
+       {
+               case wintype_TextBuffer:
+                       gtk_widget_destroy( gtk_widget_get_parent(win->widget) );
+                       /* TODO: Cancel all input requests */
+                       break;
+
+               case wintype_Blank:
+                       gtk_widget_destroy(win->widget);
+                       break;
+       }
+
+       stream_close_common(win->window_stream, result);
+
+       g_node_destroy(win->window_node);
+       /* TODO: iterate over child windows, closing them */
+
+       g_free(win);
+}
+
+/**
+ * glk_window_clear:
+ * @win: A window.
+ *
+ * Erases the window @win.
+ */
+void
+glk_window_clear(winid_t win)
+{
+       g_return_if_fail(win != NULL);
+       
+       switch(win->window_type)
+       {
+               case wintype_Blank:
+                       /* do nothing */
+                       break;
+                       
+               case wintype_TextBuffer:
+                       /* delete all text in the window */
+               {
+                       gdk_threads_enter();
+
+                       GtkTextBuffer *buffer = 
+                               gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
+                       GtkTextIter start, end;
+                       gtk_text_buffer_get_bounds(buffer, &start, &end);
+                       gtk_text_buffer_delete(buffer, &start, &end);
+
+                       gdk_threads_leave();
+               }
+                       break;
+                       
+               default:
+                       g_warning("glk_window_clear: unsupported window type");
+       }
+}
+
 /**
  * glk_set_window:
  * @win: A window.
@@ -183,6 +343,87 @@ glk_set_window(winid_t win)
  */
 strid_t glk_window_get_stream(winid_t win)
 {
+       g_return_val_if_fail(win != NULL, NULL);
        return win->window_stream;
 }
 
+/**
+ * glk_window_set_echo_stream:
+ * @win: A window.
+ * @str: A stream to attach to the window, or #NULL.
+ *
+ * Attaches the stream @str to @win as a second stream. Any text printed to the
+ * window is also echoed to this second stream, which is called the window's
+ * "echo stream."
+ *
+ * Effectively, any call to glk_put_char() (or the other output commands) which
+ * is directed to @win's window stream, is replicated to @win's echo stream.
+ * This also goes for the style commands such as glk_set_style().
+ *
+ * Note that the echoing is one-way. You can still print text directly to the
+ * echo stream, and it will go wherever the stream is bound, but it does not
+ * back up and appear in the window.
+ *
+ * It is illegal to set a window's echo stream to be its own window stream,
+ * which would create an infinite loop. It is similarly illegal to create a
+ * longer loop (two or more windows echoing to each other.)
+ *
+ * You can reset a window to stop echoing by setting @str to #NULL.
+ */
+void
+glk_window_set_echo_stream(winid_t win, strid_t str)
+{
+       g_return_if_fail(win != NULL);
+       
+       /* Test for an infinite loop */
+       strid_t next_str;
+       for(next_str = str;
+               next_str != NULL && next_str->stream_type == STREAM_TYPE_WINDOW;
+               next_str = next_str->window->echo_stream)
+       {
+               if(next_str == win->window_stream)
+               {
+                       g_warning("glk_window_set_echo_stream: Infinite loop detected");
+                       win->echo_stream = NULL;
+                       return;
+               }
+       }
+       
+       win->echo_stream = str; 
+}
+
+/**
+ * glk_window_get_echo_stream:
+ * @win: A window.
+ *
+ * Returns the echo stream of window @win. If the window has no echo stream (as
+ * is initially the case) then this returns #NULL.
+ *
+ * Returns: A stream, or #NULL.
+ */
+strid_t
+glk_window_get_echo_stream(winid_t win)
+{
+       g_return_val_if_fail(win != NULL, NULL);
+       return win->echo_stream;
+}
+
+void
+glk_window_get_size(winid_t win, glui32 *widthptr, glui32 *heightptr)
+{
+       g_return_if_fail(win != NULL);
+
+       if(widthptr != NULL) {
+               *widthptr = 0;
+       }
+
+       if(heightptr != NULL) {
+               *heightptr = 0;
+       }
+}
+
+void
+glk_window_move_cursor(winid_t win, glui32 xpos, glui32 ypos)
+{
+       g_return_if_fail(win != NULL);
+}