Merge libs/ardour and gtk2_ardour with 2.0-ongoing R2837.
[ardour.git] / gtk2_ardour / mtest.cc
1 /*
2     Copyright (C) 2000-2007 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 <vector>
21 #include <iostream>
22 #include <gtkmm.h>
23 #include <gtkmm/accelmap.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <gtk/gtkaccelmap.h>
26
27 using namespace Gtk;
28 using namespace std;
29 using namespace sigc;
30 using namespace Glib;
31
32 void
33 printit (string txt)
34 {
35         cout << "This is the " << txt << " item\n";
36 }
37
38 Glib::RefPtr<Action>
39 make_action (Glib::RefPtr<ActionGroup> group, string name, string label, RefPtr<AccelGroup> accels, slot<void> sl, guint key, Gdk::ModifierType mods)
40 {
41         Glib::RefPtr<Action> act;
42
43         act = Action::create (name, label);
44         group->add (act, sl);
45         AccelMap::add_entry (act->get_accel_path(), key, mods);
46
47         act->set_accel_group (accels);
48
49         cerr << "action " << name << " has path " << act->get_accel_path() << endl;
50         
51         return act;
52 }
53
54 Glib::RefPtr<Action>
55 make_action (Glib::RefPtr<ActionGroup> group, string name, string label)
56 {
57         Glib::RefPtr<Action> act;
58
59         act = Action::create (name, label);
60         group->add (act);
61
62         cerr << "action " << name << " has path " << act->get_accel_path() << endl;
63
64         return act;
65 }
66
67 bool 
68 lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
69 {
70         GtkAccelKey gkey;
71         bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
72         
73         if (known) {
74                 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
75         } else {
76                 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
77         }
78
79         return known;
80 }
81
82 RefPtr<ActionGroup>
83 copy_actions (const RefPtr<ActionGroup> src)
84 {
85         RefPtr<ActionGroup> grp = ActionGroup::create (src->get_name());
86         
87         ListHandle<RefPtr<Action> > group_actions = src->get_actions();
88         
89         for (ListHandle<RefPtr<Action> >::iterator a = group_actions.begin(); a != group_actions.end(); ++a) {
90                 RefPtr<Action> act = Action::create ((*a)->get_name(), (*a)->property_label());
91                 grp->add (act);
92         }
93
94         return grp;
95 }
96
97 int
98 main (int argc, char* argv[])
99 {
100         Main app (argc, argv);
101         Window hidden (WINDOW_TOPLEVEL);
102         Window window (WINDOW_TOPLEVEL);
103         Window other_window (WINDOW_TOPLEVEL);
104         Button button ("click me for baz");
105         Button other_button ("click me for baz");
106         VBox   vpacker;
107         VBox   other_vpacker;
108
109         Glib::RefPtr<ActionGroup> actions;
110         Glib::RefPtr<ActionGroup> other_actions;
111         Glib::RefPtr<ActionGroup> shared_actions;
112         Glib::RefPtr<UIManager> uimanager;
113         Glib::RefPtr<UIManager> other_uimanager;
114         Glib::RefPtr<UIManager> shared_uimanager;
115
116         window.set_name ("Editor");
117         window.set_title ("Editor");
118
119         other_window.set_name ("Other");
120         other_window.set_title ("Other");
121
122         uimanager = UIManager::create();
123         other_uimanager = UIManager::create();
124         shared_uimanager = UIManager::create();
125
126         actions = ActionGroup::create("MyActions");
127         other_actions = ActionGroup::create("OtherActions");
128         shared_actions = ActionGroup::create("SharedActions");
129
130         uimanager->add_ui_from_file ("mtest.menus");
131         other_uimanager->add_ui_from_file ("mtest_other.menus");
132         
133         // AccelMap::load ("mtest.bindings");
134
135         RefPtr<AccelGroup> accels = hidden.get_accel_group();
136
137         make_action (actions, "TopMenu", "top");
138         make_action (actions, "Foo", "foo", accels, bind (sigc::ptr_fun (printit), "foo"), GDK_p, Gdk::ModifierType (0));
139
140         make_action (other_actions, "OTopMenu", "otop");
141         make_action (other_actions, "OFoo", "foo", accels, bind (sigc::ptr_fun (printit), "o-foo"), GDK_p, Gdk::ModifierType (0));
142
143         make_action (shared_actions, "Bar", "bar", accels, bind (sigc::ptr_fun (printit), "barshared"), GDK_p, Gdk::CONTROL_MASK);
144         RefPtr<Action> act = make_action (shared_actions, "Baz", "baz", accels, bind (sigc::ptr_fun (printit), "baz-shared"), GDK_p, Gdk::SHIFT_MASK);
145         
146         act->connect_proxy (button);
147         act->connect_proxy (other_button);
148
149         uimanager->insert_action_group (copy_actions (actions));
150         uimanager->insert_action_group (copy_actions (shared_actions));
151         other_uimanager->insert_action_group (copy_actions (other_actions));
152         other_uimanager->insert_action_group (copy_actions (shared_actions));
153
154         other_window.add_accel_group (accels);
155         // window.add_accel_group (accels);
156
157         Gtk::MenuBar* m;
158
159         m = dynamic_cast<MenuBar*>(other_uimanager->get_widget ("/OTop"));
160
161         other_vpacker.pack_start (*m);
162         other_vpacker.pack_start (other_button);
163
164         other_window.add (other_vpacker);
165         other_window.show_all ();
166
167         m = dynamic_cast<MenuBar*>(uimanager->get_widget ("/Top"));
168
169         vpacker.pack_start (*m);
170         vpacker.pack_start (button);
171
172         window.add (vpacker);
173         window.show_all ();
174
175         Settings::get_default()->property_gtk_can_change_accels() = true;
176
177         AccelMap::save ("mtest.bindings");
178
179         app.run ();
180
181         return 0;
182 }