From f7bf51a2f41b0e47e3d89fa4cb577b363f3f7051 Mon Sep 17 00:00:00 2001
From: Ivica Ico Bukvic <ico@monsoon-hp.(none)>
Date: Thu, 26 Jan 2012 00:12:43 -0500
Subject: [PATCH] Fixed segfault when creating undo for array apply (e.g. when
 changing array size). Also expanded moving by tag to arrays and scalars. Now,
 the entire array even with a ton of points is moving like knife through the
 butter :-)

---
 src/g_editor.c   |  2 +-
 src/g_graph.c    | 27 +++++++++++++--------------
 src/g_scalar.c   |  2 +-
 src/g_template.c | 29 ++++++++++++++++++-----------
 src/g_text.c     |  6 ++++--
 src/pd.tk        |  2 +-
 6 files changed, 38 insertions(+), 30 deletions(-)

diff --git a/src/g_editor.c b/src/g_editor.c
index 9191553fa..bf45c6825 100644
--- a/src/g_editor.c
+++ b/src/g_editor.c
@@ -1049,7 +1049,7 @@ void *canvas_undo_set_apply(t_canvas *x, int n)
 	if (!x->gl_edit)
 		canvas_editmode(x, 1);
 	obj = glist_nth(x, n);
-	if (!glist_isselected(x, obj))
+	if (obj && !glist_isselected(x, obj))
 		glist_select(x, obj);
     int nnotsel= glist_selectionindex(x, 0, 0); /* get number of all items for the offset below */
     buf = (t_undo_apply *)getbytes(sizeof(*buf));
diff --git a/src/g_graph.c b/src/g_graph.c
index 42bcfd9e0..529ad5e78 100644
--- a/src/g_graph.c
+++ b/src/g_graph.c
@@ -1052,19 +1052,11 @@ static void graph_displace_withtag(t_gobj *z, t_glist *glist, int dx, int dy)
 {
     t_glist *x = (t_glist *)z;
     if (!x->gl_isgraph)
-        text_widgetbehavior.w_displacefn(z, glist, dx, dy);
+        text_widgetbehavior.w_displacefnwtag(z, glist, dx, dy);
     else
     {
         x->gl_obj.te_xpix += dx;
         x->gl_obj.te_ypix += dy;
-		/*char tag[80];
-		sprintf(tag, "%s", rtext_gettag(glist_findrtext((x->gl_owner ? x->gl_owner: x), &x->gl_obj)));
-        sys_vgui(".x%lx.c move %s %d %d\n",
-            glist_getcanvas(x->gl_owner), tag, dx, dy);
-        sys_vgui(".x%lx.c move %sR %d %d\n",
-            glist_getcanvas(x->gl_owner), tag, dx, dy);*/
-        //glist_redraw(x);
-		//gobj_select(z, glist, 1);
         canvas_fixlinesfor(glist_getcanvas(glist), &x->gl_obj);
     }
 }
@@ -1092,16 +1084,23 @@ static void graph_displace(t_gobj *z, t_glist *glist, int dx, int dy)
 
 static void graph_select(t_gobj *z, t_glist *glist, int state)
 {
-	//fprintf(stderr,"graph_select\n");
+	//fprintf(stderr,"graph_select .x%lx...\n", (t_int)z);
     t_glist *x = (t_glist *)z;
     if (!x->gl_isgraph)
         text_widgetbehavior.w_selectfn(z, glist, state);
-    else if(glist_istoplevel(glist))
+    else //if(glist_istoplevel(glist))
     {
+		//fprintf(stderr,"...yes\n");
         t_rtext *y = glist_findrtext(glist, &x->gl_obj);
         if (canvas_showtext(x))
             rtext_select(y, state);
-        sys_vgui(".x%lx.c itemconfigure %sR -fill %s\n", glist, 
+		t_glist *canvas;
+		if (!glist_istoplevel(glist)) {
+			canvas = glist_getcanvas(glist);
+		} else {
+			canvas = glist;
+		}
+        sys_vgui(".x%lx.c itemconfigure %sR -fill %s\n", canvas, 
                  rtext_gettag(y), (state? "$select_color" : "$graph_outline"));
 /*
         sys_vgui(".x%lx.c itemconfigure graph%lx -fill %s\n",
@@ -1109,13 +1108,13 @@ static void graph_select(t_gobj *z, t_glist *glist, int state)
                  (state? "$select_color" : "$graph_outline"));
 */
         sys_vgui(".x%lx.c itemconfigure %s -fill %s\n",
-                 glist, rtext_gettag(y), 
+                 canvas, rtext_gettag(y), 
                  (state? "$select_color" : "black"));
 		t_gobj *g;
 		if (x->gl_list)
 			for (g = x->gl_list; g; g = g->g_next)
 				gobj_select(g, x, state);
-		sys_vgui("pdtk_select_all_gop_widgets .x%lx %s %d\n", glist, rtext_gettag(glist_findrtext(glist, &x->gl_obj)), state);
+		sys_vgui("pdtk_select_all_gop_widgets .x%lx %s %d\n", canvas, rtext_gettag(glist_findrtext(glist, &x->gl_obj)), state);
     }
 }
 
diff --git a/src/g_scalar.c b/src/g_scalar.c
index 0896eec17..b6a7e19cd 100644
--- a/src/g_scalar.c
+++ b/src/g_scalar.c
@@ -204,7 +204,7 @@ static void scalar_drawselectrect(t_scalar *x, t_glist *glist, int state)
         scalar_getrect(&x->sc_gobj, glist, &x1, &y1, &x2, &y2);
         x1--; x2++; y1--; y2++;
         sys_vgui(".x%lx.c create line %d %d %d %d %d %d %d %d %d %d \
-            -width 0 -fill $select_color -tags select%lx\n",
+            -width 0 -fill $select_color -tags {select%lx selected}\n",
                 glist_getcanvas(glist), x1, y1, x1, y2, x2, y2, x2, y1, x1, y1,
                 x);
     }
diff --git a/src/g_template.c b/src/g_template.c
index c6dcadd3f..e7279d92b 100644
--- a/src/g_template.c
+++ b/src/g_template.c
@@ -1173,14 +1173,16 @@ static void curve_vis(t_gobj *z, t_glist *glist,
                 fill, outline);
             else sys_vgui("-fill %s\\\n", outline);
             if (flags & BEZ) sys_vgui("-smooth 1\\\n");
-            sys_vgui("-tags curve%lx\n", data);
+            sys_vgui("-tags .x%lx%s.curve%lx\n", glist_getcanvas(glist), 
+				(glist_istoplevel(glist) ? "bogus" : rtext_gettag(glist_findrtext(glist_getcanvas(glist), &glist->gl_obj))),data);
         }
         else post("warning: curves need at least two points to be graphed");
     }
     else
     {
-        if (n > 1) sys_vgui(".x%lx.c delete curve%lx\n",
-            glist_getcanvas(glist), data);      
+        if (n > 1) sys_vgui(".x%lx.c delete .x%lx%s.curve%lx\n",
+            glist_getcanvas(glist), glist_getcanvas(glist), 
+			(glist_istoplevel(glist) ? "bogus" : rtext_gettag(glist_findrtext(glist_getcanvas(glist), &glist->gl_obj))),data);      
     }
 }
 
@@ -1646,6 +1648,7 @@ static void plot_vis(t_gobj *z, t_glist *glist,
     t_word *data, t_template *template, t_float basex, t_float basey,
     int tovis)
 {
+	//fprintf(stderr,"plot_vis %lx %lx %lx %s\n", (t_int)z, (t_int)glist, (t_int)glist_getcanvas(glist), rtext_gettag(glist_findrtext(glist_getcanvas(glist), &glist->gl_obj)));
     t_plot *x = (t_plot *)z;
     int elemsize, yonset, wonset, xonset, i;
     t_canvas *elemtemplatecanvas;
@@ -1717,13 +1720,14 @@ static void plot_vis(t_gobj *z, t_glist *glist,
                 {
 					//we subtract 1 from y to keep it in sync with the rest of the types of templates
                     sys_vgui(
-".x%lx.c create rectangle %d %d %d %d -fill black -width 0  -tags plot%lx\n",
+".x%lx.c create rectangle %d %d %d %d -fill black -width 0  -tags .x%lx%s.plot%lx\n",
                         glist_getcanvas(glist),
                         ixpix, (int)glist_ytopixels(glist, 
                             basey + fielddesc_cvttocoord(yfielddesc, minyval)) - 1,
                         inextx, (int)(glist_ytopixels(glist, 
                             basey + fielddesc_cvttocoord(yfielddesc, maxyval))
-                                + linewidth) - 1, data);
+                                + linewidth) - 1, glist_getcanvas(glist), 
+						(glist_istoplevel(glist) ? "bogus" : rtext_gettag(glist_findrtext(glist_getcanvas(glist), &glist->gl_obj))), data);
                     ndrawn++;
                     minyval = 1e20;
                     maxyval = -1e20;
@@ -1816,7 +1820,8 @@ static void plot_vis(t_gobj *z, t_glist *glist,
                     outline, outline);
                 if (style == PLOTSTYLE_BEZ) sys_vgui("-smooth 1\\\n");
 
-                sys_vgui("-tags plot%lx\n", data);
+                sys_vgui("-tags .x%lx%s.plot%lx\n", glist_getcanvas(glist), 
+					(glist_istoplevel(glist) ? "bogus" : rtext_gettag(glist_findrtext(glist_getcanvas(glist), &glist->gl_obj))), data);
             }
             else if (linewidth > 0)
             {
@@ -1859,7 +1864,7 @@ static void plot_vis(t_gobj *z, t_glist *glist,
                 sys_vgui("-fill %s\\\n", outline);
                 if (style == PLOTSTYLE_BEZ) sys_vgui("-smooth 1\\\n");
 
-                sys_vgui("-tags plot%lx\n", data);
+                sys_vgui("-tags .x%lx%s.plot%lx\n", glist_getcanvas(glist), (glist_istoplevel(glist) ? "bogus" : rtext_gettag(glist_findrtext(glist_getcanvas(glist), &glist->gl_obj))), data);
             }
         }
             /* We're done with the outline; now draw all the points.
@@ -1911,8 +1916,8 @@ static void plot_vis(t_gobj *z, t_glist *glist,
             }
         }
             /* and then the trace */
-        sys_vgui(".x%lx.c delete plot%lx\n",
-            glist_getcanvas(glist), data);      
+        sys_vgui(".x%lx.c delete .x%lx%s.plot%lx\n",
+            glist_getcanvas(glist), glist_getcanvas(glist), (glist_istoplevel(glist) ? "bogus" : rtext_gettag(glist_findrtext(glist_getcanvas(glist), &glist->gl_obj))), data);      
     }
 }
 
@@ -2139,9 +2144,11 @@ static void drawnumber_vis(t_gobj *z, t_glist *glist,
                 glist_getcanvas(glist), xloc, yloc, colorstring, buf);
         sys_vgui(" -font {{%s} %d %s}", sys_font,
 				 sys_hostfontsize(glist_getfont(glist)), sys_fontweight);
-        sys_vgui(" -tags drawnumber%lx\n", data);
+        sys_vgui(" -tags .x%lx%s.drawnumber%lx\n", 
+			glist_getcanvas(glist), (glist_istoplevel(glist) ? "bogus" : rtext_gettag(glist_findrtext(glist_getcanvas(glist), &glist->gl_obj))), data);
     }
-    else sys_vgui(".x%lx.c delete drawnumber%lx\n", glist_getcanvas(glist), data);
+    else sys_vgui(".x%lx.c delete .x%lx%s.drawnumber%lx\n", glist_getcanvas(glist), 
+		glist_getcanvas(glist), (glist_istoplevel(glist) ? "bogus" : rtext_gettag(glist_findrtext(glist_getcanvas(glist), &glist->gl_obj))), data);
 }
 
 static t_float drawnumber_motion_ycumulative;
diff --git a/src/g_text.c b/src/g_text.c
index 0d57d6302..23c54a35d 100644
--- a/src/g_text.c
+++ b/src/g_text.c
@@ -1150,6 +1150,7 @@ static void text_getrect(t_gobj *z, t_glist *glist,
 static void text_displace(t_gobj *z, t_glist *glist,
     int dx, int dy)
 {
+	//fprintf(stderr,"text_displace\n");
     t_text *x = (t_text *)z;
     x->te_xpix += dx;
     x->te_ypix += dy;
@@ -1166,14 +1167,15 @@ static void text_displace(t_gobj *z, t_glist *glist,
 static void text_displace_withtag(t_gobj *z, t_glist *glist,
     int dx, int dy)
 {
+	//fprintf(stderr,"text_displace_withtag\n");
     t_text *x = (t_text *)z;
     x->te_xpix += dx;
     x->te_ypix += dy;
     if (glist_isvisible(glist))
     {
         t_rtext *y = glist_findrtext(glist, x);
-        text_drawborder_withtag(x, glist, rtext_gettag(y),
-            rtext_width(y), rtext_height(y), 0);
+        //text_drawborder_withtag(x, glist, rtext_gettag(y),
+        //    rtext_width(y), rtext_height(y), 0);
         canvas_fixlinesfor(glist_getcanvas(glist), x);
 		/*if this is a subpatcher in which case we may be possibly moving nlets around
 		  which in turn requires that we redraw parent's nlets */
diff --git a/src/pd.tk b/src/pd.tk
index 466297293..cf9cb19dd 100644
--- a/src/pd.tk
+++ b/src/pd.tk
@@ -2897,7 +2897,7 @@ proc pdtk_canvas_mouseup {name x y b} {
 #}
 
 proc pdtk_select_all_gop_widgets {name gop state} {
-	#puts stderr "======================$name"
+	#puts stderr "======================$name $gop"
 	foreach item [$name.c find all] {
 		#puts stderr [$name.c gettags $item]
 		foreach tag [$name.c gettags $item] {
-- 
GitLab