074d0866511bcd0326c42574fd6f3c2f8ae193d9
[ardour.git] / libs / gtkmm2ext / stateful_button.cc
1 #include <string>
2 #include <iostream>
3 #include "gtkmm2ext/stateful_button.h"
4
5 using namespace Gtk;
6 using namespace Glib;
7 using namespace Gtkmm2ext;
8 using namespace std;
9
10 StatefulButton::StatefulButton ()
11 {
12         current_state = 0;
13         have_saved_bg = false;
14 }
15
16 StatefulButton::StatefulButton (const string& label)
17         : Button (label)
18 {
19         current_state = 0;
20         have_saved_bg = false;
21 }
22
23 void
24 StatefulButton::set_colors (const vector<Gdk::Color>& c)
25 {
26         colors = c;
27         current_state++; // to force transition
28         set_state (current_state - 1);
29 }
30
31 void
32 StatefulButton::on_realize ()
33 {
34         Button::on_realize ();
35
36         if (!have_saved_bg) {
37                 saved_bg = get_style()->get_bg (STATE_NORMAL);
38                 have_saved_bg = true;
39         }
40
41         current_state++; // to force transition
42         set_state (current_state - 1);
43 }
44
45 void
46 StatefulButton::set_state (int n)
47 {
48         if (is_realized()) {
49
50                 if (n == current_state) {
51                         return;
52                 }
53                 
54                 if (n == 0) {
55                         
56                         /* back to the default color */
57                         
58                         if (have_saved_bg) {
59                                 modify_bg (STATE_NORMAL, saved_bg);
60                                 modify_bg (STATE_ACTIVE, saved_bg);
61                                 modify_bg (STATE_SELECTED, saved_bg);
62                                 modify_bg (STATE_PRELIGHT, saved_bg);
63                         }
64                         
65
66                 } else {
67                         
68                         int index = (n-1) % colors.size ();
69                         
70                         modify_bg (STATE_NORMAL, colors[index]);
71                         modify_bg (STATE_ACTIVE, colors[index]);
72                         modify_bg (STATE_SELECTED, colors[index]);
73                         modify_bg (STATE_PRELIGHT, colors[index]);
74                 }
75                 
76                 /* leave insensitive alone */
77         }
78
79         current_state = n;
80 }