Re: Set maximum length for text control

This turns out to be trickier than expected. In GTK3 I kept getting an invalid iter warning. Found a previous discussion about it at

Stefan Salewski figured it out. Thankfully so because I tried quite a few things to try to get rid of the iter warning.

Test the following out. See if it helps. There is a gtk_text_iter_assign() in there for GTK3. You can remove that for GTK2. GTK2 doesn't seem to be cranky about the invalid iters. You should be able to set a max length for the textview buffer in both GTK2 and GTK3 though.


//gcc -Wall textview1.c -o textview1 `pkg-config --cflags --libs gtk+-3.0`

#include <gtk/gtk.h>

static void insert_text(GtkTextBuffer *buffer, GtkTextIter *location, gchar *text, gint len, gpointer user_data)
    static int i=1;
    gint count=gtk_text_buffer_get_char_count(buffer);
    g_print("%i Chars %i\n", i++, count);
        GtkTextIter offset, end;
        gtk_text_buffer_get_iter_at_offset(buffer, &offset, 10);
        gtk_text_buffer_get_end_iter(buffer, &end);
        g_print("Remove Range %i %i\n", gtk_text_iter_get_offset(&offset), gtk_text_iter_get_offset(&end));
        gtk_text_buffer_delete(buffer, &offset, &end);
        gtk_text_iter_assign(location, &offset);
int main (int argc, char *argv[])
    gtk_init(&argc, &argv);

    GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Textview");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_container_set_border_width(GTK_CONTAINER(window), 20);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    GtkWidget *scroll=gtk_scrolled_window_new(NULL, NULL);

    GtkWidget *textview=gtk_text_view_new();
    GtkTextBuffer *buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
    g_signal_connect_after(buffer, "insert-text", G_CALLBACK(insert_text), NULL);

    gtk_container_add(GTK_CONTAINER(scroll), textview);

    gtk_container_add(GTK_CONTAINER(window), scroll);


    return 0;

-----Original Message-----
From: Igor Korot <ikorot01 gmail com>
To: gtk-list <gtk-list gnome org>
Sent: Sat, Dec 31, 2016 4:33 pm
Subject: Set maximum length for text control

In the GtkEntry there is a function "gtk_entry_set_max_length()".
However, it is for a single line text entry.

Is there a way to set maximum length for multi-line text control?

Thank you.

Happy New Year!!
gtk-list mailing list
gtk-list gnome org

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