Canvas is the main widget containing a number of canvas items.
Here is a simple example:
<informalexample><programlisting> #include <goocanvas.h>
static gboolean on_rect_button_press (GooCanvasItem *view, GooCanvasItem *target, GdkEventButton *event, gpointer data);
int main (int argc, char *argv[]) { GtkWidget *window, *scrolled_win, *canvas; GooCanvasItem *root, *rect_item, *text_item;
/* Initialize GTK+. */ gtk_init (&argc, &argv);
/* Create the window and widgets. */ window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_default_size (
GTK_WINDOW (window), 640, 600); gtk_widget_show (window); g_signal_connect (window, "delete_event", G_CALLBACK (on_delete_event), NULL)
;
scrolled_win = gtk_scrolled_window_new (NULL, NULL); gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
GTK_SHADOW_IN); gtk_widget_show (scrolled_win); gtk_container_add (GTK_CONTAINER (window), scrolled_win);
canvas = goo_canvas_new (); gtk_widget_set_size_request (canvas, 600, 450); goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0,
1000, 1000); gtk_widget_show (canvas); gtk_container_add (GTK_CONTAINER (scrolled_win), canvas);
root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
/* Add a few simple items. */ rect_item = goo_canvas_rect_new (root, 100, 100, 400, 400, "line-width", 10.0,
"radius-x", 20.0, "radius-y", 10.0, "stroke-color", "yellow", "fill-color", "red", NULL);
text_item = goo_canvas_text_new (root, "Hello World", 300, 300, -1, GOO_CANVAS_ANCHOR_CENTER, "font", "Sans 24", NULL);
goo_canvas_item_rotate (text_item, 45, 300, 300);
/* Connect a signal handler for the rectangle item. */ g_signal_connect (rect_item, "button_press_event", G_CALLBACK (
on_rect_button_press), NULL);
/* Pass control to the GTK+ main event loop. */ gtk_main ();
return 0; }
/* This handles button presses in item views. We simply output a message to the console. */ static gboolean
on_rect_button_press (GooCanvasItem *item, GooCanvasItem *target, GdkEventButton *event, gpointer data) { g_print ("rect item
received button press event\n"); return TRUE; }
</programlisting></informalexample>
A Canvas widget is usually placed inside a ScrolledWindow widget and can be
scrolled with the scrollbar or with the scroll wheel on a mouse. To disable mouse wheel scrolling, do this: <informalexample><
programlisting> g_signal_connect (canvas, "scroll-event", G_CALLBACK (gtk_true), NULL); </programlisting></informalexample
>
- public void convert_bounds_to_item_space (CanvasItem item, ref CanvasBounds bounds)
Converts the given bounds in the canvas coordinate space to a bounding
box in item space.
- public void convert_from_item_space (CanvasItem item, ref double x, ref double y)
Converts a coordinate from the given item's coordinate space to the
canvas coordinate space, applying all transformation matrices including the item's own transformation matrix, if it has one.
- public void convert_from_pixels (ref double x, ref double y)
Converts a coordinate from pixels to the canvas coordinate space.
- public void convert_to_item_space (CanvasItem item, ref double x, ref double y)
Converts a coordinate from the canvas coordinate space to the given
item's coordinate space, applying all transformation matrices including the item's own transformation matrix, if it has one.
- public void convert_to_pixels (ref double x, ref double y)
Converts a coordinate from the canvas coordinate space to pixels.
- public void convert_units_from_pixels (ref double x, ref double y)
Converts a coordinate from pixels to the canvas's units, ignoring
scaling and ignoring the coordinate space specified in the call to set_bounds.
- public void convert_units_to_pixels (ref double x, ref double y)
Converts a coordinate from the canvas's units to pixels, ignoring
scaling and ignoring the coordinate space specified in the call to set_bounds.
- public Context create_cairo_context ()
Creates a cairo context, initialized with the default canvas settings.
- public virtual CanvasItem create_item (CanvasItemModel model)
This function is only intended to be used when implementing new canvas
items, typically container items such as CanvasGroup.
- public void get_bounds (out double left, out double top, out double right, out double bottom)
Gets the bounds of the canvas, in canvas units.
- public double get_default_line_width ()
Gets the default line width, which depends on the current units
setting.
- public unowned CanvasItem get_item (CanvasItemModel model)
- public unowned CanvasItem get_item_at (double x, double y, bool is_pointer_event)
Gets the item at the given point.
- public List<unowned CanvasItem> get_items_at (double x, double y, bool is_pointer_event)
Gets all items at the given point.
- public List<unowned CanvasItem> get_items_in_area (CanvasBounds area, bool inside_area, bool allow_overlaps, bool include_containers)
Gets a list of items inside or outside a given area.
- public unowned CanvasItem get_root_item ()
Gets the root item of the canvas, usually a
CanvasGroup.
- public unowned CanvasItemModel get_root_item_model ()
Gets the root item model of the canvas.
- public double get_scale ()
Gets the current scale of the canvas.
- public unowned CanvasItem get_static_root_item ()
Gets the static root item of the canvas.
- public unowned CanvasItemModel get_static_root_item_model ()
Gets the static root item model of the canvas.
- public void grab_focus (CanvasItem item)
Grabs the keyboard focus for the given item.
- public GrabStatus keyboard_grab (CanvasItem item, bool owner_events, uint32 time)
Attempts to grab the keyboard for the given item.
- public void keyboard_ungrab (CanvasItem item, uint32 time)
Ungrabs the keyboard, if the given item has the keyboard grab.
- public GrabStatus pointer_grab (CanvasItem item, EventMask event_mask, Cursor? cursor, uint32 time)
Attempts to grab the pointer for the given item.
- public void pointer_ungrab (CanvasItem item, uint32 time)
Ungrabs the pointer, if the given item has the pointer grab.
- public void register_widget_item (CanvasWidget witem)
This function should only be used by
CanvasWidget and subclass implementations.
- public void render (Context cr, CanvasBounds? bounds, double scale)
Renders all or part of a canvas to the given cairo context.
- public void request_item_redraw (CanvasBounds bounds, bool is_static)
This function is only intended to be used by subclasses of
Canvas or CanvasItem implementations.
- public void request_redraw (CanvasBounds bounds)
This function is only intended to be used by subclasses of
Canvas or CanvasItem implementations.
- public void request_update ()
This function is only intended to be used by subclasses of
Canvas or CanvasItem implementations.
- public void scroll_to (double left, double top)
Scrolls the canvas, placing the given point as close to the top-left
of the view as possible.
- public void set_bounds (double left, double top, double right, double bottom)
Sets the bounds of the Canvas, in canvas
units.
- public void set_root_item (CanvasItem item)
Sets the root item of the canvas.
- public void set_root_item_model (CanvasItemModel model)
Sets the root item model of the canvas.
- public void set_scale (double scale)
Sets the scale of the canvas.
- public void set_static_root_item (CanvasItem item)
Sets the static root item.
- public void set_static_root_item_model (CanvasItemModel model)
Sets the static root item model.
- public void unregister_item (CanvasItemModel model)
This function is only intended to be used when implementing new canvas
items.
- public void unregister_widget_item (CanvasWidget witem)
This function should only be used by
CanvasWidget and subclass implementations.
- public void update ()
This function is only intended to be used by subclasses of
Canvas or CanvasItem implementations.