[Glade-users] How to block some input in GtkEntry field

Arthur Shats ashats at hotmail.com
Wed Aug 25 19:38:39 EDT 2010


Thank you guys for a very quick response. Please take a look at this code that I found under library.gnome.org. This code forces all characters to uppercase, and limits the range of characters to A-Z. I am doing very similar thing, only need to make sure I permit only digits and '.' (dots) at certain places so to validate an IP address entry. However, I don't understand a few things here.
 
void insert_text_handler (GtkEntry    *entry,
                          const gchar *text,
                          gint         length,
                          gint        *position,
                          gpointer     data)
{
  GtkEditable *editable = GTK_EDITABLE(entry);
  int i, count=0;
  gchar *result = g_new (gchar, length);

  for (i=0; i < length; i++) {
    if (!isalpha(text[i]))
      continue;
    result[count++] = islower(text[i]) ? toupper(text[i]) : text[i];
  }
  
  if (count > 0) {
    g_signal_handlers_block_by_func (G_OBJECT (editable),
                                     G_CALLBACK (insert_text_handler),
                                     data);
    gtk_editable_insert_text (editable, result, count, position);
    g_signal_handlers_unblock_by_func (G_OBJECT (editable),
                                       G_CALLBACK (insert_text_handler),
                                       data);
  }
  g_signal_stop_emission_by_name (G_OBJECT (editable), "insert_text");

  g_free (result);
}

What exactly are we blocking/unblocking with the calls to g_signal_handlers_block_by_func and _unblock_by_func and why we need to do this?
Also, since we are typing char by char, the text argument seem to contain only one char at a time and the length arg is 1 and so the result string is going to have only one char, so why do we need to have a loop?
 
I appreciate your help.
Thanks,
Arthur.

 
> From: tadeboro at gmail.com
> Date: Wed, 25 Aug 2010 22:39:32 +0200
> Subject: Re: [Glade-users] How to block some input in GtkEntry field
> To: axischire at gmail.com
> CC: ashats at hotmail.com; glade-users at lists.ximian.com
> 
> Hi.
> 
> Validation should be done prior insertion (so you actually prevent
> character from being inserted) like Cristobal said. You can do that by
> connecting to GtkEditable::insert-text signal. If the input is
> invalid, you simply stop emission of this signal using
> g_signal_stop_emission_by_name(), which will cause default handler to
> be skipped and text ignored.
> 
> Tadej
> 
> 
> -- 
> Tadej Borovšak
> tadeboro.blogspot.com
> tadeboro at gmail.com
> tadej.borovsak at gmail.com
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/glade-users/attachments/20100825/7ba0ec28/attachment-0001.html 


More information about the Glade-users mailing list