Re: GtkEditable and utf8 question



On Sat, 2005-11-19 at 08:35 +0100, Vincent Torri wrote:
> Hello,
> 
> I would like to force an entry to insert only digit characters. I have
> tried to modify the insert_text_handler example that is in the GtkEditable
> API. The result is:
> 
> static void
> _insert_numeric (GtkEditable *editable,
>                  const gchar *text,
>                  gint         length,
>                  gint        *position,
>                  gpointer     data)
> {
>   gint i;
>   gint j;
>   gchar *result;
> 
>   result = (gchar *)g_malloc (sizeof (gchar) * (length + 1));
>   if (!result)
>     return;
> 
>   for (i = 0, j = 0; i < length; i++)
>     {
>       if (g_ascii_isdigit (text[i]))
>         {
>           result[j] = text[i];
>           j++;
>         }
>     }
>   result[j] = '\0';
> 
>   g_signal_handlers_block_by_func (editable,
> 				   (gpointer) _insert_numeric, data);
>   gtk_editable_insert_text (editable, result, length, position);
>   g_signal_handlers_unblock_by_func (editable,
>                                      (gpointer) _insert_numeric, data);
> 
>   g_signal_stop_emission_by_name (editable, "insert-text");
> 
>   g_free (result);
> }
> 
> but, each time I insert something in the entry, I get the following
> warning:
> 
> (avisynth_test:13311): Pango-WARNING **: Invalid UTF-8 string passed to
> pango_layout_set_text()
> 
> I know almost nothing about utf8 and pango. I've tried to use the g_utf8*
> functions, but, in the best case (when it works a bit), I always have that
> warning.
> 
> Can someone tell me what is the correct way of inserting digit char ?

I would recommend this method, it reads a little cleaner and is better
than calling gtk_editable_insert_text() with blocking functions.

static void
insert_text_cb (GtkEditable *editable,
                gchar       *new_text,
                gint         len,
                gint        *position,
                gpointer     user_data)
{
        gint i;

        for (i = 0; i < len; ++i) {
                gchar *ch = new_text + i;
                if (!isdigit (*ch)) {
                        g_signal_stop_emission_by_name (editable,
                                                        "insert-text");
                        return;
                }
        }
}

-- 
Regards,
Martyn




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