1 //
2 // Copyright (C) 2009 Nick Gasson
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 3 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, see <http://www.gnu.org/licenses/>.
16 //
17
18 #include "gui/ToggleButton.hpp"
19 #include "ILogger.hpp"
20
21 using namespace gui;
22
ToggleButton(const AttributeSet & attrs)23 ToggleButton::ToggleButton(const AttributeSet& attrs)
24 : Widget(attrs),
25 enabled(false)
26 {
27 texture = load_texture(attrs.get<string>("image"));
28 }
29
render(RenderContext & rc) const30 void ToggleButton::render(RenderContext& rc) const
31 {
32 rc.image(x(), y(), width(), height(), texture);
33
34 if (enabled)
35 rc.border(x(), y(), width(), height(), colour::WHITE);
36 }
37
handle_click(int x,int y)38 bool ToggleButton::handle_click(int x, int y)
39 {
40 on();
41 return Widget::handle_click(x, y);
42 }
43
on()44 void ToggleButton::on()
45 {
46 if (enabled == false) {
47 enabled = true;
48 raise(SIG_ENTER);
49 }
50 }
51
off()52 void ToggleButton::off()
53 {
54 if (enabled == true) {
55 enabled = false;
56 raise(SIG_LEAVE);
57 }
58 }
59