simplfiy drawstatus() remove screen useless arg
[awesome.git] / draw.c
blob671424861b4feaccab1400f09334f2ee5cd5d5f0
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 size_t 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, Drawable drawable, Bool filled, unsigned long col)
75 int x;
76 XGCValues gcv;
77 XRectangle r = { drawcontext.x, drawcontext.y, drawcontext.w, drawcontext.h };
79 gcv.foreground = col;
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 r.width = r.height = x;
85 if(filled)
87 r.width++; r.height++;
88 XFillRectangles(disp, drawable, drawcontext.gc, &r, 1);
90 else
91 XDrawRectangles(disp, drawable, drawcontext.gc, &r, 1);
94 int
95 textnw(XFontSet set, XFontStruct *xfont, const char *text, int len)
97 XRectangle r;
99 if(set)
101 XmbTextExtents(set, text, len, NULL, &r);
102 return r.width;
104 return XTextWidth(xfont, text, len);