major design changes: use glib event loop for MIDI thread/UI; rework design of BaseUI...
[ardour.git] / libs / gtkmm2ext / stateful_button.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 <string>
21 #include <iostream>
22
23 #include <gtkmm/main.h>
24
25 #include "pbd/stacktrace.h"
26
27 #include <gtkmm2ext/stateful_button.h>
28
29 using namespace Gtk;
30 using namespace Glib;
31 using namespace Gtkmm2ext;
32 using namespace std;
33
34 StateButton::StateButton () : visual_state (0), _self_managed (false), _is_realized (false)
35 {
36   
37 }
38
39 void
40 StateButton::set_visual_state (int n)
41 {
42         if (!_is_realized) {
43                 /* not yet realized */
44                 visual_state = n;
45                 return;
46         }
47
48         if (n == visual_state) {
49                 return;
50         }
51
52         string name = get_widget_name ();
53         name = name.substr (0, name.find_last_of ('-'));
54
55         switch (n) {
56         case 0:
57                 /* relax */
58                 break;
59         case 1:
60                 name += "-active";
61                 break;
62         case 2:
63                 name += "-alternate";
64                 break;
65         }
66
67         set_widget_name (name);
68         visual_state = n;
69 }
70
71 /* ----------------------------------------------------------------- */
72
73 void
74 StatefulToggleButton::on_realize ()
75 {
76         ToggleButton::on_realize ();
77
78         _is_realized = true;
79         visual_state++; // to force transition
80         set_visual_state (visual_state - 1);
81 }
82
83 void
84 StatefulButton::on_realize ()
85 {
86         Button::on_realize ();
87
88         _is_realized = true;
89         visual_state++; // to force transition
90         set_visual_state (visual_state - 1);
91 }
92
93 void
94 StatefulToggleButton::on_toggled ()
95 {
96         if (!_self_managed) {
97                 if (get_active()) {
98                         set_visual_state (1);
99                 } else {
100                         set_visual_state (0);
101                 }
102         }
103 }
104
105 void
106 StatefulToggleButton::set_widget_name (const std::string& name)
107 {
108         set_name (name); 
109         Widget* w = get_child();
110
111         if (w) {
112                 w->set_name (name); 
113         } else {
114                 cerr << "Statefull TOggle button - no child\n";
115                 PBD::stacktrace (cerr, 20);
116         }
117 }
118
119 void
120 StatefulButton::set_widget_name (const std::string& name)
121 {
122         set_name (name); 
123         Widget* w = get_child();
124
125         if (w) {
126                 w->set_name (name); 
127         } else {
128                 cerr << "Stateful button - no child\n";
129                 PBD::stacktrace (cerr, 20);
130         }
131 }