]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/DEVEL/mathml_editor/test/editor.cc
ocaml 3.09 transition
[helm.git] / helm / DEVEL / mathml_editor / test / editor.cc
index 8ee408a3db41777c1a0c9aa080c5175bf35d7f7e..3efc17cee2168b72acb5429a1f73c3c9bdb12c78 100644 (file)
-
-#include "TNode.hh"
-#include "TToken.hh"
-#include "TDocument.hh"
+/* This file is part of EdiTeX, an editor of mathematical
+ * expressions based on TeX syntax.
+ * 
+ * Copyright (C) 2002-2003 Luca Padovani <lpadovan@cs.unibo.it>,
+ *                    2003 Paolo Marinelli <pmarinel@cs.unibo.it>.
+ *
+ * This library 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 library 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 library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * For more information, please visit the project's home page
+ * http://helm.cs.unibo.it/editex/
+ * or send an email to <lpadovan@cs.unibo.it>
+ */
+
+#include <cassert>
+
+#include "dom.hh"
 #include "TPushParser.hh"
-#include "TPushLexer.hh"
+#include "timer.hh"
+
+#include "ILPushLexer.hh"
 #include "TDictionary.hh"
-#include "TListener.hh"
-#include <GdomeSmartDOMXSLT.hh>
+#include "CLoggerConsole.hh"
+#include "CMathMLFactoryXSLT.hh"
+#include "CMathMLFactoryXSLTDiff.hh"
+#include "AMathMLConsumer.hh"
 
 #include "guiGTK.h"
 
-class MyResultListener : public DOM::EventListener
-{
-public:
-  MyResultListener(const std::string& s) : msg(s) { };
-
-  virtual void handleEvent(const DOM::Event&);
-
-private:
-  const std::string msg;
-};
-
-void
-MyResultListener::handleEvent(const DOM::Event& ev)
-{
-  cout << "RECEIVED EVENT: " << ev.get_type() << " " << msg << " ";
-  const DOM::MutationEvent& me(ev);
-  assert(me);
-  const DOM::Node target(me.get_target());
-  assert(target);
-  cout << "target = " << target.get_nodeName() << " " << target.get_nodeType() << endl;
-}
-
-MyResultListener l1("?");
-
-TDictionary dictionary;
-DOM::Document result;
-
 extern void *parseMathMLFile(char *);
 
-bool
-subst(const DOM::Element& e1, const DOM::GdomeString& id, const DOM::Element& e2)
-{
-  assert(e1);
-  assert(e2);
-  if (e1.getAttribute("xref") == id)
-    {
-      DOM::Node parent = e1.get_parentNode();
-      assert(parent);
-      DOM::Node next = e1.get_nextSibling();
-      parent.removeChild(e1);
-      parent.insertBefore(e2, next);
-      //parent.replaceChild(e2, e1);
-      return true;
-    }
-  else
-    {
-      DOM::Node p = e1.get_firstChild();
-      while (p)
-       {
-         while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
-         if (p)
-           if (subst(p, id, e2)) return true;
-           else p = p.get_nextSibling();
-       }
-      return false;
-    }
-}
-
-class MyListener : public TListener
-{
-public:
-  MyListener(const DOM::XSLTStylesheet& s) : style(s) { };
-
-  void callback(TDocument& doc)
-  {
-    cout << "listener callback " << static_cast<GdomeNode*>(doc.document()) << endl;
-    TNode dirty = doc.dirtyNode();
-    if (dirty) 
-      {
-       cout << "recreating subtree with id " << std::string(dirty["id"]) << endl;
-       std::vector< std::pair<DOM::GdomeString, DOM::GdomeString> > dirtyId;
-       if (result)
-         dirtyId.push_back(std::make_pair(DOM::GdomeString("id"),
-                                          DOM::GdomeString("'" + std::string(dirty["id"]) + "'")));
-       DOM::Document res = style.apply(doc.document(), dirtyId);
-       assert(res);
-       style.save(doc.document(), stdout);
-       //style.save(res, stdout);
-       if (result)
-         {
-           cout << "REPLACING A FRAGMENT OF THE DOCUMENT" << endl;
-           DOM::Element root = res.get_documentElement();
-           assert(root);
-           assert(root.hasAttribute("xref"));
-
-           if (result.get_documentElement().getAttribute("xref") == root.getAttribute("xref"))
-             {
-               cout << "REPLACING ROOT" << endl;
-               result.replaceChild(result.importNode(root, true), result.get_documentElement());
-#if 0
-               // the following remove should not be necessary
-               // according to the spec replaceChild should work just fine
-               result.removeChild(result.get_documentElement());
-               result.appendChild(result.importNode(root, true));
-#endif
-             }
-           else
-             try
-               {
-                 cout << "before" << endl;
-                 bool ok = subst(result.get_documentElement(), root.getAttribute("xref"), result.importNode(root, true));
-                 assert(ok);
-                 cout << "after" << endl;
-               }
-             catch (DOM::DOMException e)
-               {
-                 cout << "!!!!!!!!!!!!!!!! EXCEPTION " << e.code << " " << e.msg << endl;
-                 assert(0);
-               }
-         }
-       else
-         {
-           cout << "SETTING THE DOCUMENT FOR THE FIRST TIME" << endl;
-           result = res;
-
-           DOM::EventTarget et(result);
-           assert(et);
-           cout << "SETTING EVENT LISTENER (EDITOR) ON " << static_cast<GdomeNode*>(result) << endl;
-           et.addEventListener("DOMSubtreeModified", l1, true);
-
-            if (GUI_load_document(gdome_cast_doc(static_cast<GdomeNode*>(result))) < 0)
-             cerr << "c'e' stato un errore" << endl;
-         }
-       style.save(result, stdout);
-
-       doc.clearDirty();
-      }
-  }
-
-private:
-  const DOM::XSLTStylesheet& style;
-};
-
 struct Context
 {
-  Context(const std::string& s, TPushLexer& l) : buffer(s), i(0), lexer(l) { };
+  /*
+  Context(const std::string& s, TPushLexer& l, TPushParser& p) : buffer(s), i(0), lexer(l), parser(p) { };
+  */
+  Context(const std::string& s, APushLexer& l, TPushParser& p, DOMX::XSLTStylesheet& ts) 
+    : buffer(s), i(0), lexer(l), parser(p), texStyle(ts) { };
 
   void send(void)
   {
-    if (i < buffer.length())
-      {
-       cout << "document is " << static_cast<GdomeNode*>(result) << endl;
-       lexer.push(buffer[i++]);
-      }
-    else lexer.push('\n');
+    if (i < buffer.length()) lexer.push(buffer[i++]);
   }
 
   std::string buffer;
   unsigned i;
-  TPushLexer& lexer;
+  APushLexer& lexer;
+  TPushParser& parser;
+  DOMX::XSLTStylesheet& texStyle;
 };
 
+extern "C" void
+edit_output_tex(Context* data)
+{
+  assert(data);
+  DOM::Document res = data->texStyle.apply(data->parser.document());
+#if 0
+  res.normalize();
+  DOM::Node c = res.get_firstChild();
+  if (c) std::cout << "HEY, there is a child! " << c.get_nodeName() << " " << c.get_nodeValue() << std::endl;
+#endif
+  //data->texStyle.save(res, stdout);
+}
+
 extern "C" int
 edit_timeout(Context* data)
 {
@@ -173,27 +84,93 @@ edit_timeout(Context* data)
 }
 
 extern "C" void
-push_char(Context* context, gchar ch)
+edit_push_char(Context* context, gchar ch)
 {
+  assert(context != NULL);
+  long t0 = getTimer();
   GUI_freeze();
-  cout << "*** SENDING " << ch << endl;
+  std::cout << "*** SENDING " << ch << std::endl;
   context->lexer.push(ch);
   GUI_thaw();
+  long t1 = getTimer();
+  std::cout << "=== OVERALL TIME = " << (t1 - t0) / 1000 << std::endl;
+}
+
+#include <unistd.h>
+
+extern "C" void
+edit_push_string(Context* context, const gchar* s)
+{
+  assert(context != NULL);
+  assert(s != NULL);
+#if 0
+//   GUI_freeze();
+//   context->parser.freeze();
+  for (unsigned i = 0; s[i]; i++)
+    {
+      GUI_freeze();
+      context->lexer.push(s[i]);
+      GUI_thaw();
+      usleep(100000);
+      usleep(100000);
+    }
+//   context->parser.thaw();
+//   GUI_thaw();
+#endif
+  context->buffer = s;
+}
+
+extern "C" void
+edit_drop(Context* context, gboolean alt, gboolean control)
+{
+  // At the moment, the last parameter is not used, but it will
+  // be useful when we will handle the "fast" deletion
+  assert(context != NULL);
+  GUI_freeze();
+  context->lexer.drop(alt);
+  GUI_thaw();
 }
 
+extern "C" void
+edit_reset_tex(Context* context)
+{
+  assert(context != NULL);
+  GUI_freeze();
+  context->lexer.reset();
+  context->parser.reset();
+  GUI_thaw();
+}
+
+extern "C" void
+edit_complete(Context* context)
+{
+  assert(context != NULL);
+  GUI_freeze();
+  if (!context->lexer.complete()) context->lexer.push('\t');
+  GUI_thaw();
+}
+
+int
 main(int argc, char* argv[])
 {
-  cout << "loading the dictionary..." << endl;
-  dictionary.load("dictionary.xml");
+  CLoggerConsole logger;
+  logger.verbosity(ALogger::Debug);
 
-  cout << "loading the stylesheet..." << endl;
+  TDictionary dictionary(logger);
+  logger.info("loading the dictionary...");
+  dictionary.load("./dict/dictionary-tex.xml");
+
+  logger.info("loading the stylesheet...");
   DOM::DOMImplementation di;
-  DOM::Document docStyle = di.createDocumentFromURI("./xsl/tml-mmlp.xsl");
-  DOM::XSLTStylesheet style(docStyle);
-  
-  MyListener listener(style);
-  TPushParser parser(dictionary, listener);
-  TPushLexer lexer(parser);
+  DOM::Document mmlStyleDoc = di.createDocumentFromURI("./xsl/tml-mmlp.xsl");
+  DOMX::XSLTStylesheet mmlStyle(mmlStyleDoc);
+
+  DOM::Document texStyleDoc = di.createDocumentFromURI("./xsl/tml-texid.xsl");
+  DOMX::XSLTStylesheet texStyle(texStyleDoc);
+
+  CMathMLFactoryXSLT factory(logger, mmlStyle);
+  TPushParser parser(logger, factory, dictionary);
+  ILPushLexer lexer(logger, parser, dictionary);
 
 #if 0
   lexer.push('$');
@@ -208,11 +185,12 @@ main(int argc, char* argv[])
   style.save(result, stdout);
 #endif
 
-  Context context("", lexer);
+  Context context("", lexer, parser, texStyle);
 
-  cout << "passing context " << &context << endl;
-  GUI_init(&argc, &argv, "mathmleditor", 500, 600, &context);
+  GUI_init(&argc, &argv, "EditTeX", 500, 600, &context);
+  GUI_load_document(gdome_cast_doc(static_cast<GdomeNode*>(factory.document())));
   GUI_run();
   GUI_uninit();
   GUI_unload_document();
+
 }