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