+ switch(win->type) {
+ case wintype_TextBuffer:
+ {
+ GtkTextIter start, end;
+ gtk_text_buffer_get_end_iter(buffer, &end);
+ gint start_offset;
+
+ start_offset = gtk_text_iter_get_offset(&end);
+ gtk_text_buffer_insert(buffer, &end, win->buffer->str, -1);
+ gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
+ style_apply(win, &start, &end);
+
+ ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
+ g_assert(glk);
+ g_signal_emit_by_name(glk, "text-buffer-output", win->rock, win->buffer->str);
+ }
+ break;
+
+ case wintype_TextGrid:
+ {
+ /* Number of characters to insert */
+ glong length = win->buffer->len;
+ glong chars_left = length;
+
+ GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
+
+ /* Get cursor position */
+ GtkTextIter start, insert;
+ gint start_offset;
+
+ gtk_text_buffer_get_iter_at_mark(buffer, &insert, cursor);
+
+ while(chars_left > 0 && !gtk_text_iter_is_end(&insert))
+ {
+ /* Spaces available on this line */
+ gint available_space = win->width - gtk_text_iter_get_line_offset(&insert);
+
+ GtkTextIter end = insert;
+ if(chars_left <= available_space)
+ gtk_text_iter_forward_chars(&end, chars_left);
+ else
+ gtk_text_iter_forward_to_line_end(&end);
+
+ gtk_text_buffer_delete(buffer, &insert, &end);
+
+ start_offset = gtk_text_iter_get_offset(&insert);
+ gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), MIN(chars_left, available_space));
+ gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
+ style_apply(win, &start, &insert);
+
+ chars_left -= available_space;
+
+ if(gtk_text_iter_get_line_offset(&insert) >= win->width)
+ gtk_text_iter_forward_line(&insert);
+ }
+
+ gtk_text_buffer_move_mark(buffer, cursor, &insert);
+ }
+ break;
+ }