2 * lablgtksourceview, OCaml binding for the GtkSourceView text widget
4 * Copyright (C) 2005 Stefano Zacchiroli <zack@cs.unibo.it>
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 open OgtkSourceViewProps
31 let get_bool = function `BOOL x -> x | _ -> assert false
33 let get_uint = function `INT x -> x | _ -> assert false
35 let get_int = function `INT x -> x | _ -> assert false
37 let get_gobject = function `OBJECT x -> x | _ -> assert false
38 let gobject x = `OBJECT (Some x)
40 (** {2 GtkSourceLanguage} *)
42 class source_language_signals obj' =
44 inherit ['a] gobject_signals (obj' : [> Gtk_sourceview.source_language] obj)
45 inherit source_language_sigs
48 class source_language (obj: Gtk_sourceview.source_language obj) =
50 method as_source_language = obj
51 method connect = new source_language_signals obj
52 method misc = new gobject_ops obj
53 method get_name = SourceLanguage.get_name obj
54 method get_section = SourceLanguage.get_section obj
55 method get_escape_char = SourceLanguage.get_escape_char obj
58 (** {2 GtkSourceLanguagesManager} *)
60 class source_languages_manager
61 (obj: Gtk_sourceview.source_languages_manager obj) =
63 method get_oid = Gobject.get_oid obj
64 method as_source_languages_manager = obj
65 method get_language_from_mime_type s =
66 match SourceLanguagesManager.get_language_from_mime_type obj s with
68 | Some obj -> Some (new source_language obj)
69 method lang_files_dirs = SourceLanguagesManager.get_lang_files_dirs obj
72 (* let source_languages_manager ?lang_files_dirs () =
74 match lang_files_dirs with
77 let list_obj = gslist_of_string_list dirs in
80 (`OBJECT (Some list_obj))]
82 new source_languages_manager (SourceLanguagesManager.create properties) *)
84 let source_languages_manager () =
85 new source_languages_manager (SourceLanguagesManager.create [])
87 let source_language_from_file ?languages_manager fname =
88 let languages_manager =
89 match languages_manager with
90 | None -> source_languages_manager ()
93 let manager_obj = languages_manager#as_source_languages_manager in
94 match SourceLanguage.new_from_file fname manager_obj with
96 | Some lang_obj -> Some (new source_language lang_obj)
98 (** {2 GtkSourceBuffer} *)
100 class source_buffer_signals obj' =
102 inherit ['a] gobject_signals (obj' : [> Gtk_sourceview.source_buffer] obj)
103 inherit GText.buffer_signals obj'
104 inherit source_buffer_sigs
107 class source_buffer (obj: Gtk_sourceview.source_buffer obj) =
109 inherit GText.buffer_skel obj
110 method connect = new source_buffer_signals obj
111 method misc = new gobject_ops obj
112 method check_brackets = get_bool (self#misc#get_property "check-brackets")
113 method set_check_brackets x = self#misc#set_property "check-brackets" (bool x)
114 method highlight = get_bool (self#misc#get_property "highlight")
115 method set_highlight x = self#misc#set_property "highlight" (bool x)
116 method max_undo_levels = get_int (self#misc#get_property "max-undo-levels")
117 method set_max_undo_levels x =
118 self#misc#set_property "max-undo-levels" (int x)
120 match get_gobject (self#misc#get_property "language") with
123 Some (new source_language (Gobject.try_cast obj "GtkSourceLanguage"))
124 method set_language (x: source_language) =
125 self#misc#set_property "language" (gobject x#as_source_language)
126 method escape_char = get_uint (self#misc#get_property "escape-char")
127 method set_escape_char x = self#misc#set_property "escape-char" (uint x)
128 method can_undo = SourceBuffer.can_undo obj
129 method can_redo = SourceBuffer.can_redo obj
130 method undo () = SourceBuffer.undo obj
131 method redo () = SourceBuffer.redo obj
132 method begin_not_undoable_action () =
133 SourceBuffer.begin_not_undoable_action obj
134 method end_not_undoable_action () =
135 SourceBuffer.end_not_undoable_action obj
138 let source_buffer ?language ?text (* ?tag_table *) =
142 | Some source_language -> Some (source_language#as_source_language)
144 SourceBuffer.make_params [] ?language ~cont:(fun pl () ->
145 let buf = new source_buffer (SourceBuffer.create pl) in
148 | Some text -> buf#set_text text);
151 (* alias used below, needed because "source_buffer" is a name in scope *)
152 let source_buffer' = source_buffer
154 (** {2 GtkSourceView} *)
156 class source_view_signals obj' =
158 inherit widget_signals_impl (obj' : [> Gtk_sourceview.source_view] obj)
159 inherit GText.view_signals obj'
160 inherit source_view_sigs
163 class source_view (obj': Gtk_sourceview.source_view obj) =
165 inherit GText.view_skel obj'
168 Gobject.try_cast (GtkText.View.get_buffer obj') "GtkSourceBuffer"
170 new source_buffer buf_obj
171 method source_buffer = source_buf
172 method connect = new source_view_signals obj'
173 method set_show_line_numbers x =
174 self#misc#set_property "show_line_numbers" (bool x)
175 method show_line_numbers =
176 get_bool (self#misc#get_property "show_line_numbers")
177 method set_show_line_markers x =
178 self#misc#set_property "show_line_markers" (bool x)
179 method show_line_markers =
180 get_bool (self#misc#get_property "show_line_markers")
181 method set_tabs_width x = self#misc#set_property "tabs_width" (uint x)
182 method tabs_width = get_uint (self#misc#get_property "tabs_width")
183 method set_auto_indent x = self#misc#set_property "auto_indent" (bool x)
184 method auto_indent = get_bool (self#misc#get_property "auto_indent")
185 method set_insert_spaces_instead_of_tabs x =
186 self#misc#set_property "insert_spaces_instead_of_tabs" (bool x)
187 method insert_spaces_instead_of_tabs =
188 get_bool (self#misc#get_property "insert_spaces_instead_of_tabs")
189 method set_show_margin x = self#misc#set_property "show_margin" (bool x)
190 method show_margin = get_bool (self#misc#get_property "show_margin")
191 method set_margin x = self#misc#set_property "margin" (uint x)
192 method margin = get_uint (self#misc#get_property "margin")
193 method set_smart_home_end x = self#misc#set_property "smart_home_end" (bool x)
194 method smart_home_end = get_bool (self#misc#get_property "smart_home_end")
197 let source_view ?source_buffer =
198 SourceView.make_params [] ~cont:(
199 GtkText.View.make_params ~cont:(
200 GContainer.pack_container ~create:(fun pl ->
202 match source_buffer with
204 SourceView.new_with_buffer
205 (Gobject.try_cast buf#as_buffer "GtkSourceBuffer")
206 | None -> SourceView.new_ ()
208 Gobject.set_params (Gobject.try_cast obj "GtkSourceView") pl;
209 new source_view obj)))
213 let find_matching_bracket iter =
214 let iter = iter#copy in
215 if SourceViewMisc.find_matching_bracket iter#as_iter then