2005-11-18 18:54:58 +03:00
|
|
|
/*
|
2006-01-20 17:20:24 +03:00
|
|
|
* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
2005-11-18 18:54:58 +03:00
|
|
|
* See LICENSE file for license details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "blitz.h"
|
|
|
|
|
2006-02-02 18:44:45 +03:00
|
|
|
XFontStruct *
|
2006-03-10 20:22:48 +03:00
|
|
|
blitz_getfont(Display *dpy, char *fontstr)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2005-12-05 01:45:59 +03:00
|
|
|
XFontStruct *font;
|
2005-11-18 18:54:58 +03:00
|
|
|
font = XLoadQueryFont(dpy, fontstr);
|
|
|
|
if (!font) {
|
|
|
|
font = XLoadQueryFont(dpy, "fixed");
|
|
|
|
if (!font) {
|
|
|
|
fprintf(stderr, "%s", "wmii: error, cannot load fixed font\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
2006-02-02 18:44:45 +03:00
|
|
|
static unsigned long
|
2006-03-10 20:22:48 +03:00
|
|
|
xloadcolor(Display *dpy, int mon, char *colstr)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2005-12-05 01:45:59 +03:00
|
|
|
XColor color;
|
|
|
|
char col[8];
|
2005-11-18 18:54:58 +03:00
|
|
|
|
2005-12-06 20:58:52 +03:00
|
|
|
cext_strlcpy(col, colstr, sizeof(col));
|
2005-12-08 03:46:45 +03:00
|
|
|
col[7] = 0;
|
2005-11-18 18:54:58 +03:00
|
|
|
XAllocNamedColor(dpy, DefaultColormap(dpy, mon), col, &color, &color);
|
|
|
|
return color.pixel;
|
|
|
|
}
|
|
|
|
|
2006-02-02 18:44:45 +03:00
|
|
|
int
|
|
|
|
blitz_loadcolor(Display *dpy, int mon, char *colstr, Color *c)
|
|
|
|
{
|
|
|
|
if(!colstr || strlen(colstr) != 23)
|
|
|
|
return -1;
|
|
|
|
c->fg = xloadcolor(dpy, mon, &colstr[0]);
|
|
|
|
c->bg = xloadcolor(dpy, mon, &colstr[8]);
|
|
|
|
c->border = xloadcolor(dpy, mon, &colstr[16]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-03-10 20:22:48 +03:00
|
|
|
xdrawbg(Display *dpy, Draw *d)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2005-12-05 01:45:59 +03:00
|
|
|
XRectangle rect[4];
|
2006-02-02 18:44:45 +03:00
|
|
|
XSetForeground(dpy, d->gc, d->color.bg);
|
2005-11-18 18:54:58 +03:00
|
|
|
if (!d->notch) {
|
|
|
|
XFillRectangles(dpy, d->drawable, d->gc, &d->rect, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rect[0] = d->rect;
|
|
|
|
rect[0].height = d->notch->y;
|
|
|
|
rect[1] = d->rect;
|
|
|
|
rect[1].y = d->notch->y;
|
|
|
|
rect[1].width = d->notch->x;
|
|
|
|
rect[1].height = d->notch->height;
|
|
|
|
rect[2].x = d->notch->x + d->notch->width;
|
|
|
|
rect[2].y = d->notch->y;
|
|
|
|
rect[2].width = d->rect.width - (d->notch->x + d->notch->width);
|
|
|
|
rect[2].height = d->notch->height;
|
|
|
|
rect[3] = d->rect;
|
|
|
|
rect[3].y = d->notch->y + d->notch->height;
|
|
|
|
rect[3].height = d->rect.height - (d->notch->y + d->notch->height);
|
|
|
|
XFillRectangles(dpy, d->drawable, d->gc, rect, 4);
|
|
|
|
}
|
|
|
|
|
2006-03-10 20:22:48 +03:00
|
|
|
void
|
|
|
|
blitz_drawborder(Display *dpy, Draw *d)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2005-12-05 01:45:59 +03:00
|
|
|
XPoint points[5];
|
2005-11-18 18:54:58 +03:00
|
|
|
|
|
|
|
XSetLineAttributes(dpy, d->gc, 1, LineSolid, CapButt, JoinMiter);
|
2006-02-02 18:44:45 +03:00
|
|
|
XSetForeground(dpy, d->gc, d->color.border);
|
2005-11-18 18:54:58 +03:00
|
|
|
points[0].x = d->rect.x;
|
|
|
|
points[0].y = d->rect.y;
|
|
|
|
points[1].x = d->rect.width - 1;
|
|
|
|
points[1].y = 0;
|
|
|
|
points[2].x = 0;
|
|
|
|
points[2].y = d->rect.height - 1;
|
|
|
|
points[3].x = -(d->rect.width - 1);
|
|
|
|
points[3].y = 0;
|
|
|
|
points[4].x = 0;
|
|
|
|
points[4].y = -(d->rect.height - 1);
|
|
|
|
XDrawLines(dpy, d->drawable, d->gc, points, 5, CoordModePrevious);
|
|
|
|
}
|
|
|
|
|
2006-02-02 18:44:45 +03:00
|
|
|
static void
|
2006-03-10 20:22:48 +03:00
|
|
|
xdrawtext(Display *dpy, Draw *d)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2006-02-20 11:44:20 +03:00
|
|
|
unsigned int x = 0, y = 0, w = 1, h = 1, shortened = 0;
|
2006-03-01 13:55:46 +03:00
|
|
|
unsigned int len = 0;
|
2005-12-05 01:45:59 +03:00
|
|
|
static char text[2048];
|
2005-11-18 18:54:58 +03:00
|
|
|
|
|
|
|
if (!d->data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
len = strlen(d->data);
|
2005-12-06 20:58:52 +03:00
|
|
|
cext_strlcpy(text, d->data, sizeof(text));
|
2005-11-18 18:54:58 +03:00
|
|
|
XSetFont(dpy, d->gc, d->font->fid);
|
|
|
|
h = d->font->ascent + d->font->descent;
|
|
|
|
y = d->rect.y + d->rect.height / 2 - h / 2 + d->font->ascent;
|
|
|
|
|
|
|
|
/* shorten text if necessary */
|
|
|
|
while (len && (w = XTextWidth(d->font, text, len)) > d->rect.width) {
|
2005-12-08 03:46:45 +03:00
|
|
|
text[len - 1] = 0;
|
2005-11-18 18:54:58 +03:00
|
|
|
len--;
|
2006-01-04 18:23:09 +03:00
|
|
|
shortened = 1;
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (w > d->rect.width)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* mark shortened info in the string */
|
|
|
|
if (shortened) {
|
|
|
|
if (len > 3)
|
|
|
|
text[len - 3] = '.';
|
|
|
|
if (len > 2)
|
|
|
|
text[len - 2] = '.';
|
|
|
|
if (len > 1)
|
|
|
|
text[len - 1] = '.';
|
|
|
|
}
|
2006-02-07 02:32:57 +03:00
|
|
|
switch (d->align) {
|
|
|
|
case EAST:
|
|
|
|
x = d->rect.x + d->rect.width - (h / 2 + w);
|
|
|
|
break;
|
|
|
|
case CENTER:
|
|
|
|
x = d->rect.x + (d->rect.width - w) / 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
x = d->rect.x + h / 2;
|
|
|
|
break;
|
|
|
|
}
|
2006-02-02 18:44:45 +03:00
|
|
|
XSetBackground(dpy, d->gc, d->color.bg);
|
2005-11-18 18:54:58 +03:00
|
|
|
/*
|
|
|
|
* uncomment, if you want get an shadow effect XSetForeground(dpy,
|
|
|
|
* d->gc, BlackPixel(dpy, DefaultScreen(dpy))); XDrawString(dpy,
|
|
|
|
* d->drawable, d->gc, x + 1, y + 1, text, len);
|
|
|
|
*/
|
2006-02-02 18:44:45 +03:00
|
|
|
XSetForeground(dpy, d->gc, d->color.fg);
|
2005-11-18 18:54:58 +03:00
|
|
|
XDrawString(dpy, d->drawable, d->gc, x, y, text, len);
|
|
|
|
}
|
|
|
|
|
2006-02-02 18:44:45 +03:00
|
|
|
void
|
|
|
|
blitz_drawmeter(Display * dpy, Draw * d)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2006-02-09 21:40:12 +03:00
|
|
|
const char *err;
|
2005-12-05 01:45:59 +03:00
|
|
|
unsigned int offy, mh, val, w = d->rect.width - 4;
|
2005-11-18 18:54:58 +03:00
|
|
|
|
|
|
|
if (!d->data || strncmp(d->data, "%m:", 3))
|
|
|
|
return;
|
|
|
|
|
2006-02-09 21:40:12 +03:00
|
|
|
val = cext_strtonum(&d->data[3], 0, 100, &err);
|
|
|
|
if(err)
|
|
|
|
val = 100;
|
2006-03-10 20:22:48 +03:00
|
|
|
xdrawbg(dpy, d);
|
|
|
|
blitz_drawborder(dpy, d);
|
2005-11-18 18:54:58 +03:00
|
|
|
|
|
|
|
/* draw bg gradient */
|
|
|
|
mh = ((d->rect.height - 4) * val) / 100;
|
|
|
|
offy = d->rect.y + d->rect.height - 2 - mh;
|
2006-02-02 18:44:45 +03:00
|
|
|
XSetForeground(dpy, d->gc, d->color.fg);
|
2005-11-18 18:54:58 +03:00
|
|
|
XFillRectangle(dpy, d->drawable, d->gc, d->rect.x + 2, offy, w, mh);
|
|
|
|
}
|
|
|
|
|
2006-03-10 20:22:48 +03:00
|
|
|
void
|
|
|
|
blitz_drawlabel(Display *dpy, Draw * d)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2006-03-10 20:22:48 +03:00
|
|
|
xdrawbg(dpy, d);
|
2005-11-18 18:54:58 +03:00
|
|
|
if (d->data)
|
2006-03-10 20:22:48 +03:00
|
|
|
xdrawtext(dpy, d);
|
|
|
|
}
|