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