On 15-10-07 04:14 AM, Pierre Wieser wrote:

On the application I'm currently working on [1], I want limit the size of the
text entered in a GtkTextBuffer to those saved in the DBMS, so 4096 chars.

I so connected to the 'changed' signal of the GtkTextBuffer, and the handler
is :

static void
on_notes_changed( GtkTextBuffer *buffer, void *empty )
        static const gchar *thisfn = "my_utils_on_notes_changed";
        static const gint MAX_LENGTH = 4096;
        static gboolean in = FALSE;
        gint count;
        GtkTextIter start, end;

        /* prevent an infinite recursion */
        if( !in ){
                count = gtk_text_buffer_get_char_count( buffer );
                if( count >= MAX_LENGTH ){
                         * this code works, but emit the following Gtk-Warning:
                         * Invalid text buffer iterator: either the iterator is
                         * uninitialized, or the characters/pixbufs/widgets in 
                         * buffer have been modified since the iterator was 
                         * You must use marks, character numbers, or line 
numbers to
                         * preserve a position across buffer modifications.
                         * You can apply tags and insert marks without 
                         * your iterators, but any mutation that affects 
                         * buffer contents (contents that can be referred to by 
                         * offset) will invalidate all outstanding iterators
                        gtk_text_buffer_get_iter_at_offset( buffer, &start, 
                        /*gtk_text_iter_backward_char( &start );*/
                        /*gtk_text_buffer_get_iter_at_offset( buffer, &end, 
count );*/
                        gtk_text_buffer_get_end_iter( buffer, &end );
                        /*gtk_text_iter_backward_char( &end );*/
                        in = TRUE;
                        g_debug( "%s: count=%d, start=%d, end=%d",
                                        thisfn, count, gtk_text_iter_get_offset( 
&start ), gtk_text_iter_get_offset( &end ));
                        gtk_text_buffer_delete( buffer, &start, &end );
                        in = FALSE;

As stated in the comment, the code works (the size if actually limited to 4095 
but each execution of gtk_text_buffer_delete() triggers the well-known warning
"Invalid text buffer iterator".

I am a bit stucked here, because I do not understand why this happens, as the 
is not modified between taking the iters and deleting the content...

Can anyone help me in this matter ?

Thanks in advance.
gtk-app-devel-list mailing list
I do not have an answer to the problem as stated, but I have written code that achieves the overall purpose of pruning the text buffer.
I work in Gtkmm rather than in GTK3.  Here is a snippet of code.
start = tb1_ptr->begin();
end = tb1_ptr->end();
if (end.get_offset() > 254) end.set_offset(254);
text = tb1_ptr->get_text(start, end, FALSE);
tb1_ptr = Gtk::TextBuffer::create();
Gtk::TextIter start;
Gtk::TextIter end;
Glib::ustring text;

In your case, rather than using gtk_text_buffer_delete, you would use
gtk_text_iter_get_offset (/|const GtkTextIter <https://developer.gnome.org/gtk3/stable/GtkTextIter.html> *iter|/);
gtk_text_iter_set_offset (/|GtkTextIter <https://developer.gnome.org/gtk3/stable/GtkTextIter.html> *iter|/, /|gint <https://developer.gnome.org/glib/unstable/glib-Basic-Types.html#gint> char_offset|/);

to limit the text buffer to your desired length.

Hope this helps...   Jim...

text = tb1_ptr->get_text(start, end, FALSE);
gtk-app-devel-list mailing list

Reply via email to