Display full name in info bar, if there is enough space

This commit is contained in:
Bert Münnich 2012-02-21 12:49:29 +01:00
parent 9c346c322a
commit ed2c9f7caa
3 changed files with 47 additions and 24 deletions

43
main.c
View file

@ -22,7 +22,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <libgen.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
@ -87,6 +86,8 @@ void cleanup(void) {
} }
void check_add_file(char *filename) { void check_add_file(char *filename) {
const char *bn;
if (filename == NULL || *filename == '\0') if (filename == NULL || *filename == '\0')
return; return;
@ -108,9 +109,12 @@ void check_add_file(char *filename) {
} }
files[fileidx].loaded = false; files[fileidx].loaded = false;
files[fileidx].name = s_strdup(filename); files[fileidx].name = s_strdup(filename);
files[fileidx].base = s_strdup(basename(filename));
if (*filename == '/') if (*filename == '/')
files[fileidx].path = files[fileidx].name; files[fileidx].path = files[fileidx].name;
if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
files[fileidx].base = ++bn;
else
files[fileidx].base = files[fileidx].name;
fileidx++; fileidx++;
} }
@ -221,7 +225,7 @@ void load_image(int new) {
} }
void update_info(void) { void update_info(void) {
unsigned int i, fw, pw; int i, fw, pw, fi, ln, rn;
char frame_info[16]; char frame_info[16];
const char *size_unit; const char *size_unit;
float size = filesize; float size = filesize;
@ -235,8 +239,13 @@ void update_info(void) {
snprintf(win_bar_l, sizeof win_bar_l, "Loading... %0*d/%d", snprintf(win_bar_l, sizeof win_bar_l, "Loading... %0*d/%d",
pw, tns.cnt, filecnt); pw, tns.cnt, filecnt);
} else { } else {
snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s%s", fi = snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s",
pw, tns.sel + 1, filecnt, BAR_SEPARATOR, files[tns.sel].base); pw, tns.sel + 1, filecnt, BAR_SEPARATOR);
ln = snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
files[tns.sel].name) + fi;
if (win_textwidth(win_bar_l, ln, true) > win.w)
snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
files[tns.sel].base);
} }
win_set_title(&win, "sxiv"); win_set_title(&win, "sxiv");
win_set_bar_info(&win, win_bar_l, NULL); win_set_bar_info(&win, win_bar_l, NULL);
@ -247,18 +256,28 @@ void update_info(void) {
for (i = img.multi.cnt; i > 0; i /= 10) for (i = img.multi.cnt; i > 0; i /= 10)
fw++; fw++;
snprintf(frame_info, sizeof frame_info, "%s%0*d/%d", snprintf(frame_info, sizeof frame_info, "%s%0*d/%d",
BAR_SEPARATOR, fw, img.multi.sel + 1, img.multi.cnt); BAR_SEPARATOR, fw, img.multi.sel+1, img.multi.cnt);
} else { } else {
frame_info[0] = '\0'; frame_info[0] = '\0';
} }
snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s%s", fi = snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s",
pw, fileidx + 1, filecnt, BAR_SEPARATOR, files[fileidx].base); pw, fileidx + 1, filecnt, BAR_SEPARATOR);
snprintf(win_bar_r, sizeof win_bar_r, "%.2f%s%s%dx%d%s%3d%%%s", ln = snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
size, size_unit, BAR_SEPARATOR, img.w, img.h, BAR_SEPARATOR, files[fileidx].name) + fi;
(int) (img.zoom * 100.0), frame_info); rn = snprintf(win_bar_r, sizeof win_bar_r, "%.2f%s%s%dx%d%s%3d%%%s",
size, size_unit, BAR_SEPARATOR, img.w, img.h, BAR_SEPARATOR,
(int) (img.zoom * 100.0), frame_info);
if (win_textwidth(win_bar_l, ln, true) +
win_textwidth(win_bar_r, rn, true) > win.w)
{
snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
files[fileidx].base);
}
win_set_bar_info(&win, win_bar_l, win_bar_r);
snprintf(win_title, sizeof win_title, "sxiv - %s", files[fileidx].name); snprintf(win_title, sizeof win_title, "sxiv - %s", files[fileidx].name);
win_set_title(&win, win_title); win_set_title(&win, win_title);
win_set_bar_info(&win, win_bar_l, win_bar_r);
} }
} }

View file

@ -338,16 +338,6 @@ void win_clear(win_t *win) {
XFillRectangle(e->dpy, win->pm, gc, 0, 0, e->scrw, e->scrh); 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) { void win_draw_bar(win_t *win) {
win_env_t *e; win_env_t *e;
int len, x, y, w, tw = 0, seplen; int len, x, y, w, tw = 0, seplen;
@ -369,7 +359,7 @@ void win_draw_bar(win_t *win) {
if (win->lbar != NULL) { if (win->lbar != NULL) {
len = strlen(win->lbar); len = strlen(win->lbar);
while (len > 0 && (tw = win_textwidth(win->lbar, len)) > w) while (len > 0 && (tw = win_textwidth(win->lbar, len, false)) > w)
len--; len--;
w -= tw + 2 * H_TEXT_PAD; w -= tw + 2 * H_TEXT_PAD;
if (font.set) if (font.set)
@ -381,7 +371,7 @@ void win_draw_bar(win_t *win) {
len = strlen(win->rbar); len = strlen(win->rbar);
seplen = strlen(BAR_SEPARATOR); seplen = strlen(BAR_SEPARATOR);
rt = win->rbar; rt = win->rbar;
while (len > 0 && (tw = win_textwidth(rt, len)) > w) { while (len > 0 && (tw = win_textwidth(rt, len, false)) > w) {
rt = strstr(rt, BAR_SEPARATOR); rt = strstr(rt, BAR_SEPARATOR);
if (rt != NULL) { if (rt != NULL) {
rt += seplen; rt += seplen;
@ -427,6 +417,18 @@ void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h); XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
} }
int win_textwidth(const char *text, unsigned int len, bool with_padding) {
XRectangle r;
int padding = with_padding ? 2 * H_TEXT_PAD : 0;
if (font.set) {
XmbTextExtents(font.set, text, len, NULL, &r);
return r.width + padding;
} else {
return XTextWidth(font.xfont, text, len) + padding;
}
}
void win_set_title(win_t *win, const char *title) { void win_set_title(win_t *win, const char *title) {
if (win == NULL || win->xwin == None) if (win == NULL || win->xwin == None)
return; return;

View file

@ -76,6 +76,8 @@ void win_draw(win_t*);
void win_draw_rect(win_t*, Pixmap, int, int, int, int, bool, int, void win_draw_rect(win_t*, Pixmap, int, int, int, int, bool, int,
unsigned long); unsigned long);
int win_textwidth(const char*, unsigned int, bool);
void win_set_title(win_t*, const char*); void win_set_title(win_t*, const char*);
void win_set_bar_info(win_t*, const char*, const char*); void win_set_bar_info(win_t*, const char*, const char*);
void win_set_cursor(win_t*, cursor_t); void win_set_cursor(win_t*, cursor_t);