dwm-meillo

view draw.c @ 750:3d957a703ce2

added absolute path to halt
author meillo@marmaro.de
date Thu, 05 Jul 2007 22:05:11 +0200
parents 61821891835c
children
line source
1 /* (C)opyright MMIV-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
3 */
4 #include "dwm.h"
5 #include <stdio.h>
6 #include <string.h>
8 /* static */
10 static Bool
11 isoccupied(unsigned int t)
12 {
13 Client *c;
14 for(c = clients; c; c = c->next)
15 if(c->tags[t])
16 return True;
17 return False;
18 }
20 static unsigned int
21 textnw(const char *text, unsigned int len) {
22 XRectangle r;
24 if(dc.font.set) {
25 XmbTextExtents(dc.font.set, text, len, NULL, &r);
26 return r.width;
27 }
28 return XTextWidth(dc.font.xfont, text, len);
29 }
31 static void
32 drawtext(const char *text, unsigned long col[ColLast], Bool filledsquare, Bool emptysquare) {
33 int x, y, w, h;
34 static char buf[256];
35 unsigned int len, olen;
36 XGCValues gcv;
37 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
39 XSetForeground(dpy, dc.gc, col[ColBG]);
40 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
41 if(!text)
42 return;
43 w = 0;
44 olen = len = strlen(text);
45 if(len >= sizeof buf)
46 len = sizeof buf - 1;
47 memcpy(buf, text, len);
48 buf[len] = 0;
49 h = dc.font.ascent + dc.font.descent;
50 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
51 x = dc.x + (h / 2);
52 /* shorten text if necessary */
53 while(len && (w = textnw(buf, len)) > dc.w - h)
54 buf[--len] = 0;
55 if(len < olen) {
56 if(len > 1)
57 buf[len - 1] = '.';
58 if(len > 2)
59 buf[len - 2] = '.';
60 if(len > 3)
61 buf[len - 3] = '.';
62 }
63 if(w > dc.w)
64 return; /* too long */
65 gcv.foreground = col[ColFG];
66 if(dc.font.set) {
67 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
68 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
69 }
70 else {
71 gcv.font = dc.font.xfont->fid;
72 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
73 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
74 }
75 }
77 /* extern */
79 void
80 drawstatus(void) {
81 int i, x;
83 dc.x = dc.y = 0;
84 for(i = 0; i < ntags; i++) {
85 dc.w = textw(tags[i]);
86 if(seltag[i])
87 drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
88 else
89 drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
90 dc.x += dc.w + 1;
91 }
92 dc.w = bmw;
93 drawtext("", dc.norm, False, False);
94 x = dc.x + dc.w;
95 dc.w = textw(stext);
96 dc.x = sw - dc.w;
97 if(dc.x < x) {
98 dc.x = x;
99 dc.w = sw - x;
100 }
101 drawtext(stext, dc.norm, False, False);
102 if((dc.w = dc.x - x) > bh) {
103 dc.x = x;
104 drawtext(sel ? sel->name : NULL, dc.norm, False, False);
105 }
106 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
107 XSync(dpy, False);
108 }
110 unsigned long
111 getcolor(const char *colstr) {
112 Colormap cmap = DefaultColormap(dpy, screen);
113 XColor color;
115 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
116 eprint("error, cannot allocate color '%s'\n", colstr);
117 return color.pixel;
118 }
120 void
121 setfont(const char *fontstr) {
122 char *def, **missing;
123 int i, n;
125 missing = NULL;
126 if(dc.font.set)
127 XFreeFontSet(dpy, dc.font.set);
128 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
129 if(missing) {
130 while(n--)
131 fprintf(stderr, "missing fontset: %s\n", missing[n]);
132 XFreeStringList(missing);
133 }
134 if(dc.font.set) {
135 XFontSetExtents *font_extents;
136 XFontStruct **xfonts;
137 char **font_names;
138 dc.font.ascent = dc.font.descent = 0;
139 font_extents = XExtentsOfFontSet(dc.font.set);
140 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
141 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
142 if(dc.font.ascent < (*xfonts)->ascent)
143 dc.font.ascent = (*xfonts)->ascent;
144 if(dc.font.descent < (*xfonts)->descent)
145 dc.font.descent = (*xfonts)->descent;
146 xfonts++;
147 }
148 }
149 else {
150 if(dc.font.xfont)
151 XFreeFont(dpy, dc.font.xfont);
152 dc.font.xfont = NULL;
153 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
154 eprint("error, cannot load font: '%s'\n", fontstr);
155 dc.font.ascent = dc.font.xfont->ascent;
156 dc.font.descent = dc.font.xfont->descent;
157 }
158 dc.font.height = dc.font.ascent + dc.font.descent;
159 }
161 unsigned int
162 textw(const char *text) {
163 return textnw(text, strlen(text)) + dc.font.height;
164 }