]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/test/editor.cc
Added some controls concerning the graphical deleting.
[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                 result.replaceChild(result.importNode(root, true), result.get_documentElement());
102 #if 0
103                 // the following remove should not be necessary
104                 // according to the spec replaceChild should work just fine
105                 result.removeChild(result.get_documentElement());
106                 result.appendChild(result.importNode(root, true));
107 #endif
108               }
109             else
110               try
111                 {
112                   cout << "before" << endl;
113                   bool ok = subst(result.get_documentElement(), root.getAttribute("xref"), result.importNode(root, true));
114                   assert(ok);
115                   cout << "after" << endl;
116                 }
117               catch (DOM::DOMException e)
118                 {
119                   cout << "!!!!!!!!!!!!!!!! EXCEPTION " << e.code << " " << e.msg << endl;
120                   assert(0);
121                 }
122           }
123         else
124           {
125             cout << "SETTING THE DOCUMENT FOR THE FIRST TIME" << endl;
126             result = res;
127
128             DOM::EventTarget et(result);
129             assert(et);
130             cout << "SETTING EVENT LISTENER (EDITOR) ON " << static_cast<GdomeNode*>(result) << endl;
131             et.addEventListener("DOMSubtreeModified", l1, true);
132
133             if (GUI_load_document(gdome_cast_doc(static_cast<GdomeNode*>(result))) < 0)
134               cerr << "c'e' stato un errore" << endl;
135           }
136         style.save(result, stdout);
137
138         doc.clearDirty();
139       }
140   }
141
142 private:
143   const DOM::XSLTStylesheet& style;
144 };
145
146 struct Context
147 {
148   Context(const std::string& s, TPushLexer& l) : buffer(s), i(0), lexer(l) { };
149
150   void send(void)
151   {
152     if (i < buffer.length())
153       {
154         cout << "document is " << static_cast<GdomeNode*>(result) << endl;
155         lexer.push(buffer[i++]);
156       }
157     else lexer.push('\n');
158   }
159
160   std::string buffer;
161   unsigned i;
162   TPushLexer& lexer;
163 };
164
165 extern "C" int
166 edit_timeout(Context* data)
167 {
168   assert(data);
169   GUI_freeze();
170   data->send();
171   GUI_thaw();
172   return 1;
173 }
174
175 extern "C" void
176 push_char(Context* context, gchar ch)
177 {
178   GUI_freeze();
179   cout << "*** SENDING " << ch << endl;
180   context->lexer.push(ch);
181   GUI_thaw();
182 }
183
184 main(int argc, char* argv[])
185 {
186   cout << "loading the dictionary..." << endl;
187   dictionary.load("dictionary.xml");
188
189   cout << "loading the stylesheet..." << endl;
190   DOM::DOMImplementation di;
191   DOM::Document docStyle = di.createDocumentFromURI("./xsl/tml-mmlp.xsl");
192   DOM::XSLTStylesheet style(docStyle);
193   
194   MyListener listener(style);
195   TPushParser parser(dictionary, listener);
196   TPushLexer lexer(parser);
197
198 #if 0
199   lexer.push('$');
200   lexer.push(' ');
201   assert(result);
202 #endif
203
204 #if 0
205   DOM::Document doc = parser.document().document();
206   std::vector< std::pair<DOM::GdomeString, DOM::GdomeString> > np;
207   result = style.apply(doc, np);
208   style.save(result, stdout);
209 #endif
210
211   Context context("", lexer);
212
213   cout << "passing context " << &context << endl;
214   GUI_init(&argc, &argv, "mathmleditor", 500, 600, &context);
215   GUI_run();
216   GUI_uninit();
217   GUI_unload_document();
218 }