Krolyxon
Commit 0801842

Branches

Refs →

nsxiv0801842

Put some useful information in the window title

Author
Bert
Date
2011-01-20 17:00 +0100
Parent
eeb5888
Commit
08018427c61435de9cd8dca1d1cc9bb69ca2fd74

+37 −2

3 files changed

  1. M main.c +23 −0
  2. M window.c +12 −2
  3. M window.h +2 −0
M main.c View
@@ -31,6 +31,8 @@ void on_keypress(XEvent*);
3131 void on_configurenotify(XEvent*);
3232 void on_expose(XEvent*);
3333
34+void update_title();
35+
3436 static void (*handler[LASTEvent])(XEvent*) = {
3537 [Expose] = on_expose,
3638 [ConfigureNotify] = on_configurenotify,
@@ -44,6 +46,9 @@ const char **filenames;
4446 unsigned int filecnt;
4547 unsigned int fileidx;
4648
49+#define TITLE_LEN 256
50+char win_title[TITLE_LEN];
51+
4752 void run() {
4853 XEvent ev;
4954
@@ -90,6 +95,7 @@ int main(int argc, char **argv) {
9095
9196 img_load(&img, filenames[fileidx]);
9297 img_display(&img, &win);
98+ update_title();
9399
94100 run();
95101
@@ -127,6 +133,7 @@ void on_keypress(XEvent *ev) {
127133 if (fileidx + 1 < filecnt) {
128134 img_load(&img, filenames[++fileidx]);
129135 img_display(&img, &win);
136+ update_title();
130137 }
131138 break;
132139 case XK_p:
@@ -134,6 +141,7 @@ void on_keypress(XEvent *ev) {
134141 if (fileidx > 0) {
135142 img_load(&img, filenames[--fileidx]);
136143 img_display(&img, &win);
144+ update_title();
137145 }
138146 break;
139147 }
@@ -153,3 +161,18 @@ void on_expose(XEvent *ev) {
153161 img_render(&img, &win, ev->xexpose.x, ev->xexpose.y,
154162 ev->xexpose.width, ev->xexpose.height);
155163 }
164+
165+void update_title() {
166+ int n;
167+
168+ n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1,
169+ filecnt, (int) (img.zoom * 100.0), filenames[fileidx]);
170+
171+ if (n >= TITLE_LEN) {
172+ win_title[TITLE_LEN - 2] = '.';
173+ win_title[TITLE_LEN - 3] = '.';
174+ win_title[TITLE_LEN - 4] = '.';
175+ }
176+
177+ win_set_title(&win, win_title);
178+}
M window.c View
@@ -72,8 +72,7 @@ void win_open(win_t *win) {
7272 XSelectInput(e->dpy, win->xwin,
7373 StructureNotifyMask | ExposureMask | KeyPressMask);
7474
75− XStoreName(e->dpy, win->xwin, "sxiv");
76− XSetIconName(e->dpy, win->xwin, "Sxiv");
75+ win_set_title(win, "sxiv");
7776
7877 if ((classhint = XAllocClassHint())) {
7978 classhint->res_name = "sxiv";
@@ -94,6 +93,17 @@ void win_close(win_t *win) {
9493 XCloseDisplay(win->env.dpy);
9594 }
9695
96+void win_set_title(win_t *win, const char *title) {
97+ if (!win)
98+ return;
99+
100+ if (!title)
101+ title = "sxiv";
102+
103+ XStoreName(win->env.dpy, win->xwin, title);
104+ XSetIconName(win->env.dpy, win->xwin, title);
105+}
106+
97107 int win_configure(win_t *win, XConfigureEvent *cev) {
98108 int changed;
99109
M window.h View
@@ -46,6 +46,8 @@ typedef struct win_s {
4646 void win_open(win_t*);
4747 void win_close(win_t*);
4848
49+void win_set_title(win_t*, const char*);
50+
4951 int win_configure(win_t*, XConfigureEvent*);
5052 void win_clear(win_t*);
5153