dwm-meillo

annotate draw.c @ 516:e5f8d6ac509c

added symbols for different modes
author Anselm R. Garbe <arg@10kloc.org>
date Fri, 29 Sep 2006 18:08:20 +0200
parents 3a812750f983
children 6cad48068e4c
rev   line source
garbeam@2 1 /*
garbeam@2 2 * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
garbeam@2 3 * See LICENSE file for license details.
garbeam@2 4 */
garbeam@76 5 #include "dwm.h"
garbeam@2 6 #include <stdio.h>
garbeam@2 7 #include <string.h>
garbeam@32 8 #include <X11/Xlocale.h>
garbeam@32 9
garbeam@84 10 /* static */
garbeam@76 11
garbeam@77 12 static unsigned int
arg@461 13 textnw(const char *text, unsigned int len) {
garbeam@77 14 XRectangle r;
arg@123 15
garbeam@77 16 if(dc.font.set) {
garbeam@77 17 XmbTextExtents(dc.font.set, text, len, NULL, &r);
garbeam@77 18 return r.width;
garbeam@77 19 }
garbeam@77 20 return XTextWidth(dc.font.xfont, text, len);
garbeam@75 21 }
garbeam@75 22
garbeam@77 23 static void
arg@461 24 drawtext(const char *text, unsigned long col[ColLast], Bool highlight) {
arg@344 25 int x, y, w, h;
arg@344 26 static char buf[256];
arg@344 27 unsigned int len, olen;
arg@352 28 XGCValues gcv;
arg@344 29 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
arg@344 30
arg@353 31 XSetForeground(dpy, dc.gc, col[ColBG]);
arg@344 32 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
arg@237 33
garbeam@3 34 if(!text)
garbeam@2 35 return;
garbeam@2 36
arg@218 37 w = 0;
arg@269 38 olen = len = strlen(text);
garbeam@2 39 if(len >= sizeof(buf))
garbeam@2 40 len = sizeof(buf) - 1;
garbeam@3 41 memcpy(buf, text, len);
garbeam@2 42 buf[len] = 0;
garbeam@2 43
garbeam@34 44 h = dc.font.ascent + dc.font.descent;
garbeam@34 45 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
garbeam@34 46 x = dc.x + (h / 2);
garbeam@2 47
garbeam@2 48 /* shorten text if necessary */
garbeam@43 49 while(len && (w = textnw(buf, len)) > dc.w - h)
garbeam@2 50 buf[--len] = 0;
arg@269 51 if(len < olen) {
arg@273 52 if(len > 1)
arg@273 53 buf[len - 1] = '.';
arg@273 54 if(len > 2)
arg@273 55 buf[len - 2] = '.';
arg@269 56 if(len > 3)
arg@273 57 buf[len - 3] = '.';
arg@269 58 }
garbeam@2 59
garbeam@34 60 if(w > dc.w)
garbeam@2 61 return; /* too long */
arg@353 62 gcv.foreground = col[ColFG];
arg@352 63 if(dc.font.set) {
arg@353 64 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
arg@342 65 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
arg@352 66 }
garbeam@2 67 else {
arg@352 68 gcv.font = dc.font.xfont->fid;
arg@353 69 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
arg@342 70 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
arg@342 71 }
arg@345 72 if(highlight) {
arg@352 73 r.x = dc.x + 2;
arg@352 74 r.y = dc.y + 2;
arg@352 75 r.width = r.height = 3;
arg@352 76 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
garbeam@2 77 }
garbeam@2 78 }
garbeam@2 79
garbeam@84 80 /* extern */
garbeam@77 81
garbeam@77 82 void
arg@487 83 drawall(void) {
garbeam@77 84 Client *c;
garbeam@77 85
arg@142 86 for(c = clients; c; c = getnext(c->next))
garbeam@77 87 drawtitle(c);
garbeam@77 88 drawstatus();
garbeam@77 89 }
garbeam@77 90
garbeam@77 91 void
arg@487 92 drawstatus(void) {
arg@124 93 int i, x;
garbeam@77 94
garbeam@77 95 dc.x = dc.y = 0;
arg@353 96
arg@178 97 for(i = 0; i < ntags; i++) {
garbeam@77 98 dc.w = textw(tags[i]);
arg@353 99 if(seltag[i])
arg@353 100 drawtext(tags[i], dc.sel, sel && sel->tags[i]);
garbeam@77 101 else
arg@353 102 drawtext(tags[i], dc.norm, sel && sel->tags[i]);
arg@362 103 dc.x += dc.w;
garbeam@77 104 }
arg@362 105
arg@362 106 dc.w = bmw;
arg@516 107 drawtext(arrange == dofloat ?
arg@516 108 FLOATSYMBOL : stackpos == StackBottom ?
arg@516 109 BSTACKSYMBOL : VSTACKSYMBOL, dc.status, False);
arg@362 110
arg@361 111 x = dc.x + dc.w;
garbeam@77 112 dc.w = textw(stext);
garbeam@77 113 dc.x = bx + bw - dc.w;
arg@342 114 if(dc.x < x) {
arg@342 115 dc.x = x;
arg@342 116 dc.w = bw - x;
arg@342 117 }
arg@353 118 drawtext(stext, dc.status, False);
arg@340 119
arg@371 120 if((dc.w = dc.x - x) > bh) {
arg@342 121 dc.x = x;
arg@371 122 if(sel)
arg@371 123 drawtext(sel->name, dc.sel, False);
arg@371 124 else
arg@371 125 drawtext(NULL, dc.norm, False);
arg@124 126 }
garbeam@77 127 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
garbeam@79 128 XSync(dpy, False);
garbeam@77 129 }
garbeam@77 130
garbeam@76 131 void
arg@461 132 drawtitle(Client *c) {
arg@239 133 if(c == sel && issel) {
garbeam@76 134 drawstatus();
arg@342 135 XUnmapWindow(dpy, c->twin);
arg@353 136 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
garbeam@76 137 return;
garbeam@76 138 }
garbeam@76 139
arg@353 140 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
arg@342 141 XMapWindow(dpy, c->twin);
arg@342 142 dc.x = dc.y = 0;
arg@342 143 dc.w = c->tw;
arg@353 144 drawtext(c->name, dc.norm, False);
arg@342 145 XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
garbeam@79 146 XSync(dpy, False);
garbeam@76 147 }
garbeam@76 148
garbeam@43 149 unsigned long
arg@461 150 getcolor(const char *colstr) {
arg@123 151 Colormap cmap = DefaultColormap(dpy, screen);
garbeam@2 152 XColor color;
garbeam@43 153
arg@495 154 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
arg@495 155 eprint("error, cannot allocate color '%s'\n", colstr);
garbeam@2 156 return color.pixel;
garbeam@2 157 }
garbeam@2 158
garbeam@2 159 void
arg@461 160 setfont(const char *fontstr) {
garbeam@2 161 char **missing, *def;
garbeam@33 162 int i, n;
garbeam@2 163
garbeam@7 164 missing = NULL;
garbeam@2 165 setlocale(LC_ALL, "");
garbeam@43 166 if(dc.font.set)
garbeam@43 167 XFreeFontSet(dpy, dc.font.set);
garbeam@43 168 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
garbeam@2 169 if(missing) {
garbeam@2 170 while(n--)
garbeam@2 171 fprintf(stderr, "missing fontset: %s\n", missing[n]);
garbeam@2 172 XFreeStringList(missing);
garbeam@43 173 if(dc.font.set) {
garbeam@43 174 XFreeFontSet(dpy, dc.font.set);
garbeam@43 175 dc.font.set = NULL;
garbeam@2 176 }
garbeam@2 177 }
garbeam@43 178 if(dc.font.set) {
garbeam@2 179 XFontSetExtents *font_extents;
garbeam@2 180 XFontStruct **xfonts;
garbeam@2 181 char **font_names;
garbeam@2 182
garbeam@43 183 dc.font.ascent = dc.font.descent = 0;
garbeam@43 184 font_extents = XExtentsOfFontSet(dc.font.set);
garbeam@43 185 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
garbeam@43 186 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
garbeam@43 187 if(dc.font.ascent < (*xfonts)->ascent)
garbeam@43 188 dc.font.ascent = (*xfonts)->ascent;
garbeam@43 189 if(dc.font.descent < (*xfonts)->descent)
garbeam@43 190 dc.font.descent = (*xfonts)->descent;
garbeam@2 191 xfonts++;
garbeam@2 192 }
garbeam@2 193 }
garbeam@2 194 else {
garbeam@43 195 if(dc.font.xfont)
garbeam@43 196 XFreeFont(dpy, dc.font.xfont);
garbeam@43 197 dc.font.xfont = NULL;
garbeam@43 198 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
garbeam@43 199 if (!dc.font.xfont)
garbeam@43 200 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
garbeam@43 201 if (!dc.font.xfont)
garbeam@75 202 eprint("error, cannot init 'fixed' font\n");
garbeam@43 203 dc.font.ascent = dc.font.xfont->ascent;
garbeam@43 204 dc.font.descent = dc.font.xfont->descent;
garbeam@2 205 }
garbeam@43 206 dc.font.height = dc.font.ascent + dc.font.descent;
garbeam@2 207 }
garbeam@76 208
garbeam@76 209 unsigned int
arg@461 210 textw(const char *text) {
garbeam@76 211 return textnw(text, strlen(text)) + dc.font.height;
garbeam@76 212 }