/* gcc test.c -o test `pkg-config gtk+-3.0 --cflags --libs` && ./test */

#include <gtk/gtk.h>

static void
add_label_clicked_cb (GtkButton *button, GtkDialog *dialog)
{
	const gchar *text;
	gboolean wrap, preset_size;
	GtkBox *content;
	GtkWidget *label;
	GString *str;

	text = gtk_entry_get_text (g_object_get_data (G_OBJECT (dialog), "text-entry"));
	wrap = gtk_toggle_button_get_active (g_object_get_data (G_OBJECT (dialog), "wrap-check"));
	preset_size = gtk_toggle_button_get_active (g_object_get_data (G_OBJECT (dialog), "sizereq-check"));

	content = GTK_BOX (gtk_dialog_get_content_area (dialog));

	str = g_string_new (text);
	g_string_append_printf (str, " (wrap:%s preset_size:%s)", wrap ? "yes" : "no", preset_size ? "yes" : "no");
	label = gtk_label_new (str->str);
	g_string_free (str, TRUE);
	gtk_label_set_line_wrap (GTK_LABEL (label), wrap);

	if (preset_size) {
		GtkAllocation allocation;

		gtk_widget_get_allocation (GTK_WIDGET (content), &allocation);
		gtk_widget_set_size_request (label, allocation.width, 10);
	}

	gtk_box_pack_start (content, label, FALSE, FALSE, 6);
	gtk_widget_show (label);
}

gint
main (gint argc, gchar **argv)
{
	GMainLoop *loop;
	GtkWidget *dialog, *w;
	GtkBox *content;

	g_type_init ();
	gtk_init (&argc, &argv);

	dialog = gtk_dialog_new_with_buttons ("Test label wrapping", NULL, GTK_DIALOG_MODAL, GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, NULL);
	content = GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog)));

	w = gtk_entry_new ();
	gtk_entry_set_text (GTK_ENTRY (w), "Pretty long text which may wrap when the line is too long and it cannot get to the window width, but it should not be too tall, with a gap at the bottom");
	gtk_box_pack_start (content, w, FALSE, FALSE, 0);
	g_object_set_data (G_OBJECT (dialog), "text-entry", w);

	w = gtk_check_button_new_with_label ("Wrap text");
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE);
	gtk_box_pack_start (content, w, FALSE, FALSE, 0);
	g_object_set_data (G_OBJECT (dialog), "wrap-check", w);

	w = gtk_check_button_new_with_label ("Preset size request");
	gtk_box_pack_start (content, w, FALSE, FALSE, 0);
	g_object_set_data (G_OBJECT (dialog), "sizereq-check", w);

	w = gtk_button_new_with_mnemonic ("_Add new label");
	gtk_box_pack_start (content, w, FALSE, FALSE, 6);
	g_signal_connect (w, "clicked", G_CALLBACK (add_label_clicked_cb), dialog);

	gtk_widget_show_all (GTK_WIDGET (content));

	gtk_window_set_default_size (GTK_WINDOW (dialog), 640, 100);
	gtk_dialog_run (GTK_DIALOG (dialog));
	gtk_widget_destroy (dialog);

	return 0;
}
