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_radio_action (
87 RefPtr
<ActionGroup
> group
, RadioAction::Group
& rgroup
, string
const & name
, string
const & label
, string
const & tooltip
, slot
<void> sl
92 act
= RadioAction::create (rgroup
, name
, label
, tooltip
);
99 ActionManager::register_toggle_action (RefPtr
<ActionGroup
> group
, const char * name
, const char * label
, slot
<void> sl
)
103 act
= ToggleAction::create (name
, label
);
104 group
->add (act
, sl
);
110 ActionManager::register_toggle_action (RefPtr
<ActionGroup
> group
, string
const & name
, string
const & label
, string
const & tooltip
, slot
<void> sl
)
114 act
= ToggleAction::create (name
, label
, tooltip
);
115 group
->add (act
, sl
);
121 ActionManager::lookup_entry (const ustring accel_path
, Gtk::AccelKey
& key
)
124 bool known
= gtk_accel_map_lookup_entry (accel_path
.c_str(), &gkey
);
127 key
= AccelKey (gkey
.accel_key
, Gdk::ModifierType (gkey
.accel_mods
));
129 key
= AccelKey (GDK_VoidSymbol
, Gdk::ModifierType (0));
135 struct SortActionsByLabel
{
136 bool operator() (Glib::RefPtr
<Gtk::Action
> a
, Glib::RefPtr
<Gtk::Action
> b
) {
137 ustring astr
= a
->get_accel_path();
138 ustring bstr
= b
->get_accel_path();
144 ActionManager::get_all_actions (vector
<string
>& groups
, vector
<string
>& names
, vector
<string
>& tooltips
, vector
<AccelKey
>& bindings
)
146 /* the C++ API for functions used here appears to be broken in
147 gtkmm2.6, so we fall back to the C level.
150 GList
* list
= gtk_ui_manager_get_action_groups (ui_manager
->gobj());
154 for (node
= list
; node
; node
= g_list_next (node
)) {
156 GtkActionGroup
* group
= (GtkActionGroup
*) node
->data
;
158 /* first pass: collect them all */
160 typedef std::list
<Glib::RefPtr
<Gtk::Action
> > action_list
;
161 action_list the_acts
;
163 for (acts
= gtk_action_group_list_actions (group
); acts
; acts
= g_list_next (acts
)) {
164 GtkAction
* action
= (GtkAction
*) acts
->data
;
165 the_acts
.push_back (Glib::wrap (action
, true));
168 /* now sort by label */
170 SortActionsByLabel cmp
;
173 for (action_list::iterator a
= the_acts
.begin(); a
!= the_acts
.end(); ++a
) {
175 string accel_path
= (*a
)->get_accel_path ();
177 groups
.push_back (gtk_action_group_get_name(group
));
178 names
.push_back (accel_path
.substr (accel_path
.find_last_of ('/') + 1));
179 tooltips
.push_back ((*a
)->get_tooltip ());
182 lookup_entry (accel_path
, key
);
183 bindings
.push_back (AccelKey (key
.get_key(), Gdk::ModifierType (key
.get_mod())));
189 ActionManager::get_all_actions (vector
<string
>& names
, vector
<string
>& paths
, vector
<string
>& tooltips
, vector
<string
>& keys
, vector
<AccelKey
>& bindings
)
191 /* the C++ API for functions used here appears to be broken in
192 gtkmm2.6, so we fall back to the C level.
195 GList
* list
= gtk_ui_manager_get_action_groups (ui_manager
->gobj());
199 for (node
= list
; node
; node
= g_list_next (node
)) {
201 GtkActionGroup
* group
= (GtkActionGroup
*) node
->data
;
203 /* first pass: collect them all */
205 typedef std::list
<Glib::RefPtr
<Gtk::Action
> > action_list
;
206 action_list the_acts
;
208 for (acts
= gtk_action_group_list_actions (group
); acts
; acts
= g_list_next (acts
)) {
209 GtkAction
* action
= (GtkAction
*) acts
->data
;
210 the_acts
.push_back (Glib::wrap (action
, true));
213 /* now sort by label */
215 SortActionsByLabel cmp
;
218 for (action_list::iterator a
= the_acts
.begin(); a
!= the_acts
.end(); ++a
) {
220 ustring
const label
= (*a
)->property_label ();
221 string
const accel_path
= (*a
)->get_accel_path ();
223 names
.push_back (label
);
224 paths
.push_back (accel_path
);
225 tooltips
.push_back ((*a
)->get_tooltip ());
228 keys
.push_back (get_key_representation (accel_path
, key
));
229 bindings
.push_back (AccelKey (key
.get_key(), Gdk::ModifierType (key
.get_mod())));
235 ActionManager::add_action_group (RefPtr
<ActionGroup
> grp
)
237 ui_manager
->insert_action_group (grp
);
241 ActionManager::get_widget (const char * name
)
243 return ui_manager
->get_widget (name
);
247 ActionManager::get_action (const char* path
)
252 if ((_act
= gtk_ui_manager_get_action (ui_manager
->gobj(), path
)) != 0) {
253 return Glib::wrap (_act
, true);
260 ActionManager::get_action (const char* group_name
, const char* action_name
)
262 /* the C++ API for functions used here appears to be broken in
263 gtkmm2.6, so we fall back to the C level.
266 if (ui_manager
== 0) {
267 return RefPtr
<Action
> ();
270 GList
* list
= gtk_ui_manager_get_action_groups (ui_manager
->gobj());
274 for (node
= list
; node
; node
= g_list_next (node
)) {
276 GtkActionGroup
* _ag
= (GtkActionGroup
*) node
->data
;
278 if (strcmp (group_name
, gtk_action_group_get_name (_ag
)) == 0) {
282 if ((_act
= gtk_action_group_get_action (_ag
, action_name
)) != 0) {
283 act
= Glib::wrap (_act
, true);
293 ActionManager::set_sensitive (vector
<RefPtr
<Action
> >& actions
, bool state
)
295 for (vector
<RefPtr
<Action
> >::iterator i
= actions
.begin(); i
!= actions
.end(); ++i
) {
296 (*i
)->set_sensitive (state
);
301 ActionManager::uncheck_toggleaction (string n
)
303 char const * name
= n
.c_str ();
305 const char *last_slash
= strrchr (name
, '/');
307 if (last_slash
== 0) {
308 fatal
<< string_compose ("programmer error: %1 %2", "illegal toggle action name", name
) << endmsg
;
313 /* 10 = strlen ("<Actions>/") */
314 size_t len
= last_slash
- (name
+ 10);
316 char* group_name
= new char[len
+1];
317 memcpy (group_name
, name
+ 10, len
);
318 group_name
[len
] = '\0';
320 const char* action_name
= last_slash
+ 1;
322 RefPtr
<Action
> act
= get_action (group_name
, action_name
);
324 RefPtr
<ToggleAction
> tact
= RefPtr
<ToggleAction
>::cast_dynamic(act
);
325 tact
->set_active (false);
327 error
<< string_compose (_("Unknown action name: %1"), name
) << endmsg
;
330 delete [] group_name
;
334 ActionManager::get_key_representation (const string
& accel_path
, AccelKey
& key
)
336 bool known
= lookup_entry (accel_path
, key
);
339 uint32_t k
= possibly_translate_legal_accelerator_to_real_key (key
.get_key());
340 key
= AccelKey (k
, Gdk::ModifierType (key
.get_mod()));
341 return ui_manager
->get_accel_group()->name (key
.get_key(), Gdk::ModifierType (key
.get_mod()));
344 return unbound_string
;