Re: restricting data entry (filters?)



> Carl B. Constantine wrote:
> > I posted a note a while back about adding an "input filter" to a
> > GtkEntry widget. I recieved a couple of responces, but I don't think
> > they are what I need.
> > 
> > I want the user to only be able to type numbers in a GtkEntry field. I
> > don't want to have to check the value after the fact. One person came up
> > with declaring the field cast as an INT, that may work, but I have no
> > idea how to hook that into Glade (which is generating my source at
> > present) and thus into my app.
> > 
> > Does anyone have other ideas on how to do what I want?
> > 
> > Thanks.
> > 
> 
> You could connect a callback to the gtk_entry key-press-event that
> monitors the typing and blocks any invalid characters with
> gtk_entry_set_text(); (haven't tried it)


attach the following callback to the "insert-text" signal of your gtkentry
: (it is a modification of the example given in the api of the
gtkeditable)


void
insert_digits (GtkEditable *editable,
	       const gchar *text,
	       gint         length,
	       gint        *position,
	       gpointer     data)
{
  gint i, j;
  gchar *result = g_new (gchar, length);

  j = 0;
  for (i = 0; i < length; i++)
    {
      if (g_ascii_isdigit (text[i]))
	{
	  result[j] = text[i];
	  j++;
	}
    }

  g_signal_handlers_block_by_func (G_OBJECT (editable), 
                                   G_CALLBACK (insert_digits), 
				   (gpointer)data);
  gtk_editable_insert_text (editable, result, j, position);
  g_signal_handlers_unblock_by_func (G_OBJECT (editable), 
                                     G_CALLBACK (insert_digits), 
				     (gpointer)data);

  g_signal_stop_emission_by_name (G_OBJECT (editable), "insert_text"); 

  g_free (result);
}

regards

Vincent TORRI






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]