]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/test/editor.cc
Initial revision
[helm.git] / helm / DEVEL / mathml_editor / test / editor.cc
1
2 #include "TNode.hh"
3 #include "TToken.hh"
4 #include "TDocument.hh"
5 #include "TPushParser.hh"
6 #include "TPushLexer.hh"
7 #include "TDictionary.hh"
8 #include "TListener.hh"
9 #include <GdomeSmartDOMXSLT.hh>
10
11 #include "guiGTK.h"
12
13 class MyResultListener : public DOM::EventListener
14 {
15 public:
16   MyResultListener(const std::string& s) : msg(s) { };
17
18   virtual void handleEvent(const DOM::Event&);
19
20 private:
21   const std::string msg;
22 };
23
24 void
25 MyResultListener::handleEvent(const DOM::Event& ev)
26 {
27   cout << "RECEIVED EVENT: " << ev.get_type() << " " << msg << " ";
28   const DOM::MutationEvent& me(ev);
29   assert(me);
30   const DOM::Node target(me.get_target());
31   assert(target);
32   cout << "target = " << target.get_nodeName() << " " << target.get_nodeType() << endl;
33 }
34
35 MyResultListener l1("?");
36
37 TDictionary dictionary;
38 DOM::Document result;
39
40 extern void *parseMathMLFile(char *);
41
42 bool
43 subst(const DOM::Element& e1, const DOM::GdomeString& id, const DOM::Element& e2)
44 {
45   assert(e1);
46   assert(e2);
47   if (e1.getAttribute("xref") == id)
48     {
49       DOM::Node parent = e1.get_parentNode();
50       assert(parent);
51       DOM::Node next = e1.get_nextSibling();
52       parent.removeChild(e1);
53       parent.insertBefore(e2, next);
54       //parent.replaceChild(e2, e1);
55       return true;
56     }
57   else
58     {
59       DOM::Node p = e1.get_firstChild();
60       while (p)
61         {
62           while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
63           if (p)
64             if (subst(p, id, e2)) return true;
65             else p = p.get_nextSibling();
66         }
67       return false;
68     }
69 }
70
71 class MyListener : public TListener
72 {
73 public:
74   MyListener(const DOM::XSLTStylesheet& s) : style(s) { };
75
76   void callback(TDocument& doc)
77   {
78     cout << "listener callback " << static_cast<GdomeNode*>(doc.document()) << endl;
79     TNode dirty = doc.dirtyNode();
80     if (dirty) 
81       {
82         cout << "recreating subtree with id " << std::string(dirty["id"]) << endl;
83         std::vector< std::pair<DOM::GdomeString, DOM::GdomeString> > dirtyId;
84         if (result)
85           dirtyId.push_back(std::make_pair(DOM::GdomeString("id"),
86                                            DOM::GdomeString("'" + std::string(dirty["id"]) + "'")));
87         DOM::Document res = style.apply(doc.document(), dirtyId);
88         assert(res);
89         style.save(doc.document(), stdout);
90         style.save(res, stdout);
91         if (result)
92           {
93             cout << "REPLACING A FRAGMENT OF THE DOCUMENT" << endl;
94             DOM::Element root = res.get_documentElement();
95             assert(root);
96             assert(root.hasAttribute("xref"));
97
98             if (result.get_documentElement().getAttribute("xref") == root.getAttribute("xref"))
99               {
100                 cout << "REPLACING ROOT" << endl;
101                 // the following remove should not be necessary
102                 // according to the spec replaceChild should work just fine
103                 result.removeChild(result.get_documentElement());
104                 result.appendChild(result.importNode(root, true));
105               }
106             else
107               try
108                 {
109                   cout << "before" << endl;
110                   bool ok = subst(result.get_documentElement(), root.getAttribute("xref"), result.importNode(root, true));
111                   assert(ok);
112                   cout << "after" << endl;
113                 }
114               catch (DOM::DOMException e)
115                 {
116                   cout << "!!!!!!!!!!!!!!!! EXCEPTION " << e.code << " " << e.msg << endl;
117                   assert(0);
118                 }
119           }
120         else
121           {
122             cout << "SETTING THE DOCUMENT FOR THE FIRST TIME" << endl;
123             result = res;
124
125             DOM::EventTarget et(result);
126             assert(et);
127             cout << "SETTING EVENT LISTENER (EDITOR) ON " << static_cast<GdomeNode*>(result) << endl;
128             et.addEventListener("DOMSubtreeModified", l1, true);
129
130             if (GUI_load_document(gdome_cast_doc(static_cast<GdomeNode*>(result))) < 0)
131               cerr << "c'e' stato un errore" << endl;
132           }
133
134         doc.clearDirty();
135       }
136   }
137
138 private:
139   const DOM::XSLTStylesheet& style;
140 };
141
142 struct Context
143 {
144   Context(const std::string& s, TPushLexer& l) : buffer(s), i(0), lexer(l) { };
145
146   void send(void)
147   {
148     if (i < buffer.length())
149       {
150         cout << "document is " << static_cast<GdomeNode*>(result) << endl;
151         lexer.push(buffer[i++]);
152       }
153     else lexer.push('\n');
154   }
155
156   std::string buffer;
157   unsigned i;
158   TPushLexer& lexer;
159 };
160
161 extern "C" int
162 edit_timeout(Context* data)
163 {
164   assert(data);
165   GUI_freeze();
166   data->send();
167   GUI_thaw();
168   return 1;
169 }
170
171 extern "C" void
172 push_char(Context* context, gchar ch)
173 {
174   GUI_freeze();
175   cout << "*** SENDING " << ch << endl;
176   context->lexer.push(ch);
177   GUI_thaw();
178 }
179
180 main(int argc, char* argv[])
181 {
182   cout << "loading the dictionary..." << endl;
183   dictionary.load("dictionary.xml");
184
185   cout << "loading the stylesheet..." << endl;
186   DOM::DOMImplementation di;
187   DOM::Document docStyle = di.createDocumentFromURI("./xsl/tml-mmlp.xsl");
188   DOM::XSLTStylesheet style(docStyle);
189   
190   MyListener listener(style);
191   TPushParser parser(dictionary, listener);
192   TPushLexer lexer(parser);
193
194 #if 0
195   lexer.push('$');
196   lexer.push(' ');
197   assert(result);
198 #endif
199
200 #if 0
201   DOM::Document doc = parser.document().document();
202   std::vector< std::pair<DOM::GdomeString, DOM::GdomeString> > np;
203   result = style.apply(doc, np);
204   style.save(result, stdout);
205 #endif
206
207   Context context("", lexer);
208
209   cout << "passing context " << &context << endl;
210   GUI_init(&argc, &argv, "mathmleditor", 500, 600, &context);
211   GUI_run();
212   GUI_uninit();
213   GUI_unload_document();
214 }