gnome-canvas-util.c 4.41 KB
Newer Older
1 2 3 4 5 6
/*
 * Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation
 * All rights reserved.
 *
 * This file is part of the Gnome Library.
 *
7 8 9
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * published by the Free Software Foundation; either the
10
 *
11 12 13 14
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17 18 19 20 21 22 23
 * License along with the Gnome Library; see the file COPYING.LIB.  If not,
 */
/*
  @NOTATION@
 */
/* Miscellaneous utility functions for the GnomeCanvas widget
 *
24 25 26
 * GnomeCanvas is basically a port of the Tk toolkit's most excellent canvas
 * widget.  Tk is copyrighted by the Regents of the University of California,
 * Sun Microsystems, and other parties.
27 28 29 30 31
 *
 *
 * Author: Federico Mena <federico@nuclecu.unam.mx>
 */

32
#ifdef HAVE_CONFIG_H
33
#include <config.h>
34
#endif
35 36

/* needed for M_PI_2 under 'gcc -ansi -predantic' on GNU/Linux */
37 38
#ifndef _DEFAULT_SOURCE
#  define _DEFAULT_SOURCE 1
39 40 41 42 43 44 45 46 47 48 49 50
#endif
#include <sys/types.h>

#include <math.h>
#include "gnome-canvas.h"
#include "gnome-canvas-util.h"

/* Here are some helper functions for aa rendering: */

/**
 * gnome_canvas_item_reset_bounds:
 * @item: A canvas item
51
 *
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
 * Resets the bounding box of a canvas item to an empty rectangle.
 **/
void
gnome_canvas_item_reset_bounds (GnomeCanvasItem *item)
{
	item->x1 = 0.0;
	item->y1 = 0.0;
	item->x2 = 0.0;
	item->y2 = 0.0;
}

/**
 * gnome_canvas_update_bbox:
 * @item: the canvas item needing update
 * @x1: Left coordinate of the new bounding box
 * @y1: Top coordinate of the new bounding box
 * @x2: Right coordinate of the new bounding box
 * @y2: Bottom coordinate of the new bounding box
 *
 * Sets the bbox to the new value, requesting full repaint.
 **/
void
74 75 76 77 78
gnome_canvas_update_bbox (GnomeCanvasItem *item,
                          gint x1,
                          gint y1,
                          gint x2,
                          gint y2)
79
{
80 81 82
	gnome_canvas_request_redraw (
		item->canvas, item->x1, item->y1, item->x2, item->y2);

83 84 85 86
	item->x1 = x1;
	item->y1 = y1;
	item->x2 = x2;
	item->y2 = y2;
87 88 89

	gnome_canvas_request_redraw (
		item->canvas, item->x1, item->y1, item->x2, item->y2);
90 91
}

92 93 94 95 96 97 98 99 100 101 102
/**
 * gnome_canvas_cairo_create_scratch:
 *
 * Create a scratch #cairo_t. This is useful for measuring purposes or
 * calling functions like cairo_in_fill().
 *
 * Returns: A new cairo_t. Destroy with cairo_destroy() after use.
 **/
cairo_t *
gnome_canvas_cairo_create_scratch (void)
{
103 104
	cairo_surface_t *surface;
	cairo_t *cr;
105

106 107 108
	surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
	cr = cairo_create (surface);
	cairo_surface_destroy (surface);
109

110
	return cr;
111
}
112 113 114 115 116 117 118 119 120 121 122 123 124 125

/**
 * gnome_canvas_matrix_transform_rect:
 * @matrix: a cairo matrix
 * @x1: x coordinate of top left position of rectangle (in-out)
 * @y1: y coordinate of top left position of rectangle (in-out)
 * @x2: x coordinate of bottom right position of rectangle (in-out)
 * @y2: y coordinate of bottom right position of rectangle (in-out)
 *
 * Computes the smallest rectangle containing the whole area of the given
 * rectangle after applying the transformation given in @matrix.
 **/
void
gnome_canvas_matrix_transform_rect (const cairo_matrix_t *matrix,
126 127 128 129
                                    gdouble *x1,
                                    gdouble *y1,
                                    gdouble *x2,
                                    gdouble *y2)
130
{
131 132
	gdouble maxx, maxy, minx, miny;
	gdouble tmpx, tmpy;
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

	tmpx = *x1;
	tmpy = *y1;
	cairo_matrix_transform_point (matrix, &tmpx, &tmpy);
	minx = maxx = tmpx;
	miny = maxy = tmpy;

	tmpx = *x2;
	tmpy = *y1;
	cairo_matrix_transform_point (matrix, &tmpx, &tmpy);
	minx = MIN (minx, tmpx);
	maxx = MAX (maxx, tmpx);
	miny = MIN (miny, tmpy);
	maxy = MAX (maxy, tmpy);

	tmpx = *x2;
	tmpy = *y2;
	cairo_matrix_transform_point (matrix, &tmpx, &tmpy);
	minx = MIN (minx, tmpx);
	maxx = MAX (maxx, tmpx);
	miny = MIN (miny, tmpy);
	maxy = MAX (maxy, tmpy);

	tmpx = *x1;
	tmpy = *y2;
	cairo_matrix_transform_point (matrix, &tmpx, &tmpy);
	minx = MIN (minx, tmpx);
	maxx = MAX (maxx, tmpx);
	miny = MIN (miny, tmpy);
	maxy = MAX (maxy, tmpy);
163 164 165 166 167 168 169

        *x1 = minx;
        *x2 = maxx;
        *y1 = miny;
        *y2 = maxy;
}