Re: Set maximum length for text control



Eric,
Here is my corrected code:

static void maxlen_handler(GtkTextBuffer *buffer, GtkTextIter
*location, gchar *text, gint len, gpointer ptr)
{
    text = text;
    len = len;
    ptr = ptr;
    gint count = gtk_text_buffer_get_char_count( buffer );
    if( count > 5 )
    {
        GtkTextIter end;
        gtk_text_buffer_get_end_iter( buffer, &end );
        gtk_text_buffer_delete( buffer, location, &end );
    }
}

Basically what I'm doing is:
I already have the beginning iterator - location, so all I need is the
end iterator, which is
retrieved using gtk_text_buffer_get_end_iter().
Then I am removing the text between location and and end.

However, it doesn't work. I can still see the typing text which more
that 5 symbols.
Running under gdb I see that _buffer_delete() call is executed, but
the text is not being
updated.
I am trying it on GTK+-2.24.

Thank you.

On Wed, Jan 11, 2017 at 2:21 PM,  <cecashon aol com> wrote:

Check that your callback looks similar to the following.

https://developer.gnome.org/gtk3/stable/GtkTextBuffer.html#GtkTextBuffer-insert-text

It looks like you have "gint WXUNUSED(len), gpointer *user_data" that is
likely giving you trouble.

A couple of notes about this. It is a good idea to look up the callback in
the documentation. If you get the parameters wrong your code may compile
without warning and then later segfault. This is something in C you have to
be careful of. The compiler might not help you here. Also a gpointer is a
void* pointer.

https://developer.gnome.org/glib/stable/glib-Basic-Types.html#gpointer

This can cause confusion. When you are using a gpointer *user_data, that is
a pointer to an array of pointers like a void **user_data or a void
*user_data[]. That is valid if you are sending to your callback a pointer to
some array of pointers and you might want to do this. Again, the compiler
might not be able to help you when using void* pointers and you will
probably get a segfault if you get it wrong. The documentation has a
gpointer user_data parameter so that you can pass a pointer of any type to
your callback along with the other required callback parameters. If you know
the type of the pointer you can use the type in the callback so the compiler
can help find potential errors. If you are sending a GRand* to your callback
you can type it as such. If you are sending a pointer to an array of widget
pointers you might have something like GtkWidget **widgets or GtkWidget
*widgets[] although gpointer *widgets will work also and is valid but the
compiler can't type check it for you.

Eric




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