more fixes on the long road to compiling
[ardour.git] / gtk2_ardour / mtest.cc
1 #include <vector>
2 #include <iostream>
3 #include <gtkmm.h>
4 #include <gtkmm/accelmap.h>
5 #include <gdk/gdkkeysyms.h>
6 #include <gtk/gtkaccelmap.h>
7
8 using namespace Gtk;
9 using namespace std;
10 using namespace sigc;
11 using namespace Glib;
12
13 struct ActionBinding {
14     Glib::ustring             name;
15     Glib::ustring             label;
16     Gtk::Action::SlotActivate binding;
17     guint                     key;
18     Gdk::ModifierType         mods;
19
20     ActionBinding (Glib::ustring n, Glib::ustring l, Gtk::Action::SlotActivate b, 
21                    guint k = GDK_VoidSymbol, Gdk::ModifierType m = Gdk::ModifierType (0)) 
22             : name (n),
23               label (l),
24               binding (b),
25               key (k),
26               mods (m) {}
27 };
28
29
30 void
31 printit (string txt)
32 {
33         cout << "This is the " << txt << " item\n";
34 }
35
36 Glib::RefPtr<Action>
37 make_action (vector<Glib::RefPtr<ActionGroup> >& groups, string name, string label, slot<void> sl, guint key, Gdk::ModifierType mods)
38 {
39         Glib::RefPtr<Action> last;
40
41         for (vector<RefPtr<ActionGroup> >::iterator g = groups.begin(); g != groups.end(); ++g) {
42                 Glib::RefPtr<Action> act = Action::create (name, label);
43                 (*g)->add (act, sl);
44                 AccelMap::add_entry (act->get_accel_path(), key, mods);
45                 last = act;
46         }
47
48         return last;
49 }
50
51 Glib::RefPtr<Action>
52 make_action (Glib::RefPtr<ActionGroup> group, Glib::RefPtr<AccelGroup> accel_group, string name, string label, slot<void> sl, guint key, Gdk::ModifierType mods)
53 {
54         Glib::RefPtr<Action> act;
55
56         act = Action::create (name, label);
57         group->add (act, sl);
58         AccelMap::add_entry (act->get_accel_path(), key, mods);
59         act->set_accel_group (accel_group);
60
61         cerr << "action " << name << " has path " << act->get_accel_path() << endl;
62         
63         return act;
64 }
65
66 Glib::RefPtr<Action>
67 make_action (Glib::RefPtr<ActionGroup> group, string name, string label, slot<void> sl, guint key, Gdk::ModifierType mods)
68 {
69         Glib::RefPtr<Action> act;
70
71         act = Action::create (name, label);
72         group->add (act, sl);
73         AccelMap::add_entry (act->get_accel_path(), key, mods);
74
75         cerr << "action " << name << " has path " << act->get_accel_path() << endl;
76         
77         return act;
78 }
79
80 Glib::RefPtr<Action>
81 make_action (Glib::RefPtr<ActionGroup> group, string name, string label, slot<void> sl)
82 {
83         Glib::RefPtr<Action> act;
84
85         act = Action::create (name, label);
86         group->add (act, sl);
87
88         cerr << "action " << name << " has path " << act->get_accel_path() << endl;
89
90         return act;
91 }
92
93 Glib::RefPtr<Action>
94 make_action (Glib::RefPtr<ActionGroup> group, string name, string label)
95 {
96         Glib::RefPtr<Action> act;
97
98         act = Action::create (name, label);
99         group->add (act);
100
101         cerr << "action " << name << " has path " << act->get_accel_path() << endl;
102
103         return act;
104 }
105
106 bool 
107 lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
108 {
109         GtkAccelKey gkey;
110         bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
111         
112         if (known) {
113                 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
114         } else {
115                 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
116         }
117
118         return known;
119 }
120
121 RefPtr<ActionGroup>
122 make_shared_action_group (ustring name, vector<ActionBinding*>& actions)
123 {
124         RefPtr<ActionGroup> grp = ActionGroup::create (name);
125
126         for (vector<ActionBinding*>::iterator i = actions.begin(); i != actions.end(); ++i) {
127                 RefPtr<Action> act = Action::create ((*i)->name, (*i)->label);
128                 grp->add (act);
129
130                 if ((*i)->key != GDK_VoidSymbol) {
131                         Gtk::AccelKey key;
132
133                         /* since this is a shared action, only add it once */
134
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;
138                         }
139                 }
140         }
141
142         return grp;
143 }
144
145
146 int
147 main (int argc, char* argv[])
148 {
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");
154         VBox   vpacker;
155         VBox   other_vpacker;
156
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;
163
164         window.set_name ("Editor");
165         window.set_title ("Editor");
166
167         other_window.set_name ("Other");
168         other_window.set_title ("Other");
169
170         uimanager = UIManager::create();
171         other_uimanager = UIManager::create();
172
173         actions = ActionGroup::create("MyActions");
174         other_actions = ActionGroup::create("OtherActions");
175
176         uimanager->add_ui_from_file ("mtest.menus");
177         other_uimanager->add_ui_from_file ("mtest_other.menus");
178         
179         // AccelMap::load ("mtest.bindings");
180
181         vector<RefPtr<ActionGroup> > all_groups;
182         all_groups.push_back (actions);
183         all_groups.push_back (other_actions);
184         
185         make_action (actions, "TopMenu", "top");
186         make_action (actions, "Foo", "foo", bind (sigc::ptr_fun (printit), "foo"), GDK_p, Gdk::ModifierType (0));
187
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));
190
191         vector<ActionBinding*> shared_actions;
192
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));
195
196         RefPtr<Action> act = Action::create (shared_actions.back()->name, shared_actions.back()->label);
197         
198         act->connect_proxy (button);
199         act->connect_proxy (other_button);
200
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));
205
206         other_window.add_accel_group (other_uimanager->get_accel_group());
207         window.add_accel_group (uimanager->get_accel_group());
208
209         Gtk::MenuBar* m;
210
211         m = dynamic_cast<MenuBar*>(other_uimanager->get_widget ("/OTop"));
212
213         other_vpacker.pack_start (*m);
214         other_vpacker.pack_start (other_button);
215
216         other_window.add (other_vpacker);
217         other_window.show_all ();
218
219         m = dynamic_cast<MenuBar*>(uimanager->get_widget ("/Top"));
220
221         vpacker.pack_start (*m);
222         vpacker.pack_start (button);
223
224         window.add (vpacker);
225         window.show_all ();
226
227         Settings::get_default()->property_gtk_can_change_accels() = true;
228
229         AccelMap::save ("mtest.bindings");
230
231         app.run ();
232
233         return 0;
234 }