]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/ocaml/c_mathml_editor.cc
* added show/hide cursro methods
[helm.git] / helm / DEVEL / mathml_editor / ocaml / c_mathml_editor.cc
1
2 #include <GdomeSmartDOMXSLT.hh>
3
4 #include "ALogger.hh"
5 #include "TDictionary.hh"
6 #include "CMathMLFactoryXSLT.hh"
7 #include "TPushLexer.hh"
8 #include "TPushParser.hh"
9
10 class CCallbackLogger : public ALogger
11 {
12 public:
13   CCallbackLogger(void (*)(int, const char*, void*), void*);
14   virtual ~CCallbackLogger() { };
15
16 protected:
17   virtual void message(Level, const std::string&);
18
19 private:
20   void (*callback)(int, const char*, void*);
21   void* user_data;
22 };
23
24 CCallbackLogger::CCallbackLogger(void (*cb)(int, const char*, void*), void* data) : callback(cb), user_data(data)
25 {
26   assert(callback);
27 }
28
29 void
30 CCallbackLogger::message(Level l, const std::string& s)
31 {
32   assert(callback);
33   callback(l, s.c_str(), user_data);
34 }
35
36 struct Editor
37 {
38   Editor(const DOM::Document&, const DOM::Document&, const DOM::Document&, void (*)(int, const char*, void*), void*);
39   ~Editor();
40
41   ALogger*        logger;
42   TDictionary*    dictionary;
43   DOMX::XSLTStylesheet* tml_mml;
44   DOMX::XSLTStylesheet* tml_tex;
45   AMathMLFactory* factory;
46   TPushParser*    parser;
47   APushLexer*     lexer;
48 };
49
50 Editor::Editor(const DOM::Document& dict, const DOM::Document& mml, const DOM::Document& tex,
51                void (*cb)(int, const char*, void*), void* data)
52 {
53   assert(cb);
54   logger = new CCallbackLogger(cb, data);
55   dictionary = new TDictionary(*logger);
56   dictionary->load(DOM::Document(dict));
57   tml_mml = new DOMX::XSLTStylesheet(mml);
58   tml_tex = new DOMX::XSLTStylesheet(tex);
59   factory = new CMathMLFactoryXSLT(*logger, *tml_mml);
60   parser = new TPushParser(*logger, *factory, *dictionary);
61   lexer = new TPushLexer(*logger, *parser);
62 }
63
64 Editor::~Editor()
65 {
66   delete lexer;
67   delete parser;
68   delete factory;
69   delete tml_tex;
70   delete tml_mml;
71   delete dictionary;
72   delete logger;
73 }
74
75 extern "C" Editor*
76 c_mathml_editor_new(GdomeDocument* dictionary,
77                     GdomeDocument* tml_mml,
78                     GdomeDocument* tml_tex,
79                     void (*log_message_cb)(int, const char*, void*),
80                     void* user_data)
81 {
82   return new Editor(DOM::Document(dictionary),
83                     DOM::Document(tml_mml),
84                     DOM::Document(tml_tex), log_message_cb, user_data);
85 }
86
87 extern "C" void
88 c_mathml_editor_destroy(Editor* editor)
89 {
90   assert(editor);
91   delete editor;
92 }
93
94 extern "C" int
95 c_mathml_editor_freeze(Editor* editor)
96 {
97   assert(editor);
98   return editor->parser->freeze();
99 }
100
101 extern "C" int
102 c_mathml_editor_thaw(Editor* editor)
103 {
104   assert(editor);
105   return editor->parser->thaw();
106 }
107
108 extern "C" void
109 c_mathml_editor_push(Editor* editor, char ch)
110 {
111   assert(editor);
112   editor->lexer->push(ch);
113 }
114
115 extern "C" void
116 c_mathml_editor_drop(Editor* editor, int alt)
117 {
118   assert(editor);
119   editor->lexer->drop(alt != 0);
120 }
121
122 extern "C" int
123 c_mathml_editor_cursor_hide(Editor* editor)
124 {
125   assert(editor);
126   return editor->parser->hideCursor();
127 }
128
129 extern "C" int
130 c_mathml_editor_cursor_show(Editor* editor)
131 {
132   assert(editor);
133   return editor->parser->showCursor();
134 }
135
136 extern "C" char*
137 c_mathml_editor_get_tex(const Editor* editor)
138 {
139   assert(editor);
140   DOM::Document res = editor->tml_tex->apply(editor->parser->document());
141   assert(res);
142   res.normalize();
143   assert(res.get_firstChild() && res.get_firstChild().get_nodeName() == "#text");
144   return strdup(std::string(res.get_firstChild().get_nodeValue()).c_str());
145 }
146
147 extern "C" void
148 c_mathml_editor_reset(Editor* editor)
149 {
150   assert(editor);
151   editor->parser->reset();
152 }
153
154 extern "C" GdomeDocument*
155 c_mathml_editor_get_tml(const Editor* editor)
156 {
157   assert(editor);
158   GdomeNode* n = editor->parser->document().cloneNode(true).gdome_object();
159   GdomeDocument* doc = gdome_cast_doc(n);
160   assert(n && doc);
161   return doc;
162 }
163
164 extern "C" GdomeDocument*
165 c_mathml_editor_get_mml(const Editor* editor)
166 {
167   assert(editor);
168   GdomeNode* n = editor->factory->document().gdome_object();
169   GdomeDocument* doc = gdome_cast_doc(n);
170   assert(n && doc);
171   return doc;
172 }
173