Ok, after thinking about this and experimenting quite a bit, I came up with the following solution to this problem. In the event that someone else can benefit from my labors, I'll go ahead and post the ('C' pseudo code) solution here:
Basically, my solution was to establish a callback function whenever the associated GtkTextBuffer changed as a result of user input. Upon each character entry, the callback checks the length of the buffer. If the length exceeds the desired value (e.g. MAX_BUFFER_LENGTH - 1 : which leaves room for a NULL termination) then I remove the extra text within the buffer. The net effect is that it appears to the user that no more characters can be entered. (I also output the remaining characters and a message telling the user that the maximum length has been reached.)
So... in a nutshell, here it is:
Code:
void create_textview()
{
/* Create the GtkTextView */
GtkTextView *textview = gtk_text_view_new(); // I actually created mine via Glade
/* Get the buffer from the GtkTextView */
GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(textview) );
/* Connect a callback to the "changed" signal associated with the buffer */
g_signal_connect
(
G_OBJECT( buffer ),
"changed",
G_CALLBACK( textview_buffer_changed_callback ),
info
);
}
void textview_buffer_changed_callback
(
GtkTextBuffer *buffer,
gpointer user_data
)
{
/*
* Get the current length of the buffer
*/
int remaining = MAX_BUFFER_LENGTH - 1;
char text[80];
if ( remaining > 0 )
{
sprintf( text, "%d chars remaining", remaining );
}
else
{
sprintf( text, "Max chars reached" );
/*
* If the maximum buffer length has been exceeded, then erase the last
* character entered
*/
if ( remaining < 0 )
{
GtkTextIter start;
GtkTextIter end;
gtk_text_buffer_get_iter_at_offset( buffer,
&start,
MAX_BUFFER_LENGTH - 1 ); /* leave room for NULL */
gtk_text_buffer_get_iter_at_offset( buffer,
&end,
MAX_BUFFER_LENGTH );
gtk_text_buffer_delete( buffer, &start, &end );
}
}
/* print text to a previously created GtkLabel widget */
gtk_label_set_text( GTK_LABEL( buffer_label ), text );
}
Perhaps this should now be moved to the Example Code forum... I'm not sure exactly how to go about that... so I leave that to a moderator.
By the way... if you don't have Andrew Krause's book "Foundations of GTK+ Development" go buy it! I wasn't able to find the solution to this particular problem, but it has saved my skin on multiple other occasions.