2
/* $Id: textbuffer.ccg,v 1.8 2006/11/20 09:19:49 murrayc Exp $ */
4
/* Copyright(C) 1998-2002 The gtkmm Development Team
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
11
* This library 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 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
18
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
#include <glibmm/vectorutils.h>
25
#include <gtkmm/textmark.h>
26
#include <gtkmm/textiter.h>
30
static guint8* SignalProxy_Serialize(GtkTextBuffer* , GtkTextBuffer* content_buffer,
31
const GtkTextIter* start, const GtkTextIter* end,
35
Gtk::TextBuffer::SlotSerialize* the_slot = static_cast<Gtk::TextBuffer::SlotSerialize*>(user_data);
39
return (*the_slot)(Glib::wrap(content_buffer), Glib::wrap(start), Glib::wrap(end), *length);
43
Glib::exception_handlers_invoke();
46
return 0; // arbitrary value
49
static void SignalProxy_Serialize_gtk_callback_destroy(void* user_data)
51
delete static_cast<Gtk::TextBuffer::SlotSerialize*>(user_data);
54
static gboolean SignalProxy_Deserialize(GtkTextBuffer* , GtkTextBuffer* content_buffer,
55
GtkTextIter* iter, const guint8* data,
56
gsize length, gboolean create_tags,
60
Gtk::TextBuffer::SlotDeserialize* the_slot = static_cast<Gtk::TextBuffer::SlotDeserialize*>(user_data);
64
(*the_slot)(Glib::wrap(content_buffer, true), Glib::wrap(iter), data, length, create_tags);
69
//TODO: Fill the error parameter.
70
Glib::exception_handlers_invoke();
73
return 0; // arbitrary value
76
static void SignalProxy_Deserialize_gtk_callback_destroy(void* user_data)
78
delete static_cast<Gtk::TextBuffer::SlotDeserialize*>(user_data);
85
typedef TextChildAnchor ChildAnchor; //Help the code-generator so that it does not have to fully qualify this type in the .cc file.
87
TextBuffer::TextBuffer(const Glib::RefPtr<TagTable>& tag_table)
89
_CONSTRUCT("tag_table", Glib::unwrap(tag_table))
92
Glib::RefPtr<TextBuffer::Tag> TextBuffer::create_tag(const Glib::ustring& tag_name)
94
//gtk_text_buffer_create_tag takes a varargs list of property names and values.
95
//gtkmm coders should use the Tag.set_* method instead.
96
return Glib::wrap(gtk_text_buffer_create_tag(gobj(), tag_name.c_str(), (char*)0), true); //true = take_copy.
97
//We have to take a copy because gtk_text_buffer_create_tag() doesn't ref for us, for no real reason.
100
Glib::RefPtr<TextBuffer::Tag> TextBuffer::create_tag()
102
//gtk_text_buffer_create_tag takes a varargs list of property names and values.
103
//gtkmm coders should use the Tag.set_* method instead.
104
return Glib::wrap(gtk_text_buffer_create_tag(gobj(), (const char*)0, (char*)0), true); //true = take_copy.
105
//We have to take a copy because gtk_text_buffer_create_tag() doesn't ref for us, for no real reason.
108
Glib::RefPtr<TextBuffer::Mark>
109
TextBuffer::create_mark(const TextBuffer::iterator& where, bool left_gravity)
111
return Glib::wrap(gtk_text_buffer_create_mark(
112
gobj(), 0, const_cast<GtkTextIter*>(where.gobj()), left_gravity),
113
true); // acquire reference
116
TextBuffer::iterator TextBuffer::get_iter_at_line_offset(int line_number, int char_offset)
119
gtk_text_buffer_get_iter_at_line_offset(gobj(), iter.gobj(), line_number, char_offset);
123
TextBuffer::iterator TextBuffer::get_iter_at_line_index(int line_number, int byte_index)
126
gtk_text_buffer_get_iter_at_line_index(gobj(), iter.gobj(), line_number, byte_index);
130
TextBuffer::iterator TextBuffer::get_iter_at_offset(int char_offset)
133
gtk_text_buffer_get_iter_at_offset(gobj(), iter.gobj(), char_offset);
137
TextBuffer::iterator TextBuffer::get_iter_at_line(int line_number)
140
gtk_text_buffer_get_iter_at_line(gobj(), iter.gobj(), line_number);
144
TextBuffer::iterator TextBuffer::begin()
147
gtk_text_buffer_get_start_iter(gobj(), iter.gobj());
151
TextBuffer::iterator TextBuffer::end()
154
gtk_text_buffer_get_end_iter(gobj(), iter.gobj());
158
void TextBuffer::get_bounds(iterator& range_begin, iterator& range_end)
160
gtk_text_buffer_get_bounds(gobj(), range_begin.gobj(), range_end.gobj());
163
TextBuffer::iterator TextBuffer::get_iter_at_mark(const Glib::RefPtr<Mark>& mark)
166
gtk_text_buffer_get_iter_at_mark(gobj(), iter.gobj(), mark->gobj());
170
void TextBuffer::set_text(const Glib::ustring& text)
172
gtk_text_buffer_set_text(gobj(), text.data(), text.bytes());
175
void TextBuffer::set_text(const char* text_begin, const char* text_end)
177
gtk_text_buffer_set_text(gobj(), text_begin, text_end - text_begin);
180
TextBuffer::iterator TextBuffer::insert(const iterator& pos, const Glib::ustring& text)
182
// gtk_text_buffer_insert() modifies the iterator, but that's not the
183
// STL way so we give it something that we don't mind it modifying.
184
iterator iterCopy (pos);
185
gtk_text_buffer_insert(gobj(), iterCopy.gobj(), text.data(), text.bytes());
187
// According to the gtk_text_buffer_insert() docs, the "default signal handler
188
// revalidates it to point to the end of the inserted text".
192
TextBuffer::iterator TextBuffer::insert(const iterator& pos, const char* text_begin, const char* text_end)
194
// gtk_text_buffer_insert() modifies the iterator, but that's not the
195
// STL way so we give it something that we don't mind it modifying.
196
iterator iterCopy (pos);
197
gtk_text_buffer_insert(gobj(), iterCopy.gobj(), text_begin, text_end - text_begin);
199
// According to the gtk_text_buffer_insert() docs, the "default signal handler
200
// revalidates it to point to the end of the inserted text".
204
TextBuffer::iterator TextBuffer::insert_pixbuf(const iterator& pos, const Glib::RefPtr<Gdk::Pixbuf>& pixbuf)
206
iterator iterCopy (pos);
207
gtk_text_buffer_insert_pixbuf(gobj(), iterCopy.gobj(), pixbuf->gobj());
211
TextBuffer::iterator TextBuffer::insert_child_anchor(const iterator& pos,
212
const Glib::RefPtr<ChildAnchor>& anchor)
214
// Copy the iterator. It might be changed because it is used as a signal parameter internally.
215
iterator iterCopy (pos);
216
gtk_text_buffer_insert_child_anchor(gobj(), iterCopy.gobj(), Glib::unwrap(anchor));
220
Glib::RefPtr<ChildAnchor> TextBuffer::create_child_anchor(const iterator& pos)
222
// Copy the iterator. It might be changed because it is used as a signal parameter internally.
223
iterator iterCopy (pos);
224
return Glib::wrap(gtk_text_buffer_create_child_anchor(gobj(), iterCopy.gobj()),
225
true); // The function does not do a ref for us.
228
void TextBuffer::insert_at_cursor(const Glib::ustring& text)
230
gtk_text_buffer_insert_at_cursor(gobj(), text.data(), text.bytes());
233
void TextBuffer::insert_at_cursor(const char* text_begin, const char* text_end)
235
gtk_text_buffer_insert_at_cursor(gobj(), text_begin, text_end - text_begin);
238
std::pair<TextBuffer::iterator,bool>
239
TextBuffer::insert_interactive(const iterator& pos, const Glib::ustring& text, bool default_editable)
241
// Since we have to copy the iterator anyway we can as well create the
242
// std::pair now. That saves another copy later (mind you, TextIter is
243
// a heavy struct), and allows modern compilers to apply the return value
245
std::pair<iterator,bool> pair_iter_success (pos, false);
247
pair_iter_success.second = gtk_text_buffer_insert_interactive(
248
gobj(), pair_iter_success.first.gobj(), text.data(), text.bytes(), default_editable);
250
return pair_iter_success;
253
std::pair<TextBuffer::iterator,bool>
254
TextBuffer::insert_interactive(const iterator& pos, const char* text_begin, const char* text_end,
255
bool default_editable)
257
// Since we have to copy the iterator anyway we can as well create the
258
// std::pair now. That saves another copy later (mind you, TextIter is
259
// a heavy struct), and allows modern compilers to apply the return value
261
std::pair<iterator,bool> pair_iter_success (pos, false);
263
pair_iter_success.second = gtk_text_buffer_insert_interactive(
264
gobj(), pair_iter_success.first.gobj(), text_begin, text_end - text_begin, default_editable);
266
return pair_iter_success;
269
bool TextBuffer::insert_interactive_at_cursor(const Glib::ustring& text, bool default_editable)
271
return gtk_text_buffer_insert_interactive_at_cursor(
272
gobj(), text.data(), text.bytes(), default_editable);
275
bool TextBuffer::insert_interactive_at_cursor(const char* text_begin, const char* text_end,
276
bool default_editable)
278
return gtk_text_buffer_insert_interactive_at_cursor(
279
gobj(), text_begin, text_end - text_begin, default_editable);
282
TextBuffer::iterator TextBuffer::insert_with_tag(const iterator& pos, const Glib::ustring& text,
283
const Glib::RefPtr<Tag>& tag)
285
// gtk_text_buffer_insert_with_tags() invalidates the iterator, but this lets us recreate it later.
286
const int offset = pos.get_offset();
288
iterator iterCopy (pos);
289
gtk_text_buffer_insert_with_tags(
290
gobj(), iterCopy.gobj(), text.data(), text.bytes(), tag->gobj(), (GtkTextTag*)0);
292
return get_iter_at_offset(offset + text.size());
295
TextBuffer::iterator TextBuffer::insert_with_tag(const iterator& pos,
296
const char* text_begin, const char* text_end,
297
const Glib::RefPtr<Tag>& tag)
299
// gtk_text_buffer_insert_with_tags() invalidates the iterator, but this lets us recreate it later.
300
const int offset = pos.get_offset();
302
iterator iterCopy (pos);
303
gtk_text_buffer_insert_with_tags(
304
gobj(), iterCopy.gobj(), text_begin, text_end - text_begin, tag->gobj(), (GtkTextTag*)0);
306
return get_iter_at_offset(offset + (text_end - text_begin));
309
TextBuffer::iterator TextBuffer::insert_with_tag(const iterator& pos, const Glib::ustring& text,
310
const Glib::ustring& tag_name)
312
// gtk_text_buffer_insert_with_tags() invalidates the iterator, but this lets us recreate it later.
313
const int offset = pos.get_offset();
315
iterator iterCopy (pos);
316
gtk_text_buffer_insert_with_tags_by_name(
317
gobj(), iterCopy.gobj(), text.data(), text.bytes(), tag_name.c_str(), (char*)0);
319
return get_iter_at_offset(offset + text.size());
322
TextBuffer::iterator TextBuffer::insert_with_tag(const iterator& pos,
323
const char* text_begin, const char* text_end,
324
const Glib::ustring& tag_name)
326
// gtk_text_buffer_insert_with_tags() invalidates the iterator, but this lets us recreate it later.
327
const int offset = pos.get_offset();
329
iterator iterCopy (pos);
330
gtk_text_buffer_insert_with_tags_by_name(
331
gobj(), iterCopy.gobj(), text_begin, text_end - text_begin, tag_name.c_str(), (char*)0);
333
return get_iter_at_offset(offset + (text_end - text_begin));
336
TextBuffer::iterator TextBuffer::insert_with_tags(const iterator& pos, const Glib::ustring& text,
337
const std::vector< Glib::RefPtr<Tag> >& tags)
339
const char *const text_begin = text.data();
340
return insert_with_tags(pos, text_begin, text_begin + text.bytes(), tags);
343
TextBuffer::iterator TextBuffer::insert_with_tags(const iterator& pos,
344
const char* text_begin, const char* text_end,
345
const std::vector< Glib::RefPtr<Tag> >& tags)
347
const int start_offset = pos.get_offset();
348
iterator range_end (insert(pos, text_begin, text_end));
350
GtkTextIter range_begin;
351
gtk_text_buffer_get_iter_at_offset(gobj(), &range_begin, start_offset);
353
Glib::ArrayHandler<Glib::RefPtr<Tag> >::ArrayKeeperType array_keeper (Glib::ArrayHandler<Glib::RefPtr<Tag> >::vector_to_array(tags));
355
//This was GtkTextTag* const * const, but the SUN Forte compiler said that it couldn't convert to that. murrayc
356
const GtkTextTag* const* tags_begin = array_keeper.data();
357
const GtkTextTag* const* tags_end = tags_begin + tags.size();
359
//TODO: Investigate if this const_cast<> is really necessary.
360
//I added it for the SUN Forte compiler. murrayc.
361
for(GtkTextTag *const * ptag = const_cast<GtkTextTag* const *>(tags_begin); ptag != const_cast<GtkTextTag* const *>(tags_end); ++ptag)
363
gtk_text_buffer_apply_tag(gobj(), *ptag, &range_begin, range_end.gobj());
369
TextBuffer::iterator TextBuffer::insert_with_tags_by_name(const iterator& pos, const Glib::ustring& text,
370
const std::vector<Glib::ustring>& tag_names)
372
const char *const text_begin = text.data();
373
return insert_with_tags_by_name(pos, text_begin, text_begin + text.bytes(), tag_names);
376
TextBuffer::iterator TextBuffer::insert_with_tags_by_name(const iterator& pos,
377
const char* text_begin, const char* text_end,
378
const std::vector<Glib::ustring>& tag_names)
380
// gtk_buffer_insert_with_tags_by_name() is a convenience wrapper, so it's kind of OK to reimplement it:
382
const int start_offset = pos.get_offset();
383
iterator range_end (insert(pos, text_begin, text_end));
385
GtkTextIter range_begin;
386
gtk_text_buffer_get_iter_at_offset(gobj(), &range_begin, start_offset);
388
GtkTextTagTable *const tag_table = gtk_text_buffer_get_tag_table(gobj());
389
Glib::ArrayHandler<Glib::ustring>::ArrayKeeperType array_keeper (Glib::ArrayHandler<Glib::ustring>::vector_to_array(tag_names));
391
const char *const *const names_begin = array_keeper.data();
392
const char *const *const names_end = names_begin + tag_names.size();
394
for(const char *const * pname = names_begin; pname != names_end; ++pname)
396
if(GtkTextTag *const tag = gtk_text_tag_table_lookup(tag_table, *pname))
398
gtk_text_buffer_apply_tag(gobj(), tag, &range_begin, range_end.gobj());
402
g_warning("Gtk::TextBuffer::insert_with_tags_by_name(): no tag with name '%s'!", *pname);
409
TextBuffer::iterator TextBuffer::insert(const iterator& pos,
410
const iterator& range_begin, const iterator& range_end)
412
iterator iterCopy (pos);
413
gtk_text_buffer_insert_range(gobj(), iterCopy.gobj(), range_begin.gobj(), range_end.gobj());
417
std::pair<TextBuffer::iterator,bool>
418
TextBuffer::insert_interactive(const iterator& pos, const iterator& range_begin,
419
const iterator& range_end, bool default_editable)
421
// Since we have to copy the iterator anyway we can as well create the
422
// std::pair now. That saves another copy later (mind you, TextIter is
423
// a heavy struct), and allows modern compilers to apply the return value
425
std::pair<iterator,bool> pair_iter_success (pos, false);
427
pair_iter_success.second = gtk_text_buffer_insert_range_interactive(
428
gobj(), pair_iter_success.first.gobj(), range_begin.gobj(), range_end.gobj(), default_editable);
430
return pair_iter_success;
433
TextBuffer::iterator TextBuffer::erase(const iterator& range_begin, const iterator& range_end)
435
// GTK+ sets the iterators to where the deletion occured. We do it the STL way and therefore need copies.
436
iterator beginCopy (range_begin);
437
iterator endCopy (range_end);
438
gtk_text_buffer_delete(gobj(), beginCopy.gobj(), endCopy.gobj());
442
TextBuffer::iterator TextBuffer::backspace(const iterator& iter, bool interactive, bool default_editable)
444
// GTK+ sets the iterators to where the deletion occured. We do it the STL way and therefore need copies.
446
gtk_text_buffer_backspace(gobj(), copy.gobj(), interactive, default_editable);
450
_IGNORE(gtk_text_buffer_backspace)
452
std::pair<TextBuffer::iterator,bool>
453
TextBuffer::erase_interactive(const iterator& range_begin, const iterator& range_end,
454
bool default_editable)
456
// Since we have to copy the iterator anyway we can as well create the
457
// std::pair now. That saves another copy later (mind you, TextIter is
458
// a heavy struct), and allows modern compilers to apply the return value
460
std::pair<iterator,bool> pair_iter_success (range_begin, false);
462
// GTK+ sets the iterators to where the deletion occured.
463
// We do it the STL way and therefore need copies.
464
iterator endCopy (range_end);
466
pair_iter_success.second = gtk_text_buffer_delete_interactive(
467
gobj(), pair_iter_success.first.gobj(), endCopy.gobj(), default_editable);
469
return pair_iter_success;
472
void TextBuffer::paste_clipboard(const Glib::RefPtr<Clipboard>& clipboard, const iterator& override_location,
473
bool default_editable)
475
gtk_text_buffer_paste_clipboard(gobj(), clipboard->gobj(),
476
const_cast<GtkTextIter*>(override_location.gobj()), default_editable);
479
void TextBuffer::paste_clipboard(const Glib::RefPtr<Clipboard>& clipboard, bool default_editable)
481
gtk_text_buffer_paste_clipboard(gobj(), clipboard->gobj(), 0, default_editable);
484
TextBuffer::iterator TextBuffer::get_iter_at_child_anchor(const Glib::RefPtr<ChildAnchor>& anchor)
487
gtk_text_buffer_get_iter_at_child_anchor(gobj(), iter.gobj(), anchor->gobj());
491
int TextBuffer::size() const
493
return get_char_count();
496
Glib::ustring TextBuffer::get_text(bool include_hidden_chars) const
498
TextBuffer* unconst = const_cast<TextBuffer*>(this); //Because begin() and end() are not const.
499
return get_text(unconst->begin(), unconst->end(), include_hidden_chars);
502
std::vector<Glib::ustring> TextBuffer::get_serialize_formats() const
505
GdkAtom* atoms = gtk_text_buffer_get_serialize_formats(const_cast<GtkTextBuffer*>(gobj()), &n_atoms);
506
return Glib::ArrayHandler<Glib::ustring, Gdk::AtomUstringTraits>::array_to_vector(atoms, n_atoms, Glib::OWNERSHIP_SHALLOW);
509
std::vector<Glib::ustring> TextBuffer::get_deserialize_formats() const
512
GdkAtom* atoms = gtk_text_buffer_get_deserialize_formats(const_cast<GtkTextBuffer*>(gobj()), &n_atoms);
513
return Glib::ArrayHandler<Glib::ustring, Gdk::AtomUstringTraits>::array_to_vector(atoms, n_atoms, Glib::OWNERSHIP_SHALLOW);
517
Glib::ustring TextBuffer::register_serialize_format(const Glib::ustring& mime_type, const SlotSerialize& slot)
519
SlotSerialize* slot_copy = new SlotSerialize(slot);
520
GdkAtom atom = gtk_text_buffer_register_serialize_format(gobj(), mime_type.c_str(),
521
&SignalProxy_Serialize, slot_copy, &SignalProxy_Serialize_gtk_callback_destroy);
523
//Convert the atom to a string:
524
Glib::ustring atom_as_string;
525
char* atom_name = gdk_atom_name(atom);
528
atom_as_string = atom_name;
532
return atom_as_string;
535
Glib::ustring TextBuffer::register_deserialize_format(const Glib::ustring& mime_type, const SlotDeserialize& slot)
537
SlotDeserialize* slot_copy = new SlotDeserialize(slot);
538
GdkAtom atom = gtk_text_buffer_register_deserialize_format(gobj(), mime_type.c_str(),
539
&SignalProxy_Deserialize, slot_copy, &SignalProxy_Deserialize_gtk_callback_destroy);
541
//Convert the atom to a string:
542
Glib::ustring atom_as_string;
543
char* atom_name = gdk_atom_name(atom);
546
atom_as_string = atom_name;
550
return atom_as_string;