+ if(split)
+ {
+ /* When splitting, construct a new parent window
+ * copying most characteristics from the window that is being split */
+ winid_t pair = g_new0(struct glk_window_struct, 1);
+ pair->rock = 0;
+ pair->type = wintype_Pair;
+ pair->window_node = g_node_new(pair);
+ pair->unit_width = split->unit_width;
+ pair->unit_height = split->unit_height;
+ pair->window_stream = NULL;
+ pair->echo_stream = NULL;
+
+ /* Insert the new window into the window tree */
+ if(split->window_node->parent == NULL)
+ {
+ glk_data->root_window = pair->window_node;
+ } else {
+ g_node_append(split->window_node->parent, pair->window_node);
+ g_node_unlink(split->window_node);
+ }
+
+ /* Keep track of the parent widget of the window that is being split */
+ GtkWidget* old_parent = gtk_widget_get_parent(split->frame);
+ gtk_widget_ref(split->frame);
+ gtk_widget_unparent(split->frame);
+
+ /* Place the windows in the correct order */
+ switch(method & winmethod_DirMask)
+ {
+ case winmethod_Left:
+ pair->widget = gtk_hbox_new(FALSE, 0);
+ gtk_box_pack_end(GTK_BOX(pair->widget), split->frame, TRUE, TRUE, 0);
+ gtk_box_pack_end(GTK_BOX(pair->widget), win->frame, TRUE, TRUE, 0);
+ g_node_append(pair->window_node, split->window_node);
+ g_node_append(pair->window_node, win->window_node);
+ break;
+ case winmethod_Right:
+ pair->widget = gtk_hbox_new(FALSE, 0);
+ gtk_box_pack_end(GTK_BOX(pair->widget), win->frame, TRUE, TRUE, 0);
+ gtk_box_pack_end(GTK_BOX(pair->widget), split->frame, TRUE, TRUE, 0);
+ g_node_append(pair->window_node, win->window_node);
+ g_node_append(pair->window_node, split->window_node);
+ break;
+ case winmethod_Above:
+ pair->widget = gtk_vbox_new(FALSE, 0);
+ gtk_box_pack_end(GTK_BOX(pair->widget), split->frame, TRUE, TRUE, 0);
+ gtk_box_pack_end(GTK_BOX(pair->widget), win->frame, TRUE, TRUE, 0);
+ g_node_append(pair->window_node, split->window_node);
+ g_node_append(pair->window_node, win->window_node);
+ break;
+ case winmethod_Below:
+ pair->widget = gtk_vbox_new(FALSE, 0);
+ gtk_box_pack_end(GTK_BOX(pair->widget), win->frame, TRUE, TRUE, 0);
+ gtk_box_pack_end(GTK_BOX(pair->widget), split->frame, TRUE, TRUE, 0);
+ g_node_append(pair->window_node, win->window_node);
+ g_node_append(pair->window_node, split->window_node);
+ break;
+ }
+ gtk_widget_unref(split->frame);
+
+ /* TODO: set the new size of the windows */
+
+ pair->frame = pair->widget;
+ gtk_widget_set_parent(pair->widget, old_parent);
+ gtk_widget_show(pair->widget);
+ } else {
+ /* Set the window as root window */
+ glk_data->root_window = win->window_node;
+ gtk_widget_set_parent(win->frame, GTK_WIDGET(glk_data->self));
+ gtk_widget_queue_resize(GTK_WIDGET(glk_data->self));
+ }
+
+ /* 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. */
+ if(wintype == wintype_TextGrid)
+ {
+ while(win->widget->allocation.width == 1 && win->widget->allocation.height == 1)
+ {
+ /* Release the GDK lock momentarily */
+ gdk_threads_leave();
+ gdk_threads_enter();
+ while(gtk_events_pending())
+ gtk_main_iteration();
+ }
+ win->width = (glui32)(win->widget->allocation.width / win->unit_width);
+ win->height = (glui32)(win->widget->allocation.height / win->unit_height);
+
+ /* Mark the cursor position */
+ GtkTextIter begin;
+ GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
+ gtk_text_buffer_get_start_iter(buffer, &begin);
+ gtk_text_buffer_create_mark(buffer, "cursor_position", &begin, TRUE);
+
+ /* Fill the buffer with blanks and move the cursor to the upper left */
+ gdk_threads_leave();
+ glk_window_clear(win);
+ gdk_threads_enter();
+
+ /* Apparently this only works after the window has been realized */
+ gtk_text_view_set_overwrite( GTK_TEXT_VIEW(win->widget), TRUE );
+ }