# HG changeset patch
# User Pantxo Diribarne <pantxo.diribarne@gmail.com>
# Date 1503390473 -7200
#      Tue Aug 22 10:27:53 2017 +0200
# Node ID 1d9a97f02656ac20755f5be26cd623e84f5c98ec
# Parent  d64985eaf56dc1df5073a9cea51b0c4b8b43a521
Fix regression in subplot alignment (bugs #51717 and #51731)

* subplot.m: new call form "subplot(R,C, IDX, HAX)" to turns HAX into a subplot.
 Store the {rows,cols,index} arguments in the axes appdata for further use. 
* newplot.m: if necessary, call subplot to reinstall listeners after resetting
 the axes

diff -r d64985eaf56d -r 1d9a97f02656 scripts/plot/util/newplot.m
--- a/scripts/plot/util/newplot.m	Sun Aug 20 21:09:27 2017 -0700
+++ b/scripts/plot/util/newplot.m	Tue Aug 22 10:27:53 2017 +0200
@@ -198,8 +198,13 @@
           delete (ca);
           ca = axes ();
         elseif (do_reset)
+          rcn = getappdata (ca, "__subplotrcn__");
           delete (allchild (ca));
           reset (ca);
+          ## Reinstall listeners for subplot
+          if (! isempty (rcn))
+            subplot (rcn{:}, ca)
+          endif            
         endif
       endif
   endswitch
diff -r d64985eaf56d -r 1d9a97f02656 scripts/plot/util/subplot.m
--- a/scripts/plot/util/subplot.m	Sun Aug 20 21:09:27 2017 -0700
+++ b/scripts/plot/util/subplot.m	Tue Aug 22 10:27:53 2017 +0200
@@ -18,6 +18,7 @@
 
 ## -*- texinfo -*-
 ## @deftypefn  {} {} subplot (@var{rows}, @var{cols}, @var{index})
+## @deftypefnx {} {} subplot (@var{rows}, @var{cols}, @var{index}, @var{hax})
 ## @deftypefnx {} {} subplot (@var{rcn})
 ## @deftypefnx {} {} subplot (@var{hax})
 ## @deftypefnx {} {} subplot (@dots{}, "align")
@@ -28,9 +29,13 @@
 ## Set up a plot grid with @var{rows} by @var{cols} subwindows and set the
 ## current axes for plotting (@code{gca}) to the location given by @var{index}.
 ##
+## If an axes handle @var{hax} is provided after the (@var{rows}, @var{cols}, 
+## @var{index}) arguments, the corresponding axes is turned into a
+## subplot.
+##
 ## If only one numeric argument is supplied, then it must be a three digit
 ## value specifying the number of rows in digit 1, the number of columns in
-## digit 2, and the plot index in digit 3.
+## digit 2, and the plot index in digit 3. 
 ##
 ## The plot index runs row-wise; First, all columns in a row are numbered
 ## and then the next row is filled.
@@ -98,6 +103,8 @@
   replace_axes = false;
   have_position = false;
   initial_args_decoded = false;
+  make_subplot = false;
+  hsubplot = [];
 
   if (nargin >= 3)
     ## R, C, N?
@@ -110,7 +117,13 @@
       rows = arg1;
       cols = arg2;
       index = arg3;
-      varargin(1:3) = [];
+      if (nargin > 3 && isaxes (varargin{4}))
+        make_subplot = true;
+        hsubplot = varargin{4};
+        varargin(1:4) = [];
+      else
+        varargin(1:3) = [];
+      endif
       initial_args_decoded = true;
     endif
   endif
@@ -211,69 +224,77 @@
 
     set (cf, "nextplot", "add");
 
-    found = false;
-    kids = get (cf, "children");
-    for child = kids(:)'
-      ## Check whether this child is still valid; this might not be the
-      ## case anymore due to the deletion of previous children (due to
-      ## "deletefcn" callback or for legends/colorbars that are deleted
-      ## with their corresponding axes).
-      if (! ishandle (child))
-        continue;
-      endif
-      if (strcmp (get (child, "type"), "axes"))
-        ## Skip legend and colorbar objects.
-        if (any (strcmp (get (child, "tag"), {"legend", "colorbar"})))
+    if (! make_subplot)
+      found = false;
+      kids = get (cf, "children");
+      for child = kids(:)'
+        ## Check whether this child is still valid; this might not be the
+        ## case anymore due to the deletion of previous children (due to
+        ## "deletefcn" callback or for legends/colorbars that are deleted
+        ## with their corresponding axes).
+        if (! ishandle (child))
           continue;
         endif
+        if (strcmp (get (child, "type"), "axes"))
+          ## Skip legend and colorbar objects.
+          if (any (strcmp (get (child, "tag"), {"legend", "colorbar"})))
+            continue;
+          endif
 
-        if (isappdata (child, "__subplotposition__"))
-          objpos = getappdata (child, "__subplotposition__");
-        else
-          objpos = get (child, "position");
-        endif
-        if (all (abs (objpos - pos) < eps) && ! replace_axes)
-          ## If the new axes are in exactly the same position
-          ## as an existing axes object, or if they share the same
-          ## appdata "__subplotposition__", use the existing axes.
-          found = true;
-          hsubplot = child;
-        else
-          ## If the new axes overlap an old axes object, delete the old axes.
-          objpos = get (child, "position");
+          if (isappdata (child, "__subplotposition__"))
+            objpos = getappdata (child, "__subplotposition__");
+          else
+            objpos = get (child, "position");
+          endif
+          if (all (abs (objpos - pos) < eps) && ! replace_axes)
+            ## If the new axes are in exactly the same position
+            ## as an existing axes object, or if they share the same
+            ## appdata "__subplotposition__", use the existing axes.
+            found = true;
+            hsubplot = child;
+          else
+            ## If the new axes overlap an old axes object, delete the old axes.
+            objpos = get (child, "position");
 
-          x0 = pos(1);
-          x1 = x0 + pos(3);
-          y0 = pos(2);
-          y1 = y0 + pos(4);
-          objx0 = objpos(1);
-          objx1 = objx0 + objpos(3);
-          objy0 = objpos(2);
-          objy1 = objy0 + objpos(4);
-          if (! (x0 >= objx1 || x1 <= objx0 || y0 >= objy1 || y1 <= objy0))
-            delete (child);
+            x0 = pos(1);
+            x1 = x0 + pos(3);
+            y0 = pos(2);
+            y1 = y0 + pos(4);
+            objx0 = objpos(1);
+            objx1 = objx0 + objpos(3);
+            objy0 = objpos(2);
+            objy1 = objy0 + objpos(4);
+            if (! (x0 >= objx1 || x1 <= objx0 || y0 >= objy1 || y1 <= objy0))
+              delete (child);
+            endif
           endif
         endif
-      endif
-    endfor
+      endfor
+    else
+      found = true;
+    endif
 
-    if (found)
+    if (found && ! make_subplot)
       ## Switch to existing subplot and set requested properties
       set (cf, "currentaxes", hsubplot);
       if (! isempty (varargin))
         set (hsubplot, varargin{:});
       endif
     else
-      hsubplot = axes ("box", "off",
-                       "activepositionproperty", "position",
-                       "position", pos, "looseinset", li,
-                       varargin{:});
+      pval = [{"activepositionproperty", "position", ...
+               "position", pos, "looseinset", li} varargin];
+      if (! make_subplot)
+        hsubplot = axes (pval{:});
+      else
+        set (hsubplot, pval{:})
+      endif
 
       if (! align_axes)
         ## base position (no ticks, no annotation, no cumbersome neighbor)
         setappdata (hsubplot, "__subplotposition__", pos);
         ## max outerposition
         setappdata (hsubplot, "__subplotouterposition__", opos);
+        setappdata (hsubplot, "__subplotrcn__", {rows, cols, index});
         addlistener (hsubplot, "outerposition", @subplot_align);
         addlistener (hsubplot, "xaxislocation", @subplot_align);
         addlistener (hsubplot, "yaxislocation", @subplot_align);
