[cairo-commit] goocanvas/docs creating-items.xml, 1.1, 1.2 goocanvas-sections.txt, 1.20, 1.21 model-view-canvas.xml, 1.1, 1.2 simple-canvas.xml, 1.1, 1.2

Damon Chaplin commit at pdx.freedesktop.org
Fri Feb 9 05:41:08 PST 2007


Committed by: damon

Update of /cvs/cairo/goocanvas/docs
In directory kemper:/tmp/cvs-serv1492/docs

Modified Files:
	creating-items.xml goocanvas-sections.txt 
	model-view-canvas.xml simple-canvas.xml 
Log Message:
2007-02-09  Damon Chaplin  <damon at gnome.org>

	* demo/demo-item.c (goo_demo_item_update): don't convert bounds to
	device space.

	* src/*.h: added padding to all *Class structs, to allow a bit of
	expansion without breaking backwards compatibility.

	* docs/*.xml: added example code to the introductory sections.
	Needs more explanation at some point.

	* configure.in: 
	* src/Makefile.am: added libtool version numbers.



Index: creating-items.xml
===================================================================
RCS file: /cvs/cairo/goocanvas/docs/creating-items.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- creating-items.xml	8 Feb 2007 22:56:45 -0000	1.1
+++ creating-items.xml	9 Feb 2007 13:41:03 -0000	1.2
@@ -16,7 +16,215 @@
   <refsect1 id="new-items-overview">
     <title>How to Create New Canvas Items</title>
     <para>
-      ... unfinished ...
+    Here's an example of how to create a new simple canvas item. (Note that
+    this item doesn't have a model-view split.)
+    </para>
+    <para>
+    Here's the header file - a fairly standard GObject header file.
+    Our item derives from #GooCanvasItemSimple and just has its own
+    x, y, width and height variables:
     </para>
+
+<informalexample><programlisting>
+  /&ast;
+   * GooCanvas Demo. Copyright (C) 2006 Damon Chaplin.
+   * Released under the GNU LGPL license. See COPYING for details.
+   *
+   * demo-item.c - a simple demo item.
+   &ast;/
+  &num;ifndef __GOO_DEMO_ITEM_H__
+  &num;define __GOO_DEMO_ITEM_H__
+  
+  &num;include &lt;gtk/gtk.h&gt;
+  &num;include "goocanvasitemsimple.h"
+  
+  G_BEGIN_DECLS
+  
+  
+  &num;define GOO_TYPE_DEMO_ITEM            (goo_demo_item_get_type&nbsp;())
+  &num;define GOO_DEMO_ITEM(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GOO_TYPE_DEMO_ITEM, GooDemoItem))
+  &num;define GOO_DEMO_ITEM_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GOO_TYPE_DEMO_ITEM, GooDemoItemClass))
+  &num;define GOO_IS_DEMO_ITEM(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GOO_TYPE_DEMO_ITEM))
+  &num;define GOO_IS_DEMO_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GOO_TYPE_DEMO_ITEM))
+  &num;define GOO_DEMO_ITEM_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GOO_TYPE_DEMO_ITEM, GooDemoItemClass))
+  
+  
+  typedef struct _GooDemoItem       GooDemoItem;
+  typedef struct _GooDemoItemClass  GooDemoItemClass;
+  
+  struct _GooDemoItem
+  {
+    GooCanvasItemSimple parent_object;
+  
+    gdouble x, y, width, height;
+  };
+  
+  struct _GooDemoItemClass
+  {
+    GooCanvasItemSimpleClass parent_class;
+  };
+  
+  
+  GType               goo_demo_item_get_type  (void) G_GNUC_CONST;
+  
+  GooCanvasItem*      goo_demo_item_new       (GooCanvasItem      *parent,
+                                               gdouble             x,
+                                               gdouble             y,
+                                               gdouble             width,
+                                               gdouble             height,
+                                               ...);
+  
+  
+  G_END_DECLS
+  
+  &num;endif /&ast; __GOO_DEMO_ITEM_H__ &ast;/
+ </programlisting></informalexample>
+
+<para>
+And here's the source file:
+</para>
+
+<informalexample><programlisting>
+  /&ast;
+   * GooCanvas Demo. Copyright (C) 2006 Damon Chaplin.
+   * Released under the GNU LGPL license. See COPYING for details.
+   *
+   * demo-item.c - a simple demo item.
+   &ast;/
+  &num;include &lt;config.h&gt;
+  &num;include "goocanvas.h"
+  &num;include "demo-item.h"
+  
+  
+  /&ast; Use the GLib convenience macro to define the type. GooDemoItem is the
+     class struct, goo_demo_item is the function prefix, and our class is a
+     subclass of GOO_TYPE_CANVAS_ITEM_SIMPLE. &ast;/
+  G_DEFINE_TYPE (GooDemoItem, goo_demo_item, GOO_TYPE_CANVAS_ITEM_SIMPLE)
+  
+  
+  /&ast; The standard object initialization function. &ast;/
+  static void
+  goo_demo_item_init (GooDemoItem *demo_item)
+  {
+    demo_item-&gt;x = 0.0;
+    demo_item-&gt;y = 0.0;
+    demo_item-&gt;width = 0.0;
+    demo_item-&gt;height = 0.0;
+  }
+  
+  
+  /&ast; The convenience function to create new items. This should start with a 
+     parent argument and end with a variable list of object properties to fit
+     in with the standard canvas items. &ast;/
+  GooCanvasItem*
+  goo_demo_item_new (GooCanvasItem      *parent,
+                     gdouble             x,
+                     gdouble             y,
+                     gdouble             width,
+                     gdouble             height,
+                     ...)
+  {
+    GooCanvasItem *item;
+    GooDemoItem *demo_item;
+    const char *first_property;
+    va_list var_args;
+  
+    item = g_object_new (GOO_TYPE_DEMO_ITEM, NULL);
+  
+    demo_item = (GooDemoItem*) item;
+    demo_item-&gt;x = x;
+    demo_item-&gt;y = y;
+    demo_item-&gt;width = width;
+    demo_item-&gt;height = height;
+  
+    va_start (var_args, height);
+    first_property = va_arg (var_args, char*);
+    if (first_property)
+      g_object_set_valist ((GObject*) item, first_property, var_args);
+    va_end (var_args);
+  
+    if (parent)
+      {
+        goo_canvas_item_add_child (parent, item, -1);
+        g_object_unref (item);
+      }
+  
+    return item;
+  }
+  
+  
+  /&ast; The update method. This is called when the canvas is initially shown and
+     also whenever the object is updated and needs to change its size and/or
+     shape. It should calculate its new bounds in its own coordinate space,
+     storing them in simple-&gt;bounds. &ast;/
+  static void
+  goo_demo_item_update  (GooCanvasItemSimple *simple,
+                         cairo_t             *cr)
+  {
+    GooDemoItem *demo_item = (GooDemoItem*) simple;
+  
+    /&ast; Compute the new bounds. &ast;/
+    simple-&gt;bounds.x1 = demo_item-&gt;x;
+    simple-&gt;bounds.y1 = demo_item-&gt;y;
+    simple-&gt;bounds.x2 = demo_item-&gt;x + demo_item-&gt;width;
+    simple-&gt;bounds.y2 = demo_item-&gt;y + demo_item-&gt;height;
+  }
+  
+  
+  /&ast; The paint method. This should draw the item on the given cairo_t, using
+     the item's own coordinate space. &ast;/
+  static void
+  goo_demo_item_paint (GooCanvasItemSimple *simple,
+                       cairo_t             *cr,
+                       GooCanvasBounds     *bounds)
+  {
+    GooDemoItem *demo_item = (GooDemoItem*) simple;
+  
+    cairo_move_to (cr, demo_item-&gt;x, demo_item-&gt;y);
+    cairo_line_to (cr, demo_item-&gt;x, demo_item-&gt;y + demo_item-&gt;height);
+    cairo_line_to (cr, demo_item-&gt;x + demo_item-&gt;width,
+                   demo_item-&gt;y + demo_item-&gt;height);
+    cairo_line_to (cr, demo_item-&gt;x + demo_item-&gt;width, demo_item-&gt;y);
+    cairo_close_path (cr);
+    goo_canvas_style_set_fill_options (simple-&gt;simple_data-&gt;style, cr);
+    cairo_fill (cr);
+  }
+  
+  
+  /&ast; Hit detection. This should check if the given coordinate (in the item's
+     coordinate space) is within the item. If it is it should return the item,
+     otherwise it should return NULL. &ast;/
+  static GooCanvasItem*
+  goo_demo_item_get_item_at (GooCanvasItemSimple *simple,
+                             gdouble              x,
+                             gdouble              y,
+                             cairo_t             *cr,
+                             gboolean             is_pointer_event)
+  {
+    GooDemoItem *demo_item = (GooDemoItem*) simple;
+  
+    if (x &lt; demo_item-&gt;x || (x &gt; demo_item-&gt;x + demo_item-&gt;width)
+        || y &lt; demo_item-&gt;y || (y &gt; demo_item-&gt;y + demo_item-&gt;height))
+      return NULL;
+  
+    return (GooCanvasItem*) simple;
+  }
+  
+  
+  /&ast; The class initialization function. Here we set the class' update(), paint()
+     and get_item_at() methods. &ast;/
+  static void
+  goo_demo_item_class_init (GooDemoItemClass *klass)
+  {
+    GooCanvasItemSimpleClass *simple_class = (GooCanvasItemSimpleClass*) klass;
+  
+    simple_class-&gt;update        = goo_demo_item_update;
+    simple_class-&gt;paint         = goo_demo_item_paint;
+    simple_class-&gt;get_item_at   = goo_demo_item_get_item_at;
+  }
+  
+  
+ </programlisting></informalexample>
+
   </refsect1>
 </refentry>

Index: goocanvas-sections.txt
===================================================================
RCS file: /cvs/cairo/goocanvas/docs/goocanvas-sections.txt,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- goocanvas-sections.txt	8 Feb 2007 17:54:25 -0000	1.20
+++ goocanvas-sections.txt	9 Feb 2007 13:41:03 -0000	1.21
@@ -112,6 +112,7 @@
 <SUBSECTION>
 goo_canvas_item_get_transform
 goo_canvas_item_set_transform
+goo_canvas_item_set_simple_transform
 goo_canvas_item_translate
 goo_canvas_item_scale
 goo_canvas_item_rotate
@@ -182,6 +183,7 @@
 <SUBSECTION>
 goo_canvas_item_model_get_transform
 goo_canvas_item_model_set_transform
+goo_canvas_item_model_set_simple_transform
 goo_canvas_item_model_translate
 goo_canvas_item_model_scale
 goo_canvas_item_model_rotate

Index: model-view-canvas.xml
===================================================================
RCS file: /cvs/cairo/goocanvas/docs/model-view-canvas.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- model-view-canvas.xml	8 Feb 2007 22:56:45 -0000	1.1
+++ model-view-canvas.xml	9 Feb 2007 13:41:03 -0000	1.2
@@ -16,7 +16,108 @@
   <refsect1 id="model-view-overview">
     <title>Model View Canvas Example</title>
     <para>
-      ... unfinished ...
+    Here's a complete example application that creates a model-view #GooCanvas
+    with a rectangle and a text item in it.
     </para>
+
+<informalexample><programlisting>
+  &num;include &lt;stdlib.h&gt;
+  &num;include &lt;goocanvas.h&gt;
+  
+  
+  static gboolean on_rect_button_press (GooCanvasItem  *view,
+                                        GooCanvasItem  *target,
+                                        GdkEventButton *event,
+                                        gpointer        data);
+  
+  static gboolean on_delete_event      (GtkWidget      *window,
+                                        GdkEvent       *event,
+                                        gpointer        unused_data);
+  
+  
+  int
+  main (int argc, char *argv[])
+  {
+    GtkWidget *window, *scrolled_win, *canvas;
+    GooCanvasItemModel *root, *rect_model, *text_model;
+    GooCanvasItem *rect_item;
+  
+    /&ast; Initialize GTK+. &ast;/
+    gtk_set_locale&nbsp;();
+    gtk_init (&amp;argc, &amp;argv);
+  
+    /&ast; Create the window and widgets. &ast;/
+    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", (GtkSignalFunc) 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&nbsp;();
+    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_group_model_new (NULL, NULL);
+  
+    /&ast; Add a few simple items. &ast;/
+    rect_model = goo_canvas_rect_model_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_model = goo_canvas_text_model_new (root, "Hello World", 300, 300, -1,
+                                            GTK_ANCHOR_CENTER,
+                                            "font", "Sans 24",
+                                            NULL);
+    goo_canvas_item_model_rotate (text_model, 45, 300, 300);
+  
+    goo_canvas_set_root_item_model (GOO_CANVAS (canvas), root);
+  
+    /&ast; Connect a signal handler for the rectangle item. &ast;/
+    rect_item = goo_canvas_get_item (GOO_CANVAS (canvas), rect_model);
+    g_signal_connect (rect_item, "button_press_event",
+                      (GtkSignalFunc) on_rect_button_press, NULL);
+  
+    /&ast; Pass control to the GTK+ main event loop. &ast;/
+    gtk_main&nbsp;();
+  
+    return 0;
+  }
+  
+  
+  /&ast; This handles button presses in item views. We simply output a message to
+     the console. &ast;/
+  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;
+  }
+  
+  
+  /&ast; This is our handler for the "delete-event" signal of the window, which
+     is emitted when the 'x' close button is clicked. We just exit here. &ast;/
+  static gboolean
+  on_delete_event (GtkWidget *window,
+                   GdkEvent  *event,
+                   gpointer   unused_data)
+  {
+    exit (0);
+  }
+ </programlisting></informalexample>
   </refsect1>
 </refentry>

Index: simple-canvas.xml
===================================================================
RCS file: /cvs/cairo/goocanvas/docs/simple-canvas.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- simple-canvas.xml	8 Feb 2007 22:56:45 -0000	1.1
+++ simple-canvas.xml	9 Feb 2007 13:41:03 -0000	1.2
@@ -16,7 +16,106 @@
   <refsect1 id="simple-canvas-example">
     <title>Simple Canvas Example</title>
     <para>
-      ... unfinished ...
+    Here's a complete example application that creates a #GooCanvas with a
+    rectangle and a text item in it:
     </para>
+
+<informalexample><programlisting>
+  &num;include &lt;stdlib.h&gt;
+  &num;include &lt;goocanvas.h&gt;
+  
+  
+  static gboolean on_rect_button_press (GooCanvasItem  *view,
+                                        GooCanvasItem  *target,
+                                        GdkEventButton *event,
+                                        gpointer        data);
+  
+  static gboolean on_delete_event      (GtkWidget      *window,
+                                        GdkEvent       *event,
+                                        gpointer        unused_data);
+  
+  
+  int
+  main (int argc, char *argv[])
+  {
+    GtkWidget *window, *scrolled_win, *canvas;
+    GooCanvasItem *root, *rect_item, *text_item;
+  
+    /&ast; Initialize GTK+. &ast;/
+    gtk_set_locale&nbsp;();
+    gtk_init (&amp;argc, &amp;argv);
+  
+    /&ast; Create the window and widgets. &ast;/
+    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", (GtkSignalFunc) 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&nbsp;();
+    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));
+  
+    /&ast; Add a few simple items. &ast;/
+    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,
+                                     GTK_ANCHOR_CENTER,
+                                     "font", "Sans 24",
+                                     NULL);
+    goo_canvas_item_rotate (text_item, 45, 300, 300);
+  
+    /&ast; Connect a signal handler for the rectangle item. &ast;/
+    g_signal_connect (rect_item, "button_press_event",
+                      (GtkSignalFunc) on_rect_button_press, NULL);
+  
+    /&ast; Pass control to the GTK+ main event loop. &ast;/
+    gtk_main&nbsp;();
+  
+    return 0;
+  }
+  
+  
+  /&ast; This handles button presses in item views. We simply output a message to
+     the console. &ast;/
+  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;
+  }
+  
+  
+  /&ast; This is our handler for the "delete-event" signal of the window, which
+     is emitted when the 'x' close button is clicked. We just exit here. &ast;/
+  static gboolean
+  on_delete_event (GtkWidget *window,
+                   GdkEvent  *event,
+                   gpointer   unused_data)
+  {
+    exit (0);
+  }
+ </programlisting></informalexample>
+
+
   </refsect1>
 </refentry>



More information about the cairo-commit mailing list