Search code examples
cgtkgtk3gtk2

Gtk2 Get data from 2 entries with the same button


So, I'm new to gtk and I'm trying to make a login form and I need username and password to be send both when button "Submit" is pressed. How can I do this? My code:

const void button_clicked (GtkWidget *widget,gpointer data)
{
    const gchar* text;
    text = gtk_entry_get_text(GTK_ENTRY(data));
    write(lala,text,100);
}
GtkWidget*window,*label_username,*label_password,*layout,*entry_username,*entry_password,*submit_button;
entry_username = gtk_entry_new();
entry_password = gtk_entry_new();
submit_button = gtk_button_new_with_label("Sumbit");
g_signal_connect(submit_button,"clicked",G_CALLBACK(button_clicked),entry_username;

How can I make the submit_button function to recive text from entry_username and entry_password? Thanks!


Solution

  • In gtk for situations like this it would be best to pass a structure which contains the widget's as members to the callback function...

    typedef struct MainWindow 
    {
    
    GtkWidget *entry_username, *entry_password;
    
    }MAINWINDOW;
    
    int main(
    MAINWINDOW *mainwindow = malloc(sizeof(MAINWINDOW));
    
    mainwindow->entry_username = gtk_entry_new();
    mainwindow->entry_password = gtk_entry_new();
    
    
    g_signal_connect(submit_button,"clicked",G_CALLBACK(button_clicked),mainwindow);
    }
    
    const void button_clicked (GtkWidget *widget,gpointer data)
    {
        MAINWINDOW *mainwindow = data;
        //now you have both the widget's 
            // modify em as per your wish :)
    }
    

    this is a very rough example. but something like this should work for your case.