awesome/draw.c

170 lines
5.5 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-05 20:15:00 +02:00
#include "layout.h"
extern Client *clients, *sel, *stack; /* global client list and stack */
/* static */
static void
2007-09-12 16:55:47 +02:00
drawtext(Display *disp, DC drawcontext, 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]);
XFillRectangles(disp, drawcontext.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-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)
XmbDrawString(disp, drawcontext.drawable, drawcontext.font.set, drawcontext.gc, x, y, buf, len);
2007-09-05 20:15:00 +02:00
else
2007-09-12 16:55:47 +02:00
XDrawString(disp, drawcontext.drawable, drawcontext.gc, x, y, buf, len);
2007-09-05 20:15:00 +02:00
}
static void
2007-09-12 16:55:47 +02:00
drawsquare(Bool filled, Bool empty, unsigned long col[ColLast], Display *disp, DC drawcontext)
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-12 16:55:47 +02:00
XFillRectangles(disp, drawcontext.drawable, drawcontext.gc, &r, 1);
2007-09-05 20:15:00 +02:00
}
else if(empty)
{
r.width = r.height = x;
2007-09-12 16:55:47 +02:00
XDrawRectangles(disp, drawcontext.drawable, drawcontext.gc, &r, 1);
2007-09-05 20:15:00 +02:00
}
}
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
2007-09-12 16:55:47 +02:00
textnw(XFontSet set, XFontStruct *xfont, const char *text, unsigned int len)
{
XRectangle r;
2007-09-12 16:55:47 +02:00
if(set)
{
2007-09-12 16:55:47 +02:00
XmbTextExtents(set, text, len, NULL, &r);
return r.width;
}
2007-09-12 16:55:47 +02:00
return XTextWidth(xfont, text, len);
}
2007-09-05 20:15:00 +02:00
void
2007-09-15 13:54:02 +02:00
drawstatusbar(Display *disp, DC *drawcontext, awesome_config * awesomeconf)
2007-09-05 20:15:00 +02:00
{
int x, i;
2007-09-12 16:55:47 +02:00
drawcontext->x = drawcontext->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-12 16:55:47 +02:00
drawcontext->w = textw(drawcontext->font.set, drawcontext->font.xfont, awesomeconf->tags[i], drawcontext->font.height);
2007-09-10 12:06:54 +02:00
if(awesomeconf->selected_tags[i])
2007-09-05 20:15:00 +02:00
{
2007-09-12 16:55:47 +02:00
drawtext(disp, *drawcontext, awesomeconf->tags[i], drawcontext->sel);
drawsquare(sel && sel->tags[i], isoccupied(i), drawcontext->sel, disp, *drawcontext);
2007-09-05 20:15:00 +02:00
}
else
{
2007-09-12 16:55:47 +02:00
drawtext(disp, *drawcontext, awesomeconf->tags[i], drawcontext->norm);
drawsquare(sel && sel->tags[i], isoccupied(i), drawcontext->norm, disp, *drawcontext);
2007-09-05 20:15:00 +02:00
}
2007-09-12 16:55:47 +02:00
drawcontext->x += drawcontext->w;
2007-09-05 20:15:00 +02:00
}
2007-09-12 16:55:47 +02:00
drawcontext->w = awesomeconf->statusbar.width;
drawtext(disp, *drawcontext, awesomeconf->current_layout->symbol, drawcontext->norm);
x = drawcontext->x + drawcontext->w;
drawcontext->w = textw(drawcontext->font.set, drawcontext->font.xfont, awesomeconf->statustext, drawcontext->font.height);
drawcontext->x = DisplayWidth(disp, DefaultScreen(disp)) - drawcontext->w;
if(drawcontext->x < x)
2007-09-05 20:15:00 +02:00
{
2007-09-12 16:55:47 +02:00
drawcontext->x = x;
drawcontext->w = DisplayWidth(disp, DefaultScreen(disp)) - x;
2007-09-05 20:15:00 +02:00
}
2007-09-12 16:55:47 +02:00
drawtext(disp, *drawcontext, awesomeconf->statustext, drawcontext->norm);
if((drawcontext->w = drawcontext->x - x) > awesomeconf->statusbar.height)
2007-09-05 20:15:00 +02:00
{
2007-09-12 16:55:47 +02:00
drawcontext->x = x;
2007-09-05 20:15:00 +02:00
if(sel)
{
2007-09-12 16:55:47 +02:00
drawtext(disp, *drawcontext, sel->name, drawcontext->sel);
drawsquare(sel->ismax, sel->isfloating, drawcontext->sel, disp, *drawcontext);
2007-09-05 20:15:00 +02:00
}
else
2007-09-12 16:55:47 +02:00
drawtext(disp, *drawcontext, NULL, drawcontext->norm);
2007-09-05 20:15:00 +02:00
}
2007-09-12 16:55:47 +02:00
XCopyArea(disp, drawcontext->drawable, awesomeconf->statusbar.window, drawcontext->gc, 0, 0, DisplayWidth(disp, DefaultScreen(disp)), awesomeconf->statusbar.height, 0, 0);
2007-09-05 20:15:00 +02:00
XSync(disp, False);
}