awesome/draw.c

106 lines
3.3 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>
#include <cairo-xlib.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
2007-10-10 13:07:12 +02:00
drawtext(Display *disp, int screen, int x, int y, int w, int h, GC gc, Drawable drawable, 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-09 21:29:19 +02:00
XRectangle r = { x, y, w, h };
2007-10-01 19:22:57 +02:00
XRenderColor xrcolor;
XftColor xftcolor;
XftDraw *xftdrawable;
2007-09-05 20:15:00 +02:00
2007-10-10 13:07:12 +02:00
XSetForeground(disp, gc, color[ColBG].pixel);
2007-10-09 21:29:19 +02:00
XFillRectangles(disp, drawable, gc, &r, 1);
2007-09-05 20:15:00 +02:00
if(!text)
return;
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-09 21:29:19 +02:00
while(len && (nw = textwidth(disp, font, buf, len)) > w - font->height)
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-10 13:07:12 +02:00
xrcolor.red = color[ColFG].red;
xrcolor.green = color[ColFG].green;
xrcolor.blue = color[ColFG].blue;
2007-10-01 19:22:57 +02:00
XftColorAllocValue(disp, DefaultVisual(disp, screen), DefaultColormap(disp, screen), &xrcolor, &xftcolor);
xftdrawable = XftDrawCreate(disp, drawable, DefaultVisual(disp, screen), DefaultColormap(disp, screen));
2007-10-09 21:29:19 +02:00
XftDrawStringUtf8(xftdrawable, &xftcolor, font,
x + (font->height / 2),
y + (h / 2) - (font->height / 2) + font->ascent,
(FcChar8 *) buf, len);
2007-10-01 19:22:57 +02:00
XftColorFree(disp, DefaultVisual(disp, screen), DefaultColormap(disp, screen), &xftcolor);
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, color.green, color.blue);
2007-09-05 20:15:00 +02:00
if(filled)
{
2007-10-11 15:44:30 +02:00
cairo_rectangle(cr, x + 1, y + 1, w + 1, h + 1);
cairo_fill(cr);
2007-09-05 20:15:00 +02:00
}
2007-09-20 20:11:33 +02:00
else
2007-10-11 15:44:30 +02:00
cairo_rectangle(cr, x + 1, y + 1, w, h);
cairo_stroke(cr);
cairo_destroy(cr);
cairo_surface_destroy(surface);
2007-09-05 20:15:00 +02:00
}
2007-10-01 19:22:57 +02:00
unsigned short
textwidth(Display *disp, XftFont *font, char *text, ssize_t len)
{
XGlyphInfo gi;
XftTextExtentsUtf8(disp, font, (FcChar8 *) text, len, &gi);
return gi.width;
}