dwm-meillo

annotate draw.c @ 235:60e73ebaab27

removed unnecessary border color
author Anselm R.Garbe <arg@10ksloc.org>
date Thu, 10 Aug 2006 10:27:08 +0200
parents c674a0baac63
children ebecb98a1c29
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@173 13 textnw(const char *text, unsigned int len)
garbeam@75 14 {
garbeam@77 15 XRectangle r;
arg@123 16
garbeam@77 17 if(dc.font.set) {
garbeam@77 18 XmbTextExtents(dc.font.set, text, len, NULL, &r);
garbeam@77 19 return r.width;
garbeam@77 20 }
garbeam@77 21 return XTextWidth(dc.font.xfont, text, len);
garbeam@75 22 }
garbeam@75 23
garbeam@77 24 static void
arg@218 25 drawtext(const char *text, Bool invert)
garbeam@2 26 {
garbeam@33 27 int x, y, w, h;
arg@123 28 static char buf[256];
garbeam@33 29 unsigned int len;
garbeam@2 30 XGCValues gcv;
garbeam@34 31 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
garbeam@2 32
garbeam@66 33 XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
garbeam@34 34 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
garbeam@2 35
garbeam@3 36 if(!text)
garbeam@2 37 return;
garbeam@2 38
arg@218 39 w = 0;
garbeam@3 40 len = strlen(text);
garbeam@2 41 if(len >= sizeof(buf))
garbeam@2 42 len = sizeof(buf) - 1;
garbeam@3 43 memcpy(buf, text, len);
garbeam@2 44 buf[len] = 0;
garbeam@2 45
garbeam@34 46 h = dc.font.ascent + dc.font.descent;
garbeam@34 47 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
garbeam@34 48 x = dc.x + (h / 2);
garbeam@2 49
garbeam@2 50 /* shorten text if necessary */
garbeam@43 51 while(len && (w = textnw(buf, len)) > dc.w - h)
garbeam@2 52 buf[--len] = 0;
garbeam@2 53
garbeam@34 54 if(w > dc.w)
garbeam@2 55 return; /* too long */
garbeam@2 56
garbeam@66 57 gcv.foreground = invert ? dc.bg : dc.fg;
garbeam@66 58 gcv.background = invert ? dc.fg : dc.bg;
garbeam@34 59 if(dc.font.set) {
garbeam@34 60 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
garbeam@34 61 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
garbeam@2 62 x, y, buf, len);
garbeam@2 63 }
garbeam@2 64 else {
garbeam@34 65 gcv.font = dc.font.xfont->fid;
garbeam@34 66 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
garbeam@34 67 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
garbeam@2 68 }
garbeam@2 69 }
garbeam@2 70
garbeam@84 71 /* extern */
garbeam@77 72
garbeam@77 73 void
garbeam@77 74 drawall()
garbeam@77 75 {
garbeam@77 76 Client *c;
garbeam@77 77
arg@142 78 for(c = clients; c; c = getnext(c->next))
garbeam@77 79 drawtitle(c);
garbeam@77 80 drawstatus();
garbeam@77 81 }
garbeam@77 82
garbeam@77 83 void
garbeam@77 84 drawstatus()
garbeam@77 85 {
arg@124 86 int i, x;
garbeam@77 87 Bool istile = arrange == dotile;
garbeam@77 88
garbeam@77 89 dc.x = dc.y = 0;
garbeam@77 90 dc.w = bw;
arg@218 91 drawtext(NULL, !istile);
garbeam@77 92
garbeam@77 93 dc.w = 0;
arg@178 94 for(i = 0; i < ntags; i++) {
garbeam@77 95 dc.x += dc.w;
garbeam@77 96 dc.w = textw(tags[i]);
garbeam@77 97 if(istile)
arg@218 98 drawtext(tags[i], (i == tsel));
garbeam@77 99 else
arg@218 100 drawtext(tags[i], (i != tsel));
garbeam@77 101 }
arg@124 102 x = dc.x + dc.w;
garbeam@77 103 dc.w = textw(stext);
garbeam@77 104 dc.x = bx + bw - dc.w;
arg@218 105 drawtext(stext, !istile);
arg@124 106 if(sel && ((dc.w = dc.x - x) >= bh)) {
arg@124 107 dc.x = x;
arg@218 108 drawtext(sel->name, istile);
arg@124 109 }
garbeam@77 110 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
garbeam@79 111 XSync(dpy, False);
garbeam@77 112 }
garbeam@77 113
garbeam@76 114 void
garbeam@76 115 drawtitle(Client *c)
garbeam@76 116 {
garbeam@76 117 int i;
garbeam@76 118 Bool istile = arrange == dotile;
garbeam@76 119
garbeam@76 120 if(c == sel) {
garbeam@76 121 drawstatus();
garbeam@76 122 XUnmapWindow(dpy, c->title);
garbeam@76 123 XSetWindowBorder(dpy, c->win, dc.fg);
garbeam@76 124 return;
garbeam@76 125 }
garbeam@76 126
garbeam@76 127 XSetWindowBorder(dpy, c->win, dc.bg);
garbeam@76 128 XMapWindow(dpy, c->title);
garbeam@76 129
garbeam@76 130 dc.x = dc.y = 0;
garbeam@76 131
garbeam@76 132 dc.w = 0;
arg@178 133 for(i = 0; i < ntags; i++) {
garbeam@76 134 if(c->tags[i]) {
garbeam@76 135 dc.x += dc.w;
arg@173 136 dc.w = textw(tags[i]);
arg@218 137 drawtext(tags[i], !istile);
garbeam@76 138 }
garbeam@76 139 }
garbeam@76 140 dc.x += dc.w;
garbeam@76 141 dc.w = textw(c->name);
arg@218 142 drawtext(c->name, !istile);
garbeam@79 143 XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0);
garbeam@79 144 XSync(dpy, False);
garbeam@76 145 }
garbeam@76 146
garbeam@43 147 unsigned long
garbeam@74 148 getcolor(const char *colstr)
garbeam@2 149 {
arg@123 150 Colormap cmap = DefaultColormap(dpy, screen);
garbeam@2 151 XColor color;
garbeam@43 152
garbeam@2 153 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
garbeam@2 154 return color.pixel;
garbeam@2 155 }
garbeam@2 156
garbeam@2 157 void
garbeam@74 158 setfont(const char *fontstr)
garbeam@2 159 {
garbeam@2 160 char **missing, *def;
garbeam@33 161 int i, n;
garbeam@2 162
garbeam@7 163 missing = NULL;
garbeam@2 164 setlocale(LC_ALL, "");
garbeam@43 165 if(dc.font.set)
garbeam@43 166 XFreeFontSet(dpy, dc.font.set);
garbeam@43 167 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
garbeam@2 168 if(missing) {
garbeam@2 169 while(n--)
garbeam@2 170 fprintf(stderr, "missing fontset: %s\n", missing[n]);
garbeam@2 171 XFreeStringList(missing);
garbeam@43 172 if(dc.font.set) {
garbeam@43 173 XFreeFontSet(dpy, dc.font.set);
garbeam@43 174 dc.font.set = NULL;
garbeam@2 175 }
garbeam@2 176 }
garbeam@43 177 if(dc.font.set) {
garbeam@2 178 XFontSetExtents *font_extents;
garbeam@2 179 XFontStruct **xfonts;
garbeam@2 180 char **font_names;
garbeam@2 181
garbeam@43 182 dc.font.ascent = dc.font.descent = 0;
garbeam@43 183 font_extents = XExtentsOfFontSet(dc.font.set);
garbeam@43 184 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
garbeam@43 185 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
garbeam@43 186 if(dc.font.ascent < (*xfonts)->ascent)
garbeam@43 187 dc.font.ascent = (*xfonts)->ascent;
garbeam@43 188 if(dc.font.descent < (*xfonts)->descent)
garbeam@43 189 dc.font.descent = (*xfonts)->descent;
garbeam@2 190 xfonts++;
garbeam@2 191 }
garbeam@2 192 }
garbeam@2 193 else {
garbeam@43 194 if(dc.font.xfont)
garbeam@43 195 XFreeFont(dpy, dc.font.xfont);
garbeam@43 196 dc.font.xfont = NULL;
garbeam@43 197 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
garbeam@43 198 if (!dc.font.xfont)
garbeam@43 199 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
garbeam@43 200 if (!dc.font.xfont)
garbeam@75 201 eprint("error, cannot init 'fixed' font\n");
garbeam@43 202 dc.font.ascent = dc.font.xfont->ascent;
garbeam@43 203 dc.font.descent = dc.font.xfont->descent;
garbeam@2 204 }
garbeam@43 205 dc.font.height = dc.font.ascent + dc.font.descent;
garbeam@2 206 }
garbeam@76 207
garbeam@76 208 unsigned int
arg@173 209 textw(const char *text)
garbeam@76 210 {
garbeam@76 211 return textnw(text, strlen(text)) + dc.font.height;
garbeam@76 212 }