GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sun Dec 21, 2014 4:05 am

All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: widget selection
PostPosted: Mon Jul 24, 2006 12:33 pm 
Offline
Familiar Face

Joined: Mon Jul 24, 2006 12:01 pm
Posts: 7
hello all,
i am new to GTK actually, but am trying to develop a UI with GTK on linux.
I wanted to know, whether any widget is available in GTK which can allow me to enter the IP address in the format its normally entered, ex: 195. 156. 15. 46 (dotted decimal)
and that i should be allowed to enter only numbers.

awaiting for ur replies,


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 24, 2006 3:49 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
There would be a few ways of doing this. The first would be to use 4 spin buttons, but this would be a copout way and wouldn't look nice.

The second would be to just use one GtkEntry that restricts the input by using the "changed" signal of the GtkEditable implementation. You could even derive a new widget to do just this.

The last way, which I personally would do would be to create 4 entries that don't have borders. Place 3 GtkLabels between them. It would be done as such:

Code:
GtkWidget *entry1, *entry2, *entry3, *entry4, *label1, *label2, *label3, *hbox;

entry1 = gtk_entry_new_with_max_length (3);
entry2 = gtk_entry_new_with_max_length (3);
entry3 = gtk_entry_new_with_max_length (3);
entry4 = gtk_entry_new_with_max_length (3);

label1 = gtk_label_new (".");
label2 = gtk_label_new (".");
label3 = gtk_label_new (".");

gtk_entry_set_has_frame (GTK_ENTRY (entry1), FALSE);
gtk_entry_set_has_frame (GTK_ENTRY (entry2), FALSE);
gtk_entry_set_has_frame (GTK_ENTRY (entry3), FALSE);
gtk_entry_set_has_frame (GTK_ENTRY (entry4), FALSE);

hbox = gtk_hbox_new (FALSE, 1);
gtk_box_pack_start_defaults (GTK_BOX (hbox), entry1);
gtk_box_pack_start_defaults (GTK_BOX (hbox), label1);
gtk_box_pack_start_defaults (GTK_BOX (hbox), entry2);
gtk_box_pack_start_defaults (GTK_BOX (hbox), label2);
gtk_box_pack_start_defaults (GTK_BOX (hbox), entry3);
gtk_box_pack_start_defaults (GTK_BOX (hbox), label3);
gtk_box_pack_start_defaults (GTK_BOX (hbox), entry4);


Then use the insert-at-cursor signal of the GtkEntry to restrict it to only digits. Of course, like I said before, making a custom widget would probably be the best thing to do, but that is your choice.

You could integrate it even better by setting the background color of every widget as the same color & then placing a black one pixel border around the hbox in a custom widget.

This is actually a really good idea for an example. I am currently working on a book on GTK+ programming that will be coming out in a few months. Would you give me permission to use this idea for an example in the book in the custom widgets chapter?

_________________
Andrew Krause

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 24, 2006 4:27 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Sort of off topic--I appologize--but can you let us know when your book is out and what it's titled?

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 24, 2006 6:09 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
It's going to be published March 2007, called Complete Linux: GTK+ Development. I'll give you a more precise date when I know and of course, I'll post a thread here when it's out.

_________________
Andrew Krause

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 24, 2006 7:38 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Very cool.

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 26, 2006 6:06 am 
Offline
Familiar Face

Joined: Mon Jul 24, 2006 12:01 pm
Posts: 7
Thanks Andrew for the above solutions. I could'nt get the idea about ur second solution about "change" signal. Can please put some light on the same.
When is your book releasing by the way?
Thnaks a lot once again,
Regards,
Shri.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 26, 2006 1:42 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
Actually, I misspoke on that. What you could do is set the default text of the entry to " . . . ", where there are spaces. Then, connect the entry to key-press-event with the g_signal_connect() function.

Then, when a key is pressed, check if it is a digit. It it is a digit, replace the space with the number. If it is delete or backspace, take appropriate action. Ignore all other keys. Then, make sure you return TRUE from the callback function so GTK+ will take no further action. Otherwise, GTK+ would continue to carry out the key-press-event action.

Being that you are new to GTK+, I would recommend using the third solution, however. The second solution requires that you are familiar with a lot of things a beginner probably wouldn't know yet. But then again, it would be a good learning experience.

Oh, and my book will be out in March of 2007. :)

_________________
Andrew Krause

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 12:35 pm 
Offline
Familiar Face

Joined: Mon Jul 24, 2006 12:01 pm
Posts: 7
Thanks andrew, i am trying on ur given solution. And i am able to get a few thing working too. Thank u once again.
Regards,
Shri.


Top
 Profile  
 
 Post subject: hi
PostPosted: Tue Aug 01, 2006 12:05 pm 
Offline
Familiar Face

Joined: Mon Jul 24, 2006 12:01 pm
Posts: 7
Hi Andrew,
i have done the boxes for entering the dotted decimal format (for IP address) numbers, as suggested by u.
but how do i get the entered numbers for checking whether they are vaild or not?
i tried this by using the API "gchar* gtk_entry_get_text (GtkEntry *entry) ", but since i have made the entry length as three the first time it returns me the single character which is entered, the 2nd time it returns me 2 characters, n the third time 3. how do i get each entry discretely, so that i can check for its validity?

Please suggest some method to receive each entered numbers discretely, so that i can check each one of it for its validity?
thanks in advance
regards,
Shri.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 01, 2006 12:22 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
Use key-press-event. This first bit of code connects the entry to the key-press-event. (dash and underscore characters are interchangeable in g_signal_connect())

Code:
g_signal_connect (G_OBJECT (entry), "key_press_event",
                  G_CALLBACK (key_press), NULL);


Then, in the callback function, check to see what the GDK key value of the key is. If it is one of the ten digit keys or one of the keypad digit keys, return FALSE. When you return false from a GDK event callback function, the GTK+ continues to handle the event. By returning TRUE if the key is not a number, GTK+ will take no further action.

Code:
static void
key_press (GtkWidget *entry, GtkEventKey *event, gpointer data)
{
  if ((event->keyval >= GDK_0 &&
       event->keyval <= GDK_9) ||
      (event->keyval >= GDK_KP_0 &&
       event->keyval <= GDK_KP_9))
    return FALSE;

  return TRUE;
}


I didn't check this code for syntax errors, so it may not compile cleanly, but the concepts should all be there. I hope this helps!

_________________
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  [ 10 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group