]> git.tdb.fi Git - libs/gltk.git/blob - source/dropdown.cpp
Change State into a bitmask to allow more fine-grained control of styles
[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::button_press(int x, int y, unsigned btn)
40 {
41         if(list->get_geometry().is_inside(x, y))
42         {
43                 const Geometry &lgeom=list->get_geometry();
44                 list->button_press(x-lgeom.x, y-lgeom.y, btn);
45                 list_active=true;
46         }
47         else if(dropped)
48         {
49                 dropped=false;
50                 state&=~ACTIVE;
51                 parent->ungrab_pointer(*this);
52         }
53         else if(btn==1)
54         {
55                 dropped=true;
56                 state|=ACTIVE;
57
58                 if(parent)
59                 {
60                         parent->raise(*this);
61                         parent->grab_pointer(*this);
62                 }
63         }
64 }
65
66 void Dropdown::button_release(int x, int y, unsigned btn)
67 {
68         if(list_active)
69         {
70                 const Geometry &lgeom=list->get_geometry();
71                 list->button_release(x-lgeom.x, y-lgeom.y, btn);
72                 list_active=false;
73         }
74 }
75
76 void Dropdown::pointer_motion(int x, int y)
77 {
78         if(list_active)
79         {
80                 const Geometry &lgeom=list->get_geometry();
81                 list->pointer_motion(x-lgeom.x, y-lgeom.y);
82         }
83 }
84
85 void Dropdown::render_special(const Part &part) const
86 {
87         if(part.get_name()=="text")
88                 render_text(part, text);
89         else if(part.get_name()=="list" && dropped)
90                 list->render();
91 }
92
93 void Dropdown::on_geometry_change()
94 {
95         list->set_geometry(Geometry(0, -100, geom.w, 100));
96 }
97
98 void Dropdown::list_item_selected(unsigned index, const std::string &item)
99 {
100         text=item;
101
102         list_active=false;
103         dropped=false;
104         state&=~ACTIVE;
105         if(parent)
106                 parent->ungrab_pointer(*this);
107
108         signal_item_selected.emit(index, item);
109 }
110
111
112 Dropdown::Loader::Loader(Dropdown &d):
113         Widget::Loader(d)
114 {
115         add("item", &Loader::item);
116 }
117
118 void Dropdown::Loader::item(const string &str)
119 {
120         static_cast<Dropdown &>(wdg).append(str);
121 }
122
123 } // namespace GLtk
124 } // namespace Msp