summaryrefslogtreecommitdiffstats
path: root/libreport/src/gtk-helpers/autowrapped_label.c
blob: 981cb70929d517ae56b4fc4ad20ed9a512ed685a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
    Copyright (C) 2011  ABRT Team
    Copyright (C) 2011  RedHat inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <gtk/gtk.h>
#include "libreport.h"

/*
 * GTK doesn't re-wrap GtkLabels which have line wrapping set to true.
 * The line wrapped label in VBox looks like this:
 * |----------------------------------------|
 * |----------------------------------------|
 * | word word word word                    |
 * | word word word word                    |
 * | word word                              |
 * |----------------------------------------|
 * |----------------------------------------|
 * So every project copy-pastes this code to make labels widen
 * and shrink horizontally on resize.
 */

static void rewrap_label_to_parent_size(GtkWidget *widget,
                GtkAllocation *allocation,
                gpointer data)
{
    GtkLabel *label = GTK_LABEL(widget);
    PangoLayout *layout = gtk_label_get_layout(label);

    int lw_old, lh_old;
    pango_layout_get_pixel_size(layout, &lw_old, &lh_old);

    /* Already right size? */
    if (lw_old == allocation->width)
        return;

    /* Rewrap text to new width */
    pango_layout_set_width(layout, allocation->width * PANGO_SCALE);

    /* Did text height change as a result? */
    int lh;
    pango_layout_get_pixel_size(layout, NULL, &lh);
    if (lh != lh_old) /* yes, resize label height */
        gtk_widget_set_size_request(widget, -1, lh);
}

void make_label_autowrap_on_resize(GtkLabel *label)
{
    // So far, only tested to work on labels which were set up as:
    //gtk_label_set_justify(label, GTK_JUSTIFY_LEFT);
    //gtk_misc_set_alignment(GTK_MISC(label), /*x,yalign:*/ 0.0, 0.0);
    // yalign != 0 definitely breaks things!
    // also, <property name="ypad">NONZERO</property> would be bad

    /* Makes no sense on non-wrapped labels, so we can as well
     * set wrapping to "on" unconditionally, istead of making it a requirement
     */
    gtk_label_set_line_wrap(label, TRUE);
    g_signal_connect(G_OBJECT(label), "size-allocate", G_CALLBACK(rewrap_label_to_parent_size), NULL);

    // So far, only tested to work on labels which were set up as:
    //gtk_box_pack_start(box, label, /*expand*/ false, /*fill*/ false, /*padding*/ 0);
}

static void fixer(GtkWidget *widget, gpointer data_unused)
{
    if (GTK_IS_CONTAINER(widget))
    {
        gtk_container_foreach((GtkContainer*)widget, fixer, NULL);
        return;
    }
    if (GTK_IS_LABEL(widget))
    {
        GtkLabel *label = (GtkLabel*)widget;
        //const char *txt = gtk_label_get_label(label);
        GtkMisc *misc = (GtkMisc*)widget;
        gfloat yalign; //= 1;
        gint ypad; //= 1;
        if (gtk_label_get_line_wrap(label)
         && (gtk_misc_get_alignment(misc, NULL, &yalign), yalign == 0)
         && (gtk_misc_get_padding(misc, NULL, &ypad), ypad == 0)
        ) {
            //log("label '%s' set to wrap", txt);
            make_label_autowrap_on_resize(label);
            return;
        }
        //log("label '%s' not set to wrap %g %d", txt, yalign, ypad);
    }
}

void fix_all_wrapped_labels(GtkWidget *widget)
{
    fixer(widget, NULL);
}