3

I'm trying to write a GTK+3 application using C.

I created a glade file with an action that it's id is 'tracks_deck.add_empty_track'.

i connected the action to a toolbar item and a menu item.

the question is how do I connect a function to that action ?

I have the following C Code:

#include <gtk/gtk.h>
#define UI_FILE "tuxmusicstudio.glade" 
#define TOP_WINDOW "application_window"

int main (int argc, char *argv[])
{
GtkWidget *main_window;
GtkBuilder *builder;
GError *error = NULL;

GtkAction *action;

gtk_init (&argc, &argv);

builder = gtk_builder_new();
if (!gtk_builder_add_from_file(builder, UI_FILE, &error)) {
    g_critical("couldn't load builder file: %s",error->message);
    g_error_free(error);
}

/* create the main, top level, window */
main_window = GTK_WIDGET(gtk_builder_get_object(builder, TOP_WINDOW));
if (!main_window) {
    g_critical("widget \"%s\" is missing in file %s.",TOP_WINDOW,UI_FILE);
} 

action = GTK_ACTION(gtk_builder_get_object(builder, "tracks_deck.add_empty_track"));
if (!action) {
    g_critical("could not fetch action tracks_deck.empty_track");
}

 gtk_widget_show_all (main_window);

/* start the main loop, and let it rest there until the application is closed */
gtk_main ();

return 0;

so as you can see i'm trying to fetch the GtkAction in order to connect a function to it. the compiler is compiling that this line is deprecated:

main.c:46:5: warning: ‘gtk_action_get_type’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkaction.h:87) [-Wdeprecated-declarations]
 action = GTK_ACTION(gtk_builder_get_object(builder, "tracks_deck.add_empty_track"));
 ^

so how do I fetch the GtkAction with some function that is not deprecated. and how do I connect the a c function to that action?!

any ideas regarding the issue would be greatly appreciated.

ufk
  • 30,912
  • 70
  • 235
  • 386

2 Answers2

2

From GtkAction

Description

In GTK+ 3.10, GtkAction has been deprecated. Use GAction instead, and associate actions with GtkActionable widgets. Use GMenuModel for creating menus with gtk_menu_new_from_model().

So, it seems the way to go is switching from GtkAction to GAction.

I've seen similar moves from gtk_something to g_something, e.g. gtk_signal_connect and g_signal_connect.

Update:

I searched around and haven't really found a good example or tutorial. The only snippet I found, is here at g_action_map_add_action_entries

static void
activate_quit (GSimpleAction *simple,
               GVariant      *parameter,
               gpointer       user_data)
{
  exit (0);
}

static void
activate_print_string (GSimpleAction *simple,
                       GVariant      *parameter,
                       gpointer       user_data)
{
  g_print ("%s\n", g_variant_get_string (parameter, NULL));
}

static GActionGroup *
create_action_group (void)
{
  const GActionEntry entries[] = {
    { "quit",         activate_quit              },
    { "print-string", activate_print_string, "s" }
  };
  GSimpleActionGroup *group;

  group = g_simple_action_group_new ();
  g_action_map_add_action_entries (G_ACTION_MAP (group), entries, G_N_ELEMENTS (entries), NULL);

  return G_ACTION_GROUP (group);
}

This example uses struct GActionEntry to name a function/action and add it to a action group with g_action_map_add_action_entries.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
1

As Olaf mentioned you should use GAction now.

To connect a function to a action is actually pretty simple, you do this by connecting to the activate signal.

Here is a small example:

GSimpleAction *action = g_simple_action_new ("save", NULL);
g_signal_connect (G_OBJECT(action), "activate", saveFunction, userData);

You can read more about actions here

Victor Aurélio
  • 2,355
  • 2
  • 24
  • 48