summaryrefslogtreecommitdiff
path: root/modules/xcos/macros/xcosPal.sci
blob: cdce8fabf670a8864d122a3037d70be2d2053721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) DIGITEO - 2010-2010 - Clément DAVID
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
//
//

function pal = xcosPal(name, scs_m)
    // Instanciate a new Xcos palette on Scilab.
    //
    // Calling Sequence
    //   pal = xcosPal();
    //   pal = xcosPal(name);
    //   pal = xcosPal(scs_m);
    //   pal = xcosPal([], scs_m);
    //   pal = xcosPal(name, scs_m);
    //
    // Parameters
    //   name: string; the optional palette name
    //   scs_m: diagram mlist; the optional source diagram
    //   pal: palette tlist; the palette instance
    //
    // Description
    // Instanciate a new palette diagram.
    //
    // The optional name argument can be used to set a name to the palette. The optional scs_m argument can be used to import diagrams as palettes.
    //
    // Examples
    //   loadXcosLibs();
    //
    //   // from scratch
    //   pal = xcosPal("My sum palette");
    //   pal = xcosPalAddBlock(pal, "SUM_f");
    //   pal = xcosPalAddBlock(pal, "BIGSOM_f");
    //   xcosPalAdd(pal);
    //
    //   // from an old palette
    //   exec(SCI + "/modules/scicos/palettes/Integer.cosf", -1);
    //   pal = xcosPal(scs_m);
    //   xcosPalAdd(pal);
    //
    // See also
    //   xcosPal
    //   xcosPalAddBlock
    //
    // Authors
    //   Clément DAVID


    [lhs,rhs] = argn(0);

    if rhs > 2 then
        error(msprintf(gettext("%s: Wrong number of input arguments: %d to %d expected.\n"), "xcosPal", 0, 2));
    end

    if exists("name", "l") == 0 then
        name = "New palette";
    elseif isempty(name) then
        name = "New palette";
    elseif typeof(name) == "diagram" then
        scs_m = name;
        name = "New palette";
    end
    // loading the scicos_diagram macro
    if exists("scicos_diagram", "a") == 0 then loadXcosLibs(), end
    if exists("scs_m", "l") == 0 then scs_m = scicos_diagram(), end

    if typeof(name) <> "string" then
        error(msprintf(gettext("%s: Wrong type for input argument ""%s"": string type expected.\n"), "xcosPal","name"));
    end

    if typeof(scs_m) <> "diagram" then
        error(msprintf(gettext("%s: Wrong type for input argument ""%s"": diagram type expected.\n"), "xcosPal","scs_m"));
    end

    // default instance
    pal = tlist(["palette", "name",  "blockNames", "icons", "style"],..
    [],        [],      [],      []);

    // find palette name
    if ~isempty(scs_m.objs) then
        name = scs_m.props.title(1);
    end

    // getting a block name list
    varsToLoad = [];
    for block = scs_m.objs
        if typeof(block)=="Block" & block.gui == "PAL_f" then
            // customize palette name
            name = block.model.rpar.props.title(1);

            // Add PAL_f children blocks
            children = block.model.rpar.objs;
            for jBlock = 1:size(children)
                varsToLoad($+1) = children(jBlock).gui;
            end
        elseif typeof(block)=="Block" then
            // old scicos palettes doesn't have a PAL_f block but directly
            // the reference instances instead.
            varsToLoad($+1) = block.gui
        end
    end

    // set name
    pal.name = name;

    // add block instance by interfunction (will generate a new reference instance).
    for index = 1:size(varsToLoad, "*")
        pal = xcosPalAddBlock(pal, varsToLoad(index));
    end
endfunction