Code

API change: render methods now take a cairo_t (not yet used)
[inkscape.git] / src / dialogs / clonetiler.cpp
index e47437c724c61e0c86ebd2ff44bdd518164d02e8..5cd5df143fe19667f9d61ee3fc10bc19e7758207 100644 (file)
@@ -4,9 +4,10 @@
  * Clone tiling dialog
  *
  * Authors:
- *   bulia byak <buliabyak@users.sf.net>
+ *   bulia byak <buliabyak@users.sf.net> 
+ *   Johan Engelen <goejendaagh@zonnet.nl>
  *
- * Copyright (C) 2004-2005 Authors
+ * Copyright (C) 2004-2006 Authors
  * Released under GNU GPL
  */
 
 
 #include "ui/widget/color-picker.h"
 
+#include "../sp-filter.h"
+#include "../filter-chemistry.h"
+
+#define MIN_ONSCREEN_DISTANCE 50
+
 static GtkWidget *dlg = NULL;
 static win_data wd;
 
@@ -780,6 +786,11 @@ clonetiler_trace_pick (NR::Rect box)
 
     /* Set up pixblock */
     guchar *px = g_new(guchar, 4 * width * height);
+
+    if (px == NULL) {
+        return 0; // buffer is too big or too small, cannot pick, so return 0
+    }
+
     memset(px, 0x00, 4 * width * height);
 
     /* Render */
@@ -787,7 +798,7 @@ clonetiler_trace_pick (NR::Rect box)
     nr_pixblock_setup_extern( &pb, NR_PIXBLOCK_MODE_R8G8B8A8N,
                               ibox.x0, ibox.y0, ibox.x1, ibox.y1,
                               px, 4 * width, FALSE, FALSE );
-    nr_arena_item_invoke_render( trace_root, &ibox, &pb,
+    nr_arena_item_invoke_render(NULL, trace_root, &ibox, &pb,
                                  NR_ARENA_ITEM_RENDER_NO_CACHE );
 
     double R = 0, G = 0, B = 0, A = 0;
@@ -863,7 +874,8 @@ clonetiler_unclump (GtkWidget *widget, void *)
 
     g_slist_free (to_unclump);
 
-    sp_document_done (sp_desktop_document (desktop));
+    sp_document_done (sp_desktop_document (desktop), SP_VERB_DIALOG_CLONETILER, 
+                      _("Unclump tiled clones"));
 }
 
 static guint
@@ -915,7 +927,8 @@ clonetiler_remove (GtkWidget *widget, void *, bool do_undo = true)
     clonetiler_change_selection (NULL, selection, dlg);
 
     if (do_undo)
-        sp_document_done (sp_desktop_document (desktop));
+        sp_document_done (sp_desktop_document (desktop), SP_VERB_DIALOG_CLONETILER, 
+                          _("Delete tiled clones"));
 }
 
 static NR::Rect
@@ -1004,6 +1017,12 @@ clonetiler_apply (GtkWidget *widget, void *)
     int alternate_roty = prefs_get_int_attribute (prefs_path, "alternate_roty", 0);
     double rand_rot = 0.01 * prefs_get_double_attribute_limited (prefs_path, "rand_rot", 0, 0, 100);
 
+    double d_blur_per_y = 0.01 * prefs_get_double_attribute_limited (prefs_path, "d_blur_per_y", 0, 0, 100);
+    double d_blur_per_x = 0.01 * prefs_get_double_attribute_limited (prefs_path, "d_blur_per_x", 0, 0, 100);
+    int alternate_blury = prefs_get_int_attribute (prefs_path, "alternate_blury", 0);
+    int alternate_blurx = prefs_get_int_attribute (prefs_path, "alternate_blurx", 0);
+    double rand_blur = 0.01 * prefs_get_double_attribute_limited (prefs_path, "rand_blur", 0, 0, 100);
+
     double d_opacity_per_y = 0.01 * prefs_get_double_attribute_limited (prefs_path, "d_opacity_per_y", 0, 0, 100);
     double d_opacity_per_x = 0.01 * prefs_get_double_attribute_limited (prefs_path, "d_opacity_per_x", 0, 0, 100);
     int alternate_opacityy = prefs_get_int_attribute (prefs_path, "alternate_opacityy", 0);
@@ -1031,8 +1050,8 @@ clonetiler_apply (GtkWidget *widget, void *)
     int ymax = prefs_get_int_attribute (prefs_path, "ymax", 2);
 
     int fillrect = prefs_get_int_attribute (prefs_path, "fillrect", 0);
-    double fillwidth = prefs_get_double_attribute_limited (prefs_path, "fillwidth", 50, 0, 6000);
-    double fillheight = prefs_get_double_attribute_limited (prefs_path, "fillheight", 50, 0, 6000);
+    double fillwidth = prefs_get_double_attribute_limited (prefs_path, "fillwidth", 50, 0, 1e6);
+    double fillheight = prefs_get_double_attribute_limited (prefs_path, "fillheight", 50, 0, 1e6);
 
     int dotrace = prefs_get_int_attribute (prefs_path, "dotrace", 0);
     int pick = prefs_get_int_attribute (prefs_path, "pick", 0);
@@ -1079,6 +1098,7 @@ clonetiler_apply (GtkWidget *widget, void *)
 
     NR::Point cur = NR::Point (0, 0);
     NR::Rect bbox_original = NR::Rect (NR::Point (c[NR::X] - w/2, c[NR::Y] - h/2), NR::Point (c[NR::X] + w/2, c[NR::Y] + h/2));
+    double perimeter_original = (w + h)/4;
 
     for (int x = 0;
          fillrect?
@@ -1132,12 +1152,23 @@ clonetiler_apply (GtkWidget *widget, void *)
                 sp_svg_write_color(color_string, 32, SP_RGBA32_F_COMPOSE(rgb[0], rgb[1], rgb[2], 1.0));
             }
 
-            // Opacity tab
+            // Blur
+            double blur = 0.0;
+            {
+            int eff_x = (alternate_blurx? (x%2) : (x));
+            int eff_y = (alternate_blury? (y%2) : (y));
+            blur =  (d_blur_per_x * eff_x + d_blur_per_y * eff_y + rand_blur * g_random_double_range (-1, 1));
+            blur = CLAMP (blur, 0, 1);
+            }
+
+            // Opacity
             double opacity = 1.0;
+            {
             int eff_x = (alternate_opacityx? (x%2) : (x));
             int eff_y = (alternate_opacityy? (y%2) : (y));
             opacity = 1 - (d_opacity_per_x * eff_x + d_opacity_per_y * eff_y + rand_opacity * g_random_double_range (-1, 1));
             opacity = CLAMP (opacity, 0, 1);
+            }
 
             // Trace tab
             if (dotrace) {
@@ -1242,7 +1273,7 @@ clonetiler_apply (GtkWidget *widget, void *)
             }
 
             // Create the clone
-            Inkscape::XML::Node *clone = sp_repr_new("svg:use");
+            Inkscape::XML::Node *clone = obj_repr->document()->createElement("svg:use");
             clone->setAttribute("x", "0");
             clone->setAttribute("y", "0");
             clone->setAttribute("inkscape:tiled-clone-of", id_href);
@@ -1255,12 +1286,9 @@ clonetiler_apply (GtkWidget *widget, void *)
                 center_set = true;
             }
 
-            gchar affinestr[80];
-            if (sp_svg_transform_write(affinestr, 79, t)) {
-                clone->setAttribute("transform", affinestr);
-            } else {
-                clone->setAttribute("transform", NULL);
-            }
+            gchar *affinestr=sp_svg_transform_write(t);
+            clone->setAttribute("transform", affinestr);
+            g_free(affinestr);
 
             if (opacity < 1.0) {
                 sp_repr_set_css_double(clone, "opacity", opacity);
@@ -1274,6 +1302,17 @@ clonetiler_apply (GtkWidget *widget, void *)
             // add the new clone to the top of the original's parent
             SP_OBJECT_REPR(parent)->appendChild(clone);
 
+            if (blur > 0.0) {
+                SPObject *clone_object = sp_desktop_document(desktop)->getObjectByRepr(clone);
+                double perimeter = perimeter_original * t.expansion();
+                double radius = blur * perimeter;
+                // it's hard to figure out exact width/height of the tile without having an object
+                // that we can take bbox of; however here we only need a lower bound so that blur
+                // margins are not too small, and the perimeter should work
+                SPFilter *constructed = new_filter_gaussian_blur(sp_desktop_document(desktop), radius, t.expansion(), t.expansionX(), t.expansionY(), perimeter, perimeter);
+                sp_style_set_property_url (clone_object, "filter", SP_OBJECT(constructed), false);
+            }
+
             if (center_set) {
                 SPObject *clone_object = sp_desktop_document(desktop)->getObjectByRepr(clone);
                 if (clone_object && SP_IS_ITEM(clone_object)) {
@@ -1294,7 +1333,8 @@ clonetiler_apply (GtkWidget *widget, void *)
 
     clonetiler_change_selection (NULL, selection, dlg);
 
-    sp_document_done(sp_desktop_document(desktop));
+    sp_document_done(sp_desktop_document(desktop), SP_VERB_DIALOG_CLONETILER, 
+                     _("Create tiled clones"));
 }
 
 static GtkWidget *
@@ -1594,8 +1634,8 @@ clonetiler_dialog (void)
 
         dlg = sp_window_new (title, TRUE);
         if (x == -1000 || y == -1000) {
-            x = prefs_get_int_attribute (prefs_path, "x", 0);
-            y = prefs_get_int_attribute (prefs_path, "y", 0);
+            x = prefs_get_int_attribute (prefs_path, "x", -1000);
+            y = prefs_get_int_attribute (prefs_path, "y", -1000);
         }
         
         if (w ==0 || h == 0) {
@@ -1603,19 +1643,19 @@ clonetiler_dialog (void)
             h = prefs_get_int_attribute (prefs_path, "h", 0);
         }
         
-        if (x<0) x=0;
-        if (y<0) y=0;
+//        if (x<0) x=0;
+//        if (y<0) y=0;
 
-        if (x != 0 || y != 0) {
+        if (w && h) {
+            gtk_window_resize ((GtkWindow *) dlg, w, h);
+        }
+        if (x >= 0 && y >= 0 && (x < (gdk_screen_width()-MIN_ONSCREEN_DISTANCE)) && (y < (gdk_screen_height()-MIN_ONSCREEN_DISTANCE))) {
             gtk_window_move ((GtkWindow *) dlg, x, y);
         
         } else {
             gtk_window_set_position(GTK_WINDOW(dlg), GTK_WIN_POS_CENTER);
         }
         
-        if (w && h) {
-            gtk_window_resize ((GtkWindow *) dlg, w, h);
-        }
         
         sp_transientize (dlg);
         wd.win = dlg;
@@ -1979,38 +2019,39 @@ clonetiler_dialog (void)
         }
 
 
-// Opacity
+// Blur and opacity
         {
-            GtkWidget *vb = clonetiler_new_tab (nb, _("_Opacity"));
+            GtkWidget *vb = clonetiler_new_tab (nb, _("_Blur & opacity"));
 
             GtkWidget *table = clonetiler_table_x_y_rand (1);
             gtk_box_pack_start (GTK_BOX (vb), table, FALSE, FALSE, 0);
 
-            // Dissolve
+
+            // Blur
             {
                 GtkWidget *l = gtk_label_new ("");
-                gtk_label_set_markup (GTK_LABEL(l), _("<b>Fade out:</b>"));
+                gtk_label_set_markup (GTK_LABEL(l), _("<b>Blur:</b>"));
                 gtk_size_group_add_widget(table_row_labels, l);
                 clonetiler_table_attach (table, l, 1, 2, 1);
             }
 
             {
                 GtkWidget *l = clonetiler_spinbox (tt,
-                                                   _("Decrease tile opacity by this percentage for each row"), "d_opacity_per_y",
+                                                   _("Blur tiles by this percentage for each row"), "d_blur_per_y",
                                                    0, 100, "%");
                 clonetiler_table_attach (table, l, 0, 2, 2);
             }
 
             {
                 GtkWidget *l = clonetiler_spinbox (tt,
-                                                   _("Decrease tile opacity by this percentage for each column"), "d_opacity_per_x",
+                                                   _("Blur tiles by this percentage for each column"), "d_blur_per_x",
                                                    0, 100, "%");
                 clonetiler_table_attach (table, l, 0, 2, 3);
             }
 
             {
                 GtkWidget *l = clonetiler_spinbox (tt,
-                                                   _("Randomize the tile opacity by this percentage"), "rand_opacity",
+                                                   _("Randomize the tile blur by this percentage"), "rand_blur",
                                                    0, 100, "%");
                 clonetiler_table_attach (table, l, 0, 2, 4);
             }
@@ -2024,14 +2065,63 @@ clonetiler_dialog (void)
             }
 
             {
-                GtkWidget *l = clonetiler_checkbox (tt, _("Alternate the sign of opacity change for each row"), "alternate_opacityy");
+                GtkWidget *l = clonetiler_checkbox (tt, _("Alternate the sign of blur change for each row"), "alternate_blury");
                 clonetiler_table_attach (table, l, 0, 3, 2);
             }
 
             {
-                GtkWidget *l = clonetiler_checkbox (tt, _("Alternate the sign of opacity change for each column"), "alternate_opacityx");
+                GtkWidget *l = clonetiler_checkbox (tt, _("Alternate the sign of blur change for each column"), "alternate_blurx");
                 clonetiler_table_attach (table, l, 0, 3, 3);
             }
+
+
+
+            // Dissolve
+            {
+                GtkWidget *l = gtk_label_new ("");
+                gtk_label_set_markup (GTK_LABEL(l), _("<b>Fade out:</b>"));
+                gtk_size_group_add_widget(table_row_labels, l);
+                clonetiler_table_attach (table, l, 1, 4, 1);
+            }
+
+            {
+                GtkWidget *l = clonetiler_spinbox (tt,
+                                                   _("Decrease tile opacity by this percentage for each row"), "d_opacity_per_y",
+                                                   0, 100, "%");
+                clonetiler_table_attach (table, l, 0, 4, 2);
+            }
+
+            {
+                GtkWidget *l = clonetiler_spinbox (tt,
+                                                   _("Decrease tile opacity by this percentage for each column"), "d_opacity_per_x",
+                                                   0, 100, "%");
+                clonetiler_table_attach (table, l, 0, 4, 3);
+            }
+
+            {
+                GtkWidget *l = clonetiler_spinbox (tt,
+                                                   _("Randomize the tile opacity by this percentage"), "rand_opacity",
+                                                   0, 100, "%");
+                clonetiler_table_attach (table, l, 0, 4, 4);
+            }
+
+            { // alternates
+                GtkWidget *l = gtk_label_new ("");
+                // TRANSLATORS: "Alternate" is a verb here
+                gtk_label_set_markup (GTK_LABEL(l), _("<small>Alternate:</small>"));
+                gtk_size_group_add_widget(table_row_labels, l);
+                clonetiler_table_attach (table, l, 1, 5, 1);
+            }
+
+            {
+                GtkWidget *l = clonetiler_checkbox (tt, _("Alternate the sign of opacity change for each row"), "alternate_opacityy");
+                clonetiler_table_attach (table, l, 0, 5, 2);
+            }
+
+            {
+                GtkWidget *l = clonetiler_checkbox (tt, _("Alternate the sign of opacity change for each column"), "alternate_opacityx");
+                clonetiler_table_attach (table, l, 0, 5, 3);
+            }
         }
 
 
@@ -2432,7 +2522,7 @@ clonetiler_dialog (void)
     
                 {
                     // Width spinbutton 
-                    GtkObject *a = gtk_adjustment_new (0.0, -SP_DESKTOP_SCROLL_LIMIT, SP_DESKTOP_SCROLL_LIMIT, 1.0, 10.0, 10.0);
+                    GtkObject *a = gtk_adjustment_new (0.0, -1e6, 1e6, 1.0, 10.0, 10.0);
                     sp_unit_selector_add_adjustment (SP_UNIT_SELECTOR (u), GTK_ADJUSTMENT (a));
 
                     double value = prefs_get_double_attribute (prefs_path, "fillwidth", 50);
@@ -2456,7 +2546,7 @@ clonetiler_dialog (void)
 
                 {
                     // Height spinbutton
-                    GtkObject *a = gtk_adjustment_new (0.0, -SP_DESKTOP_SCROLL_LIMIT, SP_DESKTOP_SCROLL_LIMIT, 1.0, 10.0, 10.0);
+                    GtkObject *a = gtk_adjustment_new (0.0, -1e6, 1e6, 1.0, 10.0, 10.0);
                     sp_unit_selector_add_adjustment (SP_UNIT_SELECTOR (u), GTK_ADJUSTMENT (a));
 
                     double value = prefs_get_double_attribute (prefs_path, "fillheight", 50);