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 || !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
74 eprint("error, cannot load font: '%s'\n", fontstr);
75 dc.font.ascent = dc.font.xfont->ascent;
76 dc.font.descent = dc.font.xfont->descent;
78 dc.font.height = dc.font.ascent + dc.font.descent;
82 isoccupied(unsigned int t) {
85 for(c = clients; c; c = c->next)
92 textnw(const char *text, unsigned int len) {
96 XmbTextExtents(dc.font.set, text, len, NULL, &r);
99 return XTextWidth(dc.font.xfont, text, len);
103 drawtext(const char *text, unsigned long col[ColLast]) {
105 static char buf[256];
106 unsigned int len, olen;
107 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
109 XSetForeground(dpy, dc.gc, col[ColBG]);
110 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
114 olen = len = strlen(text);
115 if(len >= sizeof buf)
116 len = sizeof buf - 1;
117 memcpy(buf, text, len);
119 h = dc.font.ascent + dc.font.descent;
120 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
122 /* shorten text if necessary */
123 while(len && (w = textnw(buf, len)) > dc.w - h)
134 return; /* too long */
135 XSetForeground(dpy, dc.gc, col[ColFG]);
137 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
139 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
145 unsigned int bpos = BARPOS;
154 for(i = 0; i < ntags; i++) {
155 dc.w = textw(tags[i]);
157 drawtext(tags[i], dc.sel);
158 drawsquare(sel && sel->tags[i], isoccupied(i), dc.sel);
161 drawtext(tags[i], dc.norm);
162 drawsquare(sel && sel->tags[i], isoccupied(i), dc.norm);
167 drawtext(getsymbol(), dc.norm);
175 drawtext(stext, dc.norm);
176 if((dc.w = dc.x - x) > bh) {
179 drawtext(sel->name, dc.sel);
180 drawsquare(sel->ismax, sel->isfloating, dc.sel);
183 drawtext(NULL, dc.norm);
185 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
191 dc.norm[ColBorder] = initcolor(NORMBORDERCOLOR);
192 dc.norm[ColBG] = initcolor(NORMBGCOLOR);
193 dc.norm[ColFG] = initcolor(NORMFGCOLOR);
194 dc.sel[ColBorder] = initcolor(SELBORDERCOLOR);
195 dc.sel[ColBG] = initcolor(SELBGCOLOR);
196 dc.sel[ColFG] = initcolor(SELFGCOLOR);
198 dc.h = bh = dc.font.height + 2;
203 XSetWindowAttributes wa;
205 wa.override_redirect = 1;
206 wa.background_pixmap = ParentRelative;
207 wa.event_mask = ButtonPressMask | ExposureMask;
208 barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
209 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
210 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
211 XDefineCursor(dpy, barwin, cursor[CurNormal]);
213 XMapRaised(dpy, barwin);
214 strcpy(stext, "dwm-"VERSION);
215 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
216 dc.gc = XCreateGC(dpy, root, 0, 0);
217 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
219 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
223 textw(const char *text) {
224 return textnw(text, strlen(text)) + dc.font.height;
228 togglebar(const char *arg) {
230 bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
249 XMoveWindow(dpy, barwin, sx, sy);
253 XMoveWindow(dpy, barwin, sx, sy + wah);
256 XMoveWindow(dpy, barwin, sx, sy - bh);
260 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));