remove, convert, comment several cerr statements
[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/ardour.h"
35 #include "ardour/filesystem_paths.h"
36 #include "ardour/rc_configuration.h"
37
38 #include "gtkmm2ext/actions.h"
39
40 #include "utils.h"
41 #include "actions.h"
42 #include "i18n.h"
43
44 using namespace std;
45 using namespace Gtk;
46 using namespace Glib;
47 using namespace PBD;
48 using namespace ARDOUR;
49
50 vector<RefPtr<Gtk::Action> > ActionManager::session_sensitive_actions;
51 vector<RefPtr<Gtk::Action> > ActionManager::write_sensitive_actions;
52 vector<RefPtr<Gtk::Action> > ActionManager::region_list_selection_sensitive_actions;
53 vector<RefPtr<Gtk::Action> > ActionManager::plugin_selection_sensitive_actions;
54 vector<RefPtr<Gtk::Action> > ActionManager::track_selection_sensitive_actions;
55 vector<RefPtr<Gtk::Action> > ActionManager::point_selection_sensitive_actions;
56 vector<RefPtr<Gtk::Action> > ActionManager::time_selection_sensitive_actions;
57 vector<RefPtr<Gtk::Action> > ActionManager::line_selection_sensitive_actions;
58 vector<RefPtr<Gtk::Action> > ActionManager::playlist_selection_sensitive_actions;
59 vector<RefPtr<Gtk::Action> > ActionManager::mouse_edit_point_requires_canvas_actions;
60
61 vector<RefPtr<Gtk::Action> > ActionManager::range_sensitive_actions;
62 vector<RefPtr<Gtk::Action> > ActionManager::jack_sensitive_actions;
63 vector<RefPtr<Gtk::Action> > ActionManager::jack_opposite_sensitive_actions;
64 vector<RefPtr<Gtk::Action> > ActionManager::transport_sensitive_actions;
65 vector<RefPtr<Gtk::Action> > ActionManager::edit_point_in_region_sensitive_actions;
66
67
68 void
69 ActionManager::init ()
70 {
71         sys::path ui_file;
72
73         ui_manager = UIManager::create ();
74
75         find_file_in_search_path (ardour_config_search_path(), "ardour.menus", ui_file);
76
77         bool loaded = false;
78
79         try {
80                 ui_manager->add_ui_from_file (ui_file.to_string());
81                 info << string_compose (_("Loading menus from %1"), ui_file.to_string()) << endmsg;
82                 loaded = true;
83         } catch (Glib::MarkupError& err) {
84                 error << string_compose (_("badly formatted UI definition file: %1"), err.what()) << endmsg;
85                 cerr << string_compose (_("badly formatted UI definition file: %1"), err.what()) << endl;
86         } catch (...) {
87                 error << string_compose (_("%1 menu definition file not found"), PROGRAM_NAME) << endmsg;
88         }
89
90         if (!loaded) {
91                 cerr << string_compose (_("%1 will not work without a valid ardour.menus file"), PROGRAM_NAME) << endl;
92                 error << string_compose (_("%1 will not work without a valid ardour.menus file"), PROGRAM_NAME) << endmsg;
93                 exit(1);
94         }
95 }
96
97 /** Examine the state of a Configuration setting and a toggle action, and toggle the Configuration
98  * setting if its state doesn't match the toggle action.
99  * @param group Action group.
100  * @param action Action name.
101  * @param Method to set the state of the Configuration setting.
102  * @param Method to get the state of the Configuration setting.
103  */
104 void
105 ActionManager::toggle_config_state (const char* group, const char* action, bool (RCConfiguration::*set)(bool), bool (RCConfiguration::*get)(void) const)
106 {
107         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
108
109         if (act) {
110                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
111
112                 if (tact) {
113                         bool x = (Config->*get)();
114
115                         if (x != tact->get_active()) {
116                                 (Config->*set) (!x);
117                         }
118                 }
119         }
120 }
121
122 void
123 ActionManager::toggle_config_state_foo (const char* group, const char* action, sigc::slot<bool, bool> set, sigc::slot<bool> get)
124 {
125         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
126
127         if (act) {
128                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
129
130                 if (tact) {
131                         bool const x = get ();
132
133                         if (x != tact->get_active ()) {
134                                 set (!x);
135                         }
136                 }
137         }
138 }
139
140
141 /** Set the state of a ToggleAction using a particular Configuration get() method
142  * @param group Action group.
143  * @param action Action name.
144  * @param get Method to obtain the state that the ToggleAction should have.
145  */
146 void
147 ActionManager::map_some_state (const char* group, const char* action, bool (RCConfiguration::*get)() const)
148 {
149         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
150         if (act) {
151                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
152
153                 if (tact) {
154
155                         bool x = (Config->*get)();
156
157                         if (tact->get_active() != x) {
158                                 tact->set_active (x);
159                         }
160                 }
161         }
162 }
163
164 void
165 ActionManager::map_some_state (const char* group, const char* action, sigc::slot<bool> get)
166 {
167         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
168         if (act) {
169                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
170
171                 if (tact) {
172
173                         bool const x = get ();
174
175                         if (tact->get_active() != x) {
176                                 tact->set_active (x);
177                         }
178                 }
179         }
180 }
181