compiles and runs, but crashes ... duh
[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 <pbd/error.h>
27
28 #include "actions.h"
29
30 using namespace std;
31 using namespace Gtk;
32 using namespace Glib;
33 using namespace sigc;
34
35 vector<RefPtr<Gtk::Action> > ActionManager::session_sensitive_actions;
36 vector<RefPtr<Gtk::Action> > ActionManager::region_list_selection_sensitive_actions;
37 vector<RefPtr<Gtk::Action> > ActionManager::region_selection_sensitive_actions;
38 vector<RefPtr<Gtk::Action> > ActionManager::track_selection_sensitive_actions;
39 vector<RefPtr<Gtk::Action> > ActionManager::plugin_selection_sensitive_actions;
40 vector<RefPtr<Gtk::Action> > ActionManager::range_sensitive_actions;
41 vector<RefPtr<Gtk::Action> > ActionManager::jack_sensitive_actions;
42 RefPtr<UIManager> ActionManager::ui_manager;
43 string ActionManager::unbound_string = "--";
44
45 void
46 ActionManager::init ()
47 {
48         ui_manager = UIManager::create ();
49
50         try {
51                 ui_manager->add_ui_from_file ("ardour-menus.xml");
52         } catch (Glib::MarkupError& err) {
53                 error << "badly formatted UI definition file" << endmsg;
54         }
55     
56 }
57
58 RefPtr<Action>
59 ActionManager::register_action (RefPtr<ActionGroup> group, string name, string label, slot<void> sl, guint key, Gdk::ModifierType mods)
60 {
61         RefPtr<Action> act = register_action (group, name, label, sl);
62         AccelMap::add_entry (act->get_accel_path(), key, mods);
63
64         return act;
65 }
66
67 RefPtr<Action>
68 ActionManager::register_action (RefPtr<ActionGroup> group, string name, string label, slot<void> sl)
69 {
70         RefPtr<Action> act = register_action (group, name, label);
71         group->add (act, sl);
72
73         return act;
74 }
75
76 RefPtr<Action>
77 ActionManager::register_action (RefPtr<ActionGroup> group, string name, string label)
78 {
79         RefPtr<Action> act;
80
81         act = Action::create (name, label);
82         group->add (act);
83
84         return act;
85 }
86
87
88 RefPtr<Action>
89 ActionManager::register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group rgroup, string name, string label, slot<void> sl, guint key, Gdk::ModifierType mods)
90 {
91         RefPtr<Action> act = register_radio_action (group, rgroup, name, label, sl);
92         AccelMap::add_entry (act->get_accel_path(), key, mods);
93
94         return act;
95 }
96
97 RefPtr<Action>
98 ActionManager::register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group rgroup, string name, string label, slot<void> sl)
99 {
100         RefPtr<Action> act;
101
102         act = RadioAction::create (rgroup, name, label);
103         group->add (act, sl);
104
105         return act;
106 }
107
108
109 RefPtr<Action>
110 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, string name, string label, slot<void> sl, guint key, Gdk::ModifierType mods)
111 {
112         RefPtr<Action> act = register_toggle_action (group,name, label, sl);
113         AccelMap::add_entry (act->get_accel_path(), key, mods);
114
115         return act;
116 }
117
118 RefPtr<Action>
119 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, string name, string label, slot<void> sl)
120 {
121         RefPtr<Action> act;
122
123         act = ToggleAction::create (name, label);
124         group->add (act, sl);
125
126         return act;
127 }
128
129 bool 
130 ActionManager::lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
131 {
132         GtkAccelKey gkey;
133         bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
134         
135         if (known) {
136                 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
137         } else {
138                 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
139         }
140
141         return known;
142 }
143
144 void
145 ActionManager::get_all_actions (vector<string>& names, vector<string>& paths, vector<string>& keys, vector<AccelKey>& bindings)
146 {
147         ListHandle<RefPtr<ActionGroup> > uim_groups = ui_manager->get_action_groups ();
148         
149         for (ListHandle<RefPtr<ActionGroup> >::iterator g = uim_groups.begin(); g != uim_groups.end(); ++g) {
150                 
151                 ListHandle<RefPtr<Action> > group_actions = (*g)->get_actions();
152                 
153                 for (ListHandle<RefPtr<Action> >::iterator a = group_actions.begin(); a != group_actions.end(); ++a) {
154                         
155                         ustring accel_path;
156                         
157                         accel_path = (*a)->get_accel_path();
158                         
159                         names.push_back ((*a)->get_name());
160                         paths.push_back (accel_path);
161                         
162                         AccelKey key;
163                         bool known = lookup_entry (accel_path, key);
164                         
165                         if (known) {
166                                 keys.push_back (ui_manager->get_accel_group()->name (key.get_key(), Gdk::ModifierType (key.get_mod())));
167                         } else {
168                                 keys.push_back (unbound_string);
169                         }
170                         
171                         bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
172                 }
173         }
174 }
175
176 void
177 ActionManager::add_action_group (RefPtr<ActionGroup> grp)
178 {
179         ui_manager->insert_action_group (grp);
180 }
181
182 Widget*
183 ActionManager::get_widget (ustring name)
184 {
185         return ui_manager->get_widget (name);
186 }
187
188 RefPtr<Action>
189 ActionManager::get_action (ustring name)
190 {
191         return ui_manager->get_action (name);
192 }