Is it possible to get some text displayed which is truncated to fit inside its allocation?

GTK+ FAQ
<<< Previous | Development with GTK+: general questions | Next >>>

# Is it possible to get some text displayed which is truncated to fit inside its allocation?

GTK's behavior (no clipping) is a consequence of its attempts to conserve X resources. Label widgets (among others) don't get their own X window - they just draw their contents on their parent's window. While it might be possible to have clipping occur by setting the clip mask before drawing the text, this would probably cause a substantial performance penalty.

Its possible that, in the long term, the best solution to such problems might be just to change gtk to give labels X windows. A short term workaround is to put the label widget inside another widget that does get its own window - one possible candidate would be the viewport widget.

    viewport = gtk_viewport (NULL, NULL);
    gtk_widget_set_usize (viewport, 50, 25);
    gtk_viewport_set_shadow_type (GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
    gtk_widget_show(viewport);

    label = gtk_label ("a really long label that won't fit");
    gtk_container_add (GTK_CONTAINER(viewport), label);
    gtk_widget_show (label);

If you were doing this for a bunch of widgets, you might want to copy gtkviewport.c and strip out the adjustment and shadow functionality (perhaps you could call it GtkClipper).

<<< Previous | Home | Next >>>
I need to add a new signal to a GTK+ widget. Any idea? | Up | How do I make my window modal? / How do I make a single window active?