4 #include <gtkmm/accelmap.h>
5 #include <gdk/gdkkeysyms.h>
6 #include <gtk/gtkaccelmap.h>
13 struct ActionBinding {
16 Gtk::Action::SlotActivate binding;
18 Gdk::ModifierType mods;
20 ActionBinding (Glib::ustring n, Glib::ustring l, Gtk::Action::SlotActivate b,
21 guint k = GDK_VoidSymbol, Gdk::ModifierType m = Gdk::ModifierType (0))
33 cout << "This is the " << txt << " item\n";
37 make_action (vector<Glib::RefPtr<ActionGroup> >& groups, string name, string label, slot<void> sl, guint key, Gdk::ModifierType mods)
39 Glib::RefPtr<Action> last;
41 for (vector<RefPtr<ActionGroup> >::iterator g = groups.begin(); g != groups.end(); ++g) {
42 Glib::RefPtr<Action> act = Action::create (name, label);
44 AccelMap::add_entry (act->get_accel_path(), key, mods);
52 make_action (Glib::RefPtr<ActionGroup> group, Glib::RefPtr<AccelGroup> accel_group, string name, string label, slot<void> sl, guint key, Gdk::ModifierType mods)
54 Glib::RefPtr<Action> act;
56 act = Action::create (name, label);
58 AccelMap::add_entry (act->get_accel_path(), key, mods);
59 act->set_accel_group (accel_group);
61 cerr << "action " << name << " has path " << act->get_accel_path() << endl;
67 make_action (Glib::RefPtr<ActionGroup> group, string name, string label, slot<void> sl, guint key, Gdk::ModifierType mods)
69 Glib::RefPtr<Action> act;
71 act = Action::create (name, label);
73 AccelMap::add_entry (act->get_accel_path(), key, mods);
75 cerr << "action " << name << " has path " << act->get_accel_path() << endl;
81 make_action (Glib::RefPtr<ActionGroup> group, string name, string label, slot<void> sl)
83 Glib::RefPtr<Action> act;
85 act = Action::create (name, label);
88 cerr << "action " << name << " has path " << act->get_accel_path() << endl;
94 make_action (Glib::RefPtr<ActionGroup> group, string name, string label)
96 Glib::RefPtr<Action> act;
98 act = Action::create (name, label);
101 cerr << "action " << name << " has path " << act->get_accel_path() << endl;
107 lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
110 bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
113 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
115 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
122 make_shared_action_group (ustring name, vector<ActionBinding*>& actions)
124 RefPtr<ActionGroup> grp = ActionGroup::create (name);
126 for (vector<ActionBinding*>::iterator i = actions.begin(); i != actions.end(); ++i) {
127 RefPtr<Action> act = Action::create ((*i)->name, (*i)->label);
130 if ((*i)->key != GDK_VoidSymbol) {
133 /* since this is a shared action, only add it once */
135 if (!lookup_entry (act->get_accel_path(), key)) {
136 AccelMap::add_entry (act->get_accel_path(), (*i)->key, (*i)->mods);
137 cerr << "added accel map entry for " << act->get_accel_path() << endl;
147 main (int argc, char* argv[])
149 Main app (argc, argv);
150 Window window (WINDOW_TOPLEVEL);
151 Window other_window (WINDOW_TOPLEVEL);
152 Button button ("click me for baz");
153 Button other_button ("click me for baz");
157 Glib::RefPtr<ActionGroup> actions;
158 Glib::RefPtr<ActionGroup> other_actions;
159 Glib::RefPtr<ActionGroup> shared_actions;
160 Glib::RefPtr<UIManager> uimanager;
161 Glib::RefPtr<UIManager> other_uimanager;
162 Glib::RefPtr<UIManager> shared_uimanager;
164 window.set_name ("Editor");
165 window.set_title ("Editor");
167 other_window.set_name ("Other");
168 other_window.set_title ("Other");
170 uimanager = UIManager::create();
171 other_uimanager = UIManager::create();
173 actions = ActionGroup::create("MyActions");
174 other_actions = ActionGroup::create("OtherActions");
176 uimanager->add_ui_from_file ("mtest.menus");
177 other_uimanager->add_ui_from_file ("mtest_other.menus");
179 // AccelMap::load ("mtest.bindings");
181 vector<RefPtr<ActionGroup> > all_groups;
182 all_groups.push_back (actions);
183 all_groups.push_back (other_actions);
185 make_action (actions, "TopMenu", "top");
186 make_action (actions, "Foo", "foo", bind (sigc::ptr_fun (printit), "foo"), GDK_p, Gdk::ModifierType (0));
188 make_action (other_actions, "OTopMenu", "otop");
189 make_action (other_actions, "OFoo", "foo", bind (sigc::ptr_fun (printit), "o-foo"), GDK_p, Gdk::ModifierType (0));
191 vector<ActionBinding*> shared_actions;
193 shared_actions.push_back (new ActionBinding ("Bar", "bar", bind (sigc::ptr_fun (printit), "barshared"), GDK_p, Gdk::CONTROL_MASK));
194 shared_actions.push_back (new ActionBinding ("Baz", "baz", bind (sigc::ptr_fun (printit), "baz-shared"), GDK_p, Gdk::SHIFT_MASK));
196 RefPtr<Action> act = Action::create (shared_actions.back()->name, shared_actions.back()->label);
198 act->connect_proxy (button);
199 act->connect_proxy (other_button);
201 uimanager->insert_action_group (actions);
202 uimanager->insert_action_group (make_shared_action_group ("shared", shared_actions));
203 other_uimanager->insert_action_group (other_actions);
204 other_uimanager->insert_action_group (make_shared_action_group ("othershared", shared_actions));
206 other_window.add_accel_group (other_uimanager->get_accel_group());
207 window.add_accel_group (uimanager->get_accel_group());
211 m = dynamic_cast<MenuBar*>(other_uimanager->get_widget ("/OTop"));
213 other_vpacker.pack_start (*m);
214 other_vpacker.pack_start (other_button);
216 other_window.add (other_vpacker);
217 other_window.show_all ();
219 m = dynamic_cast<MenuBar*>(uimanager->get_widget ("/Top"));
221 vpacker.pack_start (*m);
222 vpacker.pack_start (button);
224 window.add (vpacker);
227 Settings::get_default()->property_gtk_can_change_accels() = true;
229 AccelMap::save ("mtest.bindings");