commit 7bd0d9adca219f37e471c3e2c6a80c88532557b8
parent 271f6a187472506ddf73f686f4cdeb927c5292e8
Author: lumidify <nobody@lumidify.org>
Date:   Sat,  2 Jan 2021 20:25:29 +0100
Fix redrawing of cropping rectangle
Diffstat:
5 files changed, 33 insertions(+), 20 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1 +1,2 @@
 croptool
+croptool_crop
diff --git a/Makefile b/Makefile
@@ -7,7 +7,6 @@ PREFIX = /usr/local
 MANPREFIX = ${PREFIX}/man
 
 BIN = ${NAME} croptool_crop
-SRC = ${BIN:=.c}
 MAN1 = ${BIN:=.1}
 
 CFLAGS += -D_POSIX_C_SOURCE=200809L `pkg-config --cflags x11` `imlib2-config --cflags`
diff --git a/README b/README
@@ -2,4 +2,7 @@ croptool - mass image cropping tool
 
 REQUIREMENTS: xlib, imlib2
 
+croptool is a simple tool to select cropping rectangles
+on images and print out a command to crop each image.
+
 See croptool.1 for more information.
diff --git a/croptool.1 b/croptool.1
@@ -1,4 +1,4 @@
-.Dd January 1, 2021
+.Dd January 2, 2021
 .Dt CROPTOOL 1
 .Os
 .Sh NAME
diff --git a/croptool.c b/croptool.c
@@ -136,6 +136,7 @@ static void get_scaled_size(int orig_w, int orig_h, int *scaled_w, int *scaled_h
 static void set_selection(
     struct Selection *sel, int rect_x0, int rect_y0, int rect_x1,
     int rect_y1, int orig_w, int orig_h, int scaled_w, int scaled_h);
+static void queue_rectangle_redraw(int x0, int y0, int x1, int y1);
 static void drag_motion(XEvent event);
 static void resize_window(int w, int h);
 static void button_release(XEvent event);
@@ -531,6 +532,8 @@ button_press(XEvent event) {
 	int y = event.xbutton.y;
 	int x0 = rect->x0, x1 = rect->x1;
 	int y0 = rect->y0, y1 = rect->y1;
+	/* erase old rectangle */
+	queue_rectangle_redraw(x0, y0, x1, y1);
 	if (collide_point(x, y, x0, y0)) {
 		rect->x0 = x1;
 		rect->y0 = y1;
@@ -615,6 +618,27 @@ resize_window(int w, int h) {
 }
 
 static void
+queue_rectangle_redraw(int x0, int y0, int x1, int y1) {
+	sort_coordinates(&x0, &y0, &x1, &y1);
+	queue_update(
+	    x0 - LINE_WIDTH > 0 ? x0 - LINE_WIDTH : 0,
+	    y0 - LINE_WIDTH > 0 ? y0 - LINE_WIDTH : 0,
+	    x1 - x0 + LINE_WIDTH * 2, LINE_WIDTH * 2);
+	queue_update(
+	    x0 - LINE_WIDTH > 0 ? x0 - LINE_WIDTH : 0,
+	    y1 - LINE_WIDTH > 0 ? y1 - LINE_WIDTH : 0,
+	    x1 - x0 + LINE_WIDTH * 2, LINE_WIDTH * 2);
+	queue_update(
+	    x0 - LINE_WIDTH > 0 ? x0 - LINE_WIDTH : 0,
+	    y0 - LINE_WIDTH > 0 ? y0 - LINE_WIDTH : 0,
+	    LINE_WIDTH * 2, y1 - y0 + LINE_WIDTH * 2);
+	queue_update(
+	    x1 - LINE_WIDTH > 0 ? x1 - LINE_WIDTH : 0,
+	    y0 - LINE_WIDTH > 0 ? y0 - LINE_WIDTH : 0,
+	    LINE_WIDTH * 2, y1 - y0 + LINE_WIDTH * 2);
+}
+
+static void
 drag_motion(XEvent event) {
 	if (state.cur_selection < 0 || !state.selections[state.cur_selection].valid)
 		return;
@@ -624,6 +648,8 @@ drag_motion(XEvent event) {
 	int x0 = rect->x0, x1 = rect->x1;
 	int y0 = rect->y0, y1 = rect->y1;
 	sort_coordinates(&x0, &y0, &x1, &y1);
+	if (SELECTION_REDRAW && (state.moving || state.resizing))
+		queue_rectangle_redraw(x0, y0, x1, y1);
 	if (state.moving) {
 		int x_delta = x - state.move_handle.x;
 		int y_delta = y - state.move_handle.y;
@@ -663,24 +689,8 @@ drag_motion(XEvent event) {
 		return;
 	}
 
-	if (SELECTION_REDRAW) {
-		queue_update(
-		    x0 - LINE_WIDTH > 0 ? x0 - LINE_WIDTH : 0,
-		    y0 - LINE_WIDTH > 0 ? y0 - LINE_WIDTH : 0,
-		    x1 - x0 + LINE_WIDTH * 2, LINE_WIDTH * 2);
-		queue_update(
-		    x0 - LINE_WIDTH > 0 ? x0 - LINE_WIDTH : 0,
-		    y1 - LINE_WIDTH > 0 ? y1 - LINE_WIDTH : 0,
-		    x1 - x0 + LINE_WIDTH * 2, LINE_WIDTH * 2);
-		queue_update(
-		    x0 - LINE_WIDTH > 0 ? x0 - LINE_WIDTH : 0,
-		    y0 - LINE_WIDTH > 0 ? y0 - LINE_WIDTH : 0,
-		    LINE_WIDTH * 2, y1 - y0 + LINE_WIDTH * 2);
-		queue_update(
-		    x1 - LINE_WIDTH > 0 ? x1 - LINE_WIDTH : 0,
-		    y0 - LINE_WIDTH > 0 ? y0 - LINE_WIDTH : 0,
-		    LINE_WIDTH * 2, y1 - y0 + LINE_WIDTH * 2);
-	}
+	if (SELECTION_REDRAW)
+		queue_rectangle_redraw(rect->x0, rect->y0, rect->x1, rect->y1);
 }
 
 static void