]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/ocaml/c_mathml_editor.cc
* added default dictionary/stylesheet paths
[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 DOM::Document&, const DOM::Document&, const DOM::Document&, 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 DOM::Document& dict, const DOM::Document& mml, const DOM::Document& tex,
75                void (*cb)(int, const char*, void*), void* data)
76 {
77   assert(cb);
78   logger = new CCallbackLogger(cb, data);
79   dictionary = new TDictionary(*logger);
80   dictionary->load(DOM::Document(dict));
81   tml_mml = new DOMX::XSLTStylesheet(mml);
82   tml_tex = new DOMX::XSLTStylesheet(tex);
83   factory = new CMathMLFactoryXSLT(*logger, *tml_mml);
84   parser = new TPushParser(*logger, *factory, *dictionary);
85   lexer = new TPushLexer(*logger, *parser);
86 }
87
88 Editor::~Editor()
89 {
90   delete lexer;
91   delete parser;
92   delete factory;
93   delete tml_tex;
94   delete tml_mml;
95   delete dictionary;
96   delete logger;
97 }
98
99 extern "C" const char*
100 c_mathml_editor_get_default_dictionary_path()
101 {
102   return TDictionary::getDefaultDictionaryPath().c_str();
103 }
104
105 extern "C" const char*
106 c_mathml_editor_get_default_mathml_stylesheet_path()
107 {
108   return AMathMLFactory::getDefaultMathMLStylesheetPath().c_str();
109 }
110
111 extern "C" const char*
112 c_mathml_editor_get_default_tex_stylesheet_path()
113 {
114   return AMathMLFactory::getDefaultTeXStylesheetPath().c_str();
115 }
116
117 extern "C" Editor*
118 c_mathml_editor_new(GdomeDocument* dictionary,
119                     GdomeDocument* tml_mml,
120                     GdomeDocument* tml_tex,
121                     void (*log_message_cb)(int, const char*, void*),
122                     void* user_data)
123 {
124   return new Editor(DOM::Document(dictionary),
125                     DOM::Document(tml_mml),
126                     DOM::Document(tml_tex), log_message_cb, user_data);
127 }
128
129 extern "C" void
130 c_mathml_editor_destroy(Editor* editor)
131 {
132   assert(editor);
133   delete editor;
134 }
135
136 extern "C" int
137 c_mathml_editor_freeze(Editor* editor)
138 {
139   assert(editor);
140   return editor->parser->freeze();
141 }
142
143 extern "C" int
144 c_mathml_editor_thaw(Editor* editor)
145 {
146   assert(editor);
147   return editor->parser->thaw();
148 }
149
150 extern "C" void
151 c_mathml_editor_push(Editor* editor, char ch)
152 {
153   assert(editor);
154   editor->lexer->push(ch);
155 }
156
157 extern "C" void
158 c_mathml_editor_drop(Editor* editor, int alt)
159 {
160   assert(editor);
161   editor->lexer->drop(alt != 0);
162 }
163
164 extern "C" int
165 c_mathml_editor_cursor_hide(Editor* editor)
166 {
167   assert(editor);
168   return editor->parser->hideCursor();
169 }
170
171 extern "C" int
172 c_mathml_editor_cursor_show(Editor* editor)
173 {
174   assert(editor);
175   return editor->parser->showCursor();
176 }
177
178 extern "C" char*
179 c_mathml_editor_get_tex(const Editor* editor)
180 {
181   assert(editor);
182   DOM::Document res = editor->tml_tex->apply(editor->parser->document());
183   assert(res);
184   res.normalize();
185   assert(res.get_firstChild() && res.get_firstChild().get_nodeName() == "#text");
186   return strdup(std::string(res.get_firstChild().get_nodeValue()).c_str());
187 }
188
189 extern "C" void
190 c_mathml_editor_reset(Editor* editor)
191 {
192   assert(editor);
193   editor->parser->reset();
194 }
195
196 extern "C" GdomeDocument*
197 c_mathml_editor_get_tml(const Editor* editor)
198 {
199   assert(editor);
200   GdomeNode* n = editor->parser->document().cloneNode(true).gdome_object();
201   GdomeDocument* doc = gdome_cast_doc(n);
202   assert(n && doc);
203   return doc;
204 }
205
206 extern "C" GdomeDocument*
207 c_mathml_editor_get_mml(const Editor* editor)
208 {
209   assert(editor);
210   GdomeNode* n = editor->factory->document().gdome_object();
211   GdomeDocument* doc = gdome_cast_doc(n);
212   assert(n && doc);
213   return doc;
214 }
215