Example 8-2. Creating random items in the canvas
Here we define the callback function
add_object_clicked() that is used by
the "Add an object" button. When the user presses this
button, a rectangle or an ellipse will be created using
random coordinates.
/* Prototype for the item event handler */
static gint item_event (GnomeCanvasItem *item, GdkEvent *event, gpointer data);
/* Callback for the clicked signal of the Add object button. It creates a rectangle or an ellipse
* at a random position. The canvas comes in the user data pointer.
*/
static void
add_object_clicked (GtkWidget *button, gpointer data)
{
GnomeCanvas *canvas;
GnomeCanvasItem *item;
guint type;
int x1, y1, x2, y2;
int tmp;
canvas = GNOME_CANVAS (data);
/* Compute some random coordinates, with the condition that (x1 <= x2) and (y1 <= y2), and
* ensure that the objects are not too small.
*/
x1 = rand () % CANVAS_SIZE;
y1 = rand () % CANVAS_SIZE;
x2 = rand () % CANVAS_SIZE;
y2 = rand () % CANVAS_SIZE;
if (x1 > x2) {
tmp = x1;
x1 = x2;
x2 = tmp;
}
if (y1 > y2) {
tmp = y1;
y1 = y2;
y2 = tmp;
}
if ((x2 - x1) < 10)
x2 += 10;
if ((y2 - y1) < 10)
y2 += 10;
/* Pick a type for the item randomly */
if (rand () & 1)
type = gnome_canvas_rect_get_type ();
else
type = gnome_canvas_ellipse_get_type ();
/* Create the item and make it white with black outline by default. Also,
* connect to its event signal so that we can know when events get to the
* item.*/
item = gnome_canvas_item_new (gnome_canvas_root (canvas),
type,
"x1", (double) x1,
"y1", (double) y1,
"x2", (double) x2,
"y2", (double) y2,
"fill_color", "white",
"outline_color", "black",
"width_units", 1.0,
NULL);
gtk_signal_connect (GTK_OBJECT (item), "event",
(GtkSignalFunc) item_event,
NULL);
}
In this function, first we compute some random coordinates
for the corners of the rectangle or ellipse that we will
create. We have to ensure that x1 and
y1 are less than or equal to
x2 and y2, respectively.
New items are created and inserted into the canvas using the
gnome_canvas_item_new() function. The
first parameter to this function specifies the canvas item
group that will act as the new item's parent. For this
simple example, we insert all the items inside the toplevel
canvas group, the root item, which we obtain by calling the
gnome_canvas_root() function.
The second parameter to
gnome_canvas_item_new() specifies the
type of item we want to create. This is simply the Gtk type
identifier used by the class of the item you want to create.
We pick randomly between
gnome_canvas_rect_get_type() and
gnome_canvas_ellipse_get_type(), and
then we pass this value to the
gnome_canvas_item_new() function.
The following parameters are optional, and are a list of
key/value pairs that specify which arguments to set for that
particular item. Internally these are handled using the Gtk
object argument system. The simplest way of using these is,
for each argument you want to set, pass a string with the
name of the argument, and then the value you want to set for
that argument.
 | You must pass in values with the
correct type! Remember that the C compiler cannot warn
you about incorrect types when you are using a variable
argument list.
In this example, we must use doubles for the coordinates
of the corners of the rectangle or ellipse. Colors are
passed using a string with a valid X color
specification, and the width in units is passed as a
double.
Please look at the reference part of the
GnomeCanvas documentation for detailed
information on the argument types supported by each
canvas item.
|
We pass NULL as the last argument to indicate that we have
no more arguments to set for this item.
Finally, we connect to the "event" signal of the item so
that we can be notified when the item receives events from
the mouse.