shithub: view

Download patch

ref: 5739cbecbd0bf4ebea193220ce403c03cdedf300
parent: a40f71c6e963071e0eaf3ed89e6fc935e382937c
author: phil9 <telephil9@gmail.com>
date: Thu Nov 18 13:05:43 EST 2021

implement panning with mouse

--- a/view.c
+++ b/view.c
@@ -20,6 +20,7 @@
 Keyboardctl *kctl;
 Image *bg;
 Image *img;
+Point pos;
 
 Image*
 load9(char *filename)
@@ -104,12 +105,28 @@
 {
 	lockdisplay(display);
 	draw(screen, screen->r, bg, nil, ZP);
-	draw(screen, screen->r, img, nil, img->r.min);
+	draw(screen, rectaddpt(img->r, addpt(pos, screen->r.min)), img, nil, img->r.min);
 	flushimage(display, 1);
 	unlockdisplay(display);
 }
 
 void
+pan(Point Δ)
+{
+	Rectangle r, ir;
+
+	if(Δ.x == 0 && Δ.y == 0)
+		return;
+	r = rectaddpt(img->r, addpt(pos, screen->r.min));
+	pos = addpt(pos, Δ);
+	ir = rectaddpt(r, Δ);
+	lockdisplay(display);
+	draw(screen, screen->r, bg, nil, ZP);
+	draw(screen, ir, img, nil, img->r.min);
+	unlockdisplay(display);
+}
+
+void
 evtresize(int new)
 {
 	if(new && getwindow(display, Refnone)<0)
@@ -120,7 +137,18 @@
 void
 evtmouse(Mouse m)
 {
-	USED(m);
+	Point o;
+
+	if(m.buttons == 1){
+		for(;;){
+			o = mctl->xy;
+			if(readmouse(mctl) < 0)
+				break;
+			if((mctl->buttons & 1) == 0)
+				break;
+			pan(subpt(mctl->xy, o));
+		}
+	}
 }
 
 void
@@ -169,6 +197,7 @@
 	alts[Ekeyboard].c = kctl->c;
 	initbg();
 	img = load(*argv);
+	pos = subpt(ZP, img->r.min);
 	evtresize(0);
 	for(;;){
 		switch(alt(alts)){