]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/ocaml/c_mathml_editor.cc
* the documents passed to the ocaml binding have been turned into strings
[helm.git] / helm / DEVEL / mathml_editor / ocaml / c_mathml_editor.cc
1 /* This file is part of EdiTeX, an editor of mathematical
2  * expressions based on TeX syntax.
3  * 
4  * Copyright (C) 2002-2003 Luca Padovani <lpadovan@cs.unibo.it>,
5  *                    2003 Paolo Marinelli <pmarinel@cs.unibo.it>.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * For more information, please visit the project's home page
22  * http://helm.cs.unibo.it/editex/
23  * or send an email to <lpadovan@cs.unibo.it>
24  */
25
26 #include <GdomeSmartDOMXSLT.hh>
27
28 #include "ALogger.hh"
29 #include "TDictionary.hh"
30 #include "CMathMLFactoryXSLT.hh"
31 #include "TPushLexer.hh"
32 #include "TPushParser.hh"
33
34 class CCallbackLogger : public ALogger
35 {
36 public:
37   CCallbackLogger(void (*)(int, const char*, void*), void*);
38   virtual ~CCallbackLogger() { };
39
40 protected:
41   virtual void message(Level, const std::string&);
42
43 private:
44   void (*callback)(int, const char*, void*);
45   void* user_data;
46 };
47
48 CCallbackLogger::CCallbackLogger(void (*cb)(int, const char*, void*), void* data) : callback(cb), user_data(data)
49 {
50   assert(callback);
51 }
52
53 void
54 CCallbackLogger::message(Level l, const std::string& s)
55 {
56   assert(callback);
57   callback(l, s.c_str(), user_data);
58 }
59
60 struct Editor
61 {
62   Editor(const char*, const char*, const char*, void (*)(int, const char*, void*), void*);
63   ~Editor();
64
65   ALogger*        logger;
66   TDictionary*    dictionary;
67   DOMX::XSLTStylesheet* tml_mml;
68   DOMX::XSLTStylesheet* tml_tex;
69   AMathMLFactory* factory;
70   TPushParser*    parser;
71   APushLexer*     lexer;
72 };
73
74 Editor::Editor(const char* dict_uri, const char* mml_uri, const char* tex_uri,
75                void (*cb)(int, const char*, void*), void* data)
76 {
77   assert(dict_uri);
78   assert(mml_uri);
79   assert(tex_uri);
80   assert(cb);
81   logger = new CCallbackLogger(cb, data);
82   dictionary = new TDictionary(*logger);
83   dictionary->load(dict_uri);
84   DOM::DOMImplementation di;
85   DOM::Document mml = di.createDocumentFromURI(mml_uri);
86   DOM::Document tex = di.createDocumentFromURI(tex_uri);
87   tml_mml = new DOMX::XSLTStylesheet(mml);
88   tml_tex = new DOMX::XSLTStylesheet(tex);
89   factory = new CMathMLFactoryXSLT(*logger, *tml_mml);
90   parser = new TPushParser(*logger, *factory, *dictionary);
91   lexer = new TPushLexer(*logger, *parser);
92 }
93
94 Editor::~Editor()
95 {
96   delete lexer;
97   delete parser;
98   delete factory;
99   delete tml_tex;
100   delete tml_mml;
101   delete dictionary;
102   delete logger;
103 }
104
105 extern "C" const char*
106 c_mathml_editor_get_default_dictionary_path()
107 {
108   return TDictionary::getDefaultDictionaryPath().c_str();
109 }
110
111 extern "C" const char*
112 c_mathml_editor_get_default_mathml_stylesheet_path()
113 {
114   return AMathMLFactory::getDefaultMathMLStylesheetPath().c_str();
115 }
116
117 extern "C" const char*
118 c_mathml_editor_get_default_tex_stylesheet_path()
119 {
120   return AMathMLFactory::getDefaultTeXStylesheetPath().c_str();
121 }
122
123 extern "C" Editor*
124 c_mathml_editor_new(const char* dictionary_uri,
125                     const char* tml_mml_uri,
126                     const char* tml_tex_uri,
127                     void (*log_message_cb)(int, const char*, void*),
128                     void* user_data)
129 {
130   return new Editor(dictionary_uri, tml_mml_uri, tml_tex_uri, log_message_cb, user_data);
131 }
132
133 extern "C" void
134 c_mathml_editor_destroy(Editor* editor)
135 {
136   assert(editor);
137   delete editor;
138 }
139
140 extern "C" int
141 c_mathml_editor_freeze(Editor* editor)
142 {
143   assert(editor);
144   return editor->parser->freeze();
145 }
146
147 extern "C" int
148 c_mathml_editor_thaw(Editor* editor)
149 {
150   assert(editor);
151   return editor->parser->thaw();
152 }
153
154 extern "C" void
155 c_mathml_editor_push(Editor* editor, char ch)
156 {
157   assert(editor);
158   editor->lexer->push(ch);
159 }
160
161 extern "C" void
162 c_mathml_editor_drop(Editor* editor, int alt)
163 {
164   assert(editor);
165   editor->lexer->drop(alt != 0);
166 }
167
168 extern "C" int
169 c_mathml_editor_cursor_hide(Editor* editor)
170 {
171   assert(editor);
172   return editor->parser->hideCursor();
173 }
174
175 extern "C" int
176 c_mathml_editor_cursor_show(Editor* editor)
177 {
178   assert(editor);
179   return editor->parser->showCursor();
180 }
181
182 extern "C" char*
183 c_mathml_editor_get_tex(const Editor* editor)
184 {
185   assert(editor);
186   DOM::Document res = editor->tml_tex->apply(editor->parser->document());
187   assert(res);
188   res.normalize();
189   assert(res.get_firstChild() && res.get_firstChild().get_nodeName() == "#text");
190   return strdup(std::string(res.get_firstChild().get_nodeValue()).c_str());
191 }
192
193 extern "C" void
194 c_mathml_editor_reset(Editor* editor)
195 {
196   assert(editor);
197   editor->parser->reset();
198 }
199
200 extern "C" GdomeDocument*
201 c_mathml_editor_get_tml(const Editor* editor)
202 {
203   assert(editor);
204   GdomeNode* n = editor->parser->document().cloneNode(true).gdome_object();
205   GdomeDocument* doc = gdome_cast_doc(n);
206   assert(n && doc);
207   return doc;
208 }
209
210 extern "C" GdomeDocument*
211 c_mathml_editor_get_mml(const Editor* editor)
212 {
213   assert(editor);
214   GdomeNode* n = editor->factory->document().gdome_object();
215   GdomeDocument* doc = gdome_cast_doc(n);
216   assert(n && doc);
217   return doc;
218 }
219