awesome/draw.c

152 lines
3.9 KiB
C
Raw Normal View History

2007-09-05 20:15:00 +02:00
/* See LICENSE file for copyright and license details. */
#include "layout.h"
extern DC dc; /* global draw context */
extern Client *clients, *sel, *stack; /* global client list and stack */
/* static */
static void
drawtext(Display *disp, const char *text, unsigned long col[ColLast])
{
int x, y, w, h;
static char buf[256];
unsigned int len, olen;
XRectangle r = { dc.x, dc.y, dc.w, dc.h };
XSetForeground(disp, dc.gc, col[ColBG]);
XFillRectangles(disp, dc.drawable, dc.gc, &r, 1);
if(!text)
return;
w = 0;
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;
h = dc.font.ascent + dc.font.descent;
y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
x = dc.x + (h / 2);
/* shorten text if necessary */
while(len && (w = textnw(buf, len)) > dc.w - h)
buf[--len] = 0;
if(len < olen)
{
if(len > 1)
buf[len - 1] = '.';
if(len > 2)
buf[len - 2] = '.';
if(len > 3)
buf[len - 3] = '.';
}
if(w > dc.w)
return; /* too long */
XSetForeground(disp, dc.gc, col[ColFG]);
if(dc.font.set)
XmbDrawString(disp, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
else
XDrawString(disp, dc.drawable, dc.gc, x, y, buf, len);
}
static void
drawsquare(Bool filled, Bool empty, unsigned long col[ColLast], Display *disp)
{
int x;
XGCValues gcv;
XRectangle r = { dc.x, dc.y, dc.w, dc.h };
gcv.foreground = col[ColFG];
XChangeGC(disp, dc.gc, GCForeground, &gcv);
x = (dc.font.ascent + dc.font.descent + 2) / 4;
r.x = dc.x + 1;
r.y = dc.y + 1;
if(filled)
{
r.width = r.height = x + 1;
XFillRectangles(disp, dc.drawable, dc.gc, &r, 1);
}
else if(empty)
{
r.width = r.height = x;
XDrawRectangles(disp, dc.drawable, dc.gc, &r, 1);
}
}
2007-09-06 18:43:49 +02:00
/** Check if at least a client is tagged with tag number t
* \param t tag number
* \return True or False
*/
2007-09-05 20:15:00 +02:00
static Bool
isoccupied(unsigned int t)
{
Client *c;
for(c = clients; c; c = c->next)
if(c->tags[t])
return True;
return False;
}
/* extern */
unsigned int
textnw(const char *text, unsigned int len)
{
XRectangle r;
if(dc.font.set)
{
XmbTextExtents(dc.font.set, text, len, NULL, &r);
return r.width;
}
return XTextWidth(dc.font.xfont, text, len);
}
2007-09-05 20:15:00 +02:00
void
2007-09-10 12:06:54 +02:00
drawstatus(Display *disp, awesome_config * awesomeconf)
2007-09-05 20:15:00 +02:00
{
int x, i;
dc.x = dc.y = 0;
2007-09-10 12:06:54 +02:00
for(i = 0; i < awesomeconf->ntags; i++)
2007-09-05 20:15:00 +02:00
{
2007-09-10 12:06:54 +02:00
dc.w = textw(awesomeconf->tags[i]);
if(awesomeconf->selected_tags[i])
2007-09-05 20:15:00 +02:00
{
2007-09-10 12:06:54 +02:00
drawtext(disp, awesomeconf->tags[i], dc.sel);
2007-09-05 20:15:00 +02:00
drawsquare(sel && sel->tags[i], isoccupied(i), dc.sel, disp);
}
else
{
2007-09-10 12:06:54 +02:00
drawtext(disp, awesomeconf->tags[i], dc.norm);
2007-09-05 20:15:00 +02:00
drawsquare(sel && sel->tags[i], isoccupied(i), dc.norm, disp);
}
dc.x += dc.w;
}
2007-09-10 12:06:54 +02:00
dc.w = awesomeconf->statusbar.width;
drawtext(disp, awesomeconf->current_layout->symbol, dc.norm);
2007-09-05 20:15:00 +02:00
x = dc.x + dc.w;
2007-09-10 12:06:54 +02:00
dc.w = textw(awesomeconf->statustext);
2007-09-07 17:19:03 +02:00
dc.x = DisplayWidth(disp, DefaultScreen(disp)) - dc.w;
2007-09-05 20:15:00 +02:00
if(dc.x < x)
{
dc.x = x;
2007-09-07 17:19:03 +02:00
dc.w = DisplayWidth(disp, DefaultScreen(disp)) - x;
2007-09-05 20:15:00 +02:00
}
2007-09-10 12:06:54 +02:00
drawtext(disp, awesomeconf->statustext, dc.norm);
if((dc.w = dc.x - x) > awesomeconf->statusbar.height)
2007-09-05 20:15:00 +02:00
{
dc.x = x;
if(sel)
{
drawtext(disp, sel->name, dc.sel);
drawsquare(sel->ismax, sel->isfloating, dc.sel, disp);
}
else
drawtext(disp, NULL, dc.norm);
}
2007-09-10 12:06:54 +02:00
XCopyArea(disp, dc.drawable, awesomeconf->statusbar.window, dc.gc, 0, 0, DisplayWidth(disp, DefaultScreen(disp)), awesomeconf->statusbar.height, 0, 0);
2007-09-05 20:15:00 +02:00
XSync(disp, False);
}