Comment.
[ardour.git] / gtk2_ardour / route_group_menu.cc
1 /*
2     Copyright (C) 2009 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 <gtkmm/menu.h>
21 #include <gtkmm/stock.h>
22 #include "ardour/session.h"
23 #include "ardour/route_group.h"
24 #include "route_group_menu.h"
25 #include "route_group_dialog.h"
26 #include "i18n.h"
27
28 using namespace Gtk;
29 using namespace ARDOUR;
30 using namespace PBD;
31
32 RouteGroupMenu::RouteGroupMenu (Session* s, PropertyList* plist)
33         : SessionHandlePtr (s)
34         , _default_properties (plist)
35         , _inhibit_group_selected (false)
36         , _selected_route_group (0)
37 {
38         rebuild (0);
39 }
40
41 RouteGroupMenu::~RouteGroupMenu()
42 {
43         delete _default_properties; 
44 }
45
46 void
47 RouteGroupMenu::rebuild (RouteGroup* curr)
48 {
49         using namespace Menu_Helpers;
50
51         _selected_route_group = curr;
52
53         _inhibit_group_selected = true;
54
55         items().clear ();
56
57         items().push_back (MenuElem (_("New group..."), sigc::mem_fun (*this, &RouteGroupMenu::new_group)));
58         items().push_back (SeparatorElem ());
59
60         RadioMenuItem::Group group;
61         items().push_back (RadioMenuElem (group, _("No group"), sigc::bind (sigc::mem_fun (*this, &RouteGroupMenu::set_group), (RouteGroup *) 0)));
62
63         if (curr == 0) {
64                 static_cast<RadioMenuItem*> (&items().back())->set_active ();
65         }
66
67         if (_session) {
68                 _session->foreach_route_group (sigc::bind (sigc::mem_fun (*this, &RouteGroupMenu::add_item), curr, &group));
69         }
70
71         _inhibit_group_selected = false;
72 }
73
74 void
75 RouteGroupMenu::add_item (RouteGroup* rg, RouteGroup* curr, RadioMenuItem::Group* group)
76 {
77         using namespace Menu_Helpers;
78
79         items().push_back (RadioMenuElem (*group, rg->name(), sigc::bind (sigc::mem_fun(*this, &RouteGroupMenu::set_group), rg)));
80
81         if (rg == curr) {
82                 static_cast<RadioMenuItem*> (&items().back())->set_active ();
83         }
84 }
85
86 /** Called when a group is selected from the menu.
87  *  @param Group, or 0 for none.
88  */
89 void
90 RouteGroupMenu::set_group (RouteGroup* g)
91 {
92         if (g == _selected_route_group) {
93                 /* cut off the signal_toggled that GTK emits for an option that is being un-selected
94                    when a new option is being selected instead
95                 */
96                 return;
97         }
98         
99         if (!_inhibit_group_selected) {
100                 GroupSelected (g);
101         }
102
103         _selected_route_group = g;
104 }
105
106 void
107 RouteGroupMenu::new_group ()
108 {
109         if (!_session) {
110                 return;
111         }
112
113         RouteGroup* g = new RouteGroup (*_session, "");
114         g->apply_changes (*_default_properties);
115
116         RouteGroupDialog d (g, Gtk::Stock::NEW);
117         int const r = d.do_run ();
118
119         if (r == Gtk::RESPONSE_OK) {
120                 _session->add_route_group (g);
121                 set_group (g);
122         } else {
123                 delete g;
124         }
125 }