]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/linkprop.m
update packages
[CreaPhase.git] / octave_packages / m / plot / linkprop.m
1 ## Copyright (C) 2008-2012 David Bateman
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{hlink} =} linkprop (@var{h}, @var{prop})
21 ## Link graphics object properties, such that a change in one is
22 ## propagated to the others.  The properties to link are given as a
23 ## string of cell string array by @var{prop} and the objects containing
24 ## these properties by the handle array @var{h}.
25 ##
26 ## An example of the use of linkprop is
27 ##
28 ## @example
29 ## @group
30 ## x = 0:0.1:10;
31 ## subplot (1,2,1);
32 ## h1 = plot (x, sin (x));
33 ## subplot (1,2,2);
34 ## h2 = plot (x, cos (x));
35 ## hlink = linkprop ([h1, h2], @{"color","linestyle"@});
36 ## set (h1, "color", "green");
37 ## set (h2, "linestyle", "--");
38 ## @end group
39 ## @end example
40 ##
41 ## @end deftypefn
42
43 function hlink = linkprop (h, prop)
44   if (ischar (prop))
45     prop = {prop};
46   elseif (!iscellstr (prop))
47     error ("linkprop: properties must be a string or cell string array");
48   endif
49
50   for i = 1 : numel (h)
51     for j = 1 : numel (prop)
52       addlistener (h(i), prop{j}, {@update_prop, h, prop{j}});
53     endfor
54   endfor
55
56   ## This should be an object that when destroyed removes the links
57   ## The below is not quite right. As when you call "clear hlink" the
58   ## hggroup continues to exist.
59   hlink = hggroup ();
60   set (hlink, "deletefcn", {@delete_prop, h, prop});
61 endfunction
62
63 function update_prop (h, d, hlist, prop)
64   persistent recursion = false;
65
66   ## Don't allow recursion
67   if (! recursion)
68     unwind_protect
69       recursion = true;
70       val = get (h, prop);
71       for hh = hlist(:)'
72         if (hh != h)
73           oldval = get (hh, prop);
74           if (! isequal (val, oldval))
75             set (hh, prop, val);
76           endif
77         endif
78       endfor
79     unwind_protect_cleanup
80       recursion = false;
81     end_unwind_protect
82   endif
83 endfunction
84
85 function delete_prop (h, d, hlist, prop)
86   ## FIXME. Actually need to delete the linked properties.
87   ## However, only warn if the graphics objects aren't being deleted.
88   warn = false;
89   for h = hlist(:)'
90     if (ishandle (h) && !strcmpi (get (h, "beingdeleted"), "on"))
91       warn = true;
92       break;
93     endif
94   endfor
95   if (warn)
96     warning ("linkprop: can not remove linked properties");
97   endif
98 endfunction