aboutsummaryrefslogtreecommitdiff
path: root/src/draw.c
blob: c6c18a2b62624448ba02dc651d3a0d2e186c28be (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
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
#include <glib.h>
#include <math.h>
#include <pango/pangocairo.h>
#include <pango/pango-font.h>
#include <pango/pango-types.h>

#include "draw.h"
#include "button.h"
#include "log.h"

#define ANIMATION(btn, which, ...) \
    do { \
        if ((btn)->anim != NULL && (btn)->anim->which##_func != NULL) { \
            if ((btn)->anim->start == 0) { \
                (btn)->anim->start = g_get_monotonic_time(); \
                log_debug("Starting animation [type=%d, func=%s, start=%ld, duration=%ld, button=%p]", \
                    (btn)->anim->type, #which, (btn)->anim->start, (btn)->anim->duration, (btn)); \
            } \
            if (!(btn)->anim->which##_func((btn)->anim, __VA_ARGS__)) \
                (btn)->anim->which##_func = NULL; \
        } \
    } while (false)

Drawer *draw_create(const char *font, int height, int left_pad, int right_pad, int top_pad)
{
    Drawer *draw = g_malloc0(sizeof(Drawer));
    g_assert_nonnull(draw);

    log_debug("Pango loading font description '%s'", font);
    draw->desc = pango_font_description_from_string(font);
    log_debug("Pango found matching font '%s'", pango_font_description_get_family(draw->desc));

    draw->height = height;
    draw->left_pad = left_pad;
    draw->right_pad = right_pad;
    draw->top_pad = top_pad;

    log_debug("Draw context created [height=%d, left_pad=%d, right_pad=%d, top_pad=%d]",
        height, left_pad, right_pad, top_pad);

    return draw;
}

// TODO: Remove this
static void compute_width(Drawer *draw, Window *win)
{
    int screen_width = win->con->screen_size->width;
    draw->width = round(screen_width - draw->right_pad - draw->left_pad);
}

static void paint_button(cairo_t *cr, const Layout *layout)
{
    double degree = M_PI / 180.0;
    int radius = (layout->height - 2 * layout->y_pad) / 2;
    int line_radius = radius - layout->line_w / 2;

    //// Layout size
    //cairo_set_source_rgb(cr, 0, 0, 0);
    //cairo_move_to(cr, layout->x, layout->y);
    //cairo_line_to(cr, layout->x, layout->y + layout->height);
    //cairo_stroke(cr);

    //cairo_move_to(cr, layout->x + layout->width, layout->y);
    //cairo_line_to(cr, layout->x + layout->width, layout->y + layout->height);
    //cairo_stroke(cr);

    //// Layout padding
    //cairo_set_source_rgb(cr, 0.5, 0.1, 0.1);
    //cairo_rectangle(cr, layout->x + layout->x_pad, layout->y + layout->y_pad, layout->width - 2 * layout->x_pad, layout->height - 2 * layout->y_pad);
    //cairo_stroke(cr);

    cairo_set_line_width(cr, layout->line_w);

    // Button background
    Color color = layout->btn->color;
    cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
    cairo_new_sub_path(cr);
    cairo_arc(cr, layout->x + layout->x_pad + radius, layout->y + layout->y_pad + radius, radius, 90 * degree, 270 * degree);
    cairo_arc(cr, layout->x + layout->width - layout->x_pad - radius, layout->y + layout->y_pad + radius, radius, 270 * degree, 450 * degree);
    cairo_close_path(cr);
    cairo_fill(cr);

    // Button border
    color = layout->btn->line_color;
    cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
    cairo_new_sub_path(cr);
    cairo_arc(cr, layout->x + layout->x_pad + radius, layout->y + layout->y_pad + radius, line_radius, 90 * degree, 270 * degree);
    cairo_arc(cr, layout->x + layout->width - layout->x_pad - radius, layout->y + layout->y_pad + radius, line_radius, 270 * degree, 450 * degree);
    cairo_close_path(cr);
    cairo_stroke(cr);
}

static void paint_text(cairo_t *cr, const Layout *layout)
{
    int text_x = layout->x + layout->width / 2 - layout->text_w / 2;
    int text_y = layout->y + layout->height / 2 - layout->text_h / 2;

    Color color = CAST(layout->btn, ButtonSimple)->text_color;
    cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
    cairo_move_to(cr, text_x, text_y);

    // NOTE: This works only if the text didn't change in size after the layouting
    pango_layout_set_text(layout->pl, CAST(layout->btn, ButtonSimple)->text, -1);
    pango_cairo_update_layout(cr, layout->pl);
    pango_cairo_show_layout(cr, layout->pl);
}

static void paint_button_list(cairo_t *cr, GList *layouts)
{
    for (GList *it = layouts; it; it = it->next) {
        Layout *layout = it->data;
        Button *btn = layout->btn;

        cairo_push_group(cr);

        // Apply before_func on the layout and cairo context
        ANIMATION(btn, before, layout, cr);

        paint_button(cr, layout);

        if (btn->simple) {
            g_assert_null(layout->children);
            paint_text(cr, layout);
        } else {
            g_assert_nonnull(layout->children);
            paint_button_list(cr, layout->children);
        }

        // Apply after_func on the layout and cairo context
        ANIMATION(btn, after, layout, cr);

        cairo_pop_group_to_source(cr);
        cairo_paint(cr);
    }
}

void draw_paint(Drawer *draw, Window *win)
{
    compute_width(draw, win);

    // Back buffering
    cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, draw->width, draw->height);

    cairo_t *cr = cairo_create(surface);
    cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
    cairo_set_operator(cr, CAIRO_OPERATOR_OVER);

    // Fill the background
    cairo_set_source_rgba(cr, draw->background.r, draw->background.g, draw->background.b, draw->background.a);
    cairo_paint(cr);

    // Treat the top-level buttons similarly to a button group
    paint_button_list(cr, draw->layouts);
    cairo_destroy(cr);

    window_move(win, draw->left_pad, draw->top_pad);
    window_resize(win, draw->width, draw->height);

    window_paint_surface(win, surface, draw->width, draw->height);
    cairo_surface_destroy(surface);
}

static void layout_destroy(Layout *layout)
{
    if (layout->pl != NULL) g_object_unref(layout->pl);
    g_list_free_full(layout->children, (GDestroyNotify)layout_destroy);
    g_free(layout);
}

static void layout_adjust(GList *start, GList *end, int off)
{
    // NOTE: end must be present in the list or segfault
    for (GList *it = start; it != end; it = it->next) {
        Layout *layout = it->data;
        layout->x += off;
        layout_adjust(layout->children, NULL, off);
    }
}

static void layout_set_text(Layout *layout, PangoFontDescription *desc, int height)
{
    g_assert(layout->btn->simple);

    const char *text = CAST(layout->btn, ButtonSimple)->text;

    pango_layout_set_font_description(layout->pl, desc);
    pango_layout_set_text(layout->pl, text, -1);
    pango_layout_set_alignment(layout->pl, PANGO_ALIGN_CENTER);
    pango_layout_get_pixel_size(layout->pl, &layout->text_w, &layout->text_h);

    // If there is only one glyph the button should be round
    size_t text_l = g_utf8_strlen(text, -1);

    layout->width = text_l == 1 ? height : layout->text_w + height;
    layout->height = height;
}

static GList *compute_group_layout(Drawer *draw, Window *win, GList *btns, int bx, bool root)
{
    GList *layouts = NULL;
    for (GList *it = btns; it; it = it->next) {
        Button *btn = it->data;

        Layout *layout = g_malloc0(sizeof(Layout));
        layouts = g_list_prepend(layouts, layout);

        layout->btn = btn;
        layout->line_w = btn->line_width;
        layout->x_pad = btn->x_pad;
        layout->y_pad = btn->y_pad;
        layout->x = bx;
        layout->y = 0;

retry:
        if (!btn->simple) {
            ButtonGroup *group = CAST(btn, ButtonGroup);
            g_assert_nonnull(group->children);

            // If there is only one child treat a group like a single button
            if (group->children->next == NULL) {
                layout->btn = btn = group->children->data;
                g_assert_true(btn->simple);
                goto retry;
            } else {
                layout->children = compute_group_layout(draw, win, group->children, bx, false);
                g_assert_nonnull(layout->children);

                Layout *last = g_list_last(layout->children)->data;
                layout->width = last->x + last->width - bx;
                layout->height = draw->height;

                // FIXME: Temporary solution to make the antialiasing (or the circles not
                //        overlapping correctly) problem less noticeable
                bool fix_left = CAST(layout->children->data, Layout)->x_pad == 0;
                if (fix_left) {
                    layout->x += 1;
                    layout->width -= 1;
                }

                bool fix_right = last->x_pad == 0;
                if (fix_right)
                    layout->width -= 1;
            }
        } else {
            layout->pl = pango_cairo_create_layout(window_get_context(win));
            layout_set_text(layout, draw->desc, draw->height);
        }

        // NOTE: We add half a line width on both sides
        layout->width += layout->line_w;

        // Apply layout_func on the layout
        ANIMATION(btn, layout, layout);

        bx += layout->width;
        if (root) {
            draw->layout_bx[btn->align] = bx;
            draw->layout_end[btn->align] = layouts;
        }

        if (it->next != NULL && (!root || CAST(it->next->data, Button)->align == btn->align))
            bx += draw->sep;
    }

    // Otherwise button_action_find will not work!
    layouts = g_list_reverse(layouts);
    return layouts;
}

void draw_compute_layout(Drawer *draw, Window *win, GList *btns)
{
    compute_width(draw, win);
    memset(draw->layout_end, 0, sizeof(draw->layout_end));
    memset(draw->layout_bx, 0, sizeof(draw->layout_bx));

    g_list_free_full(draw->layouts, (GDestroyNotify)layout_destroy);
    draw->layouts = compute_group_layout(draw, win, btns, 0, true);

    draw->layout_width[PANGO_ALIGN_LEFT] = draw->layout_bx[PANGO_ALIGN_LEFT];
    draw->layout_width[PANGO_ALIGN_CENTER] = draw->layout_bx[PANGO_ALIGN_CENTER] - draw->layout_bx[PANGO_ALIGN_LEFT];
    draw->layout_width[PANGO_ALIGN_RIGHT] = draw->layout_bx[PANGO_ALIGN_RIGHT] - MAX(draw->layout_bx[PANGO_ALIGN_CENTER], draw->layout_bx[PANGO_ALIGN_LEFT]);

    if (draw->layout_bx[PANGO_ALIGN_RIGHT] > draw->width) {
        log_error("Layout is bigger than the window (%d vs %d)", draw->layout_bx[PANGO_ALIGN_RIGHT], draw->width);
    }

    bool has_left = draw->layout_end[PANGO_ALIGN_LEFT] != NULL;
    bool has_center = draw->layout_end[PANGO_ALIGN_CENTER] != NULL;

    GList *center_start = has_left ? draw->layout_end[PANGO_ALIGN_LEFT]->next : draw->layouts;
    GList *right_start = has_center ? draw->layout_end[PANGO_ALIGN_CENTER]->next :
        has_left ? draw->layout_end[PANGO_ALIGN_LEFT]->next : draw->layouts;

    if (center_start != NULL) {
        int center = round(draw->width / 2);
        int center_off = center - draw->layout_width[PANGO_ALIGN_CENTER] / 2 - draw->layout_bx[PANGO_ALIGN_LEFT];

        log_debug("Aligning center layout [center=%d, off=%d]", center, center_off);
        layout_adjust(center_start, right_start, center_off);
    }

    if (right_start != NULL) {
        int right = draw->width - draw->layout_width[PANGO_ALIGN_RIGHT];
        int right_off = right - MAX(draw->layout_bx[PANGO_ALIGN_LEFT], draw->layout_bx[PANGO_ALIGN_CENTER]);

        log_debug("Aligning right layout [x=%d, off=%d]", right, right_off);
        layout_adjust(right_start, NULL, right_off);
    }

    log_debug("Updated layouts");
}

void draw_set_background(Drawer *draw, Color background)
{
    draw->background = background;
}

void draw_set_separator(Drawer *draw, int sep)
{
    draw->sep = sep;
}

void draw_destroy(Drawer *draw)
{
    g_list_free_full(draw->layouts, (GDestroyNotify)layout_destroy);
    pango_font_description_free(draw->desc);
    g_free(draw);
}

// vim: ts=4 sw=4 et