Gtk::TextBuffer::signal_erase()



Hi, Everybody!
I found strange behaviour of signals on_erase() && on_insert() within Gtk::TextBuffer.

Here is a test code:
#include <gtkmm/textbuffer.h>
#include <gtkmm/main.h>
#include <iostream>

static void on_cxx_insert(const Gtk::TextIter & pos, const Glib::ustring &, int) {
    std::cout << "on_cxx_insert(): pos = " << pos.get_offset() << std::endl;
}

static void on_cxx_erase(const Gtk::TextIter & start, const Gtk::TextIter & end) {
    std::cout << "on_cxx_erase(): start = " << start.get_offset() << ", end = " << end.get_offset() << std::endl;
}

static void on_c_insert(GtkTextBuffer *, GtkTextIter * pos, const gchar *, gint, void *) {
    std::cout << "on_c_insert(): pos = " << gtk_text_iter_get_offset(pos) << std::endl;
}

static void on_c_erase(GtkTextBuffer *, GtkTextIter * start, GtkTextIter * end, void *) {
    std::cout << "on_c_erase(): start = " << gtk_text_iter_get_offset(start) << ", end = " << gtk_text_iter_get_offset(end) << std::endl;
}

int main(int argc, char * argv[]) {
    Gtk::Main gtk_main(&argc, &argv);
    Glib::RefPtr<Gtk::TextBuffer> tb = Gtk::TextBuffer::create();
    if (GtkTextBuffer * gobj = tb->gobj()) {
        g_signal_connect(gobj, "insert-text", G_CALLBACK(on_c_insert), 0);
        g_signal_connect(gobj, "delete-range", G_CALLBACK(on_c_erase), 0);
    }

    tb->signal_insert().connect(sigc::ptr_fun(on_cxx_insert));
    tb->signal_erase().connect(sigc::ptr_fun(on_cxx_erase));

    tb->set_text("This is a text");
    Gtk::TextIter start = tb->get_iter_at_offset(5), end = tb->get_iter_at_offset(8);
    tb->erase(start, end);
    std::cout << "Result = " << tb->get_text() << std::endl;

    return 0;
}

And a result:
[mrcashe@home test]$ ./test
on_c_insert(): pos = 0
on_cxx_insert(): pos = 14
on_c_erase(): start = 5, end = 8
on_cxx_erase(): start = 5, end = 5
Result = This a text

As You can see, gtk callbacks (written in C) works properly, but corresponding gtkmm (C++) slots works improperly - iterator positions are wrong. I think, gtk backend doing actual work and changing iterators somehow, after that C++ slots getting that changed iterators.
Of course, it isn't too hard to wrap C callbacks that manner as in my above code, but it looks nasty. Have authors some ideas how to resolve it?

Thanks.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]