aewl

annotate draw.c @ 33:e90449e03167

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