]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/lablgtkmathview/ml_gtk_mathview.c
Initial revision
[helm.git] / helm / DEVEL / lablgtkmathview / ml_gtk_mathview.c
1 /* Copyright (C) 2000, Luca Padovani <luca.padovani@cs.unibo.it>.
2  *
3  * This file is part of lablgtkmathview, the Ocaml binding
4  * for the GtkMathView widget.
5  * 
6  * lablgtkmathview is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * lablgtkmathview is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with lablgtkmathview; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  * 
20  * For details, send a mail to the author.
21  */
22
23 #include <assert.h>
24 #include <string.h>
25 #include <assert.h>
26 #include <gtk/gtk.h>
27 #include <caml/mlvalues.h>
28 #include <caml/alloc.h>
29 #include <caml/memory.h>
30 #include <caml/callback.h>
31 #include <caml/fail.h>
32
33 #include <gtkmathview.h>
34 #include <ml_gtk.h>
35
36 #include <wrappers.h>
37 #include <ml_glib.h>
38 #include <ml_gdk.h>
39 #include <ml_gtk.h>
40 #include <gtk_tags.h>
41
42 #include <mlgdomevalue.h>
43
44 #define GtkMathView_val(val) check_cast(GTK_MATH_VIEW,val)
45
46 #define FontManagerId_val(val) Int_val(val)
47 #define Val_FontManagerId(val) Val_int(val)
48
49 // As ML_1, but the result is optional
50 #define OML_1(cname, conv1, conv) \
51 value ml_##cname (value arg1) { return Val_option_ptr((cname (conv1 (arg1))),conv); }
52 // As ML_2, but the second argument is optional
53 #define ML_2O(cname, conv1, conv2, conv) \
54 value ml_##cname (value arg1, value arg2) \
55 { return conv (cname (conv1(arg1), ptr_val_option(arg2,conv2))); }
56
57 FontManagerId
58 font_manager_id_of_value(value v)
59 {
60   if (v == hash_variant("font_manager_gtk")) return FONT_MANAGER_GTK;
61   else if (v == hash_variant("font_manager_t1")) return FONT_MANAGER_T1;
62   else assert(0);
63 }
64
65 value
66 value_of_font_manager_id(FontManagerId id)
67 {
68   switch (id) {
69   case FONT_MANAGER_GTK:
70     return hash_variant("font_manager_gtk");
71   case FONT_MANAGER_T1:
72     return hash_variant("font_manager_t1");
73   default:
74     assert(0);
75     break;
76   }
77 }
78
79 ML_2 (gtk_math_view_new,GtkAdjustment_val, GtkAdjustment_val, Val_GtkWidget_sink)
80 ML_2 (gtk_math_view_load, GtkMathView_val, String_val, Val_bool)
81 ML_2 (gtk_math_view_load_tree, GtkMathView_val, Document_val, Val_bool)
82 ML_1 (gtk_math_view_unload, GtkMathView_val, Unit)
83 OML_1 (gtk_math_view_get_selection, GtkMathView_val, Val_Element)
84 ML_2O (gtk_math_view_set_selection, GtkMathView_val, Element_val, Unit)
85 ML_1 (gtk_math_view_get_width, GtkMathView_val, Val_int)
86 ML_1 (gtk_math_view_get_height, GtkMathView_val, Val_int)
87 ML_3 (gtk_math_view_set_top, GtkMathView_val, Int_val, Int_val, Unit)
88 ML_3 (gtk_math_view_set_adjustments, GtkMathView_val, GtkAdjustment_val, GtkAdjustment_val, Unit)
89 ML_1 (gtk_math_view_get_hadjustment, GtkMathView_val, Val_GtkWidget)
90 ML_1 (gtk_math_view_get_vadjustment, GtkMathView_val, Val_GtkWidget)
91 ML_1 (gtk_math_view_get_buffer, GtkMathView_val, Val_GdkPixmap)
92 ML_1 (gtk_math_view_get_frame, GtkMathView_val, Val_GtkWidget)
93 ML_2 (gtk_math_view_set_font_size, GtkMathView_val, Int_val, Unit)
94 ML_1 (gtk_math_view_get_font_size, GtkMathView_val, Val_int)
95 ML_2 (gtk_math_view_set_anti_aliasing, GtkMathView_val, Bool_val, Unit)
96 ML_1 (gtk_math_view_get_anti_aliasing, GtkMathView_val, Val_bool)
97 ML_2 (gtk_math_view_set_kerning, GtkMathView_val, Bool_val, Unit)
98 ML_1 (gtk_math_view_get_kerning, GtkMathView_val, Val_bool)
99 ML_2 (gtk_math_view_set_transparency, GtkMathView_val, Bool_val, Unit)
100 ML_1 (gtk_math_view_get_transparency, GtkMathView_val, Val_bool)
101 ML_2 (gtk_math_view_set_log_verbosity, GtkMathView_val, Int_val, Unit)
102 ML_1 (gtk_math_view_get_log_verbosity, GtkMathView_val, Val_int)
103 ML_2 (gtk_math_view_set_font_manager_type, GtkMathView_val, font_manager_id_of_value, Unit)
104 ML_1 (gtk_math_view_get_font_manager_type, GtkMathView_val, value_of_font_manager_id)
105 OML_1 (gtk_math_view_get_element, GtkMathView_val, Val_Element)
106 OML_1 (gtk_math_view_get_action, GtkMathView_val, Val_Element)
107 ML_1 (gtk_math_view_action_get_selected, GtkMathView_val, Val_int)
108 ML_2 (gtk_math_view_action_set_selected, GtkMathView_val, Int_val, Unit)
109 ML_1 (gtk_math_view_action_toggle, GtkMathView_val, Unit)
110
111 value
112 ml_gtk_math_view_export_to_postscript_native(value arg1,
113                 value w, value h, value x0, value y0, value disable_colors, value arg2)
114 {
115    CAMLparam5(arg1,w,h,x0,y0);
116    CAMLxparam2(disable_colors, arg2);
117
118    char *filename;
119    FILE *fd;
120    int res;
121    filename = String_val (arg2);
122    if ((fd = fopen(filename, "w"))) {
123       gtk_math_view_export_to_postscript(GtkMathView_val (arg1),
124                       Int_val(w), Int_val(h), Int_val(x0), Int_val(y0), Bool_val(disable_colors), fd);
125       fclose (fd);
126       res = 1;
127    } else {
128       fprintf(stderr, "Error opening file %s for writing\n", filename);
129       res = 0;
130    }
131    CAMLreturn (Val_bool(res));
132 }
133
134 value ml_gtk_math_view_export_to_postscript_bytecode (value* arg, int argn)
135 {
136    return ml_gtk_math_view_export_to_postscript_native(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6]);
137 }
138
139 value ml_gtk_math_view_get_top (value arg1)
140 {
141    CAMLparam1(arg1);
142    CAMLlocal1 (result);
143    int x, y;
144    gtk_math_view_get_top(GtkMathView_val (arg1), &x, &y);
145    result = alloc(2, 0);
146    Store_field(result, 0, Val_int(x));
147    Store_field(result, 0, Val_int(y));
148    CAMLreturn (result);
149 }
150
151 value ml_gtk_math_view_gdome_element_of_boxed_option (value arg1)
152 {
153    CAMLparam1(arg1);
154
155    GdomeElement* nr;
156    CAMLlocal1 (res);
157
158    if (arg1==Val_int(0)) {
159       assert(0);
160    } else {
161       nr = (GdomeElement*) Field(Field(arg1,0), 1);
162    }
163    res = Val_Element(nr);
164    if (res==Val_int(0)) {
165       assert(0);
166    }
167
168    CAMLreturn(res);
169 }
170
171 value ml_gtk_math_view_gdome_element_option_of_boxed_option (value arg1)
172 {
173    CAMLparam1(arg1);
174
175    GdomeElement* nr;
176    CAMLlocal1 (valnr);
177    CAMLlocal1 (res);
178
179    if (arg1==Val_int(0)) {
180       res=Val_unit;
181    } else {
182       res = Val_option_ptr((GdomeElement*) Field(Field(arg1,0),1),Val_Element);
183    }
184
185    CAMLreturn(res);
186 }