2007-10-03 17:26:14 +02:00
|
|
|
/*
|
2007-09-12 14:29:51 +02:00
|
|
|
* draw.c - draw functions
|
2007-10-03 17:26:14 +02:00
|
|
|
*
|
2008-01-02 16:59:43 +01:00
|
|
|
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
|
2007-10-03 17:26:14 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2007-09-12 14:29:51 +02:00
|
|
|
*/
|
2007-09-05 20:15:00 +02:00
|
|
|
|
2008-04-28 08:51:30 +02:00
|
|
|
/* asprintf */
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
2008-03-21 16:50:17 +01:00
|
|
|
#include <cairo-xcb.h>
|
|
|
|
|
2008-06-03 11:16:25 +02:00
|
|
|
#include "config.h"
|
2008-05-23 17:17:38 +02:00
|
|
|
#ifdef WITH_IMLIB2
|
2008-04-09 19:56:54 +02:00
|
|
|
#include <Imlib2.h>
|
|
|
|
#else
|
2008-04-03 06:41:30 +02:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
|
|
|
#endif
|
2008-02-04 13:28:20 +01:00
|
|
|
|
2008-03-21 16:50:17 +01:00
|
|
|
#include <xcb/xcb.h>
|
|
|
|
#include <xcb/xcb_aux.h>
|
|
|
|
|
2008-02-04 13:28:20 +01:00
|
|
|
#include <langinfo.h>
|
|
|
|
#include <iconv.h>
|
|
|
|
#include <errno.h>
|
2008-03-21 16:50:17 +01:00
|
|
|
#include <string.h>
|
2008-04-25 15:50:36 +02:00
|
|
|
#include <ctype.h>
|
2007-10-11 17:06:55 +02:00
|
|
|
#include <math.h>
|
2008-02-04 13:28:20 +01:00
|
|
|
|
2008-04-28 08:51:30 +02:00
|
|
|
#include "common/draw.h"
|
2008-04-28 11:22:47 +02:00
|
|
|
#include "common/markup.h"
|
2007-09-05 20:15:00 +02:00
|
|
|
|
2008-02-27 09:32:45 +01:00
|
|
|
/** Convert text from any charset to UTF-8 using iconv
|
|
|
|
* \param iso the ISO string to convert
|
|
|
|
* \return NULL if error, otherwise pointer to the new converted string
|
|
|
|
*/
|
2008-06-10 07:32:35 +02:00
|
|
|
char *
|
2008-04-25 15:50:36 +02:00
|
|
|
draw_iso2utf8(const char *iso)
|
2008-02-04 13:28:20 +01:00
|
|
|
{
|
|
|
|
iconv_t iso2utf8;
|
|
|
|
size_t len, utf8len;
|
2008-02-11 10:52:05 +01:00
|
|
|
char *utf8, *utf8p;
|
2008-02-04 13:28:20 +01:00
|
|
|
|
|
|
|
if(!(len = a_strlen(iso)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if(!a_strcmp(nl_langinfo(CODESET), "UTF-8"))
|
2008-02-11 09:21:09 +01:00
|
|
|
return NULL;
|
2008-02-04 13:28:20 +01:00
|
|
|
|
|
|
|
iso2utf8 = iconv_open("UTF-8", nl_langinfo(CODESET));
|
|
|
|
if(iso2utf8 == (iconv_t) -1)
|
|
|
|
{
|
|
|
|
if(errno == EINVAL)
|
|
|
|
warn("unable to convert text from %s to UTF-8, not available",
|
|
|
|
nl_langinfo(CODESET));
|
|
|
|
else
|
2008-05-23 22:53:59 +02:00
|
|
|
warn("unable to convert text: %s", strerror(errno));
|
2008-02-04 13:28:20 +01:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-04-04 11:07:57 +02:00
|
|
|
utf8len = 2 * len + 1;
|
2008-02-11 10:52:05 +01:00
|
|
|
utf8 = utf8p = p_new(char, utf8len);
|
2008-02-04 13:28:20 +01:00
|
|
|
|
2008-04-25 15:50:36 +02:00
|
|
|
if(iconv(iso2utf8, (char **) &iso, &len, &utf8, &utf8len) == (size_t) -1)
|
2008-02-04 13:28:20 +01:00
|
|
|
{
|
2008-05-23 22:53:59 +02:00
|
|
|
warn("text conversion failed: %s", strerror(errno));
|
2008-02-11 10:52:05 +01:00
|
|
|
p_delete(&utf8p);
|
2008-02-04 13:28:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(iconv_close(iso2utf8))
|
|
|
|
warn("error closing iconv");
|
|
|
|
|
2008-02-11 10:52:05 +01:00
|
|
|
return utf8p;
|
2008-02-04 13:28:20 +01:00
|
|
|
}
|
|
|
|
|
2008-03-21 16:50:17 +01:00
|
|
|
static xcb_visualtype_t *
|
2008-04-09 13:26:02 +02:00
|
|
|
draw_screen_default_visual(xcb_screen_t *s)
|
2008-03-21 16:50:17 +01:00
|
|
|
{
|
|
|
|
xcb_depth_iterator_t depth_iter;
|
|
|
|
xcb_visualtype_iterator_t visual_iter;
|
|
|
|
|
|
|
|
if(!s)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for(depth_iter = xcb_screen_allowed_depths_iterator(s);
|
|
|
|
depth_iter.rem; xcb_depth_next (&depth_iter))
|
|
|
|
for(visual_iter = xcb_depth_visuals_iterator (depth_iter.data);
|
|
|
|
visual_iter.rem; xcb_visualtype_next (&visual_iter))
|
|
|
|
if (s->root_visual == visual_iter.data->visual_id)
|
|
|
|
return visual_iter.data;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-06-04 16:13:41 +02:00
|
|
|
/** Create a new draw context.
|
|
|
|
* \param conn Connection ref.
|
|
|
|
* \param phys_screen Physical screen id.
|
|
|
|
* \param width Width.
|
|
|
|
* \param height Height.
|
|
|
|
* \param px Pixmap object to store.
|
2008-06-02 12:18:17 +02:00
|
|
|
* \param fg Foreground color.
|
|
|
|
* \param bg Background color.
|
2008-06-04 16:13:41 +02:00
|
|
|
* \return A draw context pointer.
|
2007-12-30 15:11:37 +01:00
|
|
|
*/
|
2008-04-28 15:32:30 +02:00
|
|
|
draw_context_t *
|
2008-06-02 12:18:17 +02:00
|
|
|
draw_context_new(xcb_connection_t *conn, int phys_screen,
|
2008-06-04 16:13:41 +02:00
|
|
|
int width, int height, xcb_pixmap_t px,
|
2008-06-02 12:18:17 +02:00
|
|
|
xcolor_t fg, xcolor_t bg)
|
2007-12-15 00:12:17 +01:00
|
|
|
{
|
2008-04-28 15:32:30 +02:00
|
|
|
draw_context_t *d = p_new(draw_context_t, 1);
|
2008-03-21 16:50:17 +01:00
|
|
|
xcb_screen_t *s = xcb_aux_get_screen(conn, phys_screen);
|
2007-12-30 12:26:11 +01:00
|
|
|
|
2008-03-21 16:50:17 +01:00
|
|
|
d->connection = conn;
|
2007-12-15 00:12:17 +01:00
|
|
|
d->phys_screen = phys_screen;
|
|
|
|
d->width = width;
|
|
|
|
d->height = height;
|
2008-03-21 16:50:17 +01:00
|
|
|
d->depth = s->root_depth;
|
2008-04-09 13:26:02 +02:00
|
|
|
d->visual = draw_screen_default_visual(s);
|
2008-06-04 16:13:41 +02:00
|
|
|
d->pixmap = px;
|
|
|
|
d->surface = cairo_xcb_surface_create(conn, px, d->visual, width, height);
|
2008-01-31 18:18:15 +01:00
|
|
|
d->cr = cairo_create(d->surface);
|
2008-03-17 12:38:49 +01:00
|
|
|
d->layout = pango_cairo_create_layout(d->cr);
|
2008-06-02 12:18:17 +02:00
|
|
|
d->fg = fg;
|
|
|
|
d->bg = bg;
|
2007-12-30 12:26:11 +01:00
|
|
|
|
2007-12-15 00:12:17 +01:00
|
|
|
return d;
|
|
|
|
};
|
|
|
|
|
2008-03-17 12:38:49 +01:00
|
|
|
/** Create a new Pango font
|
2008-03-21 16:50:17 +01:00
|
|
|
* \param conn Connection ref
|
2008-03-17 12:38:49 +01:00
|
|
|
* \param fontname Pango fontname (e.g. [FAMILY-LIST] [STYLE-OPTIONS] [SIZE])
|
2008-03-21 11:26:56 +01:00
|
|
|
* \return a new font
|
2008-03-17 12:38:49 +01:00
|
|
|
*/
|
|
|
|
font_t *
|
2008-05-20 15:39:47 +02:00
|
|
|
draw_font_new(xcb_connection_t *conn, int phys_screen, const char *fontname)
|
2008-03-17 12:38:49 +01:00
|
|
|
{
|
|
|
|
cairo_surface_t *surface;
|
2008-03-21 16:50:17 +01:00
|
|
|
xcb_screen_t *s = xcb_aux_get_screen(conn, phys_screen);
|
2008-03-17 12:38:49 +01:00
|
|
|
cairo_t *cr;
|
|
|
|
PangoLayout *layout;
|
|
|
|
font_t *font = p_new(font_t, 1);
|
|
|
|
|
|
|
|
/* Create a dummy cairo surface, cairo context and pango layout in
|
|
|
|
* order to get font informations */
|
2008-03-21 16:50:17 +01:00
|
|
|
surface = cairo_xcb_surface_create(conn,
|
|
|
|
phys_screen,
|
2008-04-09 13:26:02 +02:00
|
|
|
draw_screen_default_visual(s),
|
2008-03-21 16:50:17 +01:00
|
|
|
s->width_in_pixels,
|
|
|
|
s->height_in_pixels);
|
2008-03-17 12:38:49 +01:00
|
|
|
|
|
|
|
cr = cairo_create(surface);
|
|
|
|
layout = pango_cairo_create_layout(cr);
|
|
|
|
|
|
|
|
/* Get the font description used to set text on a PangoLayout */
|
|
|
|
font->desc = pango_font_description_from_string(fontname);
|
|
|
|
pango_layout_set_font_description(layout, font->desc);
|
|
|
|
|
|
|
|
/* Get height */
|
|
|
|
pango_layout_get_pixel_size(layout, NULL, &font->height);
|
|
|
|
|
2008-03-17 17:55:38 +01:00
|
|
|
/* At the moment, we don't need ascent/descent but maybe it could
|
|
|
|
* be useful in the future... */
|
|
|
|
#if 0
|
|
|
|
PangoContext *context;
|
|
|
|
PangoFontMetrics *font_metrics;
|
|
|
|
|
2008-03-17 12:38:49 +01:00
|
|
|
/* Get ascent and descent */
|
|
|
|
context = pango_layout_get_context(layout);
|
|
|
|
font_metrics = pango_context_get_metrics(context, font->desc, NULL);
|
|
|
|
|
|
|
|
/* Values in PangoFontMetrics are given in Pango units */
|
|
|
|
font->ascent = PANGO_PIXELS(pango_font_metrics_get_ascent(font_metrics));
|
|
|
|
font->descent = PANGO_PIXELS(pango_font_metrics_get_descent(font_metrics));
|
|
|
|
|
|
|
|
pango_font_metrics_unref(font_metrics);
|
2008-03-17 17:55:38 +01:00
|
|
|
#endif
|
|
|
|
|
2008-03-17 12:38:49 +01:00
|
|
|
g_object_unref(layout);
|
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Delete a font
|
|
|
|
* \param font font_t to delete
|
|
|
|
*/
|
|
|
|
void
|
2008-03-21 11:26:56 +01:00
|
|
|
draw_font_delete(font_t **font)
|
2008-03-17 12:38:49 +01:00
|
|
|
{
|
2008-05-21 17:02:22 +02:00
|
|
|
if(*font)
|
|
|
|
{
|
|
|
|
pango_font_description_free((*font)->desc);
|
|
|
|
p_delete(font);
|
|
|
|
}
|
2008-03-17 12:38:49 +01:00
|
|
|
}
|
|
|
|
|
2008-04-26 19:02:19 +02:00
|
|
|
typedef struct
|
2008-04-25 15:50:36 +02:00
|
|
|
{
|
2008-04-26 19:02:19 +02:00
|
|
|
xcb_connection_t *connection;
|
|
|
|
int phys_screen;
|
|
|
|
char *text;
|
2008-04-28 13:24:23 +02:00
|
|
|
alignment_t align;
|
2008-04-28 14:53:32 +02:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
int left, right;
|
|
|
|
} margin;
|
2008-04-26 19:02:19 +02:00
|
|
|
bool has_bg_color;
|
|
|
|
xcolor_t bg_color;
|
2008-05-20 15:39:47 +02:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
xcolor_t color;
|
|
|
|
} shadow;
|
2008-04-26 19:02:19 +02:00
|
|
|
} draw_parser_data_t;
|
|
|
|
|
|
|
|
static bool
|
|
|
|
draw_text_markup_expand(draw_parser_data_t *data,
|
|
|
|
const char *str, ssize_t slen)
|
|
|
|
{
|
2008-04-28 14:53:32 +02:00
|
|
|
const char *elements[] = { "bg", "text", "margin", NULL };
|
2008-04-28 11:22:47 +02:00
|
|
|
markup_parser_data_t *p;
|
|
|
|
int i;
|
|
|
|
|
2008-04-28 11:37:53 +02:00
|
|
|
p = markup_parser_data_new(elements, NULL, countof(elements));
|
2008-04-28 11:22:47 +02:00
|
|
|
|
|
|
|
if(!markup_parse(p, str, slen))
|
2008-04-28 21:48:17 +02:00
|
|
|
{
|
|
|
|
markup_parser_data_delete(&p);
|
|
|
|
return false;
|
|
|
|
}
|
2008-04-25 15:50:36 +02:00
|
|
|
|
2008-04-28 11:22:47 +02:00
|
|
|
/* bg */
|
2008-04-28 11:37:53 +02:00
|
|
|
if(p->attribute_names[0])
|
|
|
|
for(i = 0; p->attribute_names[0][i]; i++)
|
|
|
|
if(!a_strcmp(p->attribute_names[0][i], "color"))
|
2008-05-30 12:34:38 +02:00
|
|
|
data->has_bg_color = xcolor_new(data->connection, data->phys_screen,
|
2008-04-28 11:22:47 +02:00
|
|
|
p->attribute_values[0][i], &data->bg_color);
|
|
|
|
|
2008-04-28 13:24:23 +02:00
|
|
|
/* text */
|
|
|
|
if(p->attribute_names[1])
|
|
|
|
for(i = 0; p->attribute_names[1][i]; i++)
|
2008-04-28 16:16:02 +02:00
|
|
|
{
|
2008-04-28 13:24:23 +02:00
|
|
|
if(!a_strcmp(p->attribute_names[1][i], "align"))
|
|
|
|
data->align = draw_align_get_from_str(p->attribute_values[1][i]);
|
2008-04-28 16:16:02 +02:00
|
|
|
else if(!a_strcmp(p->attribute_names[1][i], "shadow"))
|
2008-05-30 12:34:38 +02:00
|
|
|
xcolor_new(data->connection, data->phys_screen,
|
2008-04-28 16:16:02 +02:00
|
|
|
p->attribute_values[1][i], &data->shadow.color);
|
|
|
|
else if(!a_strcmp(p->attribute_names[1][i], "shadow_offset"))
|
|
|
|
data->shadow.offset = atoi(p->attribute_values[1][i]);
|
|
|
|
}
|
2008-04-28 13:24:23 +02:00
|
|
|
|
2008-04-28 14:53:32 +02:00
|
|
|
/* margin */
|
|
|
|
if(p->attribute_names[2])
|
|
|
|
for(i = 0; p->attribute_names[2][i]; i++)
|
|
|
|
{
|
|
|
|
if(!a_strcmp(p->attribute_names[2][i], "left"))
|
|
|
|
data->margin.left = atoi(p->attribute_values[2][i]);
|
|
|
|
else if(!a_strcmp(p->attribute_names[2][i], "right"))
|
|
|
|
data->margin.right = atoi(p->attribute_values[2][i]);
|
|
|
|
}
|
|
|
|
|
2008-04-28 11:22:47 +02:00
|
|
|
/* stole text */
|
|
|
|
data->text = p->text;
|
|
|
|
p->text = NULL;
|
|
|
|
|
|
|
|
markup_parser_data_delete(&p);
|
2008-04-26 19:02:19 +02:00
|
|
|
|
|
|
|
return true;
|
2008-04-25 15:50:36 +02:00
|
|
|
}
|
|
|
|
|
2008-04-29 11:51:59 +02:00
|
|
|
/** Draw text into a draw context.
|
|
|
|
* \param ctx Draw context to draw to.
|
|
|
|
* \param area Area to draw to.
|
|
|
|
* \param text Text to draw.
|
|
|
|
* \param style A pointer to the style to use.
|
2007-12-30 15:11:37 +01:00
|
|
|
*/
|
2007-09-15 15:16:53 +02:00
|
|
|
void
|
2008-05-20 15:39:47 +02:00
|
|
|
draw_text(draw_context_t *ctx, font_t *font,
|
2008-06-02 12:18:17 +02:00
|
|
|
area_t area, const char *text)
|
2007-09-05 20:15:00 +02:00
|
|
|
{
|
2008-04-25 15:50:36 +02:00
|
|
|
int x, y;
|
2008-02-09 16:23:16 +01:00
|
|
|
ssize_t len, olen;
|
2008-02-11 10:40:38 +01:00
|
|
|
char *buf = NULL, *utf8 = NULL;
|
2008-04-25 15:50:36 +02:00
|
|
|
PangoRectangle ext;
|
2008-04-26 19:02:19 +02:00
|
|
|
draw_parser_data_t parser_data;
|
2007-09-05 20:15:00 +02:00
|
|
|
|
2008-04-25 15:50:36 +02:00
|
|
|
if(!(len = a_strlen(text)))
|
2007-09-05 20:15:00 +02:00
|
|
|
return;
|
2007-10-16 01:20:03 +02:00
|
|
|
|
2008-06-10 07:32:35 +02:00
|
|
|
utf8 = a_strdup(text);
|
2008-04-28 11:22:47 +02:00
|
|
|
|
|
|
|
p_clear(&parser_data, 1);
|
2008-04-26 19:02:19 +02:00
|
|
|
parser_data.connection = ctx->connection;
|
|
|
|
parser_data.phys_screen = ctx->phys_screen;
|
|
|
|
if(draw_text_markup_expand(&parser_data, utf8, len))
|
|
|
|
{
|
|
|
|
buf = parser_data.text;
|
|
|
|
p_delete(&utf8);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
buf = utf8;
|
2008-02-04 13:28:20 +01:00
|
|
|
|
2008-04-25 15:50:36 +02:00
|
|
|
len = olen = a_strlen(buf);
|
|
|
|
|
2008-04-26 19:02:19 +02:00
|
|
|
if(parser_data.has_bg_color)
|
|
|
|
draw_rectangle(ctx, area, 1.0, true, parser_data.bg_color);
|
2007-10-16 01:20:03 +02:00
|
|
|
|
2008-04-28 14:53:32 +02:00
|
|
|
pango_layout_set_width(ctx->layout,
|
|
|
|
pango_units_from_double(area.width
|
|
|
|
- (parser_data.margin.left
|
|
|
|
+ parser_data.margin.right)));
|
2008-04-25 15:50:36 +02:00
|
|
|
pango_layout_set_ellipsize(ctx->layout, PANGO_ELLIPSIZE_END);
|
2008-04-23 12:57:01 +02:00
|
|
|
pango_layout_set_markup(ctx->layout, buf, len);
|
2008-05-20 15:39:47 +02:00
|
|
|
pango_layout_set_font_description(ctx->layout, font->desc);
|
2008-04-25 15:50:36 +02:00
|
|
|
pango_layout_get_pixel_extents(ctx->layout, NULL, &ext);
|
2008-03-07 17:40:40 +01:00
|
|
|
|
2008-04-28 14:53:32 +02:00
|
|
|
x = area.x + parser_data.margin.left;
|
2008-04-01 17:59:23 +02:00
|
|
|
/* + 1 is added for rounding, so that in any case of doubt we rather draw
|
|
|
|
* the text 1px lower than too high which usually results in a better type
|
|
|
|
* face */
|
2008-05-20 15:39:47 +02:00
|
|
|
y = area.y + (ctx->height - font->height + 1) / 2;
|
2008-02-09 16:23:16 +01:00
|
|
|
|
2008-04-28 13:24:23 +02:00
|
|
|
switch(parser_data.align)
|
2008-01-04 19:17:20 +01:00
|
|
|
{
|
2008-03-07 17:40:40 +01:00
|
|
|
case AlignCenter:
|
2008-04-25 15:50:36 +02:00
|
|
|
x += (area.width - ext.width) / 2;
|
2008-01-04 19:17:20 +01:00
|
|
|
break;
|
|
|
|
case AlignRight:
|
2008-04-25 15:50:36 +02:00
|
|
|
x += area.width - ext.width;
|
2008-01-04 19:17:20 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-01-31 18:18:15 +01:00
|
|
|
|
2008-04-28 16:16:02 +02:00
|
|
|
if(parser_data.shadow.offset)
|
2008-03-07 17:40:40 +01:00
|
|
|
{
|
2008-05-30 13:00:52 +02:00
|
|
|
cairo_set_source_rgba(ctx->cr,
|
|
|
|
parser_data.shadow.color.red / 65535.0,
|
|
|
|
parser_data.shadow.color.green / 65535.0,
|
|
|
|
parser_data.shadow.color.blue / 65535.0,
|
|
|
|
parser_data.shadow.color.alpha / 65535.0);
|
2008-05-20 15:39:47 +02:00
|
|
|
cairo_move_to(ctx->cr, x + parser_data.shadow.offset, y + parser_data.shadow.offset);
|
2008-03-17 12:38:49 +01:00
|
|
|
pango_cairo_update_layout(ctx->cr, ctx->layout);
|
|
|
|
pango_cairo_show_layout(ctx->cr, ctx->layout);
|
2008-03-07 17:40:40 +01:00
|
|
|
}
|
|
|
|
|
2008-04-28 16:16:02 +02:00
|
|
|
cairo_move_to(ctx->cr, x, y);
|
|
|
|
|
2008-05-30 13:00:52 +02:00
|
|
|
cairo_set_source_rgba(ctx->cr,
|
2008-06-02 12:18:17 +02:00
|
|
|
ctx->fg.red / 65535.0,
|
|
|
|
ctx->fg.green / 65535.0,
|
|
|
|
ctx->fg.blue / 65535.0,
|
|
|
|
ctx->fg.alpha / 65535.0);
|
2008-03-17 12:38:49 +01:00
|
|
|
pango_cairo_update_layout(ctx->cr, ctx->layout);
|
|
|
|
pango_cairo_show_layout(ctx->cr, ctx->layout);
|
2008-02-11 10:40:38 +01:00
|
|
|
|
2008-02-09 16:23:16 +01:00
|
|
|
p_delete(&buf);
|
2007-09-05 20:15:00 +02:00
|
|
|
}
|
|
|
|
|
2008-06-12 15:23:53 +02:00
|
|
|
/** Setup color-source for cairo (gradient or mono).
|
|
|
|
* \param ctx Draw context.
|
|
|
|
* \param rect x, y to x + x_offset, y + y_offset.
|
|
|
|
* \param color Color to use at start (x, y).
|
|
|
|
* \param pcolor_center Color at 50% of width.
|
|
|
|
* \param pcolor_end Color at pattern end (x + x_offset, y + y_offset).
|
|
|
|
* \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
|
2008-02-05 15:33:42 +01:00
|
|
|
*/
|
2008-02-27 09:32:45 +01:00
|
|
|
static cairo_pattern_t *
|
2008-04-28 15:32:30 +02:00
|
|
|
draw_setup_cairo_color_source(draw_context_t *ctx, area_t rect,
|
2008-03-21 16:50:17 +01:00
|
|
|
xcolor_t *pcolor, xcolor_t *pcolor_center,
|
|
|
|
xcolor_t *pcolor_end)
|
2008-02-05 15:33:42 +01:00
|
|
|
{
|
2008-02-27 09:32:45 +01:00
|
|
|
cairo_pattern_t *pat = NULL;
|
2008-02-05 15:33:42 +01:00
|
|
|
|
2008-03-02 21:53:18 +01:00
|
|
|
/* no need for a real pattern: */
|
2008-03-02 13:13:12 +01:00
|
|
|
if(!pcolor_end && !pcolor_center)
|
2008-05-30 13:00:52 +02:00
|
|
|
cairo_set_source_rgba(ctx->cr,
|
|
|
|
pcolor->red / 65535.0,
|
|
|
|
pcolor->green / 65535.0,
|
|
|
|
pcolor->blue / 65535.0,
|
|
|
|
pcolor->alpha / 65535.0);
|
2008-03-02 13:13:12 +01:00
|
|
|
else
|
2008-02-05 15:33:42 +01:00
|
|
|
{
|
2008-04-13 03:54:09 +02:00
|
|
|
pat = cairo_pattern_create_linear(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
|
2008-02-05 15:33:42 +01:00
|
|
|
|
2008-03-02 21:53:18 +01:00
|
|
|
/* pcolor is always set (so far in awesome) */
|
2008-06-12 15:23:53 +02:00
|
|
|
cairo_pattern_add_color_stop_rgba(pat, 0.0,
|
|
|
|
pcolor->red / 65535.0,
|
|
|
|
pcolor->green / 65535.0,
|
|
|
|
pcolor->blue / 65535.0,
|
|
|
|
pcolor->alpha / 65535.0);
|
2008-03-02 21:53:18 +01:00
|
|
|
|
|
|
|
if(pcolor_center)
|
2008-06-12 15:23:53 +02:00
|
|
|
cairo_pattern_add_color_stop_rgba(pat, 0.5,
|
|
|
|
pcolor_center->red / 65535.0,
|
|
|
|
pcolor_center->green / 65535.0,
|
|
|
|
pcolor_center->blue / 65535.0,
|
|
|
|
pcolor_center->alpha / 65535.0);
|
2008-03-02 21:53:18 +01:00
|
|
|
|
2008-02-05 15:33:42 +01:00
|
|
|
if(pcolor_end)
|
2008-06-12 15:23:53 +02:00
|
|
|
cairo_pattern_add_color_stop_rgba(pat, 1.0,
|
|
|
|
pcolor_end->red / 65535.0,
|
|
|
|
pcolor_end->green / 65535.0,
|
|
|
|
pcolor_end->blue / 65535.0,
|
|
|
|
pcolor_end->alpha / 65535.0);
|
2008-02-05 15:33:42 +01:00
|
|
|
else
|
2008-06-12 15:23:53 +02:00
|
|
|
cairo_pattern_add_color_stop_rgba(pat, 1.0,
|
|
|
|
pcolor->red / 65535.0,
|
|
|
|
pcolor->green / 65535.0,
|
|
|
|
pcolor->blue / 65535.0,
|
|
|
|
pcolor->alpha / 65535.0);
|
2008-02-05 15:33:42 +01:00
|
|
|
cairo_set_source(ctx->cr, pat);
|
|
|
|
}
|
2008-02-27 09:32:45 +01:00
|
|
|
return pat;
|
2008-02-05 15:33:42 +01:00
|
|
|
}
|
|
|
|
|
2008-04-17 17:30:10 +02:00
|
|
|
/** Draw rectangle inside the coordinates
|
2008-01-12 23:38:31 +01:00
|
|
|
* \param ctx Draw context
|
|
|
|
* \param geometry geometry
|
2008-03-21 18:02:40 +01:00
|
|
|
* \param line_width line width
|
2008-02-27 09:32:45 +01:00
|
|
|
* \param filled fill rectangle?
|
2008-01-12 23:38:31 +01:00
|
|
|
* \param color color to use
|
|
|
|
*/
|
2007-09-15 15:16:53 +02:00
|
|
|
void
|
2008-04-28 15:32:30 +02:00
|
|
|
draw_rectangle(draw_context_t *ctx, area_t geometry, float line_width, bool filled, xcolor_t color)
|
2007-09-05 20:15:00 +02:00
|
|
|
{
|
2008-01-31 18:18:15 +01:00
|
|
|
cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
|
2008-03-21 18:02:40 +01:00
|
|
|
cairo_set_line_width(ctx->cr, line_width);
|
2008-04-13 02:09:10 +02:00
|
|
|
cairo_set_miter_limit(ctx->cr, 10.0);
|
2008-03-26 03:18:21 +01:00
|
|
|
cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
|
2008-05-30 13:00:52 +02:00
|
|
|
cairo_set_source_rgba(ctx->cr,
|
|
|
|
color.red / 65535.0,
|
|
|
|
color.green / 65535.0,
|
|
|
|
color.blue / 65535.0,
|
|
|
|
color.alpha / 65535.0);
|
2007-09-05 20:15:00 +02:00
|
|
|
if(filled)
|
|
|
|
{
|
2008-03-21 11:26:56 +01:00
|
|
|
cairo_rectangle(ctx->cr, geometry.x, geometry.y,
|
|
|
|
geometry.width, geometry.height);
|
2008-01-31 18:18:15 +01:00
|
|
|
cairo_fill(ctx->cr);
|
2007-09-05 20:15:00 +02:00
|
|
|
}
|
2007-09-20 20:11:33 +02:00
|
|
|
else
|
2008-02-04 11:16:30 +01:00
|
|
|
{
|
2008-04-17 17:30:10 +02:00
|
|
|
cairo_rectangle(ctx->cr, geometry.x + line_width / 2.0, geometry.y + line_width / 2.0,
|
|
|
|
geometry.width - line_width, geometry.height - line_width);
|
2008-02-04 11:16:30 +01:00
|
|
|
cairo_stroke(ctx->cr);
|
|
|
|
}
|
|
|
|
}
|
2008-03-21 11:26:56 +01:00
|
|
|
|
2008-02-05 01:27:39 +01:00
|
|
|
/** Draw rectangle with gradient colors
|
|
|
|
* \param ctx Draw context
|
|
|
|
* \param geometry geometry
|
2008-03-21 18:02:40 +01:00
|
|
|
* \param line_width line width
|
2008-02-05 01:27:39 +01:00
|
|
|
* \param filled filled rectangle?
|
2008-03-02 21:53:18 +01:00
|
|
|
* \param pattern__x pattern start x coord
|
2008-03-02 13:13:12 +01:00
|
|
|
* \param pattern_width pattern width
|
|
|
|
* \param color color to use at start
|
|
|
|
* \param pcolor_center color at 50% of width
|
|
|
|
* \param pcolor_end color at pattern_start + pattern_width
|
2008-02-05 01:27:39 +01:00
|
|
|
*/
|
2008-02-04 11:16:30 +01:00
|
|
|
void
|
2008-04-28 15:32:30 +02:00
|
|
|
draw_rectangle_gradient(draw_context_t *ctx, area_t geometry, float line_width, bool filled,
|
2008-03-21 16:50:17 +01:00
|
|
|
area_t pattern_rect, xcolor_t *pcolor,
|
|
|
|
xcolor_t *pcolor_center, xcolor_t *pcolor_end)
|
2008-02-04 11:16:30 +01:00
|
|
|
{
|
|
|
|
cairo_pattern_t *pat;
|
|
|
|
|
|
|
|
cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
|
2008-03-21 18:02:40 +01:00
|
|
|
cairo_set_line_width(ctx->cr, line_width);
|
2008-04-13 02:09:10 +02:00
|
|
|
cairo_set_miter_limit(ctx->cr, 10.0);
|
2008-03-26 03:18:21 +01:00
|
|
|
cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
|
2008-02-04 11:16:30 +01:00
|
|
|
|
2008-03-02 21:53:18 +01:00
|
|
|
pat = draw_setup_cairo_color_source(ctx, pattern_rect, pcolor, pcolor_center, pcolor_end);
|
2008-02-04 11:16:30 +01:00
|
|
|
|
|
|
|
if(filled)
|
|
|
|
{
|
|
|
|
cairo_rectangle(ctx->cr, geometry.x, geometry.y, geometry.width, geometry.height);
|
|
|
|
cairo_fill(ctx->cr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cairo_rectangle(ctx->cr, geometry.x + 1, geometry.y, geometry.width - 1, geometry.height - 1);
|
2008-02-05 01:27:39 +01:00
|
|
|
cairo_stroke(ctx->cr);
|
2008-02-04 11:16:30 +01:00
|
|
|
}
|
2007-10-11 17:06:55 +02:00
|
|
|
|
2008-02-05 15:33:42 +01:00
|
|
|
if(pat)
|
|
|
|
cairo_pattern_destroy(pat);
|
2007-09-05 20:15:00 +02:00
|
|
|
}
|
|
|
|
|
2008-02-05 15:33:42 +01:00
|
|
|
/** Setup some cairo-things for drawing a graph
|
|
|
|
* \param ctx Draw context
|
|
|
|
*/
|
2008-01-25 21:39:08 +01:00
|
|
|
void
|
2008-04-28 15:32:30 +02:00
|
|
|
draw_graph_setup(draw_context_t *ctx)
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-01-31 19:40:47 +01:00
|
|
|
cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
|
|
|
|
cairo_set_line_width(ctx->cr, 1.0);
|
2008-04-18 23:54:55 +02:00
|
|
|
/* without it, it can draw over the path on sharp angles
|
|
|
|
* ...too long lines * (...graph_line) */
|
2008-03-26 03:18:21 +01:00
|
|
|
cairo_set_miter_limit(ctx->cr, 0.0);
|
|
|
|
cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
|
2008-01-25 21:39:08 +01:00
|
|
|
}
|
2008-02-27 09:32:45 +01:00
|
|
|
|
2008-06-12 15:21:25 +02:00
|
|
|
/** Draw a graph.
|
|
|
|
* \param ctx Draw context.
|
|
|
|
* \param x x offset of widget.
|
|
|
|
* \param y y offset of widget.
|
|
|
|
* \param w Width in pixels.
|
|
|
|
* \param from Array of starting-point offsets to draw a graph lines.
|
|
|
|
* \param to Array of end-point offsets to draw a graph lines.
|
|
|
|
* \param cur_index Current position in data-array (cycles around).
|
|
|
|
* \param grow Put new values to the left or to the right.
|
|
|
|
* \param pcolor Color at the left.
|
|
|
|
* \param pcolor_center Color in the center.
|
|
|
|
* \param pcolor_end Color at the right.
|
2008-02-05 15:33:42 +01:00
|
|
|
*/
|
2008-01-25 21:39:08 +01:00
|
|
|
void
|
2008-04-28 15:32:30 +02:00
|
|
|
draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index,
|
2008-04-11 11:19:23 +02:00
|
|
|
position_t grow, area_t patt_rect,
|
2008-03-21 16:50:17 +01:00
|
|
|
xcolor_t *pcolor, xcolor_t *pcolor_center, xcolor_t *pcolor_end)
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-06-12 15:21:25 +02:00
|
|
|
int i = -1;
|
|
|
|
float x = rect.x + 0.5; /* middle of a pixel */
|
2008-02-05 15:33:42 +01:00
|
|
|
cairo_pattern_t *pat;
|
2008-03-02 21:53:18 +01:00
|
|
|
|
2008-03-21 11:26:56 +01:00
|
|
|
pat = draw_setup_cairo_color_source(ctx, patt_rect,
|
|
|
|
pcolor, pcolor_center, pcolor_end);
|
2008-03-09 23:49:03 +01:00
|
|
|
|
2008-03-18 03:15:50 +01:00
|
|
|
if(grow == Right) /* draw from right to left */
|
2008-01-06 18:23:56 +01:00
|
|
|
{
|
2008-06-12 15:21:25 +02:00
|
|
|
x += rect.width - 1;
|
|
|
|
while(++i < rect.width)
|
2008-03-18 03:15:50 +01:00
|
|
|
{
|
2008-06-12 15:21:25 +02:00
|
|
|
cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
|
|
|
|
cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
|
2008-04-17 05:03:37 +02:00
|
|
|
x -= 1.0;
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-03-18 03:15:50 +01:00
|
|
|
if (--cur_index < 0)
|
2008-06-12 15:21:25 +02:00
|
|
|
cur_index = rect.width - 1;
|
2008-03-18 03:15:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* draw from left to right */
|
2008-06-12 15:21:25 +02:00
|
|
|
while(++i < rect.width)
|
2008-03-18 03:15:50 +01:00
|
|
|
{
|
2008-06-12 15:21:25 +02:00
|
|
|
cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
|
|
|
|
cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
|
2008-04-17 05:03:37 +02:00
|
|
|
x += 1.0;
|
2008-03-18 03:15:50 +01:00
|
|
|
|
|
|
|
if (--cur_index < 0)
|
2008-06-12 15:21:25 +02:00
|
|
|
cur_index = rect.width - 1;
|
2008-03-18 03:15:50 +01:00
|
|
|
}
|
2008-01-31 18:18:15 +01:00
|
|
|
|
2008-01-31 19:40:47 +01:00
|
|
|
cairo_stroke(ctx->cr);
|
2008-02-05 15:33:42 +01:00
|
|
|
|
|
|
|
if(pat)
|
|
|
|
cairo_pattern_destroy(pat);
|
2008-01-25 21:39:08 +01:00
|
|
|
}
|
2008-02-27 09:32:45 +01:00
|
|
|
|
2008-02-05 15:33:42 +01:00
|
|
|
/** Draw a line into a graph-widget
|
|
|
|
* \param ctx Draw context
|
|
|
|
* \param x x-offset of widget
|
|
|
|
* \param y y-offset of widget
|
|
|
|
* \param w width in pixels
|
|
|
|
* \param to array of offsets to draw the line through...
|
|
|
|
* \param cur_index current position in data-array (cycles around)
|
2008-03-18 03:15:50 +01:00
|
|
|
* \param grow put new values to the left or to the right
|
2008-03-02 13:13:12 +01:00
|
|
|
* \param pcolor color at the left
|
|
|
|
* \param pcolor_center color in the center
|
|
|
|
* \param pcolor_end color at the right
|
2008-02-05 15:33:42 +01:00
|
|
|
*/
|
2008-01-25 21:39:08 +01:00
|
|
|
void
|
2008-04-28 15:32:30 +02:00
|
|
|
draw_graph_line(draw_context_t *ctx, area_t rect, int *to, int cur_index,
|
2008-04-11 11:19:23 +02:00
|
|
|
position_t grow, area_t patt_rect,
|
2008-03-21 16:50:17 +01:00
|
|
|
xcolor_t *pcolor, xcolor_t *pcolor_center, xcolor_t *pcolor_end)
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-04-18 23:54:55 +02:00
|
|
|
int i, w;
|
|
|
|
float x, y;
|
2008-02-05 15:33:42 +01:00
|
|
|
cairo_pattern_t *pat;
|
|
|
|
|
2008-04-18 23:54:55 +02:00
|
|
|
/* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
|
|
|
|
* ... it won't fill some pixels! It also looks much nicer so.
|
|
|
|
* (since line-drawing is the last on the graph, no need to reset to _NONE) */
|
|
|
|
/* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
|
|
|
|
cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_SUBPIXEL);
|
2008-04-19 05:17:02 +02:00
|
|
|
/* a nicer, better visible line compared to 1.0 */
|
|
|
|
cairo_set_line_width(ctx->cr, 1.25);
|
2008-04-18 00:58:42 +02:00
|
|
|
|
2008-03-09 23:49:03 +01:00
|
|
|
pat = draw_setup_cairo_color_source(ctx, patt_rect, pcolor, pcolor_center, pcolor_end);
|
2008-03-02 21:53:18 +01:00
|
|
|
|
2008-04-18 23:54:55 +02:00
|
|
|
/* path through the centers of pixels */
|
|
|
|
x = rect.x + 0.5;
|
|
|
|
y = rect.y + 0.5;
|
2008-03-09 23:49:03 +01:00
|
|
|
w = rect.width;
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-04-18 23:54:55 +02:00
|
|
|
if(grow == Right)
|
|
|
|
{
|
|
|
|
/* go through the values from old to new. Begin with the oldest. */
|
|
|
|
if(++cur_index > w - 1)
|
2008-04-18 00:58:42 +02:00
|
|
|
cur_index = 0;
|
2008-04-14 02:57:17 +02:00
|
|
|
|
2008-04-18 23:54:55 +02:00
|
|
|
cairo_move_to(ctx->cr, x, y - to[cur_index]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* on the left border: fills a pixel also when there's only one value */
|
|
|
|
cairo_move_to(ctx->cr, x - 1.0, y - to[cur_index]);
|
2008-04-14 02:57:17 +02:00
|
|
|
|
|
|
|
for(i = 0; i < w; i++)
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-04-18 23:54:55 +02:00
|
|
|
cairo_line_to(ctx->cr, x, y - to[cur_index]);
|
|
|
|
x += 1.0;
|
2008-04-14 02:57:17 +02:00
|
|
|
|
2008-04-18 00:58:42 +02:00
|
|
|
/* cycles around the index */
|
|
|
|
if(grow == Right)
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-04-18 23:54:55 +02:00
|
|
|
if (++cur_index > w - 1)
|
2008-04-18 00:58:42 +02:00
|
|
|
cur_index = 0;
|
2008-01-25 21:39:08 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-18 00:58:42 +02:00
|
|
|
if(--cur_index < 0)
|
|
|
|
cur_index = w - 1;
|
2008-01-25 21:39:08 +01:00
|
|
|
}
|
|
|
|
}
|
2008-04-14 02:57:17 +02:00
|
|
|
|
2008-04-18 23:54:55 +02:00
|
|
|
/* onto the right border: fills a pixel also when there's only one value */
|
|
|
|
if(grow == Right)
|
|
|
|
cairo_line_to(ctx->cr, x, y - to[cur_index]);
|
|
|
|
|
2008-01-31 19:40:47 +01:00
|
|
|
cairo_stroke(ctx->cr);
|
2008-02-05 15:33:42 +01:00
|
|
|
|
|
|
|
if(pat)
|
|
|
|
cairo_pattern_destroy(pat);
|
2008-04-19 05:17:02 +02:00
|
|
|
/* reset line-width */
|
|
|
|
cairo_set_line_width(ctx->cr, 1.0);
|
2008-01-06 18:23:56 +01:00
|
|
|
}
|
|
|
|
|
2008-02-27 09:32:45 +01:00
|
|
|
/** Draw a circle
|
|
|
|
* \param ctx Draw context to draw to
|
|
|
|
* \param x x coordinate
|
|
|
|
* \param y y coordinate
|
|
|
|
* \param r size of the circle
|
|
|
|
* \param filled fill circle?
|
|
|
|
* \param color color to use
|
|
|
|
*/
|
2007-10-11 17:06:55 +02:00
|
|
|
void
|
2008-04-28 15:32:30 +02:00
|
|
|
draw_circle(draw_context_t *ctx, int x, int y, int r, bool filled, xcolor_t color)
|
2007-10-11 17:06:55 +02:00
|
|
|
{
|
2008-01-31 18:18:15 +01:00
|
|
|
cairo_set_line_width(ctx->cr, 1.0);
|
2008-05-30 13:00:52 +02:00
|
|
|
cairo_set_source_rgba(ctx->cr,
|
|
|
|
color.red / 65535.0,
|
|
|
|
color.green / 65535.0,
|
|
|
|
color.blue / 65535.0,
|
|
|
|
color.alpha / 65535.0);
|
2008-01-31 21:49:05 +01:00
|
|
|
|
|
|
|
cairo_new_sub_path(ctx->cr); /* don't draw from the old reference point to.. */
|
|
|
|
|
2007-10-11 17:06:55 +02:00
|
|
|
if(filled)
|
|
|
|
{
|
2008-01-31 18:18:15 +01:00
|
|
|
cairo_arc (ctx->cr, x + r, y + r, r, 0, 2 * M_PI);
|
|
|
|
cairo_fill(ctx->cr);
|
2007-10-11 17:06:55 +02:00
|
|
|
}
|
|
|
|
else
|
2008-01-31 18:18:15 +01:00
|
|
|
cairo_arc (ctx->cr, x + r, y + r, r - 1, 0, 2 * M_PI);
|
2007-10-11 17:06:55 +02:00
|
|
|
|
2008-01-31 18:18:15 +01:00
|
|
|
cairo_stroke(ctx->cr);
|
2007-10-11 17:06:55 +02:00
|
|
|
}
|
2007-09-06 22:09:00 +02:00
|
|
|
|
2008-02-27 09:32:45 +01:00
|
|
|
/** Draw an image from ARGB data to a draw context.
|
|
|
|
* Data should be stored as an array of alpha, red, blue, green for each pixel
|
|
|
|
* and the array size should be w * h elements long.
|
|
|
|
* \param ctx Draw context to draw to
|
|
|
|
* \param x x coordinate
|
|
|
|
* \param y y coordinate
|
|
|
|
* \param w width
|
|
|
|
* \param h height
|
|
|
|
* \param wanted_h wanted height: if > 0, image will be resized
|
|
|
|
* \param data the image pixels array
|
|
|
|
*/
|
2008-05-23 17:17:38 +02:00
|
|
|
void
|
|
|
|
draw_image_from_argb_data(draw_context_t *ctx, int x, int y, int w, int h,
|
|
|
|
int wanted_h, unsigned char *data)
|
2007-12-22 19:32:47 +01:00
|
|
|
{
|
2007-12-22 20:14:13 +01:00
|
|
|
double ratio;
|
2007-12-22 19:32:47 +01:00
|
|
|
cairo_t *cr;
|
2008-01-31 18:18:15 +01:00
|
|
|
cairo_surface_t *source;
|
2007-12-22 19:32:47 +01:00
|
|
|
|
2008-03-24 10:26:35 +01:00
|
|
|
source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h,
|
2008-04-02 17:14:09 +02:00
|
|
|
#if CAIRO_VERSION_MAJOR < 1 || (CAIRO_VERSION_MAJOR == 1 && CAIRO_VERSION_MINOR < 5) || (CAIRO_VERSION_MAJOR == 1 && CAIRO_VERSION_MINOR == 5 && CAIRO_VERSION_MICRO < 8)
|
2008-03-24 10:47:57 +01:00
|
|
|
sizeof(unsigned char) * 4 * w);
|
|
|
|
#else
|
2008-03-24 10:26:35 +01:00
|
|
|
cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w));
|
2008-03-24 10:47:57 +01:00
|
|
|
#endif
|
2008-01-31 18:18:15 +01:00
|
|
|
cr = cairo_create(ctx->surface);
|
2007-12-27 10:33:20 +01:00
|
|
|
if(wanted_h > 0 && h > 0)
|
2007-12-22 20:14:13 +01:00
|
|
|
{
|
2007-12-27 10:33:20 +01:00
|
|
|
ratio = (double) wanted_h / (double) h;
|
2007-12-22 20:14:13 +01:00
|
|
|
cairo_scale(cr, ratio, ratio);
|
|
|
|
cairo_set_source_surface(cr, source, x / ratio, y / ratio);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cairo_set_source_surface(cr, source, x, y);
|
2008-01-31 18:18:15 +01:00
|
|
|
|
2007-12-22 19:32:47 +01:00
|
|
|
cairo_paint(cr);
|
|
|
|
|
2008-02-17 06:13:26 +01:00
|
|
|
cairo_destroy(cr);
|
2007-12-22 19:32:47 +01:00
|
|
|
cairo_surface_destroy(source);
|
|
|
|
}
|
|
|
|
|
2008-06-03 20:45:48 +02:00
|
|
|
/** Draw an image (PNG format only) from a file to a draw context
|
|
|
|
* \param ctx Draw context to draw to
|
|
|
|
* \param x x coordinate
|
|
|
|
* \param y y coordinate
|
|
|
|
* \param wanted_h wanted height: if > 0, image will be resized
|
|
|
|
* \param filename file name to draw
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
draw_image_from_file(draw_context_t *ctx, int x, int y, int wanted_h, const char *filename)
|
|
|
|
{
|
|
|
|
draw_image_t *image = draw_image_new(filename);
|
|
|
|
|
|
|
|
if(image)
|
|
|
|
{
|
|
|
|
draw_image(ctx, x, y, wanted_h, image);
|
|
|
|
draw_image_delete(&image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-23 17:17:38 +02:00
|
|
|
#ifndef WITH_IMLIB2
|
2008-04-03 06:41:30 +02:00
|
|
|
|
2008-06-03 20:42:48 +02:00
|
|
|
/** Load an image (PNG Format only) from filename
|
|
|
|
* \param filename the image file to load
|
|
|
|
* \return a new image
|
|
|
|
*/
|
|
|
|
draw_image_t *draw_image_new(const char *filename)
|
|
|
|
{
|
|
|
|
draw_image_t *image;
|
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
GError *error=NULL;
|
|
|
|
|
|
|
|
if(!(pixbuf = gdk_pixbuf_new_from_file(filename,&error))) {
|
|
|
|
warn("cannot load image %s: %s", filename, error->message);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
image = p_new(draw_image_t, 1);
|
|
|
|
image->data = pixbuf;
|
|
|
|
image->width = gdk_pixbuf_get_width(pixbuf);
|
|
|
|
image->height = gdk_pixbuf_get_height(pixbuf);
|
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Delete an image
|
|
|
|
* \param image the image to delete
|
|
|
|
*/
|
|
|
|
void draw_image_delete(draw_image_t **image)
|
|
|
|
{
|
|
|
|
if (*image)
|
|
|
|
{
|
|
|
|
gdk_pixbuf_unref((*image)->data);
|
|
|
|
p_delete(image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Draw an image to a draw context
|
|
|
|
* \param ctx Draw context to draw to
|
|
|
|
* \param x x coordinate
|
|
|
|
* \param y y coordinate
|
|
|
|
* \param wanted_h wanted height: if > 0, image will be resized
|
|
|
|
* \param image the image to draw
|
|
|
|
*/
|
|
|
|
void draw_image(draw_context_t *ctx, int x, int y, int wanted_h, draw_image_t *image)
|
|
|
|
{
|
|
|
|
cairo_t *cr;
|
|
|
|
|
|
|
|
cr = cairo_create(ctx->surface);
|
|
|
|
if(wanted_h > 0 && image->height > 0)
|
|
|
|
{
|
|
|
|
double ratio = (double) wanted_h / (double) image->height;
|
|
|
|
cairo_scale(cr, ratio, ratio);
|
|
|
|
gdk_cairo_set_source_pixbuf(cr, image->data, x / ratio, y / ratio);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
gdk_cairo_set_source_pixbuf(cr, image->data, x, y);
|
|
|
|
|
|
|
|
cairo_paint(cr);
|
|
|
|
|
|
|
|
cairo_destroy(cr);
|
|
|
|
}
|
|
|
|
|
2008-04-03 06:41:30 +02:00
|
|
|
/** get an image size
|
|
|
|
* \param filename file name
|
|
|
|
* \return area_t structure with width and height set to image size
|
|
|
|
*/
|
|
|
|
area_t
|
|
|
|
draw_get_image_size(const char *filename)
|
|
|
|
{
|
|
|
|
area_t size = { -1, -1, -1, -1, NULL, NULL };
|
|
|
|
gint width, height;
|
|
|
|
|
|
|
|
if(gdk_pixbuf_get_file_info(filename, &width, &height))
|
|
|
|
{
|
|
|
|
size.width = width;
|
|
|
|
size.height = height;
|
|
|
|
}
|
|
|
|
else
|
2008-05-23 22:53:59 +02:00
|
|
|
warn("cannot load image %s: %s", filename, "format unrecognized");
|
2008-04-03 06:41:30 +02:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2008-05-23 17:17:38 +02:00
|
|
|
#else /* WITH_IMLIB2 */
|
2008-04-03 06:41:30 +02:00
|
|
|
|
2008-03-22 15:28:25 +01:00
|
|
|
static const char *
|
|
|
|
draw_imlib_load_strerror(Imlib_Load_Error e)
|
|
|
|
{
|
|
|
|
switch(e)
|
|
|
|
{
|
|
|
|
case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST:
|
|
|
|
return "no such file or directory";
|
|
|
|
case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY:
|
|
|
|
return "file is a directory";
|
|
|
|
case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ:
|
|
|
|
return "read permission denied";
|
|
|
|
case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT:
|
|
|
|
return "no loader for file format";
|
|
|
|
case IMLIB_LOAD_ERROR_PATH_TOO_LONG:
|
|
|
|
return "path too long";
|
|
|
|
case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT:
|
|
|
|
return "path component non existant";
|
|
|
|
case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY:
|
|
|
|
return "path compoment not a directory";
|
|
|
|
case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE:
|
|
|
|
return "path points oustide address space";
|
|
|
|
case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS:
|
|
|
|
return "too many symbolic links";
|
|
|
|
case IMLIB_LOAD_ERROR_OUT_OF_MEMORY:
|
|
|
|
return "out of memory";
|
|
|
|
case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS:
|
|
|
|
return "out of file descriptors";
|
|
|
|
case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE:
|
|
|
|
return "write permission denied";
|
|
|
|
case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE:
|
|
|
|
return "out of disk space";
|
|
|
|
case IMLIB_LOAD_ERROR_UNKNOWN:
|
|
|
|
return "unknown error, that's really bad";
|
|
|
|
case IMLIB_LOAD_ERROR_NONE:
|
|
|
|
return "no error, oops";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "unknown error";
|
|
|
|
}
|
|
|
|
|
2008-06-03 20:42:48 +02:00
|
|
|
|
|
|
|
/** Load an image (PNG Format only) from filename
|
|
|
|
* \param filename the image file to load
|
|
|
|
* \return a new image
|
|
|
|
*/
|
|
|
|
draw_image_t *draw_image_new(const char *filename)
|
|
|
|
{
|
|
|
|
int w, h, size, i;
|
|
|
|
DATA32 *data;
|
|
|
|
double alpha;
|
|
|
|
unsigned char *dataimg, *rdataimg;
|
|
|
|
Imlib_Image imimage;
|
|
|
|
Imlib_Load_Error e = IMLIB_LOAD_ERROR_NONE;
|
|
|
|
draw_image_t *image;
|
|
|
|
|
|
|
|
if(!(imimage = imlib_load_image_with_error_return(filename, &e)))
|
|
|
|
{
|
|
|
|
warn("cannot load image %s: %s", filename, draw_imlib_load_strerror(e));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
imlib_context_set_image(imimage);
|
|
|
|
|
|
|
|
w = imlib_image_get_width();
|
|
|
|
h = imlib_image_get_height();
|
|
|
|
|
|
|
|
size = w * h;
|
|
|
|
|
|
|
|
data = imlib_image_get_data_for_reading_only();
|
|
|
|
|
|
|
|
rdataimg = dataimg = p_new(unsigned char, size * 4);
|
|
|
|
|
|
|
|
for(i = 0; i < size; i++, dataimg += 4)
|
|
|
|
{
|
|
|
|
dataimg[3] = (data[i] >> 24) & 0xff; /* A */
|
|
|
|
/* cairo wants pre-multiplied alpha */
|
|
|
|
alpha = dataimg[3] / 255.0;
|
|
|
|
dataimg[2] = ((data[i] >> 16) & 0xff) * alpha; /* R */
|
|
|
|
dataimg[1] = ((data[i] >> 8) & 0xff) * alpha; /* G */
|
|
|
|
dataimg[0] = (data[i] & 0xff) * alpha; /* B */
|
|
|
|
}
|
|
|
|
|
|
|
|
image = p_new(draw_image_t, 1);
|
|
|
|
|
|
|
|
image->data = rdataimg;
|
|
|
|
image->width = w;
|
|
|
|
image->height = h;
|
|
|
|
|
|
|
|
imlib_free_image();
|
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Delete an image
|
|
|
|
* \param image the image to delete
|
|
|
|
*/
|
|
|
|
void draw_image_delete(draw_image_t **image)
|
|
|
|
{
|
|
|
|
if (*image)
|
|
|
|
{
|
|
|
|
p_delete(&(*image)->data);
|
|
|
|
p_delete(image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Draw an image to a draw context
|
|
|
|
* \param ctx Draw context to draw to
|
|
|
|
* \param x x coordinate
|
|
|
|
* \param y y coordinate
|
|
|
|
* \param wanted_h wanted height: if > 0, image will be resized
|
|
|
|
* \param image the image to draw
|
|
|
|
*/
|
|
|
|
void draw_image(draw_context_t *ctx, int x, int y, int wanted_h, draw_image_t *image)
|
|
|
|
{
|
|
|
|
draw_image_from_argb_data(ctx, x, y, image->width, image->height, wanted_h, image->data);
|
|
|
|
}
|
2007-12-22 15:37:43 +01:00
|
|
|
|
2008-03-22 15:28:25 +01:00
|
|
|
/** Get an image size
|
2008-02-27 09:32:45 +01:00
|
|
|
* \param filename file name
|
2008-03-14 09:37:25 +01:00
|
|
|
* \return area_t structure with width and height set to image size
|
2008-02-27 09:32:45 +01:00
|
|
|
*/
|
2008-03-14 09:37:25 +01:00
|
|
|
area_t
|
2008-03-19 17:51:12 +01:00
|
|
|
draw_get_image_size(const char *filename)
|
2007-12-22 15:37:43 +01:00
|
|
|
{
|
2008-03-14 09:37:25 +01:00
|
|
|
area_t size = { -1, -1, -1, -1, NULL, NULL };
|
2008-03-22 15:28:25 +01:00
|
|
|
Imlib_Image image;
|
|
|
|
Imlib_Load_Error e = IMLIB_LOAD_ERROR_NONE;
|
2007-12-22 15:37:43 +01:00
|
|
|
|
2008-03-22 15:28:25 +01:00
|
|
|
if((image = imlib_load_image_with_error_return(filename, &e)))
|
2008-01-23 10:48:08 +01:00
|
|
|
{
|
2008-03-22 15:28:25 +01:00
|
|
|
imlib_context_set_image(image);
|
2007-12-22 15:37:43 +01:00
|
|
|
|
2008-03-22 15:28:25 +01:00
|
|
|
size.width = imlib_image_get_width();
|
|
|
|
size.height = imlib_image_get_height();
|
|
|
|
|
|
|
|
imlib_free_image();
|
|
|
|
}
|
|
|
|
else
|
2008-05-23 22:53:59 +02:00
|
|
|
warn("cannot load image %s: %s", filename, draw_imlib_load_strerror(e));
|
2008-02-27 09:32:45 +01:00
|
|
|
|
2007-12-29 21:44:44 +01:00
|
|
|
return size;
|
2007-12-29 19:44:15 +01:00
|
|
|
}
|
2008-05-23 17:17:38 +02:00
|
|
|
#endif /* WITH_IMLIB2 */
|
2007-12-29 19:44:15 +01:00
|
|
|
|
2008-06-04 16:13:41 +02:00
|
|
|
/** Rotate a pixmap.
|
2008-05-30 17:53:10 +02:00
|
|
|
* \param ctx Draw context to draw with.
|
|
|
|
* \param src Drawable to draw from.
|
|
|
|
* \param dest Drawable to draw to.
|
|
|
|
* \param src_w Drawable width.
|
|
|
|
* \param src_h Drawable height.
|
|
|
|
* \param dest_w Drawable width.
|
|
|
|
* \param dest_h Drawable height.
|
|
|
|
* \param angle angle to rotate.
|
|
|
|
* \param tx Translate to this x coordinate.
|
|
|
|
* \param ty Translate to this y coordinate.
|
2008-02-27 09:32:45 +01:00
|
|
|
*/
|
2008-03-20 09:17:34 +01:00
|
|
|
void
|
2008-05-30 17:53:10 +02:00
|
|
|
draw_rotate(draw_context_t *ctx,
|
2008-06-04 16:13:41 +02:00
|
|
|
xcb_pixmap_t src, xcb_pixmap_t dest,
|
2008-05-30 17:53:10 +02:00
|
|
|
int src_w, int src_h,
|
|
|
|
int dest_w, int dest_h,
|
2008-03-20 09:17:34 +01:00
|
|
|
double angle, int tx, int ty)
|
2007-11-11 18:59:11 +01:00
|
|
|
{
|
|
|
|
cairo_surface_t *surface, *source;
|
|
|
|
cairo_t *cr;
|
2007-11-11 22:27:00 +01:00
|
|
|
|
2008-03-21 16:50:17 +01:00
|
|
|
surface = cairo_xcb_surface_create(ctx->connection, dest,
|
|
|
|
ctx->visual, dest_w, dest_h);
|
2008-05-30 17:53:10 +02:00
|
|
|
source = cairo_xcb_surface_create(ctx->connection, src,
|
|
|
|
ctx->visual, src_w, src_h);
|
2007-11-11 18:59:11 +01:00
|
|
|
cr = cairo_create (surface);
|
2007-11-11 19:49:42 +01:00
|
|
|
|
|
|
|
cairo_translate(cr, tx, ty);
|
2007-11-11 21:13:37 +01:00
|
|
|
cairo_rotate(cr, angle);
|
2007-11-11 18:59:11 +01:00
|
|
|
|
|
|
|
cairo_set_source_surface(cr, source, 0.0, 0.0);
|
2007-11-11 19:49:42 +01:00
|
|
|
cairo_paint(cr);
|
2007-11-11 18:59:11 +01:00
|
|
|
|
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(source);
|
2007-11-11 22:38:29 +01:00
|
|
|
cairo_surface_destroy(surface);
|
2007-11-11 18:59:11 +01:00
|
|
|
}
|
|
|
|
|
2008-06-08 10:58:48 +02:00
|
|
|
/** Return the width and height of a text in pixel.
|
|
|
|
* \param conn Connection ref.
|
|
|
|
* \param phys_screen Physical screen number.
|
|
|
|
* \param font Font to use.
|
|
|
|
* \param text The text.
|
|
|
|
* \return Text height and width.
|
2008-02-27 09:32:45 +01:00
|
|
|
*/
|
2008-04-23 17:44:09 +02:00
|
|
|
area_t
|
2008-06-08 10:58:48 +02:00
|
|
|
draw_text_extents(xcb_connection_t *conn, int phys_screen, font_t *font, const char *text)
|
2007-10-01 19:22:57 +02:00
|
|
|
{
|
2007-10-16 01:20:03 +02:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
2008-03-17 12:38:49 +01:00
|
|
|
PangoLayout *layout;
|
|
|
|
PangoRectangle ext;
|
2008-06-08 10:58:48 +02:00
|
|
|
xcb_screen_t *s = xcb_aux_get_screen(conn, phys_screen);
|
2008-04-23 17:44:09 +02:00
|
|
|
area_t geom = { 0, 0, 0, 0, NULL, NULL };
|
2008-04-23 12:57:01 +02:00
|
|
|
ssize_t len;
|
2008-04-25 15:50:36 +02:00
|
|
|
char *buf, *utf8;
|
2008-04-26 19:02:19 +02:00
|
|
|
draw_parser_data_t parser_data;
|
2007-10-16 01:20:03 +02:00
|
|
|
|
2008-04-23 12:57:01 +02:00
|
|
|
if(!(len = a_strlen(text)))
|
2008-04-23 17:44:09 +02:00
|
|
|
return geom;
|
2007-12-30 14:22:19 +01:00
|
|
|
|
2008-04-25 15:50:36 +02:00
|
|
|
/* try to convert it to UTF-8 */
|
2008-06-10 07:32:35 +02:00
|
|
|
utf8 = a_strdup(text);
|
2008-04-28 11:22:47 +02:00
|
|
|
|
|
|
|
p_clear(&parser_data, 1);
|
2008-04-26 19:02:19 +02:00
|
|
|
parser_data.connection = conn;
|
2008-06-08 10:58:48 +02:00
|
|
|
parser_data.phys_screen = phys_screen;
|
2008-04-26 19:02:19 +02:00
|
|
|
if(draw_text_markup_expand(&parser_data, utf8, len))
|
|
|
|
{
|
|
|
|
buf = parser_data.text;
|
|
|
|
p_delete(&utf8);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
buf = utf8;
|
2008-04-25 15:50:36 +02:00
|
|
|
|
|
|
|
len = a_strlen(buf);
|
|
|
|
|
2008-06-08 10:58:48 +02:00
|
|
|
surface = cairo_xcb_surface_create(conn, phys_screen,
|
2008-04-09 13:26:02 +02:00
|
|
|
draw_screen_default_visual(s),
|
2008-03-21 16:50:17 +01:00
|
|
|
s->width_in_pixels,
|
|
|
|
s->height_in_pixels);
|
2008-04-25 15:50:36 +02:00
|
|
|
|
2007-10-16 01:20:03 +02:00
|
|
|
cr = cairo_create(surface);
|
2008-03-17 12:38:49 +01:00
|
|
|
layout = pango_cairo_create_layout(cr);
|
2008-04-25 15:50:36 +02:00
|
|
|
pango_layout_set_markup(layout, buf, len);
|
2008-03-17 12:38:49 +01:00
|
|
|
pango_layout_set_font_description(layout, font->desc);
|
|
|
|
pango_layout_get_pixel_extents(layout, NULL, &ext);
|
|
|
|
g_object_unref(layout);
|
2007-10-16 01:20:03 +02:00
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
|
2008-04-23 17:44:09 +02:00
|
|
|
geom.width = ext.width;
|
|
|
|
geom.height = ext.height * 1.5;
|
|
|
|
|
2008-04-25 15:50:36 +02:00
|
|
|
p_delete(&buf);
|
|
|
|
|
2008-04-23 17:44:09 +02:00
|
|
|
return geom;
|
2007-09-06 22:09:00 +02:00
|
|
|
}
|
2007-10-15 13:40:52 +02:00
|
|
|
|
2008-04-11 11:18:23 +02:00
|
|
|
/** Transform a string to a alignment_t type.
|
2008-02-27 09:32:45 +01:00
|
|
|
* Recognized string are left, center or right. Everything else will be
|
|
|
|
* recognized as AlignAuto.
|
|
|
|
* \param align string with align text
|
2008-04-11 11:18:23 +02:00
|
|
|
* \return alignment_t type
|
2008-02-27 09:32:45 +01:00
|
|
|
*/
|
2008-04-11 11:18:23 +02:00
|
|
|
alignment_t
|
2008-03-19 11:45:27 +01:00
|
|
|
draw_align_get_from_str(const char *align)
|
2008-01-03 16:02:32 +01:00
|
|
|
{
|
2008-03-21 11:26:56 +01:00
|
|
|
if(!a_strcmp(align, "left"))
|
2008-02-08 10:59:55 +01:00
|
|
|
return AlignLeft;
|
2008-03-21 11:26:56 +01:00
|
|
|
else if(!a_strcmp(align, "center"))
|
2008-01-03 16:02:32 +01:00
|
|
|
return AlignCenter;
|
2008-03-21 11:26:56 +01:00
|
|
|
else if(!a_strcmp(align, "right"))
|
2008-01-03 16:02:32 +01:00
|
|
|
return AlignRight;
|
2008-03-21 11:26:56 +01:00
|
|
|
else if(!a_strcmp(align, "flex"))
|
2008-02-28 16:19:26 +01:00
|
|
|
return AlignFlex;
|
2008-01-03 16:02:32 +01:00
|
|
|
|
2008-02-08 10:59:55 +01:00
|
|
|
return AlignAuto;
|
2008-01-03 16:02:32 +01:00
|
|
|
}
|
|
|
|
|
2008-03-22 20:44:23 +01:00
|
|
|
#define RGB_COLOR_8_TO_16(i) (65535 * ((i) & 0xff) / 255)
|
|
|
|
|
2008-01-27 18:56:37 +01:00
|
|
|
/** Initialize an X color
|
2008-03-21 16:50:17 +01:00
|
|
|
* \param conn Connection ref
|
2008-02-27 09:32:45 +01:00
|
|
|
* \param phys_screen Physical screen number
|
2008-01-27 18:56:37 +01:00
|
|
|
* \param colstr Color specification
|
2008-03-21 16:50:17 +01:00
|
|
|
* \param color xcolor_t struct to store color to
|
2008-02-27 09:32:45 +01:00
|
|
|
* \return true if color allocation was successfull
|
2008-01-27 18:56:37 +01:00
|
|
|
*/
|
2008-03-21 16:50:17 +01:00
|
|
|
bool
|
2008-05-30 12:34:38 +02:00
|
|
|
xcolor_new(xcb_connection_t *conn, int phys_screen, const char *colstr, xcolor_t *color)
|
2008-01-27 18:56:37 +01:00
|
|
|
{
|
2008-03-21 16:50:17 +01:00
|
|
|
xcb_screen_t *s = xcb_aux_get_screen(conn, phys_screen);
|
2008-03-22 20:44:23 +01:00
|
|
|
xcb_alloc_color_reply_t *hexa_color = NULL;
|
|
|
|
xcb_alloc_named_color_reply_t *named_color = NULL;
|
2008-04-17 11:28:56 +02:00
|
|
|
unsigned long colnum;
|
|
|
|
uint16_t red, green, blue;
|
2008-05-30 12:35:57 +02:00
|
|
|
ssize_t len;
|
2008-05-30 13:00:52 +02:00
|
|
|
char *buf;
|
2008-01-27 18:56:37 +01:00
|
|
|
|
2008-05-30 12:35:57 +02:00
|
|
|
if(!(len = a_strlen(colstr)))
|
2008-03-21 16:50:17 +01:00
|
|
|
return false;
|
|
|
|
|
2008-03-22 20:44:23 +01:00
|
|
|
/* The color is given in RGB value */
|
2008-05-30 13:00:52 +02:00
|
|
|
if(colstr[0] == '#')
|
2008-03-21 16:50:17 +01:00
|
|
|
{
|
2008-03-22 20:44:23 +01:00
|
|
|
errno = 0;
|
2008-05-30 13:00:52 +02:00
|
|
|
if(len == 7)
|
|
|
|
{
|
|
|
|
colnum = strtoul(&colstr[1], NULL, 16);
|
|
|
|
color->alpha = 0xffff;
|
|
|
|
}
|
|
|
|
/* we have alpha */
|
|
|
|
else if(len == 9)
|
|
|
|
{
|
|
|
|
buf = a_strndup(colstr + 1, 6);
|
|
|
|
colnum = strtoul(buf, NULL, 16);
|
|
|
|
p_delete(&buf);
|
|
|
|
color->alpha = RGB_COLOR_8_TO_16(strtoul(&colstr[7], NULL, 16));
|
|
|
|
if(errno != 0)
|
|
|
|
{
|
|
|
|
warn("awesome: error, invalid color '%s'", colstr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
warn("awesome: error, invalid color '%s'", colstr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-03-22 20:44:23 +01:00
|
|
|
if(errno != 0)
|
|
|
|
{
|
2008-05-23 22:53:59 +02:00
|
|
|
warn("awesome: error, invalid color '%s'", colstr);
|
2008-03-22 20:44:23 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-04-17 11:28:56 +02:00
|
|
|
red = RGB_COLOR_8_TO_16(colnum >> 16);
|
|
|
|
green = RGB_COLOR_8_TO_16(colnum >> 8);
|
|
|
|
blue = RGB_COLOR_8_TO_16(colnum);
|
2008-03-22 20:44:23 +01:00
|
|
|
|
|
|
|
hexa_color = xcb_alloc_color_reply(conn,
|
|
|
|
xcb_alloc_color(conn,
|
|
|
|
s->default_colormap,
|
|
|
|
red, green, blue),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if(hexa_color)
|
|
|
|
{
|
|
|
|
color->pixel = hexa_color->pixel;
|
|
|
|
color->red = hexa_color->red;
|
|
|
|
color->green = hexa_color->green;
|
|
|
|
color->blue = hexa_color->blue;
|
2008-03-21 16:50:17 +01:00
|
|
|
|
2008-03-22 20:44:23 +01:00
|
|
|
p_delete(&hexa_color);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
named_color = xcb_alloc_named_color_reply(conn,
|
|
|
|
xcb_alloc_named_color(conn,
|
|
|
|
s->default_colormap,
|
2008-05-30 12:35:57 +02:00
|
|
|
len,
|
2008-03-22 20:44:23 +01:00
|
|
|
colstr),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if(named_color)
|
|
|
|
{
|
|
|
|
color->pixel = named_color->pixel;
|
|
|
|
color->red = named_color->visual_red;
|
|
|
|
color->green = named_color->visual_green;
|
|
|
|
color->blue = named_color->visual_blue;
|
2008-05-30 13:00:52 +02:00
|
|
|
color->alpha = 0xffff;
|
2008-03-21 16:50:17 +01:00
|
|
|
|
2008-03-22 20:44:23 +01:00
|
|
|
p_delete(&named_color);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2008-01-27 18:56:37 +01:00
|
|
|
|
2008-05-23 22:53:59 +02:00
|
|
|
warn("awesome: error, cannot allocate color '%s'", colstr);
|
2008-03-22 20:44:23 +01:00
|
|
|
return false;
|
2008-01-27 18:56:37 +01:00
|
|
|
}
|
|
|
|
|
2008-01-28 18:30:23 +01:00
|
|
|
/** Remove a area from a list of them,
|
2008-03-21 11:26:56 +01:00
|
|
|
* spliting the space between several area that can overlap
|
2008-01-28 18:30:23 +01:00
|
|
|
* \param head list head
|
|
|
|
* \param elem area to remove
|
|
|
|
*/
|
|
|
|
void
|
2008-03-14 09:37:25 +01:00
|
|
|
area_list_remove(area_t **head, area_t *elem)
|
2008-01-28 18:30:23 +01:00
|
|
|
{
|
2008-03-14 09:37:25 +01:00
|
|
|
area_t *r, inter, *extra, *rnext;
|
2008-01-28 18:30:23 +01:00
|
|
|
|
2008-02-11 17:15:28 +01:00
|
|
|
for(r = *head; r; r = rnext)
|
|
|
|
{
|
|
|
|
rnext = r->next;
|
2008-01-28 18:30:23 +01:00
|
|
|
if(area_intersect_area(*r, *elem))
|
|
|
|
{
|
|
|
|
/* remove it from the list */
|
|
|
|
area_list_detach(head, r);
|
|
|
|
|
|
|
|
inter = area_get_intersect_area(*r, *elem);
|
|
|
|
|
|
|
|
if(AREA_LEFT(inter) > AREA_LEFT(*r))
|
|
|
|
{
|
2008-03-14 09:37:25 +01:00
|
|
|
extra = p_new(area_t, 1);
|
2008-01-28 18:30:23 +01:00
|
|
|
extra->x = r->x;
|
|
|
|
extra->y = r->y;
|
|
|
|
extra->width = AREA_LEFT(inter) - r->x;
|
|
|
|
extra->height = r->height;
|
2008-02-11 17:15:28 +01:00
|
|
|
area_list_append(head, extra);
|
2008-01-28 18:30:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(AREA_TOP(inter) > AREA_TOP(*r))
|
|
|
|
{
|
2008-03-14 09:37:25 +01:00
|
|
|
extra = p_new(area_t, 1);
|
2008-01-28 18:30:23 +01:00
|
|
|
extra->x = r->x;
|
|
|
|
extra->y = r->y;
|
|
|
|
extra->width = r->width;
|
|
|
|
extra->height = AREA_TOP(inter) - r->y;
|
2008-02-11 17:15:28 +01:00
|
|
|
area_list_append(head, extra);
|
2008-01-28 18:30:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(AREA_RIGHT(inter) < AREA_RIGHT(*r))
|
|
|
|
{
|
2008-03-14 09:37:25 +01:00
|
|
|
extra = p_new(area_t, 1);
|
2008-01-28 18:30:23 +01:00
|
|
|
extra->x = AREA_RIGHT(inter);
|
|
|
|
extra->y = r->y;
|
|
|
|
extra->width = AREA_RIGHT(*r) - AREA_RIGHT(inter);
|
|
|
|
extra->height = r->height;
|
2008-02-11 17:15:28 +01:00
|
|
|
area_list_append(head, extra);
|
2008-01-28 18:30:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(AREA_BOTTOM(inter) < AREA_BOTTOM(*r))
|
|
|
|
{
|
2008-03-14 09:37:25 +01:00
|
|
|
extra = p_new(area_t, 1);
|
2008-01-28 18:30:23 +01:00
|
|
|
extra->x = r->x;
|
|
|
|
extra->y = AREA_BOTTOM(inter);
|
|
|
|
extra->width = r->width;
|
|
|
|
extra->height = AREA_BOTTOM(*r) - AREA_BOTTOM(inter);
|
2008-02-11 17:15:28 +01:00
|
|
|
area_list_append(head, extra);
|
2008-01-28 18:30:23 +01:00
|
|
|
}
|
2008-02-11 17:15:28 +01:00
|
|
|
|
|
|
|
/* delete the elem since we removed it from the list */
|
|
|
|
p_delete(&r);
|
2008-01-28 18:30:23 +01:00
|
|
|
}
|
2008-02-11 17:15:28 +01:00
|
|
|
}
|
2008-01-28 18:30:23 +01:00
|
|
|
}
|
|
|
|
|
2007-12-18 09:24:15 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|