avoid DC in drawtext()

This commit is contained in:
Julien Danjou 2007-10-09 21:29:19 +02:00
parent 08033b1643
commit aa645b15ab
3 changed files with 19 additions and 20 deletions

23
draw.c
View File

@ -24,32 +24,28 @@
#include "draw.h" #include "draw.h"
void void
drawtext(Display *disp, int screen, DC *drawcontext, Drawable drawable, const char *text, unsigned long col[ColLast], XColor textcolor) drawtext(Display *disp, int screen, int x, int y, int w, int h, GC gc, Drawable drawable, XftFont *font, const char *text, unsigned long col[ColLast], XColor textcolor)
{ {
int x, y, w, h; int nw = 0;
static char buf[256]; static char buf[256];
size_t len, olen; size_t len, olen;
XRectangle r = { drawcontext->x, drawcontext->y, drawcontext->w, drawcontext->h }; XRectangle r = { x, y, w, h };
XRenderColor xrcolor; XRenderColor xrcolor;
XftColor xftcolor; XftColor xftcolor;
XftDraw *xftdrawable; XftDraw *xftdrawable;
XSetForeground(disp, drawcontext->gc, col[ColBG]); XSetForeground(disp, gc, col[ColBG]);
XFillRectangles(disp, drawable, drawcontext->gc, &r, 1); XFillRectangles(disp, drawable, gc, &r, 1);
if(!text) if(!text)
return; return;
w = 0;
olen = len = a_strlen(text); olen = len = a_strlen(text);
if(len >= sizeof(buf)) if(len >= sizeof(buf))
len = sizeof(buf) - 1; len = sizeof(buf) - 1;
memcpy(buf, text, len); memcpy(buf, text, len);
buf[len] = 0; buf[len] = 0;
h = drawcontext->font->height; while(len && (nw = textwidth(disp, font, buf, len)) > w - font->height)
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)
buf[--len] = 0; buf[--len] = 0;
if(w > drawcontext->w) if(nw > w)
return; /* too long */ return; /* too long */
if(len < olen) if(len < olen)
{ {
@ -65,7 +61,10 @@ drawtext(Display *disp, int screen, DC *drawcontext, Drawable drawable, const ch
xrcolor.blue = textcolor.blue; xrcolor.blue = textcolor.blue;
XftColorAllocValue(disp, DefaultVisual(disp, screen), DefaultColormap(disp, screen), &xrcolor, &xftcolor); XftColorAllocValue(disp, DefaultVisual(disp, screen), DefaultColormap(disp, screen), &xrcolor, &xftcolor);
xftdrawable = XftDrawCreate(disp, drawable, DefaultVisual(disp, screen), DefaultColormap(disp, screen)); xftdrawable = XftDrawCreate(disp, drawable, DefaultVisual(disp, screen), DefaultColormap(disp, screen));
XftDrawStringUtf8(xftdrawable, &xftcolor, drawcontext->font, x, y, (FcChar8 *) buf, len); XftDrawStringUtf8(xftdrawable, &xftcolor, font,
x + (font->height / 2),
y + (h / 2) - (font->height / 2) + font->ascent,
(FcChar8 *) buf, len);
XftColorFree(disp, DefaultVisual(disp, screen), DefaultColormap(disp, screen), &xftcolor); XftColorFree(disp, DefaultVisual(disp, screen), DefaultColormap(disp, screen), &xftcolor);
} }

2
draw.h
View File

@ -25,6 +25,6 @@
#include "config.h" #include "config.h"
void drawsquare(Display *, DC, Drawable, Bool, unsigned long); void drawsquare(Display *, DC, Drawable, Bool, unsigned long);
void drawtext(Display *, int, DC *, Drawable, const char *, unsigned long *, XColor); void drawtext(Display *, int, int, int, int, int, GC, Drawable, XftFont *, const char *, unsigned long *, XColor);
inline unsigned short textwidth(Display *, XftFont *, char *, ssize_t); inline unsigned short textwidth(Display *, XftFont *, char *, ssize_t);
#endif #endif

View File

@ -59,20 +59,20 @@ drawstatusbar(Display *disp, DC *drawcontext, awesome_config * awesomeconf)
drawcontext->w = textwidth(disp, drawcontext->font, awesomeconf->tags[i].name, a_strlen(awesomeconf->tags[i].name)) + drawcontext->font->height; drawcontext->w = textwidth(disp, drawcontext->font, awesomeconf->tags[i].name, a_strlen(awesomeconf->tags[i].name)) + drawcontext->font->height;
if(awesomeconf->tags[i].selected) if(awesomeconf->tags[i].selected)
{ {
drawtext(disp, awesomeconf->phys_screen, drawcontext, awesomeconf->statusbar.drawable, awesomeconf->tags[i].name, drawcontext->sel, drawcontext->text_selected); drawtext(disp, awesomeconf->phys_screen, drawcontext->x, drawcontext->y, drawcontext->w, drawcontext->h, drawcontext->gc, awesomeconf->statusbar.drawable, drawcontext->font, awesomeconf->tags[i].name, drawcontext->sel, drawcontext->text_selected);
if(isoccupied(i, awesomeconf->screen)) if(isoccupied(i, awesomeconf->screen))
drawsquare(disp, *drawcontext, awesomeconf->statusbar.drawable, sel && sel->tags[i], drawcontext->sel[ColFG]); drawsquare(disp, *drawcontext, awesomeconf->statusbar.drawable, sel && sel->tags[i], drawcontext->sel[ColFG]);
} }
else else
{ {
drawtext(disp, awesomeconf->phys_screen, drawcontext, awesomeconf->statusbar.drawable, awesomeconf->tags[i].name, drawcontext->norm, drawcontext->text_normal); drawtext(disp, awesomeconf->phys_screen, drawcontext->x, drawcontext->y, drawcontext->w, drawcontext->h, drawcontext->gc, awesomeconf->statusbar.drawable, drawcontext->font, awesomeconf->tags[i].name, drawcontext->norm, drawcontext->text_normal);
if(isoccupied(i, awesomeconf->screen)) if(isoccupied(i, awesomeconf->screen))
drawsquare(disp, *drawcontext, awesomeconf->statusbar.drawable, sel && sel->tags[i], drawcontext->norm[ColFG]); drawsquare(disp, *drawcontext, awesomeconf->statusbar.drawable, sel && sel->tags[i], drawcontext->norm[ColFG]);
} }
drawcontext->x += drawcontext->w; drawcontext->x += drawcontext->w;
} }
drawcontext->w = awesomeconf->statusbar.width; drawcontext->w = awesomeconf->statusbar.width;
drawtext(disp, awesomeconf->phys_screen, drawcontext, awesomeconf->statusbar.drawable, awesomeconf->current_layout->symbol, drawcontext->norm, drawcontext->text_normal); drawtext(disp, awesomeconf->phys_screen, drawcontext->x, drawcontext->y, drawcontext->w, drawcontext->h, drawcontext->gc, awesomeconf->statusbar.drawable, drawcontext->font, awesomeconf->current_layout->symbol, drawcontext->norm, drawcontext->text_normal);
x = drawcontext->x + drawcontext->w; x = drawcontext->x + drawcontext->w;
drawcontext->w = textwidth(disp, drawcontext->font, awesomeconf->statustext, a_strlen(awesomeconf->statustext)) + drawcontext->font->height; drawcontext->w = textwidth(disp, drawcontext->font, awesomeconf->statustext, a_strlen(awesomeconf->statustext)) + drawcontext->font->height;
drawcontext->x = si[awesomeconf->screen].width - drawcontext->w; drawcontext->x = si[awesomeconf->screen].width - drawcontext->w;
@ -81,13 +81,13 @@ drawstatusbar(Display *disp, DC *drawcontext, awesome_config * awesomeconf)
drawcontext->x = x; drawcontext->x = x;
drawcontext->w = si[awesomeconf->screen].width - x; drawcontext->w = si[awesomeconf->screen].width - x;
} }
drawtext(disp, awesomeconf->phys_screen, drawcontext, awesomeconf->statusbar.drawable, awesomeconf->statustext, drawcontext->norm, drawcontext->text_normal); drawtext(disp, awesomeconf->phys_screen, drawcontext->x, drawcontext->y, drawcontext->w, drawcontext->h, drawcontext->gc, awesomeconf->statusbar.drawable, drawcontext->font, awesomeconf->statustext, drawcontext->norm, drawcontext->text_normal);
if((drawcontext->w = drawcontext->x - x) > awesomeconf->statusbar.height) if((drawcontext->w = drawcontext->x - x) > awesomeconf->statusbar.height)
{ {
drawcontext->x = x; drawcontext->x = x;
if(sel) if(sel)
{ {
drawtext(disp, awesomeconf->phys_screen, drawcontext, awesomeconf->statusbar.drawable, sel->name, drawcontext->sel, drawcontext->text_selected); drawtext(disp, awesomeconf->phys_screen, drawcontext->x, drawcontext->y, drawcontext->w, drawcontext->h, drawcontext->gc, awesomeconf->statusbar.drawable, drawcontext->font, sel->name, drawcontext->sel, drawcontext->text_selected);
if(sel->isfloating) if(sel->isfloating)
drawsquare(disp, *drawcontext, awesomeconf->statusbar.drawable, sel->ismax, drawcontext->sel[ColFG]); drawsquare(disp, *drawcontext, awesomeconf->statusbar.drawable, sel->ismax, drawcontext->sel[ColFG]);
} }
@ -95,10 +95,10 @@ drawstatusbar(Display *disp, DC *drawcontext, awesome_config * awesomeconf)
{ {
char buf[256]; char buf[256];
snprintf(buf, sizeof(buf), "nmaster: %d ncol: %d mwfact: %.2lf", awesomeconf->nmaster, awesomeconf->ncol, awesomeconf->mwfact); snprintf(buf, sizeof(buf), "nmaster: %d ncol: %d mwfact: %.2lf", awesomeconf->nmaster, awesomeconf->ncol, awesomeconf->mwfact);
drawtext(disp, awesomeconf->phys_screen, drawcontext, awesomeconf->statusbar.drawable, buf, drawcontext->norm, drawcontext->text_normal); drawtext(disp, awesomeconf->phys_screen, drawcontext->x, drawcontext->y, drawcontext->w, drawcontext->h, drawcontext->gc, awesomeconf->statusbar.drawable, drawcontext->font, buf, drawcontext->norm, drawcontext->text_normal);
} }
else else
drawtext(disp, awesomeconf->phys_screen, drawcontext, awesomeconf->statusbar.drawable, NULL, drawcontext->norm, drawcontext->text_normal); drawtext(disp, awesomeconf->phys_screen, drawcontext->x, drawcontext->y, drawcontext->w, drawcontext->h, drawcontext->gc, awesomeconf->statusbar.drawable, drawcontext->font, NULL, drawcontext->norm, drawcontext->text_normal);
} }
XCopyArea(disp, awesomeconf->statusbar.drawable, awesomeconf->statusbar.window, drawcontext->gc, 0, 0, si[awesomeconf->screen].width, awesomeconf->statusbar.height, 0, 0); XCopyArea(disp, awesomeconf->statusbar.drawable, awesomeconf->statusbar.window, drawcontext->gc, 0, 0, si[awesomeconf->screen].width, awesomeconf->statusbar.height, 0, 0);
XSync(disp, False); XSync(disp, False);