awesome/common/draw.c

495 lines
14 KiB
C
Raw Normal View History

/*
2007-09-12 14:29:51 +02:00
* draw.c - draw functions
*
2008-01-02 16:59:43 +01:00
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
*
* 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
#include <cairo.h>
2007-10-16 01:20:03 +02:00
#include <cairo-ft.h>
#include <cairo-xlib.h>
#include <math.h>
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
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
*/
DrawCtx *
draw_context_new(Display *disp, int phys_screen, int width, int height, Drawable dw)
{
DrawCtx *d = p_new(DrawCtx, 1);
2008-01-24 18:43:24 +01:00
d->display = disp;
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);
d->drawable = dw;
return d;
};
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
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
* \param fg foreground color
* \param bg background color
*/
void
2008-01-03 15:57:07 +01:00
draw_text(DrawCtx *ctx,
2008-01-12 23:47:03 +01:00
Area area,
2008-01-23 16:52:15 +01:00
Alignment align,
2008-01-03 15:57:07 +01:00
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
2008-01-12 23:47:03 +01:00
draw_rectangle(ctx, area, True, bg);
olen = len = a_strlen(text);
if(!len)
2007-09-05 20:15:00 +02:00
return;
2007-10-16 01:20:03 +02:00
2008-01-24 18:43:24 +01:00
surface = cairo_xlib_surface_create(ctx->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;
2008-01-24 18:43:24 +01:00
while(len && (nw = (draw_textwidth(ctx->display, font, buf)) + padding * 2) > area.width)
2007-09-05 20:15:00 +02:00
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-01-04 19:17:20 +01:00
switch(align)
{
case AlignLeft:
2008-01-12 23:47:03 +01:00
cairo_move_to(cr, area.x + padding, area.y + font->ascent + (ctx->height - font->height) / 2);
2008-01-04 19:17:20 +01:00
break;
case AlignRight:
2008-01-12 23:47:03 +01:00
cairo_move_to(cr, area.x + (area.width - nw) + padding,
area.y + font->ascent + (ctx->height - font->height) / 2);
2008-01-04 19:17:20 +01:00
break;
default:
2008-01-12 23:47:03 +01:00
cairo_move_to(cr, area.x + ((area.width - nw) / 2) + padding,
area.y + font->ascent + (ctx->height - font->height) / 2);
2008-01-04 19:17:20 +01:00
break;
}
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
}
2008-01-12 23:38:31 +01:00
/** Draw rectangle
* \param ctx Draw context
* \param geometry geometry
* \param filled filled rectangle?
* \param color color to use
*/
void
2008-01-12 23:38:31 +01:00
draw_rectangle(DrawCtx *ctx, Area geometry, Bool filled, XColor color)
2007-09-05 20:15:00 +02:00
{
cairo_surface_t *surface;
cairo_t *cr;
2007-09-05 20:15:00 +02:00
2008-01-24 18:43:24 +01:00
surface = cairo_xlib_surface_create(ctx->display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
cr = cairo_create (surface);
cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
cairo_set_line_width(cr, 1.0);
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)
{
2008-01-12 23:38:31 +01:00
cairo_rectangle(cr, geometry.x, geometry.y, geometry.width, geometry.height);
cairo_fill(cr);
2007-09-05 20:15:00 +02:00
}
2007-09-20 20:11:33 +02:00
else
2008-01-12 23:38:31 +01:00
cairo_rectangle(cr, geometry.x + 1, geometry.y, geometry.width - 1, geometry.height - 1);
cairo_stroke(cr);
cairo_destroy(cr);
cairo_surface_destroy(surface);
2007-09-05 20:15:00 +02:00
}
/* draw_graph functions */
void
draw_graph_init(DrawCtx *ctx, cairo_surface_t **pp_surface, cairo_t **pp_cr)
{
*pp_surface = cairo_xlib_surface_create(ctx->display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
*pp_cr = cairo_create (*pp_surface);
cairo_set_antialias(*pp_cr, CAIRO_ANTIALIAS_NONE);
cairo_set_line_width(*pp_cr, 1.0);
/* without it, it can draw over the path on sharp angles (...too long lines) */
cairo_set_line_join (*pp_cr, CAIRO_LINE_JOIN_ROUND);
}
void
draw_graph_end(cairo_surface_t *surface, cairo_t *cr)
{
cairo_destroy(cr);
cairo_surface_destroy(surface);
}
/* draws a graph; it takes the line-lengths from *from and *to. It cycles
* backwards through those arrays (what have the length of w), beginning at
* position cur_index, until cur_index is reached again (wrapped around). */
void
draw_graph(cairo_t *cr, int x, int y, int w, int *from, int *to, int cur_index, XColor color)
{
int i;
cairo_set_source_rgb(cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
i = -1;
while(++i < w)
{
cairo_move_to(cr, x, y - from[cur_index]);
cairo_line_to(cr, x, y - to[cur_index]);
x++;
if (--cur_index < 0)
cur_index = w - 1;
}
cairo_stroke(cr);
}
void
draw_graph_line(cairo_t *cr, int x, int y, int w, int *to, int cur_index, XColor color)
{
int i;
int flag = 0; /* used to prevent drawing a line from 0 to 0 values */
cairo_set_source_rgb(cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
/* x-1 (on the border), paints *from* the last point (... not included itself) */
/* makes sense when you assume there is already some line drawn to it. */
cairo_move_to(cr, x - 1, y - to[cur_index]);
for (i = 0; i < w; i++)
{
if (to[cur_index] > 0)
{
cairo_line_to(cr, x, y - to[cur_index]);
flag = 1;
}
else
{
if(flag) /* only draw from values > 0 to 0-values */
{
cairo_line_to(cr, x, y);
flag = 0;
}
else
cairo_move_to(cr, x, y);
}
if (--cur_index < 0) /* cycles around the index */
cur_index = w - 1;
x++;
}
cairo_stroke(cr);
}
void
2007-12-22 20:17:24 +01:00
draw_circle(DrawCtx *ctx, int x, int y, int r, Bool filled, XColor color)
{
cairo_surface_t *surface;
cairo_t *cr;
2008-01-24 18:43:24 +01:00
surface = cairo_xlib_surface_create(ctx->display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
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);
cairo_stroke(cr);
cairo_destroy(cr);
cairo_surface_destroy(surface);
}
void draw_image_from_argb_data(DrawCtx *ctx, int x, int y, int w, int h,
int wanted_h, unsigned char *data)
2007-12-22 19:32:47 +01:00
{
double ratio;
2007-12-22 19:32:47 +01:00
cairo_surface_t *surface, *source;
cairo_t *cr;
2008-01-24 18:43:24 +01:00
surface = cairo_xlib_surface_create(ctx->display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h, 0);
cr = cairo_create (surface);
if(wanted_h > 0 && h > 0)
{
ratio = (double) wanted_h / (double) h;
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);
}
void
draw_image(DrawCtx *ctx, int x, int y, int wanted_h, const char *filename)
{
double ratio;
int h;
cairo_surface_t *surface, *source;
cairo_t *cr;
2008-01-23 13:47:56 +01:00
cairo_status_t cairo_st;
2008-01-24 18:43:24 +01:00
source = cairo_xlib_surface_create(ctx->display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
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 draw image %s: %s\n", filename, cairo_status_to_string(cairo_st));
2008-01-23 10:48:08 +01:00
return;
2008-01-23 13:47:56 +01:00
}
cr = cairo_create (source);
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);
cairo_paint(cr);
cairo_destroy(cr);
cairo_surface_destroy(source);
cairo_surface_destroy(surface);
}
Area
draw_get_image_size(const char *filename)
{
Area size = { -1, -1, -1, -1, NULL };
cairo_surface_t *surface;
2008-01-23 13:47:56 +01:00
cairo_status_t cairo_st;
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);
cairo_surface_destroy(surface);
}
return size;
}
Drawable
draw_rotate(DrawCtx *ctx, int screen, double angle, int tx, int ty)
{
cairo_surface_t *surface, *source;
cairo_t *cr;
Drawable newdrawable;
2008-01-24 18:43:24 +01:00
newdrawable = XCreatePixmap(ctx->display,
RootWindow(ctx->display, screen),
ctx->height, ctx->width,
ctx->depth);
2008-01-24 18:43:24 +01:00
surface = cairo_xlib_surface_create(ctx->display, newdrawable, ctx->visual, ctx->height, ctx->width);
source = cairo_xlib_surface_create(ctx->display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
cr = cairo_create (surface);
2007-11-11 19:49:42 +01:00
cairo_translate(cr, tx, ty);
cairo_rotate(cr, angle);
cairo_set_source_surface(cr, source, 0.0, 0.0);
2007-11-11 19:49:42 +01:00
cairo_paint(cr);
cairo_destroy(cr);
cairo_surface_destroy(source);
2007-11-11 22:38:29 +01:00
cairo_surface_destroy(surface);
return newdrawable;
}
2007-12-30 14:22:19 +01:00
unsigned short
2008-01-24 18:43:24 +01:00
draw_textwidth(Display *disp, 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;
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);
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);
return MAX(te.x_advance, te.width);
}
2007-10-15 13:40:52 +02:00
2008-01-04 19:17:20 +01:00
Alignment
draw_get_align(const char *align)
{
if(!a_strncmp(align, "center", 6))
return AlignCenter;
else if(!a_strncmp(align, "right", 5))
return AlignRight;
return AlignLeft;
}
/** Initialize an X color
* \param disp display ref
* \param screen Physical screen number
* \param colstr Color specification
* \return XColor struct
*/
XColor
draw_color_new(Display *disp, int phys_screen, const char *colstr)
{
XColor screenColor, exactColor;
if(!XAllocNamedColor(disp,
DefaultColormap(disp, phys_screen),
colstr,
&screenColor,
&exactColor))
eprint("awesome: error, cannot allocate color '%s'\n", colstr);
return screenColor;
}
/** Remove a area from a list of them,
* spliting the space between several area that
* can overlaps
* \param head list head
* \param elem area to remove
*/
void
area_list_remove(Area **head, Area *elem)
{
Area *r, inter, *extra;
area_list_detach(head, elem);
for(r = *head; r; r = r->next)
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))
{
extra = p_new(Area, 1);
extra->x = r->x;
extra->y = r->y;
extra->width = AREA_LEFT(inter) - r->x;
extra->height = r->height;
area_list_push(head, extra);
}
if(AREA_TOP(inter) > AREA_TOP(*r))
{
extra = p_new(Area, 1);
extra->x = r->x;
extra->y = r->y;
extra->width = r->width;
extra->height = AREA_TOP(inter) - r->y;
area_list_push(head, extra);
}
if(AREA_RIGHT(inter) < AREA_RIGHT(*r))
{
extra = p_new(Area, 1);
extra->x = AREA_RIGHT(inter);
extra->y = r->y;
extra->width = AREA_RIGHT(*r) - AREA_RIGHT(inter);
extra->height = r->height;
area_list_push(head, extra);
}
if(AREA_BOTTOM(inter) < AREA_BOTTOM(*r))
{
extra = p_new(Area, 1);
extra->x = r->x;
extra->y = AREA_BOTTOM(inter);
extra->width = r->width;
extra->height = AREA_BOTTOM(*r) - AREA_BOTTOM(inter);
area_list_push(head, extra);
}
}
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80