]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/ocaml/ml_mathml_editor.c
* this is a large commit
[helm.git] / helm / DEVEL / mathml_editor / ocaml / ml_mathml_editor.c
1
2 #include <caml/memory.h>
3 #include <caml/custom.h>
4 #include <caml/callback.h>
5
6 #include "mlgdomevalue.h"
7
8 #include "c_mathml_editor.h"
9
10 typedef struct
11 {
12   Editor* c_editor;
13   value   callback;
14 } ml_Editor;
15
16 ml_Editor*
17 Editor_val(value v)
18 {
19   ml_Editor* editor = (ml_Editor*) Data_custom_val(v);
20   assert(editor != NULL);
21   return editor;
22 }
23
24 static void
25 ml_mathml_editor_finalize(value v)
26 {
27   ml_Editor* editor = Editor_val(v);
28   assert(editor);
29
30   remove_global_root(&editor->callback);
31   c_mathml_editor_destroy(editor->c_editor);
32 }
33
34 void
35 ml_mathml_editor_log_callback(int level, const char* msg, void* user_data)
36 {
37   ml_Editor* ml_editor = (ml_Editor*) user_data;
38   assert(ml_editor);
39   callback2(ml_editor->callback, Val_int(level), copy_string(msg));
40 }
41
42 value
43 ml_mathml_editor_new(value dictionary,
44                      value tml_mml,
45                      value tml_tex,
46                      value log_message_cb)
47 {
48   static struct custom_operations ops =
49   {
50     "HELM/MathML Editor",
51     ml_mathml_editor_finalize,
52     custom_compare_default,
53     custom_hash_default,
54     custom_serialize_default,
55     custom_deserialize_default
56   };
57   
58   value v = alloc_custom(&ops, sizeof(ml_Editor), 0, 1);
59   ml_Editor* ml_editor = (ml_Editor*) Data_custom_val(v);
60   ml_editor->c_editor = c_mathml_editor_new(Document_val(dictionary),
61                                             Document_val(tml_mml),
62                                             Document_val(tml_tex),
63                                             ml_mathml_editor_log_callback,
64                                             (void*) ml_editor);
65   ml_editor->callback = log_message_cb;
66   register_global_root(&ml_editor->callback);
67
68   return v;
69 }
70
71 value
72 ml_mathml_editor_freeze(value v)
73 {
74   CAMLparam1(v);
75   ml_Editor* editor = Editor_val(v);
76   CAMLreturn(Val_bool(c_mathml_editor_freeze(editor->c_editor)));
77 }
78
79 value
80 ml_mathml_editor_thaw(value v)
81 {
82   CAMLparam1(v);
83   ml_Editor* editor = Editor_val(v);
84   CAMLreturn(Val_bool(c_mathml_editor_thaw(editor->c_editor)));
85 }
86
87 value
88 ml_mathml_editor_push(value v, value ch)
89 {
90   CAMLparam2(v, ch);
91   ml_Editor* editor = Editor_val(v);
92   c_mathml_editor_push(editor->c_editor, Int_val(ch));
93   CAMLreturn(Val_unit);
94 }
95
96 value
97 ml_mathml_editor_drop(value v, value alt)
98 {
99   CAMLparam2(v, alt);
100   ml_Editor* editor = Editor_val(v);
101   c_mathml_editor_drop(editor->c_editor, Bool_val(alt));
102   CAMLreturn(Val_unit);
103 }
104
105 value
106 ml_mathml_editor_get_tex(value v)
107 {
108   CAMLparam1(v);
109   ml_Editor* editor = Editor_val(v);
110   char* res = c_mathml_editor_get_tex(editor->c_editor);
111   CAMLlocal1(ml_res);
112   ml_res = copy_string(res);
113   free(res);
114   CAMLreturn(ml_res);
115 }
116
117 value
118 ml_mathml_editor_reset(value v, value s)
119 {
120   CAMLparam1(v);
121   ml_Editor* editor = Editor_val(v);
122   c_mathml_editor_reset(editor->c_editor);
123   CAMLreturn(Val_unit);
124 }
125
126 value
127 ml_mathml_editor_get_tml(value v)
128 {
129   CAMLparam1(v);
130   ml_Editor* editor = Editor_val(v);
131   GdomeDocument* doc = c_mathml_editor_get_tml(editor->c_editor);
132   CAMLreturn(Val_Document(doc));
133 }
134
135 value
136 ml_mathml_editor_get_mml(value v)
137 {
138   CAMLparam1(v);
139   ml_Editor* editor = Editor_val(v);
140   GdomeDocument* doc = c_mathml_editor_get_mml(editor->c_editor);
141   CAMLreturn(Val_Document(doc));
142 }
143