dwm-meillo

annotate draw.c @ 7:49e2fc9fb94f

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