diff options
author | Bert Münnich <ber.t@gmx.com> | 2012-02-12 19:00:41 +0100 |
---|---|---|
committer | Bert Münnich <ber.t@gmx.com> | 2012-02-12 19:00:41 +0100 |
commit | b8458271fba9929831dcc71d86c205fb52578e3a (patch) | |
tree | 47e7d0ad532ef3c0ddd8d60f79bf0b475e61696d | |
parent | 8fc7cb73b37cb00f164425bcced0afbc949a3429 (diff) |
Added text bar on bottom of window
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | config.def.h | 17 | ||||
-rw-r--r-- | image.c | 2 | ||||
-rw-r--r-- | main.c | 67 | ||||
-rw-r--r-- | thumbs.c | 1 | ||||
-rw-r--r-- | types.h | 1 | ||||
-rw-r--r-- | window.c | 178 | ||||
-rw-r--r-- | window.h | 7 |
8 files changed, 211 insertions, 64 deletions
@@ -1,4 +1,4 @@ -VERSION = git-20120211 +VERSION = git-20120212 CC = gcc CFLAGS = -ansi -Wall -pedantic -O2 diff --git a/config.def.h b/config.def.h index dd36224..f455111 100644 --- a/config.def.h +++ b/config.def.h @@ -6,11 +6,18 @@ enum { WIN_HEIGHT = 600 }; -/* default color for window background: */ -static const char * const BG_COLOR = "#777777"; -/* default color for thumbnail selection: */ -static const char * const SEL_COLOR = "#DDDDDD"; -/* (see X(7) section "COLOR NAMES" for valid values) */ +/* bar font: + * (see X(7) section "FONT NAMES" for valid values) + */ +static const char * const BAR_FONT = "6x13"; + +/* colors: + * (see X(7) section "COLOR NAMES" for valid values) + */ +static const char * const WIN_BG_COLOR = "#777777"; +static const char * const SEL_COLOR = "#DDDDDD"; +static const char * const BAR_BG_COLOR = "#222222"; +static const char * const BAR_FG_COLOR = "#EEEEEE"; #endif #ifdef _IMAGE_CONFIG @@ -414,8 +414,6 @@ void img_render(img_t *img) { imlib_context_set_drawable(win->pm); imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh); - win_draw(win); - img->dirty = false; } @@ -22,6 +22,7 @@ #include <stdlib.h> #include <stdio.h> #include <string.h> +#include <libgen.h> #include <unistd.h> #include <sys/stat.h> #include <sys/time.h> @@ -38,8 +39,8 @@ #include "config.h" enum { - TITLE_LEN = 256, - FNAME_CNT = 1024 + INFO_STR_LEN = 256, + FILENAME_CNT = 1024 }; typedef struct { @@ -64,7 +65,9 @@ size_t filesize; int prefix; -char win_title[TITLE_LEN]; +char win_bar_l[INFO_STR_LEN]; +char win_bar_r[INFO_STR_LEN]; +char win_title[INFO_STR_LEN]; timeout_t timeouts[] = { { { 0, 0 }, false, redraw }, @@ -105,6 +108,7 @@ void check_add_file(char *filename) { } files[fileidx].loaded = false; files[fileidx].name = s_strdup(filename); + files[fileidx].base = s_strdup(basename(filename)); if (*filename == '/') files[fileidx].path = files[fileidx].name; fileidx++; @@ -216,39 +220,43 @@ void load_image(int new) { reset_timeout(animate); } -void update_title(void) { - int n; +void update_info(void) { + unsigned int i, fw, pw; char frame_info[16]; - float size; const char *size_unit; + float size = filesize; + + pw = 0; + for (i = filecnt; i > 0; i /= 10) + pw++; if (mode == MODE_THUMB) { - n = snprintf(win_title, TITLE_LEN, "sxiv: %d/%d %s", - tns.cnt ? tns.sel + 1 : 0, tns.cnt, - tns.cnt ? files[tns.sel].name : ""); + snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d %s", + pw, tns.cnt > 0 ? tns.sel + 1 : 0, filecnt, + tns.cnt > 0 ? files[tns.sel].base : ""); + win_bar_r[0] = '\0'; + snprintf(win_title, sizeof win_title, "sxiv"); } else { - size = filesize; size_readable(&size, &size_unit); - - if (img.multi.cnt > 0) - snprintf(frame_info, sizeof(frame_info), " (%d/%d)", - img.multi.sel + 1, img.multi.cnt); - else + if (img.multi.cnt > 0) { + fw = 0; + for (i = img.multi.cnt; i > 0; i /= 10) + fw++; + snprintf(frame_info, sizeof frame_info, " %0*d/%d", + fw, img.multi.sel + 1, img.multi.cnt); + } else { frame_info[0] = '\0'; - - n = snprintf(win_title, TITLE_LEN, - "sxiv: %d/%d%s %dx%d %d%% %.2f%s %s", - fileidx + 1, filecnt, frame_info, img.w, img.h, - (int) (img.zoom * 100.0), size, size_unit, - files[fileidx].name); - } - - if (n >= TITLE_LEN) { - for (n = 0; n < 3; n++) - win_title[TITLE_LEN - n - 2] = '.'; + } + snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d %s", + pw, fileidx + 1, filecnt, files[fileidx].base); + snprintf(win_bar_r, sizeof win_bar_r, "%.2f%s %dx%d %3d%%%s", + size, size_unit, img.w, img.h, (int) (img.zoom * 100.0), + frame_info); + snprintf(win_title, sizeof win_title, "sxiv - %s", + files[fileidx].name); } - win_set_title(&win, win_title); + win_set_bar_info(&win, win_bar_l, win_bar_r); } void redraw(void) { @@ -256,7 +264,8 @@ void redraw(void) { img_render(&img); else tns_render(&tns); - update_title(); + update_info(); + win_draw(&win); reset_timeout(redraw); reset_cursor(); } @@ -466,7 +475,7 @@ int main(int argc, char **argv) { } if (options->recursive || options->from_stdin) - filecnt = FNAME_CNT; + filecnt = FILENAME_CNT; else filecnt = options->filecnt; @@ -385,7 +385,6 @@ void tns_highlight(tns_t *tns, int n, bool hl) { win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6, false, 2, col); } - win_draw(win); } bool tns_move_selection(tns_t *tns, direction_t dir) { @@ -34,6 +34,7 @@ typedef enum { typedef struct { const char *name; /* as given by user */ const char *path; /* always absolute */ + const char *base; bool loaded; } fileinfo_t; @@ -20,6 +20,7 @@ #define _WINDOW_CONFIG #include <string.h> +#include <locale.h> #include <X11/Xutil.h> #include <X11/cursorfont.h> @@ -28,6 +29,11 @@ #include "window.h" #include "config.h" +enum { + H_TEXT_PAD = 5, + V_TEXT_PAD = 1 +}; + static Cursor carrow; static Cursor cnone; static Cursor chand; @@ -36,9 +42,62 @@ static GC gc; Atom wm_delete_win; +struct { + int ascent; + int descent; + XFontStruct *xfont; + XFontSet set; +} font; + +void win_init_font(Display *dpy, const char *fontstr) { + int n; + char *def, **missing; + + font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); + if (missing) { + while (n--) + warn("missing fontset: %s", missing[n]); + XFreeStringList(missing); + } + if (font.set) { + XFontStruct **xfonts; + char **font_names; + + font.ascent = font.descent = 0; + 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(dpy, fontstr)) == NULL && + (font.xfont = XLoadQueryFont(dpy, "fixed")) == NULL) + { + die("could not load font: %s", fontstr); + } + font.ascent = font.xfont->ascent; + font.descent = font.xfont->descent; + } +} + +unsigned long win_alloc_color(win_t *win, const char *name) { + XColor col; + + if (win == NULL) + return 0UL; + if (XAllocNamedColor(win->env.dpy, + DefaultColormap(win->env.dpy, win->env.scr), + name, &col, &col) == 0) + { + die("could not allocate color: %s", name); + } + return col.pixel; +} + void win_init(win_t *win) { win_env_t *e; - XColor col; if (win == NULL) return; @@ -57,25 +116,21 @@ void win_init(win_t *win) { win->black = BlackPixel(e->dpy, e->scr); win->white = WhitePixel(e->dpy, e->scr); - if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR, - &col, &col) != 0) - { - win->bgcol = col.pixel; - } else { - die("could not allocate color: %s", BG_COLOR); - } - - if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), SEL_COLOR, - &col, &col) != 0) - { - win->selcol = col.pixel; - } else { - die("could not allocate color: %s", SEL_COLOR); - } + win->bgcol = win_alloc_color(win, WIN_BG_COLOR); + win->selcol = win_alloc_color(win, SEL_COLOR); + win->barbgcol = win_alloc_color(win, BAR_BG_COLOR); + win->barfgcol = win_alloc_color(win, BAR_FG_COLOR); win->xwin = 0; win->pm = 0; win->fullscreen = false; + win->lbar = NULL; + win->rbar = NULL; + + if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0) + warn("no locale support"); + + win_init_font(e->dpy, BAR_FONT); } void win_set_sizehints(win_t *win) { @@ -87,8 +142,8 @@ void win_set_sizehints(win_t *win) { sizehints.flags = PMinSize | PMaxSize; sizehints.min_width = win->w; sizehints.max_width = win->w; - sizehints.min_height = win->h; - sizehints.max_height = win->h; + sizehints.min_height = win->h + win->barh; + sizehints.max_height = win->h + win->barh; XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints); } @@ -157,6 +212,9 @@ void win_open(win_t *win) { classhint.res_class = "sxiv"; XSetClassHint(e->dpy, win->xwin, &classhint); + win->barh = font.ascent + font.descent + 2 * V_TEXT_PAD; + win->h -= win->barh; + if (options->fixed_win) win_set_sizehints(win); @@ -191,12 +249,12 @@ bool win_configure(win_t *win, XConfigureEvent *c) { if (win == NULL) return false; - changed = win->w != c->width || win->h != c->height; + changed = win->w != c->width || win->h + win->barh != c->height; win->x = c->x; win->y = c->y; win->w = c->width; - win->h = c->height; + win->h = c->height - win->barh; win->bw = c->border_width; return changed; @@ -211,18 +269,18 @@ bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) { w = MIN(w, win->env.scrw - 2 * win->bw); h = MIN(h, win->env.scrh - 2 * win->bw); - if (win->x == x && win->y == y && win->w == w && win->h == h) + if (win->x == x && win->y == y && win->w == w && win->h + win->barh == h) return false; win->x = x; win->y = y; win->w = w; - win->h = h; + win->h = h - win->barh; if (options->fixed_win) win_set_sizehints(win); - XMoveResizeWindow(win->env.dpy, win->xwin, win->x, win->y, win->w, win->h); + XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h); return true; } @@ -253,26 +311,87 @@ void win_toggle_fullscreen(win_t *win) { void win_clear(win_t *win) { win_env_t *e; - XGCValues gcval; if (win == NULL || win->xwin == None) return; e = &win->env; - gcval.foreground = win->fullscreen ? win->black : win->bgcol; if (win->pm != None) XFreePixmap(e->dpy, win->pm); win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth); - XChangeGC(e->dpy, gc, GCForeground, &gcval); + XSetForeground(e->dpy, gc, win->fullscreen ? win->black : win->bgcol); XFillRectangle(e->dpy, win->pm, gc, 0, 0, e->scrw, e->scrh); } +int win_textwidth(const char *text, unsigned int len) { + XRectangle r; + + if (font.set) { + XmbTextExtents(font.set, text, len, NULL, &r); + return r.width; + } else { + return XTextWidth(font.xfont, text, len); + } +} +void win_draw_bar(win_t *win) { + win_env_t *e; + int len, x, y, w, tw = 0; + const char *rt; + + if (win == NULL || win->xwin == None) + return; + + e = &win->env; + x = H_TEXT_PAD; + y = win->h + font.ascent + V_TEXT_PAD; + w = win->w - 2 * H_TEXT_PAD; + + XSetForeground(e->dpy, gc, win->barbgcol); + XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->barh); + + XSetForeground(e->dpy, gc, win->barfgcol); + XSetBackground(e->dpy, gc, win->barbgcol); + + if (win->lbar != NULL) { + len = strlen(win->lbar); + while (len > 0 && (tw = win_textwidth(win->lbar, len)) > w) + len--; + w -= tw + 2 * H_TEXT_PAD; + if (font.set) + XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->lbar, len); + else + XDrawString(e->dpy, win->pm, gc, x, y, win->lbar, len); + } + if (win->rbar != NULL) { + len = strlen(win->rbar); + rt = win->rbar; + while (len > 0 && (tw = win_textwidth(rt, len)) > w) { + rt = strstr(rt, " "); + if (rt != NULL) { + rt += 2; + len = strlen(rt); + } else { + len = 0; + } + } + if (len > 0) { + x = win->w - tw - H_TEXT_PAD; + if (font.set) + XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, rt, len); + else + XDrawString(e->dpy, win->pm, gc, x, y, rt, len); + } + } +} + void win_draw(win_t *win) { if (win == NULL || win->xwin == None) return; + win_draw_bar(win); + XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm); XClearWindow(win->env.dpy, win->xwin); } @@ -314,6 +433,13 @@ void win_set_title(win_t *win, const char *title) { PropModeReplace, (unsigned char *) title, strlen(title)); } +void win_set_bar_info(win_t *win, const char *li, const char *ri) { + if (win != NULL) { + win->lbar = li; + win->rbar = ri; + } +} + void win_set_cursor(win_t *win, cursor_t cursor) { if (win == NULL || win->xwin == None) return; @@ -40,15 +40,21 @@ typedef struct { unsigned long white; unsigned long bgcol; unsigned long selcol; + unsigned long barbgcol; + unsigned long barfgcol; Pixmap pm; int x; int y; unsigned int w; unsigned int h; + unsigned int barh; unsigned int bw; bool fullscreen; + + const char *lbar; + const char *rbar; } win_t; extern Atom wm_delete_win; @@ -68,6 +74,7 @@ void win_draw_rect(win_t*, Pixmap, int, int, int, int, bool, int, unsigned long); void win_set_title(win_t*, const char*); +void win_set_bar_info(win_t*, const char*, const char*); void win_set_cursor(win_t*, cursor_t); #endif /* WINDOW_H */ |