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