/**
 * press_inside_image_area:
 * @preview: an #PhotosPrintPreview
 * @x: the points x coordinate
 * @y: the points y coordinate
 *
 * Returns whether the given point is inside the image area.
 *
 * Returns: %TRUE if the given point is inside of the image area,
 * %FALSE otherwise.
 **/
static gboolean
press_inside_image_area (PhotosPrintPreview *preview, guint x, guint y)
{
  PhotosPrintPreviewPrivate *priv;
  const gint xs = (gint) x;
  const gint ys = (gint) y;
  gint x0;
  gint y0;

  priv = preview->priv;
  get_current_image_coordinates (preview, &x0, &y0);

  if (xs >= x0 &&  ys >= y0 && xs <= x0 + priv->r_width && ys <= y0 + priv->r_height)
    return TRUE;

  return FALSE;
}
/**
 * press_inside_image_area:
 * @preview: an #EomPrintPreview
 * @x: the points x coordinate
 * @y: the points y coordinate
 *
 * Returns whether the given point is inside the image area.
 *
 * Returns: %TRUE if the given point is inside of the image area,
 * %FALSE otherwise.
 **/
static gboolean
press_inside_image_area (EomPrintPreview *preview,
			 guint x,
			 guint y)
{
	EomPrintPreviewPrivate *priv;
	gint x0, y0;

	priv = preview->priv;
	get_current_image_coordinates (preview, &x0, &y0);

	if (x >= x0 &&  y >= y0 &&
	    x <= x0 + priv->r_width && y <= y0 + priv->r_height)
		return TRUE;

	return FALSE;
}
static void
photos_print_preview_draw (PhotosPrintPreview *preview, cairo_t *cr)
{
	PhotosPrintPreviewPrivate *priv;
	GtkWidget *area;
	GtkAllocation allocation;
	gint x0, y0;
	gboolean has_focus;

	priv = preview->priv;
	area = priv->area;

	has_focus = gtk_widget_has_focus (area);

	gtk_widget_get_allocation (area, &allocation);

	/* draw the page */
	cairo_set_source_rgb (cr, 1., 1., 1.);
 	cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
 	cairo_fill (cr);

	/* draw the page margins */
	cairo_set_source_rgb (cr, 0., 0., 0.);
	cairo_set_line_width (cr, 0.1);
	cairo_rectangle (cr,
			 priv->l_rmargin, priv->t_rmargin,
			 allocation.width - priv->l_rmargin - priv->r_rmargin,
			 allocation.height - priv->t_rmargin - priv->b_rmargin);
	cairo_stroke (cr);

	get_current_image_coordinates (preview, &x0, &y0);

	if (priv->flag_create_surface) {
		create_surface (preview);
	}

	if (priv->surface) {
		cairo_set_source_surface (cr, priv->surface, x0, y0);
		cairo_paint (cr);
	} else if (priv->pixbuf_scaled) {
		/* just in the remote case we don't have the surface */

		/* adjust (x0, y0) to the new scale */
		gdouble scale = priv->i_scale * priv->p_scale *
			gdk_pixbuf_get_width (priv->pixbuf) / gdk_pixbuf_get_width (priv->pixbuf_scaled);
		x0 /= scale;
		y0 /= scale;

		cairo_scale (cr, scale, scale);
		gdk_cairo_set_source_pixbuf (cr, priv->pixbuf_scaled, x0, y0);
		cairo_paint (cr);
	} else if (priv->pixbuf) {
		/* just in the remote case we don't have the surface */

		/* adjust (x0, y0) to the new scale */
		x0 /=  priv->i_scale * priv->p_scale;
		y0 /=  priv->i_scale * priv->p_scale;

		cairo_scale (cr, priv->i_scale*priv->p_scale, priv->i_scale*priv->p_scale);
		gdk_cairo_set_source_pixbuf (cr, priv->pixbuf, x0, y0);
		cairo_paint (cr);
	}

	if (has_focus) {
		GtkStyleContext *ctx;

		ctx = gtk_widget_get_style_context (area);
		gtk_render_focus (ctx, cr, x0, y0,
				  priv->r_width, priv->r_height);
	}
}
static void
eom_print_preview_draw (EomPrintPreview *preview, cairo_t *cr)
{
	EomPrintPreviewPrivate *priv;
	GtkWidget *area;
	GtkAllocation allocation;
	gint x0, y0;
	GtkStyle *style;
	gboolean has_focus;

	priv = preview->priv;
	area = priv->area;

	has_focus = gtk_widget_has_focus (area);

	style = gtk_widget_get_style (area);

	gtk_widget_get_allocation (area, &allocation);

	/* draw the page */
 	gdk_cairo_set_source_color (cr, &style->white);
 	cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
 	cairo_fill (cr);

	/* draw the page margins */
	gdk_cairo_set_source_color (cr, &style->black);
	cairo_set_line_width (cr, 0.1);
	cairo_rectangle (cr,
			 priv->l_rmargin, priv->t_rmargin,
			 allocation.width - priv->l_rmargin - priv->r_rmargin,
			 allocation.height - priv->t_rmargin - priv->b_rmargin);
	cairo_stroke (cr);

	get_current_image_coordinates (preview, &x0, &y0);

	if (priv->flag_create_surface) {
		create_surface (preview);
	}

	if (priv->surface) {
		cairo_set_source_surface (cr, priv->surface, x0, y0);
		cairo_paint (cr);
	} else if (priv->image_scaled) {
		/* just in the remote case we don't have the surface */

		/* adjust (x0, y0) to the new scale */
		gdouble scale = priv->i_scale * priv->p_scale *
			gdk_pixbuf_get_width (priv->image) / gdk_pixbuf_get_width (priv->image_scaled);
		x0 /= scale;
		y0 /= scale;

		cairo_scale (cr, scale, scale);
		gdk_cairo_set_source_pixbuf (cr, priv->image_scaled, x0, y0);
		cairo_paint (cr);
	} else if (priv->image) {
		/* just in the remote case we don't have the surface */

		/* adjust (x0, y0) to the new scale */
		x0 /=  priv->i_scale * priv->p_scale;
		y0 /=  priv->i_scale * priv->p_scale;

		cairo_scale (cr, priv->i_scale*priv->p_scale, priv->i_scale*priv->p_scale);
		gdk_cairo_set_source_pixbuf (cr, priv->image, x0, y0);
		cairo_paint (cr);
	}

	if (has_focus) {
		gtk_paint_focus (style, gtk_widget_get_window (area),
				 GTK_STATE_NORMAL, NULL, NULL, NULL,
				 0, 0, allocation.width, allocation.height);
	}
}