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-05 20:15:00 +02:00
|
|
|
|
|
|
|
#include "layout.h"
|
2007-09-15 15:16:53 +02:00
|
|
|
#include "util.h"
|
|
|
|
#include "draw.h"
|
2007-09-05 20:15:00 +02:00
|
|
|
|
|
|
|
extern Client *clients, *sel, *stack; /* global client list and stack */
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
|
2007-09-15 15:16:53 +02:00
|
|
|
void
|
|
|
|
drawtext(Display *disp, DC drawcontext, Drawable drawable, const char *text, unsigned long col[ColLast])
|
2007-09-05 20:15:00 +02:00
|
|
|
{
|
|
|
|
int x, y, w, h;
|
|
|
|
static char buf[256];
|
|
|
|
unsigned int len, olen;
|
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-09-12 16:55:47 +02:00
|
|
|
XSetForeground(disp, drawcontext.gc, col[ColBG]);
|
2007-09-15 15:16:53 +02:00
|
|
|
XFillRectangles(disp, drawable, drawcontext.gc, &r, 1);
|
2007-09-05 20:15:00 +02:00
|
|
|
if(!text)
|
|
|
|
return;
|
|
|
|
w = 0;
|
2007-09-11 14:00:49 +02:00
|
|
|
olen = len = a_strlen(text);
|
2007-09-05 20:15:00 +02:00
|
|
|
if(len >= sizeof buf)
|
|
|
|
len = sizeof buf - 1;
|
|
|
|
memcpy(buf, text, len);
|
|
|
|
buf[len] = 0;
|
2007-09-12 16:55:47 +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);
|
2007-09-05 20:15:00 +02:00
|
|
|
/* shorten text if necessary */
|
2007-09-12 16:55:47 +02:00
|
|
|
while(len && (w = textnw(drawcontext.font.set, drawcontext.font.xfont, buf, len)) > drawcontext.w - h)
|
2007-09-05 20:15:00 +02:00
|
|
|
buf[--len] = 0;
|
2007-09-12 16:55:47 +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-09-12 16:55:47 +02:00
|
|
|
XSetForeground(disp, drawcontext.gc, col[ColFG]);
|
|
|
|
if(drawcontext.font.set)
|
2007-09-15 15:16:53 +02:00
|
|
|
XmbDrawString(disp, drawable, drawcontext.font.set, drawcontext.gc, x, y, buf, len);
|
2007-09-05 20:15:00 +02:00
|
|
|
else
|
2007-09-15 15:16:53 +02:00
|
|
|
XDrawString(disp, drawable, drawcontext.gc, x, y, buf, len);
|
2007-09-05 20:15:00 +02:00
|
|
|
}
|
|
|
|
|
2007-09-15 15:16:53 +02:00
|
|
|
void
|
2007-09-15 14:58:41 +02:00
|
|
|
drawsquare(Bool filled, Bool empty, unsigned long col[ColLast], Display *disp, DC drawcontext, Statusbar *statusbar)
|
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
|
|
|
|
|
|
|
gcv.foreground = col[ColFG];
|
2007-09-12 16:55:47 +02:00
|
|
|
XChangeGC(disp, drawcontext.gc, GCForeground, &gcv);
|
|
|
|
x = (drawcontext.font.ascent + drawcontext.font.descent + 2) / 4;
|
|
|
|
r.x = drawcontext.x + 1;
|
|
|
|
r.y = drawcontext.y + 1;
|
2007-09-05 20:15:00 +02:00
|
|
|
if(filled)
|
|
|
|
{
|
|
|
|
r.width = r.height = x + 1;
|
2007-09-15 14:58:41 +02:00
|
|
|
XFillRectangles(disp, statusbar->drawable, drawcontext.gc, &r, 1);
|
2007-09-05 20:15:00 +02:00
|
|
|
}
|
|
|
|
else if(empty)
|
|
|
|
{
|
|
|
|
r.width = r.height = x;
|
2007-09-15 14:58:41 +02:00
|
|
|
XDrawRectangles(disp, statusbar->drawable, drawcontext.gc, &r, 1);
|
2007-09-05 20:15:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-06 22:09:00 +02:00
|
|
|
unsigned int
|
2007-09-12 16:55:47 +02:00
|
|
|
textnw(XFontSet set, XFontStruct *xfont, const char *text, unsigned int len)
|
2007-09-06 22:09:00 +02:00
|
|
|
{
|
|
|
|
XRectangle r;
|
|
|
|
|
2007-09-12 16:55:47 +02:00
|
|
|
if(set)
|
2007-09-06 22:09:00 +02:00
|
|
|
{
|
2007-09-12 16:55:47 +02:00
|
|
|
XmbTextExtents(set, text, len, NULL, &r);
|
2007-09-06 22:09:00 +02:00
|
|
|
return r.width;
|
|
|
|
}
|
2007-09-12 16:55:47 +02:00
|
|
|
return XTextWidth(xfont, text, len);
|
2007-09-06 22:09:00 +02:00
|
|
|
}
|
|
|
|
|