1 /***************************************************************************
4 * So Mai 21 14:44:13 2006
5 * Copyright 2006 Johannes Schmid
7 ***************************************************************************/
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "sourceview-cell.h"
27 #include <libanjuta/interfaces/ianjuta-editor-cell.h>
28 #include <libanjuta/interfaces/ianjuta-editor-cell-style.h>
29 #include <libanjuta/interfaces/ianjuta-iterable.h>
30 #include <libanjuta/anjuta-utils.h>
31 #include <libanjuta/anjuta-debug.h>
33 #include <gtk/gtktextview.h>
36 static void sourceview_cell_class_init(SourceviewCellClass
*klass
);
37 static void sourceview_cell_instance_init(SourceviewCell
*sp
);
38 static void sourceview_cell_finalize(GObject
*object
);
40 struct _SourceviewCellPrivate
{
43 GtkTextBuffer
* buffer
;
46 static gpointer sourceview_cell_parent_class
= NULL
;
49 sourceview_cell_class_init(SourceviewCellClass
*klass
)
51 GObjectClass
*object_class
= G_OBJECT_CLASS(klass
);
52 sourceview_cell_parent_class
= g_type_class_peek_parent(klass
);
55 object_class
->finalize
= sourceview_cell_finalize
;
59 sourceview_cell_instance_init(SourceviewCell
*obj
)
61 obj
->priv
= g_slice_new(SourceviewCellPrivate
);
63 /* Initialize private members, etc. */
67 sourceview_cell_finalize(GObject
*object
)
70 cobj
= SOURCEVIEW_CELL(object
);
72 gtk_text_iter_free(cobj
->priv
->iter
);
74 g_slice_free(SourceviewCellPrivate
, cobj
->priv
);
75 G_OBJECT_CLASS(sourceview_cell_parent_class
)->finalize(object
);
79 sourceview_cell_new(GtkTextIter
* iter
, GtkTextView
* view
)
83 obj
= SOURCEVIEW_CELL(g_object_new(SOURCEVIEW_TYPE_CELL
, NULL
));
85 obj
->priv
->buffer
= gtk_text_view_get_buffer(view
);
86 obj
->priv
->iter
= gtk_text_iter_copy (iter
);
87 obj
->priv
->view
= view
;
93 sourceview_cell_get_iter (SourceviewCell
* cell
)
95 return cell
->priv
->iter
;
99 icell_get_character(IAnjutaEditorCell
* icell
, GError
** e
)
101 SourceviewCell
* cell
= SOURCEVIEW_CELL(icell
);
104 length
= g_unichar_to_utf8(gtk_text_iter_get_char(cell
->priv
->iter
), outbuf
);
105 outbuf
[length
] = '\0';
106 return g_strdup(outbuf
);
110 icell_get_length(IAnjutaEditorCell
* icell
, GError
** e
)
112 SourceviewCell
* cell
= SOURCEVIEW_CELL(icell
);
113 return g_unichar_to_utf8(gtk_text_iter_get_char(cell
->priv
->iter
), NULL
);
117 icell_get_char(IAnjutaEditorCell
* icell
, gint index
, GError
** e
)
120 gchar
* utf8
= icell_get_character(icell
, NULL
);
121 if (strlen (utf8
) > index
)
127 static IAnjutaEditorAttribute
128 icell_get_attribute (IAnjutaEditorCell
* icell
, GError
**e
)
130 SourceviewCell
* cell
= SOURCEVIEW_CELL(icell
);
131 IAnjutaEditorAttribute attrib
= IANJUTA_EDITOR_TEXT
;
136 icell_iface_init(IAnjutaEditorCellIface
* iface
)
138 iface
->get_character
= icell_get_character
;
139 iface
->get_char
= icell_get_char
;
140 iface
->get_length
= icell_get_length
;
141 iface
->get_attribute
= icell_get_attribute
;
145 GtkTextAttributes
* get_attributes(GtkTextIter
* iter
, GtkTextView
* view
)
147 GtkTextAttributes
* atts
= gtk_text_view_get_default_attributes(view
);
148 gtk_text_iter_get_attributes(iter
, atts
);
153 icell_style_get_font_description(IAnjutaEditorCellStyle
* icell_style
, GError
** e
)
156 SourceviewCell
* cell
= SOURCEVIEW_CELL(icell_style
);
157 GtkTextAttributes
* atts
= get_attributes(cell
->priv
->iter
,
159 font
= pango_font_description_to_string(atts
->font
);
161 return g_strdup(font
);
165 icell_style_get_color(IAnjutaEditorCellStyle
* icell_style
, GError
** e
)
168 SourceviewCell
* cell
= SOURCEVIEW_CELL(icell_style
);
169 GtkTextAttributes
* atts
= get_attributes(cell
->priv
->iter
, cell
->priv
->view
);
170 color
= anjuta_util_string_from_color(atts
->appearance
.fg_color
.red
,
171 atts
->appearance
.fg_color
.green
, atts
->appearance
.fg_color
.blue
);
177 icell_style_get_background_color(IAnjutaEditorCellStyle
* icell_style
, GError
** e
)
180 SourceviewCell
* cell
= SOURCEVIEW_CELL(icell_style
);
181 GtkTextAttributes
* atts
= get_attributes(cell
->priv
->iter
, cell
->priv
->view
);
182 color
= anjuta_util_string_from_color(atts
->appearance
.bg_color
.red
,
183 atts
->appearance
.bg_color
.green
, atts
->appearance
.bg_color
.blue
);
189 icell_style_iface_init(IAnjutaEditorCellStyleIface
* iface
)
191 iface
->get_font_description
= icell_style_get_font_description
;
192 iface
->get_color
= icell_style_get_color
;
193 iface
->get_background_color
= icell_style_get_background_color
;
197 iiter_first(IAnjutaIterable
* iter
, GError
** e
)
199 SourceviewCell
* cell
= SOURCEVIEW_CELL(iter
);
200 gtk_text_iter_set_offset(cell
->priv
->iter
, 0);
205 iiter_next(IAnjutaIterable
* iter
, GError
** e
)
207 SourceviewCell
* cell
= SOURCEVIEW_CELL(iter
);
209 return gtk_text_iter_forward_char(cell
->priv
->iter
);
213 iiter_previous(IAnjutaIterable
* iter
, GError
** e
)
215 SourceviewCell
* cell
= SOURCEVIEW_CELL(iter
);
217 return gtk_text_iter_backward_char(cell
->priv
->iter
);
221 iiter_last(IAnjutaIterable
* iter
, GError
** e
)
223 SourceviewCell
* cell
= SOURCEVIEW_CELL(iter
);
225 gtk_text_iter_forward_to_end(cell
->priv
->iter
);
230 iiter_foreach(IAnjutaIterable
* iter
, GFunc callback
, gpointer data
, GError
** e
)
232 SourceviewCell
* cell
= SOURCEVIEW_CELL(iter
);
236 saved_offset
= gtk_text_iter_get_offset (cell
->priv
->iter
);
237 gtk_text_iter_set_offset(cell
->priv
->iter
, 0);
238 while (gtk_text_iter_forward_char(cell
->priv
->iter
))
240 (*callback
)(cell
, data
);
242 gtk_text_iter_set_offset(cell
->priv
->iter
, saved_offset
);
246 iiter_set_position (IAnjutaIterable
* iter
, gint position
, GError
** e
)
248 SourceviewCell
* cell
= SOURCEVIEW_CELL(iter
);
250 gtk_text_iter_set_offset (cell
->priv
->iter
, position
);
255 iiter_get_position(IAnjutaIterable
* iter
, GError
** e
)
257 SourceviewCell
* cell
= SOURCEVIEW_CELL(iter
);
258 return gtk_text_iter_get_offset(cell
->priv
->iter
);
262 iiter_get_length(IAnjutaIterable
* iter
, GError
** e
)
264 SourceviewCell
* cell
= SOURCEVIEW_CELL(iter
);
266 return gtk_text_buffer_get_char_count (gtk_text_iter_get_buffer(cell
->priv
->iter
));
269 static IAnjutaIterable
*
270 iiter_clone (IAnjutaIterable
*iter
, GError
**e
)
272 SourceviewCell
* cell
= SOURCEVIEW_CELL(iter
);
274 return IANJUTA_ITERABLE (sourceview_cell_new (cell
->priv
->iter
, cell
->priv
->view
));
278 iiter_assign (IAnjutaIterable
*iter
, IAnjutaIterable
*src_iter
, GError
**e
)
280 SourceviewCell
* cell
= SOURCEVIEW_CELL(iter
);
281 SourceviewCell
* src_cell
= SOURCEVIEW_CELL(src_iter
);
283 gtk_text_iter_free(cell
->priv
->iter
);
284 cell
->priv
->iter
= gtk_text_iter_copy (src_cell
->priv
->iter
);
288 iiter_compare (IAnjutaIterable
*iter
, IAnjutaIterable
*other_iter
, GError
**e
)
290 SourceviewCell
* cell
= SOURCEVIEW_CELL(iter
);
291 SourceviewCell
* other_cell
= SOURCEVIEW_CELL(other_iter
);
293 return gtk_text_iter_compare (cell
->priv
->iter
, other_cell
->priv
->iter
);
297 iiter_diff (IAnjutaIterable
*iter
, IAnjutaIterable
*other_iter
, GError
**e
)
299 SourceviewCell
* cell
= SOURCEVIEW_CELL(iter
);
300 SourceviewCell
* other_cell
= SOURCEVIEW_CELL(other_iter
);
301 return (gtk_text_iter_get_offset (other_cell
->priv
->iter
)
302 - gtk_text_iter_get_offset (cell
->priv
->iter
));
306 iiter_iface_init(IAnjutaIterableIface
* iface
)
308 iface
->first
= iiter_first
;
309 iface
->next
= iiter_next
;
310 iface
->previous
= iiter_previous
;
311 iface
->last
= iiter_last
;
312 iface
->foreach
= iiter_foreach
;
313 iface
->set_position
= iiter_set_position
;
314 iface
->get_position
= iiter_get_position
;
315 iface
->get_length
= iiter_get_length
;
316 iface
->assign
= iiter_assign
;
317 iface
->clone
= iiter_clone
;
318 iface
->diff
= iiter_diff
;
319 iface
->compare
= iiter_compare
;
323 ANJUTA_TYPE_BEGIN(SourceviewCell
, sourceview_cell
, G_TYPE_OBJECT
);
324 ANJUTA_TYPE_ADD_INTERFACE(icell
, IANJUTA_TYPE_EDITOR_CELL
);
325 ANJUTA_TYPE_ADD_INTERFACE(icell_style
, IANJUTA_TYPE_EDITOR_CELL_STYLE
);
326 ANJUTA_TYPE_ADD_INTERFACE(iiter
, IANJUTA_TYPE_ITERABLE
);