summaryrefslogtreecommitdiff
path: root/libs/gtkmm2ext/stateful_button.cc
blob: 074d0866511bcd0326c42574fd6f3c2f8ae193d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <string>
#include <iostream>
#include "gtkmm2ext/stateful_button.h"

using namespace Gtk;
using namespace Glib;
using namespace Gtkmm2ext;
using namespace std;

StatefulButton::StatefulButton ()
{
	current_state = 0;
	have_saved_bg = false;
}

StatefulButton::StatefulButton (const string& label)
	: Button (label)
{
	current_state = 0;
	have_saved_bg = false;
}

void
StatefulButton::set_colors (const vector<Gdk::Color>& c)
{
	colors = c;
	current_state++; // to force transition
	set_state (current_state - 1);
}

void
StatefulButton::on_realize ()
{
	Button::on_realize ();

	if (!have_saved_bg) {
		saved_bg = get_style()->get_bg (STATE_NORMAL);
		have_saved_bg = true;
	}

	current_state++; // to force transition
	set_state (current_state - 1);
}

void
StatefulButton::set_state (int n)
{
	if (is_realized()) {

		if (n == current_state) {
			return;
		}
		
		if (n == 0) {
			
			/* back to the default color */
			
			if (have_saved_bg) {
				modify_bg (STATE_NORMAL, saved_bg);
				modify_bg (STATE_ACTIVE, saved_bg);
				modify_bg (STATE_SELECTED, saved_bg);
				modify_bg (STATE_PRELIGHT, saved_bg);
			}
			

		} else {
			
			int index = (n-1) % colors.size ();
			
			modify_bg (STATE_NORMAL, colors[index]);
			modify_bg (STATE_ACTIVE, colors[index]);
			modify_bg (STATE_SELECTED, colors[index]);
			modify_bg (STATE_PRELIGHT, colors[index]);
		}
		
		/* leave insensitive alone */
	}

	current_state = n;
}