Abdizriel
Abdizriel

Reputation: 355

GTK C - Passing multiple variables using g_signal_connect

So, I'm trying to achieve the following:

The user is choosing file, and path to that file is saved in variable filename. After choosing file user is selecting by checkbox which chmod want to set.

I have something like this:

g_signal_connect (ux, "toggled",G_CALLBACK(user_read_only), (gpointer *)ux);

and function user_read_only:

void user_read_only(GtkWidget *widget, gpointer *data)
{
   if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data))){
       int stat;
       stat = chmod(filename, S_IRUSR);
       printf("Added Read attribute to user in file: %s\n", filename);
   } else {
      printf("No Read attribute to user in file: %s\n", filename);
   }
}

My question is: How I can pass filename to my callback function? When i try:

g_signal_connect (ux, "toggled",G_CALLBACK(user_read_only), (gpointer *)ux,filename);

I got error that I can pass only 1 variable.

Upvotes: 2

Views: 3783

Answers (1)

yaman
yaman

Reputation: 769

That data parameter of gpointer type is for you to pass whatever type of data you want to pass onto your callback.

g_signal_connect (ux, "toggled",G_CALLBACK(user_read_only), (gpointer *)filename);

Should do the trick. You do not need to pass your GTK instance (ux) as data. You also need to change the function as:

void user_read_only(GtkWidget *widget, gpointer *data)
{
   if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))){
       int stat;
       stat = chmod(data, S_IRUSR);
       printf("Added Read attribute to user in file: %s\n", (char *)data);
   } else {
      printf("No Read attribute to user in file: %s\n", (char *)data);
   }
}

Upvotes: 2

Related Questions