]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/lablgtkmathview/ml_gtk_mathview.c
* some small bug fixes
[helm.git] / helm / DEVEL / lablgtkmathview / ml_gtk_mathview.c
1 /* Copyright (C) 2000-2003, Luca Padovani <luca.padovani@cs.unibo.it>,
2  *                          Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>.
3  *
4  * This file is part of lablgtkmathview, the Ocaml binding
5  * for the GtkMathView widget.
6  * 
7  * lablgtkmathview is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * lablgtkmathview 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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with lablgtkmathview; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  * 
21  * For details, send a mail to the author.
22  */
23
24 #include <assert.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <gtk/gtk.h>
28 #include <caml/mlvalues.h>
29 #include <caml/alloc.h>
30 #include <caml/memory.h>
31 #include <caml/callback.h>
32 #include <caml/fail.h>
33
34 #include <gtkmathview/gtkmathview.h>
35 #include <ml_gtk.h>
36
37 #include <wrappers.h>
38 #include <ml_glib.h>
39 #include <ml_gdk.h>
40 #include <ml_gtk.h>
41 #include <gtk_tags.h>
42
43 #include <mlgdomevalue.h>
44
45 #define GtkMathView_val(val) check_cast(GTK_MATH_VIEW,val)
46
47 #define FontManagerId_val(val) Int_val(val)
48 #define Val_FontManagerId(val) Val_int(val)
49
50 /* As ML_1, but the result is optional */
51 #define OML_1(cname, conv1, conv) \
52 value ml_##cname (value arg1) { return Val_option_ptr((cname (conv1 (arg1))),conv); }
53 /* As ML_3, but the result is optional */
54 #define OML_3(cname, conv1, conv2, conv3, conv) \
55 value ml_##cname (value arg1, value arg2, value arg3) { return Val_option_ptr((cname (conv1 (arg1), conv2 (arg2), conv3 (arg3))),conv); }
56 /* As ML_2, but the second argument is optional */
57 #define ML_2O(cname, conv1, conv2, conv) \
58 value ml_##cname (value arg1, value arg2) \
59 { return conv (cname (conv1(arg1), ptr_val_option(arg2,conv2))); }
60
61 FontManagerId
62 font_manager_id_of_value(value v)
63 {
64   if (v == hash_variant("font_manager_gtk")) return FONT_MANAGER_GTK;
65   else if (v == hash_variant("font_manager_t1")) return FONT_MANAGER_T1;
66   else assert(0);
67 }
68
69 value
70 value_of_font_manager_id(FontManagerId id)
71 {
72   switch (id) {
73   case FONT_MANAGER_GTK:
74     return hash_variant("font_manager_gtk");
75   case FONT_MANAGER_T1:
76     return hash_variant("font_manager_t1");
77   default:
78     assert(0);
79     break;
80   }
81 }
82
83 value Val_Element_ref(GdomeElement* elem)
84 {
85   if (elem != NULL)
86     {
87       GdomeException exc = 0;
88       gdome_el_ref(elem, &exc);
89       g_assert(exc == 0);
90     }
91   return Val_Element(elem);
92 }
93
94 ML_2 (gtk_math_view_new,GtkAdjustment_val, GtkAdjustment_val, Val_GtkWidget_sink)
95 ML_1 (gtk_math_view_freeze, GtkMathView_val, Unit)
96 ML_1 (gtk_math_view_thaw, GtkMathView_val, Unit)
97 ML_2 (gtk_math_view_load_uri, GtkMathView_val, String_val, Val_bool)
98 ML_2 (gtk_math_view_load_doc, GtkMathView_val, Document_val, Val_bool)
99 ML_1 (gtk_math_view_unload, GtkMathView_val, Unit)
100 ML_2 (gtk_math_view_select, GtkMathView_val, Element_val, Unit)
101 ML_2 (gtk_math_view_unselect, GtkMathView_val, Element_val, Unit)
102 ML_2 (gtk_math_view_is_selected, GtkMathView_val, Element_val, Val_bool)
103 OML_3 (gtk_math_view_get_element_at, GtkMathView_val, Int_val, Int_val, Val_Element_ref)
104 ML_1 (gtk_math_view_get_width, GtkMathView_val, Val_int)
105 ML_1 (gtk_math_view_get_height, GtkMathView_val, Val_int)
106 ML_3 (gtk_math_view_set_top, GtkMathView_val, Int_val, Int_val, Unit)
107 ML_3 (gtk_math_view_set_adjustments, GtkMathView_val, GtkAdjustment_val, GtkAdjustment_val, Unit)
108 ML_1 (gtk_math_view_get_hadjustment, GtkMathView_val, Val_GtkWidget)
109 ML_1 (gtk_math_view_get_vadjustment, GtkMathView_val, Val_GtkWidget)
110 ML_1 (gtk_math_view_get_buffer, GtkMathView_val, Val_GdkPixmap)
111 ML_1 (gtk_math_view_get_frame, GtkMathView_val, Val_GtkWidget)
112 ML_2 (gtk_math_view_set_font_size, GtkMathView_val, Int_val, Unit)
113 ML_1 (gtk_math_view_get_font_size, GtkMathView_val, Val_int)
114 ML_2 (gtk_math_view_set_anti_aliasing, GtkMathView_val, Bool_val, Unit)
115 ML_1 (gtk_math_view_get_anti_aliasing, GtkMathView_val, Val_bool)
116 ML_2 (gtk_math_view_set_transparency, GtkMathView_val, Bool_val, Unit)
117 ML_1 (gtk_math_view_get_transparency, GtkMathView_val, Val_bool)
118 ML_2 (gtk_math_view_set_log_verbosity, GtkMathView_val, Int_val, Unit)
119 ML_1 (gtk_math_view_get_log_verbosity, GtkMathView_val, Val_int)
120 ML_2 (gtk_math_view_set_font_manager_type, GtkMathView_val, font_manager_id_of_value, Unit)
121 ML_1 (gtk_math_view_get_font_manager_type, GtkMathView_val, value_of_font_manager_id)
122
123 value
124 ml_gtk_math_view_export_to_postscript_native(value arg1,
125                 value w, value h, value x0, value y0, value disable_colors, value arg2)
126 {
127    CAMLparam5(arg1,w,h,x0,y0);
128    CAMLxparam2(disable_colors, arg2);
129
130    char *filename;
131    FILE *fd;
132    int res;
133    filename = String_val (arg2);
134    if ((fd = fopen(filename, "w"))) {
135       gtk_math_view_export_to_postscript(GtkMathView_val (arg1),
136                       Int_val(w), Int_val(h), Int_val(x0), Int_val(y0), Bool_val(disable_colors), fd);
137       fclose (fd);
138       res = 1;
139    } else {
140       fprintf(stderr, "Error opening file %s for writing\n", filename);
141       res = 0;
142    }
143    CAMLreturn (Val_bool(res));
144 }
145
146 value ml_gtk_math_view_export_to_postscript_bytecode (value* arg, int argn)
147 {
148    return ml_gtk_math_view_export_to_postscript_native(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6]);
149 }
150
151 value ml_gtk_math_view_get_top (value arg1)
152 {
153    CAMLparam1(arg1);
154    CAMLlocal1 (result);
155    int x, y;
156    gtk_math_view_get_top(GtkMathView_val (arg1), &x, &y);
157    result = alloc(2, 0);
158    Store_field(result, 0, Val_int(x));
159    Store_field(result, 1, Val_int(y));
160    CAMLreturn (result);
161 }
162
163 /*
164 value ml_gtk_math_view_get_element_coords (value arg1, value arg2)
165 {
166   CAMLparam2(arg1, arg2);
167   CAMLlocal1 (result);
168   int x, y;
169   gtk_math_view_get_element_coords(GtkMathView_val (arg1), Element_val(arg2), &x, &y);
170   result = alloc(2, 0);
171   Store_field(result, 0, Val_int(x));
172   Store_field(result, 1, Val_int(y));
173   CAMLreturn (result);
174 }
175 */
176
177 value ml_gtk_math_view_gdome_element_of_boxed_option (value arg1)
178 {
179    CAMLparam1(arg1);
180
181    GdomeException exc = 0;
182    GdomeElement* nr = NULL;
183    CAMLlocal1 (res);
184
185    if (arg1==Val_int(0)) {
186       assert(0);
187    } else {
188       nr = (GdomeElement*) Field(Field(arg1,0),1);
189    }
190
191    if (nr != NULL) {
192      gdome_el_ref(nr, &exc);
193      assert(exc == 0);
194    }
195    
196    res = Val_Element(nr);
197    if (res==Val_int(0)) {
198       assert(0);
199    }
200
201    CAMLreturn(res);
202 }
203
204 value ml_gtk_math_view_gdome_element_option_of_boxed_option (value arg1)
205 {
206    CAMLparam1(arg1);
207
208    GdomeElement* nr;
209    CAMLlocal1 (valnr);
210    CAMLlocal1 (res);
211
212    if (arg1==Val_int(0)) {
213       res=Val_unit;
214    } else {
215       GdomeException exc = 0;
216       GdomeElement* elem = (GdomeElement*) Field(Field(arg1,0),1);
217       assert(elem != NULL);
218       gdome_el_ref(elem, &exc);
219       assert(exc == 0);
220       res = Val_option_ptr(elem, Val_Element);
221    }
222
223    CAMLreturn(res);
224 }
225