dwm-meillo

annotate draw.c @ 26:e8f627998d6f

simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
author Anselm R. Garbe <garbeam@wmii.de>
date Wed, 12 Jul 2006 15:17:22 +0200
parents 49e2fc9fb94f
children 2e0fb4130bfb
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@2 9 #include "draw.h"
garbeam@2 10 #include "util.h"
garbeam@2 11
garbeam@2 12 static void
garbeam@2 13 drawborder(Display *dpy, Brush *b)
garbeam@2 14 {
garbeam@2 15 XPoint points[5];
garbeam@2 16 XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter);
garbeam@3 17 XSetForeground(dpy, b->gc, b->border);
garbeam@26 18 points[0].x = b->x;
garbeam@26 19 points[0].y = b->y;
garbeam@26 20 points[1].x = b->w - 1;
garbeam@2 21 points[1].y = 0;
garbeam@2 22 points[2].x = 0;
garbeam@26 23 points[2].y = b->h - 1;
garbeam@26 24 points[3].x = -(b->w - 1);
garbeam@2 25 points[3].y = 0;
garbeam@2 26 points[4].x = 0;
garbeam@26 27 points[4].y = -(b->h - 1);
garbeam@2 28 XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious);
garbeam@2 29 }
garbeam@2 30
garbeam@2 31 void
garbeam@3 32 draw(Display *dpy, Brush *b, Bool border, const char *text)
garbeam@2 33 {
garbeam@2 34 unsigned int x, y, w, h, len;
garbeam@2 35 static char buf[256];
garbeam@2 36 XGCValues gcv;
garbeam@26 37 XRectangle r = { b->x, b->y, b->w, b->h };
garbeam@2 38
garbeam@3 39 XSetForeground(dpy, b->gc, b->bg);
garbeam@26 40 XFillRectangles(dpy, b->drawable, b->gc, &r, 1);
garbeam@2 41
garbeam@3 42 if(border)
garbeam@2 43 drawborder(dpy, b);
garbeam@2 44
garbeam@3 45 if(!text)
garbeam@2 46 return;
garbeam@2 47
garbeam@3 48 len = strlen(text);
garbeam@2 49 if(len >= sizeof(buf))
garbeam@2 50 len = sizeof(buf) - 1;
garbeam@3 51 memcpy(buf, text, len);
garbeam@2 52 buf[len] = 0;
garbeam@2 53
garbeam@3 54 h = b->font.ascent + b->font.descent;
garbeam@26 55 y = b->y + (b->h / 2) - (h / 2) + b->font.ascent;
garbeam@26 56 x = b->x + (h / 2);
garbeam@2 57
garbeam@2 58 /* shorten text if necessary */
garbeam@26 59 while(len && (w = textnw(&b->font, buf, len)) > b->w - h)
garbeam@2 60 buf[--len] = 0;
garbeam@2 61
garbeam@26 62 if(w > b->w)
garbeam@2 63 return; /* too long */
garbeam@2 64
garbeam@3 65 gcv.foreground = b->fg;
garbeam@3 66 gcv.background = b->bg;
garbeam@3 67 if(b->font.set) {
garbeam@2 68 XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv);
garbeam@3 69 XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc,
garbeam@2 70 x, y, buf, len);
garbeam@2 71 }
garbeam@2 72 else {
garbeam@3 73 gcv.font = b->font.xfont->fid;
garbeam@2 74 XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv);
garbeam@2 75 XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
garbeam@2 76 }
garbeam@2 77 }
garbeam@2 78
garbeam@2 79 static unsigned long
garbeam@3 80 xloadcolors(Display *dpy, Colormap cmap, const char *colstr)
garbeam@2 81 {
garbeam@2 82 XColor color;
garbeam@2 83 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
garbeam@2 84 return color.pixel;
garbeam@2 85 }
garbeam@2 86
garbeam@2 87 void
garbeam@3 88 loadcolors(Display *dpy, int screen, Brush *b,
garbeam@2 89 const char *bg, const char *fg, const char *border)
garbeam@2 90 {
garbeam@2 91 Colormap cmap = DefaultColormap(dpy, screen);
garbeam@3 92 b->bg = xloadcolors(dpy, cmap, bg);
garbeam@3 93 b->fg = xloadcolors(dpy, cmap, fg);
garbeam@3 94 b->border = xloadcolors(dpy, cmap, border);
garbeam@2 95 }
garbeam@2 96
garbeam@2 97 unsigned int
garbeam@26 98 textnw(Fnt *font, char *text, unsigned int len)
garbeam@2 99 {
garbeam@26 100 XRectangle r;
garbeam@2 101 if(font->set) {
garbeam@26 102 XmbTextExtents(font->set, text, len, NULL, &r);
garbeam@2 103 return r.width;
garbeam@2 104 }
garbeam@2 105 return XTextWidth(font->xfont, text, len);
garbeam@2 106 }
garbeam@2 107
garbeam@2 108 unsigned int
garbeam@26 109 textw(Fnt *font, char *text)
garbeam@2 110 {
garbeam@26 111 return textnw(font, text, strlen(text));
garbeam@26 112 }
garbeam@26 113
garbeam@26 114 unsigned int
garbeam@26 115 texth(Fnt *font)
garbeam@26 116 {
garbeam@26 117 return font->height + 4;
garbeam@2 118 }
garbeam@2 119
garbeam@2 120 void
garbeam@2 121 loadfont(Display *dpy, Fnt *font, const char *fontstr)
garbeam@2 122 {
garbeam@2 123 char **missing, *def;
garbeam@2 124 int n;
garbeam@2 125
garbeam@7 126 missing = NULL;
garbeam@2 127 def = "?";
garbeam@2 128 setlocale(LC_ALL, "");
garbeam@2 129 if(font->set)
garbeam@2 130 XFreeFontSet(dpy, font->set);
garbeam@2 131 font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
garbeam@2 132 if(missing) {
garbeam@2 133 while(n--)
garbeam@2 134 fprintf(stderr, "missing fontset: %s\n", missing[n]);
garbeam@2 135 XFreeStringList(missing);
garbeam@2 136 if(font->set) {
garbeam@2 137 XFreeFontSet(dpy, font->set);
garbeam@7 138 font->set = NULL;
garbeam@2 139 }
garbeam@2 140 }
garbeam@2 141 if(font->set) {
garbeam@2 142 XFontSetExtents *font_extents;
garbeam@2 143 XFontStruct **xfonts;
garbeam@2 144 char **font_names;
garbeam@2 145 unsigned int i;
garbeam@2 146
garbeam@2 147 font->ascent = font->descent = 0;
garbeam@2 148 font_extents = XExtentsOfFontSet(font->set);
garbeam@2 149 n = XFontsOfFontSet(font->set, &xfonts, &font_names);
garbeam@2 150 for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
garbeam@2 151 if(font->ascent < (*xfonts)->ascent)
garbeam@2 152 font->ascent = (*xfonts)->ascent;
garbeam@2 153 if(font->descent < (*xfonts)->descent)
garbeam@2 154 font->descent = (*xfonts)->descent;
garbeam@2 155 xfonts++;
garbeam@2 156 }
garbeam@2 157 }
garbeam@2 158 else {
garbeam@2 159 if(font->xfont)
garbeam@2 160 XFreeFont(dpy, font->xfont);
garbeam@7 161 font->xfont = NULL;
garbeam@2 162 font->xfont = XLoadQueryFont(dpy, fontstr);
garbeam@2 163 if (!font->xfont)
garbeam@2 164 font->xfont = XLoadQueryFont(dpy, "fixed");
garbeam@2 165 if (!font->xfont)
garbeam@2 166 error("error, cannot load 'fixed' font\n");
garbeam@2 167 font->ascent = font->xfont->ascent;
garbeam@2 168 font->descent = font->xfont->descent;
garbeam@2 169 }
garbeam@3 170 font->height = font->ascent + font->descent;
garbeam@2 171 }