aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/table/e-table-header-utils.c
blob: 6d73d1f763f4ddb591b3c7a17a23e4e85e57b95b (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* ETable widget - utilities for drawing table header buttons
 *
 * Copyright (C) 2000 Ximian, Inc.
 *
 * Authors: Chris Lahey <clahey@ximian.com>
 *          Miguel de Icaza <miguel@ximian.com>
 *          Federico Mena-Quintero <federico@ximian.com>
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

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

#include <string.h> /* strlen() */
#include <glib.h>
#include "e-table-defines.h"
#include "e-table-header-utils.h"



/**
 * e_table_header_compute_height:
 * @ecol: Table column description.
 * @style: Style for the button's bevel.
 * @font: Font for the button's text, or NULL if no font is available.
 *
 * Computes the minimum height required for a table header button.
 *
 * Return value: The height of the button, in pixels.
 **/
double
e_table_header_compute_height (ETableCol *ecol, GtkStyle *style, GdkFont *font)
{
    int ythick;
    int height;

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

    ythick = style->klass->ythickness;

    if (font)
        height = font->ascent + font->descent;
    else
        height = 16; /* FIXME: default? */

    if (ecol->is_pixbuf) {
        g_assert (ecol->pixbuf != NULL);
        height = MAX (height, gdk_pixbuf_get_height (ecol->pixbuf));
    }

    height = MAX (height, MIN_ARROW_SIZE);

    height += 2 * (ythick + HEADER_PADDING);

    return height;
}

double
e_table_header_width_extras (GtkStyle *style)
{
    g_return_val_if_fail (style != NULL, -1);

    return 2 * (style->klass->xthickness + HEADER_PADDING);
}

/* Creates a pixmap that is a composite of a background color and the upper-left
 * corner rectangle of a pixbuf.
 */
static GdkPixmap *
make_composite_pixmap (GdkDrawable *drawable, GdkGC *gc,
               GdkPixbuf *pixbuf, GdkColor *bg, int width, int height,
               int dither_xofs, int dither_yofs)
{
    int pwidth, pheight;
    GdkPixmap *pixmap;
    GdkPixbuf *tmp;
    int color;

    pwidth = gdk_pixbuf_get_width (pixbuf);
    pheight = gdk_pixbuf_get_height (pixbuf);
    g_assert (width <= pwidth && height <= pheight);

    color = ((bg->red & 0xff00) << 8) | (bg->green & 0xff00) | ((bg->blue & 0xff00) >> 8);

    if (width >= pwidth && height >= pheight) {
        tmp = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
        if (!tmp)
            return NULL;

        gdk_pixbuf_composite_color (pixbuf, tmp,
                        0, 0,
                        width, height,
                        0, 0,
                        1.0, 1.0,
                        GDK_INTERP_NEAREST,
                        255,
                        0, 0,
                        16,
                        color, color);
    } else {
        int x, y, rowstride;
        GdkPixbuf *fade;
        guchar *pixels;

        /* Do a nice fade of the pixbuf down and to the right */

        fade = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
        if (!fade)
            return NULL;

        gdk_pixbuf_copy_area (pixbuf,
                      0, 0,
                      width, height,
                      fade,
                      0, 0);

        rowstride = gdk_pixbuf_get_rowstride (fade);
        pixels = gdk_pixbuf_get_pixels (fade);

        for (y = 0; y < height; y++) {
            guchar *p;
            int yfactor;

            p = pixels + y * rowstride;

            if (height < pheight)
                yfactor = height - y;
            else
                yfactor = height;

            for (x = 0; x < width; x++) {
                int xfactor;

                if (width < pwidth)
                    xfactor = width - x;
                else
                    xfactor = width;

                p[3] = ((int) p[3] * xfactor * yfactor / (width * height));
                p += 4;
            }
        }

        tmp = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
        if (!tmp) {
            gdk_pixbuf_unref (fade);
            return NULL;
        }

        gdk_pixbuf_composite_color (fade, tmp,
                        0, 0,
                        width, height,
                        0, 0,
                        1.0, 1.0,
                        GDK_INTERP_NEAREST,
                        255,
                        0, 0,
                        16,
                        color, color);

        gdk_pixbuf_unref (fade);
    }

    pixmap = gdk_pixmap_new (drawable, width, height, gdk_rgb_get_visual ()->depth);
    gdk_draw_rgb_image_dithalign (pixmap, gc,
                      0, 0,
                      width, height,
                      GDK_RGB_DITHER_NORMAL,
                      gdk_pixbuf_get_pixels (tmp),
                      gdk_pixbuf_get_rowstride (tmp),
                      dither_xofs, dither_yofs);
    gdk_pixbuf_unref (tmp);

    return pixmap;
}

/**
 * e_table_header_draw_button:
 * @drawable: Destination drawable.
 * @ecol: Table column for the header information.
 * @style: Style to use for drawing the button.
 * @font: Font for the button's text.
 * @state: State of the table widget.
 * @widget: The table widget.
 * @gc: GC to use for drawing.
 * @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 (GdkDrawable *drawable, ETableCol *ecol,
                GtkStyle *style, GdkFont *font, GtkStateType state,
                GtkWidget *widget, GdkGC *gc,
                int x, int y, int width, int height,
                int button_width, int button_height,
                ETableColArrow arrow)
{
    int xthick, ythick;
    int inner_x, inner_y;
    int inner_width, inner_height;

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

    xthick = style->klass->xthickness;
    ythick = style->klass->ythickness;

    /* Button bevel */

    gtk_paint_box (style, drawable, state, GTK_SHADOW_OUT,
               NULL, widget, "button",
               x, y, button_width, button_height);

    /* Inside area */

    inner_width = button_width - 2 * (xthick + HEADER_PADDING);
    inner_height = button_height - 2 * (ythick + HEADER_PADDING);

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

    inner_x = x + xthick + HEADER_PADDING;
    inner_y = y + ythick + HEADER_PADDING;

    /* Arrow */

    switch (arrow) {
    case E_TABLE_COL_ARROW_NONE:
        break;

    case E_TABLE_COL_ARROW_UP:
    case E_TABLE_COL_ARROW_DOWN: {
        int arrow_width, arrow_height;

        arrow_width = MIN (MIN_ARROW_SIZE, inner_width);
        arrow_height = MIN (MIN_ARROW_SIZE, inner_height);

        gtk_paint_arrow (style, drawable, state,
                 GTK_SHADOW_IN, NULL, widget, "header",
                 (arrow == E_TABLE_COL_ARROW_UP) ? GTK_ARROW_UP : GTK_ARROW_DOWN,
                 TRUE,
                 inner_x + inner_width - arrow_width,
                 inner_y + (inner_height - arrow_height) / 2,
                 arrow_width, arrow_height);

        inner_width -= arrow_width + HEADER_PADDING;
        break;
    }

    default:
        g_assert_not_reached ();
        return;
    }

    if (inner_width < 1)
        return; /* nothing else fits */

    /* Pixbuf or label */

    if (ecol->is_pixbuf) {
        int pwidth, pheight;
        int clip_width, clip_height;
        GdkPixmap *pixmap;

        g_assert (ecol->pixbuf != NULL);

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

        clip_width = MIN (pwidth, inner_width);
        clip_height = MIN (pheight, inner_height);

        pixmap = make_composite_pixmap (drawable, gc,
                        ecol->pixbuf, &style->bg[state],
                        clip_width, clip_height,
                        inner_x,
                        inner_y + (inner_height - clip_height) / 2);
        if (pixmap) {
            gdk_draw_pixmap (drawable, gc, pixmap,
                     0, 0,
                     inner_x,
                     inner_y + (inner_height - clip_height) / 2,
                     clip_width, clip_height);
            gdk_pixmap_unref (pixmap);
        }
    } else {
        int ypos;

        ypos = inner_y + (inner_height - font->ascent - font->descent) / 2 + font->ascent;

        e_table_draw_elided_string (drawable, font, gc,
                        inner_x, ypos,
                        ecol->text, inner_width, TRUE);
    }
}

/* Computes the length of a string that needs to be trimmed for elision */
static int
compute_elision_length (GdkFont *font, const char *str, int max_width)
{
    int len;
    int l = 0, left, right;
    int rbearing;

    len = strlen (str);

    if (len <= 0)
      return 0;

    left = 0;
    right = len;

    while (left < right) {
        l = (left + right) / 2;
        gdk_text_extents (font, str, l, NULL, &rbearing, NULL, NULL, NULL);

        if (rbearing < max_width)
            left = l + 1;
        else if (rbearing > max_width)
            right = l;
        else
            return l;
    }

    if (rbearing > max_width)
        return MAX (0, l - 1);
    else
        return l;
}

/* Default width of the elision arrow in pixels */
#define ARROW_WIDTH 4

/**
 * e_table_draw_elided_string:
 * @drawable: Destination drawable.
 * @font: Font for the text.
 * @gc: GC to use for drawing.
 * @x: X insertion point for the string.
 * @y: Y insertion point for the string's baseline.
 * @str: String to draw.
 * @max_width: Maximum width in which the string must fit.
 * @center: Whether to center the string in the available area if it does fit.
 * 
 * Draws a string, possibly trimming it so that it fits inside the specified
 * maximum width.  If it does not fit, an elision indicator is drawn after the
 * last character that does fit.
 **/
void
e_table_draw_elided_string (GdkDrawable *drawable, GdkFont *font, GdkGC *gc,
                int x, int y, const char *str, int max_width, gboolean center)
{
    int rbearing;
    int width;

    g_return_if_fail (drawable != NULL);
    g_return_if_fail (font != NULL);
    g_return_if_fail (gc != NULL);
    g_return_if_fail (str != NULL);
    g_return_if_fail (max_width >= 0);

    gdk_string_extents (font, str, NULL, &rbearing, &width, NULL, NULL);

    if (rbearing <= max_width) {
        int xpos;

        if (center)
            xpos = x + (max_width - width) / 2;
        else
            xpos = x;

        gdk_draw_string (drawable, font, gc, xpos, y, str);
    } else {
        int arrow_width;
        int len;
        int i;

        if (max_width < ARROW_WIDTH + 1)
            arrow_width = max_width - 1;
        else
            arrow_width = ARROW_WIDTH;

        len = compute_elision_length (font, str, max_width - arrow_width - 1);
        gdk_draw_text (drawable, font, gc, x, y, str, len);

        gdk_text_extents (font, str, len, NULL, &rbearing, NULL, NULL, NULL);

        y -= font->ascent;

        for (i = 0; i < arrow_width; i++) {
            int h;

            h = 2 * i + 1;

            gdk_draw_line (drawable, gc,
                       x + rbearing + arrow_width - i,
                       y + (font->ascent + font->descent - h) / 2,
                       x + rbearing + arrow_width - i,
                       y + (font->ascent + font->descent - h) / 2 + h - 1);
        }
    }
}