1 /*
2  * Copyright (C) 2005-2006 Taybin Rutkin <taybin@taybin.com>
3  * Copyright (C) 2005-2015 Tim Mayberry <mojofunk@gmail.com>
4  * Copyright (C) 2005-2018 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2005 Karsten Wiese <fzuuzf@googlemail.com>
6  * Copyright (C) 2008-2012 David Robillard <d@drobilla.net>
7  * Copyright (C) 2009-2010 Carl Hetherington <carl@carlh.net>
8  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 #include <cstring>
26 #include <string>
27 #include <list>
28 
29 #include <gtk/gtkaccelmap.h>
30 #include <gtk/gtkuimanager.h>
31 #include <gtk/gtkactiongroup.h>
32 
33 #include <gtkmm/accelmap.h>
34 #include <gtkmm/uimanager.h>
35 
36 #include "pbd/error.h"
37 #include "pbd/file_utils.h"
38 
39 #include "ardour/filesystem_paths.h"
40 #include "ardour/rc_configuration.h"
41 
42 #include "gtkmm2ext/actions.h"
43 
44 #include "actions.h"
45 #include "pbd/i18n.h"
46 
47 using namespace std;
48 using namespace Gtk;
49 using namespace Glib;
50 using namespace PBD;
51 using namespace ARDOUR;
52 
53 typedef std::vector<RefPtr<Gtk::Action> > RelatedActions;
54 
55 RelatedActions ActionManager::session_sensitive_actions;
56 RelatedActions ActionManager::write_sensitive_actions;
57 RelatedActions ActionManager::region_list_selection_sensitive_actions;
58 RelatedActions ActionManager::plugin_selection_sensitive_actions;
59 RelatedActions ActionManager::track_selection_sensitive_actions;
60 RelatedActions ActionManager::stripable_selection_sensitive_actions;
61 RelatedActions ActionManager::route_selection_sensitive_actions;
62 RelatedActions ActionManager::bus_selection_sensitive_actions;
63 RelatedActions ActionManager::vca_selection_sensitive_actions;
64 RelatedActions ActionManager::point_selection_sensitive_actions;
65 RelatedActions ActionManager::time_selection_sensitive_actions;
66 RelatedActions ActionManager::line_selection_sensitive_actions;
67 RelatedActions ActionManager::playlist_selection_sensitive_actions;
68 RelatedActions ActionManager::mouse_edit_point_requires_canvas_actions;
69 RelatedActions ActionManager::range_sensitive_actions;
70 RelatedActions ActionManager::engine_sensitive_actions;
71 RelatedActions ActionManager::engine_opposite_sensitive_actions;
72 RelatedActions ActionManager::transport_sensitive_actions;
73 RelatedActions ActionManager::rec_sensitive_actions;
74 
75 void
load_menus(const string & menus_file)76 ActionManager::load_menus (const string& menus_file)
77 {
78 	std::string ui_file;
79 
80 	find_file (ardour_config_search_path(), menus_file, ui_file);
81 
82 	bool loaded = false;
83 
84 	try {
85 		ui_manager->add_ui_from_file (ui_file);
86 		info << string_compose (_("Loading menus from %1"), ui_file) << endmsg;
87 		loaded = true;
88 	} catch (Glib::MarkupError& err) {
89 		error << string_compose (_("badly formatted menu definition file: %1"), err.what()) << endmsg;
90 		cerr << string_compose (_("badly formatted menu definition file: %1"), err.what()) << endl;
91 	} catch (...) {
92 		error << string_compose (_("%1 menu definition file not found"), PROGRAM_NAME) << endmsg;
93 	}
94 
95 	if (!loaded) {
96 		cerr << string_compose (_("%1 will not work without a valid menu definition file"), PROGRAM_NAME) << endl;
97 		error << string_compose (_("%1 will not work without a valid menu definition file"), PROGRAM_NAME) << endmsg;
98 		exit (EXIT_FAILURE);
99 	}
100 }
101 
102 /** Examine the state of a Configuration setting and a toggle action, and toggle the Configuration
103  * setting if its state doesn't match the toggle action.
104  * @param group Action group.
105  * @param action Action name.
106  * @param Method to set the state of the Configuration setting.
107  * @param Method to get the state of the Configuration setting.
108  */
109 void
toggle_config_state(const char * group,const char * action,bool (RCConfiguration::* set)(bool),bool (RCConfiguration::* get)(void)const)110 ActionManager::toggle_config_state (const char* group, const char* action, bool (RCConfiguration::*set)(bool), bool (RCConfiguration::*get)(void) const)
111 {
112 	Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
113 
114 	if (act) {
115 		Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
116 
117 		if (tact) {
118 			bool x = (Config->*get)();
119 
120 			if (x != tact->get_active()) {
121 				(Config->*set) (!x);
122 			}
123 		}
124 	}
125 }
126 
127 /** Examine the state of a Configuration setting and a toggle action, and toggle the Configuration
128  * setting if its state doesn't match the toggle action.
129  * @param group Action group.
130  * @param action Action name.
131  * @param Method to set the state of the Configuration setting.
132  * @param Method to get the state of the Configuration setting.
133  */
134 void
toggle_config_state(const char * group,const char * action,bool (UIConfiguration::* set)(bool),bool (UIConfiguration::* get)(void)const)135 ActionManager::toggle_config_state (const char* group, const char* action, bool (UIConfiguration::*set)(bool), bool (UIConfiguration::*get)(void) const)
136 {
137 	Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
138 
139 	if (act) {
140 		Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
141 
142 		if (tact) {
143 			bool x = (UIConfiguration::instance().*get)();
144 
145 			if (x != tact->get_active()) {
146 				(UIConfiguration::instance().*set) (!x);
147 			}
148 		}
149 	}
150 }
151 
152 void
toggle_config_state_foo(const char * group,const char * action,sigc::slot<bool,bool> set,sigc::slot<bool> get)153 ActionManager::toggle_config_state_foo (const char* group, const char* action, sigc::slot<bool, bool> set, sigc::slot<bool> get)
154 {
155 	Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
156 
157 	if (act) {
158 		Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
159 
160 		if (tact) {
161 			bool const x = get ();
162 
163 			if (x != tact->get_active ()) {
164 				set (!x);
165 			}
166 		}
167 	}
168 }
169 
170 
171 /** Set the state of a ToggleAction using a particular Configuration get() method
172  * @param group Action group.
173  * @param action Action name.
174  * @param get Method to obtain the state that the ToggleAction should have.
175  */
176 void
map_some_state(const char * group,const char * action,bool (RCConfiguration::* get)()const)177 ActionManager::map_some_state (const char* group, const char* action, bool (RCConfiguration::*get)() const)
178 {
179 	Glib::RefPtr<Action> act = ActionManager::get_action (group, action, false);
180 	if (act) {
181 		Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
182 
183 		if (tact) {
184 
185 			bool x = (Config->*get)();
186 
187 			if (tact->get_active() != x) {
188 				tact->set_active (x);
189 			}
190 		}
191 	}
192 }
193 
194 /** Set the state of a ToggleAction using a particular Configuration get() method
195  * @param group Action group.
196  * @param action Action name.
197  * @param get Method to obtain the state that the ToggleAction should have.
198  */
199 void
map_some_state(const char * group,const char * action,bool (UIConfiguration::* get)()const)200 ActionManager::map_some_state (const char* group, const char* action, bool (UIConfiguration::*get)() const)
201 {
202 	Glib::RefPtr<Action> act = ActionManager::get_action (group, action, false);
203 	if (act) {
204 		Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
205 
206 		if (tact) {
207 
208 			bool x = (UIConfiguration::instance().*get)();
209 
210 			if (tact->get_active() != x) {
211 				tact->set_active (x);
212 			}
213 		}
214 	}
215 }
216 
217 void
map_some_state(const char * group,const char * action,sigc::slot<bool> get)218 ActionManager::map_some_state (const char* group, const char* action, sigc::slot<bool> get)
219 {
220 	Glib::RefPtr<Action> act = ActionManager::get_action (group, action, false);
221 	if (act) {
222 		Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
223 
224 		if (tact) {
225 
226 			bool const x = get ();
227 
228 			if (tact->get_active() != x) {
229 				tact->set_active (x);
230 			}
231 		}
232 	}
233 }
234 
235