dwm-meillo

annotate draw.c @ 75:f08271b7cb20

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