X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=libs%2Fgtkmm2ext%2Factions.cc;h=25d2ca148cbf6ec19267e6ab4a2ee0564a9147af;hb=58db958839db7c052b95aae86ce9eb6f66ce9854;hp=9471067de9d48602f20d60cee37b528a626045e9;hpb=22b07e0233a29d9633ffa825a79503befaf2e16e;p=ardour.git diff --git a/libs/gtkmm2ext/actions.cc b/libs/gtkmm2ext/actions.cc index 9471067de9..25d2ca148c 100644 --- a/libs/gtkmm2ext/actions.cc +++ b/libs/gtkmm2ext/actions.cc @@ -37,11 +37,12 @@ #include #include "pbd/error.h" +#include "pbd/stacktrace.h" #include "gtkmm2ext/actions.h" #include "gtkmm2ext/utils.h" -#include "i18n.h" +#include "pbd/i18n.h" using namespace std; using namespace Gtk; @@ -50,266 +51,61 @@ using namespace sigc; using namespace PBD; using namespace Gtkmm2ext; -RefPtr ActionManager::ui_manager; -string ActionManager::unbound_string = "--"; - - -RefPtr -ActionManager::register_action (RefPtr group, const char * name, const char * label, slot sl) -{ - RefPtr act; - - act = Action::create (name, label); - group->add (act, sl); - - return act; -} - -RefPtr -ActionManager::register_action (RefPtr group, const char * name, const char * label) -{ - RefPtr act; - - act = Action::create (name, label); - group->add (act); - - return act; -} - - -RefPtr -ActionManager::register_radio_action (RefPtr group, RadioAction::Group& rgroup, const char * name, const char * label, slot sl) -{ - RefPtr act; - - act = RadioAction::create (rgroup, name, label); - group->add (act, sl); - - return act; -} +typedef std::map > ActionMap; +static ActionMap actions; +typedef std::vector > ActionGroups; +static ActionGroups groups; -RefPtr -ActionManager::register_radio_action ( - RefPtr group, RadioAction::Group& rgroup, string const & name, string const & label, string const & tooltip, slot sl - ) -{ - RefPtr act; - - act = RadioAction::create (rgroup, name, label, tooltip); - group->add (act, sl); - - return act; -} - -RefPtr -ActionManager::register_toggle_action (RefPtr group, const char * name, const char * label, slot sl) -{ - RefPtr act; - - act = ToggleAction::create (name, label); - group->add (act, sl); - - return act; -} - -RefPtr -ActionManager::register_toggle_action (RefPtr group, string const & name, string const & label, string const & tooltip, slot sl) -{ - RefPtr act; - - act = ToggleAction::create (name, label, tooltip); - group->add (act, sl); - - return act; -} - -bool -ActionManager::lookup_entry (const ustring accel_path, Gtk::AccelKey& key) -{ - GtkAccelKey gkey; - bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey); - - if (known) { - key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods)); - } else { - key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0)); - } - - return known; -} +RefPtr ActionManager::ui_manager; +string ActionManager::unbound_string = X_("--"); -struct SortActionsByLabel { - bool operator() (Glib::RefPtr a, Glib::RefPtr b) { - ustring astr = a->get_accel_path(); - ustring bstr = b->get_accel_path(); - return astr < bstr; - } +struct ActionState { + GtkAction* action; + bool sensitive; + ActionState (GtkAction* a, bool s) : action (a), sensitive (s) {} }; -void -ActionManager::get_all_actions (vector& groups, vector& names, vector& tooltips, vector& bindings) -{ - /* the C++ API for functions used here appears to be broken in - gtkmm2.6, so we fall back to the C level. - */ - - GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj()); - GList* node; - GList* acts; - - for (node = list; node; node = g_list_next (node)) { - - GtkActionGroup* group = (GtkActionGroup*) node->data; - - /* first pass: collect them all */ - - typedef std::list > action_list; - action_list the_acts; - - for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) { - GtkAction* action = (GtkAction*) acts->data; - the_acts.push_back (Glib::wrap (action, true)); - } - - /* now sort by label */ - - SortActionsByLabel cmp; - the_acts.sort (cmp); - - for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) { - - string accel_path = (*a)->get_accel_path (); - - groups.push_back (gtk_action_group_get_name(group)); - names.push_back (accel_path.substr (accel_path.find_last_of ('/') + 1)); - tooltips.push_back ((*a)->get_tooltip ()); +typedef std::vector ActionStates; - AccelKey key; - lookup_entry (accel_path, key); - bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod()))); - } - } -} +static ActionStates action_states_to_restore; +static bool actions_disabled = false; void -ActionManager::get_all_actions (vector& names, vector& paths, vector& tooltips, vector& keys, vector& bindings) +ActionManager::init () { - /* the C++ API for functions used here appears to be broken in - gtkmm2.6, so we fall back to the C level. - */ - - GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj()); - GList* node; - GList* acts; - - for (node = list; node; node = g_list_next (node)) { - - GtkActionGroup* group = (GtkActionGroup*) node->data; - - /* first pass: collect them all */ - - typedef std::list > action_list; - action_list the_acts; - - for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) { - GtkAction* action = (GtkAction*) acts->data; - the_acts.push_back (Glib::wrap (action, true)); - } - - /* now sort by label */ - - SortActionsByLabel cmp; - the_acts.sort (cmp); - - for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) { - - ustring const label = (*a)->property_label (); - string const accel_path = (*a)->get_accel_path (); - - names.push_back (label); - paths.push_back (accel_path); - tooltips.push_back ((*a)->get_tooltip ()); - - AccelKey key; - keys.push_back (get_key_representation (accel_path, key)); - bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod()))); - } - } + ui_manager = UIManager::create (); } void -ActionManager::enable_accelerators () +ActionManager::save_action_states () { - /* the C++ API for functions used here appears to be broken in - gtkmm2.6, so we fall back to the C level. - */ - - GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj()); - GList* node; - GList* acts; - string ui_string = ""; - - /* get all actions, build a string describing them all as - */ - - for (node = list; node; node = g_list_next (node)) { - - GtkActionGroup* group = (GtkActionGroup*) node->data; + for (ActionGroups::iterator g = groups.begin(); g != groups.end(); ++g) { - for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) { - ui_string += "gobj(); - string fullpath = gtk_action_get_accel_path ((GtkAction*) acts->data); - - ui_string += Glib::path_get_basename (fullpath); - ui_string += "\"/>"; + for (GList* acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) { + GtkAction* action = (GtkAction*) acts->data; + action_states_to_restore.push_back (ActionState (action, gtk_action_get_sensitive (action))); } } - - ui_string += ""; - - /* and load it */ - - ui_manager->add_ui_from_string (ui_string); } -struct ActionState { - GtkAction* action; - bool sensitive; - ActionState (GtkAction* a, bool s) : action (a), sensitive (s) {} -}; - -typedef std::vector ActionStates; - -static ActionStates action_states_to_restore; -static bool actions_disabled = false; - void -ActionManager::save_action_states () +ActionManager::set_sensitive (Glib::RefPtr group, bool yn) { /* the C++ API for functions used here appears to be broken in gtkmm2.6, so we fall back to the C level. */ - GList* list = gtk_ui_manager_get_action_groups (ActionManager::ui_manager->gobj()); - GList* node; - GList* acts; - for (node = list; node; node = g_list_next (node)) { + GtkActionGroup* grp = group->gobj(); - GtkActionGroup* group = (GtkActionGroup*) node->data; - - for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) { - GtkAction* action = (GtkAction*) acts->data; - action_states_to_restore.push_back (ActionState (action, gtk_action_get_sensitive (action))); - } + for (GList* acts = gtk_action_group_list_actions (grp); acts; acts = g_list_next (acts)) { + GtkAction* action = (GtkAction*) acts->data; + gtk_action_set_sensitive (action, yn); } } @@ -348,111 +144,12 @@ ActionManager::disable_active_actions () actions_disabled = true; } -void -ActionManager::add_action_group (RefPtr grp) -{ - ui_manager->insert_action_group (grp); -} - Widget* ActionManager::get_widget (const char * name) { return ui_manager->get_widget (name); } -RefPtr -ActionManager::get_action (const char* path) -{ - if (!path) { - return RefPtr(); - } - - /* Skip / in path */ - - int len = strlen (path); - - if (len < 3) { - /* shortest possible path: "a/b" */ - return RefPtr(); - } - - if (len > 10 && !strncmp (path, "/", 10 )) { - path = path+10; - } else if (path[0] == '/') { - path++; - } - - vector copy(len+1); - strcpy (©[0], path); - char* slash = strchr (©[0], '/'); - if (!slash) { - return RefPtr (); - } - *slash = '\0'; - - return get_action (©[0], ++slash); - -} - -RefPtr -ActionManager::get_action (const char* group_name, const char* action_name) -{ - /* the C++ API for functions used here appears to be broken in - gtkmm2.6, so we fall back to the C level. - */ - - if (ui_manager == 0) { - return RefPtr (); - } - - GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj()); - GList* node; - RefPtr act; - - for (node = list; node; node = g_list_next (node)) { - - GtkActionGroup* _ag = (GtkActionGroup*) node->data; - - if (strcmp (group_name, gtk_action_group_get_name (_ag)) == 0) { - - GtkAction* _act; - - if ((_act = gtk_action_group_get_action (_ag, action_name)) != 0) { - act = Glib::wrap (_act, true); - break; - } - } - } - - return act; -} - -RefPtr -ActionManager::get_action_from_name (const char* name) -{ - /* the C++ API for functions used here appears to be broken in - gtkmm2.6, so we fall back to the C level. - */ - - GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj()); - GList* node; - GList* acts; - - for (node = list; node; node = g_list_next (node)) { - - GtkActionGroup* group = (GtkActionGroup*) node->data; - - for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) { - GtkAction* action = (GtkAction*) acts->data; - if (!strcmp (gtk_action_get_name (action), name)) { - return Glib::wrap (action, true); - } - } - } - - return RefPtr(); -} - void ActionManager::set_sensitive (vector >& actions, bool state) { @@ -478,19 +175,19 @@ ActionManager::set_sensitive (vector >& actions, bool state) } void -ActionManager::check_toggleaction (string n) +ActionManager::check_toggleaction (const string& n) { set_toggleaction_state (n, true); } void -ActionManager::uncheck_toggleaction (string n) +ActionManager::uncheck_toggleaction (const string& n) { set_toggleaction_state (n, false); } void -ActionManager::set_toggleaction_state (string n, bool s) +ActionManager::set_toggleaction_state (const string& n, bool s) { char const * name = n.c_str (); @@ -510,30 +207,25 @@ ActionManager::set_toggleaction_state (string n, bool s) group_name[len] = '\0'; const char* action_name = last_slash + 1; - - RefPtr act = get_action (group_name, action_name); - if (act) { - RefPtr tact = RefPtr::cast_dynamic(act); - tact->set_active (s); - } else { - error << string_compose (_("Unknown action name: %1"), name) << endmsg; + if (!set_toggleaction_state (group_name, action_name, s)) { + error << string_compose (_("Unknown action name: %1/%2"), group_name, action_name) << endmsg; } delete [] group_name; } -string -ActionManager::get_key_representation (const string& accel_path, AccelKey& key) +bool +ActionManager::set_toggleaction_state (const char* group_name, const char* action_name, bool s) { - bool known = lookup_entry (accel_path, key); - - if (known) { - uint32_t k = possibly_translate_legal_accelerator_to_real_key (key.get_key()); - key = AccelKey (k, Gdk::ModifierType (key.get_mod())); - return ui_manager->get_accel_group()->get_label (key.get_key(), Gdk::ModifierType (key.get_mod())); + RefPtr act = get_action (group_name, action_name); + if (act) { + RefPtr tact = RefPtr::cast_dynamic(act); + if (tact) { + tact->set_active (s); + return true; + } } - - return unbound_string; + return false; } void @@ -548,12 +240,292 @@ ActionManager::do_action (const char* group, const char*action) void ActionManager::set_toggle_action (const char* group, const char*action, bool yn) { - Glib::RefPtr act = ActionManager::get_action (group, action); - if (act) { - Glib::RefPtr tact = Glib::RefPtr::cast_dynamic (act); - if (tact) { - tact->set_active (yn); + Glib::RefPtr tact = ActionManager::get_toggle_action (group, action); + tact->set_active (yn); +} + +RefPtr +ActionManager::get_action (const string& name, bool or_die) +{ + ActionMap::const_iterator a = actions.find (name); + + if (a != actions.end()) { + return a->second; + } + + if (or_die) { + ::abort (); + } + + cerr << "Failed to find action: [" << name << ']' << endl; + return RefPtr(); +} + +RefPtr +ActionManager::get_toggle_action (const string& name, bool or_die) +{ + RefPtr act = get_action (name, or_die); + + if (!act) { + return RefPtr(); + } + + return Glib::RefPtr::cast_dynamic (act); +} + +RefPtr +ActionManager::get_radio_action (const string& name, bool or_die) +{ + RefPtr act = get_action (name, or_die); + + if (!act) { + return RefPtr(); + } + + return Glib::RefPtr::cast_dynamic (act); +} + +RefPtr +ActionManager::get_action (char const * group_name, char const * action_name, bool or_die) +{ + string fullpath (group_name); + fullpath += '/'; + fullpath += action_name; + + ActionMap::const_iterator a = actions.find (fullpath); + + if (a != actions.end()) { + return a->second; + } + + if (or_die) { + ::abort (); + } + + cerr << "Failed to find action (2): [" << fullpath << ']' << endl; + PBD::stacktrace (std::cerr, 20); + return RefPtr(); +} + +RefPtr +ActionManager::get_toggle_action (char const * group_name, char const * action_name, bool or_die) +{ + RefPtr act = get_action (group_name, action_name, or_die); + + if (!act) { + return RefPtr(); + } + + return Glib::RefPtr::cast_dynamic (act); +} + +RefPtr +ActionManager::get_radio_action (char const * group_name, char const * action_name, bool or_die) +{ + RefPtr act = get_action (group_name, action_name, or_die); + + if (!act) { + return RefPtr(); + } + + return Glib::RefPtr::cast_dynamic (act); +} + + +RefPtr +ActionManager::create_action_group (void * owner, string const & name) +{ + for (ActionGroups::iterator g = groups.begin(); g != groups.end(); ++g) { + if ((*g)->get_name () == name) { + return *g; } } + + RefPtr g = ActionGroup::create (name); + + g->set_data (X_("owner"), owner); + groups.push_back (g); + + /* this is one of the places where our own Action management code + has to touch the GTK one, because we want the GtkUIManager to + be able to create widgets (particularly Menus) from our actions. + + This is a a necessary step for that to happen. + */ + + if (g) { + ActionManager::ui_manager->insert_action_group (g); + } + + return g; } +RefPtr +ActionManager::register_action (RefPtr group, const char* name, const char* label) +{ + string fullpath; + + RefPtr act = Action::create (name, label); + + fullpath = group->get_name(); + fullpath += '/'; + fullpath += name; + + if (actions.insert (ActionMap::value_type (fullpath, act)).second) { + group->add (act); + return act; + } + + /* already registered */ + return RefPtr (); +} + +RefPtr +ActionManager::register_action (RefPtr group, + const char* name, const char* label, sigc::slot sl) +{ + string fullpath; + + RefPtr act = Action::create (name, label); + + fullpath = group->get_name(); + fullpath += '/'; + fullpath += name; + + if (actions.insert (ActionMap::value_type (fullpath, act)).second) { + group->add (act, sl); + return act; + } + + /* already registered */ + return RefPtr(); +} + +RefPtr +ActionManager::register_radio_action (RefPtr group, + Gtk::RadioAction::Group& rgroup, + const char* name, const char* label, + sigc::slot sl) +{ + string fullpath; + + RefPtr act = RadioAction::create (rgroup, name, label); + RefPtr ract = RefPtr::cast_dynamic(act); + + fullpath = group->get_name(); + fullpath += '/'; + fullpath += name; + + if (actions.insert (ActionMap::value_type (fullpath, act)).second) { + group->add (act, sl); + return act; + } + + /* already registered */ + return RefPtr(); +} + +RefPtr +ActionManager::register_radio_action (RefPtr group, + Gtk::RadioAction::Group& rgroup, + const char* name, const char* label, + sigc::slot sl, + int value) +{ + string fullpath; + + RefPtr act = RadioAction::create (rgroup, name, label); + RefPtr ract = RefPtr::cast_dynamic(act); + ract->property_value() = value; + + fullpath = group->get_name(); + fullpath += '/'; + fullpath += name; + + if (actions.insert (ActionMap::value_type (fullpath, act)).second) { + group->add (act, sigc::bind (sl, act->gobj())); + return act; + } + + /* already registered */ + + return RefPtr(); +} + +RefPtr +ActionManager::register_toggle_action (RefPtr group, + const char* name, const char* label, sigc::slot sl) +{ + string fullpath; + + fullpath = group->get_name(); + fullpath += '/'; + fullpath += name; + + RefPtr act = ToggleAction::create (name, label); + + if (actions.insert (ActionMap::value_type (fullpath, act)).second) { + group->add (act, sl); + return act; + } + + /* already registered */ + return RefPtr(); +} + +void +ActionManager::get_actions (void* owner, std::vector >& acts) +{ + for (ActionMap::const_iterator a = actions.begin(); a != actions.end(); ++a) { + if (owner) { + Glib::RefPtr group = a->second->property_action_group (); + if (group->get_data (X_("owner")) == owner) { + acts.push_back (a->second); + } + } else { + acts.push_back (a->second); + } + } +} + +void +ActionManager::get_all_actions (std::vector& paths, + std::vector& labels, + std::vector& tooltips, + std::vector& keys, + std::vector >& acts) +{ + for (ActionMap::const_iterator a = actions.begin(); a != actions.end(); ++a) { + + Glib::RefPtr act = a->second; + + paths.push_back (act->get_accel_path()); + labels.push_back (act->get_label()); + tooltips.push_back (act->get_tooltip()); + acts.push_back (act); + + /* foreach binding */ + +#if 0 + Bindings* bindings = (*map)->bindings(); + + if (bindings) { + + KeyboardKey key; + Bindings::Operation op; + + key = bindings->get_binding_for_action (*act, op); + + if (key == KeyboardKey::null_key()) { + keys.push_back (string()); + } else { + keys.push_back (key.display_label()); + } + } else { + keys.push_back (string()); + } + } +#endif + + } +}