fix the ultimate bug when restarting awesome, client misdisplayed
[awesome.git] / draw.c
blob2293d4ad79b084f5a0e7b2028f4c5bca08ced1e4
1 /*
2 * draw.c - draw functions
3 *
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "layout.h"
23 #include "util.h"
24 #include "draw.h"
26 extern Client *clients, *sel, *stack; /* global client list and stack */
28 /* static */
30 void
31 drawtext(Display *disp, DC drawcontext, Drawable drawable, const char *text, unsigned long col[ColLast])
33 int x, y, w, h;
34 static char buf[256];
35 unsigned int len, olen;
36 XRectangle r = { drawcontext.x, drawcontext.y, drawcontext.w, drawcontext.h };
38 XSetForeground(disp, drawcontext.gc, col[ColBG]);
39 XFillRectangles(disp, drawable, drawcontext.gc, &r, 1);
40 if(!text)
41 return;
42 w = 0;
43 olen = len = a_strlen(text);
44 if(len >= sizeof buf)
45 len = sizeof buf - 1;
46 memcpy(buf, text, len);
47 buf[len] = 0;
48 h = drawcontext.font.ascent + drawcontext.font.descent;
49 y = drawcontext.y + (drawcontext.h / 2) - (h / 2) + drawcontext.font.ascent;
50 x = drawcontext.x + (h / 2);
51 /* shorten text if necessary */
52 while(len && (w = textnw(drawcontext.font.set, drawcontext.font.xfont, buf, len)) > drawcontext.w - h)
53 buf[--len] = 0;
54 if(w > drawcontext.w)
55 return; /* too long */
56 if(len < olen)
58 if(len > 1)
59 buf[len - 1] = '.';
60 if(len > 2)
61 buf[len - 2] = '.';
62 if(len > 3)
63 buf[len - 3] = '.';
65 XSetForeground(disp, drawcontext.gc, col[ColFG]);
66 if(drawcontext.font.set)
67 XmbDrawString(disp, drawable, drawcontext.font.set, drawcontext.gc, x, y, buf, len);
68 else
69 XDrawString(disp, drawable, drawcontext.gc, x, y, buf, len);
72 void
73 drawsquare(Display *disp, DC drawcontext, Bool filled, Bool empty, unsigned long col[ColLast], Statusbar *statusbar)
75 int x;
76 XGCValues gcv;
77 XRectangle r = { drawcontext.x, drawcontext.y, drawcontext.w, drawcontext.h };
79 gcv.foreground = col[ColFG];
80 XChangeGC(disp, drawcontext.gc, GCForeground, &gcv);
81 x = (drawcontext.font.ascent + drawcontext.font.descent + 2) / 4;
82 r.x = drawcontext.x + 1;
83 r.y = drawcontext.y + 1;
84 if(filled)
86 r.width = r.height = x + 1;
87 XFillRectangles(disp, statusbar->drawable, drawcontext.gc, &r, 1);
89 else if(empty)
91 r.width = r.height = x;
92 XDrawRectangles(disp, statusbar->drawable, drawcontext.gc, &r, 1);
96 unsigned int
97 textnw(XFontSet set, XFontStruct *xfont, const char *text, unsigned int len)
99 XRectangle r;
101 if(set)
103 XmbTextExtents(set, text, len, NULL, &r);
104 return r.width;
106 return XTextWidth(xfont, text, len);