| 1 |
/* Copyright (C) 1998-2002 The gtkmm Development Team |
| 2 |
* |
| 3 |
* This library is free software; you can redistribute it and/or |
| 4 |
* modify it under the terms of the GNU Lesser General Public |
| 5 |
* License as published by the Free Software Foundation; either |
| 6 |
* version 2.1 of the License, or (at your option) any later version. |
| 7 |
* |
| 8 |
* This library is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
* Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public |
| 14 |
* License along with this library; if not, see <https://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <glibmm/vectorutils.h> |
| 18 |
|
| 19 |
#include <gtk/gtk.h> |
| 20 |
#include <glib.h> |
| 21 |
#include <gtkmm/textmark.h> |
| 22 |
#include <gtkmm/textiter.h> |
| 23 |
|
| 24 |
namespace // anonymous |
| 25 |
{ |
| 26 |
extern "C" |
| 27 |
{ |
| 28 |
|
| 29 |
static guint8* SignalProxy_Serialize(GtkTextBuffer* /* register_buffer */, |
| 30 |
GtkTextBuffer* content_buffer, |
| 31 |
const GtkTextIter* start, const GtkTextIter* end, |
| 32 |
gsize* length, |
| 33 |
gpointer user_data) |
| 34 |
{ |
| 35 |
auto the_slot = static_cast<Gtk::TextBuffer::SlotSerialize*>(user_data); |
| 36 |
|
| 37 |
try |
| 38 |
{ |
| 39 |
return (*the_slot)(Glib::wrap(content_buffer, true), Glib::wrap(start), Glib::wrap(end), *length); |
| 40 |
} |
| 41 |
catch (...) |
| 42 |
{ |
| 43 |
Glib::exception_handlers_invoke(); |
| 44 |
} |
| 45 |
return nullptr; |
| 46 |
} |
| 47 |
|
| 48 |
static void SignalProxy_Serialize_gtk_callback_destroy(void* user_data) |
| 49 |
{ |
| 50 |
delete static_cast<Gtk::TextBuffer::SlotSerialize*>(user_data); |
| 51 |
} |
| 52 |
|
| 53 |
static gboolean SignalProxy_Deserialize(GtkTextBuffer* /* register_buffer */, |
| 54 |
GtkTextBuffer* content_buffer, |
| 55 |
GtkTextIter* iter, const guint8* data, |
| 56 |
gsize length, gboolean create_tags, |
| 57 |
gpointer user_data, |
| 58 |
GError** error) |
| 59 |
{ |
| 60 |
auto the_slot = static_cast<Gtk::TextBuffer::SlotDeserialize*>(user_data); |
| 61 |
|
| 62 |
try |
| 63 |
{ |
| 64 |
return (*the_slot)(Glib::wrap(content_buffer, true), Glib::wrap(iter), data, length, create_tags); |
| 65 |
} |
| 66 |
catch (Glib::Error& err) |
| 67 |
{ |
| 68 |
err.propagate(error); |
| 69 |
} |
| 70 |
catch (...) |
| 71 |
{ |
| 72 |
Glib::exception_handlers_invoke(); |
| 73 |
} |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
static void SignalProxy_Deserialize_gtk_callback_destroy(void* user_data) |
| 78 |
{ |
| 79 |
delete static_cast<Gtk::TextBuffer::SlotDeserialize*>(user_data); |
| 80 |
} |
| 81 |
|
| 82 |
} // extern "C" |
| 83 |
} // anonymous namespace |
| 84 |
|
| 85 |
namespace Gtk |
| 86 |
{ |
| 87 |
|
| 88 |
typedef TextChildAnchor ChildAnchor; //Help the code-generator so that it does not have to fully qualify this type in the .cc file. |
| 89 |
|
| 90 |
TextBuffer::TextBuffer(const Glib::RefPtr<TagTable>& tag_table) |
| 91 |
: |
| 92 |
_CONSTRUCT("tag_table", Glib::unwrap(tag_table)) |
| 93 |
{} |
| 94 |
|
| 95 |
Glib::RefPtr<TextBuffer::Tag> TextBuffer::create_tag(const Glib::ustring& tag_name) |
| 96 |
{ |
| 97 |
//gtk_text_buffer_create_tag takes a varargs list of property names and values. |
| 98 |
//gtkmm coders should use the Tag.set_* method instead. |
| 99 |
return Glib::wrap(gtk_text_buffer_create_tag(gobj(), tag_name.c_str(), nullptr), true); //true = take_copy. |
| 100 |
//We have to take a copy because gtk_text_buffer_create_tag() doesn't ref for us, for no real reason. |
| 101 |
} |
| 102 |
|
| 103 |
Glib::RefPtr<TextBuffer::Tag> TextBuffer::create_tag() |
| 104 |
{ |
| 105 |
//gtk_text_buffer_create_tag takes a varargs list of property names and values. |
| 106 |
//gtkmm coders should use the Tag.set_* method instead. |
| 107 |
return Glib::wrap(gtk_text_buffer_create_tag(gobj(), nullptr, nullptr), true); //true = take_copy. |
| 108 |
//We have to take a copy because gtk_text_buffer_create_tag() doesn't ref for us, for no real reason. |
| 109 |
} |
| 110 |
|
| 111 |
Glib::RefPtr<TextBuffer::Mark> |
| 112 |
TextBuffer::create_mark(const TextBuffer::iterator& where, bool left_gravity) |
| 113 |
{ |
| 114 |
return Glib::wrap(gtk_text_buffer_create_mark( |
| 115 |
gobj(), nullptr, const_cast<GtkTextIter*>(where.gobj()), left_gravity), |
| 116 |
true); // acquire reference |
| 117 |
} |
| 118 |
|
| 119 |
TextBuffer::iterator TextBuffer::get_iter_at_line_offset(int line_number, int char_offset) |
| 120 |
{ |
| 121 |
iterator iter; |
| 122 |
gtk_text_buffer_get_iter_at_line_offset(gobj(), iter.gobj(), line_number, char_offset); |
| 123 |
return iter; |
| 124 |
} |
| 125 |
|
| 126 |
TextBuffer::iterator TextBuffer::get_iter_at_line_index(int line_number, int byte_index) |
| 127 |
{ |
| 128 |
iterator iter; |
| 129 |
gtk_text_buffer_get_iter_at_line_index(gobj(), iter.gobj(), line_number, byte_index); |
| 130 |
return iter; |
| 131 |
} |
| 132 |
|
| 133 |
TextBuffer::iterator TextBuffer::get_iter_at_offset(int char_offset) |
| 134 |
{ |
| 135 |
iterator iter; |
| 136 |
gtk_text_buffer_get_iter_at_offset(gobj(), iter.gobj(), char_offset); |
| 137 |
return iter; |
| 138 |
} |
| 139 |
|
| 140 |
TextBuffer::iterator TextBuffer::get_iter_at_line(int line_number) |
| 141 |
{ |
| 142 |
iterator iter; |
| 143 |
gtk_text_buffer_get_iter_at_line(gobj(), iter.gobj(), line_number); |
| 144 |
return iter; |
| 145 |
} |
| 146 |
|
| 147 |
TextBuffer::iterator TextBuffer::begin() |
| 148 |
{ |
| 149 |
iterator iter; |
| 150 |
gtk_text_buffer_get_start_iter(gobj(), iter.gobj()); |
| 151 |
return iter; |
| 152 |
} |
| 153 |
|
| 154 |
TextBuffer::iterator TextBuffer::end() |
| 155 |
{ |
| 156 |
iterator iter; |
| 157 |
gtk_text_buffer_get_end_iter(gobj(), iter.gobj()); |
| 158 |
return iter; |
| 159 |
} |
| 160 |
|
| 161 |
void TextBuffer::get_bounds(iterator& range_begin, iterator& range_end) |
| 162 |
{ |
| 163 |
gtk_text_buffer_get_bounds(gobj(), range_begin.gobj(), range_end.gobj()); |
| 164 |
} |
| 165 |
|
| 166 |
TextBuffer::iterator TextBuffer::get_iter_at_mark(const Glib::RefPtr<Mark>& mark) |
| 167 |
{ |
| 168 |
iterator iter; |
| 169 |
gtk_text_buffer_get_iter_at_mark(gobj(), iter.gobj(), mark->gobj()); |
| 170 |
return iter; |
| 171 |
} |
| 172 |
|
| 173 |
void TextBuffer::set_text(const Glib::ustring& text) |
| 174 |
{ |
| 175 |
gtk_text_buffer_set_text(gobj(), text.data(), text.bytes()); |
| 176 |
} |
| 177 |
|
| 178 |
void TextBuffer::set_text(const char* text_begin, const char* text_end) |
| 179 |
{ |
| 180 |
gtk_text_buffer_set_text(gobj(), text_begin, text_end - text_begin); |
| 181 |
} |
| 182 |
|
| 183 |
TextBuffer::iterator TextBuffer::insert(const iterator& pos, const Glib::ustring& text) |
| 184 |
{ |
| 185 |
// gtk_text_buffer_insert() modifies the iterator, but that's not the |
| 186 |
// STL way so we give it something that we don't mind it modifying. |
| 187 |
iterator iterCopy (pos); |
| 188 |
gtk_text_buffer_insert(gobj(), iterCopy.gobj(), text.data(), text.bytes()); |
| 189 |
|
| 190 |
// According to the gtk_text_buffer_insert() docs, the "default signal handler |
| 191 |
// revalidates it to point to the end of the inserted text". |
| 192 |
return iterCopy; |
| 193 |
} |
| 194 |
|
| 195 |
TextBuffer::iterator TextBuffer::insert(const iterator& pos, const char* text_begin, const char* text_end) |
| 196 |
{ |
| 197 |
// gtk_text_buffer_insert() modifies the iterator, but that's not the |
| 198 |
// STL way so we give it something that we don't mind it modifying. |
| 199 |
iterator iterCopy (pos); |
| 200 |
gtk_text_buffer_insert(gobj(), iterCopy.gobj(), text_begin, text_end - text_begin); |
| 201 |
|
| 202 |
// According to the gtk_text_buffer_insert() docs, the "default signal handler |
| 203 |
// revalidates it to point to the end of the inserted text". |
| 204 |
return iterCopy; |
| 205 |
} |
| 206 |
|
| 207 |
TextBuffer::iterator TextBuffer::insert_pixbuf(const iterator& pos, const Glib::RefPtr<Gdk::Pixbuf>& pixbuf) |
| 208 |
{ |
| 209 |
iterator iterCopy (pos); |
| 210 |
gtk_text_buffer_insert_pixbuf(gobj(), iterCopy.gobj(), pixbuf->gobj()); |
| 211 |
return iterCopy; |
| 212 |
} |
| 213 |
|
| 214 |
TextBuffer::iterator TextBuffer::insert_child_anchor(const iterator& pos, |
| 215 |
const Glib::RefPtr<ChildAnchor>& anchor) |
| 216 |
{ |
| 217 |
// Copy the iterator. It might be changed because it is used as a signal parameter internally. |
| 218 |
iterator iterCopy (pos); |
| 219 |
gtk_text_buffer_insert_child_anchor(gobj(), iterCopy.gobj(), Glib::unwrap(anchor)); |
| 220 |
return iterCopy; |
| 221 |
} |
| 222 |
|
| 223 |
Glib::RefPtr<ChildAnchor> TextBuffer::create_child_anchor(const iterator& pos) |
| 224 |
{ |
| 225 |
// Copy the iterator. It might be changed because it is used as a signal parameter internally. |
| 226 |
iterator iterCopy (pos); |
| 227 |
return Glib::wrap(gtk_text_buffer_create_child_anchor(gobj(), iterCopy.gobj()), |
| 228 |
true); // The function does not do a ref for us. |
| 229 |
} |
| 230 |
|
| 231 |
void TextBuffer::insert_at_cursor(const Glib::ustring& text) |
| 232 |
{ |
| 233 |
gtk_text_buffer_insert_at_cursor(gobj(), text.data(), text.bytes()); |
| 234 |
} |
| 235 |
|
| 236 |
void TextBuffer::insert_at_cursor(const char* text_begin, const char* text_end) |
| 237 |
{ |
| 238 |
gtk_text_buffer_insert_at_cursor(gobj(), text_begin, text_end - text_begin); |
| 239 |
} |
| 240 |
|
| 241 |
std::pair<TextBuffer::iterator,bool> |
| 242 |
TextBuffer::insert_interactive(const iterator& pos, const Glib::ustring& text, bool default_editable) |
| 243 |
{ |
| 244 |
// Since we have to copy the iterator anyway we can as well create the |
| 245 |
// std::pair now. That saves another copy later (mind you, TextIter is |
| 246 |
// a heavy struct), and allows modern compilers to apply the return value |
| 247 |
// optimization. |
| 248 |
std::pair<iterator,bool> pair_iter_success (pos, false); |
| 249 |
|
| 250 |
pair_iter_success.second = gtk_text_buffer_insert_interactive( |
| 251 |
gobj(), pair_iter_success.first.gobj(), text.data(), text.bytes(), default_editable); |
| 252 |
|
| 253 |
return pair_iter_success; |
| 254 |
} |
| 255 |
|
| 256 |
std::pair<TextBuffer::iterator,bool> |
| 257 |
TextBuffer::insert_interactive(const iterator& pos, const char* text_begin, const char* text_end, |
| 258 |
bool default_editable) |
| 259 |
{ |
| 260 |
// Since we have to copy the iterator anyway we can as well create the |
| 261 |
// std::pair now. That saves another copy later (mind you, TextIter is |
| 262 |
// a heavy struct), and allows modern compilers to apply the return value |
| 263 |
// optimization. |
| 264 |
std::pair<iterator,bool> pair_iter_success (pos, false); |
| 265 |
|
| 266 |
pair_iter_success.second = gtk_text_buffer_insert_interactive( |
| 267 |
gobj(), pair_iter_success.first.gobj(), text_begin, text_end - text_begin, default_editable); |
| 268 |
|
| 269 |
return pair_iter_success; |
| 270 |
} |
| 271 |
|
| 272 |
bool TextBuffer::insert_interactive_at_cursor(const Glib::ustring& text, bool default_editable) |
| 273 |
{ |
| 274 |
return gtk_text_buffer_insert_interactive_at_cursor( |
| 275 |
gobj(), text.data(), text.bytes(), default_editable); |
| 276 |
} |
| 277 |
|
| 278 |
bool TextBuffer::insert_interactive_at_cursor(const char* text_begin, const char* text_end, |
| 279 |
bool default_editable) |
| 280 |
{ |
| 281 |
return gtk_text_buffer_insert_interactive_at_cursor( |
| 282 |
gobj(), text_begin, text_end - text_begin, default_editable); |
| 283 |
} |
| 284 |
|
| 285 |
TextBuffer::iterator TextBuffer::insert_with_tag(const iterator& pos, const Glib::ustring& text, |
| 286 |
const Glib::RefPtr<Tag>& tag) |
| 287 |
{ |
| 288 |
// gtk_text_buffer_insert_with_tags() invalidates the iterator, but this lets us recreate it later. |
| 289 |
const int offset = pos.get_offset(); |
| 290 |
|
| 291 |
iterator iterCopy (pos); |
| 292 |
gtk_text_buffer_insert_with_tags( |
| 293 |
gobj(), iterCopy.gobj(), text.data(), text.bytes(), tag->gobj(), nullptr); |
| 294 |
|
| 295 |
return get_iter_at_offset(offset + text.size()); |
| 296 |
} |
| 297 |
|
| 298 |
TextBuffer::iterator TextBuffer::insert_with_tag(const iterator& pos, |
| 299 |
const char* text_begin, const char* text_end, |
| 300 |
const Glib::RefPtr<Tag>& tag) |
| 301 |
{ |
| 302 |
// gtk_text_buffer_insert_with_tags() invalidates the iterator, but this lets us recreate it later. |
| 303 |
const int offset = pos.get_offset(); |
| 304 |
|
| 305 |
iterator iterCopy (pos); |
| 306 |
gtk_text_buffer_insert_with_tags( |
| 307 |
gobj(), iterCopy.gobj(), text_begin, text_end - text_begin, tag->gobj(), nullptr); |
| 308 |
|
| 309 |
return get_iter_at_offset(offset + (text_end - text_begin)); |
| 310 |
} |
| 311 |
|
| 312 |
TextBuffer::iterator TextBuffer::insert_with_tag(const iterator& pos, const Glib::ustring& text, |
| 313 |
const Glib::ustring& tag_name) |
| 314 |
{ |
| 315 |
// gtk_text_buffer_insert_with_tags() invalidates the iterator, but this lets us recreate it later. |
| 316 |
const int offset = pos.get_offset(); |
| 317 |
|
| 318 |
iterator iterCopy (pos); |
| 319 |
gtk_text_buffer_insert_with_tags_by_name( |
| 320 |
gobj(), iterCopy.gobj(), text.data(), text.bytes(), tag_name.c_str(), nullptr); |
| 321 |
|
| 322 |
return get_iter_at_offset(offset + text.size()); |
| 323 |
} |
| 324 |
|
| 325 |
TextBuffer::iterator TextBuffer::insert_with_tag(const iterator& pos, |
| 326 |
const char* text_begin, const char* text_end, |
| 327 |
const Glib::ustring& tag_name) |
| 328 |
{ |
| 329 |
// gtk_text_buffer_insert_with_tags() invalidates the iterator, but this lets us recreate it later. |
| 330 |
const int offset = pos.get_offset(); |
| 331 |
|
| 332 |
iterator iterCopy (pos); |
| 333 |
gtk_text_buffer_insert_with_tags_by_name( |
| 334 |
gobj(), iterCopy.gobj(), text_begin, text_end - text_begin, tag_name.c_str(), nullptr); |
| 335 |
|
| 336 |
return get_iter_at_offset(offset + (text_end - text_begin)); |
| 337 |
} |
| 338 |
|
| 339 |
TextBuffer::iterator TextBuffer::insert_with_tags(const iterator& pos, const Glib::ustring& text, |
| 340 |
const std::vector< Glib::RefPtr<Tag> >& tags) |
| 341 |
{ |
| 342 |
const char *const text_begin = text.data(); |
| 343 |
return insert_with_tags(pos, text_begin, text_begin + text.bytes(), tags); |
| 344 |
} |
| 345 |
|
| 346 |
TextBuffer::iterator TextBuffer::insert_with_tags(const iterator& pos, |
| 347 |
const char* text_begin, const char* text_end, |
| 348 |
const std::vector< Glib::RefPtr<Tag> >& tags) |
| 349 |
{ |
| 350 |
const int start_offset = pos.get_offset(); |
| 351 |
iterator range_end (insert(pos, text_begin, text_end)); |
| 352 |
|
| 353 |
GtkTextIter range_begin; |
| 354 |
gtk_text_buffer_get_iter_at_offset(gobj(), &range_begin, start_offset); |
| 355 |
|
| 356 |
Glib::ArrayHandler<Glib::RefPtr<Tag> >::ArrayKeeperType array_keeper (Glib::ArrayHandler<Glib::RefPtr<Tag> >::vector_to_array(tags)); |
| 357 |
|
| 358 |
//This was GtkTextTag* const * const, but the SUN Forte compiler said that it couldn't convert to that. murrayc |
| 359 |
const GtkTextTag* const* tags_begin = array_keeper.data(); |
| 360 |
const GtkTextTag* const* tags_end = tags_begin + tags.size(); |
| 361 |
|
| 362 |
//TODO: Investigate if this const_cast<> is really necessary. |
| 363 |
//I added it for the SUN Forte compiler. murrayc. |
| 364 |
for(GtkTextTag *const * ptag = const_cast<GtkTextTag* const *>(tags_begin); ptag != const_cast<GtkTextTag* const *>(tags_end); ++ptag) |
| 365 |
{ |
| 366 |
gtk_text_buffer_apply_tag(gobj(), *ptag, &range_begin, range_end.gobj()); |
| 367 |
} |
| 368 |
|
| 369 |
return range_end; |
| 370 |
} |
| 371 |
|
| 372 |
TextBuffer::iterator TextBuffer::insert_with_tags_by_name(const iterator& pos, const Glib::ustring& text, |
| 373 |
const std::vector<Glib::ustring>& tag_names) |
| 374 |
{ |
| 375 |
const char *const text_begin = text.data(); |
| 376 |
return insert_with_tags_by_name(pos, text_begin, text_begin + text.bytes(), tag_names); |
| 377 |
} |
| 378 |
|
| 379 |
TextBuffer::iterator TextBuffer::insert_with_tags_by_name(const iterator& pos, |
| 380 |
const char* text_begin, const char* text_end, |
| 381 |
const std::vector<Glib::ustring>& tag_names) |
| 382 |
{ |
| 383 |
// gtk_buffer_insert_with_tags_by_name() is a convenience wrapper, so it's kind of OK to reimplement it: |
| 384 |
|
| 385 |
const int start_offset = pos.get_offset(); |
| 386 |
iterator range_end (insert(pos, text_begin, text_end)); |
| 387 |
|
| 388 |
GtkTextIter range_begin; |
| 389 |
gtk_text_buffer_get_iter_at_offset(gobj(), &range_begin, start_offset); |
| 390 |
|
| 391 |
const auto tag_table = gtk_text_buffer_get_tag_table(gobj()); |
| 392 |
Glib::ArrayHandler<Glib::ustring>::ArrayKeeperType array_keeper (Glib::ArrayHandler<Glib::ustring>::vector_to_array(tag_names)); |
| 393 |
|
| 394 |
const char *const *const names_begin = array_keeper.data(); |
| 395 |
const char *const *const names_end = names_begin + tag_names.size(); |
| 396 |
|
| 397 |
for(const char *const * pname = names_begin; pname != names_end; ++pname) |
| 398 |
{ |
| 399 |
if(GtkTextTag *const tag = gtk_text_tag_table_lookup(tag_table, *pname)) |
| 400 |
{ |
| 401 |
gtk_text_buffer_apply_tag(gobj(), tag, &range_begin, range_end.gobj()); |
| 402 |
} |
| 403 |
else |
| 404 |
{ |
| 405 |
g_warning("Gtk::TextBuffer::insert_with_tags_by_name(): no tag with name '%s'!", *pname); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
return range_end; |
| 410 |
} |
| 411 |
|
| 412 |
TextBuffer::iterator TextBuffer::insert_markup(const iterator& pos, const Glib::ustring& markup) |
| 413 |
{ |
| 414 |
// gtk_text_buffer_insert_markup() modifies the iterator, but that's not the |
| 415 |
// STL way so we give it something that we don't mind it modifying. |
| 416 |
iterator iterCopy (pos); |
| 417 |
gtk_text_buffer_insert_markup(gobj(), iterCopy.gobj(), markup.data(), markup.bytes()); |
| 418 |
|
| 419 |
// According to the gtk_text_buffer_insert_markup() docs, the "default signal handler |
| 420 |
// revalidates it to point to the end of the inserted text". |
| 421 |
return iterCopy; |
| 422 |
} |
| 423 |
|
| 424 |
TextBuffer::iterator TextBuffer::insert_markup(const iterator& pos, const char* markup_begin, const char* markup_end) |
| 425 |
{ |
| 426 |
// gtk_text_buffer_insert_markup() modifies the iterator, but that's not the |
| 427 |
// STL way so we give it something that we don't mind it modifying. |
| 428 |
iterator iterCopy (pos); |
| 429 |
gtk_text_buffer_insert_markup(gobj(), iterCopy.gobj(), markup_begin, markup_end - markup_begin); |
| 430 |
|
| 431 |
// According to the gtk_text_buffer_insert_markup() docs, the "default signal handler |
| 432 |
// revalidates it to point to the end of the inserted text". |
| 433 |
return iterCopy; |
| 434 |
} |
| 435 |
|
| 436 |
TextBuffer::iterator TextBuffer::insert(const iterator& pos, |
| 437 |
const iterator& range_begin, const iterator& range_end) |
| 438 |
{ |
| 439 |
iterator iterCopy (pos); |
| 440 |
gtk_text_buffer_insert_range(gobj(), iterCopy.gobj(), range_begin.gobj(), range_end.gobj()); |
| 441 |
return iterCopy; |
| 442 |
} |
| 443 |
|
| 444 |
std::pair<TextBuffer::iterator,bool> |
| 445 |
TextBuffer::insert_interactive(const iterator& pos, const iterator& range_begin, |
| 446 |
const iterator& range_end, bool default_editable) |
| 447 |
{ |
| 448 |
// Since we have to copy the iterator anyway we can as well create the |
| 449 |
// std::pair now. That saves another copy later (mind you, TextIter is |
| 450 |
// a heavy struct), and allows modern compilers to apply the return value |
| 451 |
// optimization. |
| 452 |
std::pair<iterator,bool> pair_iter_success (pos, false); |
| 453 |
|
| 454 |
pair_iter_success.second = gtk_text_buffer_insert_range_interactive( |
| 455 |
gobj(), pair_iter_success.first.gobj(), range_begin.gobj(), range_end.gobj(), default_editable); |
| 456 |
|
| 457 |
return pair_iter_success; |
| 458 |
} |
| 459 |
|
| 460 |
TextBuffer::iterator TextBuffer::erase(const iterator& range_begin, const iterator& range_end) |
| 461 |
{ |
| 462 |
// GTK+ sets the iterators to where the deletion occured. We do it the STL way and therefore need copies. |
| 463 |
iterator beginCopy (range_begin); |
| 464 |
iterator endCopy (range_end); |
| 465 |
gtk_text_buffer_delete(gobj(), beginCopy.gobj(), endCopy.gobj()); |
| 466 |
return beginCopy; |
| 467 |
} |
| 468 |
|
| 469 |
TextBuffer::iterator TextBuffer::backspace(const iterator& iter, bool interactive, bool default_editable) |
| 470 |
{ |
| 471 |
// GTK+ sets the iterators to where the deletion occured. We do it the STL way and therefore need copies. |
| 472 |
iterator copy(iter); |
| 473 |
gtk_text_buffer_backspace(gobj(), copy.gobj(), interactive, default_editable); |
| 474 |
return copy; |
| 475 |
} |
| 476 |
|
| 477 |
_IGNORE(gtk_text_buffer_backspace) |
| 478 |
|
| 479 |
std::pair<TextBuffer::iterator,bool> |
| 480 |
TextBuffer::erase_interactive(const iterator& range_begin, const iterator& range_end, |
| 481 |
bool default_editable) |
| 482 |
{ |
| 483 |
// Since we have to copy the iterator anyway we can as well create the |
| 484 |
// std::pair now. That saves another copy later (mind you, TextIter is |
| 485 |
// a heavy struct), and allows modern compilers to apply the return value |
| 486 |
// optimization. |
| 487 |
std::pair<iterator,bool> pair_iter_success (range_begin, false); |
| 488 |
|
| 489 |
// GTK+ sets the iterators to where the deletion occured. |
| 490 |
// We do it the STL way and therefore need copies. |
| 491 |
iterator endCopy (range_end); |
| 492 |
|
| 493 |
pair_iter_success.second = gtk_text_buffer_delete_interactive( |
| 494 |
gobj(), pair_iter_success.first.gobj(), endCopy.gobj(), default_editable); |
| 495 |
|
| 496 |
return pair_iter_success; |
| 497 |
} |
| 498 |
|
| 499 |
void TextBuffer::paste_clipboard(const Glib::RefPtr<Clipboard>& clipboard, const iterator& override_location, |
| 500 |
bool default_editable) |
| 501 |
{ |
| 502 |
gtk_text_buffer_paste_clipboard(gobj(), clipboard->gobj(), |
| 503 |
const_cast<GtkTextIter*>(override_location.gobj()), default_editable); |
| 504 |
} |
| 505 |
|
| 506 |
void TextBuffer::paste_clipboard(const Glib::RefPtr<Clipboard>& clipboard, bool default_editable) |
| 507 |
{ |
| 508 |
gtk_text_buffer_paste_clipboard(gobj(), clipboard->gobj(), nullptr, default_editable); |
| 509 |
} |
| 510 |
|
| 511 |
TextBuffer::iterator TextBuffer::get_iter_at_child_anchor(const Glib::RefPtr<ChildAnchor>& anchor) |
| 512 |
{ |
| 513 |
iterator iter; |
| 514 |
gtk_text_buffer_get_iter_at_child_anchor(gobj(), iter.gobj(), anchor->gobj()); |
| 515 |
return iter; |
| 516 |
} |
| 517 |
|
| 518 |
int TextBuffer::size() const |
| 519 |
{ |
| 520 |
return get_char_count(); |
| 521 |
} |
| 522 |
|
| 523 |
Glib::ustring TextBuffer::get_text(bool include_hidden_chars) const |
| 524 |
{ |
| 525 |
auto unconst = const_cast<TextBuffer*>(this); //Because begin() and end() are not const. |
| 526 |
return get_text(unconst->begin(), unconst->end(), include_hidden_chars); |
| 527 |
} |
| 528 |
|
| 529 |
std::vector<Glib::ustring> TextBuffer::get_serialize_formats() const |
| 530 |
{ |
| 531 |
int n_atoms = 0; |
| 532 |
auto atoms = gtk_text_buffer_get_serialize_formats(const_cast<GtkTextBuffer*>(gobj()), &n_atoms); |
| 533 |
return Glib::ArrayHandler<Glib::ustring, Gdk::AtomUstringTraits>::array_to_vector(atoms, n_atoms, Glib::OWNERSHIP_SHALLOW); |
| 534 |
} |
| 535 |
|
| 536 |
std::vector<Glib::ustring> TextBuffer::get_deserialize_formats() const |
| 537 |
{ |
| 538 |
int n_atoms = 0; |
| 539 |
auto atoms = gtk_text_buffer_get_deserialize_formats(const_cast<GtkTextBuffer*>(gobj()), &n_atoms); |
| 540 |
return Glib::ArrayHandler<Glib::ustring, Gdk::AtomUstringTraits>::array_to_vector(atoms, n_atoms, Glib::OWNERSHIP_SHALLOW); |
| 541 |
} |
| 542 |
|
| 543 |
Glib::ustring TextBuffer::register_serialize_format(const Glib::ustring& mime_type, const SlotSerialize& slot) |
| 544 |
{ |
| 545 |
auto slot_copy = new SlotSerialize(slot); |
| 546 |
auto atom = gtk_text_buffer_register_serialize_format(gobj(), mime_type.c_str(), |
| 547 |
&SignalProxy_Serialize, slot_copy, &SignalProxy_Serialize_gtk_callback_destroy); |
| 548 |
|
| 549 |
//Convert the atom to a string: |
| 550 |
Glib::ustring atom_as_string; |
| 551 |
char* atom_name = gdk_atom_name(atom); |
| 552 |
if (atom_name) |
| 553 |
{ |
| 554 |
atom_as_string = atom_name; |
| 555 |
g_free(atom_name); |
| 556 |
} |
| 557 |
|
| 558 |
return atom_as_string; |
| 559 |
} |
| 560 |
|
| 561 |
Glib::ustring TextBuffer::register_deserialize_format(const Glib::ustring& mime_type, const SlotDeserialize& slot) |
| 562 |
{ |
| 563 |
auto slot_copy = new SlotDeserialize(slot); |
| 564 |
auto atom = gtk_text_buffer_register_deserialize_format(gobj(), mime_type.c_str(), |
| 565 |
&SignalProxy_Deserialize, slot_copy, &SignalProxy_Deserialize_gtk_callback_destroy); |
| 566 |
|
| 567 |
//Convert the atom to a string: |
| 568 |
Glib::ustring atom_as_string; |
| 569 |
char* atom_name = gdk_atom_name(atom); |
| 570 |
if (atom_name) |
| 571 |
{ |
| 572 |
atom_as_string = atom_name; |
| 573 |
g_free(atom_name); |
| 574 |
} |
| 575 |
|
| 576 |
return atom_as_string; |
| 577 |
} |
| 578 |
|
| 579 |
} // namespace Gtk |