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
|
|
|
|
2007-10-10 19:59:14 +02:00
|
|
|
#include <cairo.h>
|
|
|
|
#include <cairo-xlib.h>
|
2008-02-04 13:28:20 +01:00
|
|
|
|
|
|
|
#include <langinfo.h>
|
|
|
|
#include <iconv.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2007-10-11 17:06:55 +02:00
|
|
|
#include <math.h>
|
2008-02-04 13:28:20 +01:00
|
|
|
|
2008-01-24 18:43:24 +01:00
|
|
|
#include "draw.h"
|
2008-01-21 18:14:59 +01:00
|
|
|
#include "common/util.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-02-04 13:28:20 +01:00
|
|
|
static char *
|
|
|
|
draw_iso2utf8(char *iso)
|
|
|
|
{
|
|
|
|
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-03-19 13:11:13 +01:00
|
|
|
warn("unable to convert text: %s\n", strerror(errno));
|
2008-02-04 13:28:20 +01:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
utf8len = (3 * len) / 2 + 1;
|
2008-02-11 10:52:05 +01:00
|
|
|
utf8 = utf8p = p_new(char, utf8len);
|
2008-02-04 13:28:20 +01:00
|
|
|
|
|
|
|
if(iconv(iso2utf8, &iso, &len, &utf8, &utf8len) == (size_t) -1)
|
|
|
|
{
|
2008-03-19 13:11:13 +01:00
|
|
|
warn("text conversion failed: %s\n", strerror(errno));
|
2008-02-11 10:52:05 +01:00
|
|
|
p_delete(&utf8p);
|
2008-02-04 13:28:20 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-12-30 15:11:37 +01:00
|
|
|
/** Get a draw context
|
2008-02-27 09:32:45 +01:00
|
|
|
* \param disp Display ref
|
2007-12-30 15:11:37 +01:00
|
|
|
* \param phys_screen physical screen id
|
|
|
|
* \param width width
|
|
|
|
* \param height height
|
2008-02-27 09:32:45 +01:00
|
|
|
* \param dw Drawable object to store in DrawCtx
|
2007-12-30 15:11:37 +01:00
|
|
|
* \return draw context ref
|
|
|
|
*/
|
2007-12-15 00:12:17 +01:00
|
|
|
DrawCtx *
|
2008-01-27 19:02:08 +01:00
|
|
|
draw_context_new(Display *disp, int phys_screen, int width, int height, Drawable dw)
|
2007-12-15 00:12:17 +01:00
|
|
|
{
|
2007-12-30 12:26:11 +01:00
|
|
|
DrawCtx *d = p_new(DrawCtx, 1);
|
|
|
|
|
2008-01-24 18:43:24 +01:00
|
|
|
d->display = disp;
|
2007-12-15 00:12:17 +01:00
|
|
|
d->phys_screen = phys_screen;
|
|
|
|
d->width = width;
|
|
|
|
d->height = height;
|
2008-01-24 18:43:24 +01:00
|
|
|
d->depth = DefaultDepth(disp, phys_screen);
|
|
|
|
d->visual = DefaultVisual(disp, phys_screen);
|
2008-01-23 19:10:53 +01:00
|
|
|
d->drawable = dw;
|
2008-01-31 18:18:15 +01:00
|
|
|
d->surface = cairo_xlib_surface_create(disp, dw, d->visual, width, height);
|
|
|
|
d->cr = cairo_create(d->surface);
|
2008-03-17 12:38:49 +01:00
|
|
|
d->layout = pango_cairo_create_layout(d->cr);
|
2007-12-30 12:26:11 +01:00
|
|
|
|
2007-12-15 00:12:17 +01:00
|
|
|
return d;
|
|
|
|
};
|
|
|
|
|
2008-02-27 09:32:45 +01:00
|
|
|
/** Delete a draw context
|
|
|
|
* \param ctx DrawCtx to delete
|
|
|
|
*/
|
2008-01-31 18:18:15 +01:00
|
|
|
void
|
|
|
|
draw_context_delete(DrawCtx *ctx)
|
|
|
|
{
|
2008-03-17 12:38:49 +01:00
|
|
|
g_object_unref(ctx->layout);
|
2008-01-31 18:18:15 +01:00
|
|
|
cairo_surface_destroy(ctx->surface);
|
|
|
|
cairo_destroy(ctx->cr);
|
|
|
|
p_delete(&ctx);
|
|
|
|
}
|
|
|
|
|
2008-03-17 12:38:49 +01:00
|
|
|
/** Create a new Pango font
|
|
|
|
* \param disp Display ref
|
|
|
|
* \param fontname Pango fontname (e.g. [FAMILY-LIST] [STYLE-OPTIONS] [SIZE])
|
|
|
|
*/
|
|
|
|
font_t *
|
|
|
|
draw_font_new(Display *disp, char *fontname)
|
|
|
|
{
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
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 */
|
|
|
|
surface = cairo_xlib_surface_create(disp,
|
|
|
|
DefaultScreen(disp),
|
|
|
|
DefaultVisual(disp, DefaultScreen(disp)),
|
|
|
|
DisplayWidth(disp, DefaultScreen(disp)),
|
|
|
|
DisplayHeight(disp, DefaultScreen(disp)));
|
|
|
|
|
|
|
|
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
|
|
|
|
draw_font_free(font_t *font)
|
|
|
|
{
|
|
|
|
pango_font_description_free(font->desc);
|
|
|
|
p_delete(&font);
|
|
|
|
}
|
|
|
|
|
2007-12-30 15:11:37 +01:00
|
|
|
/** Draw text into a draw context
|
2008-02-27 09:32:45 +01:00
|
|
|
* \param ctx DrawCtx to draw to
|
|
|
|
* \param area area to draw to
|
2008-01-03 15:57:07 +01:00
|
|
|
* \param align alignment
|
2007-12-30 15:11:37 +01:00
|
|
|
* \param padding padding to add before drawing the text
|
|
|
|
* \param font font to use
|
|
|
|
* \param text text to draw
|
2008-03-07 17:40:40 +01:00
|
|
|
* \param enable shadow
|
2007-12-30 15:11:37 +01:00
|
|
|
* \param fg foreground color
|
|
|
|
* \param bg background color
|
|
|
|
*/
|
2007-09-15 15:16:53 +02:00
|
|
|
void
|
2008-01-03 15:57:07 +01:00
|
|
|
draw_text(DrawCtx *ctx,
|
2008-03-14 09:37:25 +01:00
|
|
|
area_t area,
|
2008-01-23 16:52:15 +01:00
|
|
|
Alignment align,
|
2008-01-03 15:57:07 +01:00
|
|
|
int padding,
|
2008-03-14 08:35:06 +01:00
|
|
|
char *text,
|
|
|
|
style_t style)
|
2007-09-05 20:15:00 +02:00
|
|
|
{
|
2008-03-07 17:40:40 +01:00
|
|
|
int nw = 0, 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;
|
2007-09-05 20:15:00 +02:00
|
|
|
|
2008-03-14 08:35:06 +01:00
|
|
|
draw_rectangle(ctx, area, True, style.bg);
|
2007-12-30 14:42:51 +01:00
|
|
|
|
2008-02-09 16:23:16 +01:00
|
|
|
if(!(len = olen = a_strlen(text)))
|
2007-09-05 20:15:00 +02:00
|
|
|
return;
|
2007-10-16 01:20:03 +02:00
|
|
|
|
2008-02-09 16:23:16 +01:00
|
|
|
/* try to convert it to UTF-8 */
|
2008-02-11 10:40:38 +01:00
|
|
|
if((utf8 = draw_iso2utf8(text)))
|
2008-02-11 09:21:09 +01:00
|
|
|
{
|
|
|
|
buf = utf8;
|
|
|
|
len = olen = a_strlen(buf);
|
|
|
|
}
|
2008-02-11 10:40:38 +01:00
|
|
|
else
|
|
|
|
buf = a_strdup(text);
|
2008-02-04 13:28:20 +01:00
|
|
|
|
2008-02-09 16:23:16 +01:00
|
|
|
/* check that the text is not too long */
|
2008-03-14 08:35:06 +01:00
|
|
|
while(len && (nw = (draw_textwidth(ctx->display, style.font, buf)) + padding * 2) > area.width)
|
2008-02-09 16:23:16 +01:00
|
|
|
{
|
|
|
|
len--;
|
|
|
|
/* we can't blindly null the char, we need to check if it's not part of
|
|
|
|
* a multi byte char: if mbtowc return -1, we know that we must go back
|
|
|
|
* in the string to find the beginning of the multi byte char */
|
|
|
|
while(mbtowc(NULL, buf + len, a_strlen(buf + len)) < 0)
|
|
|
|
len--;
|
|
|
|
buf[len] = '\0';
|
|
|
|
}
|
2008-01-12 23:47:03 +01:00
|
|
|
if(nw > area.width)
|
2007-09-11 17:46:25 +02:00
|
|
|
return; /* too long */
|
2007-09-05 20:15:00 +02:00
|
|
|
if(len < olen)
|
|
|
|
{
|
|
|
|
if(len > 1)
|
|
|
|
buf[len - 1] = '.';
|
|
|
|
if(len > 2)
|
|
|
|
buf[len - 2] = '.';
|
|
|
|
if(len > 3)
|
|
|
|
buf[len - 3] = '.';
|
|
|
|
}
|
2007-10-16 01:20:03 +02:00
|
|
|
|
2008-03-18 09:01:16 +01:00
|
|
|
pango_layout_set_text(ctx->layout, buf, -1);
|
2008-03-17 12:38:49 +01:00
|
|
|
pango_layout_set_font_description(ctx->layout, style.font->desc);
|
2008-03-07 17:40:40 +01:00
|
|
|
|
|
|
|
x = area.x + padding;
|
2008-03-17 12:38:49 +01:00
|
|
|
y = area.y + (ctx->height - style.font->height) / 2;
|
2008-02-09 16:23:16 +01:00
|
|
|
|
2008-01-04 19:17:20 +01:00
|
|
|
switch(align)
|
|
|
|
{
|
2008-03-07 17:40:40 +01:00
|
|
|
case AlignCenter:
|
|
|
|
x += (area.width - nw) / 2;
|
2008-01-04 19:17:20 +01:00
|
|
|
break;
|
|
|
|
case AlignRight:
|
2008-03-07 17:40:40 +01:00
|
|
|
x += area.width - nw;
|
2008-01-04 19:17:20 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-01-31 18:18:15 +01:00
|
|
|
|
2008-03-14 08:35:06 +01:00
|
|
|
if(style.shadow_offset > 0)
|
2008-03-07 17:40:40 +01:00
|
|
|
{
|
2008-03-13 14:03:03 +01:00
|
|
|
cairo_set_source_rgb(ctx->cr,
|
2008-03-14 08:35:06 +01:00
|
|
|
style.shadow.red / 65535.0,
|
|
|
|
style.shadow.green / 65535.0,
|
|
|
|
style.shadow.blue / 65535.0);
|
|
|
|
cairo_move_to(ctx->cr, x + style.shadow_offset, y + style.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-03-13 14:03:03 +01:00
|
|
|
cairo_set_source_rgb(ctx->cr,
|
2008-03-14 08:35:06 +01:00
|
|
|
style.fg.red / 65535.0,
|
|
|
|
style.fg.green / 65535.0,
|
|
|
|
style.fg.blue / 65535.0);
|
2008-03-07 17:40:40 +01:00
|
|
|
cairo_move_to(ctx->cr, x, y);
|
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-02-05 15:33:42 +01:00
|
|
|
/** Setup color-source for cairo (gradient or mono)
|
|
|
|
* \param ctx Draw context
|
2008-03-02 21:53:18 +01:00
|
|
|
* \param rect x,y to x+x_offset,y+y_offset
|
|
|
|
* \param color color to use at start (x,y)
|
2008-02-06 01:07:27 +01:00
|
|
|
* \param pcolor_center color at 50% of width
|
2008-03-02 21:53:18 +01:00
|
|
|
* \param pcolor_end color at pattern end (x + x_offset, y + y_offset)
|
2008-02-05 15:33:42 +01:00
|
|
|
* \return pat pattern or NULL; needs to get cairo_pattern_destroy()'ed;
|
|
|
|
*/
|
2008-02-27 09:32:45 +01:00
|
|
|
static cairo_pattern_t *
|
2008-03-14 09:37:25 +01:00
|
|
|
draw_setup_cairo_color_source(DrawCtx *ctx, area_t rect,
|
2008-03-02 13:13:12 +01:00
|
|
|
XColor *pcolor, XColor *pcolor_center, XColor *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)
|
|
|
|
cairo_set_source_rgb(ctx->cr, pcolor->red / 65535.0, pcolor->green / 65535.0, pcolor->blue / 65535.0);
|
|
|
|
else
|
2008-02-05 15:33:42 +01:00
|
|
|
{
|
2008-03-02 21:53:18 +01: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) */
|
|
|
|
cairo_pattern_add_color_stop_rgb(pat, 0, pcolor->red / 65535.0,
|
2008-03-02 13:13:12 +01:00
|
|
|
pcolor->green / 65535.0, pcolor->blue / 65535.0);
|
2008-03-02 21:53:18 +01:00
|
|
|
|
|
|
|
if(pcolor_center)
|
|
|
|
cairo_pattern_add_color_stop_rgb(pat, 0.5, pcolor_center->red / 65535.0,
|
|
|
|
pcolor_center->green / 65535.0, pcolor_center->blue / 65535.0);
|
|
|
|
|
2008-02-05 15:33:42 +01:00
|
|
|
if(pcolor_end)
|
|
|
|
cairo_pattern_add_color_stop_rgb(pat, 1, pcolor_end->red / 65535.0,
|
|
|
|
pcolor_end->green / 65535.0, pcolor_end->blue / 65535.0);
|
|
|
|
else
|
2008-03-02 13:13:12 +01:00
|
|
|
cairo_pattern_add_color_stop_rgb(pat, 1, pcolor->red / 65535.0,
|
|
|
|
pcolor->green / 65535.0, pcolor->blue / 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-01-12 23:38:31 +01:00
|
|
|
/** Draw rectangle
|
|
|
|
* \param ctx Draw context
|
|
|
|
* \param geometry geometry
|
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-03-14 09:37:25 +01:00
|
|
|
draw_rectangle(DrawCtx *ctx, area_t geometry, Bool filled, XColor 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);
|
|
|
|
cairo_set_line_width(ctx->cr, 1.0);
|
|
|
|
cairo_set_source_rgb(ctx->cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
|
2007-09-05 20:15:00 +02:00
|
|
|
if(filled)
|
|
|
|
{
|
2008-01-31 18:18:15 +01:00
|
|
|
cairo_rectangle(ctx->cr, geometry.x, geometry.y, geometry.width, geometry.height);
|
|
|
|
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-01-31 18:18:15 +01:00
|
|
|
cairo_rectangle(ctx->cr, geometry.x + 1, geometry.y, geometry.width - 1, geometry.height - 1);
|
2008-02-04 11:16:30 +01:00
|
|
|
cairo_stroke(ctx->cr);
|
|
|
|
}
|
|
|
|
}
|
2008-02-05 01:27:39 +01:00
|
|
|
/** Draw rectangle with gradient colors
|
|
|
|
* \param ctx Draw context
|
|
|
|
* \param geometry geometry
|
|
|
|
* \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-03-14 09:37:25 +01:00
|
|
|
draw_rectangle_gradient(DrawCtx *ctx, area_t geometry, Bool filled,
|
|
|
|
area_t pattern_rect, XColor *pcolor, XColor *pcolor_center, XColor *pcolor_end)
|
2008-02-04 11:16:30 +01:00
|
|
|
{
|
|
|
|
cairo_pattern_t *pat;
|
|
|
|
|
|
|
|
cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
|
|
|
|
cairo_set_line_width(ctx->cr, 1.0);
|
|
|
|
|
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-01-31 19:40:47 +01:00
|
|
|
draw_graph_setup(DrawCtx *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-01-26 15:15:31 +01:00
|
|
|
/* without it, it can draw over the path on sharp angles (...too long lines) */
|
2008-01-31 19:40:47 +01:00
|
|
|
cairo_set_line_join (ctx->cr, CAIRO_LINE_JOIN_ROUND);
|
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 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)
|
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-03-18 03:15:50 +01:00
|
|
|
draw_graph(DrawCtx *ctx, area_t rect, int *from, int *to, int cur_index, Position grow,
|
2008-03-14 09:37:25 +01:00
|
|
|
area_t patt_rect, XColor *pcolor, XColor *pcolor_center, XColor *pcolor_end)
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-03-09 23:49:03 +01:00
|
|
|
int i, x, y, w;
|
2008-02-05 15:33:42 +01:00
|
|
|
cairo_pattern_t *pat;
|
2008-03-02 21:53:18 +01:00
|
|
|
|
2008-03-09 23:49:03 +01:00
|
|
|
pat = draw_setup_cairo_color_source(ctx, patt_rect, pcolor, pcolor_center, pcolor_end);
|
|
|
|
|
|
|
|
x = rect.x;
|
|
|
|
y = rect.y;
|
|
|
|
w = rect.width;
|
2008-01-06 18:23:56 +01:00
|
|
|
|
|
|
|
i = -1;
|
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-03-18 03:15:50 +01:00
|
|
|
x += w - 1;
|
|
|
|
while(++i < w)
|
|
|
|
{
|
|
|
|
cairo_move_to(ctx->cr, x, y - from[cur_index]);
|
|
|
|
cairo_line_to(ctx->cr, x, y - to[cur_index]);
|
|
|
|
x--;
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-03-18 03:15:50 +01:00
|
|
|
if (--cur_index < 0)
|
|
|
|
cur_index = w - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* draw from left to right */
|
|
|
|
{
|
|
|
|
while(++i < w)
|
|
|
|
{
|
|
|
|
cairo_move_to(ctx->cr, x, y - from[cur_index]);
|
|
|
|
cairo_line_to(ctx->cr, x, y - to[cur_index]);
|
|
|
|
x++;
|
|
|
|
|
|
|
|
if (--cur_index < 0)
|
|
|
|
cur_index = w - 1;
|
|
|
|
}
|
2008-01-06 18:23:56 +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-03-18 03:15:50 +01:00
|
|
|
draw_graph_line(DrawCtx *ctx, area_t rect, int *to, int cur_index, Position grow,
|
2008-03-14 09:37:25 +01:00
|
|
|
area_t patt_rect, XColor *pcolor, XColor *pcolor_center, XColor *pcolor_end)
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-03-09 23:49:03 +01:00
|
|
|
int i, x, y, w;
|
2008-01-25 21:39:08 +01:00
|
|
|
int flag = 0; /* used to prevent drawing a line from 0 to 0 values */
|
2008-02-05 15:33:42 +01:00
|
|
|
cairo_pattern_t *pat;
|
|
|
|
|
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-03-09 23:49:03 +01:00
|
|
|
x = rect.x;
|
|
|
|
y = rect.y;
|
|
|
|
w = rect.width;
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-03-18 03:15:50 +01:00
|
|
|
if(grow == Right) /* draw from right to left */
|
|
|
|
{
|
|
|
|
x += w - 1;
|
|
|
|
cairo_move_to(ctx->cr, x, y - to[cur_index]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* x-1 (on the border), paints *from* the last point (... not included itself) */
|
|
|
|
/* may makes sense when you assume there is already some line drawn to it - anyway */
|
|
|
|
cairo_move_to(ctx->cr, x - 1, y - to[cur_index]);
|
|
|
|
}
|
2008-01-25 21:39:08 +01:00
|
|
|
|
|
|
|
for (i = 0; i < w; i++)
|
|
|
|
{
|
2008-01-26 15:15:31 +01:00
|
|
|
if (to[cur_index] > 0)
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-01-31 19:40:47 +01:00
|
|
|
cairo_line_to(ctx->cr, x, y - to[cur_index]);
|
2008-01-25 21:39:08 +01:00
|
|
|
flag = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(flag) /* only draw from values > 0 to 0-values */
|
|
|
|
{
|
2008-01-31 19:40:47 +01:00
|
|
|
cairo_line_to(ctx->cr, x, y);
|
2008-01-25 21:39:08 +01:00
|
|
|
flag = 0;
|
|
|
|
}
|
|
|
|
else
|
2008-01-31 19:40:47 +01:00
|
|
|
cairo_move_to(ctx->cr, x, y);
|
2008-01-25 21:39:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (--cur_index < 0) /* cycles around the index */
|
|
|
|
cur_index = w - 1;
|
2008-03-18 03:15:50 +01:00
|
|
|
|
|
|
|
if(grow == Right)
|
|
|
|
x--;
|
|
|
|
else
|
|
|
|
x++;
|
2008-01-25 21:39:08 +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-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
|
2007-12-22 20:17:24 +01:00
|
|
|
draw_circle(DrawCtx *ctx, int x, int y, int r, Bool filled, XColor 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);
|
|
|
|
cairo_set_source_rgb(ctx->cr, color.red / 65535.0, color.green / 65535.0, color.blue / 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
|
|
|
|
*/
|
2007-12-22 20:14:13 +01:00
|
|
|
void draw_image_from_argb_data(DrawCtx *ctx, int x, int y, int w, int h,
|
2007-12-27 10:33:20 +01:00
|
|
|
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
|
|
|
|
2007-12-22 20:14:13 +01:00
|
|
|
source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h, 0);
|
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-02-27 09:32:45 +01: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
|
|
|
|
*/
|
2008-03-19 17:51:12 +01:00
|
|
|
void
|
|
|
|
draw_image(DrawCtx *ctx, int x, int y, int wanted_h, const char *filename)
|
2007-12-22 15:37:43 +01:00
|
|
|
{
|
2007-12-27 00:13:44 +01:00
|
|
|
double ratio;
|
|
|
|
int h;
|
2008-01-31 18:18:15 +01:00
|
|
|
cairo_surface_t *source;
|
2007-12-22 15:37:43 +01:00
|
|
|
cairo_t *cr;
|
2008-01-23 13:47:56 +01:00
|
|
|
cairo_status_t cairo_st;
|
2007-12-22 15:37:43 +01:00
|
|
|
|
2008-01-31 18:18:15 +01:00
|
|
|
source = cairo_image_surface_create_from_png(filename);
|
|
|
|
if((cairo_st = cairo_surface_status(source)))
|
2008-01-23 13:47:56 +01:00
|
|
|
{
|
|
|
|
warn("failed to draw image %s: %s\n", filename, cairo_status_to_string(cairo_st));
|
2008-02-27 09:32:45 +01:00
|
|
|
cairo_surface_destroy(source);
|
2008-01-23 10:48:08 +01:00
|
|
|
return;
|
2008-01-23 13:47:56 +01:00
|
|
|
}
|
2008-01-31 18:18:15 +01:00
|
|
|
cr = cairo_create (ctx->surface);
|
|
|
|
if(wanted_h > 0 && (h = cairo_image_surface_get_height(source)) > 0)
|
2007-12-27 00:13:44 +01:00
|
|
|
{
|
|
|
|
ratio = (double) wanted_h / (double) h;
|
|
|
|
cairo_scale(cr, ratio, ratio);
|
2008-01-31 18:18:15 +01:00
|
|
|
cairo_set_source_surface(cr, source, x / ratio, y / ratio);
|
2007-12-27 00:13:44 +01:00
|
|
|
}
|
|
|
|
else
|
2008-01-31 18:18:15 +01:00
|
|
|
cairo_set_source_surface(cr, source, x, y);
|
2007-12-22 15:37:43 +01:00
|
|
|
cairo_paint(cr);
|
|
|
|
|
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(source);
|
|
|
|
}
|
|
|
|
|
2008-02-27 09:32:45 +01:00
|
|
|
/** Get an imag size (PNG format only)
|
|
|
|
* \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 };
|
2007-12-22 15:37:43 +01:00
|
|
|
cairo_surface_t *surface;
|
2008-01-23 13:47:56 +01:00
|
|
|
cairo_status_t cairo_st;
|
2007-12-22 15:37:43 +01:00
|
|
|
|
|
|
|
surface = cairo_image_surface_create_from_png(filename);
|
2008-01-23 13:47:56 +01:00
|
|
|
if((cairo_st = cairo_surface_status(surface)))
|
|
|
|
warn("failed to get image size %s: %s\n", filename, cairo_status_to_string(cairo_st));
|
|
|
|
else
|
2008-01-23 10:48:08 +01:00
|
|
|
{
|
|
|
|
cairo_image_surface_get_width(surface);
|
|
|
|
size.x = 0;
|
|
|
|
size.y = 0;
|
|
|
|
size.width = cairo_image_surface_get_width(surface);
|
|
|
|
size.height = cairo_image_surface_get_height(surface);
|
|
|
|
}
|
2007-12-22 15:37:43 +01:00
|
|
|
|
2008-02-27 09:32:45 +01:00
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
|
2007-12-29 21:44:44 +01:00
|
|
|
return size;
|
2007-12-29 19:44:15 +01:00
|
|
|
}
|
|
|
|
|
2008-02-27 09:32:45 +01:00
|
|
|
/** Rotate a drawable
|
|
|
|
* \param ctx Draw context to draw to
|
2008-03-20 09:17:34 +01:00
|
|
|
* \param dest Drawable to draw the result
|
|
|
|
* \param dest_w Drawable width
|
|
|
|
* \param dest_h Drawable height
|
2008-02-27 09:32:45 +01:00
|
|
|
* \param angle angle to rotate
|
|
|
|
* \param tx translate to this x coordinate
|
|
|
|
* \param ty translate to this y coordinate
|
|
|
|
* \return new rotated drawable
|
|
|
|
*/
|
2008-03-20 09:17:34 +01:00
|
|
|
void
|
|
|
|
draw_rotate(DrawCtx *ctx, Drawable dest, int dest_w, int dest_h,
|
|
|
|
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-20 09:17:34 +01:00
|
|
|
surface = cairo_xlib_surface_create(ctx->display, dest, ctx->visual, dest_w, dest_h);
|
2008-01-24 18:43:24 +01:00
|
|
|
source = cairo_xlib_surface_create(ctx->display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
|
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-02-27 09:32:45 +01:00
|
|
|
/** Return the width of a text in pixel
|
|
|
|
* \param disp Display ref
|
|
|
|
* \param font font to use
|
|
|
|
* \param text the text
|
|
|
|
* \return text width
|
|
|
|
*/
|
2007-12-30 14:22:19 +01:00
|
|
|
unsigned short
|
2008-03-17 12:38:49 +01:00
|
|
|
draw_textwidth(Display *disp, font_t *font, 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;
|
2007-10-16 01:20:03 +02:00
|
|
|
|
2007-12-30 14:22:19 +01:00
|
|
|
if (!a_strlen(text))
|
|
|
|
return 0;
|
|
|
|
|
2008-01-24 18:43:24 +01:00
|
|
|
surface = cairo_xlib_surface_create(disp, DefaultScreen(disp),
|
|
|
|
DefaultVisual(disp, DefaultScreen(disp)),
|
|
|
|
DisplayWidth(disp, DefaultScreen(disp)),
|
|
|
|
DisplayHeight(disp, DefaultScreen(disp)));
|
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);
|
|
|
|
pango_layout_set_text(layout, text, -1);
|
|
|
|
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-03-17 12:38:49 +01:00
|
|
|
return ext.width;
|
2007-09-06 22:09:00 +02:00
|
|
|
}
|
2007-10-15 13:40:52 +02:00
|
|
|
|
2008-02-27 09:32:45 +01:00
|
|
|
/** Transform a string to a Alignment type.
|
|
|
|
* Recognized string are left, center or right. Everything else will be
|
|
|
|
* recognized as AlignAuto.
|
|
|
|
* \param align string with align text
|
|
|
|
* \return Alignment type
|
|
|
|
*/
|
2008-01-04 19:17:20 +01:00
|
|
|
Alignment
|
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-02-08 10:59:55 +01:00
|
|
|
if(!a_strncmp(align, "left", 4))
|
|
|
|
return AlignLeft;
|
|
|
|
else if(!a_strncmp(align, "center", 6))
|
2008-01-03 16:02:32 +01:00
|
|
|
return AlignCenter;
|
|
|
|
else if(!a_strncmp(align, "right", 5))
|
|
|
|
return AlignRight;
|
2008-02-28 16:19:26 +01:00
|
|
|
else if(!a_strncmp(align, "flex", 4))
|
|
|
|
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-01-27 18:56:37 +01:00
|
|
|
/** Initialize an X color
|
|
|
|
* \param disp display 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-02-27 09:32:45 +01:00
|
|
|
* \param color XColor struct to store color to
|
|
|
|
* \return true if color allocation was successfull
|
2008-01-27 18:56:37 +01:00
|
|
|
*/
|
2008-02-13 08:58:21 +01:00
|
|
|
Bool
|
|
|
|
draw_color_new(Display *disp, int phys_screen, const char *colstr, XColor *color)
|
2008-01-27 18:56:37 +01:00
|
|
|
{
|
2008-02-13 08:58:21 +01:00
|
|
|
Bool ret;
|
|
|
|
XColor exactColor;
|
2008-01-27 18:56:37 +01:00
|
|
|
|
2008-03-13 17:57:38 +01:00
|
|
|
if(!a_strlen(colstr))
|
|
|
|
return False;
|
|
|
|
|
2008-02-13 08:58:21 +01:00
|
|
|
if(!(ret = XAllocNamedColor(disp,
|
|
|
|
DefaultColormap(disp, phys_screen),
|
|
|
|
colstr,
|
|
|
|
color,
|
|
|
|
&exactColor)))
|
|
|
|
warn("awesome: error, cannot allocate color '%s'\n", colstr);
|
2008-01-27 18:56:37 +01:00
|
|
|
|
2008-02-13 08:58:21 +01:00
|
|
|
return ret;
|
2008-01-27 18:56:37 +01:00
|
|
|
}
|
|
|
|
|
2008-03-13 17:57:38 +01:00
|
|
|
void
|
2008-03-14 08:35:06 +01:00
|
|
|
draw_style_init(Display *disp, int phys_screen, cfg_t *cfg,
|
|
|
|
style_t *c, style_t *m)
|
2008-03-13 17:57:38 +01:00
|
|
|
{
|
2008-03-14 08:35:06 +01:00
|
|
|
char *buf;
|
|
|
|
|
2008-03-13 17:57:38 +01:00
|
|
|
if(m)
|
|
|
|
*c = *m;
|
|
|
|
|
2008-03-14 08:35:06 +01:00
|
|
|
if(!cfg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if((buf = cfg_getstr(cfg, "font")))
|
2008-03-17 12:38:49 +01:00
|
|
|
c->font = draw_font_new(disp, buf);
|
2008-03-14 08:35:06 +01:00
|
|
|
|
2008-03-13 17:57:38 +01:00
|
|
|
draw_color_new(disp, phys_screen,
|
2008-03-14 08:35:06 +01:00
|
|
|
cfg_getstr(cfg, "fg"), &c->fg);
|
|
|
|
|
2008-03-13 17:57:38 +01:00
|
|
|
draw_color_new(disp, phys_screen,
|
2008-03-21 10:01:28 +01:00
|
|
|
cfg_getstr(cfg, "bg"), &c->bg);
|
2008-03-14 08:35:06 +01:00
|
|
|
|
2008-03-13 17:57:38 +01:00
|
|
|
draw_color_new(disp, phys_screen,
|
2008-03-21 10:01:28 +01:00
|
|
|
cfg_getstr(cfg, "border"), &c->border);
|
2008-03-14 08:35:06 +01:00
|
|
|
|
2008-03-13 17:57:38 +01:00
|
|
|
draw_color_new(disp, phys_screen,
|
2008-03-21 10:01:28 +01:00
|
|
|
cfg_getstr(cfg, "shadow"), &c->shadow);
|
2008-03-13 17:57:38 +01:00
|
|
|
|
2008-03-14 08:35:06 +01:00
|
|
|
c->shadow_offset = cfg_getint(cfg, "shadow_offset");
|
2008-03-13 17:57:38 +01:00
|
|
|
}
|
|
|
|
|
2008-01-28 18:30:23 +01:00
|
|
|
/** Remove a area from a list of them,
|
2008-02-27 09:32:45 +01:00
|
|
|
* spliting the space between several area that can overlaps
|
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
|