/* vim: set ts=4 sw=4 wm=5 : */

#include <gtk/gtk.h>

static void
move_cursor(GtkWidget *view, gboolean (*func)(GtkTextIter *i)) {
	GtkTextIter iter;
	GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
	gtk_text_buffer_get_iter_at_mark(buf, &iter,
			gtk_text_buffer_get_insert(buf));
	func(&iter);
	gtk_text_buffer_place_cursor(buf, &iter);
	gtk_widget_grab_focus(view);
}

static void
fwd_word_cb(GtkWidget *b, GtkWidget *view) {
	move_cursor(view, gtk_text_iter_forward_word_end);
}
static void
bwd_word_cb(GtkWidget *b, GtkWidget *view) {
	move_cursor(view, gtk_text_iter_backward_word_start);
}

static GtkWidget*
buttons(GtkWidget *view) {
	GtkWidget *button, *box;

	box = gtk_hbox_new(FALSE, 5);

	button = gtk_button_new_with_label("<< Backward");
	g_signal_connect(G_OBJECT(button), "clicked",
			G_CALLBACK(bwd_word_cb), view);
	gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);

	button = gtk_button_new_with_label("Forward >>");
	g_signal_connect(G_OBJECT(button), "clicked",
			G_CALLBACK(fwd_word_cb), view);
	gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);

	return box;
}

int
main(int argc, char* argv[]) {
	GtkWidget *win, *box, *scroll, *view;
	GtkTextBuffer *buffer;
	GtkTextIter iter;

	gtk_init(&argc, &argv);

	view = gtk_text_view_new();
	gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
	buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
	gtk_text_buffer_get_start_iter(buffer, &iter);
	gtk_text_buffer_insert(buffer, &iter, "Please, double click 'abc' or 'def' in following words:"
	"\n\nU+00B7--> abc·def"
	"\nU+0387--> abc·def"
	"\nU+05F4 --> abc״def"
	"\nU+2027 --> abc‧def"
	"\nU+003A --> abc:def"
	"\nU+FE13 --> abc︓def"
	"\nU+FE55 --> abc﹕def"
	"\nU+FF1A --> abc：def"
	"\nU+02D7 --> abc˗def"
	"\n\nResult: only left (or right) part of word arround MidLetter char is highlighted."
	" Same problem if cursor is moved with Ctrl+Right Arrow or 'Backward' and 'Forward' buttons."
	"\n\nExpected result: full word should be highlighted."
	"\n\n See: http://www.unicode.org/reports/tr29/#Word_Boundaries",
			-1);

	scroll = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), 
			GTK_POLICY_AUTOMATIC,
			GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
			GTK_SHADOW_IN);
	gtk_container_add(GTK_CONTAINER(scroll), view);

	box = gtk_vbox_new(FALSE, 5);
	gtk_box_pack_start(GTK_BOX(box), scroll, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(box), buttons(view), FALSE, FALSE, 0);

	win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(win), "UAX TR29 -- MidLetter test");
	gtk_window_set_default_size(GTK_WINDOW(win), 500, 500);
	gtk_container_set_border_width(GTK_CONTAINER(win), 10);
	g_signal_connect(G_OBJECT(win), "delete-event",
			G_CALLBACK(gtk_main_quit), NULL);
	gtk_container_add(GTK_CONTAINER(win), box);

	gtk_widget_show_all(win);
	gtk_main();

	return 0;
}

