]> git.tdb.fi Git - libs/gltk.git/blob - source/dropdown.cpp
Implement autosize for List and use it from Dropdown
[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 #include "text.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GLtk {
20
21 Dropdown::Dropdown(const Resources &r):
22         Widget(r),
23         Container(r),
24         list(r),
25         dropped(false)
26 {
27         add(list);
28         list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
29
30         update_style();
31 }
32
33 void Dropdown::append(const string &item)
34 {
35         list.append(item);
36         resize_list();
37 }
38
39 void Dropdown::insert(unsigned i, const string &v)
40 {
41         list.insert(i, v);
42         resize_list();
43 }
44
45 void Dropdown::remove(unsigned i)
46 {
47         list.remove(i);
48         resize_list();
49 }
50
51 void Dropdown::clear()
52 {
53         list.clear();
54         resize_list();
55 }
56
57 unsigned Dropdown::get_n_items() const
58 {
59         return list.get_n_items();
60 }
61
62 void Dropdown::set_selected_index(int i)
63 {
64         list.set_selected_index(i);
65 }
66
67 const string &Dropdown::get_selected() const
68 {
69         return list.get_selected();
70 }
71
72 int Dropdown::get_selected_index() const
73 {
74         return list.get_selected_index();
75 }
76
77 void Dropdown::button_press(int x, int y, unsigned btn)
78 {
79         if(dropped)
80         {
81                 Container::button_press(x, y, btn);
82                 if(!click_focus)
83                 {
84                         dropped = false;
85                         state &= ~ACTIVE;
86                         signal_ungrab_pointer.emit();
87                 }
88         }
89         else if(btn==1)
90         {
91                 dropped = true;
92                 state |= ACTIVE;
93                 signal_grab_pointer.emit();
94         }
95 }
96
97 void Dropdown::render_special(const Part &part) const
98 {
99         if(part.get_name()=="text")
100         {
101                 if(list.get_selected_index()>=0)
102                         Text(*style, list.get_selected()).render(part, geom);
103         }
104         else if(part.get_name()=="list" && dropped)
105                 list.render();
106 }
107
108 void Dropdown::on_geometry_change()
109 {
110         resize_list();
111 }
112
113 void Dropdown::resize_list()
114 {
115         list.autosize();
116         const Geometry &lgeom = list.get_geometry();
117         list.set_geometry(Geometry(0, -lgeom.h, max(geom.w, lgeom.w), lgeom.h));
118 }
119
120 void Dropdown::list_item_selected(unsigned index, const std::string &item)
121 {
122         if(dropped)
123         {
124                 dropped = false;
125                 state &= ~ACTIVE;
126                 signal_ungrab_pointer.emit();
127         }
128
129         signal_item_selected.emit(index, item);
130 }
131
132
133 Dropdown::Loader::Loader(Dropdown &d):
134         Widget::Loader(d)
135 {
136         add("item", &Loader::item);
137 }
138
139 void Dropdown::Loader::item(const string &str)
140 {
141         dynamic_cast<Dropdown &>(wdg).append(str);
142 }
143
144 } // namespace GLtk
145 } // namespace Msp