Like Tree6Likes
  • 3 Post By QuantumPete
  • 2 Post By QuantumPete
  • 1 Post By QuantumPete

How to give GtkEntry mask !

This is a discussion on How to give GtkEntry mask ! within the C Programming forums, part of the General Programming Boards category; Hello friends, Please tell me the solution to give masking to GtkEntry. I mean to say, How can I allow ...

  1. #1
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43

    How to give GtkEntry mask !

    Hello friends,

    Please tell me the solution to give masking to GtkEntry.

    I mean to say, How can I allow only letters or only alphabets in a GtkEntry widget.

    Thanks in advance.

  2. #2
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43
    bump

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    How about using Google?

    It's amazingly useful, within seconds I found this!
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43
    Ok,

    Thanks I understood. But still there's a problem, I want floating numbers:

    I replaced the code in the above link with isdigit, worked awesome, but when i am using isfloat its not working.

    isfloat function is not there ???

  5. #5
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43
    I am unable to give float masking in this code:
    Code:
    void
    on_txtSamekitKar_insert_text(GtkEditable *editable, gchar *text, 
        gint length, gpointer position, gpointer data)
    {
        int i, count = 0;
        gchar *result = g_new(gchar, length);
        
        for (i = 0; i <= length; i++)
        {
        if (!isdigit(text[i]))
          continue;
        result[count++] = text[i];
        }
    
      if (count > 0)
      {
        g_signal_handlers_block_by_func (G_OBJECT(editable), G_CALLBACK(on_txtSamekitKar_insert_text), data);
        gtk_editable_insert_text(editable, result, count, position);
        g_signal_handlers_unblock_by_func (G_OBJECT(editable), G_CALLBACK(on_txtSamekitKar_insert_text), data);
      }
      
      g_signal_stop_emission_by_name(G_OBJECT (editable), "insert_text");
    
      g_free (result);
    }
    Please help.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Ravi Raj View Post
    Please help.
    We're heading down the slippery slope where you don't engage your brain and expect me to breadcrumb you to the answer...

    There is no isfloat function that works on a char, because a char can't hold a floating point number (you should know this!). Instead you have two options:
    1) Check each character whether it's either a digit or the only other character allowed in a floating point number (work this out yourself)
    2) Try to convert the entire string to a float (by using a standard library function) and see whether you get a successful result or not
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43
    Thanks QuantumPete,

    I got it to work with this code, I don't know whether it is right logic or not but it is working:

    Code:
    void
    on_txtSamekitKar_insert_text(GtkEditable *editable, gchar *text, 
        gint length, gpointer position, gpointer data)
    {
        int i, count = 0;
        gchar *result = g_new(gchar, length);
        
        for (i = 0; i <= length; i++)
        {
        if (!isdigit(text[i]) && text[i]!='.')
          continue;
        result[count++] = text[i];
        }
    
      if (count > 0)
      {
        g_signal_handlers_block_by_func (G_OBJECT(editable), G_CALLBACK(on_txtSamekitKar_insert_text), data);
        gtk_editable_insert_text(editable, result, count, position);
        g_signal_handlers_unblock_by_func (G_OBJECT(editable), G_CALLBACK(on_txtSamekitKar_insert_text), data);
      }
      
      g_signal_stop_emission_by_name(G_OBJECT (editable), "insert_text");
    
      g_free (result);
    }

  8. #8
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43
    The above code is capable of printing double (.)s but in a float there must be only single (.), how can I implement this. Please help.

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Seriously... spoon-feeding...
    How would you go about ensuring that an action is carried out only once? Perhaps some state variable that can be checked via an if condition? When you encounter a "." set the state variable and then discard the "." from then on if it is encountered again?
    Or, you could try to convert the entire contents in one go via an in-built function, rather than attempting to rewrite it yourself from scratch.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  10. #10
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43
    Thanks quantumpete for the reply,
    That's what I want to say that I am really a newbie to c, will you please tell me that built-in function.

    Make a spoon feed once more... please !

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I'm going to do the whole Let Me Google That For You thing once more... and you'll find from the results that there are even two methods for doing it.
    rags_to_riches likes this.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  12. #12
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43

    Exclamation Thanks, but still some difficulties !

    Ok,

    Finally I got this to work but still some difficulties:

    Code:
     gboolean
    on_txt_demandno_focus_out_event (GtkWidget *widget, GdkEvent *event, gpointer data)
    {
        gdouble output;
        gchar *end;
        const gchar *text;
    
        text = gtk_entry_get_text (GTK_ENTRY (Ledger_Fields.txt_demandno));
        
        output = strtod (text, &end);
        (void) output;
    
        if ((end == text) && (strcmp (text, "")!=0))
        {
            display_message (Application.window, "Invalid entry found.", GTK_MESSAGE_ERROR);
            gtk_widget_grab_focus (Ledger_Fields.txt_demandno);
            return false;
        }    
    
        return true;
    }
    1. When I am entering any decimal or float value in txt_demandno field then its ok, the control goes to next field.
    2. When I am entering 2s or 345s or something the control goes to the next field, why so?
    3. But, when I am entering s2 or s345 the control is sticking on the same field.

    Here, point 1 and 3 are correct but why point no. 2 is not going correctly. Please help. Second thing if I am giving this code, the program is exiting showing error: SEGMENTATION FAULT [CORE DUMPED].

    Code:
    gboolean
    on_txt_demandno_focus_out_event (GtkWidget *widget, GdkEvent *event, gpointer data)
    {
        gdouble output;
        gchar *end;
        const gchar *text;
        
        text = gtk_entry_get_text (GTK_ENTRY (Ledger_Fields.txt_demandno));
    
        output = strtod (text, &end);
        (void) output;
        
        if (isalpha (text))
        {
            display_message (Application.window, "Invalid entry found.", GTK_MESSAGE_ERROR);
            gtk_widget_grab_focus (Ledger_Fields.txt_demandno);
            return false;
        }
        else
        {
            if ((end == text) && (strcmp (text, "")!=0))
            {
                display_message (Application.window, "Invalid entry found.", GTK_MESSAGE_ERROR);
                gtk_widget_grab_focus (Ledger_Fields.txt_demandno);
                return false;
            }
        }    
    
        return true;
    }
    Last edited by Ravi Raj; 05-31-2012 at 12:16 PM.

  13. #13
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43
    At last, I don't know the logic is correct or not but this code is working fine:

    Code:
    gchar*
    convert_value (const gchar *value, gchar type)
    {
        gchar *str;
        gchar buff[strlen(value)];
        gchar *fstr = malloc (sizeof buff);
        gint dvalue; gdouble fvalue; glong lvalue;
        
        switch (type)
        {
            case 'd':
                dvalue = strtod (value, &str);
                sprintf (buff, "%d", dvalue);
                break;
            case 'f':
                fvalue = strtof (value, &str);
                sprintf (buff, "%.2f", fvalue);
                break;
            case 'l':
                lvalue = strtol (value, &str, 0);
                sprintf (buff, "%ld", lvalue);
                break;
            default:
                strcpy (buff, value);
        }
        
        strcpy (fstr, buff);
        
        return fstr;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to mask a GtkEntry, only for float numbers ?
    By Ravi Raj in forum C Programming
    Replies: 0
    Last Post: 05-19-2012, 12:41 PM
  2. Mask Password
    By ethanhayon in forum C Programming
    Replies: 1
    Last Post: 07-06-2011, 03:36 PM
  3. C++ Password mask
    By nick753 in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2010, 01:05 AM
  4. bit shifting a mask
    By detfella in forum C++ Programming
    Replies: 1
    Last Post: 02-25-2004, 04:13 PM
  5. Mask
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-23-2002, 02:16 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21