user3053231
user3053231

Reputation:

Changing GTK label in C using signal_connect

Hello I am making GUI in GTK I have some menu items, and I am trying to change main label after clicking a mouse on specific menu element.

widgets[i][0] = gtk_menu_item_new_with_label(arrayOfStrings[i]);
//arrayOfStrings is : char** arrayOfStrings
gtk_menu_shell_append(GTK_MENU_SHELL(indicator_menu), widgets[i][0]);

I was trying this:

void set_main_label(GtkWidget *widget)
{
    app_indicator_set_label(indicator, arrayOfString[2],arrayOfString[2]);
}

and after this I call it like:

g_signal_connect(widgets[i][0], "activate",G_CALLBACK(set_main_label), widgets[i][0]);

But my problem is that void set_main_label(void) must have void argument. And I need to pass there string (char*) which is stored in arrayOfStrings. What do you suggest? Now I can change label only to one specific string set in set_main_label function, but I cannot pass it as an argument into function, what do you suggest? .

Upvotes: 0

Views: 654

Answers (1)

ptomato
ptomato

Reputation: 57920

This is what the user_data parameter is for. set_main_label() does not have a void argument list - check the documentation:

void
user_function (GtkMenuItem *widget,
               gpointer     user_data)

You can pass any argument you like into the callback via the user_data parameter. But it must be known at the time you connect the signal.

So you could do something like this:

void
set_main_label(GtkMenuItem *widget, gpointer user_data)
{
    const char *label = (const char *)user_data;
    app_indicator_set_label(indicator, label, label);
}

g_signal_connect(widgets[i][0], "activate",
    G_CALLBACK(set_main_label), arrayOfString[2]);

Upvotes: 2

Related Questions