#include <gtkmm.h>

#define LONG_TEXT "This is some really long text that should wrap to at " \
                  "least more than one line."

static bool
expose_cb (GdkEventExpose *event, Gtk::Widget *w)
{
    Gtk::Allocation alloc = w->get_allocation ();

    Glib::RefPtr<Pango::Layout> layout = w->create_pango_layout (LONG_TEXT);
    layout->set_wrap (Pango::WRAP_WORD_CHAR);
    layout->set_width (alloc.get_width () * PANGO_SCALE);
    layout->set_alignment (Pango::ALIGN_CENTER);
    //layout->set_justify (true);

    Pango::Rectangle rect = layout->get_logical_extents ();

    Glib::RefPtr<Gdk::Window> window = w->get_window ();

    // Paint spaces in a different color
    Pango::AttrList attr_list;
    Glib::ustring remaining_text = layout->get_text ();
    Glib::ustring::size_type loc = (Glib::ustring::size_type) -1;

    while ((loc = remaining_text.find_first_of (' ', loc + 1)) != (Glib::ustring::size_type) -1)
    {
        Pango::AttrColor attr = Pango::Attribute::create_attr_background (65535, 0, 0);
        attr.set_start_index (loc);
        attr.set_end_index (loc + 1);

        attr_list.insert (attr);
    }

    layout->set_attributes (attr_list);

    window->draw_layout (w->get_style ()->get_black_gc (),
                         alloc.get_x (),
                         alloc.get_y (),
                         layout);
    return false;
}

int
main (int argc, char *argv[])
{
    Gtk::Main loop (argc, argv);

    Gtk::Window w (Gtk::WINDOW_TOPLEVEL);
    Gtk::Label l ("");

    w.set_default_size (200, 200);
    w.add (l);
    w.show_all ();

    l.signal_expose_event ().connect (sigc::bind (sigc::ptr_fun (&expose_cb), &l));

    loop.run (w);
    return 0;
}
