commit a02921f770f7fe0de7ea4dc184851a2cd0b8cf1b
parent 65d16f82af9f93ad26d93b94feb963e64dfa0b60
Author: lumidify <nobody@lumidify.org>
Date:   Mon, 25 May 2020 20:38:54 +0200
Allow two different selection box colors
Diffstat:
2 files changed, 29 insertions(+), 6 deletions(-)
diff --git a/README b/README
@@ -32,6 +32,8 @@ Several keys are recognized:
 * Delete removes the selection for the current image (this is then
   also not printed out at the end).
 * Space bar resizes the image if the window was resized.
+* Tab switches the color of the selection box between the two colors
+  defined at the top of `croptool.c` (SELECTION_COLOR1, SELECTION_COLOR2).
 
 Note that resizing the window currently does not resize the images.
 It will only take effect if you move to another image or press
@@ -56,7 +58,8 @@ Configuration:
 If you want to, you can edit a few things at the top of `bookcrop.c`.
 COLLISION_PADDING is the number of pixels to check for collision if
 an edge or corner is clicked.
-SELECTION_COLOR is the color the selection box is drawn in.
+SELECTION_COLOR1 and SELECTION_COLOR2 are the two colors for the
+selection box that can be switched with tab.
 If you want to change the command that is output, you can change
 the function `print_cmd`. It just receives the filename, the coordinates
 of the top left corner of the cropping box, and the width and height
diff --git a/croptool.c b/croptool.c
@@ -27,7 +27,9 @@
  * (in order to change the size of the box) */
 static const int COLLISION_PADDING = 10;
 /* The color of the selection box */
-static const char *SELECTION_COLOR = "#000";
+static const char *SELECTION_COLOR1 = "#000";
+/* The second selection color - when tab is pressed */
+static const char *SELECTION_COLOR2 = "#fff";
 
 /* Change this if you want a different output format. */
 static void
@@ -68,7 +70,9 @@ struct State {
 	gboolean resizing;
 	gboolean lock_x;
 	gboolean lock_y;
-	GdkColor gdk_color;
+	GdkColor col1;
+	GdkColor col2;
+	int cur_col;
 };
 
 static void swap(int *a, int *b);
@@ -92,6 +96,7 @@ static GdkPixbuf *load_pixbuf(char *filename, int w, int h, int *actual_w, int *
 static void print_selection(struct Selection *sel, const char *filename);
 static void clear_selection(GtkWidget *area, struct State *state);
 static void resize_manual(GtkWidget *area, struct State *state);
+static void switch_color(GtkWidget *area, struct State *state);
 
 int main(int argc, char *argv[]) {
 	GtkWidget *window;
@@ -116,6 +121,7 @@ int main(int argc, char *argv[]) {
 	state->lock_y = FALSE;
 	state->window_w = 0;
 	state->window_h = 0;
+	state->cur_col = 1;
 	for (int i = 0; i < argc; i++) {
 		state->selections[i] = NULL;
 	}
@@ -143,8 +149,10 @@ int main(int argc, char *argv[]) {
 	gtk_widget_show_all(window);
 
 	GdkColormap *cmap = gdk_drawable_get_colormap(area->window);
-	gdk_colormap_alloc_color(cmap, &state->gdk_color, FALSE, TRUE);
-	gdk_color_parse(SELECTION_COLOR, &state->gdk_color);
+	gdk_colormap_alloc_color(cmap, &state->col1, FALSE, TRUE);
+	gdk_color_parse(SELECTION_COLOR1, &state->col1);
+	gdk_colormap_alloc_color(cmap, &state->col2, FALSE, TRUE);
+	gdk_color_parse(SELECTION_COLOR2, &state->col2);
 	g_object_unref(cmap);
 
 	gtk_main();
@@ -326,9 +334,10 @@ redraw(GtkWidget *area, struct State *state) {
 	gdk_cairo_set_source_pixbuf(cr, state->cur_pixbuf, 0, 0);
 	cairo_paint(cr);
 
+	GdkColor col = state->cur_col == 1 ? state->col1 : state->col2;
 	if (state->selections[state->cur_selection]) {
 		struct Rect rect = state->selections[state->cur_selection]->rect;
-		gdk_cairo_set_source_color(cr, &state->gdk_color);
+		gdk_cairo_set_source_color(cr, &col);
 		cairo_move_to(cr, rect.x0, rect.y0);
 		cairo_line_to(cr, rect.x1, rect.y0);
 		cairo_line_to(cr, rect.x1, rect.y1);
@@ -537,6 +546,14 @@ resize_manual(GtkWidget *area, struct State *state) {
 	change_picture(area, tmp_pixbuf, state->cur_selection, orig_w, orig_h, state, FALSE);
 }
 
+static void
+switch_color(GtkWidget *area, struct State *state) {
+	if (state->cur_selection < 0 || !state->selections[state->cur_selection])
+		return;
+	state->cur_col = state->cur_col == 1 ? 2 : 1;
+	gtk_widget_queue_draw(area);
+}
+
 static gboolean
 key_press(GtkWidget *area, GdkEventKey *event, gpointer data) {
 	struct State *state = (struct State *)data;
@@ -556,6 +573,9 @@ key_press(GtkWidget *area, GdkEventKey *event, gpointer data) {
 	case GDK_KEY_space:
 		resize_manual(area, state);
 		break;
+	case GDK_KEY_Tab:
+		switch_color(area, state);
+		break;
 	}
 	return FALSE;
 }