ff8002e82f01d236f52feeb8b1e3295d7b452faf
[scilab.git] / scilab / modules / graphics / macros / %h_set.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) INRIA
3 // Copyright (C) 2012 - 2016 - Scilab Enterprises
4 // Copyright (C) 2018 - Samuel GOUGEON
5 //
6 // This file is hereby licensed under the terms of the GNU GPL v2.0,
7 // pursuant to article 5.3.4 of the CeCILL v.2.1.
8 // This file was originally licensed under the terms of the CeCILL v2.1,
9 // and continues to be available under such terms.
10 // For more information, see the COPYING file which you should have received
11 // along with this program.
12
13 function hdl = %h_set(varargin)
14     s = size(varargin);
15     if s == 2
16         str = varargin(1)
17         val = varargin(2)
18         hdl=[]
19         set(str, val)
20
21     elseif s > 2
22         hdl = varargin(1)
23         Hsize = size(hdl);
24         nh = prod(Hsize);
25         for i = 2:2:s   // Loop on ("property",value) pairs
26             prop = varargin(i);
27             rhs = varargin(i+1);
28             if nh==1
29                 // scalar handle => direct assignment
30                 hdl(prop) = rhs;
31             else
32                 // array of handles
33                 RHSsize = size(rhs);
34                 for j = 1:nh  // Loop on handles
35                     h = hdl(j)  // works around http://bugzilla.scilab.org/15802
36                     lhs = h(prop);     // Current handle's content
37                     LHSsize = size(lhs);
38                     if and(RHSsize==LHSsize) | lhs==[]  // => repeated assignment
39                         h(prop) = rhs;
40                     elseif size(lhs,"*")==1 & and(RHSsize==Hsize)
41                         // => element-wise assignments
42                         h(prop) = rhs(j);
43                     elseif LHSsize(2)==1 & LHSsize(1)==RHSsize(1) & RHSsize(2)==nh
44                         // column-wise assignments
45                         h(prop) = rhs(:,j);
46                     elseif LHSsize(1)==1 & LHSsize(2)==RHSsize(2) & RHSsize(1)==nh
47                         // row-wise assignments
48                         h(prop) = rhs(j,:);
49                     elseif prod(LHSsize)==prod(RHSsize)
50                         // Repeated assignment after RHS reformating
51                         h(prop) = matrix(rhs,LHSsize);
52                     elseif RHSsize(1)==nh & RHSsize(2)==prod(LHSsize)
53                         // example: axes.data_bounds is a 2x2 matrix,
54                         //          but a 1x4 assigment is possible
55                         h(prop) = rhs(j,:);
56                     else
57                         msg = _("%s: Unsupported assignment for property ''%s''.\n")
58                         error(msprintf(msg, "%h_set", prop))
59                     end
60                     hdl(j) = h;
61                 end
62             end
63         end
64     end
65 endfunction