]> git.tdb.fi Git - libs/gltk.git/blob - source/dropdown.cpp
Store the Resources reference only in Root widget
[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():
22         dropped(false)
23 {
24         add(list);
25         list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
26 }
27
28 void Dropdown::append(const string &item)
29 {
30         list.append(item);
31         resize_list();
32 }
33
34 void Dropdown::insert(unsigned i, const string &v)
35 {
36         list.insert(i, v);
37         resize_list();
38 }
39
40 void Dropdown::remove(unsigned i)
41 {
42         list.remove(i);
43         resize_list();
44 }
45
46 void Dropdown::clear()
47 {
48         list.clear();
49         resize_list();
50 }
51
52 unsigned Dropdown::get_n_items() const
53 {
54         return list.get_n_items();
55 }
56
57 void Dropdown::set_selected_index(int i)
58 {
59         list.set_selected_index(i);
60 }
61
62 const string &Dropdown::get_selected() const
63 {
64         return list.get_selected();
65 }
66
67 int Dropdown::get_selected_index() const
68 {
69         return list.get_selected_index();
70 }
71
72 void Dropdown::button_press(int x, int y, unsigned btn)
73 {
74         if(dropped)
75         {
76                 Container::button_press(x, y, btn);
77                 if(!click_focus)
78                 {
79                         dropped = false;
80                         state &= ~ACTIVE;
81                         signal_ungrab_pointer.emit();
82                 }
83         }
84         else if(btn==1)
85         {
86                 dropped = true;
87                 state |= ACTIVE;
88                 signal_grab_pointer.emit();
89         }
90 }
91
92 void Dropdown::render_special(const Part &part) const
93 {
94         if(part.get_name()=="text")
95         {
96                 if(list.get_selected_index()>=0)
97                         Text(*style, list.get_selected()).render(part, geom);
98         }
99         else if(part.get_name()=="list" && dropped)
100                 list.render();
101 }
102
103 void Dropdown::on_geometry_change()
104 {
105         resize_list();
106 }
107
108 void Dropdown::on_style_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