Krolyxon
Commit ed2c9f7

Branches

Refs →

nsxived2c9f7

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

Author
Bert Münnich
Date
2012-02-21 12:49 +0100
Parent
9c346c3
Commit
ed2c9f7caa67dcd1c9b0331c5634d9ccdb3b9fa8

+47 −24

3 files changed

  1. M main.c +31 −12
  2. M window.c +14 −12
  3. M window.h +2 −0
M main.c View
@@ -22,7 +22,6 @@
2222 #include <stdlib.h>
2323 #include <stdio.h>
2424 #include <string.h>
25−#include <libgen.h>
2625 #include <unistd.h>
2726 #include <sys/stat.h>
2827 #include <sys/time.h>
@@ -87,6 +86,8 @@ void cleanup(void) {
8786 }
8887
8988 void check_add_file(char *filename) {
89+ const char *bn;
90+
9091 if (filename == NULL || *filename == '\0')
9192 return;
9293
@@ -108,9 +109,12 @@ void check_add_file(char *filename) {
108109 }
109110 files[fileidx].loaded = false;
110111 files[fileidx].name = s_strdup(filename);
111− files[fileidx].base = s_strdup(basename(filename));
112112 if (*filename == '/')
113113 files[fileidx].path = files[fileidx].name;
114+ if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
115+ files[fileidx].base = ++bn;
116+ else
117+ files[fileidx].base = files[fileidx].name;
114118 fileidx++;
115119 }
116120
@@ -221,7 +225,7 @@ void load_image(int new) {
221225 }
222226
223227 void update_info(void) {
224− unsigned int i, fw, pw;
228+ int i, fw, pw, fi, ln, rn;
225229 char frame_info[16];
226230 const char *size_unit;
227231 float size = filesize;
@@ -235,8 +239,13 @@ void update_info(void) {
235239 snprintf(win_bar_l, sizeof win_bar_l, "Loading... %0*d/%d",
236240 pw, tns.cnt, filecnt);
237241 } else {
238− snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s%s",
239− pw, tns.sel + 1, filecnt, BAR_SEPARATOR, files[tns.sel].base);
242+ fi = snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s",
243+ pw, tns.sel + 1, filecnt, BAR_SEPARATOR);
244+ ln = snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
245+ files[tns.sel].name) + fi;
246+ if (win_textwidth(win_bar_l, ln, true) > win.w)
247+ snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
248+ files[tns.sel].base);
240249 }
241250 win_set_title(&win, "sxiv");
242251 win_set_bar_info(&win, win_bar_l, NULL);
@@ -247,18 +256,28 @@ void update_info(void) {
247256 for (i = img.multi.cnt; i > 0; i /= 10)
248257 fw++;
249258 snprintf(frame_info, sizeof frame_info, "%s%0*d/%d",
250− BAR_SEPARATOR, fw, img.multi.sel + 1, img.multi.cnt);
259+ BAR_SEPARATOR, fw, img.multi.sel+1, img.multi.cnt);
251260 } else {
252261 frame_info[0] = '\0';
253262 }
254− snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s%s",
255− pw, fileidx + 1, filecnt, BAR_SEPARATOR, files[fileidx].base);
256− snprintf(win_bar_r, sizeof win_bar_r, "%.2f%s%s%dx%d%s%3d%%%s",
257− size, size_unit, BAR_SEPARATOR, img.w, img.h, BAR_SEPARATOR,
258− (int) (img.zoom * 100.0), frame_info);
263+ fi = snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s",
264+ pw, fileidx + 1, filecnt, BAR_SEPARATOR);
265+ ln = snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
266+ files[fileidx].name) + fi;
267+ rn = snprintf(win_bar_r, sizeof win_bar_r, "%.2f%s%s%dx%d%s%3d%%%s",
268+ size, size_unit, BAR_SEPARATOR, img.w, img.h, BAR_SEPARATOR,
269+ (int) (img.zoom * 100.0), frame_info);
270+
271+ if (win_textwidth(win_bar_l, ln, true) +
272+ win_textwidth(win_bar_r, rn, true) > win.w)
273+ {
274+ snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
275+ files[fileidx].base);
276+ }
277+ win_set_bar_info(&win, win_bar_l, win_bar_r);
278+
259279 snprintf(win_title, sizeof win_title, "sxiv - %s", files[fileidx].name);
260280 win_set_title(&win, win_title);
261− win_set_bar_info(&win, win_bar_l, win_bar_r);
262281 }
263282 }
264283
M window.c View
@@ -338,16 +338,6 @@ void win_clear(win_t *win) {
338338 XFillRectangle(e->dpy, win->pm, gc, 0, 0, e->scrw, e->scrh);
339339 }
340340
341−int win_textwidth(const char *text, unsigned int len) {
342− XRectangle r;
343−
344− if (font.set) {
345− XmbTextExtents(font.set, text, len, NULL, &r);
346− return r.width;
347− } else {
348− return XTextWidth(font.xfont, text, len);
349− }
350−}
351341 void win_draw_bar(win_t *win) {
352342 win_env_t *e;
353343 int len, x, y, w, tw = 0, seplen;
@@ -369,7 +359,7 @@ void win_draw_bar(win_t *win) {
369359
370360 if (win->lbar != NULL) {
371361 len = strlen(win->lbar);
372− while (len > 0 && (tw = win_textwidth(win->lbar, len)) > w)
362+ while (len > 0 && (tw = win_textwidth(win->lbar, len, false)) > w)
373363 len--;
374364 w -= tw + 2 * H_TEXT_PAD;
375365 if (font.set)
@@ -381,7 +371,7 @@ void win_draw_bar(win_t *win) {
381371 len = strlen(win->rbar);
382372 seplen = strlen(BAR_SEPARATOR);
383373 rt = win->rbar;
384− while (len > 0 && (tw = win_textwidth(rt, len)) > w) {
374+ while (len > 0 && (tw = win_textwidth(rt, len, false)) > w) {
385375 rt = strstr(rt, BAR_SEPARATOR);
386376 if (rt != NULL) {
387377 rt += seplen;
@@ -427,6 +417,18 @@ void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
427417 XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
428418 }
429419
420+int win_textwidth(const char *text, unsigned int len, bool with_padding) {
421+ XRectangle r;
422+ int padding = with_padding ? 2 * H_TEXT_PAD : 0;
423+
424+ if (font.set) {
425+ XmbTextExtents(font.set, text, len, NULL, &r);
426+ return r.width + padding;
427+ } else {
428+ return XTextWidth(font.xfont, text, len) + padding;
429+ }
430+}
431+
430432 void win_set_title(win_t *win, const char *title) {
431433 if (win == NULL || win->xwin == None)
432434 return;
M window.h View
@@ -76,6 +76,8 @@ void win_draw(win_t*);
7676 void win_draw_rect(win_t*, Pixmap, int, int, int, int, bool, int,
7777 unsigned long);
7878
79+int win_textwidth(const char*, unsigned int, bool);
80+
7981 void win_set_title(win_t*, const char*);
8082 void win_set_bar_info(win_t*, const char*, const char*);
8183 void win_set_cursor(win_t*, cursor_t);