]> git.tdb.fi Git - libs/gltk.git/blob - source/dropdown.cpp
Make font size a property of Style
[libs/gltk.git] / source / dropdown.cpp
1 #include <msp/gl/font.h>
2 #include "dropdown.h"
3 #include "list.h"
4 #include "panel.h"
5 #include "part.h"
6 #include "root.h"
7 #include "style.h"
8 #include "text.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GLtk {
14
15 Dropdown::Dropdown():
16         dropped(false)
17 {
18         add(list);
19         list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
20         list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed));
21 }
22
23 void Dropdown::autosize()
24 {
25         if(!style)
26                 return;
27
28         Widget::autosize();
29         geom.w = max(geom.w, list.get_geometry().w);
30
31         if(const Part *text_part = style->get_part("text"))
32         {
33                 const Sides &margin = text_part->get_margin();
34                 const GL::Font *font = style->get_font();
35                 float font_size = style->get_font_size();
36                 unsigned line_height = static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
37                 geom.h = max(geom.h, line_height+margin.top+margin.bottom);
38         }
39 }
40
41 void Dropdown::append(const string &item)
42 {
43         list.append(item);
44 }
45
46 void Dropdown::insert(unsigned i, const string &v)
47 {
48         list.insert(i, v);
49 }
50
51 void Dropdown::remove(unsigned i)
52 {
53         list.remove(i);
54 }
55
56 void Dropdown::clear()
57 {
58         list.clear();
59 }
60
61 unsigned Dropdown::get_n_items() const
62 {
63         return list.get_n_items();
64 }
65
66 void Dropdown::set_selected_index(int i)
67 {
68         list.set_selected_index(i);
69 }
70
71 const string &Dropdown::get_selected() const
72 {
73         return list.get_selected();
74 }
75
76 int Dropdown::get_selected_index() const
77 {
78         return list.get_selected_index();
79 }
80
81 void Dropdown::render_special(const Part &part) const
82 {
83         if(part.get_name()=="text")
84         {
85                 if(list.get_selected_index()>=0)
86                         Text(*style, list.get_selected()).render(part, geom);
87         }
88         else if(part.get_name()=="list" && dropped)
89                 list.render();
90 }
91
92 void Dropdown::button_press(int x, int y, unsigned btn)
93 {
94         if(dropped)
95         {
96                 Container::button_press(x, y, btn);
97                 if(!click_focus)
98                 {
99                         dropped = false;
100                         state &= ~ACTIVE;
101                         signal_ungrab_pointer.emit();
102                 }
103         }
104         else if(btn==1)
105         {
106                 dropped = true;
107                 state |= ACTIVE;
108                 signal_grab_pointer.emit();
109         }
110 }
111
112 void Dropdown::on_geometry_change()
113 {
114         resize_list();
115 }
116
117 void Dropdown::on_style_change()
118 {
119         resize_list();
120 }
121
122 void Dropdown::list_autosize_changed()
123 {
124         resize_list();
125         signal_autosize_changed.emit();
126 }
127
128 void Dropdown::resize_list()
129 {
130         list.autosize_all();
131         Geometry lgeom = list.get_geometry();
132         lgeom.x = 0;
133         lgeom.y = -lgeom.h;
134         lgeom.w = max(geom.w, lgeom.w);
135         int root_x = geom.x;
136         int root_y = geom.y;
137         for(Widget *p=parent; p; p=p->get_parent())
138         {
139                 root_x += p->get_geometry().x;
140                 root_y += p->get_geometry().y;
141                 if(Root *root = dynamic_cast<Root *>(p))
142                 {
143                         const Geometry &rgeom = root->get_geometry();
144                         if(lgeom.h*2>rgeom.h)
145                                 lgeom.h = rgeom.h/2;
146                         if(root_y+lgeom.y<0)
147                                 lgeom.y = -root_y;
148                         if(root_y+lgeom.y+lgeom.h>rgeom.h)
149                                 lgeom.y = rgeom.h-lgeom.h-root_y;
150                         if(root_x+lgeom.x+lgeom.w>rgeom.w)
151                                 lgeom.x = rgeom.w-lgeom.w-root_x;
152                         break;
153                 }
154         }
155         list.set_geometry(lgeom);
156 }
157
158 void Dropdown::list_item_selected(unsigned index, const std::string &item)
159 {
160         if(dropped)
161         {
162                 dropped = false;
163                 state &= ~ACTIVE;
164                 signal_ungrab_pointer.emit();
165         }
166
167         signal_item_selected.emit(index, item);
168 }
169
170
171 Dropdown::Loader::Loader(Dropdown &d):
172         Widget::Loader(d)
173 {
174         add("item", &Loader::item);
175 }
176
177 void Dropdown::Loader::item(const string &str)
178 {
179         dynamic_cast<Dropdown &>(obj).append(str);
180 }
181
182 } // namespace GLtk
183 } // namespace Msp