2 Copyright (C) 2005 Paul Davis
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.
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.
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.
26 #include <gtk/gtkaccelmap.h>
27 #include <gtk/gtkuimanager.h>
28 #include <gtk/gtkactiongroup.h>
30 #include <gtkmm/accelmap.h>
31 #include <gtkmm/uimanager.h>
33 #include "pbd/error.h"
35 #include "gtkmm2ext/actions.h"
36 #include "gtkmm2ext/utils.h"
45 using namespace Gtkmm2ext
;
47 RefPtr
<UIManager
> ActionManager::ui_manager
;
48 string
ActionManager::unbound_string
= "--";
52 ActionManager::register_action (RefPtr
<ActionGroup
> group
, const char * name
, const char * label
, slot
<void> sl
)
56 act
= Action::create (name
, label
);
63 ActionManager::register_action (RefPtr
<ActionGroup
> group
, const char * name
, const char * label
)
67 act
= Action::create (name
, label
);
75 ActionManager::register_radio_action (RefPtr
<ActionGroup
> group
, RadioAction::Group
& rgroup
, const char * name
, const char * label
, slot
<void> sl
)
79 act
= RadioAction::create (rgroup
, name
, label
);
86 ActionManager::register_toggle_action (RefPtr
<ActionGroup
> group
, const char * name
, const char * label
, slot
<void> sl
)
90 act
= ToggleAction::create (name
, label
);
97 ActionManager::lookup_entry (const ustring accel_path
, Gtk::AccelKey
& key
)
100 bool known
= gtk_accel_map_lookup_entry (accel_path
.c_str(), &gkey
);
103 key
= AccelKey (gkey
.accel_key
, Gdk::ModifierType (gkey
.accel_mods
));
105 key
= AccelKey (GDK_VoidSymbol
, Gdk::ModifierType (0));
111 struct SortActionsByLabel
{
112 bool operator() (Glib::RefPtr
<Gtk::Action
> a
, Glib::RefPtr
<Gtk::Action
> b
) {
113 ustring astr
= a
->get_accel_path();
114 ustring bstr
= b
->get_accel_path();
120 ActionManager::get_all_actions (vector
<string
>& groups
, vector
<string
>& names
, vector
<AccelKey
>& bindings
)
122 /* the C++ API for functions used here appears to be broken in
123 gtkmm2.6, so we fall back to the C level.
126 GList
* list
= gtk_ui_manager_get_action_groups (ui_manager
->gobj());
130 for (node
= list
; node
; node
= g_list_next (node
)) {
132 GtkActionGroup
* group
= (GtkActionGroup
*) node
->data
;
134 /* first pass: collect them all */
136 typedef std::list
<Glib::RefPtr
<Gtk::Action
> > action_list
;
137 action_list the_acts
;
139 for (acts
= gtk_action_group_list_actions (group
); acts
; acts
= g_list_next (acts
)) {
140 GtkAction
* action
= (GtkAction
*) acts
->data
;
141 the_acts
.push_back (Glib::wrap (action
, true));
144 /* now sort by label */
146 SortActionsByLabel cmp
;
149 for (action_list::iterator a
= the_acts
.begin(); a
!= the_acts
.end(); ++a
) {
151 string accel_path
= (*a
)->get_accel_path ();
153 groups
.push_back (gtk_action_group_get_name(group
));
154 names
.push_back (accel_path
.substr (accel_path
.find_last_of ('/') + 1));
157 lookup_entry (accel_path
, key
);
158 bindings
.push_back (AccelKey (key
.get_key(), Gdk::ModifierType (key
.get_mod())));
164 ActionManager::get_all_actions (vector
<string
>& names
, vector
<string
>& paths
, vector
<string
>& keys
, vector
<AccelKey
>& bindings
)
166 /* the C++ API for functions used here appears to be broken in
167 gtkmm2.6, so we fall back to the C level.
170 GList
* list
= gtk_ui_manager_get_action_groups (ui_manager
->gobj());
174 for (node
= list
; node
; node
= g_list_next (node
)) {
176 GtkActionGroup
* group
= (GtkActionGroup
*) node
->data
;
178 /* first pass: collect them all */
180 typedef std::list
<Glib::RefPtr
<Gtk::Action
> > action_list
;
181 action_list the_acts
;
183 for (acts
= gtk_action_group_list_actions (group
); acts
; acts
= g_list_next (acts
)) {
184 GtkAction
* action
= (GtkAction
*) acts
->data
;
185 the_acts
.push_back (Glib::wrap (action
, true));
188 /* now sort by label */
190 SortActionsByLabel cmp
;
193 for (action_list::iterator a
= the_acts
.begin(); a
!= the_acts
.end(); ++a
) {
195 string accel_path
= (*a
)->get_accel_path ();
196 ustring label
= (*a
)->property_label();
198 names
.push_back (label
);
199 paths
.push_back (accel_path
);
202 keys
.push_back (get_key_representation (accel_path
, key
));
203 bindings
.push_back (AccelKey (key
.get_key(), Gdk::ModifierType (key
.get_mod())));
209 ActionManager::add_action_group (RefPtr
<ActionGroup
> grp
)
211 ui_manager
->insert_action_group (grp
);
215 ActionManager::get_widget (const char * name
)
217 return ui_manager
->get_widget (name
);
221 ActionManager::get_action (const char* path
)
226 if ((_act
= gtk_ui_manager_get_action (ui_manager
->gobj(), path
)) != 0) {
227 return Glib::wrap (_act
, true);
234 ActionManager::get_action (const char* group_name
, const char* action_name
)
236 /* the C++ API for functions used here appears to be broken in
237 gtkmm2.6, so we fall back to the C level.
240 GList
* list
= gtk_ui_manager_get_action_groups (ui_manager
->gobj());
244 for (node
= list
; node
; node
= g_list_next (node
)) {
246 GtkActionGroup
* _ag
= (GtkActionGroup
*) node
->data
;
248 if (strcmp (group_name
, gtk_action_group_get_name (_ag
)) == 0) {
252 if ((_act
= gtk_action_group_get_action (_ag
, action_name
)) != 0) {
253 act
= Glib::wrap (_act
, true);
263 ActionManager::set_sensitive (vector
<RefPtr
<Action
> >& actions
, bool state
)
265 for (vector
<RefPtr
<Action
> >::iterator i
= actions
.begin(); i
!= actions
.end(); ++i
) {
266 (*i
)->set_sensitive (state
);
271 ActionManager::uncheck_toggleaction (const char * name
)
273 const char *last_slash
= strrchr (name
, '/');
275 if (last_slash
== 0) {
276 fatal
<< string_compose ("programmer error: %1 %2", "illegal toggle action name", name
) << endmsg
;
281 /* 10 = strlen ("<Actions>/") */
282 size_t len
= last_slash
- (name
+ 10);
284 char* group_name
= new char[len
+1];
285 memcpy (group_name
, name
+ 10, len
);
286 group_name
[len
] = '\0';
288 const char* action_name
= last_slash
+ 1;
290 RefPtr
<Action
> act
= get_action (group_name
, action_name
);
292 RefPtr
<ToggleAction
> tact
= RefPtr
<ToggleAction
>::cast_dynamic(act
);
293 tact
->set_active (false);
295 error
<< string_compose (_("Unknown action name: %1"), name
) << endmsg
;
298 delete [] group_name
;
302 ActionManager::get_key_representation (const string
& accel_path
, AccelKey
& key
)
304 bool known
= lookup_entry (accel_path
, key
);
307 uint32_t k
= possibly_translate_legal_accelerator_to_real_key (key
.get_key());
308 key
= AccelKey (k
, Gdk::ModifierType (key
.get_mod()));
309 return ui_manager
->get_accel_group()->name (key
.get_key(), Gdk::ModifierType (key
.get_mod()));
312 return unbound_string
;