add copyright, and test loginfo fix in CVS
[ardour.git] / gtk2_ardour / actions.cc
1 /*
2     Copyright (C) 2005 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #include <vector>
22 #include <gtk/gtkaccelmap.h>
23 #include <gtkmm/accelmap.h>
24 #include <gtkmm/uimanager.h>
25
26 #include "actions.h"
27
28 using namespace std;
29 using namespace Gtk;
30 using namespace Glib;
31 using namespace sigc;
32
33 vector<Glib::RefPtr<Gtk::Action> > ActionManager::session_sensitive_actions;
34 vector<Glib::RefPtr<Gtk::Action> > ActionManager::region_list_selection_sensitive_actions;
35 vector<Glib::RefPtr<Gtk::Action> > ActionManager::region_selection_sensitive_actions;
36 vector<Glib::RefPtr<Gtk::Action> > ActionManager::track_selection_sensitive_actions;
37 vector<Glib::RefPtr<Gtk::Action> > ActionManager::plugin_selection_sensitive_actions;
38 vector<Glib::RefPtr<Gtk::Action> > ActionManager::range_sensitive_actions;
39 vector<Glib::RefPtr<Gtk::Action> > ActionManager::jack_sensitive_actions;
40
41 namespace ActionManager {
42
43 static vector<Glib::RefPtr<UIManager> > ui_managers;
44
45 void
46 register_ui_manager (Glib::RefPtr<UIManager> uim)
47 {
48         ui_managers.push_back (uim);
49 }
50
51 RefPtr<Action>
52 register_action (RefPtr<ActionGroup> group, string name, string label, slot<void> sl, guint key, Gdk::ModifierType mods)
53 {
54         RefPtr<Action> act = register_action (group, name, label, sl);
55         AccelMap::add_entry (act->get_accel_path(), key, mods);
56
57         return act;
58 }
59
60 RefPtr<Action>
61 register_action (RefPtr<ActionGroup> group, string name, string label, slot<void> sl)
62 {
63         RefPtr<Action> act = register_action (group, name, label);
64         group->add (act, sl);
65
66         return act;
67 }
68
69 RefPtr<Action>
70 register_action (RefPtr<ActionGroup> group, string name, string label)
71 {
72         RefPtr<Action> act;
73
74         act = Action::create (name, label);
75         group->add (act);
76
77         return act;
78 }
79
80
81 RefPtr<Action>
82 register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group rgroup, string name, string label, slot<void> sl, guint key, Gdk::ModifierType mods)
83 {
84         RefPtr<Action> act = register_radio_action (group, rgroup, name, label, sl);
85         AccelMap::add_entry (act->get_accel_path(), key, mods);
86
87         return act;
88 }
89
90 RefPtr<Action>
91 register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group rgroup, string name, string label, slot<void> sl)
92 {
93         RefPtr<Action> act;
94
95         act = RadioAction::create (rgroup, name, label);
96         group->add (act, sl);
97
98         return act;
99 }
100
101
102 RefPtr<Action>
103 register_toggle_action (RefPtr<ActionGroup> group, string name, string label, slot<void> sl, guint key, Gdk::ModifierType mods)
104 {
105         RefPtr<Action> act = register_toggle_action (group,name, label, sl);
106         AccelMap::add_entry (act->get_accel_path(), key, mods);
107
108         return act;
109 }
110
111 RefPtr<Action>
112 register_toggle_action (RefPtr<ActionGroup> group, string name, string label, slot<void> sl)
113 {
114         RefPtr<Action> act;
115
116         act = ToggleAction::create (name, label);
117         group->add (act, sl);
118
119         return act;
120 }
121
122 bool lookup_entry (const Glib::ustring accel_path, Gtk::AccelKey& key)
123 {
124         GtkAccelKey gkey;
125         bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
126         
127         if (known) {
128                 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
129         } else {
130                 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
131         }
132
133         return known;
134 }
135
136 void
137 get_all_actions (vector<string>& names, vector<string>& paths, vector<string>& keys, vector<AccelKey>& bindings)
138 {
139         for (vector<Glib::RefPtr<UIManager> >::iterator u = ui_managers.begin(); u != ui_managers.end(); ++u) {
140
141                 ListHandle<RefPtr<ActionGroup> > uim_groups = (*u)->get_action_groups ();
142
143                 for (ListHandle<RefPtr<ActionGroup> >::iterator g = uim_groups.begin(); g != uim_groups.end(); ++g) {
144
145                         ListHandle<RefPtr<Action> > group_actions = (*g)->get_actions();
146
147                         for (ListHandle<RefPtr<Action> >::iterator a = group_actions.begin(); a != group_actions.end(); ++a) {
148                                 
149                                 ustring accel_path;
150                                 
151                                 accel_path = (*a)->get_accel_path();
152                                 
153                                 names.push_back ((*a)->get_name());
154                                 paths.push_back (accel_path);
155
156                                 AccelKey key;
157                                 bool known = lookup_entry (accel_path, key);
158
159                                 if (known) {
160                                         keys.push_back ((*u)->get_accel_group()->name (key.get_key(), Gdk::ModifierType (key.get_mod())));
161                                 } else {
162                                         keys.push_back ("--");
163                                 }
164
165                                 bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
166                         }
167                 }
168         }
169 }
170
171 }