File: uitree.cpp

package info (click to toggle)
faust 2.81.10%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 431,496 kB
  • sloc: cpp: 283,941; ansic: 116,215; javascript: 18,529; sh: 14,356; vhdl: 14,052; java: 5,900; python: 5,091; objc: 3,852; makefile: 2,725; cs: 1,672; lisp: 1,146; ruby: 954; yacc: 586; xml: 471; lex: 247; awk: 111; tcl: 26
file content (301 lines) | stat: -rw-r--r-- 8,621 bytes parent folder | download
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/************************************************************************
 ************************************************************************
    FAUST compiler
    Copyright (C) 2003-2018 GRAME, Centre National de Creation Musicale
    ---------------------------------------------------------------------
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 ************************************************************************
 ************************************************************************/

#include "uitree.hh"
#include <sstream>
#include "exception.hh"
#include "global.hh"

using namespace std;

static Tree makeSubFolderChain(Tree path, Tree elem);
static Tree putFolder(Tree folder, Tree item);
static Tree getFolder(Tree folder, Tree ilabel);

static void error(const char* s, Tree t)
{
    fprintf(stderr, "ERROR : %s (%p)\n", s, (void*)t);
}

#define FAUST_ERROR(s, t)        \
    {                            \
        error(s, t);             \
        throw faustexception(s); \
    }

//------------------------------------------------------------------------------
// Property list
//------------------------------------------------------------------------------

#if 0

// normal version, which works, but does not arrange in alphabetical order
static bool findKey(Tree pl, Tree key, Tree& val)
{
    if (isNil(pl)) {
        return false;
    }
    if (left(hd(pl)) == key) {
        val = right(hd(pl));
        return true;
    }
    /*  left(hd(pl)) != key	*/ return findKey(tl(pl), key, val);
}

static Tree updateKey(Tree pl, Tree key, Tree val)
{
    if (isNil(pl)) {
        return cons(cons(key, val), gGlobal->nil);
    }
    if (left(hd(pl)) == key) {
        return cons(cons(key, val), tl(pl));
    }
    /*  left(hd(pl)) != key	*/ return cons(hd(pl), updateKey(tl(pl), key, val));
}

static Tree removeKey(Tree pl, Tree key)
{
    if (isNil(pl)) {
        return gGlobal->nil;
    }
    if (left(hd(pl)) == key) {
        return tl(pl);
    }
    /*  left(hd(pl)) != key	*/ return cons(hd(pl), removeKey(tl(pl), key));
}

#else

// Experimental version that arranges in alphabetical order

static bool isBefore(Tree k1, Tree k2)
{
    // before comparing replace (type . label) by label
    if (isList(k1)) {
        k1 = tl(k1);
    }
    if (isList(k2)) {
        k2 = tl(k2);
    }

    // fprintf(stderr, "isBefore("); print(k1, stderr); fprintf(stderr,", "); print(k2, stderr);
    // fprintf(stderr,")\n");
    Sym s1, s2;
    if (!isSym(k1->node(), &s1)) {
        FAUST_ERROR("the node of the tree is not a symbol", k1);
    }
    if (!isSym(k2->node(), &s2)) {
        FAUST_ERROR("the node of the tree is not a symbol", k2);
    }

    // fprintf (stderr, "strcmp(\"%s\", \"%s\") = %d\n", name(s1), name(s2), strcmp(name(s1),
    // name(s2)));
    return strcmp(name(s1), name(s2)) < 0;
}

static bool findKey(Tree pl, Tree key, Tree& val)
{
    if (isNil(pl)) {
        return false;
    }
    if (left(hd(pl)) == key) {
        val = right(hd(pl));
        return true;
    }
    if (isBefore(left(hd(pl)), key)) {
        return findKey(tl(pl), key, val);
    }
    return false;
}

static Tree updateKey(Tree pl, Tree key, Tree val)
{
    if (isNil(pl)) {
        return cons(cons(key, val), gGlobal->nil);
    }
    if (left(hd(pl)) == key) {
        return cons(cons(key, val), tl(pl));
    }
    if (isBefore(left(hd(pl)), key)) {
        return cons(hd(pl), updateKey(tl(pl), key, val));
    }
    return cons(cons(key, val), pl);
}

/**
 * Like updateKey but allow multiple items with same key
 */
static Tree addKey(Tree pl, Tree key, Tree val)
{
    if (isNil(pl)) {
        return cons(cons(key, val), gGlobal->nil);
    }
    if (isBefore(key, left(hd(pl)))) {
        return cons(cons(key, val), pl);
    }
    return cons(hd(pl), addKey(tl(pl), key, val));
}

#if 0
static Tree removeKey(Tree pl, Tree key)
{
	if (isNil(pl)) 					return gGlobal->nil;
	if (left(hd(pl)) == key) 		return tl(pl);
	if (isBefore(left(hd(pl)),key))	return cons(hd(pl), removeKey(tl(pl), key));
	return pl;
}
#endif
#endif

//-----------------------------------------------------
// Management of the user interface tree construction
//-----------------------------------------------------

Tree uiFolder(Tree label, Tree elements)
{
    return tree(gGlobal->UIFOLDER, label, elements);
}
bool isUiFolder(Tree t)
{
    return isTree(t, gGlobal->UIFOLDER);
}
bool isUiFolder(Tree t, Tree& label, Tree& elements)
{
    return isTree(t, gGlobal->UIFOLDER, label, elements);
}

Tree uiWidget(Tree label, Tree varname, Tree sig)
{
    return tree(gGlobal->UIWIDGET, label, varname, sig);
}
bool isUiWidget(Tree t, Tree& label, Tree& varname, Tree& sig)
{
    return isTree(t, gGlobal->UIWIDGET, label, varname, sig);
}

// places an item in a folder. Eventually replaces the element with the same name.
Tree putFolder(Tree folder, Tree item)
{
    Tree label, content;

    if (!isUiFolder(folder, label, content)) {
        fprintf(stderr, "ERROR in addFolder : not a folder\n");
    }
    return uiFolder(label, updateKey(content, uiLabel(item), item));
}

// places an item in a folder. Without replacement
Tree addToFolder(Tree folder, Tree item)
{
    Tree label, content;

    if (!isUiFolder(folder, label, content)) {
        fprintf(stderr, "ERROR in addFolder : not a folder\n");
    }
    return uiFolder(label, addKey(content, uiLabel(item), item));
}

// get an item from a folder (or return NIL)
Tree getFolder(Tree folder, Tree ilabel)
{
    Tree flabel, content, item;
    if (!isUiFolder(folder, flabel, content)) {
        fprintf(stderr, "ERROR in getFolder : not a folder\n");
    }
    if (findKey(content, ilabel, item)) {
        return item;
    } else {
        return gGlobal->nil;
    }
}

// creates a string of folders corresponding to path and containing in fine elem
Tree makeSubFolderChain(Tree path, Tree elem)
{
    if (isNil(path)) {
        return elem;
    } else {
        return putFolder(uiFolder(hd(path), gGlobal->nil), makeSubFolderChain(tl(path), elem));
    }
}

Tree putSubFolder(Tree folder, Tree path, Tree item)
{
    if (isNil(path)) {
        // return putFolder(folder, item);
        return addToFolder(folder, item);
    } else {
        Tree subfolder = getFolder(folder, hd(path));
        if (isUiFolder(subfolder)) {
            return putFolder(folder, putSubFolder(subfolder, tl(path), item));
        } else {
            return putFolder(folder, makeSubFolderChain(path, item));
        }
    }
}

/*
 How folders work.
 Folder at 1 level : a folder contains a list of things identified by a name:
 Folder[(l1,d1)...(ln,dn)]
 where (lx,dx) is a thing dx identified by a name lx. We assume that the lx are all different.

 You can add a thing to a folder: Add(Folder, Thing) -> Folder

 If the folder already contains something with the same name, this thing is replaced by the new one.

 ADD (Folder[(l1,d1)...(ln,dn)], (lx,dx)) -> Folder[(l1,d1)...(lx,dx)...(ln,dn)]

 ADD (Folder[(l1,d1)...(lx,dx)...(ln,dn)], (lx,dx')) -> Folder[(l1,d1)...(lx,dx')...(ln,dn)]
*/

string checkNullLabel(Tree t, const string& label)
{
    return (label == "") ? string("0x00") : label;
}

string checkNullBargraphLabel(Tree t, const string& label, int direction)
{
    return (label == "") ? gGlobal->getFreshID((direction == 0) ? "hbargraph" : "vbargraph")
                         : label;
}

/**
 * Add a widget with a certain path to the user interface tree
 */
void UITree::addUIWidget(Tree path, Tree widget)
{
    fUIRoot = putSubFolder(fUIRoot, path, widget);
}

/**
 * Remove fake root folder if not needed (that is if the UI
 * is completely enclosed in one folder)
 */
Tree UITree::prepareUserInterfaceTree()
{
    Tree root, elems;
    if (isUiFolder(fUIRoot, root, elems) && isList(elems) && isNil(tl(elems))) {
        Tree folder = right(hd(elems));
        return (isUiFolder(folder)) ? folder : fUIRoot;
    }
    return fUIRoot;
}