]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/lablgtksourceview/gSourceView.ml
snaphot
[helm.git] / helm / DEVEL / lablgtksourceview / gSourceView.ml
1 (* Copyright (C) 2005:
2  *      Stefano Zacchiroli     <zack@cs.unibo.it>
3  *      Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>
4  *
5  * This file is part of lablgtksourceview, the OCaml binding for the
6  * GtkSourceView widget.
7  * 
8  * lablgtksourceview is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * lablgtksourceview is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with lablgtksourceview; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  * 02111-1307, USA.
22  * 
23  * For details, send a mail to the authors.
24  *)
25
26 open Gaux
27 open Gtk_sourceview
28 open Gobject
29 open Gtk
30 open GtkBase
31 open GtkSourceView
32 open OgtkSourceViewProps
33 open GObj
34
35 let get_bool = function `BOOL x -> x | _ -> assert false
36 let bool x = `BOOL x
37 let get_uint = function `INT x -> x | _ -> assert false
38 let uint x = `INT x
39 let get_int = function `INT x -> x | _ -> assert false
40 let int x = `INT x
41 let get_gobject = function `OBJECT x -> x | _ -> assert false
42 let gobject x = `OBJECT (Some x)
43
44 (** {2 GtkSourceLanguage} *)
45
46 class source_language_signals obj' =
47 object (self)
48   inherit ['a] gobject_signals (obj' : [> Gtk_sourceview.source_language] obj)
49   inherit source_language_sigs
50 end
51
52 class source_language (obj: Gtk_sourceview.source_language obj) =
53 object (self)
54   method as_source_language = obj
55   method connect = new source_language_signals obj
56   method misc = new gobject_ops obj
57   method get_name = SourceLanguage.get_name obj
58   method get_section = SourceLanguage.get_section obj
59   method get_escape_char = SourceLanguage.get_escape_char obj
60 end
61
62 (** {2 GtkSourceBuffer} *)
63
64 class source_buffer_signals obj' =
65 object
66   inherit ['a] gobject_signals (obj' : [> Gtk_sourceview.source_buffer] obj)
67   inherit GText.buffer_signals obj'
68   inherit source_buffer_sigs
69 end
70
71 class source_buffer (obj: Gtk_sourceview.source_buffer obj) =
72 object (self)
73   inherit GText.buffer_skel obj
74   method connect = new source_buffer_signals obj
75   method misc = new gobject_ops obj
76   method check_brackets = get_bool (self#misc#get_property "check-brackets")
77   method set_check_brackets x = self#misc#set_property "check-brackets" (bool x)
78   method highlight = get_bool (self#misc#get_property "highlight")
79   method set_highlight x = self#misc#set_property "highlight" (bool x)
80   method max_undo_levels = get_int (self#misc#get_property "max-undo-levels")
81   method set_max_undo_levels x =
82     self#misc#set_property "max-undo-levels" (int x)
83   method language =
84     match get_gobject (self#misc#get_property "language") with
85     | None -> None
86     | Some obj ->
87         Some (new source_language (Gobject.try_cast obj "GtkSourceLanguage"))
88   method set_language (x: source_language) =
89     self#misc#set_property "language" (gobject x#as_source_language)
90   method escape_char = get_uint (self#misc#get_property "escape-char")
91   method set_escape_char x = self#misc#set_property "escape-char" (uint x)
92   method can_undo = SourceBuffer.can_undo obj
93   method can_redo = SourceBuffer.can_redo obj
94   method undo () = SourceBuffer.undo obj
95   method redo () = SourceBuffer.redo obj
96   method begin_not_undoable_action () =
97     SourceBuffer.begin_not_undoable_action obj
98   method end_not_undoable_action () =
99     SourceBuffer.end_not_undoable_action obj
100 end
101
102 let source_buffer ?language ?text (* ?tag_table *) =
103   let language =
104     match language with
105     | None -> None
106     | Some source_language -> Some (source_language#as_source_language)
107   in
108   SourceBuffer.make_params [] ?language ~cont:(fun pl () ->
109     let buf = new source_buffer (SourceBuffer.create pl) in
110     (match text with
111     | None -> ()
112     | Some text -> buf#set_text text);
113     buf)
114
115   (* alias used below, needed because "source_buffer" is a name in scope *)
116 let source_buffer' = source_buffer
117
118 (** {2 GtkSourceView} *)
119
120 class source_view_signals obj' =
121 object
122   inherit widget_signals_impl (obj' : [> Gtk_sourceview.source_view] obj)
123   inherit GText.view_signals obj'
124   inherit source_view_sigs
125 end
126
127 class source_view (obj': Gtk_sourceview.source_view obj) =
128 object (self)
129   inherit GText.view_skel obj'
130   val source_buf =
131     let buf_obj =
132       Gobject.try_cast (GtkText.View.get_buffer obj') "GtkSourceBuffer"
133     in
134     new source_buffer buf_obj
135   method source_buffer = source_buf
136   method connect = new source_view_signals obj'
137   method set_show_line_numbers x =
138     self#misc#set_property "show_line_numbers" (bool x)
139   method show_line_numbers =
140     get_bool (self#misc#get_property "show_line_numbers")
141   method set_show_line_markers x =
142     self#misc#set_property "show_line_markers" (bool x)
143   method show_line_markers =
144     get_bool (self#misc#get_property "show_line_markers")
145   method set_tabs_width x = self#misc#set_property "tabs_width" (uint x)
146   method tabs_width = get_uint (self#misc#get_property "tabs_width")
147   method set_auto_indent x = self#misc#set_property "auto_indent" (bool x)
148   method auto_indent = get_bool (self#misc#get_property "auto_indent")
149   method set_insert_spaces_instead_of_tabs x =
150     self#misc#set_property "insert_spaces_instead_of_tabs" (bool x)
151   method insert_spaces_instead_of_tabs =
152     get_bool (self#misc#get_property "insert_spaces_instead_of_tabs")
153   method set_show_margin x = self#misc#set_property "show_margin" (bool x)
154   method show_margin = get_bool (self#misc#get_property "show_margin")
155   method set_margin x = self#misc#set_property "margin" (uint x)
156   method margin = get_uint (self#misc#get_property "margin")
157   method set_smart_home_end x = self#misc#set_property "smart_home_end" (bool x)
158   method smart_home_end = get_bool (self#misc#get_property "smart_home_end")
159 end
160
161 let source_view ?source_buffer =
162   SourceView.make_params [] ~cont:(
163     GtkText.View.make_params ~cont:(
164       GContainer.pack_container ~create:(fun pl ->
165         let obj =
166           match source_buffer with
167           | Some buf ->
168               SourceView.new_with_buffer
169                 (Gobject.try_cast buf#as_buffer "GtkSourceBuffer")
170           | None -> SourceView.new_ ()
171         in
172         Gobject.set_params (Gobject.try_cast obj "GtkSourceView") pl;
173         new source_view obj)))
174