From ec3ace4a67246ef98af6457dd62d4f49d1302b6f Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Tue, 21 Jul 2015 00:58:59 -0400
Subject: [PATCH] fix regression where autopatching would leave an empty box

---
 pd/nw/pd_canvas.html | 30 +++++++++++++++++++++++++-----
 pd/nw/pdgui.js       | 11 ++++++-----
 pd/src/g_editor.c    | 39 ++++++++++++++++++++++++++++++---------
 3 files changed, 61 insertions(+), 19 deletions(-)

diff --git a/pd/nw/pd_canvas.html b/pd/nw/pd_canvas.html
index 6be95dc7a..e2b8c4ed7 100644
--- a/pd/nw/pd_canvas.html
+++ b/pd/nw/pd_canvas.html
@@ -199,9 +199,10 @@ var canvas_events = (function() {
                     // here.  I want those newlines: although that isn't
                     // standard in Pd-Vanilla, Pd-l2ork uses and preserves
                     // them inside comments
-                    var fudi_msg = text_to_fudi(textbox().innerText);
-                    pdgui.pdsend(name + " stringforobj " + fudi_msg);
-                    pdgui.gui_post("formatted content is " + fudi_msg);
+                    utils.create_obj();
+//                    var fudi_msg = text_to_fudi(textbox().innerText);
+//                    pdgui.pdsend(name + " createobj " + fudi_msg);
+//                    pdgui.gui_post("formatted content is " + fudi_msg);
                     events.mousedown(evt);
                     canvas_events.normal();
                 }
@@ -245,6 +246,18 @@ var canvas_events = (function() {
                 //evt.preventDefault();
                 //return false;
             }
+        },
+        utils = {
+            create_obj: function() {
+                var fudi_msg = text_to_fudi(textbox().innerText);
+                pdgui.pdsend(name + " createobj " + fudi_msg);
+                pdgui.gui_post("formatted content is " + fudi_msg);
+            },
+            set_obj: function() {
+                var fudi_msg = text_to_fudi(textbox().innerText);
+                pdgui.pdsend(name + " setobj " + fudi_msg);
+                pdgui.gui_post("formatted content is " + fudi_msg);
+            }
         }
     ;
 
@@ -346,6 +359,9 @@ var canvas_events = (function() {
         },
         get_state: function() {
             return state;
+        },
+        set_obj: function() {
+            utils.set_obj();
         }
     }
 }());
@@ -775,8 +791,12 @@ function nw_create_patch_window_menus (name) {
     putMenu.append(new nw.MenuItem({
         label: l('menu.object'),
         click: function() {
-                   pdgui.pdsend(name + " dirty 1");
-                   pdgui.pdsend(name + " obj 0");
+            var state = canvas_events.get_state();
+            if (state === 'text' || state === 'floating_text') {
+                canvas_events.set_obj();
+            }
+            pdgui.pdsend(name + " dirty 1");
+            pdgui.pdsend(name + " obj 0");
         },
         key: '1',
         modifiers: "ctrl",
diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index b745f0f45..d8c5ad040 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -3178,7 +3178,7 @@ console.log("obj_tag is " + obj_tag);
             y: y,
             id: obj_tag + i,
             visibility: seqno === i ? 'visible' : 'hidden',
-//            preserveAspectRatio: "xMinYMin meet"
+            preserveAspectRatio: "xMinYMin meet"
         });
         item.setAttributeNS('http://www.w3.org/1999/xlink', 'href', 
             'data:image/' + drawimage_data[obj][i].type + ';base64,' +
@@ -3201,10 +3201,11 @@ function gui_drawimage_index(cid, obj, data, index) {
         last_image,
         image = image_container.childNodes[index],
         last_image = image_container.querySelectorAll('[visibility="visible"]');
-        for (i = 0; i < last_image.length; i++) {
-            configure_item(last_image[i], { visibility: 'hidden' });
-        }
-        configure_item(image, { visibility: 'visible' });
+
+    for (i = 0; i < last_image.length; i++) {
+        configure_item(last_image[i], { visibility: 'hidden' });
+    }
+    configure_item(image, { visibility: 'visible' });
 }
 
 function add_popup(cid, popup) {
diff --git a/pd/src/g_editor.c b/pd/src/g_editor.c
index 0c07af7c1..0ae46f51c 100644
--- a/pd/src/g_editor.c
+++ b/pd/src/g_editor.c
@@ -7646,10 +7646,27 @@ static void canvas_tip(t_canvas *x, t_symbol *s, int argc, t_atom *argv)
     }
 }
 
-static void canvas_stringforobj(t_canvas *x, t_symbol *s, int argc, t_atom *argv)
+static void canvas_stringforobj(t_rtext *rtext, int argc, t_atom *argv)
 {
     int length;
     char *buf;
+    t_binbuf *b = binbuf_new();
+    binbuf_restore(b, argc, argv);
+    binbuf_gettext(b, &buf, &length);
+    /* We hand off "buf" to rtext as
+       its x_buf member, and "length"
+       as x_bufsize. Pd will handle
+       deallocation of those members
+       automatically, so we don't need
+       to free the "buf" here. */
+    rtext_settext(rtext, buf, length);
+    binbuf_free(b);
+}
+
+static void canvas_createobj(t_canvas *x, t_symbol *s, int argc, t_atom *argv)
+{
+//    int length;
+//    char *buf;
     t_gobj *y;
     t_rtext *rtext;
     if (!x->gl_editor) return;
@@ -7657,14 +7674,16 @@ static void canvas_stringforobj(t_canvas *x, t_symbol *s, int argc, t_atom *argv
     {
         if (glist_isselected(x, y) && (rtext = glist_findrtext(x, (t_text *)y)))
         {
-            t_binbuf *b = binbuf_new();
-            binbuf_restore(b, argc, argv);
-            binbuf_gettext(b, &buf, &length);
-            rtext_settext(rtext, buf, length);
-            binbuf_free(b);
+            canvas_stringforobj(rtext, argc, argv);
+//            t_binbuf *b = binbuf_new();
+//            binbuf_restore(b, argc, argv);
+//            binbuf_gettext(b, &buf, &length);
+//            rtext_settext(rtext, buf, length);
+//            binbuf_free(b);
             // Set the dirty flag since we've changed the rtext content...
             x->gl_editor->e_textdirty = 1;
-            glist_deselect(x, y);
+            if (s == gensym("createobj"))
+                glist_deselect(x, y); // instantiate
             break;
         }
     }
@@ -7691,8 +7710,10 @@ void g_editor_setup(void)
     A_GIMME, A_NULL);
     class_addmethod(canvas_class, (t_method)canvas_tip, gensym("echo"),
         A_GIMME, A_NULL);
-    class_addmethod(canvas_class, (t_method)canvas_stringforobj,
-        gensym("stringforobj"), A_GIMME, A_NULL);
+    class_addmethod(canvas_class, (t_method)canvas_createobj,
+        gensym("setobj"), A_GIMME, A_NULL);
+    class_addmethod(canvas_class, (t_method)canvas_createobj,
+        gensym("createobj"), A_GIMME, A_NULL);
 /* ------------------------ menu actions ---------------------------- */
     class_addmethod(canvas_class, (t_method)canvas_menuclose,
         gensym("menuclose"), A_DEFFLOAT, 0);
-- 
GitLab