From bfb265d6048018f569756c48384b99e5ea4debbe Mon Sep 17 00:00:00 2001
From: user <user@user-ThinkPad-X60.(none)>
Date: Fri, 22 May 2015 16:21:03 -0400
Subject: [PATCH] select all text in object box when object receives a click in
 editmode

---
 pd/nw/pd_canvas.html |  6 +++---
 pd/nw/pdgui.js       | 19 +++++++++++++++++--
 pd/src/g_editor.c    | 13 ++++++++-----
 pd/src/g_rtext.c     |  5 +++--
 4 files changed, 31 insertions(+), 12 deletions(-)

diff --git a/pd/nw/pd_canvas.html b/pd/nw/pd_canvas.html
index 46c16ce03..7414dacef 100644
--- a/pd/nw/pd_canvas.html
+++ b/pd/nw/pd_canvas.html
@@ -188,9 +188,9 @@ var canvas_events = (function() {
             floating_text_keypress: function(evt) {
                pdgui.gui_post("leaving floating mode");
                 canvas_events.text();
-                evt.stopPropagation();
-                evt.preventDefault();
-                return false;
+//                evt.stopPropagation();
+//                evt.preventDefault();
+//                return false;
             }
         }
     ;
diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index fb2202b6b..09622bee3 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -3415,9 +3415,23 @@ exports.skin = (function () {
     };
 }());
 
-function gui_textarea(cid, tag, x, y, max_char_width, font_size, state) {
+function select_text(cid, elem) {
+        var range, win = patchwin[cid].window;
+        if (win.document.selection) {
+            range = win.document.body.createTextRange();
+            range.moveToElementText(elem);
+            range.select();
+        } else if (win.getSelection) {
+            range = win.document.createRange();
+            range.selectNodeContents(elem);
+            win.getSelection().addRange(range);
+        }
+}
+
+function gui_textarea(cid, tag, x, y, max_char_width, text, font_size, state) {
     gui_post("x/y is " + x + '/' + y);
     gui_post("state? " + state);
+    var range;
     if (state !== 0) {
         var p = patchwin[cid].window.document.createElement('p');
         configure_item(p, {
@@ -3432,9 +3446,10 @@ function gui_textarea(cid, tag, x, y, max_char_width, font_size, state) {
             max_char_width === 0 ? '60ch' : max_char_width + 'ch');
         p.style.setProperty('min-width',
             max_char_width === 0 ? '3ch' : max_char_width + 'ch');
-//        p.textContent = "Fuck Butts";
+        p.textContent = text;
         patchwin[cid].window.document.body.appendChild(p);
         p.focus();
+        select_text(cid, p);
         if (state === 1) {
             patchwin[cid].window.canvas_events.text();
         } else {
diff --git a/pd/src/g_editor.c b/pd/src/g_editor.c
index 9c4650e9c..59b6fa615 100644
--- a/pd/src/g_editor.c
+++ b/pd/src/g_editor.c
@@ -571,14 +571,14 @@ void glist_deselect(t_glist *x, t_gobj *y)
         int pos = glist_getindex(glist_getcanvas(x), y);
         if (x->gl_editor->e_textedfor)
         {
-            //fprintf(stderr, "e_textedfor\n");
+            fprintf(stderr, "e_textedfor\n");
             fuddy = glist_findrtext(x, (t_text *)y);
             if (x->gl_editor->e_textedfor == fuddy)
             {
-                //fprintf(stderr, "e_textedfor == fuddy\n");
+                fprintf(stderr, "e_textedfor == fuddy\n");
                 if (x->gl_editor->e_textdirty)
                 {
-                    //fprintf(stderr, "textdirty yes\n");
+                    fprintf(stderr, "textdirty yes\n");
                     z = fuddy;
                     canvas_stowconnections(glist_getcanvas(x));
                     glist_checkanddeselectall(x, y);
@@ -618,7 +618,7 @@ void glist_deselect(t_glist *x, t_gobj *y)
 
         if (z)
         {
-            //fprintf(stderr, "setto\n");
+            fprintf(stderr, "setto\n");
             char *buf;
             int bufsize;
 
@@ -7619,7 +7619,7 @@ static void canvas_stringforobj(t_canvas *x, t_symbol *s, int argc, t_atom *argv
     char *buf;
     t_gobj *y;
     t_rtext *rtext;
-    if (!x->gl_editor || argc < 1) return;
+    if (!x->gl_editor) return;
     for (y = x->gl_list; y; y = y->g_next)
     {
         if (glist_isselected(x, y) && (rtext = glist_findrtext(x, (t_text *)y)))
@@ -7630,7 +7630,10 @@ static void canvas_stringforobj(t_canvas *x, t_symbol *s, int argc, t_atom *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);
+            break;
         }
     }
 }
diff --git a/pd/src/g_rtext.c b/pd/src/g_rtext.c
index 79b54200e..04fbeb60b 100644
--- a/pd/src/g_rtext.c
+++ b/pd/src/g_rtext.c
@@ -108,7 +108,7 @@ void rtext_gettext(t_rtext *x, char **buf, int *bufsize)
 
 void rtext_settext(t_rtext *x, char *buf, int bufsize)
 {
-    freebytes(x->x_buf, x->x_bufsize);
+    if (x->x_bufsize) freebytes(x->x_buf, x->x_bufsize);
     x->x_buf = buf;
     x->x_bufsize = bufsize;
 }
@@ -597,12 +597,13 @@ void rtext_activate(t_rtext *x, int state)
     */
 
     widthspec = x->x_text->te_width; // width if any specified
-    gui_vmess("gui_textarea", "xsiiiii",
+    gui_vmess("gui_textarea", "xsiiisii",
         canvas,
         x->x_tag,
         x->x_text->te_xpix,
         x->x_text->te_ypix,
         widthspec,
+        x->x_buf,
         sys_hostfontsize(glist_getfont(glist)),
         state);
 }
-- 
GitLab