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>
|
2007-10-16 01:20:03 +02:00
|
|
|
#include <cairo-ft.h>
|
2007-10-10 19:59:14 +02:00
|
|
|
#include <cairo-xlib.h>
|
2007-10-11 17:06:55 +02:00
|
|
|
#include <math.h>
|
2007-09-15 15:16:53 +02:00
|
|
|
#include "util.h"
|
2008-01-01 18:02:36 +01:00
|
|
|
#include "config.h"
|
2007-09-05 20:15:00 +02:00
|
|
|
|
2007-12-27 15:49:00 +01:00
|
|
|
extern AwesomeConf globalconf;
|
|
|
|
|
2007-12-30 15:11:37 +01:00
|
|
|
/** Get a draw context
|
|
|
|
* \param phys_screen physical screen id
|
|
|
|
* \param width width
|
|
|
|
* \param height height
|
|
|
|
* \return draw context ref
|
|
|
|
*/
|
2007-12-15 00:12:17 +01:00
|
|
|
DrawCtx *
|
2007-12-30 12:33:57 +01:00
|
|
|
draw_get_context(int phys_screen, int width, int height)
|
2007-12-15 00:12:17 +01:00
|
|
|
{
|
2007-12-30 12:26:11 +01:00
|
|
|
DrawCtx *d = p_new(DrawCtx, 1);
|
|
|
|
|
2007-12-15 00:12:17 +01:00
|
|
|
d->phys_screen = phys_screen;
|
|
|
|
d->width = width;
|
|
|
|
d->height = height;
|
2007-12-30 12:26:11 +01:00
|
|
|
d->depth = DefaultDepth(globalconf.display, phys_screen);
|
|
|
|
d->visual = DefaultVisual(globalconf.display, phys_screen);
|
2007-12-30 12:33:57 +01:00
|
|
|
d->drawable = XCreatePixmap(globalconf.display,
|
|
|
|
RootWindow(globalconf.display, phys_screen),
|
|
|
|
width, height, d->depth);
|
2007-12-30 12:26:11 +01:00
|
|
|
|
2007-12-15 00:12:17 +01:00
|
|
|
return d;
|
|
|
|
};
|
|
|
|
|
2007-12-30 15:11:37 +01:00
|
|
|
/** Free a draw context and its drawable
|
|
|
|
* \param ctx the draw context to free
|
|
|
|
*/
|
2007-12-30 12:33:57 +01:00
|
|
|
void
|
|
|
|
draw_free_context(DrawCtx *ctx)
|
|
|
|
{
|
|
|
|
XFreePixmap(globalconf.display, ctx->drawable);
|
|
|
|
p_delete(&ctx);
|
|
|
|
}
|
|
|
|
|
2007-12-30 15:11:37 +01:00
|
|
|
/** Draw text into a draw context
|
|
|
|
* \param x x coord
|
|
|
|
* \param y y coord
|
|
|
|
* \param w width
|
|
|
|
* \param h height
|
|
|
|
* \param padding padding to add before drawing the text
|
|
|
|
* \param font font to use
|
|
|
|
* \param text text to draw
|
|
|
|
* \param fg foreground color
|
|
|
|
* \param bg background color
|
|
|
|
*/
|
2007-09-15 15:16:53 +02:00
|
|
|
void
|
2007-12-30 14:42:51 +01:00
|
|
|
draw_text(DrawCtx *ctx, int x, int y, int w, int h, int padding, XftFont *font, const char *text, XColor fg, XColor bg)
|
2007-09-05 20:15:00 +02:00
|
|
|
{
|
2007-10-09 21:29:19 +02:00
|
|
|
int nw = 0;
|
2007-09-05 20:15:00 +02:00
|
|
|
static char buf[256];
|
2007-09-20 20:11:33 +02:00
|
|
|
size_t len, olen;
|
2007-10-16 01:20:03 +02:00
|
|
|
cairo_font_face_t *font_face;
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
2007-09-05 20:15:00 +02:00
|
|
|
|
2007-12-22 20:17:24 +01:00
|
|
|
draw_rectangle(ctx, x, y, w, h, True, bg);
|
2007-12-30 14:42:51 +01:00
|
|
|
|
|
|
|
olen = len = a_strlen(text);
|
|
|
|
|
|
|
|
if(!len)
|
2007-09-05 20:15:00 +02:00
|
|
|
return;
|
2007-10-16 01:20:03 +02:00
|
|
|
|
2007-12-30 12:26:11 +01:00
|
|
|
surface = cairo_xlib_surface_create(globalconf.display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
|
2007-10-16 01:20:03 +02:00
|
|
|
cr = cairo_create(surface);
|
2007-10-16 18:54:58 +02:00
|
|
|
font_face = cairo_ft_font_face_create_for_pattern(font->pattern);
|
2007-10-16 01:20:03 +02:00
|
|
|
cairo_set_font_face(cr, font_face);
|
|
|
|
cairo_set_font_size(cr, font->height);
|
2007-12-18 04:19:41 +01:00
|
|
|
cairo_set_source_rgb(cr, fg.red / 65535.0, fg.green / 65535.0, fg.blue / 65535.0);
|
2007-10-16 01:20:03 +02:00
|
|
|
|
2007-09-20 20:11:33 +02:00
|
|
|
if(len >= sizeof(buf))
|
|
|
|
len = sizeof(buf) - 1;
|
2007-09-05 20:15:00 +02:00
|
|
|
memcpy(buf, text, len);
|
|
|
|
buf[len] = 0;
|
2007-12-30 14:42:51 +01:00
|
|
|
while(len && (nw = (textwidth(font, buf)) + padding * 2) > w)
|
2007-09-05 20:15:00 +02:00
|
|
|
buf[--len] = 0;
|
2007-10-09 21:29:19 +02:00
|
|
|
if(nw > w)
|
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
|
|
|
|
2007-12-30 14:42:51 +01:00
|
|
|
cairo_move_to(cr, x + padding, y + font->ascent + (ctx->height - font->height) / 2);
|
2007-10-16 01:20:03 +02:00
|
|
|
cairo_show_text(cr, buf);
|
|
|
|
|
|
|
|
cairo_font_face_destroy(font_face);
|
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(surface);
|
2007-09-05 20:15:00 +02:00
|
|
|
}
|
|
|
|
|
2007-09-15 15:16:53 +02:00
|
|
|
void
|
2007-12-22 20:17:24 +01:00
|
|
|
draw_rectangle(DrawCtx *ctx, int x, int y, int w, int h, Bool filled, XColor color)
|
2007-09-05 20:15:00 +02:00
|
|
|
{
|
2007-10-10 19:59:14 +02:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
2007-09-05 20:15:00 +02:00
|
|
|
|
2007-12-30 12:26:11 +01:00
|
|
|
surface = cairo_xlib_surface_create(globalconf.display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
|
2007-10-10 19:59:14 +02:00
|
|
|
cr = cairo_create (surface);
|
|
|
|
|
|
|
|
cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
|
|
|
|
cairo_set_line_width(cr, 1.0);
|
2007-10-11 16:29:20 +02:00
|
|
|
cairo_set_source_rgb(cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
|
2007-09-05 20:15:00 +02:00
|
|
|
if(filled)
|
|
|
|
{
|
2007-12-30 14:06:45 +01:00
|
|
|
cairo_rectangle(cr, x, y, w, h);
|
2007-10-10 19:59:14 +02:00
|
|
|
cairo_fill(cr);
|
2007-09-05 20:15:00 +02:00
|
|
|
}
|
2007-09-20 20:11:33 +02:00
|
|
|
else
|
2007-12-30 14:06:45 +01:00
|
|
|
cairo_rectangle(cr, x + 1, y, w - 1, h - 1);
|
2007-10-11 17:06:55 +02:00
|
|
|
|
2007-10-10 19:59:14 +02:00
|
|
|
cairo_stroke(cr);
|
|
|
|
|
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(surface);
|
2007-09-05 20:15:00 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
|
|
|
|
2007-12-30 12:26:11 +01:00
|
|
|
surface = cairo_xlib_surface_create(globalconf.display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
|
2007-10-11 17:06:55 +02:00
|
|
|
cr = cairo_create (surface);
|
|
|
|
cairo_set_line_width(cr, 1.0);
|
|
|
|
cairo_set_source_rgb(cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
|
|
|
|
if(filled)
|
|
|
|
{
|
|
|
|
cairo_arc (cr, x + r, y + r, r, 0, 2 * M_PI);
|
|
|
|
cairo_fill(cr);
|
|
|
|
}
|
|
|
|
else
|
2007-10-11 17:09:38 +02:00
|
|
|
cairo_arc (cr, x + r, y + r, r - 1, 0, 2 * M_PI);
|
2007-10-11 17:06:55 +02:00
|
|
|
|
|
|
|
cairo_stroke(cr);
|
|
|
|
|
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
}
|
2007-09-06 22:09:00 +02:00
|
|
|
|
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_surface_t *surface, *source;
|
|
|
|
cairo_t *cr;
|
|
|
|
|
2007-12-30 12:26:11 +01:00
|
|
|
surface = cairo_xlib_surface_create(globalconf.display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
|
2007-12-22 20:14:13 +01:00
|
|
|
source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h, 0);
|
|
|
|
cr = cairo_create (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);
|
2007-12-22 19:32:47 +01:00
|
|
|
cairo_paint(cr);
|
|
|
|
|
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(source);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
}
|
|
|
|
|
2007-12-22 15:37:43 +01:00
|
|
|
void
|
2007-12-27 00:13:44 +01:00
|
|
|
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;
|
2007-12-22 15:37:43 +01:00
|
|
|
cairo_surface_t *surface, *source;
|
|
|
|
cairo_t *cr;
|
|
|
|
|
2007-12-30 12:26:11 +01:00
|
|
|
source = cairo_xlib_surface_create(globalconf.display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
|
2007-12-22 15:37:43 +01:00
|
|
|
surface = cairo_image_surface_create_from_png(filename);
|
|
|
|
cr = cairo_create (source);
|
2007-12-27 00:13:44 +01:00
|
|
|
if(wanted_h > 0 && (h = cairo_image_surface_get_height(surface)) > 0)
|
|
|
|
{
|
|
|
|
ratio = (double) wanted_h / (double) h;
|
|
|
|
cairo_scale(cr, ratio, ratio);
|
|
|
|
cairo_set_source_surface(cr, surface, x / ratio, y / ratio);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cairo_set_source_surface(cr, surface, x, y);
|
2007-12-22 15:37:43 +01:00
|
|
|
cairo_paint(cr);
|
|
|
|
|
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(source);
|
|
|
|
cairo_surface_destroy(surface);
|
2007-12-27 00:13:44 +01:00
|
|
|
|
|
|
|
|
2007-12-22 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2007-12-29 21:44:44 +01:00
|
|
|
Area
|
|
|
|
draw_get_image_size(const char *filename)
|
2007-12-22 15:37:43 +01:00
|
|
|
{
|
2007-12-29 21:44:44 +01:00
|
|
|
Area size;
|
2007-12-22 15:37:43 +01:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
|
|
|
|
surface = cairo_image_surface_create_from_png(filename);
|
|
|
|
cairo_image_surface_get_width(surface);
|
2007-12-29 21:44:44 +01:00
|
|
|
size.width = cairo_image_surface_get_width(surface);
|
|
|
|
size.height = cairo_image_surface_get_height(surface);
|
2007-12-22 15:37:43 +01:00
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
|
2007-12-29 21:44:44 +01:00
|
|
|
return size;
|
2007-12-29 19:44:15 +01:00
|
|
|
}
|
|
|
|
|
2007-11-11 22:27:00 +01:00
|
|
|
Drawable
|
2007-12-15 00:12:17 +01:00
|
|
|
draw_rotate(DrawCtx *ctx, int screen, 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
|
|
|
Drawable newdrawable;
|
|
|
|
|
2007-12-30 12:26:11 +01:00
|
|
|
newdrawable = XCreatePixmap(globalconf.display,
|
|
|
|
RootWindow(globalconf.display, screen),
|
2007-12-15 00:12:17 +01:00
|
|
|
ctx->height, ctx->width,
|
|
|
|
ctx->depth);
|
2007-12-30 12:26:11 +01:00
|
|
|
surface = cairo_xlib_surface_create(globalconf.display, newdrawable, ctx->visual, ctx->height, ctx->width);
|
|
|
|
source = cairo_xlib_surface_create(globalconf.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 22:27:00 +01:00
|
|
|
|
|
|
|
return newdrawable;
|
2007-11-11 18:59:11 +01:00
|
|
|
}
|
|
|
|
|
2007-12-30 14:22:19 +01:00
|
|
|
unsigned short
|
|
|
|
textwidth(XftFont *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;
|
|
|
|
cairo_font_face_t *font_face;
|
|
|
|
cairo_text_extents_t te;
|
|
|
|
|
2007-12-30 14:22:19 +01:00
|
|
|
if (!a_strlen(text))
|
|
|
|
return 0;
|
|
|
|
|
2007-12-27 15:49:00 +01:00
|
|
|
surface = cairo_xlib_surface_create(globalconf.display, DefaultScreen(globalconf.display),
|
|
|
|
DefaultVisual(globalconf.display, DefaultScreen(globalconf.display)),
|
|
|
|
DisplayWidth(globalconf.display, DefaultScreen(globalconf.display)),
|
|
|
|
DisplayHeight(globalconf.display, DefaultScreen(globalconf.display)));
|
2007-10-16 01:20:03 +02:00
|
|
|
cr = cairo_create(surface);
|
|
|
|
font_face = cairo_ft_font_face_create_for_pattern(font->pattern);
|
|
|
|
cairo_set_font_face(cr, font_face);
|
|
|
|
cairo_set_font_size(cr, font->height);
|
|
|
|
cairo_text_extents(cr, text, &te);
|
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
cairo_font_face_destroy(font_face);
|
|
|
|
|
2007-12-30 14:42:51 +01:00
|
|
|
return MAX(te.x_advance, te.width);
|
2007-09-06 22:09:00 +02:00
|
|
|
}
|
2007-10-15 13:40:52 +02:00
|
|
|
|
2007-12-18 09:24:15 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|