]> git.tdb.fi Git - libs/gltk.git/blob - source/button.cpp
Implement autosize() method for most widgets
[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 }
55
56 void Button::set_icon(const GL::Texture2D *i)
57 {
58         icon = i;
59 }
60
61 void Button::render_special(const Part &part) const
62 {
63         if(part.get_name()=="text")
64                 text.render(part, geom);
65         if(part.get_name()=="icon" && icon)
66         {
67                 Geometry rgeom;
68                 rgeom.w = icon->get_width();
69                 rgeom.h = icon->get_height();
70                 part.get_alignment().apply(rgeom, geom, part.get_margin());
71
72                 icon->bind();
73                 GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
74                 imm.color(1.0f, 1.0f, 1.0f);
75                 imm.begin(GL::QUADS);
76                 imm.texcoord(0, 0);
77                 imm.vertex(rgeom.x, rgeom.y);
78                 imm.texcoord(1, 0);
79                 imm.vertex(rgeom.x+rgeom.w, rgeom.y);
80                 imm.texcoord(1, 1);
81                 imm.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
82                 imm.texcoord(0, 1);
83                 imm.vertex(rgeom.x, rgeom.y+rgeom.h);
84                 imm.end();
85                 GL::Texture::unbind();
86         }
87 }
88
89 void Button::button_press(int, int, unsigned btn)
90 {
91         if(btn==1)
92         {
93                 pressed = true;
94                 state |= ACTIVE;
95         }
96 }
97
98 void Button::button_release(int x, int y, unsigned btn)
99 {
100         if(pressed && btn==1)
101         {
102                 state &= ~ACTIVE;
103                 pressed = false;
104
105                 if(geom.is_inside_relative(x, y))
106                         signal_clicked.emit();
107         }
108 }
109
110 void Button::pointer_motion(int x, int y)
111 {
112         if(pressed)
113         {
114                 if(!geom.is_inside_relative(x, y))
115                         state &= ~ACTIVE;
116                 else
117                         state |= ACTIVE;
118         }
119 }
120
121 void Button::on_style_change()
122 {
123         text.set_style(style);
124 }
125
126
127 Button::Loader::Loader(Button &btn):
128         Widget::Loader(btn)
129 {
130         add("text", &Loader::text);
131 }
132
133 void Button::Loader::text(const std::string &t)
134 {
135         static_cast<Button &>(obj).text = t;
136 }
137
138 } // namespace GLtk
139 } // namespace Msp