fix incorrect accumulation of export video options each time the dialog is used
[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 */
19
20 #include <cstring>
21 #include <string>
22 #include <list>
23
24 #include <gtk/gtkaccelmap.h>
25 #include <gtk/gtkuimanager.h>
26 #include <gtk/gtkactiongroup.h>
27
28 #include <gtkmm/accelmap.h>
29 #include <gtkmm/uimanager.h>
30
31 #include "pbd/error.h"
32 #include "pbd/file_utils.h"
33
34 #include "ardour/filesystem_paths.h"
35 #include "ardour/rc_configuration.h"
36
37 #include "gtkmm2ext/actions.h"
38
39 #include "actions.h"
40 #include "pbd/i18n.h"
41
42 using namespace std;
43 using namespace Gtk;
44 using namespace Glib;
45 using namespace PBD;
46 using namespace ARDOUR;
47
48 typedef std::vector<RefPtr<Gtk::Action> > RelatedActions;
49
50 RelatedActions ActionManager::session_sensitive_actions;
51 RelatedActions ActionManager::write_sensitive_actions;
52 RelatedActions ActionManager::region_list_selection_sensitive_actions;
53 RelatedActions ActionManager::plugin_selection_sensitive_actions;
54 RelatedActions ActionManager::track_selection_sensitive_actions;
55 RelatedActions ActionManager::stripable_selection_sensitive_actions;
56 RelatedActions ActionManager::route_selection_sensitive_actions;
57 RelatedActions ActionManager::bus_selection_sensitive_actions;
58 RelatedActions ActionManager::vca_selection_sensitive_actions;
59 RelatedActions ActionManager::point_selection_sensitive_actions;
60 RelatedActions ActionManager::time_selection_sensitive_actions;
61 RelatedActions ActionManager::line_selection_sensitive_actions;
62 RelatedActions ActionManager::playlist_selection_sensitive_actions;
63 RelatedActions ActionManager::mouse_edit_point_requires_canvas_actions;
64 RelatedActions ActionManager::range_sensitive_actions;
65 RelatedActions ActionManager::engine_sensitive_actions;
66 RelatedActions ActionManager::engine_opposite_sensitive_actions;
67 RelatedActions ActionManager::transport_sensitive_actions;
68 RelatedActions ActionManager::edit_point_in_region_sensitive_actions;
69 RelatedActions ActionManager::rec_sensitive_actions;
70
71 void
72 ActionManager::init ()
73 {
74         ui_manager = UIManager::create ();
75 }
76
77 void
78 ActionManager::load_menus (const string& menus_file)
79 {
80         std::string ui_file;
81
82         find_file (ardour_config_search_path(), menus_file, ui_file);
83
84         bool loaded = false;
85
86         try {
87                 ui_manager->add_ui_from_file (ui_file);
88                 info << string_compose (_("Loading menus from %1"), ui_file) << endmsg;
89                 loaded = true;
90         } catch (Glib::MarkupError& err) {
91                 error << string_compose (_("badly formatted menu definition file: %1"), err.what()) << endmsg;
92                 cerr << string_compose (_("badly formatted menu definition file: %1"), err.what()) << endl;
93         } catch (...) {
94                 error << string_compose (_("%1 menu definition file not found"), PROGRAM_NAME) << endmsg;
95         }
96
97         if (!loaded) {
98                 cerr << string_compose (_("%1 will not work without a valid menu definition file"), PROGRAM_NAME) << endl;
99                 error << string_compose (_("%1 will not work without a valid menu definition file"), PROGRAM_NAME) << endmsg;
100                 exit(1);
101         }
102 }
103
104 /** Examine the state of a Configuration setting and a toggle action, and toggle the Configuration
105  * setting if its state doesn't match the toggle action.
106  * @param group Action group.
107  * @param action Action name.
108  * @param Method to set the state of the Configuration setting.
109  * @param Method to get the state of the Configuration setting.
110  */
111 void
112 ActionManager::toggle_config_state (const char* group, const char* action, bool (RCConfiguration::*set)(bool), bool (RCConfiguration::*get)(void) const)
113 {
114         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
115
116         if (act) {
117                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
118
119                 if (tact) {
120                         bool x = (Config->*get)();
121
122                         if (x != tact->get_active()) {
123                                 (Config->*set) (!x);
124                         }
125                 }
126         }
127 }
128
129 /** Examine the state of a Configuration setting and a toggle action, and toggle the Configuration
130  * setting if its state doesn't match the toggle action.
131  * @param group Action group.
132  * @param action Action name.
133  * @param Method to set the state of the Configuration setting.
134  * @param Method to get the state of the Configuration setting.
135  */
136 void
137 ActionManager::toggle_config_state (const char* group, const char* action, bool (UIConfiguration::*set)(bool), bool (UIConfiguration::*get)(void) const)
138 {
139         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
140
141         if (act) {
142                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
143
144                 if (tact) {
145                         bool x = (UIConfiguration::instance().*get)();
146
147                         if (x != tact->get_active()) {
148                                 (UIConfiguration::instance().*set) (!x);
149                         }
150                 }
151         }
152 }
153
154 void
155 ActionManager::toggle_config_state_foo (const char* group, const char* action, sigc::slot<bool, bool> set, sigc::slot<bool> get)
156 {
157         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
158
159         if (act) {
160                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
161
162                 if (tact) {
163                         bool const x = get ();
164
165                         if (x != tact->get_active ()) {
166                                 set (!x);
167                         }
168                 }
169         }
170 }
171
172
173 /** Set the state of a ToggleAction using a particular Configuration get() method
174  * @param group Action group.
175  * @param action Action name.
176  * @param get Method to obtain the state that the ToggleAction should have.
177  */
178 void
179 ActionManager::map_some_state (const char* group, const char* action, bool (RCConfiguration::*get)() const)
180 {
181         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
182         if (act) {
183                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
184
185                 if (tact) {
186
187                         bool x = (Config->*get)();
188
189                         if (tact->get_active() != x) {
190                                 tact->set_active (x);
191                         }
192                 }
193         }
194 }
195
196 /** Set the state of a ToggleAction using a particular Configuration get() method
197  * @param group Action group.
198  * @param action Action name.
199  * @param get Method to obtain the state that the ToggleAction should have.
200  */
201 void
202 ActionManager::map_some_state (const char* group, const char* action, bool (UIConfiguration::*get)() const)
203 {
204         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
205         if (act) {
206                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
207
208                 if (tact) {
209
210                         bool x = (UIConfiguration::instance().*get)();
211
212                         if (tact->get_active() != x) {
213                                 tact->set_active (x);
214                         }
215                 }
216         }
217 }
218
219 void
220 ActionManager::map_some_state (const char* group, const char* action, sigc::slot<bool> get)
221 {
222         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
223         if (act) {
224                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
225
226                 if (tact) {
227
228                         bool const x = get ();
229
230                         if (tact->get_active() != x) {
231                                 tact->set_active (x);
232                         }
233                 }
234         }
235 }
236