# HG changeset patch
# User pantxo <pantxo.diribarne@gmail.com>
# Date 1396539410 -7200
# Node ID eeab4593b9ca347f07fafb0c86d3dea03355c6bb
# Parent  86e270a679c248ab5dc054e9257a34bd18839901
legend.m: honor axes handle argument (bug #42035)

* add "parent" property/value pair for all graphics objects creation
* updateline: specify parent axes for susequent call to legend 
* add a test for bug #42035

diff -r 86e270a679c2 -r eeab4593b9ca scripts/plot/appearance/legend.m
--- a/scripts/plot/appearance/legend.m	Sun Apr 06 17:55:11 2014 +0200
+++ b/scripts/plot/appearance/legend.m	Thu Apr 03 17:36:50 2014 +0200
@@ -150,7 +150,7 @@
     [~, n] = unique (ca);
     ca = ca(sort (n));
   endif
-
+  
   if (nargin > 0 && all (ishandle (varargin{1})))
     kids = flipud (varargin{1}(:));
     varargin(1) = [];
@@ -579,7 +579,7 @@
                           "xlim", [0, 1], "ylim", [0, 1],
                           "visible", ifelse (strcmp (box, "on"), "on", "off"),
                           "activepositionproperty", "position",
-                          "interpreter", "tex");
+                          "interpreter", "tex", "parent", fig);
           ## Inherit properties from current axis
           ## "fontunits" shoud be first because it affects interpretation
           ## of "fontsize" property
@@ -631,7 +631,8 @@
                                     "fontname", fontname,
                                     "fontsize", fontsize,
                                     "fontweight", fontweight,
-                                    "userdata", hplots(k));
+                                    "userdata", hplots(k),
+                                    "parent", hlegend);
           units = get (texthandle(end), "units");
           unwind_protect
             set (texthandle(end), "units", "points");
@@ -835,7 +836,7 @@
                            "ydata", [1, 1] .* (lpos(4) - yoffset - yk * ystep) / lpos(4),
                            "color", color, "linestyle", style, "linewidth", lwidth,
                            "marker", "none",
-                           "userdata", hplots(k));
+                           "userdata", hplots(k), "parent", hlegend);
                 hobjects(end+1) = l1;
               endif
               marker = get (hplots(k), "marker");
@@ -847,7 +848,7 @@
                            "markeredgecolor",get (hplots(k), "markeredgecolor"),
                            "markerfacecolor",get (hplots(k), "markerfacecolor"),
                            "markersize", min (get (hplots(k), "markersize"),10),
-                           "userdata", hplots(k));
+                           "userdata", hplots(k), "parent", hlegend);
                 hobjects(end+1) = l1;
               endif
 
@@ -880,7 +881,8 @@
                             "ydata", (lpos(4) - yoffset -
                                       [yk-0.3, yk-0.3, yk+0.3, yk+0.3] .* ystep) / lpos(4),
                             "facecolor", facecolor, "edgecolor", edgecolor,
-                            "cdata", cdata, "userdata", hplots(k));
+                            "cdata", cdata, "userdata", hplots(k),
+                            "parent", hlegend);
               else
                 ## non-standard patch only making use of marker styles
                 ## such as scatter plot.
@@ -909,7 +911,8 @@
                             "ydata", (lpos(4) - yoffset -
                                       [yk-0.3, yk-0.3, yk+0.3, yk+0.3] .* ystep) / lpos(4),
                             "facecolor", facecolor, "edgecolor", edgecolor,
-                            "cdata", cdata, "userdata", hplots(k));
+                            "cdata", cdata, "userdata", hplots(k),
+                            "parent", hlegend);
                 hobjects(end+1) = p1;
               endif
               ## FIXME: Need listeners, as for line objects.
@@ -1178,7 +1181,8 @@
   if (update_name)
     ## When string changes, have to rebuild legend completely
     [hplots, text_strings] = __getlegenddata__ (hlegend);
-    legend (hplots, text_strings);
+    
+    legend (get (hplots, "parent"), hplots, text_strings);
   else
     kids = get (hlegend, "children");
     ll = lm = [];
@@ -1624,3 +1628,20 @@
 %!   graphics_toolkit (toolkit);
 %! end_unwind_protect
 
+%!test
+%! h = figure ("visible", "off");
+%! unwind_protect
+%!   hax1 = subplot (1,2,1);
+%!   plot (1:10);
+%!   subplot (1,2,2);
+%!   plot (1:10);
+%!   hleg1 = legend (hax1, "toto");
+%!   hleg2 = legend ("toto");
+%!   assert (get (hleg1, "userdata").handle, hax1)
+%!   assert (get (hleg2, "userdata").handle, gca ())
+%! unwind_protect_cleanup
+%!   close (h);
+%! end_unwind_protect
+
+
+
