GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Aug 29, 2014 4:03 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: GtkTextView: Setting max length (Solved!)
PostPosted: Tue Jan 29, 2008 8:53 pm 
Offline
Familiar Face

Joined: Wed Oct 03, 2007 12:28 pm
Posts: 25
Location: Virginia (Northern)
Is there a way to limit the length of a GtkTextView (similar to what can be done with the GtkEntry's max-length property)?
I'm trying to provide a textual editing widget so that the user can enter multi-line text for a fixed number of characters.
If there is a more appropriate widget that I have overlooked, please feel free to point it out!
Note: I tried to handle this with the GtkTextView, but it was ugly since the text to be entered is long relative to the space available on my window.


Last edited by kharrison on Thu Feb 14, 2008 3:11 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: GtkTextView: Setting max length (Solved!)
PostPosted: Thu Feb 14, 2008 3:07 am 
Offline
Familiar Face

Joined: Wed Oct 03, 2007 12:28 pm
Posts: 25
Location: Virginia (Northern)
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group