[gimp] app: some cairo tool drawing infrastructure for review, please have a look



commit 69a898cc9f29e2b0bb6189dcd5d98addf92078a4
Author: Michael Natterer <mitch gimp org>
Date:   Sun Sep 19 19:45:51 2010 +0200

    app: some cairo tool drawing infrastructure for review, please have a look

 app/display/Makefile.am           |    4 +
 app/display/gimpcanvasitem.c      |  120 +++++++++++++++
 app/display/gimpcanvasitem.h      |   75 ++++++++++
 app/display/gimpcanvasrectangle.c |  289 +++++++++++++++++++++++++++++++++++++
 app/display/gimpcanvasrectangle.h |   59 ++++++++
 5 files changed, 547 insertions(+), 0 deletions(-)
---
diff --git a/app/display/Makefile.am b/app/display/Makefile.am
index 2cf3aac..8aba517 100644
--- a/app/display/Makefile.am
+++ b/app/display/Makefile.am
@@ -19,6 +19,10 @@ libappdisplay_a_sources = \
 	display-types.h				\
 	gimpcanvas.c				\
 	gimpcanvas.h				\
+	gimpcanvasitem.c			\
+	gimpcanvasitem.h			\
+	gimpcanvasrectangle.c			\
+	gimpcanvasrectangle.h			\
 	gimpcursorview.c			\
 	gimpcursorview.h			\
 	gimpdisplay.c				\
diff --git a/app/display/gimpcanvasitem.c b/app/display/gimpcanvasitem.c
new file mode 100644
index 0000000..2cd1905
--- /dev/null
+++ b/app/display/gimpcanvasitem.c
@@ -0,0 +1,120 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <gegl.h>
+#include <gtk/gtk.h>
+
+#include "display-types.h"
+
+#include "gimpcanvasitem.h"
+#include "gimpdisplayshell.h"
+#include "gimpdisplayshell-style.h"
+
+
+/*  local function prototypes  */
+
+static void   gimp_canvas_item_real_draw       (GimpCanvasItem   *item,
+                                                GimpDisplayShell *shell,
+                                                cairo_t          *cr);
+static void   gimp_canvas_item_real_invalidate (GimpCanvasItem   *item,
+                                                GimpDisplayShell *shell);
+
+
+G_DEFINE_TYPE (GimpCanvasItem, gimp_canvas_item,
+               GIMP_TYPE_OBJECT)
+
+#define parent_class gimp_canvas_item_parent_class
+
+
+static void
+gimp_canvas_item_class_init (GimpCanvasItemClass *klass)
+{
+  klass->draw       = gimp_canvas_item_real_draw;
+  klass->invalidate = gimp_canvas_item_real_invalidate;
+}
+
+static void
+gimp_canvas_item_init (GimpCanvasItem *item)
+{
+}
+
+static void
+gimp_canvas_item_real_draw (GimpCanvasItem   *item,
+                            GimpDisplayShell *shell,
+                            cairo_t          *cr)
+{
+  g_warn_if_reached ();
+}
+
+static void
+gimp_canvas_item_real_invalidate (GimpCanvasItem   *item,
+                                  GimpDisplayShell *shell)
+{
+  g_warn_if_reached ();
+}
+
+
+/*  public functions  */
+
+void
+gimp_canvas_item_draw (GimpCanvasItem   *item,
+                       GimpDisplayShell *shell,
+                       cairo_t          *cr)
+{
+  g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));
+  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
+  g_return_if_fail (cr != NULL);
+
+  cairo_save (cr);
+
+  GIMP_CANVAS_ITEM_GET_CLASS (item)->draw (item, shell, cr);
+
+  cairo_restore (cr);
+}
+
+void
+gimp_canvas_item_invalidate (GimpCanvasItem   *item,
+                             GimpDisplayShell *shell)
+{
+  g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));
+  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
+
+  GIMP_CANVAS_ITEM_GET_CLASS (item)->invalidate (item, shell);
+}
+
+void
+_gimp_canvas_item_stroke (GimpCanvasItem   *item,
+                          GimpDisplayShell *shell,
+                          cairo_t          *cr)
+{
+  gimp_display_shell_set_tool_bg_style (shell, cr);
+  cairo_stroke_preserve (cr);
+
+  gimp_display_shell_set_tool_fg_style (shell, cr);
+  cairo_stroke (cr);
+}
+
+void
+_gimp_canvas_item_fill (GimpCanvasItem   *item,
+                        GimpDisplayShell *shell,
+                        cairo_t          *cr)
+{
+  gimp_display_shell_set_tool_fg_style (shell, cr);
+  cairo_fill (cr);
+}
diff --git a/app/display/gimpcanvasitem.h b/app/display/gimpcanvasitem.h
new file mode 100644
index 0000000..285d2df
--- /dev/null
+++ b/app/display/gimpcanvasitem.h
@@ -0,0 +1,75 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * gimpcanvasitem.h
+ * Copyright (C) 2010 Michael Natterer <mitch gimp org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GIMP_CANVAS_ITEM_H__
+#define __GIMP_CANVAS_ITEM_H__
+
+
+#include "core/gimpobject.h"
+
+
+#define GIMP_TYPE_CANVAS_ITEM            (gimp_canvas_item_get_type ())
+#define GIMP_CANVAS_ITEM(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_CANVAS_ITEM, GimpCanvasItem))
+#define GIMP_CANVAS_ITEM_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_CANVAS_ITEM, GimpCanvasItemClass))
+#define GIMP_IS_CANVAS_ITEM(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_CANVAS_ITEM))
+#define GIMP_IS_CANVAS_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_CANVAS_ITEM))
+#define GIMP_CANVAS_ITEM_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_CANVAS_ITEM, GimpCanvasItemClass))
+
+
+typedef struct _GimpCanvasItem      GimpCanvasItem;
+typedef struct _GimpCanvasItemClass GimpCanvasItemClass;
+
+struct _GimpCanvasItem
+{
+  GimpObject       parent_instance;
+};
+
+struct _GimpCanvasItemClass
+{
+  GimpObjectClass  parent_class;
+
+  void (* draw)       (GimpCanvasItem   *item,
+                       GimpDisplayShell *shell,
+                       cairo_t          *cr);
+  void (* invalidate) (GimpCanvasItem   *item,
+                       GimpDisplayShell *shell);
+};
+
+
+GType   gimp_canvas_item_get_type   (void) G_GNUC_CONST;
+
+void    gimp_canvas_item_draw       (GimpCanvasItem   *item,
+                                     GimpDisplayShell *shell,
+                                     cairo_t          *cr);
+void    gimp_canvas_item_invalidate (GimpCanvasItem   *item,
+                                     GimpDisplayShell *shell);
+
+
+/*  protected  */
+
+void    _gimp_canvas_item_stroke    (GimpCanvasItem   *item,
+                                     GimpDisplayShell *shell,
+                                     cairo_t          *cr);
+void    _gimp_canvas_item_fill      (GimpCanvasItem   *item,
+                                     GimpDisplayShell *shell,
+                                     cairo_t          *cr);
+
+
+#endif /* __GIMP_CANVAS_ITEM_H__ */
diff --git a/app/display/gimpcanvasrectangle.c b/app/display/gimpcanvasrectangle.c
new file mode 100644
index 0000000..463b09e
--- /dev/null
+++ b/app/display/gimpcanvasrectangle.c
@@ -0,0 +1,289 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <gegl.h>
+#include <gtk/gtk.h>
+
+#include "libgimpbase/gimpbase.h"
+#include "libgimpmath/gimpmath.h"
+
+#include "display-types.h"
+
+#include "gimpcanvasrectangle.h"
+#include "gimpdisplayshell.h"
+#include "gimpdisplayshell-expose.h"
+#include "gimpdisplayshell-transform.h"
+
+
+enum
+{
+  PROP_0,
+  PROP_X,
+  PROP_Y,
+  PROP_WIDTH,
+  PROP_HEIGHT,
+  PROP_FILLED
+};
+
+
+typedef struct _GimpCanvasRectanglePrivate GimpCanvasRectanglePrivate;
+
+struct _GimpCanvasRectanglePrivate
+{
+  gdouble  x;
+  gdouble  y;
+  gdouble  width;
+  gdouble  height;
+  gboolean filled;
+};
+
+#define GET_PRIVATE(rectangle) \
+        G_TYPE_INSTANCE_GET_PRIVATE (rectangle, \
+                                     GIMP_TYPE_CANVAS_RECTANGLE, \
+                                     GimpCanvasRectanglePrivate)
+
+
+/*  local function prototypes  */
+
+static void   gimp_canvas_rectangle_set_property (GObject          *object,
+                                                  guint             property_id,
+                                                  const GValue     *value,
+                                                  GParamSpec       *pspec);
+static void   gimp_canvas_rectangle_get_property (GObject          *object,
+                                                  guint             property_id,
+                                                  GValue           *value,
+                                                  GParamSpec       *pspec);
+static void   gimp_canvas_rectangle_draw         (GimpCanvasItem   *item,
+                                                  GimpDisplayShell *shell,
+                                                  cairo_t          *cr);
+static void   gimp_canvas_rectangle_invalidate   (GimpCanvasItem   *item,
+                                                  GimpDisplayShell *shell);
+
+
+G_DEFINE_TYPE (GimpCanvasRectangle, gimp_canvas_rectangle,
+               GIMP_TYPE_CANVAS_ITEM)
+
+#define parent_class gimp_canvas_rectangle_parent_class
+
+
+static void
+gimp_canvas_rectangle_class_init (GimpCanvasRectangleClass *klass)
+{
+  GObjectClass        *object_class = G_OBJECT_CLASS (klass);
+  GimpCanvasItemClass *item_class   = GIMP_CANVAS_ITEM_CLASS (klass);
+
+  object_class->set_property = gimp_canvas_rectangle_set_property;
+  object_class->get_property = gimp_canvas_rectangle_get_property;
+
+  item_class->draw           = gimp_canvas_rectangle_draw;
+  item_class->invalidate     = gimp_canvas_rectangle_invalidate;
+
+  g_object_class_install_property (object_class, PROP_X,
+                                   g_param_spec_double ("x", NULL, NULL,
+                                                        -GIMP_MAX_IMAGE_SIZE,
+                                                        GIMP_MAX_IMAGE_SIZE, 0,
+                                                        GIMP_PARAM_READWRITE));
+
+  g_object_class_install_property (object_class, PROP_Y,
+                                   g_param_spec_double ("y", NULL, NULL,
+                                                        -GIMP_MAX_IMAGE_SIZE,
+                                                        GIMP_MAX_IMAGE_SIZE, 0,
+                                                        GIMP_PARAM_READWRITE));
+
+  g_object_class_install_property (object_class, PROP_WIDTH,
+                                   g_param_spec_double ("width", NULL, NULL,
+                                                        0, GIMP_MAX_IMAGE_SIZE, 0,
+                                                        GIMP_PARAM_READWRITE));
+
+  g_object_class_install_property (object_class, PROP_HEIGHT,
+                                   g_param_spec_double ("height", NULL, NULL,
+                                                        0, GIMP_MAX_IMAGE_SIZE, 0,
+                                                        GIMP_PARAM_READWRITE));
+
+  g_object_class_install_property (object_class, PROP_FILLED,
+                                   g_param_spec_boolean ("filled", NULL, NULL,
+                                                         FALSE,
+                                                         GIMP_PARAM_READWRITE));
+
+  g_type_class_add_private (klass, sizeof (GimpCanvasRectanglePrivate));
+}
+
+static void
+gimp_canvas_rectangle_init (GimpCanvasRectangle *rectangle)
+{
+}
+
+static void
+gimp_canvas_rectangle_set_property (GObject      *object,
+                                    guint         property_id,
+                                    const GValue *value,
+                                    GParamSpec   *pspec)
+{
+  GimpCanvasRectanglePrivate *private = GET_PRIVATE (object);
+
+  switch (property_id)
+    {
+    case PROP_X:
+      private->x = g_value_get_double (value);
+      break;
+    case PROP_Y:
+      private->y = g_value_get_double (value);
+      break;
+    case PROP_WIDTH:
+      private->width = g_value_get_double (value);
+      break;
+    case PROP_HEIGHT:
+      private->height = g_value_get_double (value);
+      break;
+    case PROP_FILLED:
+      private->filled = g_value_get_boolean (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gimp_canvas_rectangle_get_property (GObject    *object,
+                                    guint       property_id,
+                                    GValue     *value,
+                                    GParamSpec *pspec)
+{
+  GimpCanvasRectanglePrivate *private = GET_PRIVATE (object);
+
+  switch (property_id)
+    {
+    case PROP_X:
+      g_value_set_double (value, private->x);
+      break;
+    case PROP_Y:
+      g_value_set_double (value, private->y);
+      break;
+    case PROP_WIDTH:
+      g_value_set_double (value, private->width);
+      break;
+    case PROP_HEIGHT:
+      g_value_set_double (value, private->height);
+      break;
+    case PROP_FILLED:
+      g_value_set_boolean (value, private->filled);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gimp_canvas_rectangle_transform (GimpCanvasItem   *item,
+                                 GimpDisplayShell *shell,
+                                 gdouble          *x,
+                                 gdouble          *y,
+                                 gdouble          *w,
+                                 gdouble          *h)
+{
+  GimpCanvasRectanglePrivate *private = GET_PRIVATE (item);
+
+  gimp_display_shell_transform_xy_f (shell,
+                                     MIN (private->x,
+                                          private->x + private->width),
+                                     MIN (private->y,
+                                          private->y + private->height),
+                                     x, y,
+                                     FALSE);
+  gimp_display_shell_transform_xy_f (shell,
+                                     MAX (private->x,
+                                          private->x + private->width),
+                                     MAX (private->y,
+                                          private->y + private->height),
+                                     w, h,
+                                     FALSE);
+
+  *w -= *x;
+  *h -= *y;
+
+  if (private->filled)
+    {
+      *x = floor (*x);
+      *y = floor (*y);
+      *w = ceil (*w);
+      *h = ceil (*h);
+    }
+  else
+    {
+      *x = PROJ_ROUND (*x) + 0.5;
+      *y = PROJ_ROUND (*x) + 0.5;
+      *w = PROJ_ROUND (*w) - 1.0;
+      *h = PROJ_ROUND (*h) - 1.0;
+    }
+}
+
+static void
+gimp_canvas_rectangle_draw (GimpCanvasItem   *item,
+                            GimpDisplayShell *shell,
+                            cairo_t          *cr)
+{
+  GimpCanvasRectanglePrivate *private = GET_PRIVATE (item);
+  gdouble                     x, y;
+  gdouble                     w, h;
+
+  gimp_canvas_rectangle_transform (item, shell, &x, &y, &w, &h);
+
+  cairo_rectangle (cr, x, y, w, h);
+
+  if (private->filled)
+    _gimp_canvas_item_fill (item, shell, cr);
+  else
+    _gimp_canvas_item_stroke (item, shell, cr);
+}
+
+static void
+gimp_canvas_rectangle_invalidate (GimpCanvasItem   *item,
+                                  GimpDisplayShell *shell)
+{
+  GimpCanvasRectanglePrivate *private = GET_PRIVATE (item);
+  gdouble                     x, y;
+  gdouble                     w, h;
+
+  gimp_canvas_rectangle_transform (item, shell, &x, &y, &w, &h);
+
+  if (private->filled)
+    gimp_display_shell_expose_area (shell, x, y, w, h);
+  else
+    gimp_display_shell_expose_area (shell, x - 1.5, y - 1.5, w + 1.5, h + 1.5);
+}
+
+GimpCanvasItem *
+gimp_canvas_rectangle_new (gdouble  x,
+                           gdouble  y,
+                           gdouble  width,
+                           gdouble  height,
+                           gboolean filled)
+{
+  return g_object_new (GIMP_TYPE_CANVAS_RECTANGLE,
+                       "x",      x,
+                       "y",      y,
+                       "width",  width,
+                       "height", height,
+                       "filled", filled,
+                       NULL);
+}
diff --git a/app/display/gimpcanvasrectangle.h b/app/display/gimpcanvasrectangle.h
new file mode 100644
index 0000000..4035904
--- /dev/null
+++ b/app/display/gimpcanvasrectangle.h
@@ -0,0 +1,59 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * gimpcanvasrectangle.h
+ * Copyright (C) 2010 Michael Natterer <mitch gimp org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GIMP_CANVAS_RECTANGLE_H__
+#define __GIMP_CANVAS_RECTANGLE_H__
+
+
+#include "gimpcanvasitem.h"
+
+
+#define GIMP_TYPE_CANVAS_RECTANGLE            (gimp_canvas_rectangle_get_type ())
+#define GIMP_CANVAS_RECTANGLE(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_CANVAS_RECTANGLE, GimpCanvasRectangle))
+#define GIMP_CANVAS_RECTANGLE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_CANVAS_RECTANGLE, GimpCanvasRectangleClass))
+#define GIMP_IS_CANVAS_RECTANGLE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_CANVAS_RECTANGLE))
+#define GIMP_IS_CANVAS_RECTANGLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_CANVAS_RECTANGLE))
+#define GIMP_CANVAS_RECTANGLE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_CANVAS_RECTANGLE, GimpCanvasRectangleClass))
+
+
+typedef struct _GimpCanvasRectangle      GimpCanvasRectangle;
+typedef struct _GimpCanvasRectangleClass GimpCanvasRectangleClass;
+
+struct _GimpCanvasRectangle
+{
+  GimpCanvasItem  parent_instance;
+};
+
+struct _GimpCanvasRectangleClass
+{
+  GimpCanvasItemClass  parent_class;
+};
+
+
+GType            gimp_canvas_rectangle_get_type (void) G_GNUC_CONST;
+
+GimpCanvasItem * gimp_canvas_rectangle_new      (gdouble  x,
+                                                 gdouble  y,
+                                                 gdouble  width,
+                                                 gdouble  height,
+                                                 gboolean filled);
+
+
+#endif /* __GIMP_CANVAS_RECTANGLE_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]