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 

Using a submit button to get text from entry widgets...

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
JoeHughto



Joined: 26 Mar 2007
Posts: 4
Location: Geneseo, NY

PostPosted: Mon Mar 26, 2007 1:47 am    Post subject: Using a submit button to get text from entry widgets... Reply with quote

I apologize if this has been asked before but I cannot find the answer to this question:

I have a few entry widgets that I would like to get the text from when I hit the submit button. How would I do this?

I can get the text out of each entry using the "activate" signal like so:

Code: (Plaintext)
1
2
3

g_signal_connect(G_OBJECT(metalentry),"activate",G_CALLBACK(changemetallicity),(gpointer) metalentry);


where my entry widget is called metalentry and the changemetallicity function parses it.

I've tried using g_signal_connect_swapped, but I was unable to find a way to pass data.

Thanks for any help on this issue.
Back to top
openldev
Never Seen the Sunlight


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

PostPosted: Mon Mar 26, 2007 2:48 pm    Post subject: Reply with quote

If you have multiple widgets that you want to retrieve data from, let's say they are called entry1, entry2 and entry3. You first should create a structure to hold all of the widgets:

Code: (Plaintext)
1
2
3
typedef struct {
GtkWidget *entry1, *entry2, *entry3;
} Entries;


Then, do the following when you initialize the UI:

Code: (Plaintext)
1
2
3
4
5
6
7
8
9
Entries *e = g_slice_new (Entries);

e->entry1 = gtk_entry_new ();
e->entry2 = gtk_entry_new ();
e->entry3 = gtk_entry_new ();

/* perform other initialization */

g_signal_connect (e->entry1, "activate", G_CALLBACK (activate_func), e);


Then, in the callback function:

Code: (Plaintext)
1
2
3
4
5
6
static void
activate_func (GtkEntry *entry, gpointer data)
{
  Entries *e = (Entries*) data;
  /* use us. */
}


You have one other option. Let's say you have a GtkButton that is the default widget in window. You can use gtk_widget_grab_default() to make that button the default widget. Then, use gtk_entry_set_activates_default() on all of the GtkEntry widgets. This means that when any of the entry widgets are activated (without the need to connect to the activate signal), the button will be activated. This equates to the button being clicked. To handle this, use the following connection:

Code: (Plaintext)
1
g_signal_connect (button, "clicked", G_CALLBACK (button_clicked), e);


By doing this, any time the button is clicked or any of the GtkEntry widgets are activated, the button_clicked() callback function will be called, which accepts the structure full of your GtkEntry widgets.

I hope this helps!
Back to top
JoeHughto



Joined: 26 Mar 2007
Posts: 4
Location: Geneseo, NY

PostPosted: Tue Mar 27, 2007 3:28 pm    Post subject: Reply with quote

Thanks for the reply.

I am left with a few questions though.

I run into a problem with the *e declaration. I get this error message:

Code: (Plaintext)
1
2
3
4
horizon.c: In function 'main':
horizon.c:49: warning: implicit declaration of function 'g_slice_new'
horizon.c:49: error: parse error before 'Entries'
make: *** [horizon] Error 1


Any idea what's going on there?

Also, I was thinking that I could just pass the entry widgets I have now one at a time to a function, since they all have to go different places anyways, but I am getting an invalid cast from `GtkButton` to `GtkEntry`. Is there any way that I could get around this and just pass the pointer to my entry widget to the function?

Thanks for your help.
Back to top
Micah Carrick
Never Seen the Sunlight


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

PostPosted: Tue Mar 27, 2007 3:34 pm    Post subject: Reply with quote

If you are using a version of Glib prior to 2.10 then you won't have the g_slice_new function. Either upgrade Glib or use the g_malloc or g_new functions:
Code: (Plaintext)
1
2
3
4

Entries *e = g_new (Entries, 1);
/* ... */
g_free (e);
Back to top
JoeHughto



Joined: 26 Mar 2007
Posts: 4
Location: Geneseo, NY

PostPosted: Tue Mar 27, 2007 3:56 pm    Post subject: Reply with quote

Now I've run into other problems. I'll give you the relevant bits of what I have and I'd really appreciate some pointers.

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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

/* ... */
#include <glib.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

typedef struct {
GtkWidget *metalentry, *redsentry, *redientry;
} Entries;

/* ... */
float metallicity=0.00;
float reddenedinterval=0.03;
float reddenedinitial=0.00;

/* ... */
void changemetallicity(GtkEntry *entry, gpointer data);
void redstartchange (GtkEntry *entry);
void redintervalchange (GtkEntry *entry);

int main (int argc, char *argv[])
{
/* ... */
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *metalentry, *redsentry, *redientry, *rednbutton;
  char metalstring[10];
  char redsstring[10];
  char redistring[10];
  Entries *e = g_new (Entries, 1);

  gtk_init(&argc,&argv);

 /* ... */

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "Horizon Replacement");
 
/* ... */

  e->metalentry = gtk_entry_new();
  gtk_entry_set_max_length(GTK_ENTRY(e->metalentry),10);
  sprintf(metalstring,"%4.2f",metallicity);
  gtk_entry_set_text(GTK_ENTRY(e->metalentry),metalstring);
  g_signal_connect(e->metalentry,"activate",G_CALLBACK(changemetallicity),e);
  gtk_box_pack_start(GTK_BOX(box2),e->metalentry,TRUE,TRUE,0);
  gtk_widget_show(e->metalentry);

/* ... */

  e->redsentry = gtk_entry_new();
  gtk_entry_set_max_length(GTK_ENTRY(e->redsentry),10);
  sprintf(redsstring,"%4.2f\n",reddenedinitial);
  gtk_entry_set_text(GTK_ENTRY(e->redsentry),redsstring);
  g_signal_connect(G_OBJECT(e->redsentry),"activate",G_CALLBACK(redstartchange), e);
  gtk_box_pack_start(GTK_BOX(box3),e->redsentry,TRUE,TRUE,0);
  gtk_widget_show(e->redsentry);

/* ... */

  e->redientry = gtk_entry_new();
  gtk_entry_set_max_length(GTK_ENTRY(e->redientry),10);
  sprintf(redistring,"%4.2f",reddenedinterval);
  gtk_entry_set_text(GTK_ENTRY(e->redientry),redistring);
  g_signal_connect(G_OBJECT(redientry),"activate",G_CALLBACK(redintervalchange), e);
  gtk_box_pack_start(GTK_BOX(box3),e->redientry,TRUE,TRUE,0);
  gtk_widget_show(e->redientry);

/* ... */

  button = gtk_button_new_with_label("Submit");
  g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(makeplot),NULL);
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(changemetallicity),e);
  gtk_box_pack_start(GTK_BOX(box1),button,TRUE,TRUE,0);
  gtk_widget_show(button);
  gtk_widget_show(window);

/* ... */

  g_free(e);
  gtk_main();

  return 0;
}

/* ... */

static void changemetallicity(GtkEntry *metalentry, gpointer data)
{
  Entries *e = (Entries*) data;
 
  const gchar *str;

  str = gtk_entry_get_text(GTK_ENTRY(e->metalentry));

  metallicity = atof(str);
}

void redstartchange (GtkEntry *redsentry, gpointer data)
{
  Entries *e = (Entries*) data;

  const gchar *str;

  str = gtk_entry_get_text(GTK_ENTRY(e->redsentry));

  reddenedinitial = atof(str);

}

void redintervalchange (GtkEntry *redientry, gpointer data)
{
  Entries *e = (Entries*) data;

  const gchar *str;

  str = gtk_entry_get_text(GTK_ENTRY(e->redientry));

  reddenedinterval = atof(str);

}


Thanks.
Back to top
JoeHughto



Joined: 26 Mar 2007
Posts: 4
Location: Geneseo, NY

PostPosted: Tue Mar 27, 2007 8:25 pm    Post subject: Reply with quote

Well, it turns out that I'm an idiot and all I had to do was use g_signal_connect_swapped. So my issue is resolved for now. Thanks for the help and the information on structures though.
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