aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-table-header-utils.c
blob: d3ee1aca387595253b815d8d9732c2070a5ce6e3 (plain) (blame)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Chris Lahey <clahey@ximian.com>
 *      Miguel de Icaza <miguel@ximian.com>
 *      Federico Mena-Quintero <federico@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-table-header-utils.h"

#include <string.h> /* strlen() */

#include <gtk/gtk.h>

#include "e-table-defines.h"
#include "e-unicode.h"

static void
get_button_padding (GtkWidget *widget,
                    GtkBorder *padding)
{
    GtkStyleContext *context;
    GtkStateFlags state_flags;

    context = gtk_widget_get_style_context (widget);
    state_flags = gtk_widget_get_state_flags (widget);

    gtk_style_context_save (context);
    gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
    gtk_style_context_get_padding (context, state_flags, padding);

    gtk_style_context_restore (context);
}

/**
 * e_table_header_compute_height:
 * @ecol: Table column description.
 * @widget: The widget from which to build the PangoLayout.
 *
 * Computes the minimum height required for a table header button.
 *
 * Return value: The height of the button, in pixels.
 **/
gdouble
e_table_header_compute_height (ETableCol *ecol,
                               GtkWidget *widget)
{
    gint height;
    PangoLayout *layout;
    GtkBorder padding;

    g_return_val_if_fail (ecol != NULL, -1);
    g_return_val_if_fail (E_IS_TABLE_COL (ecol), -1);
    g_return_val_if_fail (GTK_IS_WIDGET (widget), -1);

    get_button_padding (widget, &padding);

    layout = gtk_widget_create_pango_layout (widget, ecol->text);

    pango_layout_get_pixel_size (layout, NULL, &height);

    if (ecol->icon_name != NULL) {
        g_return_val_if_fail (ecol->pixbuf != NULL, -1);
        height = MAX (height, gdk_pixbuf_get_height (ecol->pixbuf));
    }

    height = MAX (height, MIN_ARROW_SIZE);
    height += padding.top + padding.bottom + 2 * HEADER_PADDING;

    g_object_unref (layout);

    return height;
}

gdouble
e_table_header_width_extras (GtkWidget *widget)
{
    GtkBorder padding;

    get_button_padding (widget, &padding);
    return padding.left + padding.right + 2 * HEADER_PADDING;
}

/**
 * e_table_header_draw_button:
 * @drawable: Destination drawable.
 * @ecol: Table column for the header information.
 * @widget: The table widget.
 * @x: Leftmost coordinate of the button.
 * @y: Topmost coordinate of the button.
 * @width: Width of the region to draw.
 * @height: Height of the region to draw.
 * @button_width: Width for the complete button.
 * @button_height: Height for the complete button.
 * @arrow: Arrow type to use as a sort indicator.
 *
 * Draws a button suitable for a table header.
 **/
void
e_table_header_draw_button (cairo_t *cr,
                            ETableCol *ecol,
                            GtkWidget *widget,
                            gint x,
                            gint y,
                            gint width,
                            gint height,
                            gint button_width,
                            gint button_height,
                            ETableColArrow arrow)
{
    gint inner_x, inner_y;
    gint inner_width, inner_height;
    gint arrow_width = 0, arrow_height = 0;
    PangoContext *pango_context;
    PangoLayout *layout;
    GtkStyleContext *context;
    GtkBorder padding;
    GtkStateFlags state_flags;

    g_return_if_fail (cr != NULL);
    g_return_if_fail (ecol != NULL);
    g_return_if_fail (E_IS_TABLE_COL (ecol));
    g_return_if_fail (widget != NULL);
    g_return_if_fail (GTK_IS_WIDGET (widget));
    g_return_if_fail (button_width > 0 && button_height > 0);

    /* Button bevel */
    context = gtk_widget_get_style_context (widget);
    state_flags = gtk_widget_get_state_flags (widget);

    gtk_style_context_save (context);
    gtk_style_context_set_state (context, state_flags);
    gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);

    gtk_style_context_get_padding (context, state_flags, &padding);

    gtk_render_background (
        context, cr, x, y,
        button_width, button_height);
    gtk_render_frame (
        context, cr, x, y,
        button_width, button_height);

    /* Inside area */

    inner_width =
        button_width -
        (padding.left + padding.right + 2 * HEADER_PADDING);
    inner_height =
        button_height -
        (padding.top + padding.bottom + 2 * HEADER_PADDING);

    if (inner_width < 1 || inner_height < 1) {
        return; /* nothing fits */
    }

    inner_x = x + padding.left + HEADER_PADDING;
    inner_y = y + padding.top + HEADER_PADDING;

    /* Arrow space */

    switch (arrow) {
    case E_TABLE_COL_ARROW_NONE:
        break;

    case E_TABLE_COL_ARROW_UP:
    case E_TABLE_COL_ARROW_DOWN:
        arrow_width = MIN (MIN_ARROW_SIZE, inner_width);
        arrow_height = MIN (MIN_ARROW_SIZE, inner_height);

        if (ecol->icon_name == NULL)
            inner_width -= arrow_width + HEADER_PADDING;
        break;
    default:
        cairo_restore (cr);
        g_return_if_reached ();
    }

    if (inner_width < 1) {
        gtk_style_context_restore (context);
        return; /* nothing else fits */
    }

    pango_context = gtk_widget_create_pango_context (widget);
    layout = pango_layout_new (pango_context);
    g_object_unref (pango_context);

    pango_layout_set_text (layout, ecol->text, -1);
    pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);

    /* Pixbuf or label */
    if (ecol->icon_name != NULL) {
        gint pwidth, pheight;
        gint clip_height;
        gint xpos;

        g_return_if_fail (ecol->pixbuf != NULL);

        pwidth = gdk_pixbuf_get_width (ecol->pixbuf);
        pheight = gdk_pixbuf_get_height (ecol->pixbuf);

        clip_height = MIN (pheight, inner_height);

        xpos = inner_x;

        if (inner_width - pwidth > 11) {
            gint ypos;

            pango_layout_get_pixel_size (layout, &width, NULL);

            if (width < inner_width - (pwidth + 1)) {
                xpos = inner_x + (inner_width - width - (pwidth + 1)) / 2;
            }

            ypos = inner_y;

            pango_layout_set_width (
                layout, (inner_width - (xpos - inner_x)) *
                PANGO_SCALE);

            gtk_render_layout (
                context, cr, xpos + pwidth + 1,
                ypos, layout);
        }

        gtk_render_icon (
            context, cr, ecol->pixbuf, xpos,
            inner_y + (inner_height - clip_height) / 2);

    } else {
        pango_layout_set_width (layout, inner_width * PANGO_SCALE);

        gtk_render_layout (context, cr, inner_x, inner_y, layout);
    }

    switch (arrow) {
    case E_TABLE_COL_ARROW_NONE:
        break;

    case E_TABLE_COL_ARROW_UP:
    case E_TABLE_COL_ARROW_DOWN: {
        if (ecol->icon_name == NULL)
            inner_width += arrow_width + HEADER_PADDING;

        gtk_render_arrow (
            context, cr,
            (arrow == E_TABLE_COL_ARROW_UP) ? 0 : G_PI,
            inner_x + inner_width - arrow_width,
            inner_y + (inner_height - arrow_height) / 2,
            MAX (arrow_width, arrow_height));

        break;
    }

    default:
        cairo_restore (cr);
        g_return_if_reached ();
    }

    g_object_unref (layout);
    gtk_style_context_restore (context);
}