From f2544a331845ad80655c1f79c9dc61922fe3441c Mon Sep 17 00:00:00 2001 From: Anselm R Garbe Date: Wed, 17 Apr 2013 21:21:47 +0200 Subject: renamed draw into drw --- Makefile | 2 +- draw.c | 202 --------------------------------------------------------------- draw.h | 64 -------------------- drw.c | 202 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ drw.h | 61 +++++++++++++++++++ dwm.c | 2 +- 6 files changed, 265 insertions(+), 268 deletions(-) delete mode 100644 draw.c delete mode 100644 draw.h create mode 100644 drw.c create mode 100644 drw.h diff --git a/Makefile b/Makefile index b04b79c3..fd07d594 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ include config.mk -SRC = util.c draw.c dwm.c +SRC = drw.c dwm.c util.c OBJ = ${SRC:.c=.o} all: options dwm diff --git a/draw.c b/draw.c deleted file mode 100644 index fa340ffb..00000000 --- a/draw.c +++ /dev/null @@ -1,202 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include -#include -#include -#include - -#include "draw.h" -#include "util.h" - -Draw * -draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) { - Draw *draw = (Draw *)calloc(1, sizeof(Draw)); - draw->dpy = dpy; - draw->screen = screen; - draw->win = win; - draw->w = w; - draw->h = h; - draw->drawable = XCreatePixmap(dpy, win, w, h, DefaultDepth(dpy, screen)); - draw->gc = XCreateGC(dpy, win, 0, NULL); - XSetLineAttributes(dpy, draw->gc, 1, LineSolid, CapButt, JoinMiter); - return draw; -} - -void -draw_resize(Draw *draw, unsigned int w, unsigned int h) { - if(!draw) - return; - draw->w = w; - draw->h = h; - XFreePixmap(draw->dpy, draw->drawable); - draw->drawable = XCreatePixmap(draw->dpy, draw->win, w, h, DefaultDepth(draw->dpy, draw->screen)); -} - -void -draw_free(Draw *draw) { - XFreePixmap(draw->dpy, draw->drawable); - XFreeGC(draw->dpy, draw->gc); - free(draw); -} - -Fnt * -draw_font_create(Draw *draw, const char *fontname) { - Fnt *font; - char *def, **missing; - int n; - - if(!draw) - return NULL; - font = (Fnt *)calloc(1, sizeof(Fnt)); - font->set = XCreateFontSet(draw->dpy, fontname, &missing, &n, &def); - if(missing) { - while(n--) - fprintf(stderr, "draw: missing fontset: %s\n", missing[n]); - XFreeStringList(missing); - } - if(font->set) { - XFontStruct **xfonts; - char **font_names; - XExtentsOfFontSet(font->set); - n = XFontsOfFontSet(font->set, &xfonts, &font_names); - while(n--) { - font->ascent = MAX(font->ascent, (*xfonts)->ascent); - font->descent = MAX(font->descent,(*xfonts)->descent); - xfonts++; - } - } - else { - if(!(font->xfont = XLoadQueryFont(draw->dpy, fontname)) - && !(font->xfont = XLoadQueryFont(draw->dpy, "fixed"))) - die("error, cannot load font: '%s'\n", fontname); - font->ascent = font->xfont->ascent; - font->descent = font->xfont->descent; - } - font->h = font->ascent + font->descent; - return font; -} - -void -draw_font_free(Draw *draw, Fnt *font) { - if(!draw || !font) - return; - if(font->set) - XFreeFontSet(draw->dpy, font->set); - else - XFreeFont(draw->dpy, font->xfont); - free(font); -} - -Col * -draw_col_create(Draw *draw, const char *colname) { - Col *col = (Col *)calloc(1, sizeof(Col)); - Colormap cmap = DefaultColormap(draw->dpy, draw->screen); - XColor color; - - if(!XAllocNamedColor(draw->dpy, cmap, colname, &color, &color)) - die("error, cannot allocate color '%s'\n", colname); - col->rgb = color.pixel; - return col; -} - -void -draw_col_free(Draw *draw, Col *col) { - if(!col) - return; - free(col); -} - -void -draw_setfont(Draw *draw, Fnt *font) { - if(!draw) - return; - draw->font = font; -} - -void -draw_setfg(Draw *draw, Col *col) { - if(!draw) - return; - draw->fg = col; -} - -void -draw_setbg(Draw *draw, Col *col) { - if(!draw) - return; - draw->bg = col; -} - -void -draw_rect(Draw *draw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert) { - int dx; - - if(!draw || !draw->font || !draw->fg || !draw->bg) - return; - XSetForeground(draw->dpy, draw->gc, invert ? draw->bg->rgb : draw->fg->rgb); - dx = (draw->font->ascent + draw->font->descent + 2) / 4; - if(filled) - XFillRectangle(draw->dpy, draw->drawable, draw->gc, x+1, y+1, dx+1, dx+1); - else if(empty) - XDrawRectangle(draw->dpy, draw->drawable, draw->gc, x+1, y+1, dx, dx); -} - -void -draw_text(Draw *draw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert) { - char buf[256]; - int i, tx, ty, len, olen; - TextExtents tex; - - if(!draw || !draw->fg || !draw->bg) - return; - XSetForeground(draw->dpy, draw->gc, invert ? draw->fg->rgb : draw->bg->rgb); - XFillRectangle(draw->dpy, draw->drawable, draw->gc, x, y, w, h); - if(!text || !draw->font) - return; - olen = strlen(text); - draw_getextents(draw, text, olen, &tex); - ty = y + (h / 2) - tex.yOff; - tx = x + tex.xOff; - /* shorten text if necessary */ - for(len = MIN(olen, sizeof buf); len && tex.w > w - tex.h; len--) - draw_getextents(draw, text, len, &tex); - if(!len) - return; - memcpy(buf, text, len); - if(len < olen) - for(i = len; i && i > len - 3; buf[--i] = '.'); - XSetForeground(draw->dpy, draw->gc, invert ? draw->bg->rgb : draw->fg->rgb); - if(draw->font->set) - XmbDrawString(draw->dpy, draw->drawable, draw->font->set, draw->gc, tx, ty, buf, len); - else - XDrawString(draw->dpy, draw->drawable, draw->gc, tx, ty, buf, len); -} - -void -draw_map(Draw *draw, int x, int y, unsigned int w, unsigned int h) { - if(!draw) - return; - XCopyArea(draw->dpy, draw->drawable, draw->win, draw->gc, x, y, w, h, x, y); - XSync(draw->dpy, False); -} - - -void -draw_getextents(Draw *draw, const char *text, unsigned int len, TextExtents *extents) { - XRectangle r; - - if(!draw || !draw->font || !text) - return; - if(draw->font->set) { - XmbTextExtents(draw->font->set, text, len, NULL, &r); - extents->xOff = r.x; - extents->yOff = r.y; - extents->w = r.width; - extents->h = r.height; - } - else { - extents->h = draw->font->ascent + draw->font->descent; - extents->w = XTextWidth(draw->font->xfont, text, len); - extents->xOff = extents->h / 2; - extents->yOff = (extents->h / 2) + draw->font->ascent; - } -} diff --git a/draw.h b/draw.h deleted file mode 100644 index 17d2c89e..00000000 --- a/draw.h +++ /dev/null @@ -1,64 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -struct _XCol { - unsigned long rgb; -}; -typedef struct _XCol Col; - -struct _XFont { - int ascent; - int descent; - unsigned int h; - XFontSet set; - XFontStruct *xfont; -}; -typedef struct _XFont Fnt; - -typedef struct _XDraw Draw; -struct _XDraw { - unsigned int w, h; - Display *dpy; - int screen; - Window win; - Drawable drawable; - GC gc; - Col *fg; - Col *bg; - Fnt *font; -}; - -typedef struct { - unsigned int w; - unsigned int h; - int xOff; - int yOff; -} TextExtents; - -/* Drawable abstraction */ -Draw *draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h); -void draw_resize(Draw *draw, unsigned int w, unsigned int h); -void draw_free(Draw *draw); - -/* Fnt abstraction */ -Fnt *draw_font_create(Draw *draw, const char *fontname); -void draw_font_free(Draw *draw, Fnt *font); - -/* Colour abstraction */ -Col *draw_col_create(Draw *draw, const char *colname); -void draw_col_free(Draw *draw, Col *col); - -/* Drawing context manipulation */ -void draw_setfont(Draw *draw, Fnt *font); -void draw_setfg(Draw *draw, Col *col); -void draw_setbg(Draw *draw, Col *col); - -/* Drawing functions */ -void draw_rect(Draw *draw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert); -void draw_text(Draw *draw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert); - -/* Map functions */ -void draw_map(Draw *draw, int x, int y, unsigned int w, unsigned int h); - -/* Text functions */ -void draw_getextents(Draw *draw, const char *text, unsigned int len, TextExtents *extents); - diff --git a/drw.c b/drw.c new file mode 100644 index 00000000..65fd870d --- /dev/null +++ b/drw.c @@ -0,0 +1,202 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include +#include + +#include "drw.h" +#include "util.h" + +Drw * +drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) { + Drw *drw = (Drw *)calloc(1, sizeof(Drw)); + drw->dpy = dpy; + drw->screen = screen; + drw->win = win; + drw->w = w; + drw->h = h; + drw->drwable = XCreatePixmap(dpy, win, w, h, DefaultDepth(dpy, screen)); + drw->gc = XCreateGC(dpy, win, 0, NULL); + XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter); + return drw; +} + +void +drw_resize(Drw *drw, unsigned int w, unsigned int h) { + if(!drw) + return; + drw->w = w; + drw->h = h; + XFreePixmap(drw->dpy, drw->drwable); + drw->drwable = XCreatePixmap(drw->dpy, drw->win, w, h, DefaultDepth(drw->dpy, drw->screen)); +} + +void +drw_free(Drw *drw) { + XFreePixmap(drw->dpy, drw->drwable); + XFreeGC(drw->dpy, drw->gc); + free(drw); +} + +Fnt * +drw_font_create(Drw *drw, const char *fontname) { + Fnt *font; + char *def, **missing; + int n; + + if(!drw) + return NULL; + font = (Fnt *)calloc(1, sizeof(Fnt)); + font->set = XCreateFontSet(drw->dpy, fontname, &missing, &n, &def); + if(missing) { + while(n--) + fprintf(stderr, "drw: missing fontset: %s\n", missing[n]); + XFreeStringList(missing); + } + if(font->set) { + XFontStruct **xfonts; + char **font_names; + XExtentsOfFontSet(font->set); + n = XFontsOfFontSet(font->set, &xfonts, &font_names); + while(n--) { + font->ascent = MAX(font->ascent, (*xfonts)->ascent); + font->descent = MAX(font->descent,(*xfonts)->descent); + xfonts++; + } + } + else { + if(!(font->xfont = XLoadQueryFont(drw->dpy, fontname)) + && !(font->xfont = XLoadQueryFont(drw->dpy, "fixed"))) + die("error, cannot load font: '%s'\n", fontname); + font->ascent = font->xfont->ascent; + font->descent = font->xfont->descent; + } + font->h = font->ascent + font->descent; + return font; +} + +void +drw_font_free(Drw *drw, Fnt *font) { + if(!drw || !font) + return; + if(font->set) + XFreeFontSet(drw->dpy, font->set); + else + XFreeFont(drw->dpy, font->xfont); + free(font); +} + +Clr * +drw_clr_create(Drw *drw, const char *clrname) { + Clr *clr = (Clr *)calloc(1, sizeof(Clr)); + Colormap cmap = DefaultColormap(drw->dpy, drw->screen); + XColor color; + + if(!XAllocNamedColor(drw->dpy, cmap, clrname, &color, &color)) + die("error, cannot allocate color '%s'\n", clrname); + clr->rgb = color.pixel; + return clr; +} + +void +drw_clr_free(Drw *drw, Clr *clr) { + if(!clr) + return; + free(clr); +} + +void +drw_setfont(Drw *drw, Fnt *font) { + if(!drw) + return; + drw->font = font; +} + +void +drw_setfg(Drw *drw, Clr *clr) { + if(!drw) + return; + drw->fg = clr; +} + +void +drw_setbg(Drw *drw, Clr *clr) { + if(!drw) + return; + drw->bg = clr; +} + +void +drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert) { + int dx; + + if(!drw || !drw->font || !drw->fg || !drw->bg) + return; + XSetForeground(drw->dpy, drw->gc, invert ? drw->bg->rgb : drw->fg->rgb); + dx = (drw->font->ascent + drw->font->descent + 2) / 4; + if(filled) + XFillRectangle(drw->dpy, drw->drwable, drw->gc, x+1, y+1, dx+1, dx+1); + else if(empty) + XDrawRectangle(drw->dpy, drw->drwable, drw->gc, x+1, y+1, dx, dx); +} + +void +drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert) { + char buf[256]; + int i, tx, ty, len, olen; + Extnts tex; + + if(!drw || !drw->fg || !drw->bg) + return; + XSetForeground(drw->dpy, drw->gc, invert ? drw->fg->rgb : drw->bg->rgb); + XFillRectangle(drw->dpy, drw->drwable, drw->gc, x, y, w, h); + if(!text || !drw->font) + return; + olen = strlen(text); + drw_getexts(drw, text, olen, &tex); + ty = y + (h / 2) - tex.yOff; + tx = x + tex.xOff; + /* shorten text if necessary */ + for(len = MIN(olen, sizeof buf); len && tex.w > w - tex.h; len--) + drw_getexts(drw, text, len, &tex); + if(!len) + return; + memcpy(buf, text, len); + if(len < olen) + for(i = len; i && i > len - 3; buf[--i] = '.'); + XSetForeground(drw->dpy, drw->gc, invert ? drw->bg->rgb : drw->fg->rgb); + if(drw->font->set) + XmbDrawString(drw->dpy, drw->drwable, drw->font->set, drw->gc, tx, ty, buf, len); + else + XDrawString(drw->dpy, drw->drwable, drw->gc, tx, ty, buf, len); +} + +void +drw_map(Drw *drw, int x, int y, unsigned int w, unsigned int h) { + if(!drw) + return; + XCopyArea(drw->dpy, drw->drwable, drw->win, drw->gc, x, y, w, h, x, y); + XSync(drw->dpy, False); +} + + +void +drw_getexts(Drw *drw, const char *text, unsigned int len, Extnts *tex) { + XRectangle r; + + if(!drw || !drw->font || !text) + return; + if(drw->font->set) { + XmbTextExtents(drw->font->set, text, len, NULL, &r); + tex->xOff = r.x; + tex->yOff = r.y; + tex->w = r.width; + tex->h = r.height; + } + else { + tex->h = drw->font->ascent + drw->font->descent; + tex->w = XTextWidth(drw->font->xfont, text, len); + tex->xOff = tex->h / 2; + tex->yOff = (tex->h / 2) + drw->font->ascent; + } +} diff --git a/drw.h b/drw.h new file mode 100644 index 00000000..819da85d --- /dev/null +++ b/drw.h @@ -0,0 +1,61 @@ +/* See LICENSE file for copyright and license details. */ + +typedef struct { + unsigned long rgb; +} Clr; + +typedef struct { + int ascent; + int descent; + unsigned int h; + XFontSet set; + XFontStruct *xfont; +} Fnt; + +typedef struct { + unsigned int w, h; + Display *dpy; + int screen; + Window win; + Drawable drwable; + GC gc; + Clr *fg; + Clr *bg; + Fnt *font; +} Drw; + +typedef struct { + unsigned int w; + unsigned int h; + int xOff; + int yOff; +} Extnts; + +/* Drawable abstraction */ +Drw *drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h); +void drw_resize(Drw *drw, unsigned int w, unsigned int h); +void drw_free(Drw *drw); + +/* Fnt abstraction */ +Fnt *drw_font_create(Drw *drw, const char *fontname); +void drw_font_free(Drw *drw, Fnt *font); + +/* Clrour abstraction */ +Clr *drw_clr_create(Drw *drw, const char *clrname); +void drw_clr_free(Drw *drw, Clr *clr); + +/* Drawing context manipulation */ +void drw_setfont(Drw *drw, Fnt *font); +void drw_setfg(Drw *drw, Clr *clr); +void drw_setbg(Drw *drw, Clr *clr); + +/* Drawing functions */ +void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert); +void drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert); + +/* Map functions */ +void drw_map(Drw *drw, int x, int y, unsigned int w, unsigned int h); + +/* Text functions */ +void drw_getexts(Drw *drw, const char *text, unsigned int len, Extnts *extnts); + diff --git a/dwm.c b/dwm.c index d9443dae..7941bd14 100644 --- a/dwm.c +++ b/dwm.c @@ -40,7 +40,7 @@ #include #endif /* XINERAMA */ -#include "draw.h" +#include "drw.h" #include "util.h" /* macros */ -- cgit v1.2.3