changeset 17719:ed9a21a90221

Fix incorrect parenting of axes and hggroups (bug #39813) Modify newplot to accept any type of graphics handle so that it can be preserved. Modify __plt_get_axis_arg__ to return "parent" handle instead of axis handle. * newplot.m: Make the function preserve the hsave handle. Add Matlab compatible tests *__plt_get_axis_arg__: Return "parent" argument in case one is specified instead of axis.
author Pantxo Diribarne <pantxo.diribarne@gmail.com>
date Mon, 23 Sep 2013 19:44:57 +0200
parents 6ed0a8532bcf
children 1ab8e21d9cfc
files scripts/plot/util/__plt_get_axis_arg__.m scripts/plot/util/newplot.m
diffstat 2 files changed, 47 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/plot/util/__plt_get_axis_arg__.m	Mon Oct 21 16:57:02 2013 -0700
+++ b/scripts/plot/util/__plt_get_axis_arg__.m	Mon Sep 23 19:44:57 2013 +0200
@@ -50,7 +50,7 @@
         varargin(parent:parent+1) = [];
       else
         ## 'parent' property for some other type like hggroup
-        h = ancestor (htmp, "axes");
+        h = htmp;
       endif
     else
       error ("%s: expecting parent value to be axes handle", caller);
--- a/scripts/plot/util/newplot.m	Mon Oct 21 16:57:02 2013 -0700
+++ b/scripts/plot/util/newplot.m	Mon Sep 23 19:44:57 2013 +0200
@@ -138,8 +138,10 @@
 
   if (isempty (ca))
     ca = gca ();
+    deleteall = true;
   else
     set (cf, "currentaxes", ca);
+    deleteall = false;
   endif
 
   ## FIXME: Is this necessary anymore?
@@ -159,8 +161,19 @@
     case "replacechildren"
       delete (get (ca, "children"));
     case "replace"
-      __go_axes_init__ (ca, "replace");
-      __request_drawnow__ ();
+      if (! deleteall && ca != hsave)
+        ## preserve hsave and its parents, uncles, ...
+        kids = allchild (ca);
+        hkid = hsave;
+        while (! any (hkid == kids))
+          hkid = get (hkid, "parent");
+        endwhile
+        kids(kids == hkid) = [];
+        delete (kids);
+      else
+        __go_axes_init__ (ca, "replace");
+        __request_drawnow__ ();
+      endif
       ## FIXME: The code above should perform the following:
       ###########################
       ## delete (allchild (ca));
@@ -192,3 +205,34 @@
 %!   close (hf);
 %! end_unwind_protect
 
+%!test
+%! hf = figure ("visible", "off");
+%! unwind_protect
+%!   hax = axes ();
+%!   hold on;
+%!   hg1 = hggroup ();
+%!   hg2 = hggroup ("parent", hg1);
+%!   li0 = line (1:10, 1:10);
+%!   li1 = line (1:10, -1:-1:-10, "parent", hg1);
+%!   li2 = line (1:10, sin (1:10), "parent", hg2);
+%!   hold off;
+%!   newplot (hg2);
+%!   assert (ishandle (li0), false);
+%!   assert (get (hax, "children"), hg1);
+%! 
+%!   ## kids are preserved for hggroups
+%!   kids = get (hg1, "children");
+%!   newplot (hg1); 
+%!   assert (get (hg1, "children"), kids));
+%! 
+%!   ## preserve objects
+%!   newplot (li1);
+%!   assert (ishandle (li1));
+%! 
+%!   ## kids are deleted for axes
+%!   newplot (hax);  
+%!   assert (isempty (get (hax, "children")));
+%! unwind_protect_cleanup
+%!   close (hf);
+%! end_unwind_protect
+