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