]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/ocaml/ml_mathml_editor.c
* added show/hide cursro methods
[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_cursor_hide(value v)
107 {
108   CAMLparam1(v);
109   ml_Editor* editor = Editor_val(v);
110   CAMLreturn(Val_bool(c_mathml_editor_cursor_hide(editor->c_editor)));
111 }
112
113 value
114 ml_mathml_editor_cursor_show(value v)
115 {
116   CAMLparam1(v);
117   ml_Editor* editor = Editor_val(v);
118   CAMLreturn(Val_bool(c_mathml_editor_cursor_show(editor->c_editor)));
119 }
120
121 value
122 ml_mathml_editor_get_tex(value v)
123 {
124   CAMLparam1(v);
125   ml_Editor* editor = Editor_val(v);
126   char* res = c_mathml_editor_get_tex(editor->c_editor);
127   CAMLlocal1(ml_res);
128   ml_res = copy_string(res);
129   free(res);
130   CAMLreturn(ml_res);
131 }
132
133 value
134 ml_mathml_editor_reset(value v, value s)
135 {
136   CAMLparam1(v);
137   ml_Editor* editor = Editor_val(v);
138   c_mathml_editor_reset(editor->c_editor);
139   CAMLreturn(Val_unit);
140 }
141
142 value
143 ml_mathml_editor_get_tml(value v)
144 {
145   CAMLparam1(v);
146   ml_Editor* editor = Editor_val(v);
147   GdomeDocument* doc = c_mathml_editor_get_tml(editor->c_editor);
148   CAMLreturn(Val_Document(doc));
149 }
150
151 value
152 ml_mathml_editor_get_mml(value v)
153 {
154   CAMLparam1(v);
155   ml_Editor* editor = Editor_val(v);
156   GdomeDocument* doc = c_mathml_editor_get_mml(editor->c_editor);
157   CAMLreturn(Val_Document(doc));
158 }
159