Re: gtk_text_buffer_delete ?
On Thu, 08 Oct 2015 20:59:21 +0200 Stefan Salewski wrote: > I was hoping that it could be possible to simple block input when buffer > has a maximum length, but I got that not working. Note that this can give an unpleasant user experience. It might be more helpful just to have a count of characters available that becomes negative and changes from grey to red (change both lightness and colour, for people who can't differentiate black from red) - you see this approach on Web sites quite often. Then people can paste a longer text and delete it, or can add a new word and then delete a longer one. Best, Liam -- Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/ Pictures from old books: http://fromoldbooks.org/ ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: gtk_text_buffer_delete ?
On Thu, 2015-10-08 at 10:30 +0200, Pierre Wieser wrote: > Oop's. Sorry ! I've clicked on the bad button !! Yes, it is sad that this trivial task is so hard, and that no one can really help. I was hoping that it could be possible to simple block input when buffer has a maximum length, but I got that not working. But deleting works at least. https://developer.gnome.org/gtk3/stable/GtkTextBuffer.html#GtkTextBuffer -insert-text >Note that if your handler runs before the default handler it must not >invalidate the location iter (or has to revalidate it). So our callback have to reset the iter! I did some test in Nim, a change to test my bindings and become some more comfortable with that language. There we have discard g_signal_connect_after(buffer, "insert-text", g_callback(clip), nil) proc clip*(buffer: TextBuffer, iter: TextIter; text: cstring; len: gint; data: gpointer) {.cdecl.} = const max = 8 var i, j: TextIterObj var cc = buffer.char_count if cc > max: buffer.get_iter_at_offset(i, cc) j = i # copy iter object discard j.backward_chars(cc - max) buffer.delete(j, i) buffer.get_iter_at_offset(iter[], max) # set iter to valid value again, value seems to be arbitray So the trick is just the last line, reset the iter callback parameter to a valid value after deletion. It is strange, I have the feeling that the callback is executed after the new text was already inserted, but in spite of that gtk complains about invalid iter. Strange. I guess you can understand the idea of that Nim code. I connected to "insert-text" signal, so no danger of recursion. I used g_signal_connect_after here, seems to make more sense when we delete last char. g_signal_connect works similar, but we seem to get one char more? Maybe Emmanuele Bassi can comment on this later... ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: gtk_text_buffer_delete ?
> - Original Message - >> From: "Stefan Salewski" >> Sent: Wednesday, October 7, 2015 7:36:05 PM > >> On Wed, 2015-10-07 at 13:14 +0200, Pierre Wieser wrote: >>> I so connected to the 'changed' signal of the GtkTextBuffer, >> >> Maybe try connecting to insert-text signal -- changed signal may be >> emitted again when you delete. See >> >> http://stackoverflow.com/questions/2791035/how-do-i-set-buffer-limit-for >> -gtk-text-view-in-c > > Humm.. I've seen this thread too, but didn't understand the proposed behavior. Oop's. Sorry ! I've clicked on the bad button !! Please, do not consider this mail, as I have to worked a bit more on it.. Thanks Pierre ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: gtk_text_buffer_delete ?
On Wed, 2015-10-07 at 13:14 +0200, Pierre Wieser wrote: > I so connected to the 'changed' signal of the GtkTextBuffer, Maybe try connecting to insert-text signal -- changed signal may be emitted again when you delete. See http://stackoverflow.com/questions/2791035/how-do-i-set-buffer-limit-for -gtk-text-view-in-c ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: gtk_text_buffer_delete ?
On Wed, 2015-10-07 at 15:05 +0200, Stefan Salewski wrote: > On Wed, 2015-10-07 at 13:14 +0200, Pierre Wieser wrote: >> Hello, >> >> 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. > > Seen that: > > http://www.gtkforums.com/viewtopic.php?t=1012 > > Very similar to your solution, but I think that was GTK2, and your is > GTK3. Hi Stefan, Thanks for your interest, but the thread you point to is actually the one on which I based my own code. I do not think that GTK2 vs GTK3 is a point in this matter, do you ? > No other idea, sorry. And Google has not been my friend further... ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: gtk_text_buffer_delete ?
On 15-10-07 04:14 AM, Pierre Wieser wrote: Hello, 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 the * buffer have been modified since the iterator was created. * You must use marks, character numbers, or line numbers to * preserve a position across buffer modifications. * You can apply tags and insert marks without invalidating * your iterators, but any mutation that affects 'indexable' * buffer contents (contents that can be referred to by character * offset) will invalidate all outstanding iterators */ gtk_text_buffer_get_iter_at_offset( buffer, &start, MAX_LENGTH-1 ); /*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 chars), 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 buffer is not modified between taking the iters and deleting the content... Can anyone help me in this matter ? Thanks in advance. Regards Pierre ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-app-devel-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); where 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|/); and 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 gtk-app-devel-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: gtk_text_buffer_delete ?
On Wed, 2015-10-07 at 13:14 +0200, Pierre Wieser wrote: > Hello, > > 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. Seen that: http://www.gtkforums.com/viewtopic.php?t=1012 Very similar to your solution, but I think that was GTK2, and your is GTK3. No other idea, sorry. ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: gtk_text_buffer_delete ?
> On the application I'm currently working on [1], I want limit the size of the > [...] I omit the application link, for reference ;) [1] https://github.com/trychlos/openbook/ ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
gtk_text_buffer_delete ?
Hello, 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 the * buffer have been modified since the iterator was created. * You must use marks, character numbers, or line numbers to * preserve a position across buffer modifications. * You can apply tags and insert marks without invalidating * your iterators, but any mutation that affects 'indexable' * buffer contents (contents that can be referred to by character * offset) will invalidate all outstanding iterators */ gtk_text_buffer_get_iter_at_offset( buffer, &start, MAX_LENGTH-1 ); /*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 chars), 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 buffer is not modified between taking the iters and deleting the content... Can anyone help me in this matter ? Thanks in advance. Regards Pierre ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list