[gthumb] use the difference operator to draw the grid



commit 7c0b23f5949265e0cd8580d4d008e7b46f68c507
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Thu Aug 23 11:48:12 2012 +0200

    use the difference operator to draw the grid

 extensions/file_tools/gth-image-rotator.c |    8 +++
 gthumb/cairo-utils.c                      |   79 +++++++++++++-----------
 gthumb/gth-image-selector.c               |   94 ++++++++++++++++++++++++++--
 3 files changed, 137 insertions(+), 44 deletions(-)
---
diff --git a/extensions/file_tools/gth-image-rotator.c b/extensions/file_tools/gth-image-rotator.c
index 70b9e50..3319579 100644
--- a/extensions/file_tools/gth-image-rotator.c
+++ b/extensions/file_tools/gth-image-rotator.c
@@ -400,11 +400,19 @@ paint_point (GthImageRotator *self,
 {
 	double radius = 10.0;
 
+	cairo_save (cr);
+
+#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 9, 2)
+	cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
+#endif
+
 	cairo_move_to (cr, p->x - radius, p->y - radius);
 	cairo_line_to (cr, p->x + radius, p->y + radius);
 	cairo_move_to (cr, p->x - radius, p->y + radius);
 	cairo_line_to (cr, p->x + radius, p->y - radius);
 	cairo_stroke (cr);
+
+	cairo_restore (cr);
 }
 
 
diff --git a/gthumb/cairo-utils.c b/gthumb/cairo-utils.c
index 406334f..d635f5e 100644
--- a/gthumb/cairo-utils.c
+++ b/gthumb/cairo-utils.c
@@ -835,6 +835,7 @@ _cairo_draw_slide (cairo_t  *cr,
 #define GRID_STEP_1         10
 #define GRID_STEP_2         (GRID_STEP_1 * 5)
 #define GRID_STEP_3         (GRID_STEP_2 * 2)
+#define MAX_GRID_LINES      50
 
 
 void
@@ -854,7 +855,7 @@ _cairo_paint_grid (cairo_t               *cr,
 	cairo_clip (cr);
 
 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 9, 2)
-	/* cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE); */
+	cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
 #endif
 
 	cairo_rectangle (cr, rectangle->x + 0.5, rectangle->y + 0.5, rectangle->width - 0.5, rectangle->height - 0.5);
@@ -866,8 +867,6 @@ _cairo_paint_grid (cairo_t               *cr,
 		return;
 	}
 
-	cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
-
 	if (grid_type == GTH_GRID_THIRDS) {
 		int i;
 
@@ -962,44 +961,50 @@ _cairo_paint_grid (cairo_t               *cr,
 		int x;
 		int y;
 
-		cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.40);
-		for (x = GRID_STEP_3; x < rectangle->width; x += GRID_STEP_3) {
-			cairo_move_to (cr, rectangle->x + x + 0.5, rectangle->y + 1.5);
-			cairo_line_to (cr, rectangle->x + x + 0.5, rectangle->y + rectangle->height - 0.5);
-		}
-		for (y = GRID_STEP_3; y < rectangle->height; y += GRID_STEP_3) {
-			cairo_move_to (cr, rectangle->x + 1.5, rectangle->y + y + 0.5);
-			cairo_line_to (cr, rectangle->x + rectangle->width - 0.5, rectangle->y + y + 0.5);
+		if (rectangle->width / GRID_STEP_3 <= MAX_GRID_LINES) {
+			cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.40);
+			for (x = GRID_STEP_3; x < rectangle->width; x += GRID_STEP_3) {
+				cairo_move_to (cr, rectangle->x + x + 0.5, rectangle->y + 1.5);
+				cairo_line_to (cr, rectangle->x + x + 0.5, rectangle->y + rectangle->height - 0.5);
+			}
+			for (y = GRID_STEP_3; y < rectangle->height; y += GRID_STEP_3) {
+				cairo_move_to (cr, rectangle->x + 1.5, rectangle->y + y + 0.5);
+				cairo_line_to (cr, rectangle->x + rectangle->width - 0.5, rectangle->y + y + 0.5);
+			}
+			cairo_stroke (cr);
 		}
-		cairo_stroke (cr);
 
-		cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.20);
-		for (x = GRID_STEP_2; x < rectangle->width; x += GRID_STEP_2) {
-			if (x % GRID_STEP_3 == 0)
-				continue;
-			cairo_move_to (cr, rectangle->x + x + 0.5, rectangle->y + 1.5);
-			cairo_line_to (cr, rectangle->x + x + 0.5, rectangle->y + rectangle->height - 0.5);
-		}
-		for (y = GRID_STEP_2; y < rectangle->height; y += GRID_STEP_2) {
-			if (y % GRID_STEP_3 == 0)
-				continue;
-			cairo_move_to (cr, rectangle->x + 1.5, rectangle->y + y + 0.5);
-			cairo_line_to (cr, rectangle->x + rectangle->width - 0.5, rectangle->y + y + 0.5);
+		if (rectangle->width / GRID_STEP_2 <= MAX_GRID_LINES) {
+			cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.20);
+			for (x = GRID_STEP_2; x < rectangle->width; x += GRID_STEP_2) {
+				if (x % GRID_STEP_3 == 0)
+					continue;
+				cairo_move_to (cr, rectangle->x + x + 0.5, rectangle->y + 1.5);
+				cairo_line_to (cr, rectangle->x + x + 0.5, rectangle->y + rectangle->height - 0.5);
+			}
+			for (y = GRID_STEP_2; y < rectangle->height; y += GRID_STEP_2) {
+				if (y % GRID_STEP_3 == 0)
+					continue;
+				cairo_move_to (cr, rectangle->x + 1.5, rectangle->y + y + 0.5);
+				cairo_line_to (cr, rectangle->x + rectangle->width - 0.5, rectangle->y + y + 0.5);
+			}
+			cairo_stroke (cr);
 		}
-		cairo_stroke (cr);
 
-		cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.10);
-		for (x = GRID_STEP_1; x < rectangle->width; x += GRID_STEP_1) {
-			if (x % GRID_STEP_2 == 0)
-				continue;
-			cairo_move_to (cr, rectangle->x + x + 0.5, rectangle->y + 1.5);
-			cairo_line_to (cr, rectangle->x + x + 0.5, rectangle->y + rectangle->height - 0.5);
-		}
-		for (y = GRID_STEP_1; y < rectangle->height; y += GRID_STEP_1) {
-			if (y % GRID_STEP_2 == 0)
-				continue;
-			cairo_move_to (cr, rectangle->x + 1.5, rectangle->y + y + 0.5);
-			cairo_line_to (cr, rectangle->x + rectangle->width - 0.5, rectangle->y + y + 0.5);
+		if (rectangle->width / GRID_STEP_1 <= MAX_GRID_LINES) {
+			cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.10);
+			for (x = GRID_STEP_1; x < rectangle->width; x += GRID_STEP_1) {
+				if (x % GRID_STEP_2 == 0)
+					continue;
+				cairo_move_to (cr, rectangle->x + x + 0.5, rectangle->y + 1.5);
+				cairo_line_to (cr, rectangle->x + x + 0.5, rectangle->y + rectangle->height - 0.5);
+			}
+			for (y = GRID_STEP_1; y < rectangle->height; y += GRID_STEP_1) {
+				if (y % GRID_STEP_2 == 0)
+					continue;
+				cairo_move_to (cr, rectangle->x + 1.5, rectangle->y + y + 0.5);
+				cairo_line_to (cr, rectangle->x + rectangle->width - 0.5, rectangle->y + y + 0.5);
+			}
 		}
 		cairo_stroke (cr);
 	}
diff --git a/gthumb/gth-image-selector.c b/gthumb/gth-image-selector.c
index 9214432..3c4d7f0 100644
--- a/gthumb/gth-image-selector.c
+++ b/gthumb/gth-image-selector.c
@@ -347,6 +347,90 @@ get_event_area_from_id (GthImageSelector *self,
 }
 
 
+typedef enum {
+	SIDE_NONE = 0,
+	SIDE_TOP = 1 << 0,
+	SIDE_RIGHT = 1 << 1,
+	SIDE_BOTTOM = 1 << 2,
+	SIDE_LEFT = 1 << 3
+} Sides;
+
+
+static void
+_cairo_rectangle_partial (cairo_t *cr,
+			  double   x,
+			  double   y,
+			  double   w,
+			  double   h,
+			  Sides    sides)
+{
+	if (sides & SIDE_TOP) {
+		cairo_move_to (cr, x, y);
+		cairo_rel_line_to (cr, w, 0);
+	}
+
+	if (sides & SIDE_RIGHT) {
+		cairo_move_to (cr, x + w, y);
+		cairo_rel_line_to (cr, 0, h);
+	}
+
+	if (sides & SIDE_BOTTOM) {
+		cairo_move_to (cr, x, y + h);
+		cairo_rel_line_to (cr, w, 0);
+	}
+
+	if (sides & SIDE_LEFT) {
+		cairo_move_to (cr, x, y);
+		cairo_rel_line_to (cr, 0, h);
+	}
+}
+
+
+static void
+event_area_paint (GthImageSelector *self,
+		  EventArea        *event_area,
+		  cairo_t          *cr)
+{
+	Sides sides = SIDE_NONE;
+
+	switch (event_area->id) {
+	case C_SELECTION_AREA:
+		break;
+	case C_TOP_AREA:
+		sides = SIDE_RIGHT | SIDE_BOTTOM | SIDE_LEFT;
+		break;
+	case C_BOTTOM_AREA:
+		sides = SIDE_TOP | SIDE_RIGHT | SIDE_LEFT;
+		break;
+	case C_LEFT_AREA:
+		sides = SIDE_TOP | SIDE_RIGHT | SIDE_BOTTOM;
+		break;
+	case C_RIGHT_AREA:
+		sides = SIDE_TOP | SIDE_BOTTOM | SIDE_LEFT;
+		break;
+	case C_TOP_LEFT_AREA:
+		sides = SIDE_RIGHT | SIDE_BOTTOM;
+		break;
+	case C_TOP_RIGHT_AREA:
+		sides = SIDE_BOTTOM | SIDE_LEFT;
+		break;
+	case C_BOTTOM_LEFT_AREA:
+		sides = SIDE_TOP | SIDE_RIGHT;
+		break;
+	case C_BOTTOM_RIGHT_AREA:
+		sides = SIDE_TOP | SIDE_LEFT;
+		break;
+	}
+
+	_cairo_rectangle_partial (cr,
+				  event_area->area.x + self->priv->viewer->image_area.x - self->priv->viewer->x_offset + 0.5,
+				  event_area->area.y + self->priv->viewer->image_area.y - self->priv->viewer->y_offset + 0.5,
+				  event_area->area.width - 1.0,
+				  event_area->area.height - 1.0,
+				  sides);
+}
+
+
 /**/
 
 
@@ -660,16 +744,12 @@ paint_selection (GthImageSelector *self,
 
 	_cairo_paint_grid (cr, &selection_area, self->priv->grid_type);
 
-	cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
 	if ((self->priv->current_area != NULL) && (self->priv->current_area->id != C_SELECTION_AREA)) {
 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 9, 2)
-		/* cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE); */
+		cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
 #endif
-		cairo_rectangle (cr,
-				 self->priv->current_area->area.x + self->priv->viewer->image_area.x - self->priv->viewer->x_offset + 0.5,
-				 self->priv->current_area->area.y + self->priv->viewer->image_area.y - self->priv->viewer->y_offset + 0.5,
-				 self->priv->current_area->area.width - 1,
-				 self->priv->current_area->area.height - 1);
+		cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
+		event_area_paint (self, self->priv->current_area, cr);
 	}
 	cairo_stroke (cr);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]