GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

GtkTextView: Setting max length (Solved!)

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
kharrison
Familiar Face


Joined: 03 Oct 2007
Posts: 25
Location: Virginia (Northern)

PostPosted: Tue Jan 29, 2008 8:53 pm    Post subject: GtkTextView: Setting max length (Solved!) Reply with quote

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
Back to top
kharrison
Familiar Face


Joined: 03 Oct 2007
Posts: 25
Location: Virginia (Northern)

PostPosted: Thu Feb 14, 2008 3:07 am    Post subject: GtkTextView: Setting max length (Solved!) Reply with quote

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: (C)
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 );

}


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.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP