commit 9abe5617bed41b35fe53c8830708fd6a6eb2ec0e
parent 60095c0878b2e20accd0412419e3472a00b6ef6a
Author: lumidify <nobody@lumidify.org>
Date:   Sat,  6 Mar 2021 15:30:57 +0100
Change cursor when image changes
Diffstat:
| M | croptool.c |  |  | 91 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------- | 
1 file changed, 62 insertions(+), 29 deletions(-)
diff --git a/croptool.c b/croptool.c
@@ -102,6 +102,8 @@ static struct {
 	int num_files;
 	int window_w;
 	int window_h;
+	int cursor_x;
+	int cursor_y;
 	struct Point move_handle;
 	short moving;
 	short resizing;
@@ -148,6 +150,7 @@ 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 set_cursor(struct Rect rect);
 static void drag_motion(XEvent event);
 static void resize_window(int w, int h);
 static void button_release(void);
@@ -283,6 +286,8 @@ setup(int argc, char *argv[]) {
 	state.lock_y = 0;
 	state.window_w = 500;
 	state.window_h = 500;
+	state.cursor_x = 0;
+	state.cursor_y = 0;
 	state.cur_col = 1;
 
 	for (int i = 0; i < argc; i++) {
@@ -781,13 +786,57 @@ queue_rectangle_redraw(int x0, int y0, int x1, int y1) {
 	    LINE_WIDTH * 2, y1 - y0 + LINE_WIDTH * 2);
 }
 
+/* set the appropriate cursor based on the
+ * current mouse position and a cropping rectangle */
+static void
+set_cursor(struct Rect rect) {
+	Cursor c = None;
+	sort_coordinates(&rect.x0, &rect.y0, &rect.x1, &rect.y1);
+	if (collide_point(
+	    state.cursor_x, state.cursor_y,
+	    rect.x0, rect.y0)) {
+		c = cursors.topleft;
+	} else if (collide_point(
+	    state.cursor_x, state.cursor_y,
+	    rect.x1, rect.y0)) {
+		c = cursors.topright;
+	} else if (collide_point(
+	    state.cursor_x, state.cursor_y,
+	    rect.x0, rect.y1)) {
+		c = cursors.bottomleft;
+	} else if (collide_point(
+	    state.cursor_x, state.cursor_y,
+	    rect.x1, rect.y1)) {
+		c = cursors.bottomright;
+	} else if (collide_line(
+	    state.cursor_x, state.cursor_y,
+	    rect.x0, rect.y0, rect.x1, rect.y0)) {
+		c = cursors.top;
+	} else if (collide_line(
+	    state.cursor_x, state.cursor_y,
+	    rect.x1, rect.y1, rect.x0, rect.y1)) {
+		c = cursors.bottom;
+	} else if (collide_line(
+	    state.cursor_x, state.cursor_y,
+	    rect.x1, rect.y1, rect.x1, rect.y0)) {
+		c = cursors.right;
+	} else if (collide_line(
+	    state.cursor_x, state.cursor_y,
+	    rect.x0, rect.y0, rect.x0, rect.y1)) {
+		c = cursors.left;
+	} else if (collide_rect(state.cursor_x, state.cursor_y, rect)) {
+		c = cursors.grab;
+	}
+	XDefineCursor(state.dpy, state.win, c);
+}
+
 static void
 drag_motion(XEvent event) {
 	if (state.cur_selection < 0 || !state.selections[state.cur_selection].valid)
 		return;
 	struct Rect *rect = &state.selections[state.cur_selection].rect;
-	int x = event.xbutton.x;
-	int y = event.xbutton.y;
+	state.cursor_x = event.xbutton.x;
+	state.cursor_y = event.xbutton.y;
 	int x0 = rect->x0, x1 = rect->x1;
 	int y0 = rect->y0, y1 = rect->y1;
 	sort_coordinates(&x0, &y0, &x1, &y1);
@@ -795,43 +844,24 @@ drag_motion(XEvent event) {
 	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;
+		int x_delta = state.cursor_x - state.move_handle.x;
+		int y_delta = state.cursor_y - state.move_handle.y;
 		rect->x0 += x_delta;
 		rect->y0 += y_delta;
 		rect->x1 += x_delta;
 		rect->y1 += y_delta;
-		state.move_handle.x = x;
-		state.move_handle.y = y;
+		state.move_handle.x = state.cursor_x;
+		state.move_handle.y = state.cursor_y;
 	} else if (state.resizing) {
 		if (!state.lock_y)
-			rect->x1 = x;
+			rect->x1 = state.cursor_x;
 		if (!state.lock_x)
-			rect->y1 = y;
+			rect->y1 = state.cursor_y;
 	} else {
-		Cursor c = None;
-		if (collide_point(x, y, x0, y0)) {
-			c = cursors.topleft;
-		} else if (collide_point(x, y, x1, y0)) {
-			c = cursors.topright;
-		} else if (collide_point(x, y, x0, y1)) {
-			c = cursors.bottomleft;
-		} else if (collide_point(x, y, x1, y1)) {
-			c = cursors.bottomright;
-		} else if (collide_line(x, y, x0, y0, x1, y0)) {
-			c = cursors.top;
-		} else if (collide_line(x, y, x1, y1, x0, y1)) {
-			c = cursors.bottom;
-		} else if (collide_line(x, y, x1, y1, x1, y0)) {
-			c = cursors.right;
-		} else if (collide_line(x, y, x0, y0, x0, y1)) {
-			c = cursors.left;
-		} else if (collide_rect(x, y, *rect)) {
-			c = cursors.grab;
-		}
-		XDefineCursor(state.dpy, state.win, c);
+		set_cursor(*rect);
 		return;
 	}
+	set_cursor(*rect);
 
 	/* redraw the new rectangle */
 	if (SELECTION_REDRAW)
@@ -928,6 +958,9 @@ change_picture(Imlib_Image new_image, int new_selection, int copy_box) {
 	sel->scaled_h = actual_h;
 	sel->valid = 1;
 	queue_update(0, 0, sel->scaled_w, sel->scaled_h);
+
+	/* set the cursor since the cropping rectangle may have changed */
+	set_cursor(sel->rect);
 }
 
 /* show the next image in the argument list - unloadable files are skipped