tesseract
tesseract

Reputation: 279

c - Passing multiple arguments to a callback function in GTK

So, I'm trying to achieve the following: The user shall be able to fill out multiple gtk_entry's and click Apply after that, on being clicked I want the Apply button to emit a signal, something like this:

g_signal_connect (G_OBJECT (Apply), "clicked", G_CALLBACK(apply_clicked), # an argument #);

Afterwards, in apply_clicked(), I want the entered text to be saved.

My question is: How do I pass those gtk_entry's to my callback function apply_clicked? If it were only one I'd just set it as # an argument #, but what should I do with multiple entries ?

Upvotes: 4

Views: 6097

Answers (2)

jstedfast
jstedfast

Reputation: 38528

The typical way of doing this is to do:

g_object_set_data (context_object, "entry1", entry1);
g_object_set_data (context_object, "entry2", entry2);

g_signal_connect (G_OBJECT (Apply), "clicked", G_CALLBACK (apply_clicked), context_object);

and then in apply_clicked:

GtkEntry *entry1 = g_object_get_data (context_object, "entry1");
...

Usually the context_object will be the GtkDialog or whatever these widgets exist on.

Alternatively, if you subclass the GtkDialog, you can do:

struct _MyDialog {
    GtkDialog parent_object;
    GtkEntry *entry1;
    GtkEntry *entry2;
    ...
};

Then, when constructing your dialog, just set entry1, 2, 3, etc... and you don't need to use the g_object_[g,s]et_data() hack.

Upvotes: 5

Evan Teran
Evan Teran

Reputation: 90422

create a data structure (a linked list perhaps) to contain pointers to the gtk_entrys and pass that instead. Or better yet, why not just pass a pointer to the object which contains all of thise gtk_entrys?

Upvotes: 1

Related Questions