]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/ocaml/c_mathml_editor.cc
ocaml 3.09 transition
[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 #include <cassert>
28
29 #include "ALogger.hh"
30 #include "TDictionary.hh"
31 #include "CMathMLFactoryXSLT.hh"
32 #include "TPushLexer.hh"
33 #include "LPushLexer.hh"
34 #include "TPushParser.hh"
35
36 class CCallbackLogger : public ALogger
37 {
38 public:
39   CCallbackLogger(void (*)(int, const char*, void*), void*);
40   virtual ~CCallbackLogger() { };
41
42 protected:
43   virtual void message(Level, const std::string&);
44
45 private:
46   void (*callback)(int, const char*, void*);
47   void* user_data;
48 };
49
50 CCallbackLogger::CCallbackLogger(void (*cb)(int, const char*, void*), void* data) : callback(cb), user_data(data)
51 {
52   assert(callback);
53 }
54
55 void
56 CCallbackLogger::message(Level l, const std::string& s)
57 {
58   assert(callback);
59   callback(l, s.c_str(), user_data);
60 }
61
62 struct Editor
63 {
64   Editor(const char*, const char*, const char*, void (*)(int, const char*, void*), void*, bool);
65   ~Editor();
66
67   ALogger*        logger;
68   TDictionary*    dictionary;
69   DOMX::XSLTStylesheet* tml_mml;
70   DOMX::XSLTStylesheet* tml_tex;
71   AMathMLFactory* factory;
72   TPushParser*    parser;
73   APushLexer*     lexer;
74 };
75
76 Editor::Editor(const char* dict_uri, const char* mml_uri, const char* tex_uri,
77                void (*cb)(int, const char*, void*), void* data, bool alt)
78 {
79   assert(dict_uri);
80   assert(mml_uri);
81   assert(tex_uri);
82   assert(cb);
83   logger = new CCallbackLogger(cb, data);
84   dictionary = new TDictionary(*logger);
85   dictionary->load(dict_uri);
86   DOM::DOMImplementation di;
87   DOM::Document mml = di.createDocumentFromURI(mml_uri);
88   DOM::Document tex = di.createDocumentFromURI(tex_uri);
89   tml_mml = new DOMX::XSLTStylesheet(mml);
90   tml_tex = new DOMX::XSLTStylesheet(tex);
91   factory = new CMathMLFactoryXSLT(*logger, *tml_mml);
92   parser = new TPushParser(*logger, *factory, *dictionary);
93   if (alt) lexer = new LPushLexer(*logger, *parser);
94   else lexer = new TPushLexer(*logger, *parser);
95 }
96
97 Editor::~Editor()
98 {
99   delete lexer;
100   delete parser;
101   delete factory;
102   delete tml_tex;
103   delete tml_mml;
104   delete dictionary;
105   delete logger;
106 }
107
108 extern "C" const char*
109 c_mathml_editor_get_default_dictionary_path()
110 {
111   return TDictionary::getDefaultDictionaryPath().c_str();
112 }
113
114 extern "C" const char*
115 c_mathml_editor_get_default_mathml_stylesheet_path()
116 {
117   return AMathMLFactory::getDefaultMathMLStylesheetPath().c_str();
118 }
119
120 extern "C" const char*
121 c_mathml_editor_get_default_tex_stylesheet_path()
122 {
123   return AMathMLFactory::getDefaultTeXStylesheetPath().c_str();
124 }
125
126 extern "C" Editor*
127 c_mathml_editor_new(bool alt,
128                     const char* dictionary_uri,
129                     const char* tml_mml_uri,
130                     const char* tml_tex_uri,
131                     void (*log_message_cb)(int, const char*, void*),
132                     void* user_data)
133 {
134   return new Editor(dictionary_uri, tml_mml_uri, tml_tex_uri, log_message_cb, user_data, alt);
135 }
136
137 extern "C" void
138 c_mathml_editor_destroy(Editor* editor)
139 {
140   assert(editor);
141   delete editor;
142 }
143
144 extern "C" int
145 c_mathml_editor_freeze(Editor* editor)
146 {
147   assert(editor);
148   return editor->parser->freeze();
149 }
150
151 extern "C" int
152 c_mathml_editor_thaw(Editor* editor)
153 {
154   assert(editor);
155   return editor->parser->thaw();
156 }
157
158 extern "C" void
159 c_mathml_editor_push(Editor* editor, char ch)
160 {
161   assert(editor);
162   editor->lexer->push(ch);
163 }
164
165 extern "C" void
166 c_mathml_editor_drop(Editor* editor, int alt)
167 {
168   assert(editor);
169   editor->lexer->drop(alt != 0);
170 }
171
172 extern "C" int
173 c_mathml_editor_cursor_hide(Editor* editor)
174 {
175   assert(editor);
176   return editor->parser->hideCursor();
177 }
178
179 extern "C" int
180 c_mathml_editor_cursor_show(Editor* editor)
181 {
182   assert(editor);
183   return editor->parser->showCursor();
184 }
185
186 extern "C" char*
187 c_mathml_editor_get_tex(const Editor* editor)
188 {
189   assert(editor);
190   DOM::Document res = editor->tml_tex->apply(editor->parser->document());
191   assert(res);
192   res.normalize();
193   assert(res.get_firstChild() && res.get_firstChild().get_nodeName() == "#text");
194   return strdup(std::string(res.get_firstChild().get_nodeValue()).c_str());
195 }
196
197 extern "C" void
198 c_mathml_editor_reset(Editor* editor)
199 {
200   assert(editor);
201   editor->lexer->reset();
202   editor->parser->reset();
203 }
204
205 extern "C" GdomeDocument*
206 c_mathml_editor_get_tml(const Editor* editor)
207 {
208   assert(editor);
209   GdomeNode* n = editor->parser->document().cloneNode(true).gdome_object();
210   GdomeDocument* doc = gdome_cast_doc(n);
211   assert(n && doc);
212   return doc;
213 }
214
215 extern "C" GdomeDocument*
216 c_mathml_editor_get_mml(const Editor* editor)
217 {
218   assert(editor);
219   GdomeNode* n = editor->factory->document().gdome_object();
220   GdomeDocument* doc = gdome_cast_doc(n);
221   assert(n && doc);
222   return doc;
223 }
224