awesome/draw.c

102 lines
3.4 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 "layout.h"
#include "util.h"
#include "draw.h"
2007-09-05 20:15:00 +02:00
void
2007-10-01 19:22:57 +02:00
drawtext(Display *disp, int screen, DC *drawcontext, Drawable drawable, const char *text, unsigned long col[ColLast], XColor textcolor)
2007-09-05 20:15:00 +02:00
{
int x, y, w, h;
static char buf[256];
2007-09-20 20:11:33 +02:00
size_t len, olen;
2007-10-01 19:22:57 +02:00
XRectangle r = { drawcontext->x, drawcontext->y, drawcontext->w, drawcontext->h };
XRenderColor xrcolor;
XftColor xftcolor;
XftDraw *xftdrawable;
2007-09-05 20:15:00 +02:00
2007-10-01 19:22:57 +02:00
XSetForeground(disp, drawcontext->gc, col[ColBG]);
XFillRectangles(disp, drawable, drawcontext->gc, &r, 1);
2007-09-05 20:15:00 +02:00
if(!text)
return;
w = 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-01 19:22:57 +02:00
h = drawcontext->font->ascent + drawcontext->font->descent;
y = drawcontext->y + (drawcontext->h / 2) - (h / 2) + drawcontext->font->ascent;
x = drawcontext->x + (h / 2);
while(len && (w = textwidth(disp, drawcontext->font, buf, len)) > drawcontext->w - h)
2007-09-05 20:15:00 +02:00
buf[--len] = 0;
2007-10-01 19:22:57 +02:00
if(w > drawcontext->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-01 19:22:57 +02:00
xrcolor.red = textcolor.red;
xrcolor.green = textcolor.green;
xrcolor.blue = textcolor.blue;
XftColorAllocValue(disp, DefaultVisual(disp, screen), DefaultColormap(disp, screen), &xrcolor, &xftcolor);
xftdrawable = XftDrawCreate(disp, drawable, DefaultVisual(disp, screen), DefaultColormap(disp, screen));
XftDrawStringUtf8(xftdrawable, &xftcolor, drawcontext->font, x, y, (FcChar8 *) buf, len);
XftColorFree(disp, DefaultVisual(disp, screen), DefaultColormap(disp, screen), &xftcolor);
2007-09-05 20:15:00 +02:00
}
void
2007-09-20 20:11:33 +02:00
drawsquare(Display *disp, DC drawcontext, Drawable drawable, Bool filled, unsigned long col)
2007-09-05 20:15:00 +02:00
{
int x;
XGCValues gcv;
2007-09-12 16:55:47 +02:00
XRectangle r = { drawcontext.x, drawcontext.y, drawcontext.w, drawcontext.h };
2007-09-05 20:15:00 +02:00
2007-10-01 19:22:57 +02:00
x = (drawcontext.font->ascent + drawcontext.font->descent + 2) / 4;
2007-09-19 15:42:40 +02:00
gcv.foreground = col;
2007-09-12 16:55:47 +02:00
XChangeGC(disp, drawcontext.gc, GCForeground, &gcv);
r.x = drawcontext.x + 1;
r.y = drawcontext.y + 1;
2007-09-20 20:11:33 +02:00
r.width = r.height = x;
2007-09-05 20:15:00 +02:00
if(filled)
{
2007-09-20 20:11:33 +02:00
r.width++; r.height++;
XFillRectangles(disp, drawable, drawcontext.gc, &r, 1);
2007-09-05 20:15:00 +02:00
}
2007-09-20 20:11:33 +02:00
else
XDrawRectangles(disp, drawable, drawcontext.gc, &r, 1);
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;
}