GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Aug 23, 2014 9:28 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Hexadecimal value entry
PostPosted: Wed Oct 10, 2007 5:42 pm 
Offline
Familiar Face

Joined: Wed Oct 03, 2007 12:28 pm
Posts: 25
Location: Virginia (Northern)
Does anyone have a recommended solution (or example) for capturing user entry for hexadecimal values as is commonly used to specify hardware addresses?
I was thinking of using a modified GtkEntry or GtkSpinButton widget to do this... but it looks like a lot of work.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 10, 2007 6:57 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Do you mean you want the gchar* from a GtkEntry to be converted to a hexidecimal value?

Code:
#include <glib-2.0/glib.h>

int
main (int argc, char *argv[])
{
    g_print("0x00ff=%d\n", (guint)g_strtod("0x00ff", NULL));
    g_print("0xfafa=%d\n", (guint)g_strtod("0xfafa", NULL));
    g_print("0x1c1c1c1c1c=%d\n", (guint)g_strtod("0x1c1c1c1c1c", NULL));
   
    return 0;
}

// gcc -Wall -g `pkg-config --cflags --libs glib-2.0` -o example example.c

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject: Hexadecimal value entry
PostPosted: Wed Oct 10, 2007 7:11 pm 
Offline
Familiar Face

Joined: Wed Oct 03, 2007 12:28 pm
Posts: 25
Location: Virginia (Northern)
Thanks for the quick response...
Actually, I am looking for a widget to accept (and manage) entry of hexadecimal values. Preferably with a static prefix ("0x" or even '#').


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 10, 2007 7:46 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
If you want a multi-lined hex editor widget, you will have to use GtkTextView. GtkEntry will work for a single lined hex editor. However, there aren't any widgets currently for this. (If you give a more detailed description of what exactly you want to do with this, I can point you in the right direction.)

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject: Hexadecimal value entry
PostPosted: Mon Oct 29, 2007 1:34 pm 
Offline
Familiar Face

Joined: Wed Oct 03, 2007 12:28 pm
Posts: 25
Location: Virginia (Northern)
Actually, I was simply looking for a GtkEntry widget that manages hexadecimal entry. I was able to find a good example of using the "key-press-event" in your book, "Foundations of GTK+ Development" which helped me to quickly resolve this. (I should note that I wanted to preserve a '0x' prefix on the entry field of the widget.)
By the way... I've purchased a LOT of technical books over the past 20 years of my career and this one is among the better written. Have you considered tackling more advanced topics in a sequel?

Here is the code which manages hexadecimal input for a GtkEntry widget:

Code:
gboolean validate_hex_entry
(
   GtkEntry     *entry,
   GdkEventKey  *event
)
{
   gboolean  status = TRUE;

   guint keyval = event->keyval;

   /*
    * Get the current entry value and string length
    */
   const gchar *current_text = gtk_entry_get_text( entry );
   size_t entry_length = strlen(current_text);

   /*
    * If the key a valid hexadecimal diget then append it to the entry
    */
   if (  g_ascii_isxdigit( keyval ) )
   {
      /*
       * Limit the number of caharacters to 10 characters
       * 0x00000000 through 0xFFFFFFFF
       */
      if ( entry_length < 10 )
      {
         status = FALSE;
      }
   }
   else if ( keyval == GDK_BackSpace )
   {
      if ( entry_length > 2 ) /* preserve "0x" hex prefix */
      {
         gchar *new_text = g_strndup( current_text, entry_length );
         gtk_entry_set_text( entry, new_text );
         g_free( new_text );
         gtk_entry_set_position( entry, entry_length );
         status = FALSE;
      }
   }
   else if ( ( keyval == GDK_Return ) || ( keyval == GDK_KP_Enter ) )
   {
      gtk_widget_activate( GTK_WIDGET( entry ) );
      status = FALSE;
   }
   return status;

} /* validate_hex_entry */


I then set up the callback as follows:
Code:
   g_signal_connect
     (
        G_OBJECT( widget ), "key-press-event",
        G_CALLBACK( validate_hex_entry ),
        NULL
     );


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 4:08 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
I currently wasn't planning a sequel just because it's senior year of college and I'm so very busy. However, I am writing articles on other concepts that weren't covered in the book, so if you have any suggestions, just let me know.

Also, when you are done reading the book, I would appreciate if you took a moment to review it at Amazon or B&N if you enjoyed it! :)

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group