diff options
author | gotmor <gotmor@f2baff5b-bf2c-0410-a398-912abdc3d8b2> | 2007-07-20 09:30:48 +0000 |
---|---|---|
committer | gotmor <gotmor@f2baff5b-bf2c-0410-a398-912abdc3d8b2> | 2007-07-20 09:30:48 +0000 |
commit | e433da722fde36fd522013704ffca886121e61a6 (patch) | |
tree | e4815d68a80c7d18eb570fc3921053921f48f1d5 | |
parent | 32039df0be3c299ee7f77b3e37956e3b331ebba4 (diff) | |
download | dzen-e433da722fde36fd522013704ffca886121e61a6.tar.gz dzen-e433da722fde36fd522013704ffca886121e61a6.zip |
added textwidth gadget
git-svn-id: http://dzen.googlecode.com/svn/trunk@139 f2baff5b-bf2c-0410-a398-912abdc3d8b2
-rw-r--r-- | gadgets/Makefile | 16 | ||||
-rw-r--r-- | gadgets/README.textwidth | 9 | ||||
-rw-r--r-- | gadgets/config.mk | 5 | ||||
-rw-r--r-- | gadgets/textwidth.c | 117 | ||||
-rw-r--r-- | main.c | 3 |
5 files changed, 144 insertions, 6 deletions
diff --git a/gadgets/Makefile b/gadgets/Makefile index db0cc74..b73e756 100644 --- a/gadgets/Makefile +++ b/gadgets/Makefile @@ -3,10 +3,10 @@ include config.mk -SRC = dbar.c +SRC = dbar.c textwidth.c OBJ = ${SRC:.c=.o} -all: options dbar +all: options dbar textwidth options: @echo dzen2 gadgets build options: @@ -23,21 +23,31 @@ ${OBJ}: config.mk dbar: ${OBJ} @echo LD $@ - @${LD} -o $@ ${OBJ} ${LDFLAGS} + @${LD} -o $@ dbar.o ${LDFLAGS} + @strip $@ + +textwidth: ${OBJ} + @echo LD $@ + @${LD} -o $@ textwidth.o ${LDFLAGS} -L${X11LIB} -lX11 @strip $@ clean: @echo cleaning @rm -f ${OBJ} dbar + @rm -f ${OBJ} textwidth install: all @echo installing executable file to ${DESTDIR}${PREFIX}/bin @mkdir -p ${DESTDIR}${PREFIX}/bin @cp -f dbar ${DESTDIR}${PREFIX}/bin @chmod 755 ${DESTDIR}${PREFIX}/bin/dbar + @mkdir -p ${DESTDIR}${PREFIX}/textwidth + @cp -f textwidth ${DESTDIR}${PREFIX}/bin + @chmod 755 ${DESTDIR}${PREFIX}/bin/textwidth uninstall: @echo removing executable file from ${DESTDIR}${PREFIX}/bin @rm -f ${DESTDIR}${PREFIX}/bin/dbar + @rm -f ${DESTDIR}${PREFIX}/bin/textwidth .PHONY: all options clean install uninstall diff --git a/gadgets/README.textwidth b/gadgets/README.textwidth new file mode 100644 index 0000000..1201eec --- /dev/null +++ b/gadgets/README.textwidth @@ -0,0 +1,9 @@ +=================================== +textwidth, (c) 2007 by Robert Manea +=================================== + +Simple utility to calculate the width in pixels of text with a given +font. + +Usage: textwidth <fontname> <text> + diff --git a/gadgets/config.mk b/gadgets/config.mk index 9dd7697..b6fc39f 100644 --- a/gadgets/config.mk +++ b/gadgets/config.mk @@ -4,9 +4,12 @@ PREFIX = /usr/local MANPREFIX = ${PREFIX}/share/man -INCS = -I. -I/usr/include +X11INC = /usr/X11R6/include +INCS = -I. -I/usr/include -I${X11INC} +X11LIB = /usr/X11R6/lib LIBS = -L/usr/lib + CFLAGS = -Os ${INCS} LDFLAGS = ${LIBS} diff --git a/gadgets/textwidth.c b/gadgets/textwidth.c new file mode 100644 index 0000000..e32cb1e --- /dev/null +++ b/gadgets/textwidth.c @@ -0,0 +1,117 @@ +/* + textwidth - calculate width in pixels of text with a given font + + Copyright (C) 2007 by Robert Manea <rob dot manea at gmail dot com> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + + +#include<stdio.h> +#include<stdlib.h> +#include<string.h> +#include<X11/Xlib.h> + +typedef struct _Fnt { + XFontStruct *xfont; + XFontSet set; + int ascent; + int descent; + int height; +} Fnt; + +Fnt font; +Display *dpy; + +static unsigned int +textw(const char *text, unsigned int len) { + XRectangle r; + + if(font.set) { + XmbTextExtents(font.set, text, len, NULL, &r); + return r.width; + } + return XTextWidth(font.xfont, text, len); +} + +void +setfont(const char *fontstr) { + char *def, **missing; + int i, n; + + missing = NULL; + if(font.set) + XFreeFontSet(dpy, font.set); + font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); + if(missing) + XFreeStringList(missing); + if(font.set) { + XFontSetExtents *font_extents; + XFontStruct **xfonts; + char **font_names; + font.ascent = font.descent = 0; + font_extents = XExtentsOfFontSet(font.set); + n = XFontsOfFontSet(font.set, &xfonts, &font_names); + for(i = 0, font.ascent = 0, font.descent = 0; i < n; i++) { + if(font.ascent < (*xfonts)->ascent) + font.ascent = (*xfonts)->ascent; + if(font.descent < (*xfonts)->descent) + font.descent = (*xfonts)->descent; + xfonts++; + } + } + else { + if(font.xfont) + XFreeFont(dpy, font.xfont); + font.xfont = NULL; + if(!(font.xfont = XLoadQueryFont(dpy, fontstr))) { + fprintf(stderr, "error, cannot load font: '%s'\n", fontstr); + exit(EXIT_FAILURE); + } + font.ascent = font.xfont->ascent; + font.descent = font.xfont->descent; + } + font.height = font.ascent + font.descent; +} + +int +main(int argc, char *argv[]) +{ + char *myfont, *text; + + if(argc < 3) { + fprintf(stderr, "usage: %s <font> <string>\n", argv[0]); + return EXIT_FAILURE; + } + + myfont = argv[1]; + text = argv[2]; + + dpy = XOpenDisplay(0); + if(!dpy) { + fprintf(stderr, "cannot open display\n"); + return EXIT_FAILURE; + } + + setfont(myfont); + printf("%d\n", textw(text, strlen(text))); + + return EXIT_SUCCESS; +} + @@ -461,8 +461,7 @@ handle_xev(void) { if(!dzen.slave_win.ishmenu && ev.xexpose.window == dzen.title_win.win) drawheader(NULL); - if(ev.xexpose.window == dzen.slave_win.win) { - /*x_draw_body();*/ + if(!dzen.tsupdate && ev.xexpose.window == dzen.slave_win.win) { for(i=0; i < dzen.slave_win.max_lines; i++) XCopyArea(dzen.dpy, dzen.slave_win.drawable[i], dzen.slave_win.line[i], dzen.gc, 0, 0, dzen.slave_win.width, dzen.line_height, 0, 0); |