]> git.tdb.fi Git - libs/gltk.git/blob - source/dropdown.cpp
5d8a547bebae925083fb40222e8dde20f8178ab2
[libs/gltk.git] / source / dropdown.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "dropdown.h"
9 #include "list.h"
10 #include "panel.h"
11 #include "part.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GLtk {
17
18 Dropdown::Dropdown(const Resources &r):
19         Widget(r),
20         list(new List(res)),
21         dropped(false),
22         list_active(false)
23 {
24         list->signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
25
26         update_style();
27 }
28
29 Dropdown::~Dropdown()
30 {
31         delete list;
32 }
33
34 void Dropdown::append(const string &item)
35 {
36         list->append(item);
37 }
38
39 void Dropdown::insert(unsigned i, const string &v)
40 {
41         list->insert(i, v);
42 }
43
44 void Dropdown::remove(unsigned i)
45 {
46         list->remove(i);
47 }
48
49 void Dropdown::clear()
50 {
51         list->clear();
52 }
53
54 void Dropdown::set_selected_index(int i)
55 {
56         list->set_selected_index(i);
57 }
58
59 const string &Dropdown::get_selected() const
60 {
61         return list->get_selected();
62 }
63
64 int Dropdown::get_selected_index() const
65 {
66         return list->get_selected_index();
67 }
68
69 void Dropdown::button_press(int x, int y, unsigned btn)
70 {
71         if(list->get_geometry().is_inside(x, y))
72         {
73                 const Geometry &lgeom=list->get_geometry();
74                 list->button_press(x-lgeom.x, y-lgeom.y, btn);
75                 list_active=true;
76         }
77         else if(dropped)
78         {
79                 dropped=false;
80                 state&=~ACTIVE;
81                 parent->ungrab_pointer(*this);
82         }
83         else if(btn==1)
84         {
85                 dropped=true;
86                 state|=ACTIVE;
87
88                 if(parent)
89                 {
90                         parent->raise(*this);
91                         parent->grab_pointer(*this);
92                 }
93         }
94 }
95
96 void Dropdown::button_release(int x, int y, unsigned btn)
97 {
98         if(list_active)
99         {
100                 const Geometry &lgeom=list->get_geometry();
101                 list->button_release(x-lgeom.x, y-lgeom.y, btn);
102                 list_active=false;
103         }
104 }
105
106 void Dropdown::pointer_motion(int x, int y)
107 {
108         if(list_active)
109         {
110                 const Geometry &lgeom=list->get_geometry();
111                 list->pointer_motion(x-lgeom.x, y-lgeom.y);
112         }
113 }
114
115 void Dropdown::render_special(const Part &part) const
116 {
117         if(part.get_name()=="text")
118         {
119                 if(list->get_selected_index()>=0)
120                         render_text(part, list->get_selected());
121         }
122         else if(part.get_name()=="list" && dropped)
123                 list->render();
124 }
125
126 void Dropdown::on_geometry_change()
127 {
128         list->set_geometry(Geometry(0, -100, geom.w, 100));
129 }
130
131 void Dropdown::list_item_selected(unsigned index, const std::string &item)
132 {
133         if(dropped)
134         {
135                 list_active=false;
136                 dropped=false;
137                 state&=~ACTIVE;
138                 if(parent)
139                         parent->ungrab_pointer(*this);
140         }
141
142         signal_item_selected.emit(index, item);
143 }
144
145
146 Dropdown::Loader::Loader(Dropdown &d):
147         Widget::Loader(d)
148 {
149         add("item", &Loader::item);
150 }
151
152 void Dropdown::Loader::item(const string &str)
153 {
154         static_cast<Dropdown &>(wdg).append(str);
155 }
156
157 } // namespace GLtk
158 } // namespace Msp