From 067d167b8fe43a22afd21846787cf02d1a3e352a Mon Sep 17 00:00:00 2001
From: Ivica Ico Bukvic <ico@vt.edu>
Date: Sat, 7 Dec 2013 20:08:51 -0500
Subject: [PATCH] backported comment resize marker, updated it to support
 pd-l2ork and selection colors, added fix to the resizable objects to support
 abstractions and subcanvases.

---
 pd/src/g_canvas.c | 28 ++++++++++++++++++-----
 pd/src/g_editor.c | 14 ++++++++++++
 pd/src/g_text.c   | 57 +++++++++++++++++++++++++++++++++++++----------
 3 files changed, 81 insertions(+), 18 deletions(-)

diff --git a/pd/src/g_canvas.c b/pd/src/g_canvas.c
index 3f56a7f4f..d7e6505d0 100644
--- a/pd/src/g_canvas.c
+++ b/pd/src/g_canvas.c
@@ -1235,6 +1235,7 @@ static void *subcanvas_new(t_symbol *s)
 {
     t_atom a[6];
     t_canvas *x, *z = canvas_getcurrent();
+    fprintf(stderr,"subcanvas_new current canvas .x%lx\n", (t_int)z);
     if (!*s->s_name) s = gensym("/SUBPATCH/");
     SETFLOAT(a, 0);
     SETFLOAT(a+1, GLIST_DEFCANVASYLOC);
@@ -1810,6 +1811,8 @@ int canvas_open(t_canvas *x, const char *name, const char *ext,
 static void canvas_f(t_canvas *x, t_symbol *s, int argc, t_atom *argv)
 {
     static int warned;
+    //fprintf(stderr,"canvas_f .x%lx\n", (t_int)x);
+    t_canvas *xp = x; //parent window for a special case dealing with subpatches
     t_gobj *g, *g2;
     t_object *ob;
     if (argc > 1 && !warned)
@@ -1817,17 +1820,30 @@ static void canvas_f(t_canvas *x, t_symbol *s, int argc, t_atom *argv)
         post("** ignoring width or font settings from future Pd version **");
         warned = 1;
     }
-    if (!x->gl_list)
-        return;
+    if (!x->gl_list) {
+        if (x->gl_owner && !x->gl_isgraph) {
+            // this means that we are a canvas that was just created
+            // and that our width applies to our appearance on our parent
+            xp = x->gl_owner;
+            for (g = xp->gl_list; g != (t_gobj *)x; g = g->g_next) {
+                //fprintf(stderr,".x%lx .x%lx\n", (t_int)g, (t_int)x);
+                ;
+            }
+            //fprintf(stderr,"done %d\n", (g != NULL ? 1: 0));
+        } else return;
+    } else {
     for (g = x->gl_list; g2 = g->g_next; g = g2)
         ;
-    if (ob = pd_checkobject(&g->g_pd))
+    }
+    //fprintf(stderr,"is canvas_class? %d\n", (pd_class(&g->g_pd) == canvas_class ? 1:0));
+    if ((ob = pd_checkobject(&g->g_pd)) || pd_class(&g->g_pd) == canvas_class)
     {
+        //fprintf(stderr,"f received\n");
         ob->te_width = atom_getfloatarg(0, argc, argv);
-        if (glist_isvisible(x))
+        if (glist_isvisible(xp))
         {
-            gobj_vis(g, x, 0);
-            gobj_vis(g, x, 1);
+            gobj_vis(g, xp, 0);
+            gobj_vis(g, xp, 1);
         }
     }
 }
diff --git a/pd/src/g_editor.c b/pd/src/g_editor.c
index 94068132e..2729372fa 100644
--- a/pd/src/g_editor.c
+++ b/pd/src/g_editor.c
@@ -6366,8 +6366,17 @@ void canvas_editmode(t_canvas *x, t_floatarg fyesplease)
     x->gl_edit = !x->gl_edit;
     if (x->gl_edit && glist_isvisible(x) && glist_istoplevel(x)){
 		//dpsaha@vt.edu add the resize blobs on GOP
+		t_gobj *g;
+        t_object *ob;
 		if (x->gl_goprect)	canvas_draw_gop_resize_hooks(x);
 		canvas_setcursor(x, CURSOR_EDITMODE_NOTHING);
+        for (g = x->gl_list; g; g = g->g_next)
+            if ((ob = pd_checkobject(&g->g_pd)) && ob->te_type == T_TEXT)
+        {
+            t_rtext *y = glist_findrtext(x, ob);
+            text_drawborder(ob, x,
+                rtext_gettag(y), rtext_width(y), rtext_height(y), 1);
+        }
 	}
     else
     {
@@ -6375,6 +6384,11 @@ void canvas_editmode(t_canvas *x, t_floatarg fyesplease)
         glist_noselect(x);
         if (glist_isvisible(x) && glist_istoplevel(x))
         {
+        	if (glist_isvisible(x) && glist_istoplevel(x))
+        	{
+            	canvas_setcursor(x, CURSOR_RUNMODE_NOTHING);
+            	sys_vgui(".x%lx.c delete commentbar\n", glist_getcanvas(x));
+        	}
             // jsarlo
             if (x->gl_editor->canvas_cnct_inlet_tag[0] != 0)
             {
diff --git a/pd/src/g_text.c b/pd/src/g_text.c
index 2ae760b57..08ce6b237 100644
--- a/pd/src/g_text.c
+++ b/pd/src/g_text.c
@@ -105,7 +105,7 @@ void glist_text(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
 extern t_pd *newest;
 void canvas_getargs(int *argcp, t_atom **argvp);
 
-static void canvas_objtext(t_glist *gl, int xpix, int ypix, int selected,
+static void canvas_objtext(t_glist *gl, int xpix, int ypix, int width, int selected,
     t_binbuf *b)
 {
 	//fprintf(stderr,"canvas_objtext\n");
@@ -167,7 +167,7 @@ static void canvas_objtext(t_glist *gl, int xpix, int ypix, int selected,
 
     x->te_xpix = xpix;
     x->te_ypix = ypix;
-    x->te_width = 0;
+    x->te_width = width;
     x->te_type = T_OBJECT;
 	/* let's see if iemgui objects did not already set the value to 1, otherwise set it explicitly to 0 */
 	if (x->te_iemgui != 1)
@@ -275,7 +275,7 @@ void canvas_obj(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
         t_binbuf *b = binbuf_new();
         binbuf_restore(b, argc-2, argv+2);
         canvas_objtext(gl, atom_getintarg(0, argc, argv),
-            atom_getintarg(1, argc, argv), 0, b);
+            atom_getintarg(1, argc, argv), 0, 0, b);
     }
         /* JMZ: don't go into interactive mode in a closed canvas */
     else if (!glist_isvisible(gl))
@@ -287,7 +287,7 @@ void canvas_obj(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
         int connectme, xpix, ypix, indx, nobj;
         canvas_howputnew(gl, &connectme, &xpix, &ypix, &indx, &nobj);
         pd_vmess(&gl->gl_pd, gensym("editmode"), "i", 1);
-        canvas_objtext(gl, xpix, ypix, 1, b);
+        canvas_objtext(gl, xpix, ypix, 0, 1, b);
         if (connectme == 1) {
 			//fprintf(stderr,"canvas_obj calls canvas_connect\n");
 			connect_exception = 1;
@@ -323,7 +323,7 @@ void canvas_obj_abstraction_from_menu(t_glist *gl, t_symbol *s, int argc, t_atom
 	if (sys_k12_mode)
 		pd_vmess (&gl->gl_pd, gensym("tooltips"), "i", 1);
 #endif
-    canvas_objtext(gl, xpix+atom_getintarg(1, argc, argv), ypix+atom_getintarg(2, argc, argv), 1, b);
+    canvas_objtext(gl, xpix+atom_getintarg(1, argc, argv), ypix+atom_getintarg(2, argc, argv), 0, 1, b);
 
 	// the object is now the last on the glist so we locate it and send it loadbang
 	// we know we have at least one object since we just created one so we don't check for y being valid
@@ -375,7 +375,7 @@ void canvas_iemguis(t_glist *gl, t_symbol *guiobjname)
     glist_noselect(gl);
     SETSYMBOL(&at, guiobjname);
     binbuf_restore(b, 1, &at);
-	canvas_objtext(gl, xpix, ypix, 1, b);
+	canvas_objtext(gl, xpix, ypix, 0, 1, b);
     if (connectme == 1)
         canvas_connect(gl, indx, 0, nobj, 0);
     //glist_getnextxy(gl, &xpix, &ypix);
@@ -1375,10 +1375,13 @@ static void text_select(t_gobj *z, t_glist *glist, int state)
     t_rtext *y = glist_findrtext(glist, x);
     char *outline;
     rtext_select(y, state);
-	//fprintf(stderr,"text_select %s\n", rtext_gettag(y));
-    if (pd_class(&x->te_pd) == text_class)
+	//fprintf(stderr,"text_select %s %d\n", rtext_gettag(y), state);
+
+    // text_class is either a comment or an object that failed to creates
+    // so we distinguish between it and comment using T_TEXT type check
+    if (pd_class(&x->te_pd) == text_class && x->te_type != T_TEXT) {
         outline = "$dash_outline";
-    else
+    } else
         outline = "$box_outline";
 	//fprintf(stderr,"text_select isvisible=%d shouldvis=%d istoplevel=%d\n", glist_isvisible(glist), gobj_shouldvis(&x->te_g, glist), glist_istoplevel(glist));
     if (gobj_shouldvis(&x->te_g, glist)) {
@@ -1886,6 +1889,21 @@ void text_drawborder(t_text *x, t_glist *glist,
                 glist_getcanvas(glist), tag,
                 x1, y1,  x2-4, y1,  x2, y1+4,  x2, y2,  x1, y2,  x1, y1);
     }
+        /* for comments, just draw a bar on RHS if unlocked; when a visible
+        canvas is unlocked we have to call this anew on all comments, and when
+        locked we erase them all via the annoying "commentbar" tag. */
+    else if (x->te_type == T_TEXT && glist->gl_edit)
+    {
+        if (firsttime) {
+            sys_vgui(".x%lx.c create pline\
+ %d %d %d %d -tags [list %sR commentbar] -stroke $obj_box_fill\n",
+                glist_getcanvas(glist),
+                x2, y1,  x2, y2, tag);
+        } else {
+            sys_vgui(".x%lx.c coords %sR %d %d %d %d\n",
+                glist_getcanvas(glist), tag, x2, y1,  x2, y2);
+        }
+    }
 
 	/* draw inlets/outlets */    
     if (ob = pd_checkobject(&x->te_pd)) {
@@ -1955,6 +1973,21 @@ void text_drawborder_withtag(t_text *x, t_glist *glist,
                      x1, y1,  x2-atom_draw_const, y1,  x2, y1+atom_draw_const,  
                      x2, y2,  x1, y2,  x1, y1, 
                     tag, tag);
+    }
+        /* for comments, just draw a bar on RHS if unlocked; when a visible
+        canvas is unlocked we have to call this anew on all comments, and when
+        locked we erase them all via the annoying "commentbar" tag. */
+    else if (x->te_type == T_TEXT && glist->gl_edit)
+    {
+        if (firsttime) {
+            sys_vgui(".x%lx.c create pline\
+ %d %d %d %d -tags [list %sR commentbar] -stroke $obj_box_fill\n",
+                glist_getcanvas(glist),
+                x2, y1,  x2, y2, tag);
+        } else {
+            sys_vgui(".x%lx.c coords %sR %d %d %d %d\n",
+                glist_getcanvas(glist), tag, x2, y1,  x2, y2);
+        }
     }
         /* draw inlets/outlets */
     
@@ -1985,7 +2018,7 @@ void glist_eraseiofor(t_glist *glist, t_object *ob, char *tag)
 
 void text_eraseborder(t_text *x, t_glist *glist, char *tag)
 {
-    if (x->te_type == T_TEXT) return;
+    //if (x->te_type == T_TEXT) return;
     sys_vgui(".x%lx.c delete %sR\n",
         glist_getcanvas(glist), tag);
     glist_eraseiofor(glist, x, tag);
@@ -2003,7 +2036,7 @@ void text_setto(t_text *x, t_glist *glist, char *buf, int bufsize, int pos)
     if (x->te_type == T_OBJECT)
     {
         t_binbuf *b = binbuf_new();
-        int natom1, natom2;
+        int natom1, natom2, widthwas = x->te_width;
         t_atom *vec1, *vec2;
         binbuf_text(b, buf, bufsize);
         natom1 = binbuf_getnatom(x->te_binbuf);
@@ -2047,7 +2080,7 @@ void text_setto(t_text *x, t_glist *glist, char *buf, int bufsize, int pos)
 		        int xwas = x->te_xpix, ywas = x->te_ypix;
 				canvas_eraselinesfor(glist, x);
 		        glist_delete(glist, &x->te_g);
-		        canvas_objtext(glist, xwas, ywas, 0, b);
+		        canvas_objtext(glist, xwas, ywas, widthwas, 0, b);
 		            /* if it's an abstraction loadbang it here */
 		        if (newest && pd_class(newest) == canvas_class)
 		            canvas_loadbang((t_canvas *)newest);
-- 
GitLab