Blame src/gtk-helpers/autowrapped_label.c

Packit 4f15d5
/*
Packit 4f15d5
    Copyright (C) 2011  ABRT Team
Packit 4f15d5
    Copyright (C) 2011  RedHat inc.
Packit 4f15d5
Packit 4f15d5
    This program is free software; you can redistribute it and/or modify
Packit 4f15d5
    it under the terms of the GNU General Public License as published by
Packit 4f15d5
    the Free Software Foundation; either version 2 of the License, or
Packit 4f15d5
    (at your option) any later version.
Packit 4f15d5
Packit 4f15d5
    This program is distributed in the hope that it will be useful,
Packit 4f15d5
    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4f15d5
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4f15d5
    GNU General Public License for more details.
Packit 4f15d5
Packit 4f15d5
    You should have received a copy of the GNU General Public License along
Packit 4f15d5
    with this program; if not, write to the Free Software Foundation, Inc.,
Packit 4f15d5
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Packit 4f15d5
*/
Packit 4f15d5
#include <gtk/gtk.h>
Packit 4f15d5
#include "internal_libreport.h"
Packit 4f15d5
#include "internal_libreport_gtk.h"
Packit 4f15d5
Packit 4f15d5
/*
Packit 4f15d5
 * GTK doesn't re-wrap GtkLabels which have line wrapping set to true.
Packit 4f15d5
 * The line wrapped label in VBox looks like this:
Packit 4f15d5
 * |----------------------------------------|
Packit 4f15d5
 * |----------------------------------------|
Packit 4f15d5
 * | word word word word                    |
Packit 4f15d5
 * | word word word word                    |
Packit 4f15d5
 * | word word                              |
Packit 4f15d5
 * |----------------------------------------|
Packit 4f15d5
 * |----------------------------------------|
Packit 4f15d5
 * So every project copy-pastes this code to make labels widen
Packit 4f15d5
 * and shrink horizontally on resize.
Packit 4f15d5
 */
Packit 4f15d5
Packit 4f15d5
static void rewrap_label_to_parent_size(GtkWidget *widget,
Packit 4f15d5
                GtkAllocation *allocation,
Packit 4f15d5
                gpointer data)
Packit 4f15d5
{
Packit 4f15d5
    GtkLabel *label = GTK_LABEL(widget);
Packit 4f15d5
    PangoLayout *layout = gtk_label_get_layout(label);
Packit 4f15d5
Packit 4f15d5
    int lw_old, lh_old;
Packit 4f15d5
    pango_layout_get_pixel_size(layout, &lw_old, &lh_old);
Packit 4f15d5
Packit 4f15d5
    /* Already right size? */
Packit 4f15d5
    if (lw_old == allocation->width)
Packit 4f15d5
        return;
Packit 4f15d5
Packit 4f15d5
    /* Rewrap text to new width */
Packit 4f15d5
    pango_layout_set_width(layout, allocation->width * PANGO_SCALE);
Packit 4f15d5
Packit 4f15d5
    /* Did text height change as a result? */
Packit 4f15d5
    int lh;
Packit 4f15d5
    pango_layout_get_pixel_size(layout, NULL, &lh);
Packit 4f15d5
    if (lh != lh_old) /* yes, resize label height */
Packit 4f15d5
        gtk_widget_set_size_request(widget, -1, lh);
Packit 4f15d5
}
Packit 4f15d5
Packit 4f15d5
void make_label_autowrap_on_resize(GtkLabel *label)
Packit 4f15d5
{
Packit 4f15d5
    // So far, only tested to work on labels which were set up as:
Packit 4f15d5
    // gtk_label_set_justify(label, GTK_JUSTIFY_LEFT);
Packit 4f15d5
    // gtk_widget_set_halign (label, GTK_ALIGN_START);
Packit 4f15d5
    // gtk_widget_set_valign (label, GTK_ALIGN_END);
Packit 4f15d5
    // yalign != 0 definitely breaks things!
Packit 4f15d5
    // also, <property name="ypad">NONZERO</property> would be bad
Packit 4f15d5
Packit 4f15d5
    /* Makes no sense on non-wrapped labels, so we can as well
Packit 4f15d5
     * set wrapping to "on" unconditionally, istead of making it a requirement
Packit 4f15d5
     */
Packit 4f15d5
    gtk_label_set_line_wrap(label, TRUE);
Packit 4f15d5
    g_signal_connect(G_OBJECT(label), "size-allocate", G_CALLBACK(rewrap_label_to_parent_size), NULL);
Packit 4f15d5
Packit 4f15d5
    // So far, only tested to work on labels which were set up as:
Packit 4f15d5
    //gtk_box_pack_start(box, label, /*expand*/ false, /*fill*/ false, /*padding*/ 0);
Packit 4f15d5
}