]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/ocaml/c_mathml_editor.cc
* added license and copyright to every source file
[helm.git] / helm / DEVEL / mathml_editor / ocaml / c_mathml_editor.cc
1 /* Copyright (C) 2002-2003, Luca Padovani <luca.padovani@cs.unibo.it>,
2  *                    2003, Paolo Marinelli <pmarinel@cs.unibo.it>.
3  *
4  * This file is part of EdiTeX, an editor of mathematical
5  * expressions based on TeX syntax
6  * 
7  * EdiTeX is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * EdiTeX 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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with EdiTeX; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  * 
21  * For details, see the EdiTeX World-Wide-Web page,
22  * http://helm.cs.unibo.it/editex, or send a mail to
23  * <luca.padovani@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" Editor*
100 c_mathml_editor_new(GdomeDocument* dictionary,
101                     GdomeDocument* tml_mml,
102                     GdomeDocument* tml_tex,
103                     void (*log_message_cb)(int, const char*, void*),
104                     void* user_data)
105 {
106   return new Editor(DOM::Document(dictionary),
107                     DOM::Document(tml_mml),
108                     DOM::Document(tml_tex), log_message_cb, user_data);
109 }
110
111 extern "C" void
112 c_mathml_editor_destroy(Editor* editor)
113 {
114   assert(editor);
115   delete editor;
116 }
117
118 extern "C" int
119 c_mathml_editor_freeze(Editor* editor)
120 {
121   assert(editor);
122   return editor->parser->freeze();
123 }
124
125 extern "C" int
126 c_mathml_editor_thaw(Editor* editor)
127 {
128   assert(editor);
129   return editor->parser->thaw();
130 }
131
132 extern "C" void
133 c_mathml_editor_push(Editor* editor, char ch)
134 {
135   assert(editor);
136   editor->lexer->push(ch);
137 }
138
139 extern "C" void
140 c_mathml_editor_drop(Editor* editor, int alt)
141 {
142   assert(editor);
143   editor->lexer->drop(alt != 0);
144 }
145
146 extern "C" int
147 c_mathml_editor_cursor_hide(Editor* editor)
148 {
149   assert(editor);
150   return editor->parser->hideCursor();
151 }
152
153 extern "C" int
154 c_mathml_editor_cursor_show(Editor* editor)
155 {
156   assert(editor);
157   return editor->parser->showCursor();
158 }
159
160 extern "C" char*
161 c_mathml_editor_get_tex(const Editor* editor)
162 {
163   assert(editor);
164   DOM::Document res = editor->tml_tex->apply(editor->parser->document());
165   assert(res);
166   res.normalize();
167   assert(res.get_firstChild() && res.get_firstChild().get_nodeName() == "#text");
168   return strdup(std::string(res.get_firstChild().get_nodeValue()).c_str());
169 }
170
171 extern "C" void
172 c_mathml_editor_reset(Editor* editor)
173 {
174   assert(editor);
175   editor->parser->reset();
176 }
177
178 extern "C" GdomeDocument*
179 c_mathml_editor_get_tml(const Editor* editor)
180 {
181   assert(editor);
182   GdomeNode* n = editor->parser->document().cloneNode(true).gdome_object();
183   GdomeDocument* doc = gdome_cast_doc(n);
184   assert(n && doc);
185   return doc;
186 }
187
188 extern "C" GdomeDocument*
189 c_mathml_editor_get_mml(const Editor* editor)
190 {
191   assert(editor);
192   GdomeNode* n = editor->factory->document().gdome_object();
193   GdomeDocument* doc = gdome_cast_doc(n);
194   assert(n && doc);
195   return doc;
196 }
197