# HG changeset patch
# User Ben Abbott  <bpabbott@mac.com>
# Date 1338002711 14400
# Node ID 4740933b4bc57b5617d0753b0d3fa84a83fe9810
# Parent  e3da2a68d709399ac6f69aed68b168fbb8fdc15b
legend() fixes (Bug # 36408).

legend.m: Preserve parent axes' "outerposition" value.
Use "xpad" instead of "ypad" for horizontal dimensions.
Add "legend right" to duplicate demo.
Include "clf" to beginning of demos.

diff --git a/scripts/plot/legend.m b/scripts/plot/legend.m
--- a/scripts/plot/legend.m
+++ b/scripts/plot/legend.m
@@ -510,7 +510,7 @@
                           "xticklabel", "", "yticklabel", "", "zticklabel", "",
                           "xlim", [0, 1], "ylim", [0, 1],
                           "visible", ifelse (strcmp (box, "on"), "on", "off"),
-                          "activepositionproperty", "position");
+                          "activepositionproperty", "outerposition");
         else
           addprops = false;
           axes (hlegend);
@@ -611,51 +611,51 @@
             endif
           case "east"
             if (outside)
-              lpos = [ca_outpos(1) + ca_outpos(3) - lpos(3) - ypad, ...
+              lpos = [ca_outpos(1) + ca_outpos(3) - lpos(3) - xpad, ...
                       ca_pos(2) + (ca_pos(4) - lpos(4)) / 2, lpos(3), lpos(4)];
               new_pos = [ca_pos(1), ca_pos(2), ca_pos(3) - lpos(3), ca_pos(4)];
             else
-              lpos = [ca_pos(1) + ca_pos(3) - lpos(3) - ypad, ...
+              lpos = [ca_pos(1) + ca_pos(3) - lpos(3) - xpad, ...
                       ca_pos(2) + (ca_pos(4) - lpos(4)) / 2, lpos(3), lpos(4)];
             endif
           case "west"
             if (outside)
-              lpos = [ca_outpos(1) + ypad, ...
+              lpos = [ca_outpos(1) + xpad, ...
                       ca_pos(2) + (ca_pos(4) - lpos(4)) / 2, ...
                       lpos(3), lpos(4)];
               new_pos = [ca_pos(1) + lpos(3), ca_pos(2), ...
                          ca_pos(3) - lpos(3), ca_pos(4)];
             else
-              lpos = [ca_pos(1) +  ypad, ...
+              lpos = [ca_pos(1) +  xpad, ...
                       ca_pos(2) + (ca_pos(4) - lpos(4)) / 2, lpos(3), lpos(4)];
             endif
           case "northeast"
             if (outside)
-              lpos = [ca_outpos(1) + ca_outpos(3) - lpos(3) - ypad, ...
+              lpos = [ca_outpos(1) + ca_outpos(3) - lpos(3) - xpad, ...
                       ca_pos(2) + ca_pos(4) - lpos(4), lpos(3), lpos(4)];
               new_pos = [ca_pos(1), ca_pos(2), ca_pos(3) - lpos(3), ca_pos(4)];
             else
-              lpos = [ca_pos(1) + ca_pos(3) - lpos(3) - ypad, ...
+              lpos = [ca_pos(1) + ca_pos(3) - lpos(3) - xpad, ...
                       ca_pos(2) + ca_pos(4) - lpos(4) - ypad, lpos(3), lpos(4)];
             endif
           case "northwest"
             if (outside)
-              lpos = [ca_outpos(1) + ypad , ca_pos(2) + ca_pos(4) - lpos(4), ...
+              lpos = [ca_outpos(1) + xpad , ca_pos(2) + ca_pos(4) - lpos(4), ...
                       lpos(3), lpos(4)];
               new_pos = [ca_pos(1) + lpos(3), ca_pos(2), ...
                          ca_pos(3) - lpos(3), ca_pos(4)];
             else
-              lpos = [ca_pos(1) + ypad, ...
+              lpos = [ca_pos(1) + xpad, ...
                       ca_pos(2) + ca_pos(4) - lpos(4) - ypad, lpos(3), lpos(4)];
             endif
           case "southeast"
             if (outside)
-              lpos = [ca_outpos(1) + ca_outpos(3) - lpos(3) - ypad, ...
+              lpos = [ca_outpos(1) + ca_outpos(3) - lpos(3) - xpad, ...
                       ca_pos(2), lpos(3), lpos(4)];
               new_pos = [ca_pos(1), ca_pos(2), ...
                          ca_pos(3) - lpos(3), ca_pos(4)];
             else
-              lpos = [ca_pos(1) + ca_pos(3) - lpos(3) - ypad, ...
+              lpos = [ca_pos(1) + ca_pos(3) - lpos(3) - xpad, ...
                       ca_pos(2) + ypad, lpos(3), lpos(4)];
             endif
           case "southwest"
@@ -747,7 +747,8 @@
             units = get (ca(i), "units");
             unwind_protect
               set (ca(i), "units", "points");
-              set (ca (i), "position", new_pos);
+              set (ca(i), "position", new_pos);
+              set (ca(i), "outerposition", ca_outpos);
             unwind_protect_cleanup
               set (ca(i), "units", units);
             end_unwind_protect
@@ -1043,6 +1044,7 @@
 %! plot (1:10, 1:10);
 %! title ('a very long label can sometimes cause problems');
 %! legend ('hello world', 'location', 'northeastoutside');
+%! legend ('right');
 
 %!demo
 %! clf;
@@ -1159,6 +1161,7 @@
 %! set (h, 'interpreter', 'none');
 
 %!demo
+%! clf;
 %! x = 0:10;
 %! y1 = rand (size (x));
 %! y2 = rand (size (x));
@@ -1166,6 +1169,7 @@
 %! legend ([h1, h2], {'Blue', 'Green'}, 'location', 'south');
 
 %!demo
+%! clf;
 %! x = 0:10;
 %! y1 = rand (size (x));
 %! y2 = rand (size (x));
@@ -1173,6 +1177,7 @@
 %! legend ({'Blue', 'Green'}, 'location', 'south');
 
 %!demo
+%! clf;
 %! x = 0:10;
 %! y1 = rand (size (x));
 %! y2 = rand (size (x));
