dwm-meillo

annotate draw.c @ 73:c2ddb9dbbd10

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