]> git.tdb.fi Git - libs/gltk.git/blob - source/dropdown.cpp
Add Container class
[libs/gltk.git] / source / dropdown.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007-2009  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 "style.h"
14
15 using namespace std;
16
17 namespace Msp {
18 namespace GLtk {
19
20 Dropdown::Dropdown(const Resources &r):
21         Widget(r),
22         Container(r),
23         list(r),
24         dropped(false)
25 {
26         add(list);
27         list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
28
29         update_style();
30 }
31
32 void Dropdown::append(const string &item)
33 {
34         list.append(item);
35         resize_list();
36 }
37
38 void Dropdown::insert(unsigned i, const string &v)
39 {
40         list.insert(i, v);
41         resize_list();
42 }
43
44 void Dropdown::remove(unsigned i)
45 {
46         list.remove(i);
47         resize_list();
48 }
49
50 void Dropdown::clear()
51 {
52         list.clear();
53         resize_list();
54 }
55
56 unsigned Dropdown::get_n_items() const
57 {
58         return list.get_n_items();
59 }
60
61 void Dropdown::set_selected_index(int i)
62 {
63         list.set_selected_index(i);
64 }
65
66 const string &Dropdown::get_selected() const
67 {
68         return list.get_selected();
69 }
70
71 int Dropdown::get_selected_index() const
72 {
73         return list.get_selected_index();
74 }
75
76 void Dropdown::button_press(int x, int y, unsigned btn)
77 {
78         if(dropped)
79         {
80                 Container::button_press(x, y, btn);
81                 if(!click_focus)
82                 {
83                         dropped=false;
84                         state&=~ACTIVE;
85                         signal_ungrab_pointer.emit();
86                 }
87         }
88         else if(btn==1)
89         {
90                 dropped=true;
91                 state|=ACTIVE;
92                 signal_grab_pointer.emit();
93         }
94 }
95
96 void Dropdown::render_special(const Part &part) const
97 {
98         if(part.get_name()=="text")
99         {
100                 if(list.get_selected_index()>=0)
101                         render_text(part, list.get_selected());
102         }
103         else if(part.get_name()=="list" && dropped)
104                 list.render();
105 }
106
107 void Dropdown::on_geometry_change()
108 {
109         resize_list();
110 }
111
112 void Dropdown::resize_list()
113 {
114         // XXX This is a hack.
115         unsigned n_items=list.get_n_items();
116         const Style &stl=list.get_style();
117         const GL::Font &font=*stl.get_font();
118         unsigned h=min(max(n_items, 1U), 10U)*static_cast<unsigned>((font.get_ascent()-font.get_descent())*font.get_default_size());
119         for(std::list<Part>::const_iterator i=stl.get_parts().begin(); i!=stl.get_parts().end(); ++i)
120                 if(i->get_name()=="items")
121                 {
122                         const Sides &margin=i->get_margin();
123                         h+=margin.top+margin.bottom;
124                 }
125         list.set_geometry(Geometry(0, -h, geom.w, h));
126 }
127
128 void Dropdown::list_item_selected(unsigned index, const std::string &item)
129 {
130         if(dropped)
131         {
132                 dropped=false;
133                 state&=~ACTIVE;
134                 signal_ungrab_pointer.emit();
135         }
136
137         signal_item_selected.emit(index, item);
138 }
139
140
141 Dropdown::Loader::Loader(Dropdown &d):
142         Widget::Loader(d)
143 {
144         add("item", &Loader::item);
145 }
146
147 void Dropdown::Loader::item(const string &str)
148 {
149         dynamic_cast<Dropdown &>(wdg).append(str);
150 }
151
152 } // namespace GLtk
153 } // namespace Msp