Re: State actions and Glade
- From: <cecashon aol com>
- To: voidtrance gmail com
- Cc: gtk-app-devel-list gnome org
- Subject: Re: State actions and Glade
- Date: Fri, 5 Oct 2018 20:44:26 +0000 (UTC)
I don't know if this is of any help. Another try at it. This one uses a GtkApplication. Setup an action on
the button along with the standard "clicked" callback. A menu in there also.
Eric
//gcc -Wall toolbar2.c -o toolbar2 `pkg-config --cflags --libs gtk+-3.0`
#include<gtk/gtk.h>
static void quit_program(GSimpleAction *action, GVariant *parameter, gpointer user_data)
{
g_application_quit(G_APPLICATION(user_data));
}
static void test1_callback(GSimpleAction *action, GVariant *parameter, gpointer user_data)
{
g_print("Test1 Callback\n");
}
static void radio_test1(GtkWidget *button, GtkToolItem **radios)
{
gint i=0;
for(i=0;i<3;i++)
{
if(gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(radios[i])))
{
g_print("test1 radio%i\n", i+1);
}
}
}
static void radio_test2(GSimpleAction *action, GVariant *parameter, GtkToolItem **radios)
{
gint i=0;
for(i=0;i<3;i++)
{
if(gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(radios[i])))
{
g_print(" test2 radio%i\n", i+1);
}
}
}
static void app_startup(GtkApplication *app, gpointer user_data)
{
//Setup menu callbacks.
GSimpleAction *test1=g_simple_action_new("test1", NULL);
g_signal_connect(test1, "activate", G_CALLBACK(test1_callback), app);
g_action_map_add_action(G_ACTION_MAP(app), G_ACTION(test1));
g_object_unref(test1);
GSimpleAction *quit=g_simple_action_new("quit", NULL);
g_signal_connect(quit, "activate", G_CALLBACK(quit_program), app);
g_action_map_add_action(G_ACTION_MAP(app), G_ACTION(quit));
g_object_unref(quit);
//Setup menu.
GMenu *menu=g_menu_new();
GMenu *submenu=g_menu_new();
g_menu_append_submenu(menu, "Application", G_MENU_MODEL(submenu));
GMenu *section=g_menu_new();
g_menu_append_section(submenu, NULL, G_MENU_MODEL(section));
g_menu_append(section, "Test1", "app.test1");
g_menu_append(section, "Quit", "app.quit");
g_object_unref(submenu);
g_object_unref(section);
gtk_application_set_menubar(GTK_APPLICATION(app), G_MENU_MODEL(menu));
g_object_unref(menu);
}
static void app_activate(GtkApplication *app, GtkToolItem **radios)
{
GtkWidget *window=gtk_application_window_new(GTK_APPLICATION(app));
gtk_window_set_title(GTK_WINDOW(window), "Toolbar2");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 100);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(window), 20);
GtkToolItem *radio1=gtk_radio_tool_button_new(NULL);
gtk_tool_item_set_expand(radio1, TRUE);
gtk_tool_button_set_label(GTK_TOOL_BUTTON(radio1), "radio1");
GtkToolItem *radio2=gtk_radio_tool_button_new_from_widget(GTK_RADIO_TOOL_BUTTON(radio1));
gtk_tool_item_set_expand(radio2, TRUE);
gtk_tool_button_set_label(GTK_TOOL_BUTTON(radio2), "radio2");
GtkToolItem *radio3=gtk_radio_tool_button_new_from_widget(GTK_RADIO_TOOL_BUTTON(radio1));
gtk_tool_item_set_expand(radio3, TRUE);
gtk_tool_button_set_label(GTK_TOOL_BUTTON(radio3), "radio3");
radios[0]=radio1;
radios[1]=radio2;
radios[2]=radio3;
GtkWidget *toolbar=gtk_toolbar_new();
gtk_widget_set_hexpand(toolbar, TRUE);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), radio1, 0);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), radio2, 1);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), radio3, 2);
GtkWidget *button=gtk_button_new_with_label("Get Active Radio");
gtk_widget_set_hexpand(button, TRUE);
gtk_widget_set_vexpand(button, TRUE);
g_signal_connect(button, "clicked", G_CALLBACK(radio_test1), radios);
GSimpleAction *radio_action1=g_simple_action_new("radio_test2", NULL);
g_action_map_add_action(G_ACTION_MAP(app), (GAction*)radio_action1);
g_signal_connect(radio_action1, "activate", G_CALLBACK(radio_test2), radios);
gtk_actionable_set_action_name(GTK_ACTIONABLE(button), "app.radio_test2");
g_object_unref(radio_action1);
GtkWidget *grid1=gtk_grid_new();
gtk_container_set_border_width(GTK_CONTAINER(grid1), 15);
gtk_grid_set_row_spacing(GTK_GRID(grid1), 8);
gtk_grid_attach(GTK_GRID(grid1), toolbar, 0, 0, 1, 1);
gtk_grid_attach(GTK_GRID(grid1), button, 0, 1, 1, 1);
gtk_container_add(GTK_CONTAINER(window), grid1);
gtk_widget_show_all(window);
gtk_widget_grab_focus(button);
}
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
GtkToolItem *radios[3];
app=gtk_application_new("app.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "startup", G_CALLBACK(app_startup), NULL);
g_signal_connect(app, "activate", G_CALLBACK(app_activate), radios);
status=g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return(status);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]