Web lists-archives.com

Re: Set maximum length for text control

Eric at al,

On Thu, Jan 12, 2017 at 1:56 PM,  <cecashon@xxxxxxx> wrote:
> Your callback looks to be set up correctly but you have some unnecessary
> assignments in the function. You don't want.
> text = text;
> len = len;
> ptr = ptr;
> The len and text variables are already set up for you to use in the
> callback. GTK takes care of that part for you. The ptr variable in your set
> up is going to have the variable or variables that you want to send to the
> maxlen_handler() function. It can also be NULL so I don't know by looking at
> your code how that pointer is set up. What does
> g_signal_connect_after(buffer, "insert-text", G_CALLBACK(maxlen_handler),
> NULL);
> look like in your code? Is there a value in the NULL position that you need
> in your function?
> Check where the location iter is.
> g_print("Iters %i %i\n", gtk_text_iter_get_offset(location),
> gtk_text_iter_get_offset(&end));
> It might be at the same position as the end iter. Then the delete won't do
> anything. You might need another iter to get the starting position of where
> you want to remove text.

Yes, you were right. The iterator was at the same positions.

I ended up with the following code:

    gint count = gtk_text_buffer_get_char_count( buffer );
    if( count > 5 )
        GtkTextIter offset, end;
        gint startPos = gtk_text_iter_get_offset( location );
        gtk_text_buffer_get_iter_at_offset( buffer, &offset, startPos - 1 );
        gtk_text_buffer_get_iter_at_offset( buffer, &end, (startPos -
1) + len );
        gtk_text_buffer_delete( buffer, &offset, &end );
        gtk_text_iter_assign( location, offset );

for the GTK+2.24.
I will still need to test it under GTK+3 though.

Thank you for the help.

> Eric
gtk-list mailing list