]> git.tdb.fi Git - libs/gltk.git/blob - source/button.cpp
Add a signal to notify when the automatic size of a widget changes
[libs/gltk.git] / source / button.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007-2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/gl/immediate.h>
9 #include "button.h"
10 #include "part.h"
11 #include "style.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GLtk {
17
18 Button::Button(const std::string &t):
19         text(),
20         icon(0),
21         pressed(false)
22 {
23         set_text(t);
24 }
25
26 void Button::autosize()
27 {
28         if(!style)
29                 return;
30
31         Widget::autosize();
32
33         if(const Part *text_part = style->get_part("text"))
34         {
35                 const Sides &margin = text_part->get_margin();
36                 geom.w = max(geom.w, text.get_width()+margin.left+margin.right);
37                 geom.h = max(geom.h, text.get_height()+margin.top+margin.bottom);
38         }
39
40         if(icon)
41         {
42                 if(const Part *icon_part = style->get_part("icon"))
43                 {
44                         const Sides &margin = icon_part->get_margin();
45                         geom.w = max(geom.w, icon->get_width()+margin.left+margin.right);
46                         geom.h = max(geom.h, icon->get_height()+margin.top+margin.bottom);
47                 }
48         }
49 }
50
51 void Button::set_text(const std::string &t)
52 {
53         text = t;
54         signal_autosize_changed.emit();
55 }
56
57 void Button::set_icon(const GL::Texture2D *i)
58 {
59         icon = i;
60 }
61
62 void Button::render_special(const Part &part) const
63 {
64         if(part.get_name()=="text")
65                 text.render(part, geom);
66         if(part.get_name()=="icon" && icon)
67         {
68                 Geometry rgeom;
69                 rgeom.w = icon->get_width();
70                 rgeom.h = icon->get_height();
71                 part.get_alignment().apply(rgeom, geom, part.get_margin());
72
73                 icon->bind();
74                 GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
75                 imm.color(1.0f, 1.0f, 1.0f);
76                 imm.begin(GL::QUADS);
77                 imm.texcoord(0, 0);
78                 imm.vertex(rgeom.x, rgeom.y);
79                 imm.texcoord(1, 0);
80                 imm.vertex(rgeom.x+rgeom.w, rgeom.y);
81                 imm.texcoord(1, 1);
82                 imm.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
83                 imm.texcoord(0, 1);
84                 imm.vertex(rgeom.x, rgeom.y+rgeom.h);
85                 imm.end();
86                 GL::Texture::unbind();
87         }
88 }
89
90 void Button::button_press(int, int, unsigned btn)
91 {
92         if(btn==1)
93         {
94                 pressed = true;
95                 state |= ACTIVE;
96         }
97 }
98
99 void Button::button_release(int x, int y, unsigned btn)
100 {
101         if(pressed && btn==1)
102         {
103                 state &= ~ACTIVE;
104                 pressed = false;
105
106                 if(geom.is_inside_relative(x, y))
107                         signal_clicked.emit();
108         }
109 }
110
111 void Button::pointer_motion(int x, int y)
112 {
113         if(pressed)
114         {
115                 if(!geom.is_inside_relative(x, y))
116                         state &= ~ACTIVE;
117                 else
118                         state |= ACTIVE;
119         }
120 }
121
122 void Button::on_style_change()
123 {
124         text.set_style(style);
125 }
126
127
128 Button::Loader::Loader(Button &btn):
129         Widget::Loader(btn)
130 {
131         add("text", &Loader::text);
132 }
133
134 void Button::Loader::text(const std::string &t)
135 {
136         static_cast<Button &>(obj).text = t;
137 }
138
139 } // namespace GLtk
140 } // namespace Msp