page: refactor menu

This commit is contained in:
cinap_lenrek 2011-12-21 21:19:37 +01:00
parent 1d83d2a74e
commit 27830ae53e

View file

@ -47,37 +47,57 @@ enum {
NPATH = 1024, NPATH = 1024,
}; };
char *pagemenugen(int i); enum {
Corigsize,
char *menuitems[] = { Czoomin,
"orig size", Czoomout,
"rotate 90", Cfitwidth,
"upside down", Cfitheight,
"", Crotate90,
"fit width", Cupsidedown,
"fit height", Cdummy1,
"", Cnext,
"zoom in", Cprev,
"zoom out", Czerox,
"", Cwrite,
"next", Cdummy2,
"prev", Cquit,
"zerox",
"write",
"",
"quit",
nil
}; };
struct {
char *m;
Rune k1;
Rune k2;
Rune k3;
} cmds[] = {
[Corigsize] "orig size", 'o', Kesc, 0,
[Czoomin] "zoom in", '+', 0, 0,
[Czoomout] "zoom out", '-', 0, 0,
[Cfitwidth] "fit width", 'f', 0, 0,
[Cfitheight] "fit height", 'h', 0, 0,
[Crotate90] "rotate 90", 'r', 0, 0,
[Cupsidedown] "upside down", 'u', 0, 0,
[Cdummy1] "", 0, 0, 0,
[Cnext] "next", Kright, ' ', '\n',
[Cprev] "prev", Kleft, Kbs, 0,
[Czerox] "zerox", 'z', 0, 0,
[Cwrite] "write", 'w', 0, 0,
[Cdummy2] "", 0, 0, 0,
[Cquit] "quit", 'q', Kdel, Keof,
};
char *pagemenugen(int i);
char *cmdmenugen(int i);
Menu pagemenu = { Menu pagemenu = {
nil, nil,
pagemenugen, pagemenugen,
-1, -1,
}; };
Menu menu = { Menu cmdmenu = {
menuitems,
nil, nil,
cmdmenugen,
-1, -1,
}; };
@ -1071,6 +1091,14 @@ pagemenugen(int i)
return nil; return nil;
} }
char*
cmdmenugen(int i)
{
if(i < 0 || i >= nelem(cmds))
return nil;
return cmds[i].m;
}
void void
showpage(Page *p) showpage(Page *p)
{ {
@ -1182,6 +1210,106 @@ usage(void)
exits("usage"); exits("usage");
} }
int
docmd(int i, Mouse *m)
{
char buf[NPATH], *s;
Point o;
int fd;
switch(i){
case Corigsize:
pos = ZP;
zoom = 1;
resize = ZP;
rotate = 0;
Unload:
viewgen++;
unlockdisplay(display);
esetcursor(&reading);
unloadpages(0);
showpage(current);
return 0;
case Cupsidedown:
rotate += 90;
case Crotate90:
rotate += 90;
rotate %= 360;
goto Unload;
case Cfitwidth:
pos = ZP;
zoom = 1;
resize = subpt(screen->r.max, screen->r.min);
resize.y = 0;
goto Unload;
case Cfitheight:
pos = ZP;
zoom = 1;
resize = subpt(screen->r.max, screen->r.min);
resize.x = 0;
goto Unload;
case Czoomin:
case Czoomout:
if(current == nil || !canqlock(current))
return 1;
o = subpt(m->xy, screen->r.min);
if(i == Czoomin){
if(zoom < 0x40000000){
zoom *= 2;
pos = addpt(mulpt(subpt(pos, o), 2), o);
}
}else{
if(zoom > 1){
zoom /= 2;
pos = addpt(divpt(subpt(pos, o), 2), o);
}
}
drawpage(current);
qunlock(current);
return 1;
case Cwrite:
if(current == nil || !canqlock(current))
return 1;
if(current->image){
s = nil;
if(current->up && current->up != root)
s = current->up->label;
snprint(buf, sizeof(buf), "%s%s%s.bit",
s ? s : "",
s ? "." : "",
current->label);
if(eenter("Write", buf, sizeof(buf), m) > 0){
if((fd = create(buf, OWRITE, 0666)) < 0){
errstr(buf, sizeof(buf));
eenter(buf, 0, 0, m);
} else {
esetcursor(&reading);
writeimage(fd, current->image, 0);
close(fd);
esetcursor(nil);
}
}
}
qunlock(current);
return 1;
case Cnext:
unlockdisplay(display);
shownext();
return 0;
case Cprev:
unlockdisplay(display);
showprev();
return 0;
case Czerox:
unlockdisplay(display);
zerox(current);
return 0;
case Cquit:
exits(0);
}
return 1;
}
void void
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
@ -1261,211 +1389,93 @@ main(int argc, char *argv[])
lockdisplay(display); lockdisplay(display);
m = e.mouse; m = e.mouse;
if(m.buttons & 1){ if(m.buttons & 1){
if(current == nil || !canqlock(current)) if(current && canqlock(current)){
goto Unlock; for(;;) {
for(;;) { o = m.xy;
o = m.xy; m = emouse();
m = emouse(); if((m.buttons & 1) == 0)
if((m.buttons & 1) == 0) break;
break; translate(current, subpt(m.xy, o));
translate(current, subpt(m.xy, o)); }
qunlock(current);
} }
qunlock(current); } else if(m.buttons & 2){
goto Unlock;
}
if(m.buttons & 2){
o = m.xy; o = m.xy;
i = emenuhit(2, &m, &menu); i = emenuhit(2, &m, &cmdmenu);
if(i < 0 || i >= nelem(menuitems) || menuitems[i]==nil) m.xy = o;
goto Unlock; if(!docmd(i, &m))
s = menuitems[i]; continue;
PageMenu: } else if(m.buttons & 4){
if(strcmp(s, "orig size")==0){ if(root->down){
pos = ZP; Page *x;
zoom = 1;
resize = ZP; qlock(&pagelock);
rotate = 0; pagemenu.lasthit = pageindex(current);
Unload: x = pageat(emenuhit(3, &m, &pagemenu));
viewgen++; qunlock(&pagelock);
unlockdisplay(display); unlockdisplay(display);
esetcursor(&reading); showpage(x);
unloadpages(0);
showpage(current);
continue; continue;
} }
if(strncmp(s, "rotate ", 7)==0){
rotate += atoi(s+7);
rotate %= 360;
goto Unload;
}
if(strcmp(s, "upside down")==0){
rotate += 180;
goto Unload;
}
if(strcmp(s, "fit width")==0){
pos = ZP;
zoom = 1;
resize = subpt(screen->r.max, screen->r.min);
resize.y = 0;
goto Unload;
}
if(strcmp(s, "fit height")==0){
pos = ZP;
zoom = 1;
resize = subpt(screen->r.max, screen->r.min);
resize.x = 0;
goto Unload;
}
if(strncmp(s, "zoom", 4)==0){
if(current && canqlock(current)){
o = subpt(o, screen->r.min);
if(strstr(s, "in")){
if(zoom < 0x40000000){
zoom *= 2;
pos = addpt(mulpt(subpt(pos, o), 2), o);
}
}else{
if(zoom > 1){
zoom /= 2;
pos = addpt(divpt(subpt(pos, o), 2), o);
}
}
drawpage(current);
qunlock(current);
}
goto Unlock;
}
if(strcmp(s, "write")==0){
if(current && canqlock(current)){
if(current->image){
s = nil;
if(current->up && current->up != root)
s = current->up->label;
snprint(buf, sizeof(buf), "%s%s%s.bit",
s ? s : "",
s ? "." : "",
current->label);
if(eenter("Write", buf, sizeof(buf), &m) > 0){
int fd;
if((fd = create(buf, OWRITE, 0666)) < 0){
errstr(buf, sizeof(buf));
eenter(buf, 0, 0, &m);
} else {
esetcursor(&reading);
writeimage(fd, current->image, 0);
close(fd);
esetcursor(nil);
}
}
}
qunlock(current);
}
goto Unlock;
}
unlockdisplay(display);
if(strcmp(s, "next")==0)
shownext();
if(strcmp(s, "prev")==0)
showprev();
if(strcmp(s, "zerox")==0)
zerox(current);
if(strcmp(s, "quit")==0)
exits(0);
continue;
} }
if(m.buttons & 4){
Page *x;
if(root->down == nil)
goto Unlock;
qlock(&pagelock);
pagemenu.lasthit = pageindex(current);
x = pageat(emenuhit(3, &m, &pagemenu));
qunlock(&pagelock);
unlockdisplay(display);
showpage(x);
continue;
}
Unlock:
unlockdisplay(display); unlockdisplay(display);
break; break;
case Ekeyboard: case Ekeyboard:
lockdisplay(display);
switch(e.kbdc){ switch(e.kbdc){
case 'q':
case Kdel:
case Keof:
exits(0);
break;
case 'w':
s = "write";
goto DoMenu;
case 'u':
s = "upside down";
goto DoMenu;
case '+':
s = "zoom in";
goto DoMenu;
case '-':
s = "zoom out";
goto DoMenu;
case Kesc:
s = "orig size";
DoMenu:
lockdisplay(display);
o = m.xy;
goto PageMenu;
case Kup: case Kup:
if(current == nil || !canqlock(current)) if(current == nil || !canqlock(current))
break; break;
lockdisplay(display);
if(pos.y < 0){ if(pos.y < 0){
translate(current, Pt(0, Dy(screen->r)/2)); translate(current, Pt(0, Dy(screen->r)/2));
unlockdisplay(display);
qunlock(current); qunlock(current);
continue; break;
} }
unlockdisplay(display);
qunlock(current);
if(prevpage(current)) if(prevpage(current))
pos.y = 0; pos.y = 0;
case Kbs: qunlock(current);
case Kleft: if(!docmd(Cprev, &m))
showprev(); continue;
break; break;
case Kdown: case Kdown:
if(current == nil || !canqlock(current)) if(current == nil || !canqlock(current))
break; break;
o = addpt(pos, pagesize(current)); o = addpt(pos, pagesize(current));
lockdisplay(display);
if(o.y > Dy(screen->r)){ if(o.y > Dy(screen->r)){
translate(current, Pt(0, -Dy(screen->r)/2)); translate(current, Pt(0, -Dy(screen->r)/2));
unlockdisplay(display);
qunlock(current); qunlock(current);
continue; break;
} }
unlockdisplay(display);
qunlock(current);
if(nextpage(current)) if(nextpage(current))
pos.y = 0; pos.y = 0;
case '\n': qunlock(current);
case ' ': if(!docmd(Cnext, &m))
case Kright: continue;
shownext();
break; break;
default: default:
for(i = 0; i<nelem(cmds); i++)
if((cmds[i].k1 == e.kbdc) ||
(cmds[i].k2 == e.kbdc) ||
(cmds[i].k3 == e.kbdc))
break;
if(i < nelem(cmds)){
if(!docmd(i, &m))
continue;
break;
}
if((e.kbdc < 0x20) || if((e.kbdc < 0x20) ||
(e.kbdc & 0xFF00) == KF || (e.kbdc & 0xFF00) == KF ||
(e.kbdc & 0xFF00) == Spec) (e.kbdc & 0xFF00) == Spec)
break; break;
snprint(buf, sizeof(buf), "%C", (Rune)e.kbdc); snprint(buf, sizeof(buf), "%C", (Rune)e.kbdc);
lockdisplay(display);
i = eenter("Go to", buf, sizeof(buf), &m); i = eenter("Go to", buf, sizeof(buf), &m);
unlockdisplay(display); if(i > 0){
if(i > 0) unlockdisplay(display);
showpage(findpage(buf)); showpage(findpage(buf));
continue;
}
} }
unlockdisplay(display);
break; break;
case Eplumb: case Eplumb:
pm = e.v; pm = e.v;