2016-08-04 02:55:51 +03:00
|
|
|
/*
|
2016-08-18 02:54:25 +03:00
|
|
|
* gui/over.c - GUI: overlays
|
2016-08-04 02:55:51 +03:00
|
|
|
*
|
|
|
|
* Written 2016 by Werner Almesberger
|
|
|
|
* Copyright 2016 by Werner Almesberger
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Resources:
|
|
|
|
*
|
|
|
|
* http://zetcode.com/gfx/cairo/cairobackends/
|
|
|
|
* https://developer.gnome.org/gtk3/stable/gtk-migrating-2-to-3.html
|
|
|
|
* https://www.cairographics.org/samples/rounded_rectangle/
|
2016-08-07 12:08:42 +03:00
|
|
|
*
|
|
|
|
* Section "Description" in
|
|
|
|
* https://developer.gnome.org/pango/stable/pango-Cairo-Rendering.html
|
2016-08-04 02:55:51 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
2016-08-07 01:14:31 +03:00
|
|
|
#include <stdarg.h>
|
2016-08-04 02:55:51 +03:00
|
|
|
#include <stdlib.h>
|
2016-08-07 01:14:31 +03:00
|
|
|
#include <stdio.h>
|
2016-08-04 02:55:51 +03:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include <cairo/cairo.h>
|
2016-08-06 19:26:59 +03:00
|
|
|
#include <pango/pangocairo.h>
|
2016-08-04 02:55:51 +03:00
|
|
|
|
|
|
|
#include "util.h"
|
2016-08-07 01:14:31 +03:00
|
|
|
#include "fmt-pango.h"
|
2016-08-18 02:54:25 +03:00
|
|
|
#include "gui/aoi.h"
|
|
|
|
#include "gui/style.h"
|
|
|
|
#include "gui/over.h"
|
2016-08-04 02:55:51 +03:00
|
|
|
|
|
|
|
|
|
|
|
struct overlay {
|
|
|
|
const char *s;
|
2016-08-09 19:12:10 +03:00
|
|
|
struct overlay_style style;
|
2016-08-04 13:53:25 +03:00
|
|
|
|
|
|
|
struct aoi **aois;
|
|
|
|
bool (*hover)(void *user, bool on);
|
|
|
|
void (*click)(void *user);
|
2016-08-14 19:01:35 +03:00
|
|
|
void (*drag)(void *user, int dx, int dy);
|
2016-08-04 13:53:25 +03:00
|
|
|
void *user;
|
|
|
|
|
2016-08-07 11:09:34 +03:00
|
|
|
struct aoi *aoi;
|
2016-08-04 13:53:25 +03:00
|
|
|
|
2016-08-15 10:47:43 +03:00
|
|
|
struct overlay *next, *prev;
|
2016-08-04 02:55:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void rrect(cairo_t *cr, int x, int y, int w, int h, int r)
|
|
|
|
{
|
|
|
|
const double deg = M_PI / 180.0;
|
|
|
|
|
|
|
|
cairo_new_path(cr);
|
|
|
|
cairo_arc(cr, x + w - r, y + r, r, -90 * deg, 0);
|
|
|
|
cairo_arc(cr, x + w - r, y + h - r, r, 0, 90 * deg);
|
|
|
|
cairo_arc(cr, x + r, y + h - r, r, 90 * deg, 180 * deg);
|
|
|
|
cairo_arc(cr, x + r, y + r, r, 180 * deg, 270 * deg);
|
|
|
|
cairo_close_path(cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-14 20:35:57 +03:00
|
|
|
static unsigned overlay_draw(struct overlay *over, cairo_t *cr,
|
|
|
|
unsigned x, unsigned y, int dx, int dy)
|
2016-08-04 02:55:51 +03:00
|
|
|
{
|
2016-08-09 19:12:10 +03:00
|
|
|
const struct overlay_style *style = &over->style;
|
2016-08-09 22:01:01 +03:00
|
|
|
const struct color *fg = &style->fg;
|
|
|
|
const struct color *bg = &style->bg;
|
|
|
|
const struct color *frame = &style->frame;
|
2016-08-07 12:08:42 +03:00
|
|
|
unsigned ink_w, ink_h; /* effectively used text area size */
|
|
|
|
unsigned w, h; /* box size */
|
|
|
|
int tx, ty; /* text start position */
|
2016-08-04 02:55:51 +03:00
|
|
|
|
2016-08-06 19:26:59 +03:00
|
|
|
PangoLayout *layout;
|
|
|
|
PangoFontDescription *desc;
|
|
|
|
PangoRectangle ink_rect;
|
2016-08-04 02:55:51 +03:00
|
|
|
|
2016-08-07 02:46:03 +03:00
|
|
|
desc = pango_font_description_from_string(style->font);
|
2016-08-06 19:26:59 +03:00
|
|
|
layout = pango_cairo_create_layout(cr);
|
|
|
|
pango_layout_set_font_description(layout, desc);
|
2016-08-07 02:46:03 +03:00
|
|
|
pango_layout_set_markup(layout, over->s, -1);
|
2016-08-06 19:26:59 +03:00
|
|
|
pango_font_description_free(desc);
|
|
|
|
|
|
|
|
pango_layout_get_extents(layout, &ink_rect, NULL);
|
2016-08-07 02:46:03 +03:00
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "%d + %d %d + %d\n",
|
|
|
|
ink_rect.x / PANGO_SCALE, ink_rect.width / PANGO_SCALE,
|
|
|
|
ink_rect.y / PANGO_SCALE, ink_rect.height / PANGO_SCALE);
|
|
|
|
#endif
|
|
|
|
ink_w = ink_rect.width / PANGO_SCALE;
|
|
|
|
ink_h = ink_rect.height / PANGO_SCALE;
|
|
|
|
|
|
|
|
ink_w = ink_w > style->wmin ? ink_w : style->wmin;
|
|
|
|
ink_w = !style->wmax || ink_w < style->wmax ? ink_w : style->wmax;
|
|
|
|
w = ink_w + 2 * style->pad;
|
|
|
|
h = ink_h + 2 * style->pad;
|
|
|
|
|
2016-08-15 10:33:34 +03:00
|
|
|
if (dx < 0)
|
|
|
|
x -= w;
|
|
|
|
if (dy < 0)
|
|
|
|
y -= h;
|
2016-08-07 12:08:42 +03:00
|
|
|
|
2016-08-14 20:52:07 +03:00
|
|
|
tx = x - ink_rect.x / PANGO_SCALE + style->pad;
|
|
|
|
ty = y - ink_rect.y / PANGO_SCALE + style->pad;
|
2016-08-04 13:53:25 +03:00
|
|
|
|
2016-08-14 20:52:07 +03:00
|
|
|
rrect(cr, x, y, w, h, style->radius);
|
2016-08-04 02:55:51 +03:00
|
|
|
|
2016-08-09 22:01:01 +03:00
|
|
|
cairo_set_source_rgba(cr, bg->r, bg->g, bg->b, bg->alpha);
|
2016-08-04 02:55:51 +03:00
|
|
|
cairo_fill_preserve(cr);
|
2016-08-09 22:01:01 +03:00
|
|
|
cairo_set_source_rgba(cr, frame->r, frame->g, frame->b, frame->alpha);
|
2016-08-07 02:46:03 +03:00
|
|
|
cairo_set_line_width(cr, style->width);
|
2016-08-04 02:55:51 +03:00
|
|
|
cairo_stroke(cr);
|
|
|
|
|
2016-08-07 02:46:03 +03:00
|
|
|
if (style->wmax) {
|
|
|
|
cairo_new_path(cr);
|
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "%u(%d) %u %.60s\n", ty, ink_rect.y / PANGO_SCALE, ink_h, over->s);
|
|
|
|
#endif
|
2016-08-07 05:01:51 +03:00
|
|
|
/*
|
|
|
|
* @@@ for some mysterious reason, we get
|
|
|
|
* ink_h = ink_rect.height / PANGO_SCALE = 5
|
|
|
|
* instead of 2 if using overlay_style_dense_selected. Strangely, changing
|
|
|
|
* overlay_style_dense_selected such that it becomes more like
|
|
|
|
* overlay_style_dense has no effect.
|
|
|
|
*
|
|
|
|
* This causes the text to be cut vertically, roughly in the middle. We hack
|
|
|
|
* around this problem by growind the clipping area vertically. This works,
|
|
|
|
* since we're currently only concerned about horizontal clipping anyway.
|
|
|
|
*/
|
|
|
|
|
|
|
|
cairo_rectangle(cr, tx, ty, ink_w, ink_h + 20);
|
2016-08-07 02:46:03 +03:00
|
|
|
cairo_clip(cr);
|
|
|
|
}
|
|
|
|
|
2016-08-09 22:01:01 +03:00
|
|
|
cairo_set_source_rgba(cr, fg->r, fg->g, fg->b, fg->alpha);
|
2016-08-07 02:46:03 +03:00
|
|
|
cairo_move_to(cr, tx, ty);
|
2016-08-06 19:26:59 +03:00
|
|
|
|
|
|
|
pango_cairo_update_layout(cr, layout);
|
|
|
|
pango_cairo_show_layout(cr, layout);
|
2016-08-07 02:46:03 +03:00
|
|
|
cairo_reset_clip(cr);
|
|
|
|
g_object_unref(layout);
|
2016-08-04 02:55:51 +03:00
|
|
|
|
2016-08-14 20:35:57 +03:00
|
|
|
if (over->hover || over->click || over->drag) {
|
2016-08-07 11:09:34 +03:00
|
|
|
struct aoi aoi_cfg = {
|
2016-08-14 20:52:07 +03:00
|
|
|
.x = x,
|
|
|
|
.y = y,
|
2016-08-04 13:53:25 +03:00
|
|
|
.w = w,
|
|
|
|
.h = h,
|
|
|
|
.hover = over->hover,
|
|
|
|
.click = over->click,
|
2016-08-14 19:01:35 +03:00
|
|
|
.drag = over->drag,
|
2016-08-04 13:53:25 +03:00
|
|
|
.user = over->user,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (over->aoi)
|
2016-08-07 11:09:34 +03:00
|
|
|
aoi_update(over->aoi, &aoi_cfg);
|
|
|
|
else
|
|
|
|
over->aoi = aoi_add(over->aois, &aoi_cfg);
|
2016-08-04 13:53:25 +03:00
|
|
|
}
|
|
|
|
|
2016-08-14 20:35:57 +03:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void overlay_draw_all_d(struct overlay *overlays, cairo_t *cr,
|
|
|
|
unsigned x, unsigned y, int dx, int dy)
|
|
|
|
{
|
2016-08-15 10:47:43 +03:00
|
|
|
struct overlay *over = overlays;
|
2016-08-14 20:35:57 +03:00
|
|
|
unsigned h;
|
2016-08-05 02:35:41 +03:00
|
|
|
|
2016-08-15 10:47:43 +03:00
|
|
|
if (dy < 0)
|
|
|
|
while (over && over->next)
|
|
|
|
over = over->next;
|
|
|
|
while (over) {
|
2016-08-14 20:35:57 +03:00
|
|
|
h = overlay_draw(over, cr, x, y, dx, dy);
|
|
|
|
y += dy * (h + over->style.skip);
|
2016-08-15 10:47:43 +03:00
|
|
|
if (dy >= 0)
|
|
|
|
over = over->next;
|
|
|
|
else
|
|
|
|
over = over->prev;
|
|
|
|
|
2016-08-14 20:35:57 +03:00
|
|
|
}
|
2016-08-04 02:55:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-07 12:08:42 +03:00
|
|
|
void overlay_draw_all(struct overlay *overlays, cairo_t *cr, int x, int y)
|
2016-08-04 02:55:51 +03:00
|
|
|
{
|
2016-08-14 20:35:57 +03:00
|
|
|
int dx = 1;
|
|
|
|
int dy = 1;
|
2016-08-04 02:55:51 +03:00
|
|
|
|
2016-08-15 10:33:34 +03:00
|
|
|
if (x < 0 || y < 0) {
|
|
|
|
double x1, y1, x2, y2;
|
|
|
|
int sw, sh;
|
|
|
|
|
|
|
|
cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
|
|
|
|
sw = x2 - x1;
|
|
|
|
sh = y2 - y1;
|
|
|
|
if (x < 0) {
|
|
|
|
x = sw + x;
|
|
|
|
dx = -1;
|
|
|
|
}
|
|
|
|
if (y < 0) {
|
|
|
|
y = sh + y;
|
|
|
|
dy = -1;
|
|
|
|
}
|
2016-08-14 20:35:57 +03:00
|
|
|
}
|
2016-08-15 10:33:34 +03:00
|
|
|
|
2016-08-14 20:35:57 +03:00
|
|
|
overlay_draw_all_d(overlays, cr, x, y, dx, dy);
|
2016-08-04 02:55:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-07 01:14:31 +03:00
|
|
|
struct overlay *overlay_add(struct overlay **overlays, struct aoi **aois,
|
2016-08-04 13:53:25 +03:00
|
|
|
bool (*hover)(void *user, bool on), void (*click)(void *user), void *user)
|
2016-08-04 02:55:51 +03:00
|
|
|
{
|
2016-08-15 10:47:43 +03:00
|
|
|
struct overlay *over, *prev;
|
2016-08-04 02:55:51 +03:00
|
|
|
struct overlay **anchor;
|
2016-08-05 01:25:31 +03:00
|
|
|
|
2016-08-04 02:55:51 +03:00
|
|
|
over = alloc_type(struct overlay);
|
2016-08-07 01:14:31 +03:00
|
|
|
over->s = NULL;
|
2016-08-09 19:12:10 +03:00
|
|
|
over->style = overlay_style_default;
|
2016-08-04 02:55:51 +03:00
|
|
|
|
2016-08-04 13:53:25 +03:00
|
|
|
over->aois = aois;
|
|
|
|
over->hover = hover;
|
|
|
|
over->click = click;
|
|
|
|
over->user = user;
|
|
|
|
over->aoi = NULL;
|
|
|
|
|
2016-08-15 10:47:43 +03:00
|
|
|
prev = NULL;
|
|
|
|
for (anchor = overlays; *anchor; anchor = &(*anchor)->next)
|
|
|
|
prev = *anchor;
|
2016-08-04 02:55:51 +03:00
|
|
|
over->next = NULL;
|
2016-08-15 10:47:43 +03:00
|
|
|
over->prev = prev;
|
2016-08-04 02:55:51 +03:00
|
|
|
*anchor = over;
|
|
|
|
|
|
|
|
return over;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-07 02:46:03 +03:00
|
|
|
void overlay_style(struct overlay *over, const struct overlay_style *style)
|
|
|
|
{
|
2016-08-09 19:12:10 +03:00
|
|
|
over->style = *style;
|
2016-08-07 02:46:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-14 19:01:35 +03:00
|
|
|
void overlay_draggable(struct overlay *over,
|
|
|
|
void (*drag)(void *user, int dx, int dy))
|
|
|
|
{
|
|
|
|
over->drag = drag;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-07 11:09:34 +03:00
|
|
|
void overlay_text_raw(struct overlay *over, const char *s)
|
|
|
|
{
|
|
|
|
free((char *) over->s);
|
|
|
|
over->s = stralloc(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-07 01:14:31 +03:00
|
|
|
void overlay_text(struct overlay *over, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2016-08-07 11:09:34 +03:00
|
|
|
overlay_text_raw(over, vfmt_pango(fmt, ap));
|
2016-08-07 01:14:31 +03:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-04 02:55:51 +03:00
|
|
|
static void overlay_free(struct overlay *over)
|
|
|
|
{
|
2016-08-04 13:53:25 +03:00
|
|
|
if (over->aoi)
|
|
|
|
aoi_remove(over->aois, over->aoi);
|
2016-08-04 02:55:51 +03:00
|
|
|
free((void *) over->s);
|
|
|
|
free(over);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void overlay_remove(struct overlay **overlays, struct overlay *over)
|
|
|
|
{
|
2016-08-15 10:47:43 +03:00
|
|
|
if (over->next)
|
|
|
|
over->next->prev = over->prev;
|
|
|
|
if (over->prev)
|
|
|
|
over->prev->next = over->next;
|
|
|
|
else
|
|
|
|
*overlays = over->next;
|
|
|
|
overlay_free(over);
|
2016-08-04 02:55:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void overlay_remove_all(struct overlay **overlays)
|
|
|
|
{
|
|
|
|
struct overlay *next;
|
|
|
|
|
|
|
|
while (*overlays) {
|
|
|
|
next = (*overlays)->next;
|
|
|
|
overlay_free(*overlays);
|
|
|
|
*overlays = next;
|
|
|
|
}
|
|
|
|
}
|