GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

widget selection

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
shri_desai09
Familiar Face


Joined: 24 Jul 2006
Posts: 7

PostPosted: Mon Jul 24, 2006 12:33 pm    Post subject: widget selection Reply with quote

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,
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 376
Location: State College, Pennsylvania

PostPosted: Mon Jul 24, 2006 3:49 pm    Post subject: Reply with quote

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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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?
Back to top
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 427
Location: Portland, OR USA

PostPosted: Mon Jul 24, 2006 4:27 pm    Post subject: Reply with quote

Sort of off topic--I appologize--but can you let us know when your book is out and what it's titled?
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 376
Location: State College, Pennsylvania

PostPosted: Mon Jul 24, 2006 6:09 pm    Post subject: Reply with quote

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.
Back to top
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 427
Location: Portland, OR USA

PostPosted: Mon Jul 24, 2006 7:38 pm    Post subject: Reply with quote

Very cool.
Back to top
shri_desai09
Familiar Face


Joined: 24 Jul 2006
Posts: 7

PostPosted: Wed Jul 26, 2006 6:06 am    Post subject: Reply with quote

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.
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 376
Location: State College, Pennsylvania

PostPosted: Wed Jul 26, 2006 1:42 pm    Post subject: Reply with quote

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. :)
Back to top
shri_desai09
Familiar Face


Joined: 24 Jul 2006
Posts: 7

PostPosted: Thu Jul 27, 2006 12:35 pm    Post subject: Reply with quote

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.
Back to top
shri_desai09
Familiar Face


Joined: 24 Jul 2006
Posts: 7

PostPosted: Tue Aug 01, 2006 12:05 pm    Post subject: hi Reply with quote

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.
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 376
Location: State College, Pennsylvania

PostPosted: Tue Aug 01, 2006 12:22 pm    Post subject: Reply with quote

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: (Plaintext)
1
2
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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
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!
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP