]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/ocaml/ml_mathml_editor.c
ocaml 3.09 transition
[helm.git] / helm / DEVEL / mathml_editor / ocaml / ml_mathml_editor.c
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 <assert.h>
27
28 #include <caml/memory.h>
29 #include <caml/custom.h>
30 #include <caml/callback.h>
31
32 #include "mlgdomevalue.h"
33
34 #include "c_mathml_editor.h"
35
36 typedef struct
37 {
38   Editor* c_editor;
39   value   callback;
40 } ml_Editor;
41
42 ml_Editor*
43 Editor_val(value v)
44 {
45   ml_Editor* editor = *((ml_Editor**) Data_custom_val(v));
46   assert(editor != NULL);
47   return editor;
48 }
49
50 static void
51 ml_mathml_editor_finalize(value v)
52 {
53   ml_Editor* editor = Editor_val(v);
54   assert(editor);
55
56   remove_global_root(&editor->callback);
57   c_mathml_editor_destroy(editor->c_editor);
58   free(editor);
59 }
60
61 static void
62 ml_mathml_editor_log_callback(int level, const char* msg, void* user_data)
63 {
64   ml_Editor* ml_editor = (ml_Editor*) user_data;
65   assert(ml_editor);
66   callback2(ml_editor->callback, Val_int(level), copy_string(msg));
67 }
68
69 value
70 ml_mathml_editor_get_default_dictionary_path(value unit)
71 {
72   CAMLparam1(unit);
73   CAMLreturn(copy_string(c_mathml_editor_get_default_dictionary_path()));
74 }
75
76 value
77 ml_mathml_editor_get_default_mathml_stylesheet_path(value unit)
78 {
79   CAMLparam1(unit);
80   CAMLreturn(copy_string(c_mathml_editor_get_default_mathml_stylesheet_path()));
81 }
82
83 value
84 ml_mathml_editor_get_default_tex_stylesheet_path(value unit)
85 {
86   CAMLparam1(unit);
87   CAMLreturn(copy_string(c_mathml_editor_get_default_tex_stylesheet_path()));
88 }
89
90 value
91 ml_mathml_editor_new(value alt,
92                      value dictionary_uri,
93                      value tml_mml_uri,
94                      value tml_tex_uri,
95                      value log_message_cb)
96 {
97   static struct custom_operations ops =
98   {
99     "HELM/MathML Editor",
100     ml_mathml_editor_finalize,
101     custom_compare_default,
102     custom_hash_default,
103     custom_serialize_default,
104     custom_deserialize_default
105   };
106   
107   value v = alloc_custom(&ops, sizeof(ml_Editor*), 0, 1);
108   ml_Editor** ml_editor_ref = (ml_Editor**) Data_custom_val(v);
109   ml_Editor* ml_editor = *ml_editor_ref = malloc(sizeof(ml_Editor));
110   ml_editor->c_editor = c_mathml_editor_new(Bool_val(alt),
111                                             String_val(dictionary_uri),
112                                             String_val(tml_mml_uri),
113                                             String_val(tml_tex_uri),
114                                             ml_mathml_editor_log_callback,
115                                             (void*) ml_editor);
116   ml_editor->callback = log_message_cb;
117   register_global_root(&ml_editor->callback);
118
119   return v;
120 }
121
122 value
123 ml_mathml_editor_freeze(value v)
124 {
125   CAMLparam1(v);
126   ml_Editor* editor = Editor_val(v);
127   CAMLreturn(Val_bool(c_mathml_editor_freeze(editor->c_editor)));
128 }
129
130 value
131 ml_mathml_editor_thaw(value v)
132 {
133   CAMLparam1(v);
134   ml_Editor* editor = Editor_val(v);
135   CAMLreturn(Val_bool(c_mathml_editor_thaw(editor->c_editor)));
136 }
137
138 value
139 ml_mathml_editor_push(value v, value ch)
140 {
141   CAMLparam2(v, ch);
142   ml_Editor* editor = Editor_val(v);
143   c_mathml_editor_push(editor->c_editor, Int_val(ch));
144   CAMLreturn(Val_unit);
145 }
146
147 value
148 ml_mathml_editor_drop(value v, value alt)
149 {
150   CAMLparam2(v, alt);
151   ml_Editor* editor = Editor_val(v);
152   c_mathml_editor_drop(editor->c_editor, Bool_val(alt));
153   CAMLreturn(Val_unit);
154 }
155
156 value
157 ml_mathml_editor_cursor_hide(value v)
158 {
159   CAMLparam1(v);
160   ml_Editor* editor = Editor_val(v);
161   CAMLreturn(Val_bool(c_mathml_editor_cursor_hide(editor->c_editor)));
162 }
163
164 value
165 ml_mathml_editor_cursor_show(value v)
166 {
167   CAMLparam1(v);
168   ml_Editor* editor = Editor_val(v);
169   CAMLreturn(Val_bool(c_mathml_editor_cursor_show(editor->c_editor)));
170 }
171
172 value
173 ml_mathml_editor_get_tex(value v)
174 {
175   CAMLparam1(v);
176   ml_Editor* editor = Editor_val(v);
177   char* res = c_mathml_editor_get_tex(editor->c_editor);
178   CAMLlocal1(ml_res);
179   ml_res = copy_string(res);
180   free(res);
181   CAMLreturn(ml_res);
182 }
183
184 value
185 ml_mathml_editor_reset(value v, value s)
186 {
187   CAMLparam1(v);
188   ml_Editor* editor = Editor_val(v);
189   c_mathml_editor_reset(editor->c_editor);
190   CAMLreturn(Val_unit);
191 }
192
193 value
194 ml_mathml_editor_get_tml(value v)
195 {
196   CAMLparam1(v);
197   ml_Editor* editor = Editor_val(v);
198   GdomeDocument* doc = c_mathml_editor_get_tml(editor->c_editor);
199   CAMLreturn(Val_Document(doc));
200 }
201
202 value
203 ml_mathml_editor_get_mml(value v)
204 {
205   CAMLparam1(v);
206   ml_Editor* editor = Editor_val(v);
207   GdomeDocument* doc = c_mathml_editor_get_mml(editor->c_editor);
208   CAMLreturn(Val_Document(doc));
209 }
210