dwm-meillo

annotate draw.c @ 36:d969b6807162

last change to dwm.html
author Anselm R. Garbe <garbeam@wmii.de>
date Thu, 13 Jul 2006 10:02:28 +0200
parents e90449e03167
children 989178822938
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@34 14 drawborder(void)
garbeam@2 15 {
garbeam@2 16 XPoint points[5];
garbeam@34 17 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
garbeam@34 18 XSetForeground(dpy, dc.gc, dc.border);
garbeam@34 19 points[0].x = dc.x;
garbeam@34 20 points[0].y = dc.y;
garbeam@34 21 points[1].x = dc.w - 1;
garbeam@2 22 points[1].y = 0;
garbeam@2 23 points[2].x = 0;
garbeam@34 24 points[2].y = dc.h - 1;
garbeam@34 25 points[3].x = -(dc.w - 1);
garbeam@2 26 points[3].y = 0;
garbeam@2 27 points[4].x = 0;
garbeam@34 28 points[4].y = -(dc.h - 1);
garbeam@34 29 XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
garbeam@2 30 }
garbeam@2 31
garbeam@2 32 void
garbeam@34 33 draw(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@34 39 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
garbeam@2 40
garbeam@34 41 XSetForeground(dpy, dc.gc, dc.bg);
garbeam@34 42 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
garbeam@2 43
garbeam@30 44 w = 0;
garbeam@3 45 if(border)
garbeam@34 46 drawborder();
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@34 57 h = dc.font.ascent + dc.font.descent;
garbeam@34 58 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
garbeam@34 59 x = dc.x + (h / 2);
garbeam@2 60
garbeam@2 61 /* shorten text if necessary */
garbeam@34 62 while(len && (w = textnw(&dc.font, buf, len)) > dc.w - h)
garbeam@2 63 buf[--len] = 0;
garbeam@2 64
garbeam@34 65 if(w > dc.w)
garbeam@2 66 return; /* too long */
garbeam@2 67
garbeam@34 68 gcv.foreground = dc.fg;
garbeam@34 69 gcv.background = dc.bg;
garbeam@34 70 if(dc.font.set) {
garbeam@34 71 XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
garbeam@34 72 XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
garbeam@2 73 x, y, buf, len);
garbeam@2 74 }
garbeam@2 75 else {
garbeam@34 76 gcv.font = dc.font.xfont->fid;
garbeam@34 77 XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
garbeam@34 78 XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
garbeam@2 79 }
garbeam@2 80 }
garbeam@2 81
garbeam@2 82 static unsigned long
garbeam@34 83 xinitcolors(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@34 91 initcolors(const char *bg, const char *fg, const char *border)
garbeam@2 92 {
garbeam@34 93 Colormap cmap = DefaultColormap(dpy, screen);
garbeam@34 94 dc.bg = xinitcolors(cmap, bg);
garbeam@34 95 dc.fg = xinitcolors(cmap, fg);
garbeam@34 96 dc.border = xinitcolors(cmap, border);
garbeam@2 97 }
garbeam@2 98
garbeam@2 99 unsigned int
garbeam@26 100 textnw(Fnt *font, char *text, unsigned int len)
garbeam@2 101 {
garbeam@26 102 XRectangle r;
garbeam@2 103 if(font->set) {
garbeam@26 104 XmbTextExtents(font->set, text, len, NULL, &r);
garbeam@2 105 return r.width;
garbeam@2 106 }
garbeam@2 107 return XTextWidth(font->xfont, text, len);
garbeam@2 108 }
garbeam@2 109
garbeam@2 110 unsigned int
garbeam@26 111 textw(Fnt *font, char *text)
garbeam@2 112 {
garbeam@26 113 return textnw(font, text, strlen(text));
garbeam@26 114 }
garbeam@26 115
garbeam@26 116 unsigned int
garbeam@26 117 texth(Fnt *font)
garbeam@26 118 {
garbeam@26 119 return font->height + 4;
garbeam@2 120 }
garbeam@2 121
garbeam@2 122 void
garbeam@34 123 initfont(Fnt *font, const char *fontstr)
garbeam@2 124 {
garbeam@2 125 char **missing, *def;
garbeam@33 126 int i, n;
garbeam@2 127
garbeam@7 128 missing = NULL;
garbeam@2 129 setlocale(LC_ALL, "");
garbeam@2 130 if(font->set)
garbeam@2 131 XFreeFontSet(dpy, font->set);
garbeam@2 132 font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
garbeam@2 133 if(missing) {
garbeam@2 134 while(n--)
garbeam@2 135 fprintf(stderr, "missing fontset: %s\n", missing[n]);
garbeam@2 136 XFreeStringList(missing);
garbeam@2 137 if(font->set) {
garbeam@2 138 XFreeFontSet(dpy, font->set);
garbeam@7 139 font->set = NULL;
garbeam@2 140 }
garbeam@2 141 }
garbeam@2 142 if(font->set) {
garbeam@2 143 XFontSetExtents *font_extents;
garbeam@2 144 XFontStruct **xfonts;
garbeam@2 145 char **font_names;
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@34 166 error("error, cannot init '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 }