Correctly handles what happens when a window that is the key window of another
[rodin/chimara.git] / src / window.c
index ea490c93db88d7a2e484a45a8c2b6a0adbce819a..bac8e18ce2c753dd2fc6b5db9d5ea2dadf7a4195 100644 (file)
@@ -364,22 +364,17 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype,
                
                case wintype_TextGrid:
                {
-                   GtkWidget *scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
                    GtkWidget *textview = gtk_text_view_new();
-                   
-                   gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_NEVER );
-                   
+
                    gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW(textview), GTK_WRAP_CHAR );
                    gtk_text_view_set_editable( GTK_TEXT_VIEW(textview), FALSE );
-                   
-                   gtk_container_add( GTK_CONTAINER(scrolledwindow), textview );
-                   gtk_widget_show_all(scrolledwindow);
+                       gtk_widget_show(textview);
                                
                        /* Set the window's font */
                        gtk_widget_modify_font(textview, glk_data->monospace_font_desc);
                    
                    win->widget = textview;
-                   win->frame = scrolledwindow;
+                   win->frame = textview;
                        
                        /* Determine the size of a "0" character in pixels */
                        PangoLayout *zero = gtk_widget_create_pango_layout(textview, "0");
@@ -460,6 +455,10 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype,
                        return NULL;
        }
 
+       /* Set the minimum size to "as small as possible" so it doesn't depend on
+        the size of the window contents */
+       gtk_widget_set_size_request(win->widget, 0, 0);
+       
        if(split)
        {
                /* When splitting, construct a new parent window
@@ -515,12 +514,15 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype,
 
        gdk_threads_leave();
        
-    /* For text grid windows, wait until GTK draws the window (see note in glk_window_get_size() ), calculate the size and fill the buffer with blanks. */
+       /* For blank or pair windows, this is almost a no-op. For text grid and
+        text buffer windows, this will wait for GTK to draw the window. Otherwise,
+        opening a window and getting its size immediately will give you the wrong
+        size. */
+       glk_window_get_size(win, NULL, NULL);
+       
+    /* For text grid windows, fill the buffer with blanks. */
     if(wintype == wintype_TextGrid)
     {
-               /* Force the window to be drawn and cache its size */
-        glk_window_get_size(win, NULL, NULL);
-               
         /* Create the cursor position mark */
                gdk_threads_enter();
         GtkTextIter begin;
@@ -536,6 +538,17 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype,
        return win;
 }
 
+/* Internal function: if node's key window is closing_win or one of its
+ children, set node's key window to NULL. */
+static gboolean 
+remove_key_windows(GNode *node, winid_t closing_win)
+{
+       winid_t win = (winid_t)node->data;
+       if(win->key_window && (win->key_window == closing_win || g_node_is_ancestor(closing_win->window_node, win->key_window->window_node)))
+               win->key_window = NULL;
+       return FALSE; /* Don't stop the traversal */
+}
+
 /* Internal function: destroy this window's GTK widgets, window streams, 
  and those of all its children */
 static void
@@ -630,7 +643,11 @@ glk_window_close(winid_t win, stream_result_t *result)
 {
        VALID_WINDOW(win, return);
        
-       /* First close all the window streams and destroy the widgets of this window
+       /* If any pair windows have this window or its children as a key window,
+        set their key window to NULL */
+       g_node_traverse(glk_data->root_window, G_IN_ORDER, G_TRAVERSE_NON_LEAVES, -1, remove_key_windows, win);
+       
+       /* Close all the window streams and destroy the widgets of this window
         and below, before trashing the window tree */
        destroy_windows_below(win, result);