aewl

annotate draw.c @ 751:743061dbe7f2

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