Now it's possible to add png images :D
authordanigm <dani@danigm.net>
Wed, 7 Apr 2010 21:23:01 +0000 (21:23 +0000)
committerdanigm <danigm@aa302d1f-72b1-406a-902b-8593ca2b2744>
Wed, 7 Apr 2010 21:23:01 +0000 (21:23 +0000)
From: danigm <dani@danigm.net>

git-svn-id: https://forja.rediris.es/svn/cusl4-tbo@114 aa302d1f-72b1-406a-902b-8593ca2b2744

data/icons/pix.svg [new file with mode: 0644]
data/ui/tbo-toolbar-ui.xml
src/Makefile.am
src/comic-load.c
src/custom-stock.c
src/custom-stock.h
src/dnd.h
src/piximage.c [new file with mode: 0644]
src/piximage.h [new file with mode: 0644]
src/tbo-types.h
src/ui-toolbar.c

diff --git a/data/icons/pix.svg b/data/icons/pix.svg
new file mode 100644 (file)
index 0000000..d3f8ac1
--- /dev/null
@@ -0,0 +1,115 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!-- Created with Inkscape (http://www.inkscape.org/) -->
+
+<svg
+   xmlns:svg="http://www.w3.org/2000/svg"
+   xmlns="http://www.w3.org/2000/svg"
+   xmlns:xlink="http://www.w3.org/1999/xlink"
+   version="1.0"
+   width="24"
+   height="24"
+   id="svg6003">
+  <defs
+     id="defs6005">
+    <linearGradient
+       id="linearGradient5740">
+      <stop
+         id="stop5742"
+         style="stop-color:#ffffff;stop-opacity:1"
+         offset="0" />
+      <stop
+         id="stop5744"
+         style="stop-color:#98b6d3;stop-opacity:1"
+         offset="1" />
+    </linearGradient>
+    <linearGradient
+       x1="101.09393"
+       y1="221.06877"
+       x2="112.20101"
+       y2="234.00311"
+       id="linearGradient2832"
+       xlink:href="#linearGradient5740"
+       gradientUnits="userSpaceOnUse"
+       gradientTransform="matrix(1.13356,0,0,1.133368,328.993,104.14206)" />
+    <linearGradient
+       x1="144.08243"
+       y1="157.82079"
+       x2="176.86269"
+       y2="188.41277"
+       id="linearGradient2834"
+       xlink:href="#linearGradient5740"
+       gradientUnits="userSpaceOnUse"
+       gradientTransform="matrix(0.809546,0,0,0.809417,332.4176,231.42374)" />
+    <linearGradient
+       x1="148"
+       y1="214.81735"
+       x2="143.22507"
+       y2="215.9465"
+       id="linearGradient10641"
+       xlink:href="#linearGradient1610"
+       gradientUnits="userSpaceOnUse" />
+    <linearGradient
+       id="linearGradient1610">
+      <stop
+         id="stop1611"
+         style="stop-color:#000000;stop-opacity:1"
+         offset="0" />
+      <stop
+         id="stop3053"
+         style="stop-color:#7f7f7f;stop-opacity:1"
+         offset="0.35076979" />
+      <stop
+         id="stop3054"
+         style="stop-color:#bfbfbf;stop-opacity:1"
+         offset="0.59668732" />
+      <stop
+         id="stop3055"
+         style="stop-color:#dfdfdf;stop-opacity:1"
+         offset="0.78382427" />
+      <stop
+         id="stop1612"
+         style="stop-color:#ffffff;stop-opacity:1"
+         offset="1" />
+    </linearGradient>
+  </defs>
+  <g
+     transform="translate(-430.85714,-343.21933)"
+     id="layer1">
+    <rect
+       width="18.991671"
+       height="18.991779"
+       x="433.36545"
+       y="345.72754"
+       id="draw_rect1"
+       style="fill:#729fcf;stroke:#000000;stroke-width:1;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:0;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0;marker:none;display:inline" />
+    <path
+       d="m 15.956756,8.9837837 a 5.0918918,5.0918918 0 1 1 -10.1837834,0 5.0918918,5.0918918 0 1 1 10.1837834,0 z"
+       transform="translate(430.85714,343.21933)"
+       id="path2851"
+       style="fill:#edd400;fill-opacity:1;stroke:none" />
+    <rect
+       width="24"
+       height="24"
+       x="430.85715"
+       y="343.21933"
+       id="rect4167"
+       style="fill:none;stroke:none;stroke-width:1.00000012;marker:none;display:inline" />
+    <path
+       d="M 16.34375,4.875 C 12.67757,7.32577 10.89627,11.82282 9.5,16.375 l 0,5.125 12,0 0,-12.5625 C 19.877064,6.6245788 18.102411,4.9832304 16.34375,4.875 z"
+       transform="translate(430.85714,343.21933)"
+       id="path2836"
+       style="fill:#73d216;fill-opacity:1;stroke:none" />
+    <path
+       d="m 437.98214,352.93808 c -2.47311,1.65322 -3.68311,4.67919 -4.625,7.75 l 0,4.03125 12.4375,0 c -1.35727,-3.72507 -4.58921,-11.58289 -7.8125,-11.78125 z"
+       id="path2831"
+       style="fill:#4e9a06;fill-opacity:1;stroke:none" />
+    <path
+       d="m 438.1377,353.5297 c -0.61767,0.41289 -1.12009,0.96214 -1.53056,1.59097 0.46727,0.7799 1.29397,0.36994 1.36944,0.18125 0.0836,-0.20901 0.3256,0.57303 0.78542,0.32222 0.45982,-0.25081 0.64445,-0.58403 0.64445,-0.58403 0,0 0.21086,0.71248 0.50347,0.50348 0.1463,-0.10451 0.24384,-0.28184 0.32222,-0.42292 -0.66743,-0.90528 -1.37869,-1.54693 -2.09444,-1.59097 z"
+       id="path2841"
+       style="fill:#ffffff;fill-opacity:1;stroke:none" />
+    <path
+       d="m 447.28642,348.84981 c -1.48279,0.99121 -2.56838,2.41822 -3.42928,4.04605 1.04379,2.27123 3.16915,1.18076 3.35526,0.71546 0.19515,-0.48781 0.77711,1.37485 1.85033,0.78947 1.0732,-0.5854 1.48027,-1.3569 1.48027,-1.3569 0,0 0.31866,1.1029 0.81414,1.23355 l 0,-2.2204 c -1.28127,-1.82599 -2.68231,-3.12179 -4.07072,-3.20723 z"
+       id="path2843"
+       style="fill:#ffffff;fill-opacity:1;stroke:none" />
+  </g>
+</svg>
index 9b015fe..db8cf38 100644 (file)
@@ -16,6 +16,7 @@
        <separator/>
        <toolitem name="doodle" action="Doodle"/>
        <toolitem name="text" action="Text"/>
+       <toolitem name="pix" action="Pix"/>
        <separator/>
        <toolitem name="zoom100" action="Zoom100"/>
        <toolitem name="zoomout" action="Zoomout"/>
index 68035a8..4ded119 100644 (file)
@@ -29,6 +29,7 @@ tbo_SOURCES = \
        textobj.c \
        export.c \
        svgimage.c \
+       piximage.c \
        dnd.c \
        tbo-object.c \
        comic-load.c \
@@ -50,6 +51,7 @@ tbo_SOURCES = \
        doodle-treeview.h \
        text-tool.h \
        svgimage.h \
+       piximage.h \
        textobj.h \
        dnd.h \
        tbo-object.h \
index ac66fbf..f784694 100644 (file)
@@ -12,6 +12,7 @@
 #include "tbo-object.h"
 #include "svgimage.h"
 #include "textobj.h"
+#include "piximage.h"
 #include "tbo-utils.h"
 
 char *TITLE;
@@ -101,6 +102,33 @@ create_tbo_frame (const gchar **attribute_names, const gchar **attribute_values)
 }
 
 void
+create_tbo_piximage (const gchar **attribute_names, const gchar **attribute_values)
+{
+    PIXImage *pix;
+    int x=0, y=0;
+    int width=0, height=0;
+    float angle=0.0;
+    int flipv=0, fliph=0;
+    char path[255];
+
+    struct attr attrs[] = {
+        {"x", "%d", &x},
+        {"y", "%d", &y},
+        {"width", "%d", &width},
+        {"height", "%d", &height},
+        {"flipv", "%d", &flipv},
+        {"fliph", "%d", &fliph},
+        {"angle", "%f", &angle},
+        {"path", "%s", path},
+    };
+
+    parse_attrs (attrs, G_N_ELEMENTS (attrs), attribute_names, attribute_values);
+
+    pix = tbo_piximage_new_width_params (x, y, width, height, path);
+    tbo_frame_add_obj (CURRENT_FRAME, pix);
+}
+
+void
 create_tbo_svgimage (const gchar **attribute_names, const gchar **attribute_values)
 {
     SVGImage *svg;
@@ -187,6 +215,10 @@ start_element (GMarkupParseContext *context,
     {
         create_tbo_svgimage (attribute_names, attribute_values);
     }
+    else if (strcmp (element_name, "piximage") == 0)
+    {
+        create_tbo_piximage (attribute_names, attribute_values);
+    }
     else if (strcmp (element_name, "text") == 0)
     {
         create_tbo_text (attribute_names, attribute_values);
index 66063b0..e900f8b 100644 (file)
@@ -21,7 +21,8 @@ void load_custom_stock ()
         {DATA_DIR ICONDIR "frame.svg", TBO_STOCK_FRAME},
         {DATA_DIR ICONDIR "selector.svg", TBO_STOCK_SELECTOR},
         {DATA_DIR ICONDIR "doodle.svg", TBO_STOCK_DOODLE},
-        {DATA_DIR ICONDIR "text.svg", TBO_STOCK_TEXT}
+        {DATA_DIR ICONDIR "text.svg", TBO_STOCK_TEXT},
+        {DATA_DIR ICONDIR "pix.svg", TBO_STOCK_PIX},
     };
 
     int i;
index 1732b5c..c6532f4 100644 (file)
@@ -5,6 +5,7 @@
 #define TBO_STOCK_SELECTOR "tbo-selector"
 #define TBO_STOCK_DOODLE "tbo-doodle"
 #define TBO_STOCK_TEXT "tbo-text"
+#define TBO_STOCK_PIX "tbo-pix"
 
 void load_custom_stock ();
 
index 3126b98..5515088 100644 (file)
--- a/src/dnd.h
+++ b/src/dnd.h
@@ -5,15 +5,13 @@
 #include "tbo-window.h"
 
 enum {
-        TARGET_INT32,
-        TARGET_STRING,
-        TARGET_ROOTWIN
+    TARGET_STRING,
 };
 
 
 static GtkTargetEntry TARGET_LIST[] = {
-        { "STRING",     0, TARGET_STRING },
-        { "text/plain", 0, TARGET_STRING },
+    { "STRING",     0, TARGET_STRING },
+    { "text/plain", 0, TARGET_STRING },
 };
 
 static guint N_TARGETS = G_N_ELEMENTS (TARGET_LIST);
diff --git a/src/piximage.c b/src/piximage.c
new file mode 100644 (file)
index 0000000..6ee8912
--- /dev/null
@@ -0,0 +1,127 @@
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <gtk/gtk.h>
+#include <glib/gi18n.h>
+#include <cairo.h>
+#include "piximage.h"
+#include "tbo-types.h"
+
+#define DOODLE_DIR DATA_DIR "/doodle/"
+
+
+PIXImage *
+tbo_piximage_new ()
+{
+    PIXImage *image;
+    image = malloc (sizeof(PIXImage));
+    image->data = malloc(sizeof(char)*255);
+    image->free = tbo_pix_image_free;
+    image->draw = tbo_pix_image_draw;
+    image->save = tbo_pix_image_save;
+    image->clone = tbo_pix_image_clone;
+    image->type = PIXOBJ;
+    image->flipv = FALSE;
+    image->fliph = FALSE;
+    return image;
+}
+
+PIXImage *
+tbo_piximage_new_width_params (int x,
+                               int y,
+                               int width,
+                               int height,
+                               const char *path)
+{
+    PIXImage *image;
+    image = tbo_piximage_new ();
+    image->x = x;
+    image->y = y;
+    image->width = width;
+    image->height = height;
+    snprintf (image->data, 255, "%s", path);
+    image->type = PIXOBJ;
+    image->flipv = FALSE;
+    image->fliph = FALSE;
+    return image;
+}
+
+void
+tbo_pix_image_free (PIXImage *self)
+{
+    free (self->data);
+    free (self);
+}
+
+void
+tbo_pix_image_draw (PIXImage *self, Frame *frame, cairo_t *cr)
+{
+    int w, h;
+    cairo_surface_t *image;
+
+    image = cairo_image_surface_create_from_png (self->data);
+    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);
+}
+
+void
+tbo_pix_image_save (PIXImage *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, (char*)self->data);
+    fwrite (buffer, sizeof (char), strlen (buffer), file);
+
+    snprintf (buffer, 1024, "   </piximage>\n");
+    fwrite (buffer, sizeof (char), strlen (buffer), file);
+}
+
+PIXImage *
+tbo_pix_image_clone (PIXImage *self)
+{
+    PIXImage *newimage;
+
+    newimage = tbo_piximage_new_width_params (self->x,
+                                              self->y,
+                                              self->width,
+                                              self->height,
+                                              self->data);
+    newimage->angle = self->angle;
+    newimage->flipv = self->flipv;
+    newimage->fliph = self->fliph;
+
+    return newimage;
+}
diff --git a/src/piximage.h b/src/piximage.h
new file mode 100644 (file)
index 0000000..af16e9c
--- /dev/null
@@ -0,0 +1,17 @@
+#ifndef __TBO_PIX_IMAGE__
+#define __TBO_PIX_IMAGE__
+
+#include <cairo.h>
+#include <stdio.h>
+#include "tbo-types.h"
+
+typedef tbo_object PIXImage;
+
+PIXImage * tbo_piximage_new ();
+PIXImage * tbo_piximage_new_width_params (int x, int y, int width, int height, const char *path);
+void tbo_pix_image_free (PIXImage *self);
+void tbo_pix_image_draw (PIXImage *self, Frame *frame, cairo_t *cr);
+void tbo_pix_image_save (PIXImage *self, FILE *file);
+PIXImage * tbo_pix_image_clone (PIXImage *self);
+
+#endif
index 2838927..925ae3b 100644 (file)
@@ -43,6 +43,7 @@ typedef struct
 enum TYPE
 {
     SVGOBJ,
+    PIXOBJ,
     TEXTOBJ,
 };
 
index 367e469..9c49727 100644 (file)
@@ -17,6 +17,7 @@
 #include "selector-tool.h"
 #include "doodle-tool.h"
 #include "text-tool.h"
+#include "piximage.h"
 
 static int SELECTED_TOOL = NONE;
 static GtkActionGroup *ACTION_GROUP = NULL;
@@ -125,6 +126,7 @@ update_toolbar (TboWindow *tbo)
     GtkAction *doodle;
     GtkAction *text;
     GtkAction *new_frame;
+    GtkAction *pix;
 
     if (!ACTION_GROUP)
         return;
@@ -152,17 +154,20 @@ update_toolbar (TboWindow *tbo)
     doodle = gtk_action_group_get_action (ACTION_GROUP, "Doodle");
     text = gtk_action_group_get_action (ACTION_GROUP, "Text");
     new_frame = gtk_action_group_get_action (ACTION_GROUP, "NewFrame");
+    pix = gtk_action_group_get_action (ACTION_GROUP, "Pix");
 
     if (get_frame_view() == NULL)
     {
         gtk_action_set_sensitive (doodle, FALSE);
         gtk_action_set_sensitive (text, FALSE);
+        gtk_action_set_sensitive (pix, FALSE);
         gtk_action_set_sensitive (new_frame, TRUE);
     }
     else
     {
         gtk_action_set_sensitive (doodle, TRUE);
         gtk_action_set_sensitive (text, TRUE);
+        gtk_action_set_sensitive (pix, TRUE);
         gtk_action_set_sensitive (new_frame, FALSE);
     }
 }
@@ -242,6 +247,43 @@ zoom_out (GtkAction *action, TboWindow *tbo)
     return FALSE;
 }
 
+gboolean
+add_pix (GtkAction *action, TboWindow *tbo)
+{
+    GtkWidget *dialog;
+    GtkFileFilter *filter;
+
+    dialog = gtk_file_chooser_dialog_new (_("Add an Image"),
+                     GTK_WINDOW (tbo->window),
+                     GTK_FILE_CHOOSER_ACTION_OPEN,
+                     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                     GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
+                     NULL);
+
+    filter = gtk_file_filter_new ();
+    gtk_file_filter_set_name (filter, _("png"));
+    gtk_file_filter_add_pattern (filter, "*.png");
+    gtk_file_filter_add_pattern (filter, "*.PNG");
+    gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
+    filter = gtk_file_filter_new ();
+    gtk_file_filter_set_name (filter, _("All files"));
+    gtk_file_filter_add_pattern (filter, "*");
+    gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
+
+    if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
+    {
+        char *filename;
+        filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
+        PIXImage *piximage = tbo_piximage_new_width_params (0, 0, 0, 0, filename);
+        tbo_frame_add_obj (get_frame_view(), piximage);
+        update_drawing (tbo);
+        g_free (filename);
+    }
+
+    gtk_widget_destroy (dialog);
+    return FALSE;
+}
+
 static const GtkActionEntry tbo_tools_entries [] = {
     { "NewFileTool", GTK_STOCK_NEW, N_("_New"), "<control>N",
       N_("New Comic"),
@@ -282,6 +324,11 @@ static const GtkActionEntry tbo_tools_entries [] = {
     { "Zoomout", GTK_STOCK_ZOOM_OUT, N_("Zoom out"), "",
       N_("Zoom out"),
       G_CALLBACK (zoom_out) },
+
+    // Png image tool
+    { "Pix", TBO_STOCK_PIX, N_("Image"), "",
+      N_("Image"),
+      G_CALLBACK (add_pix) },
 };
 
 static const GtkToggleActionEntry tbo_tools_toogle_entries [] = {