# HG changeset patch # User Rik # Date 1397366018 25200 # Node ID ce36233e4849de89ef1a460bd74bba44abf2088c # Parent 2deed6538c726b1dbd5e64b1f8ab779c047bd339 legend.m: Correctly handle axis handle as first argument (bug #42035). * legend.m: Save and restore figure and axis handle as is done for other plotting routines. Add %!test to check behavior. * legend.m (updateline): When string changes and legend must be re-built, call routine with axis handle as first argument. diff -r 2deed6538c72 -r ce36233e4849 scripts/plot/appearance/legend.m --- a/scripts/plot/appearance/legend.m Tue Mar 04 13:07:41 2014 +0100 +++ b/scripts/plot/appearance/legend.m Sat Apr 12 22:13:38 2014 -0700 @@ -565,6 +565,12 @@ linelength = 15; ## Create the axis first + oldfig = get (0, "currentfigure"); + if (oldfig != fig) + set (0, "currentfigure", fig); + else + oldfig = []; + endif curaxes = get (fig, "currentaxes"); unwind_protect ud = ancestor (hplots, "axes"); @@ -1046,6 +1052,9 @@ endif unwind_protect_cleanup set (fig, "currentaxes", curaxes); + if (! isempty (oldfig)) + set (0, "currentfigure", oldfig); + endif end_unwind_protect endif endif @@ -1180,7 +1189,7 @@ if (update_name) ## When string changes, have to rebuild legend completely [hplots, text_strings] = __getlegenddata__ (hlegend); - legend (hplots, text_strings); + legend (get (hplots(1), "parent"), hplots, text_strings); else kids = get (hlegend, "children"); ll = lm = []; @@ -1626,3 +1635,20 @@ %! graphics_toolkit (toolkit); %! end_unwind_protect +%!test +%! ## bug #42035 +%! h = figure ("visible", "off"); +%! unwind_protect +%! hax1 = subplot (1,2,1); +%! plot (1:10); +%! hax2 = subplot (1,2,2); +%! plot (1:10); +%! hleg1 = legend (hax1, "foo"); +%! assert (get (hleg1, "userdata").handle, hax1) +%! assert (gca (), hax2); +%! hleg2 = legend ("bar"); +%! assert (get (hleg2, "userdata").handle, gca ()) +%! unwind_protect_cleanup +%! close (h); +%! end_unwind_protect +