#include <gtk/gtktextview.h>
#include <gtk/gtkscrolledwindow.h>
#include <gtk/gtkwindow.h>
#include <gtk/gtksignal.h>

static gint delete_event_cb(GtkWidget* w, GdkEventAny* e, gpointer data);

int main(int argc, char **argv)
{
	GtkWidget* window;
	GtkWidget* text_view;
	GtkWidget* scrolled_window;
	GtkTextBuffer* text_buf;
	GtkTextIter cursor;

	gchar *very_long_line;
	gint i;

	gtk_init(&argc, &argv);

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

	scrolled_window = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_ALWAYS);
	gtk_widget_show (scrolled_window);

	text_buf = gtk_text_buffer_new (NULL);
	text_view = gtk_text_view_new_with_buffer (text_buf);

	gtk_text_view_set_editable (GTK_TEXT_VIEW (text_view), FALSE);
	gtk_widget_set_size_request (text_view, 400, 120);

	gtk_container_add(GTK_CONTAINER(scrolled_window), text_view);
	gtk_container_add(GTK_CONTAINER(window), scrolled_window);

	gtk_window_set_title(GTK_WINDOW(window), "TextView");

	gtk_signal_connect(GTK_OBJECT(window),
                      "delete_event",
                      GTK_SIGNAL_FUNC(delete_event_cb),
                      NULL);

	/* build a long line with one first '1' and lots of 'g' */
	very_long_line = (gchar*) g_malloc(9000);
	very_long_line[0] = '1';
	for (i=1; i < 8200-2; i++)
		very_long_line[i] = 'g';

	gtk_text_buffer_get_end_iter (text_buf, &cursor);

	/* all the following strings are 1g*2\n */
	/* complete output trash - first line ends at the end of second pass */
	very_long_line[8200-3] = '2';
	very_long_line[8200-2] = '\n';
	very_long_line[8200-1] = '\0';
	gtk_text_buffer_insert (text_buf, &cursor, very_long_line, -1);

	/* first and second characters trash - no more character */
	very_long_line[8196-3] = '2';
	very_long_line[8196-2] = '\n';
	very_long_line[8196-1] = '\0';
	gtk_text_buffer_insert (text_buf, &cursor, very_long_line, -1);

	/* first character trash - no more character */
	very_long_line[8195-3] = '2';
	very_long_line[8195-2] = '\n';
	very_long_line[8195-1] = '\0';
	gtk_text_buffer_insert (text_buf, &cursor, very_long_line, -1);

    /* last character '2' does not appear */
	very_long_line[8194-3] = '2';
	very_long_line[8194-2] = '\n';
	very_long_line[8194-1] = '\0';
	gtk_text_buffer_insert (text_buf, &cursor, very_long_line, -1);

	/* trivial case OK */
	very_long_line[20-3] = '2';
	very_long_line[20-2] = '\n';
	very_long_line[20-1] = '\0';
	gtk_text_buffer_insert (text_buf, &cursor, very_long_line, -1);

	/* let's got */
	gtk_widget_show_all(window);

	gtk_main();

	return 0;
}

static gint 
delete_event_cb(GtkWidget* window, GdkEventAny* e, gpointer data)
{
   gtk_main_quit();
   return FALSE;
}

