Bug fixed for unix error "readlink /proc/self/fd/0" on MacOS.
[Faustine.git] / interpreter / preprocessor / faust-0.9.47mr3 / architecture / gui / GUI.h
1 #ifndef FAUST_GUI_H
2 #define FAUST_GUI_H
3
4 #include "UI.h"
5 #include <list>
6 #include <map>
7
8 using namespace std;
9
10
11 /*******************************************************************************
12 * GUI : Abstract Graphic User Interface
13 * Provides additional macchanismes to synchronize widgets and zones. Widgets
14 * should both reflect the value of a zone and allow to change this value.
15 ******************************************************************************/
16
17 struct uiItem;
18 typedef void (*uiCallback)(float val, void* data);
19
20 class GUI : public UI
21 {
22 typedef list<uiItem*> clist;
23 typedef map<float*, clist*> zmap;
24
25 private:
26 static list<GUI*> fGuiList;
27 zmap fZoneMap;
28 bool fStopped;
29
30 public:
31
32 GUI() : fStopped(false) {
33 fGuiList.push_back(this);
34 }
35
36 virtual ~GUI() {
37 // suppression de this dans fGuiList
38 }
39
40 // -- registerZone(z,c) : zone management
41
42 void registerZone(float* z, uiItem* c)
43 {
44 if (fZoneMap.find(z) == fZoneMap.end()) fZoneMap[z] = new clist();
45 fZoneMap[z]->push_back(c);
46 }
47
48 void updateAllZones();
49
50 void updateZone(float* z);
51
52 static void updateAllGuis()
53 {
54 list<GUI*>::iterator g;
55 for (g = fGuiList.begin(); g != fGuiList.end(); g++) {
56 (*g)->updateAllZones();
57 }
58 }
59 void addCallback(float* zone, uiCallback foo, void* data);
60 virtual void show() {};
61 virtual void run() {};
62
63 void stop() { fStopped = true; }
64 bool stopped() { return fStopped; }
65
66 virtual void declare(float* , const char* , const char* ) {}
67 };
68
69
70
71 /**
72 * User Interface Item: abstract definition
73 */
74
75 class uiItem
76 {
77 protected :
78
79 GUI* fGUI;
80 float* fZone;
81 float fCache;
82
83 uiItem (GUI* ui, float* zone) : fGUI(ui), fZone(zone), fCache(-123456.654321)
84 {
85 ui->registerZone(zone, this);
86 }
87
88
89 public :
90 virtual ~uiItem() {}
91
92 void modifyZone(float v)
93 {
94 fCache = v;
95 if (*fZone != v) {
96 *fZone = v;
97 fGUI->updateZone(fZone);
98 }
99 }
100
101 float cache() { return fCache; }
102 virtual void reflectZone() = 0;
103 };
104
105
106 /**
107 * Callback Item
108 */
109
110 struct uiCallbackItem : public uiItem
111 {
112 uiCallback fCallback;
113 void* fData;
114
115 uiCallbackItem(GUI* ui, float* zone, uiCallback foo, void* data)
116 : uiItem(ui, zone), fCallback(foo), fData(data) {}
117
118 virtual void reflectZone() {
119 float v = *fZone;
120 fCache = v;
121 fCallback(v, fData);
122 }
123 };
124
125 // en cours d'installation de call back. a finir!!!!!
126
127 /**
128 * Update all user items reflecting zone z
129 */
130
131 inline void GUI::updateZone(float* z)
132 {
133 float v = *z;
134 clist* l = fZoneMap[z];
135 for (clist::iterator c = l->begin(); c != l->end(); c++) {
136 if ((*c)->cache() != v) (*c)->reflectZone();
137 }
138 }
139
140
141 /**
142 * Update all user items not up to date
143 */
144
145 inline void GUI::updateAllZones()
146 {
147 for (zmap::iterator m = fZoneMap.begin(); m != fZoneMap.end(); m++) {
148 float* z = m->first;
149 clist* l = m->second;
150 float v = *z;
151 for (clist::iterator c = l->begin(); c != l->end(); c++) {
152 if ((*c)->cache() != v) (*c)->reflectZone();
153 }
154 }
155 }
156
157 inline void GUI::addCallback(float* zone, uiCallback foo, void* data)
158 {
159 new uiCallbackItem(this, zone, foo, data);
160 };
161
162 #endif