[cairo-commit] goocanvas/demo Makefile.am, 1.6, 1.7 demo-item.c, NONE, 1.1 demo-item.h, NONE, 1.1 demo.c, 1.9, 1.10

Damon Chaplin commit at pdx.freedesktop.org
Wed Nov 29 12:45:01 PST 2006


Committed by: damon

Update of /cvs/cairo/goocanvas/demo
In directory kemper:/tmp/cvs-serv7947/demo

Modified Files:
	Makefile.am demo.c 
Added Files:
	demo-item.c demo-item.h 
Log Message:
2006-11-29  Damon Chaplin  <damon at gnome.org>

	* demo/demo-item.[hc]: new demo item to show how to create new items.
	There's more boilerplate code than I'd like, but I don't know the best
	way to deal with it.



Index: Makefile.am
===================================================================
RCS file: /cvs/cairo/goocanvas/demo/Makefile.am,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- Makefile.am	29 Nov 2006 18:47:02 -0000	1.6
+++ Makefile.am	29 Nov 2006 20:44:58 -0000	1.7
@@ -12,7 +12,7 @@
 demo_SOURCES = \
 	demo.c demo-fifteen.c demo-scalability.c demo-grabs.c \
 	demo-arrowhead.c demo-features.c demo-events.c \
-	demo-paths.c demo-focus.c
+	demo-paths.c demo-focus.c demo-item.c
 
 demo_LDADD = $(top_builddir)/src/libgoocanvas.la @PACKAGE_LIBS@ $(INTLLIBS)
 

--- NEW FILE: demo-item.c ---
/*
 * GooCanvas Demo. Copyright (C) 2006 Damon Chaplin.
 * Released under the GNU LGPL license. See COPYING for details.
 *
 * demo-item.c - a simple demo item.
 */
#include <config.h>
#include "goocanvas.h"
#include "demo-item.h"

static void canvas_item_interface_init    (GooCanvasItemIface *iface);

G_DEFINE_TYPE_WITH_CODE (GooDemoItem, goo_demo_item,
			 GOO_TYPE_CANVAS_ITEM_SIMPLE,
			 G_IMPLEMENT_INTERFACE (GOO_TYPE_CANVAS_ITEM,
						canvas_item_interface_init))


static void
goo_demo_item_class_init (GooDemoItemClass *klass)
{
  /* You would normally setup a few class functions here, and install any
     object properties and signals. */
}


static void
goo_demo_item_init (GooDemoItem *demo_item)
{
  /* Here's where you initialize the object. */
  demo_item->x = 0.0;
  demo_item->y = 0.0;
  demo_item->width = 0.0;
  demo_item->height = 0.0;
}


/*
 * NOTE: Quite a bit of the code below is the same in all the subclasses
 * of GooCanvasItemSimple, so I've marked the item-specific code.
 */

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;

  /* ===== START OF ITEM-SPECIFIC CODE ===== */

  item = g_object_new (GOO_TYPE_DEMO_ITEM, NULL);

  demo_item = (GooDemoItem*) item;
  demo_item->x = x;
  demo_item->y = y;
  demo_item->width = width;
  demo_item->height = height;

  va_start (var_args, height);

  /* ===== END OF ITEM-SPECIFIC CODE ===== */

  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;
}


static GooCanvasItem*
goo_demo_item_get_item_at (GooCanvasItem  *item,
			   gdouble         x,
			   gdouble         y,
			   cairo_t        *cr,
			   gboolean        is_pointer_event,
			   gboolean        parent_visible)
{
  GooCanvasItemSimple *simple = (GooCanvasItemSimple*) item;
  GooCanvasItemSimpleData *simple_data = simple->simple_data;
  GooDemoItem *demo_item = (GooDemoItem*) item;
  GooCanvasItem *found_item = item;
  double user_x = x, user_y = y;

  if (simple->need_update)
    goo_canvas_item_ensure_updated (item);

  /* Check if the item should receive events. */
  if (is_pointer_event)
    {
      if (simple_data->pointer_events == GOO_CANVAS_EVENTS_NONE)
	return NULL;
      if (simple_data->pointer_events & GOO_CANVAS_EVENTS_VISIBLE_MASK
	  && (!parent_visible
	      || simple_data->visibility == GOO_CANVAS_ITEM_INVISIBLE
	      || (simple_data->visibility == GOO_CANVAS_ITEM_VISIBLE_ABOVE_THRESHOLD
		  && simple->canvas->scale < simple_data->visibility_threshold)))
	return NULL;
    }

  cairo_save (cr);

  if (simple_data->transform)
    cairo_transform (cr, simple_data->transform);

  cairo_device_to_user (cr, &user_x, &user_y);

  /* ===== START OF ITEM-SPECIFIC CODE ===== */

  /* Check if the given point is in the item. */
  if (user_x < demo_item->x || (user_x > demo_item->x + demo_item->width)
      || user_y < demo_item->y || (user_y > demo_item->y + demo_item->height))
    found_item = NULL;

  /* ===== END OF ITEM-SPECIFIC CODE ===== */

  cairo_restore (cr);

  return found_item;
}


static void
goo_demo_item_update  (GooCanvasItem      *item,
		       gboolean            entire_tree,
		       cairo_t            *cr,
		       GooCanvasBounds    *bounds)
{
  GooCanvasItemSimple *simple = (GooCanvasItemSimple*) item;
  GooCanvasItemSimpleData *simple_data = simple->simple_data;
  GooDemoItem *demo_item = (GooDemoItem*) item;

  if (entire_tree || simple->need_update)
    {
      simple->need_update = FALSE;

      goo_canvas_item_simple_check_style (simple);

      cairo_save (cr);
      if (simple_data->transform)
	cairo_transform (cr, simple_data->transform);

      /* Request a redraw of the existing bounds. */
      goo_canvas_request_redraw (simple->canvas, &simple->bounds);

      /* ===== START OF ITEM-SPECIFIC CODE ===== */

      /* Compute the new bounds, converting to device coordinates. */
      simple->bounds.x1 = demo_item->x;
      simple->bounds.y1 = demo_item->y;
      simple->bounds.x2 = demo_item->x + demo_item->width;
      simple->bounds.y2 = demo_item->y + demo_item->height;

      goo_canvas_item_simple_user_bounds_to_device (simple, cr,
						    &simple->bounds);

      /* ===== END OF ITEM-SPECIFIC CODE ===== */

      /* Request a redraw of the new bounds. */
      goo_canvas_request_redraw (simple->canvas, &simple->bounds);

      cairo_restore (cr);
    }

  *bounds = simple->bounds;
}


static void
goo_demo_item_paint (GooCanvasItem     *item,
		     cairo_t           *cr,
		     GooCanvasBounds   *bounds,
		     gdouble            scale)
{
  GooCanvasItemSimple *simple = (GooCanvasItemSimple*) item;
  GooCanvasItemSimpleData *simple_data = simple->simple_data;
  GooDemoItem *demo_item = (GooDemoItem*) item;

  /* Check if the item should be visible. */
  if (simple_data->visibility == GOO_CANVAS_ITEM_INVISIBLE
      || (simple_data->visibility == GOO_CANVAS_ITEM_VISIBLE_ABOVE_THRESHOLD
	  && scale < simple_data->visibility_threshold))
    return;

  cairo_save (cr);
  if (simple_data->transform)
    cairo_transform (cr, simple_data->transform);

  /* ===== START OF ITEM-SPECIFIC CODE ===== */

  /* Draw the item here. */
  cairo_move_to (cr, demo_item->x, demo_item->y);
  cairo_line_to (cr, demo_item->x, demo_item->y + demo_item->height);
  cairo_line_to (cr, demo_item->x + demo_item->width,
		 demo_item->y + demo_item->height);
  cairo_line_to (cr, demo_item->x + demo_item->width, demo_item->y);
  cairo_close_path (cr);
  goo_canvas_style_set_fill_options (simple_data->style, cr);
  cairo_fill (cr);

  /* ===== END OF ITEM-SPECIFIC CODE ===== */

  cairo_restore (cr);
}


static void
canvas_item_interface_init (GooCanvasItemIface *iface)
{
  iface->get_item_at = goo_demo_item_get_item_at;
  iface->update      = goo_demo_item_update;
  iface->paint       = goo_demo_item_paint;
}

--- NEW FILE: demo-item.h ---
/*
 * GooCanvas Demo. Copyright (C) 2006 Damon Chaplin.
 * Released under the GNU LGPL license. See COPYING for details.
 *
 * demo-item.c - a simple demo item.
 */
#ifndef __GOO_DEMO_ITEM_H__
#define __GOO_DEMO_ITEM_H__

#include <gtk/gtk.h>
#include "goocanvasitemsimple.h"

G_BEGIN_DECLS


#define GOO_TYPE_DEMO_ITEM            (goo_demo_item_get_type ())
#define GOO_DEMO_ITEM(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GOO_TYPE_DEMO_ITEM, GooDemoItem))
#define GOO_DEMO_ITEM_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GOO_TYPE_DEMO_ITEM, GooDemoItemClass))
#define GOO_IS_DEMO_ITEM(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GOO_TYPE_DEMO_ITEM))
#define GOO_IS_DEMO_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GOO_TYPE_DEMO_ITEM))
#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

#endif /* __GOO_DEMO_ITEM_H__ */

Index: demo.c
===================================================================
RCS file: /cvs/cairo/goocanvas/demo/demo.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- demo.c	29 Nov 2006 18:47:02 -0000	1.9
+++ demo.c	29 Nov 2006 20:44:58 -0000	1.10
@@ -15,7 +15,7 @@
 #include <cairo-pdf.h>
 #endif
 #include <goocanvas.h>
-
+#include "demo-item.h"
 
 GooCanvasItem *ellipse2, *textitem;
 
@@ -675,6 +675,11 @@
 			      "fill-color-rgba", 0x3cb3f180,
 			      NULL);
   setup_item_signals (item);
+
+  item = goo_demo_item_new (root, 30, 20, 50, 30,
+			    "fill-color", "yellow",
+			    NULL);
+  setup_item_signals (item);
 }
 
 



More information about the cairo-commit mailing list