globally remove all trailing whitespace from ardour code base.
[ardour.git] / libs / gtkmm2ext / grouped_buttons.cc
1 /*
2     Copyright (C) 2001 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     $Id$
19 */
20
21 #include <gtkmm.h>
22
23 #include <gtkmm2ext/grouped_buttons.h>
24
25 using namespace std;
26
27 GroupedButtons::GroupedButtons (vector<Gtk::ToggleButton *>& buttonset)
28 {
29         uint32_t n = 0;
30
31         buttons = buttonset;
32
33         for (vector<Gtk::ToggleButton *>::iterator i = buttons.begin(); i != buttons.end(); ++i, ++n) {
34                 if ((*i)->get_active()) {
35                         current_active = n;
36                 }
37                 (*i)->signal_clicked().connect (sigc::bind (mem_fun (*this, &GroupedButtons::one_clicked), n));
38         }
39 }
40
41 GroupedButtons::GroupedButtons (uint32_t nbuttons, uint32_t first_active)
42 {
43         buttons.reserve (nbuttons);
44         current_active = first_active;
45
46         for (uint32_t n = 0; n < nbuttons; ++n) {
47
48                 Gtk::ToggleButton *button;
49                 
50                 button = manage (new (Gtk::ToggleButton));
51                 
52                 if (n == current_active) {
53                         button->set_active (true);
54                 }
55
56                 button->signal_clicked().connect (sigc::bind (mem_fun (*this, &GroupedButtons::one_clicked), n));
57                 buttons.push_back (button);
58         }
59 }
60
61 static gint
62 reactivate_button (void *arg)
63 {
64         Gtk::ToggleButton *b = (Gtk::ToggleButton *) arg;
65         b->set_active (true);
66         return FALSE;
67 }
68
69 void
70 GroupedButtons::one_clicked (uint32_t which)
71 {
72         if (buttons[which]->get_active()) {
73
74                 if (which != current_active) {
75                         uint32_t old = current_active;
76                         current_active = which;
77                         buttons[old]->set_active (false);
78                 }
79
80         } else if (which == current_active) {
81
82                 /* Someobody tried to unset the current active
83                    button by clicking on it. This caused
84                    set_active (false) to be called. We don't
85                    allow that, so just reactivate it.
86
87                    Don't try this right here, because of some
88                    design glitches with GTK+ toggle buttons.
89                    Setting the button back to active from
90                    within the signal emission that marked
91                    it as inactive causes a segfault ...
92                 */
93
94                 g_idle_add (reactivate_button, buttons[which]);
95         }
96 }