awesome/draw.c

180 lines
5.6 KiB
C
Raw Normal View History

/*
2007-09-12 14:29:51 +02:00
* draw.c - draw functions
*
* Copyright © 2007 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>
2007-09-05 20:15:00 +02:00
#include "layout.h"
#include "util.h"
#include "draw.h"
2007-09-05 20:15:00 +02:00
void
drawtext(Display *disp, int screen, int x, int y, int w, int h, Drawable drawable, int dw, int dh, XftFont *font, const char *text, XColor color[ColLast])
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
drawrectangle(disp, screen, x, y, w, h, drawable, dw, dh, True, color[ColBG]);
2007-10-15 12:42:48 +02:00
if(!a_strlen(text))
2007-09-05 20:15:00 +02:00
return;
2007-10-16 01:20:03 +02:00
surface = cairo_xlib_surface_create(disp, drawable, DefaultVisual(disp, screen), dw, dh);
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);
cairo_set_source_rgb(cr, color[ColFG].red / 65535.0, color[ColFG].green / 65535.0, color[ColFG].blue / 65535.0);
olen = len = a_strlen(text);
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-10-16 01:20:03 +02:00
while(len && (nw = textwidth(disp, font, buf)) > 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-10-16 19:20:39 +02:00
cairo_move_to(cr, x + font->height / 2, y + font->ascent + (dh - 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
}
void
2007-10-11 15:44:30 +02:00
drawrectangle(Display *disp, int screen, int x, int y, int w, int h, Drawable drawable, int dw, int dh, 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
surface = cairo_xlib_surface_create(disp, drawable, DefaultVisual(disp, screen), dw, dh);
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)
{
cairo_rectangle(cr, x, y, w + 1, h + 1);
cairo_fill(cr);
2007-09-05 20:15:00 +02:00
}
2007-09-20 20:11:33 +02:00
else
cairo_rectangle(cr, x + 1, y, w, h);
cairo_stroke(cr);
cairo_destroy(cr);
cairo_surface_destroy(surface);
2007-09-05 20:15:00 +02:00
}
void
drawcircle(Display *disp, int screen, int x, int y, int r, Drawable drawable, int dw, int dh, Bool filled, XColor color)
{
cairo_surface_t *surface;
cairo_t *cr;
surface = cairo_xlib_surface_create(disp, drawable, DefaultVisual(disp, screen), dw, dh);
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);
}
Drawable
2007-11-11 19:49:42 +01:00
draw_rotate(Display *disp, int screen, Drawable drawable, int dw, int dh, double angle, int tx, int ty)
{
cairo_surface_t *surface, *source;
cairo_t *cr;
Drawable newdrawable;
newdrawable = XCreatePixmap(disp,
RootWindow(disp, screen),
dw, dw,
DefaultDepth(disp, screen));
surface = cairo_xlib_surface_create(disp, newdrawable, DefaultVisual(disp, screen), dw, dw);
source = cairo_xlib_surface_create(disp, drawable, DefaultVisual(disp, screen), dw, dw);
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);
return newdrawable;
}
2007-10-01 19:22:57 +02:00
unsigned short
2007-10-16 01:20:03 +02:00
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;
surface = cairo_xlib_surface_create(disp, DefaultScreen(disp),
DefaultVisual(disp, DefaultScreen(disp)),
DisplayWidth(disp, DefaultScreen(disp)),
DisplayHeight(disp, DefaultScreen(disp)));
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) + font->height;
}
2007-10-15 13:40:52 +02:00
2007-10-15 13:56:24 +02:00
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99