Small refactorings, removed over-ambitious todo
This commit is contained in:
parent
7334bdfa51
commit
9b24f82404
2
Makefile
2
Makefile
@ -1,6 +1,6 @@
|
||||
all: sxiv
|
||||
|
||||
VERSION=0.7
|
||||
VERSION=git-20110227
|
||||
|
||||
CC?=gcc
|
||||
PREFIX?=/usr/local
|
||||
|
70
main.c
70
main.c
@ -69,17 +69,20 @@ void cleanup() {
|
||||
}
|
||||
}
|
||||
|
||||
int load_image() {
|
||||
int load_image(int new) {
|
||||
struct stat fstats;
|
||||
|
||||
if (new >= 0 && new < filecnt) {
|
||||
img_close(&img);
|
||||
|
||||
fileidx = new;
|
||||
if (!stat(filenames[fileidx], &fstats))
|
||||
filesize = fstats.st_size;
|
||||
else
|
||||
filesize = 0;
|
||||
|
||||
return img_load(&img, filenames[fileidx]);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
@ -140,7 +143,7 @@ int main(int argc, char **argv) {
|
||||
} else {
|
||||
mode = MODE_NORMAL;
|
||||
tns.thumbs = NULL;
|
||||
load_image();
|
||||
load_image(fileidx);
|
||||
img_render(&img, &win);
|
||||
}
|
||||
|
||||
@ -307,41 +310,29 @@ void on_keypress(XKeyEvent *kev) {
|
||||
/* navigate image list */
|
||||
case XK_n:
|
||||
case XK_space:
|
||||
if (fileidx + 1 < filecnt) {
|
||||
++fileidx;
|
||||
changed = load_image();
|
||||
}
|
||||
if (fileidx + 1 < filecnt)
|
||||
changed = load_image(fileidx + 1);
|
||||
break;
|
||||
case XK_p:
|
||||
case XK_BackSpace:
|
||||
if (fileidx > 0) {
|
||||
--fileidx;
|
||||
changed = load_image();
|
||||
}
|
||||
if (fileidx > 0)
|
||||
changed = load_image(fileidx - 1);
|
||||
break;
|
||||
case XK_bracketleft:
|
||||
if (fileidx != 0) {
|
||||
fileidx = MAX(0, fileidx - 10);
|
||||
changed = load_image();
|
||||
}
|
||||
if (fileidx != 0)
|
||||
changed = load_image(MAX(0, fileidx - 10));
|
||||
break;
|
||||
case XK_bracketright:
|
||||
if (fileidx != filecnt - 1) {
|
||||
fileidx = MIN(fileidx + 10, filecnt - 1);
|
||||
changed = load_image();
|
||||
}
|
||||
if (fileidx != filecnt - 1)
|
||||
changed = load_image(MIN(fileidx + 10, filecnt - 1));
|
||||
break;
|
||||
case XK_g:
|
||||
if (fileidx != 0) {
|
||||
fileidx = 0;
|
||||
changed = load_image();
|
||||
}
|
||||
if (fileidx != 0)
|
||||
changed = load_image(0);
|
||||
break;
|
||||
case XK_G:
|
||||
if (fileidx != filecnt - 1) {
|
||||
fileidx = filecnt - 1;
|
||||
changed = load_image();
|
||||
}
|
||||
if (fileidx != filecnt - 1)
|
||||
changed = load_image(filecnt - 1);
|
||||
break;
|
||||
|
||||
/* zooming */
|
||||
@ -418,7 +409,7 @@ void on_keypress(XKeyEvent *kev) {
|
||||
changed = 1;
|
||||
break;
|
||||
case XK_r:
|
||||
changed = load_image();
|
||||
changed = load_image(fileidx);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
@ -426,8 +417,7 @@ void on_keypress(XKeyEvent *kev) {
|
||||
switch (ksym) {
|
||||
/* open selected image */
|
||||
case XK_Return:
|
||||
fileidx = tns.sel;
|
||||
load_image();
|
||||
load_image(tns.sel);
|
||||
mode = MODE_NORMAL;
|
||||
win_set_cursor(&win, CURSOR_NONE);
|
||||
changed = 1;
|
||||
@ -496,10 +486,8 @@ void on_buttonpress(XButtonEvent *bev) {
|
||||
if (mode == MODE_NORMAL) {
|
||||
switch (bev->button) {
|
||||
case Button1:
|
||||
if (fileidx + 1 < filecnt) {
|
||||
++fileidx;
|
||||
changed = load_image();
|
||||
}
|
||||
if (fileidx + 1 < filecnt)
|
||||
changed = load_image(fileidx + 1);
|
||||
break;
|
||||
case Button2:
|
||||
mox = bev->x;
|
||||
@ -509,10 +497,8 @@ void on_buttonpress(XButtonEvent *bev) {
|
||||
drag = 1;
|
||||
break;
|
||||
case Button3:
|
||||
if (fileidx > 0) {
|
||||
--fileidx;
|
||||
changed = load_image();
|
||||
}
|
||||
if (fileidx > 0)
|
||||
changed = load_image(fileidx - 1);
|
||||
break;
|
||||
case Button4:
|
||||
if (mask == ControlMask)
|
||||
@ -543,8 +529,7 @@ void on_buttonpress(XButtonEvent *bev) {
|
||||
case Button1:
|
||||
if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
|
||||
if (sel == tns.sel) {
|
||||
fileidx = tns.sel;
|
||||
load_image();
|
||||
load_image(tns.sel);
|
||||
mode = MODE_NORMAL;
|
||||
timo_cursor = TO_CURSOR_HIDE;
|
||||
} else {
|
||||
@ -600,7 +585,8 @@ void run() {
|
||||
gettimeofday(&t0, 0);
|
||||
|
||||
while (!XPending(win.env.dpy) && tns.cnt < filecnt) {
|
||||
tns_load(&tns, &win, filenames[tns.cnt]);
|
||||
/* tns.cnt is increased inside tns_load */
|
||||
tns_load(&tns, &win, tns.cnt, filenames[tns.cnt]);
|
||||
gettimeofday(&t1, 0);
|
||||
if (TV_TO_DOUBLE(t1) - TV_TO_DOUBLE(t0) >= 0.25)
|
||||
break;
|
||||
|
18
thumbs.c
18
thumbs.c
@ -35,6 +35,7 @@ void tns_init(tns_t *tns, int cnt) {
|
||||
tns->cnt = tns->first = tns->sel = 0;
|
||||
tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
|
||||
memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
|
||||
tns->cap = cnt;
|
||||
tns->dirty = 0;
|
||||
}
|
||||
|
||||
@ -51,7 +52,7 @@ void tns_free(tns_t *tns, win_t *win) {
|
||||
tns->thumbs = NULL;
|
||||
}
|
||||
|
||||
void tns_load(tns_t *tns, win_t *win, const char *filename) {
|
||||
void tns_load(tns_t *tns, win_t *win, int n, const char *filename) {
|
||||
int w, h;
|
||||
float z, zw, zh;
|
||||
thumb_t *t;
|
||||
@ -60,10 +61,17 @@ void tns_load(tns_t *tns, win_t *win, const char *filename) {
|
||||
if (!tns || !win || !filename)
|
||||
return;
|
||||
|
||||
if ((im = imlib_load_image(filename)))
|
||||
if (n >= tns->cap)
|
||||
return;
|
||||
else if (n >= tns->cnt)
|
||||
tns->cnt = n + 1;
|
||||
|
||||
if ((im = imlib_load_image(filename))) {
|
||||
imlib_context_set_image(im);
|
||||
else
|
||||
imlib_image_set_changes_on_disk();
|
||||
} else {
|
||||
imlib_context_set_image(im_broken);
|
||||
}
|
||||
|
||||
w = imlib_image_get_width();
|
||||
h = imlib_image_get_height();
|
||||
@ -73,10 +81,12 @@ void tns_load(tns_t *tns, win_t *win, const char *filename) {
|
||||
if (!im && z > 1.0)
|
||||
z = 1.0;
|
||||
|
||||
t = &tns->thumbs[tns->cnt++];
|
||||
t = &tns->thumbs[n];
|
||||
t->w = z * w;
|
||||
t->h = z * h;
|
||||
|
||||
if (t->pm)
|
||||
win_free_pixmap(win, t->pm);
|
||||
t->pm = win_create_pixmap(win, t->w, t->h);
|
||||
imlib_context_set_drawable(t->pm);
|
||||
imlib_context_set_anti_alias(1);
|
||||
|
3
thumbs.h
3
thumbs.h
@ -38,6 +38,7 @@ typedef struct thumb_s {
|
||||
|
||||
typedef struct tns_s {
|
||||
thumb_t *thumbs;
|
||||
int cap;
|
||||
int cnt;
|
||||
int x;
|
||||
int y;
|
||||
@ -51,7 +52,7 @@ typedef struct tns_s {
|
||||
void tns_init(tns_t*, int);
|
||||
void tns_free(tns_t*, win_t*);
|
||||
|
||||
void tns_load(tns_t*, win_t*, const char*);
|
||||
void tns_load(tns_t*, win_t*, int, const char*);
|
||||
|
||||
void tns_render(tns_t*, win_t*);
|
||||
void tns_highlight(tns_t*, win_t*, int, Bool);
|
||||
|
Loading…
Reference in New Issue
Block a user