1 /* See LICENSE file for copyright and license details. */
9 drawsquare(Bool filled, Bool empty, unsigned long col[ColLast]) {
12 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
14 gcv.foreground = col[ColFG];
15 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
16 x = (dc.font.ascent + dc.font.descent + 2) / 4;
20 r.width = r.height = x + 1;
21 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
24 r.width = r.height = x;
25 XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
30 initcolor(const char *colstr) {
31 Colormap cmap = DefaultColormap(dpy, screen);
34 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
35 eprint("error, cannot allocate color '%s'\n", colstr);
40 initfont(const char *fontstr) {
46 XFreeFontSet(dpy, dc.font.set);
47 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
50 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
51 XFreeStringList(missing);
54 XFontSetExtents *font_extents;
57 dc.font.ascent = dc.font.descent = 0;
58 font_extents = XExtentsOfFontSet(dc.font.set);
59 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
60 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
61 if(dc.font.ascent < (*xfonts)->ascent)
62 dc.font.ascent = (*xfonts)->ascent;
63 if(dc.font.descent < (*xfonts)->descent)
64 dc.font.descent = (*xfonts)->descent;
70 XFreeFont(dpy, dc.font.xfont);
72 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
73 eprint("error, cannot load font: '%s'\n", fontstr);
74 dc.font.ascent = dc.font.xfont->ascent;
75 dc.font.descent = dc.font.xfont->descent;
77 dc.font.height = dc.font.ascent + dc.font.descent;
81 isoccupied(unsigned int t) {
84 for(c = clients; c; c = c->next)
91 textnw(const char *text, unsigned int len) {
95 XmbTextExtents(dc.font.set, text, len, NULL, &r);
98 return XTextWidth(dc.font.xfont, text, len);
102 drawtext(const char *text, unsigned long col[ColLast]) {
104 static char buf[256];
105 unsigned int len, olen;
106 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
108 XSetForeground(dpy, dc.gc, col[ColBG]);
109 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
113 olen = len = strlen(text);
114 if(len >= sizeof buf)
115 len = sizeof buf - 1;
116 memcpy(buf, text, len);
118 h = dc.font.ascent + dc.font.descent;
119 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
121 /* shorten text if necessary */
122 while(len && (w = textnw(buf, len)) > dc.w - h)
133 return; /* too long */
134 XSetForeground(dpy, dc.gc, col[ColFG]);
136 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
138 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
144 unsigned int bpos = BARPOS;
153 for(i = 0; i < ntags; i++) {
154 dc.w = textw(tags[i]);
156 drawtext(tags[i], dc.sel);
157 drawsquare(sel && sel->tags[i], isoccupied(i), dc.sel);
160 drawtext(tags[i], dc.norm);
161 drawsquare(sel && sel->tags[i], isoccupied(i), dc.norm);
166 drawtext(getsymbol(), dc.norm);
174 drawtext(stext, dc.norm);
175 if((dc.w = dc.x - x) > bh) {
178 drawtext(sel->name, dc.sel);
179 drawsquare(sel->ismax, sel->isfloating, dc.sel);
182 drawtext(NULL, dc.norm);
184 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
190 XSetWindowAttributes wa;
192 dc.norm[ColBorder] = initcolor(NORMBORDERCOLOR);
193 dc.norm[ColBG] = initcolor(NORMBGCOLOR);
194 dc.norm[ColFG] = initcolor(NORMFGCOLOR);
195 dc.sel[ColBorder] = initcolor(SELBORDERCOLOR);
196 dc.sel[ColBG] = initcolor(SELBGCOLOR);
197 dc.sel[ColFG] = initcolor(SELFGCOLOR);
199 dc.h = bh = dc.font.height + 2;
200 wa.override_redirect = 1;
201 wa.background_pixmap = ParentRelative;
202 wa.event_mask = ButtonPressMask | ExposureMask;
203 barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
204 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
205 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
206 XDefineCursor(dpy, barwin, cursor[CurNormal]);
208 XMapRaised(dpy, barwin);
209 strcpy(stext, "dwm-"VERSION);
210 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
211 dc.gc = XCreateGC(dpy, root, 0, 0);
212 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
214 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
218 textw(const char *text) {
219 return textnw(text, strlen(text)) + dc.font.height;
223 togglebar(const char *arg) {
225 bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
244 XMoveWindow(dpy, barwin, sx, sy);
248 XMoveWindow(dpy, barwin, sx, sy + wah);
251 XMoveWindow(dpy, barwin, sx, sy - bh);
255 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));