Defined pixmap object as gobject
authordanigm <dani@danigm.net>
Sun, 15 Aug 2010 12:43:44 +0000 (14:43 +0200)
committerdanigm <dani@danigm.net>
Sun, 15 Aug 2010 12:43:44 +0000 (14:43 +0200)
src/Makefile.am
src/tbo-object-pixmap.c [new file with mode: 0644]
src/tbo-object-pixmap.h [new file with mode: 0644]
src/typestest.c

index 3564585..1df0b87 100644 (file)
@@ -68,6 +68,8 @@ SOURCES = \
        tbo-object-svg.c \
        tbo-object-text.h \
        tbo-object-text.c \
+       tbo-object-pixmap.h \
+       tbo-object-pixmap.c \
        custom-stock.h
 
 AM_CFLAGS = @GTK_CFLAGS@ \
diff --git a/src/tbo-object-pixmap.c b/src/tbo-object-pixmap.c
new file mode 100644 (file)
index 0000000..7eb260a
--- /dev/null
@@ -0,0 +1,170 @@
+/*
+ * This file is part of TBO, a gnome comic editor
+ * Copyright (C) 2010  Daniel Garcia Moreno <dani@danigm.net>
+ *
+ * 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 <glib.h>
+#include <string.h>
+#include <cairo.h>
+#include <stdio.h>
+#include "tbo-types.h"
+#include "tbo-object-pixmap.h"
+
+G_DEFINE_TYPE (TboObjectPixmap, tbo_object_pixmap, TBO_TYPE_OBJECT_BASE);
+
+static void draw (TboObjectBase *, Frame *, cairo_t *);
+static void save (TboObjectBase *, FILE *);
+static TboObjectBase * clone (TboObjectBase *);
+
+static void
+draw (TboObjectBase *self, Frame *frame, cairo_t *cr)
+{
+    TboObjectPixmap *pixmap = TBO_OBJECT_PIXMAP (self);
+    int w, h;
+    cairo_surface_t *image;
+
+    image = cairo_image_surface_create_from_png (pixmap->path->str);
+    w = cairo_image_surface_get_width (image);
+    h = cairo_image_surface_get_height (image);
+
+    if (!self->width) self->width = w;
+    if (!self->height) self->height = h;
+
+    float factorw = (float)self->width / (float)w;
+    float factorh = (float)self->height / (float)h;
+
+    cairo_matrix_t mx = {1, 0, 0, 1, 0, 0};
+    tbo_object_get_flip_matrix (self, &mx);
+
+    cairo_rectangle(cr, frame->x+2, frame->y+2, frame->width-4, frame->height-4);
+    cairo_clip (cr);
+    cairo_translate (cr, frame->x+self->x, frame->y+self->y);
+    cairo_rotate (cr, self->angle);
+    cairo_transform (cr, &mx);
+    cairo_scale (cr, factorw, factorh);
+
+    cairo_set_source_surface (cr, image, 0, 0);
+    cairo_paint (cr);
+
+    cairo_scale (cr, 1/factorw, 1/factorh);
+    cairo_transform (cr, &mx);
+    cairo_rotate (cr, -self->angle);
+    cairo_translate (cr, -(frame->x+self->x), -(frame->y+self->y));
+    cairo_reset_clip (cr);
+
+    cairo_surface_destroy (image);
+}
+
+static void
+save (TboObjectBase *self, FILE *file)
+{
+    char buffer[1024];
+
+    snprintf (buffer, 1024, "   <piximage x=\"%d\" y=\"%d\" "
+                           "width=\"%d\" height=\"%d\" "
+                           "angle=\"%f\" flipv=\"%d\" fliph=\"%d\" "
+                           "path=\"%s\">\n ",
+                           self->x, self->y, self->width, self->height,
+                           self->angle, self->flipv, self->fliph, TBO_OBJECT_PIXMAP (self)->path->str);
+    fwrite (buffer, sizeof (char), strlen (buffer), file);
+
+    snprintf (buffer, 1024, "   </piximage>\n");
+    fwrite (buffer, sizeof (char), strlen (buffer), file);
+}
+
+static TboObjectBase *
+clone (TboObjectBase *self)
+{
+    TboObjectPixmap *pixmap;
+    TboObjectBase *newpixmap;
+    pixmap = TBO_OBJECT_PIXMAP (self);
+
+    newpixmap = TBO_OBJECT_BASE (tbo_object_pixmap_new_with_params (self->x,
+                                                                    self->y,
+                                                                    self->width,
+                                                                    self->height,
+                                                                    pixmap->path->str));
+    newpixmap->angle = self->angle;
+    newpixmap->flipv = self->flipv;
+    newpixmap->fliph = self->fliph;
+
+    return newpixmap;
+}
+
+/* init methods */
+
+static void
+tbo_object_pixmap_init (TboObjectPixmap *self)
+{
+    self->path = NULL;
+}
+
+static void
+tbo_object_pixmap_finalize (GObject *self)
+{
+    if (TBO_OBJECT_PIXMAP (self)->path)
+        g_string_free (TBO_OBJECT_PIXMAP (self)->path, TRUE);
+    /* Chain up to the parent class */
+    G_OBJECT_CLASS (tbo_object_pixmap_parent_class)->finalize (self);
+}
+
+static void
+tbo_object_pixmap_class_init (TboObjectPixmapClass *klass)
+{
+    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+    TboObjectBaseClass *object_class = TBO_OBJECT_BASE_CLASS (klass);
+    object_class->draw = draw;
+    object_class->save = save;
+    object_class->clone = clone;
+
+    gobject_class->finalize = tbo_object_pixmap_finalize;
+}
+
+/* object functions */
+
+GObject *
+tbo_object_pixmap_new ()
+{
+    GObject *tbo_object;
+    TboObjectPixmap *pixmap;
+    tbo_object = g_object_new (TBO_TYPE_OBJECT_PIXMAP, NULL);
+    pixmap = TBO_OBJECT_PIXMAP (tbo_object);
+    pixmap->path = g_string_new ("");
+
+    return tbo_object;
+}
+
+GObject *
+tbo_object_pixmap_new_with_params (gint   x,
+                                   gint   y,
+                                   gint   width,
+                                   gint   height,
+                                   gchar *path)
+{
+    TboObjectPixmap *pixmap;
+    TboObjectBase *obj;
+    pixmap = TBO_OBJECT_PIXMAP (tbo_object_pixmap_new ());
+    obj = TBO_OBJECT_BASE (pixmap);
+    obj->x = x;
+    obj->y = y;
+    obj->width = width;
+    obj->height = height;
+    g_string_assign (pixmap->path, path);
+
+    return G_OBJECT (pixmap);
+}
+
diff --git a/src/tbo-object-pixmap.h b/src/tbo-object-pixmap.h
new file mode 100644 (file)
index 0000000..e5f87a1
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * This file is part of TBO, a gnome comic editor
+ * Copyright (C) 2010  Daniel Garcia Moreno <dani@danigm.net>
+ *
+ * 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 __TBO_OBJECT_PIXMAP_H__
+#define __TBO_OBJECT_PIXMAP_H__
+
+#include <glib.h>
+#include "tbo-object-base.h"
+
+#define TBO_TYPE_OBJECT_PIXMAP            (tbo_object_pixmap_get_type ())
+#define TBO_OBJECT_PIXMAP(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), TBO_TYPE_OBJECT_PIXMAP, TboObjectPixmap))
+#define TBO_IS_OBJECT_PIXMAP(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TBO_TYPE_OBJECT_PIXMAP))
+#define TBO_OBJECT_PIXMAP_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), TBO_TYPE_OBJECT_PIXMAP, TboObjectPixmapClass))
+#define TBO_IS_OBJECT_PIXMAP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TBO_TYPE_OBJECT_PIXMAP))
+#define TBO_OBJECT_PIXMAP_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), TBO_TYPE_OBJECT_PIXMAP, TboObjectPixmapClass))
+
+typedef struct _TboObjectPixmap      TboObjectPixmap;
+typedef struct _TboObjectPixmapClass TboObjectPixmapClass;
+
+struct _TboObjectPixmap
+{
+    TboObjectBase parent_instance;
+
+    /* instance members */
+    GString *path;
+};
+
+struct _TboObjectPixmapClass
+{
+    TboObjectBaseClass parent_class;
+
+    /* class members */
+};
+
+/* used by TBO_TYPE_OBJECT_PIXMAP */
+GType tbo_object_pixmap_get_type (void);
+
+/*
+ * Method definitions.
+ */
+
+GObject * tbo_object_pixmap_new ();
+GObject * tbo_object_pixmap_new_with_params (gint   x,
+                                             gint   y,
+                                             gint   width,
+                                             gint   height,
+                                             gchar *path);
+
+#endif /* __TBO_OBJECT_PIXMAP_H__ */
+
index 38f9e7f..8ac71bc 100644 (file)
@@ -2,6 +2,7 @@
 #include "tbo-object-base.h"
 #include "tbo-object-svg.h"
 #include "tbo-object-text.h"
+#include "tbo-object-pixmap.h"
 
 void
 print_tbo_object (TboObjectBase *obj)
@@ -49,6 +50,21 @@ test_object_text ()
     g_object_unref (text);
 }
 
+void
+test_object_pixmap ()
+{
+    TboObjectPixmap *pixmap = TBO_OBJECT_PIXMAP (tbo_object_pixmap_new ());
+
+    /* pixmap object with params */
+    pixmap = TBO_OBJECT_PIXMAP (tbo_object_pixmap_new_with_params (100, 200,
+                                150, 300, "/path/to/pngfile.png"));
+
+    print_tbo_object (TBO_OBJECT_BASE (pixmap));
+    printf ("path: '%s'\n", pixmap->path->str);
+
+    g_object_unref (pixmap);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -58,6 +74,8 @@ main (int argc, char **argv)
     test_object_svg ();
     printf ("\nobject text\n--------------\n");
     test_object_text ();
+    printf ("\nobject pixmap\n--------------\n");
+    test_object_pixmap ();
 
     return 0;
 }