aewl

annotate draw.c @ 54:a64602deac83

except improvements to the mouse handling this is already nearly feature complete
author Anselm R. Garbe <garbeam@wmii.de>
date Thu, 13 Jul 2006 21:49:55 +0200
parents cd30cce52b78
children f005d46462e8
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@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@43 62 while(len && (w = textnw(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@43 82 unsigned long
garbeam@43 83 initcolor(const char *colstr)
garbeam@2 84 {
garbeam@2 85 XColor color;
garbeam@43 86 Colormap cmap = DefaultColormap(dpy, screen);
garbeam@43 87
garbeam@2 88 XAllocNamedColor(dpy, cmap, colstr, &color, &color);
garbeam@2 89 return color.pixel;
garbeam@2 90 }
garbeam@2 91
garbeam@43 92 unsigned int
garbeam@43 93 textnw(char *text, unsigned int len)
garbeam@2 94 {
garbeam@43 95 XRectangle r;
garbeam@43 96 if(dc.font.set) {
garbeam@43 97 XmbTextExtents(dc.font.set, text, len, NULL, &r);
garbeam@43 98 return r.width;
garbeam@43 99 }
garbeam@43 100 return XTextWidth(dc.font.xfont, text, len);
garbeam@2 101 }
garbeam@2 102
garbeam@2 103 unsigned int
garbeam@43 104 textw(char *text)
garbeam@2 105 {
garbeam@43 106 return textnw(text, strlen(text));
garbeam@2 107 }
garbeam@2 108
garbeam@2 109 void
garbeam@43 110 initfont(const char *fontstr)
garbeam@2 111 {
garbeam@2 112 char **missing, *def;
garbeam@33 113 int i, n;
garbeam@2 114
garbeam@7 115 missing = NULL;
garbeam@2 116 setlocale(LC_ALL, "");
garbeam@43 117 if(dc.font.set)
garbeam@43 118 XFreeFontSet(dpy, dc.font.set);
garbeam@43 119 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
garbeam@2 120 if(missing) {
garbeam@2 121 while(n--)
garbeam@2 122 fprintf(stderr, "missing fontset: %s\n", missing[n]);
garbeam@2 123 XFreeStringList(missing);
garbeam@43 124 if(dc.font.set) {
garbeam@43 125 XFreeFontSet(dpy, dc.font.set);
garbeam@43 126 dc.font.set = NULL;
garbeam@2 127 }
garbeam@2 128 }
garbeam@43 129 if(dc.font.set) {
garbeam@2 130 XFontSetExtents *font_extents;
garbeam@2 131 XFontStruct **xfonts;
garbeam@2 132 char **font_names;
garbeam@2 133
garbeam@43 134 dc.font.ascent = dc.font.descent = 0;
garbeam@43 135 font_extents = XExtentsOfFontSet(dc.font.set);
garbeam@43 136 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
garbeam@43 137 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
garbeam@43 138 if(dc.font.ascent < (*xfonts)->ascent)
garbeam@43 139 dc.font.ascent = (*xfonts)->ascent;
garbeam@43 140 if(dc.font.descent < (*xfonts)->descent)
garbeam@43 141 dc.font.descent = (*xfonts)->descent;
garbeam@2 142 xfonts++;
garbeam@2 143 }
garbeam@2 144 }
garbeam@2 145 else {
garbeam@43 146 if(dc.font.xfont)
garbeam@43 147 XFreeFont(dpy, dc.font.xfont);
garbeam@43 148 dc.font.xfont = NULL;
garbeam@43 149 dc.font.xfont = XLoadQueryFont(dpy, fontstr);
garbeam@43 150 if (!dc.font.xfont)
garbeam@43 151 dc.font.xfont = XLoadQueryFont(dpy, "fixed");
garbeam@43 152 if (!dc.font.xfont)
garbeam@34 153 error("error, cannot init 'fixed' font\n");
garbeam@43 154 dc.font.ascent = dc.font.xfont->ascent;
garbeam@43 155 dc.font.descent = dc.font.xfont->descent;
garbeam@2 156 }
garbeam@43 157 dc.font.height = dc.font.ascent + dc.font.descent;
garbeam@2 158 }