1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
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 );
}
|