GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Nov 21, 2014 2:15 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Using a submit button to get text from entry widgets...
PostPosted: Mon Mar 26, 2007 1:47 am 
Offline

Joined: Mon Mar 26, 2007 1:40 am
Posts: 4
Location: Geneseo, NY
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:
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.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 2:48 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
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:
typedef struct {
GtkWidget *entry1, *entry2, *entry3;
} Entries;


Then, do the following when you initialize the UI:

Code:
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:
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:
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!

_________________
Andrew Krause

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 27, 2007 3:28 pm 
Offline

Joined: Mon Mar 26, 2007 1:40 am
Posts: 4
Location: Geneseo, NY
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:
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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 27, 2007 3:34 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
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:
Entries *e = g_new (Entries, 1);
/* ... */
g_free (e);

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 27, 2007 3:56 pm 
Offline

Joined: Mon Mar 26, 2007 1:40 am
Posts: 4
Location: Geneseo, NY
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:
/* ... */
#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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 27, 2007 8:25 pm 
Offline

Joined: Mon Mar 26, 2007 1:40 am
Posts: 4
Location: Geneseo, NY
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.


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 3 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