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
);