]> git.tdb.fi Git - libs/gltk.git/blob - source/dropdown.cpp
bc44f27c3ec774a4b982ea9ab72c5dbf5bf40e71
[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         list_active(false)
22 {
23         list->signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
24
25         update_style();
26 }
27
28 Dropdown::~Dropdown()
29 {
30         delete list;
31 }
32
33 void Dropdown::append(const string &item)
34 {
35         list->append(item);
36 }
37
38 void Dropdown::button_press(int x, int y, unsigned btn)
39 {
40         if(list->get_geometry().is_inside(x, y))
41         {
42                 const Geometry &lgeom=list->get_geometry();
43                 list->button_press(x-lgeom.x, y-lgeom.y, btn);
44                 list_active=true;
45         }
46         else if(state==ACTIVE)
47         {
48                 state=HOVER;
49                 parent->ungrab_pointer(*this);
50         }
51         else if(btn==1)
52         {
53                 state=ACTIVE;
54
55                 if(parent)
56                 {
57                         parent->raise(*this);
58                         parent->grab_pointer(*this);
59                 }
60         }
61 }
62
63 void Dropdown::button_release(int x, int y, unsigned btn)
64 {
65         if(list_active)
66         {
67                 const Geometry &lgeom=list->get_geometry();
68                 list->button_release(x-lgeom.x, y-lgeom.y, btn);
69                 list_active=false;
70         }
71 }
72
73 void Dropdown::pointer_motion(int x, int y)
74 {
75         if(list_active)
76         {
77                 const Geometry &lgeom=list->get_geometry();
78                 list->pointer_motion(x-lgeom.x, y-lgeom.y);
79         }
80 }
81
82 void Dropdown::pointer_enter()
83 {
84         if(state==NORMAL)
85                 state=HOVER;
86 }
87
88 void Dropdown::pointer_leave()
89 {
90         if(state==HOVER)
91                 state=NORMAL;
92 }
93
94 void Dropdown::render_special(const Part &part) const
95 {
96         if(part.get_name()=="text")
97                 render_text(part, text);
98         else if(part.get_name()=="list" && state==ACTIVE)
99                 list->render();
100 }
101
102 void Dropdown::on_geometry_change()
103 {
104         list->set_geometry(Geometry(0, -100, geom.w, 100));
105 }
106
107 void Dropdown::list_item_selected(unsigned index, const std::string &item)
108 {
109         text=item;
110
111         list_active=false;
112         state=NORMAL;
113         if(parent)
114                 parent->ungrab_pointer(*this);
115
116         signal_item_selected.emit(index, item);
117 }
118
119 } // namespace GLtk
120 } // namespace Msp