Strange problem with redefining Action callback

Hi, guys!

I have an GtkAction with callback, and I want to switch the callback for that action at runtime. The action is called when a hotkey is pressed.

For example you can press a hotkey (“Backspace”) and for different objects it will do different actions.

But then I trying to change or delete callback - nothing happens. It looks for me what I missed something, so I need help here (link to good tutorials for Gtk::AccelMap are welcome).

What I did:

First registered backspace as a hotkey using Gtk::AccelMap for action-LayerRemove:

Gtk::AccelMap::add_entry("<Actions>/action_group_layer_action_manager/action-LayerRemove", GDK_KEY_BackSpace, Gdk::ModifierType());

Also I am using ui_manager (Gtk::UIManager) to link all together.

(Skipped some code which adds action to ActionGroup)

After that, I call the action manually (just to make sure everything works as intended):

  GQuark accel_quark;
  const auto accel_name = Gtk::AccelGroup::name(GDK_KEY_BackSpace, Gdk::ModifierType());
  accel_quark = g_quark_from_string (accel_name.c_str());
  Glib::RefPtr<Glib::Object>this_window = Glib::wrap(this->gobj_copy(), false);
  if (ui_manager->get_accel_group()->activate(accel_quark, this_window, GDK_KEY_BackSpace,Gdk::ModifierType())) {
    std::cout << "WORKS!" << endl;
  }

Everything is working fine and the action callback is activated.

Next I want to replace callback. If I understand right, I need to switch old ActionGroup to new one. So I remove all ActionGroups from UIManager:

  // Remove old groups
  auto old_groups = ui_manager->get_action_groups();
  for (auto ag : old_groups) {
    ui_manager->remove_action_group(ag);
  }
  ui_manager->ensure_update();

Next I create ActionGroup and add action (with the new callback) to it:

  auto action_group = Gtk::ActionGroup::create("action_group_layer_action_manager");
  action_group->add(Gtk::Action::create("action-LayerRemove"),
          sigc::bind<std::string>(&TestFunc, "test77")
  );
  ui_manager->insert_action_group(action_group);
  ui_manager->ensure_update();

After that I call action again with the same code:

  GQuark accel_quark;
  const auto accel_name = Gtk::AccelGroup::name(GDK_KEY_BackSpace, Gdk::ModifierType());
  accel_quark = g_quark_from_string (accel_name.c_str());
  Glib::RefPtr<Glib::Object>this_window = Glib::wrap(this->gobj_copy(), false);
  if (ui_manager->get_accel_group()->activate(accel_quark, this_window, GDK_KEY_BackSpace,Gdk::ModifierType())) {
    std::cout << "WORKS!" << endl;
  }

And it calls old callback :frowning:

What I am doing wrong here?

P.S.
TestFunc is just a boilerplate:

void TestFunc(std::string str) {
  std::cout << str;
}
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.