/* gcc `pkg-config --libs --cflags gtk+-2.0` -o testellipsise testellipsise.c */

#include <gtk/gtk.h>

GList *labels = NULL;

static void
combo_changed_cb (GtkComboBox *combo)
{
  PangoEllipsizeMode mode = (PangoEllipsizeMode) gtk_combo_box_get_active (combo);
  GList *l;

  for (l = labels; l != NULL; l = l->next)
    gtk_label_set_ellipsize (GTK_LABEL (l->data), mode);
}

static GtkWidget *
new_row (char *text, int width)
{
  GtkWidget *hbox;
  GtkWidget *label;

  hbox = gtk_hbox_new (FALSE, 10);

  label = gtk_label_new (text);
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
  gtk_label_set_max_width_chars (GTK_LABEL (label), width);
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), gtk_label_new ("Text!"), FALSE, FALSE, 0);
  labels = g_list_prepend (labels, label);

  return hbox;
}

static char *texts[] = {
  "Short text.",
  "Medium length text.",
  "Excessively long text. And it's even longer!"
};

int main(int argc, char **argv)
{
  GtkWidget *window, *vbox, *hbox, *label, *combo, *button;
  int i, width;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
  g_signal_connect (window, "delete-event", G_CALLBACK (gtk_main_quit), NULL);

  vbox = gtk_vbox_new (FALSE, 6);
  gtk_container_add (GTK_CONTAINER (window), vbox);

  hbox = gtk_hbox_new (FALSE, 12);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

  label = gtk_label_new ("Ellipsize mode:");
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);

  combo = gtk_combo_box_new_text ();
  gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "None");
  gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "Start");
  gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "Middle");
  gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "End");
  gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
  gtk_box_pack_start (GTK_BOX (hbox), combo, FALSE, FALSE, 0);
  g_signal_connect (combo, "changed", G_CALLBACK (combo_changed_cb), NULL);

  hbox = gtk_hbox_new (FALSE, 24);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

  for (i = 0; i < G_N_ELEMENTS (texts); i++)
    {
      vbox = gtk_vbox_new (FALSE, 6);
      gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);

      for (width = 4; width <= 32; width += 2)
	gtk_box_pack_start (GTK_BOX (vbox), new_row (texts[i],width), FALSE, FALSE, 0);
    }

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}
