Re: Set maximum length for text control



Hi, Eric,

On Tue, Jan 3, 2017 at 5:55 PM,  <cecashon aol com> wrote:

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

https://mail.gnome.org/archives/gtk-app-devel-list/2015-October/msg00015.html

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.

I tried to modify the condition to prevent inserting the text at any
location if the
resulting text is bigger than maximum, but it didn't even compiled:

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

Any idea?

Thank you.


Eric


//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);
    if(count>10)
      {
        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);

    gtk_widget_show_all(window);

    gtk_main();

    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

Hi,
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
https://mail.gnome.org/mailman/listinfo/gtk-list


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