From 45b6d294be1417aeadcac612b7190a35444f111c Mon Sep 17 00:00:00 2001 From: Martin Duquesnoy Date: Thu, 1 Sep 2011 19:07:54 +0200 Subject: [PATCH] Font per theme --- wmfs2/src/config.c | 4 +++ wmfs2/src/draw.h | 10 +++---- wmfs2/src/event.c | 2 +- wmfs2/src/infobar.c | 5 ++-- wmfs2/src/wmfs.c | 69 +++++++++++++++++++++++++++++---------------- wmfs2/src/wmfs.h | 14 +++++---- wmfs2/wmfsrc2 | 4 +++ 7 files changed, 70 insertions(+), 38 deletions(-) diff --git a/wmfs2/src/config.c b/wmfs2/src/config.c index 4331f88..62fcdb2 100644 --- a/wmfs2/src/config.c +++ b/wmfs2/src/config.c @@ -28,6 +28,8 @@ config_theme(void) if(!(n = fetch_section_count(ks))) ++n; + SLIST_INIT(&W->h.theme); + /* [theme]*/ for(i = 0; i < n; ++i) { @@ -35,6 +37,8 @@ config_theme(void) t->name = fetch_opt_first(ks[i], "default", "name").str; + wmfs_init_font(fetch_opt_first(ks[i], "fixed", "font").str, t); + /* bars */ t->bars.fg = color_atoh(fetch_opt_first(ks[i], "#CCCCCC", "bars_fg").str); t->bars.bg = color_atoh(fetch_opt_first(ks[i], "#222222", "bars_bg").str); diff --git a/wmfs2/src/draw.h b/wmfs2/src/draw.h index 720ef7e..06e7b32 100644 --- a/wmfs2/src/draw.h +++ b/wmfs2/src/draw.h @@ -12,22 +12,22 @@ #include "wmfs.h" -#define TEXTY(w) ((W->font.height - W->font.de) + ((w - W->font.height) >> 1)) +#define TEXTY(t, w) ((t->font.height - t->font.de) + ((w - t->font.height) >> 1)) #define PAD (8) static inline void -draw_text(Drawable d, int x, int y, Color fg, const char *str) +draw_text(Drawable d, Theme *t, int x, int y, Color fg, const char *str) { XSetForeground(W->dpy, W->gc, fg); - XmbDrawString(W->dpy, d, W->font.fontset, W->gc, x, y, str, strlen(str)); + XmbDrawString(W->dpy, d, t->font.fontset, W->gc, x, y, str, strlen(str)); } static inline unsigned short -draw_textw(const char *str) +draw_textw(Theme *t, const char *str) { XRectangle r; - XmbTextExtents(W->font.fontset, str, strlen(str), NULL, &r); + XmbTextExtents(t->font.fontset, str, strlen(str), NULL, &r); return r.width; } diff --git a/wmfs2/src/event.c b/wmfs2/src/event.c index 11139e9..1101298 100644 --- a/wmfs2/src/event.c +++ b/wmfs2/src/event.c @@ -228,7 +228,7 @@ event_expose(XEvent *e) static void event_dummy(XEvent *e) { - printf("%d\n", e->type); + /* printf("%d\n", e->type);*/ (void)e; } diff --git a/wmfs2/src/infobar.c b/wmfs2/src/infobar.c index 3b5ffc8..5f60a05 100644 --- a/wmfs2/src/infobar.c +++ b/wmfs2/src/infobar.c @@ -46,7 +46,7 @@ infobar_elem_tag_init(Element *e) TAILQ_FOREACH(t, &e->infobar->screen->tags, next) { - s = draw_textw(t->name) + PAD; + s = draw_textw(e->infobar->theme, t->name) + PAD; /* Init barwin */ b = barwin_new(e->infobar->bar->win, j, 0, s, e->geo.h, 0, 0, false); @@ -106,7 +106,8 @@ infobar_elem_tag_update(Element *e) barwin_refresh_color(b); - draw_text(b->dr, (PAD >> 1), TEXTY(e->geo.h), b->fg, t->name); + draw_text(b->dr, e->infobar->theme, (PAD >> 1), + TEXTY(e->infobar->theme, e->geo.h), b->fg, t->name); barwin_refresh(b); } diff --git a/wmfs2/src/wmfs.c b/wmfs2/src/wmfs.c index 8556dbb..488936d 100644 --- a/wmfs2/src/wmfs.c +++ b/wmfs2/src/wmfs.c @@ -57,13 +57,37 @@ wmfs_error_handler_dummy(Display *d, XErrorEvent *event) return 0; } +void +wmfs_init_font(char *font, Theme *t) +{ + XFontStruct **xfs = NULL; + char **misschar, **names, *defstring; + int d; + + if(!(t->font.fontset = XCreateFontSet(W->dpy, font, &misschar, &d, &defstring))) + { + warnx("Can't load font '%s'", font); + t->font.fontset = XCreateFontSet(W->dpy, "fixed", &misschar, &d, &defstring); + } + + XExtentsOfFontSet(t->font.fontset); + XFontsOfFontSet(t->font.fontset, &xfs, &names); + + t->font.as = xfs[0]->max_bounds.ascent; + t->font.de = xfs[0]->max_bounds.descent; + t->font.width = xfs[0]->max_bounds.width; + + t->font.height = t->font.as + t->font.de; + + if(misschar) + XFreeStringList(misschar); +} + static void wmfs_xinit(void) { - char **misschar, **names, *defstring; - int d, i, j; + int i, j; XModifierKeymap *mm; - XFontStruct **xfs = NULL; XSetWindowAttributes at; /* @@ -90,24 +114,10 @@ wmfs_xinit(void) XChangeWindowAttributes(W->dpy, W->root, CWEventMask | CWCursor, &at); /* - * Font + * Locale (font encode) */ setlocale(LC_CTYPE, ""); - W->font.fontset = XCreateFontSet(W->dpy, "fixed", &misschar, &d, &defstring); - - XExtentsOfFontSet(W->font.fontset); - XFontsOfFontSet(W->font.fontset, &xfs, &names); - - W->font.as = xfs[0]->max_bounds.ascent; - W->font.de = xfs[0]->max_bounds.descent; - W->font.width = xfs[0]->max_bounds.width; - - W->font.height = W->font.as + W->font.de; - - if(misschar) - XFreeStringList(misschar); - /* * Keys */ @@ -230,19 +240,30 @@ wmfs_init(void) void wmfs_quit(void) { - /* X stuffs */ - XFreeFontSet(W->dpy, W->font.fontset); + Theme *t; + /* Will free: + * + * Screens -> Tags + * -> Infobars -> Elements + */ screen_free(); XCloseDisplay(W->dpy); - free(W->net_atom); - free(W); - /* Conf stuffs */ FREE_LIST(Keybind, W->h.keybind); - FREE_LIST(Theme, W->h.theme); + + while(!SLIST_EMPTY(&W->h.theme)) + { + t = SLIST_FIRST(&W->h.theme); + SLIST_REMOVE_HEAD(&W->h.theme, next); + XFreeFontSet(W->dpy, t->font.fontset); + free(t); + } + + free(W->net_atom); + free(W); W->running = false; } diff --git a/wmfs2/src/wmfs.h b/wmfs2/src/wmfs.h index cd066d7..cbc75fd 100644 --- a/wmfs2/src/wmfs.h +++ b/wmfs2/src/wmfs.h @@ -151,6 +151,13 @@ struct Theme { char *name; + /* Font */ + struct + { + int as, de, width, height; + XFontSet fontset; + } font; + /* Bars */ struct Colpair bars; int bars_width; @@ -188,12 +195,6 @@ struct Wmfs Atom *net_atom; bool running; - struct - { - int as, de, width, height; - XFontSet fontset; - } font; - /* Lists heads */ struct { @@ -217,6 +218,7 @@ struct Wmfs int wmfs_error_handler(Display *d, XErrorEvent *event); int wmfs_error_handler_dummy(Display *d, XErrorEvent *event); void wmfs_grab_keys(void); +void wmfs_init_font(char *font, Theme *t); void wmfs_quit(void); void uicb_reload(Uicb cmd); void uicb_quit(Uicb cmd); diff --git a/wmfs2/wmfsrc2 b/wmfs2/wmfsrc2 index 397ce3e..131b491 100644 --- a/wmfs2/wmfsrc2 +++ b/wmfs2/wmfsrc2 @@ -7,6 +7,8 @@ [theme] # name = "default" + font = "fixed" + # Bars bars_width = 14 bars_fg = "#CCCCCC" @@ -25,6 +27,8 @@ [theme] name = "perso" + font = "courier" + bars_width = 20 bars_fg = "#222222" bars_bg = "#CCCCCC"