]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/dropdown.cpp
Improve Dropdown list management
[libs/gltk.git] / source / dropdown.cpp
index 50aced9fe9fcaafd4317884ea4b370caa2395b03..e48303317b809601514ce71db7dded49dffbc068 100644 (file)
@@ -28,16 +28,18 @@ void Dropdown::init()
        dropped = false;
 
        add(list);
+       list.set_visible(false);
        list.set_view_all();
        list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
        list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed));
 }
 
-void Dropdown::autosize_special(const Part &part, Geometry &ageom)
+void Dropdown::autosize_special(const Part &part, Geometry &ageom) const
 {
        if(part.get_name()=="list")
        {
-               list.autosize();
+               Geometry lgeom;
+               list.autosize(lgeom);
                ageom.w = max(ageom.w, list.get_geometry().w);
        }
        else if(part.get_name()=="text")
@@ -58,8 +60,6 @@ void Dropdown::autosize_special(const Part &part, Geometry &ageom)
                unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
                ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
        }
-
-       rebuild();
 }
 
 void Dropdown::set_selected_index(int index)
@@ -91,6 +91,7 @@ void Dropdown::button_press(int x, int y, unsigned btn)
                if(!click_focus)
                {
                        dropped = false;
+                       list.set_visible(false);
                        clear_state(ACTIVE);
                        signal_ungrab_pointer.emit();
                }
@@ -98,6 +99,8 @@ void Dropdown::button_press(int x, int y, unsigned btn)
        else if(btn==1)
        {
                dropped = true;
+               list.set_visible(true);
+               resize_list();
                set_state(ACTIVE);
                signal_grab_pointer.emit();
        }
@@ -105,25 +108,28 @@ void Dropdown::button_press(int x, int y, unsigned btn)
 
 void Dropdown::on_geometry_change()
 {
-       resize_list();
+       if(dropped)
+               resize_list();
 }
 
 void Dropdown::on_style_change()
 {
        text.set_style(style);
-       resize_list();
+       if(dropped)
+               resize_list();
 }
 
 void Dropdown::list_autosize_changed()
 {
-       resize_list();
+       if(dropped)
+               resize_list();
        signal_autosize_changed.emit();
 }
 
 void Dropdown::resize_list()
 {
-       list.autosize();
-       Geometry lgeom = list.get_geometry();
+       Geometry lgeom;
+       list.autosize(lgeom);
        lgeom.x = 0;
        lgeom.y = -lgeom.h;
        lgeom.w = max(geom.w, lgeom.w);
@@ -137,7 +143,10 @@ void Dropdown::resize_list()
                {
                        const Geometry &rgeom = root->get_geometry();
                        if(lgeom.h*2>rgeom.h)
+                       {
                                lgeom.h = rgeom.h/2;
+                               lgeom.y = -lgeom.h;
+                       }
                        if(root_y+lgeom.y<0)
                                lgeom.y = -root_y;
                        if(root_y+lgeom.y+lgeom.h>rgeom.h)