add variant of Rect::expand() that allows different amounts in each direction
[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::point_selection_sensitive_actions;
56 RelatedActions ActionManager::time_selection_sensitive_actions;
57 RelatedActions ActionManager::line_selection_sensitive_actions;
58 RelatedActions ActionManager::playlist_selection_sensitive_actions;
59 RelatedActions ActionManager::mouse_edit_point_requires_canvas_actions;
60 RelatedActions ActionManager::range_sensitive_actions;
61 RelatedActions ActionManager::engine_sensitive_actions;
62 RelatedActions ActionManager::engine_opposite_sensitive_actions;
63 RelatedActions ActionManager::transport_sensitive_actions;
64 RelatedActions ActionManager::edit_point_in_region_sensitive_actions;
65 RelatedActions ActionManager::rec_sensitive_actions;
66
67 void
68 ActionManager::init ()
69 {
70         ui_manager = UIManager::create ();
71 }
72
73 void
74 ActionManager::load_menus (const string& menus_file)
75 {
76         std::string ui_file;
77
78         find_file (ardour_config_search_path(), menus_file, ui_file);
79
80         bool loaded = false;
81
82         try {
83                 ui_manager->add_ui_from_file (ui_file);
84                 info << string_compose (_("Loading menus from %1"), ui_file) << endmsg;
85                 loaded = true;
86         } catch (Glib::MarkupError& err) {
87                 error << string_compose (_("badly formatted menu definition file: %1"), err.what()) << endmsg;
88                 cerr << string_compose (_("badly formatted menu definition file: %1"), err.what()) << endl;
89         } catch (...) {
90                 error << string_compose (_("%1 menu definition file not found"), PROGRAM_NAME) << endmsg;
91         }
92
93         if (!loaded) {
94                 cerr << string_compose (_("%1 will not work without a valid menu definition file"), PROGRAM_NAME) << endl;
95                 error << string_compose (_("%1 will not work without a valid menu definition file"), PROGRAM_NAME) << endmsg;
96                 exit(1);
97         }
98 }
99
100 /** Examine the state of a Configuration setting and a toggle action, and toggle the Configuration
101  * setting if its state doesn't match the toggle action.
102  * @param group Action group.
103  * @param action Action name.
104  * @param Method to set the state of the Configuration setting.
105  * @param Method to get the state of the Configuration setting.
106  */
107 void
108 ActionManager::toggle_config_state (const char* group, const char* action, bool (RCConfiguration::*set)(bool), bool (RCConfiguration::*get)(void) const)
109 {
110         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
111
112         if (act) {
113                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
114
115                 if (tact) {
116                         bool x = (Config->*get)();
117
118                         if (x != tact->get_active()) {
119                                 (Config->*set) (!x);
120                         }
121                 }
122         }
123 }
124
125 /** Examine the state of a Configuration setting and a toggle action, and toggle the Configuration
126  * setting if its state doesn't match the toggle action.
127  * @param group Action group.
128  * @param action Action name.
129  * @param Method to set the state of the Configuration setting.
130  * @param Method to get the state of the Configuration setting.
131  */
132 void
133 ActionManager::toggle_config_state (const char* group, const char* action, bool (UIConfiguration::*set)(bool), bool (UIConfiguration::*get)(void) const)
134 {
135         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
136
137         if (act) {
138                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
139
140                 if (tact) {
141                         bool x = (UIConfiguration::instance().*get)();
142
143                         if (x != tact->get_active()) {
144                                 (UIConfiguration::instance().*set) (!x);
145                         }
146                 }
147         }
148 }
149
150 void
151 ActionManager::toggle_config_state_foo (const char* group, const char* action, sigc::slot<bool, bool> set, sigc::slot<bool> get)
152 {
153         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
154
155         if (act) {
156                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
157
158                 if (tact) {
159                         bool const x = get ();
160
161                         if (x != tact->get_active ()) {
162                                 set (!x);
163                         }
164                 }
165         }
166 }
167
168
169 /** Set the state of a ToggleAction using a particular Configuration get() method
170  * @param group Action group.
171  * @param action Action name.
172  * @param get Method to obtain the state that the ToggleAction should have.
173  */
174 void
175 ActionManager::map_some_state (const char* group, const char* action, bool (RCConfiguration::*get)() const)
176 {
177         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
178         if (act) {
179                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
180
181                 if (tact) {
182
183                         bool x = (Config->*get)();
184
185                         if (tact->get_active() != x) {
186                                 tact->set_active (x);
187                         }
188                 }
189         }
190 }
191
192 /** Set the state of a ToggleAction using a particular Configuration get() method
193  * @param group Action group.
194  * @param action Action name.
195  * @param get Method to obtain the state that the ToggleAction should have.
196  */
197 void
198 ActionManager::map_some_state (const char* group, const char* action, bool (UIConfiguration::*get)() const)
199 {
200         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
201         if (act) {
202                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
203
204                 if (tact) {
205
206                         bool x = (UIConfiguration::instance().*get)();
207
208                         if (tact->get_active() != x) {
209                                 tact->set_active (x);
210                         }
211                 }
212         }
213 }
214
215 void
216 ActionManager::map_some_state (const char* group, const char* action, sigc::slot<bool> get)
217 {
218         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
219         if (act) {
220                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
221
222                 if (tact) {
223
224                         bool const x = get ();
225
226                         if (tact->get_active() != x) {
227                                 tact->set_active (x);
228                         }
229                 }
230         }
231 }
232