]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/dropdown.cpp
Store the Resources reference only in Root widget
[libs/gltk.git] / source / dropdown.cpp
index cbd7ee437111bc3726b1c0ba988471638142140b..bcd501950a08aee01f584c925725717c5b83223b 100644 (file)
@@ -11,22 +11,18 @@ Distributed under the LGPL
 #include "panel.h"
 #include "part.h"
 #include "style.h"
+#include "text.h"
 
 using namespace std;
 
 namespace Msp {
 namespace GLtk {
 
-Dropdown::Dropdown(const Resources &r):
-       Widget(r),
-       Container(r),
-       list(r),
+Dropdown::Dropdown():
        dropped(false)
 {
        add(list);
        list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
-
-       update_style();
 }
 
 void Dropdown::append(const string &item)
@@ -80,15 +76,15 @@ void Dropdown::button_press(int x, int y, unsigned btn)
                Container::button_press(x, y, btn);
                if(!click_focus)
                {
-                       dropped=false;
-                       state&=~ACTIVE;
+                       dropped = false;
+                       state &= ~ACTIVE;
                        signal_ungrab_pointer.emit();
                }
        }
        else if(btn==1)
        {
-               dropped=true;
-               state|=ACTIVE;
+               dropped = true;
+               state |= ACTIVE;
                signal_grab_pointer.emit();
        }
 }
@@ -98,7 +94,7 @@ void Dropdown::render_special(const Part &part) const
        if(part.get_name()=="text")
        {
                if(list.get_selected_index()>=0)
-                       render_text(part, list.get_selected());
+                       Text(*style, list.get_selected()).render(part, geom);
        }
        else if(part.get_name()=="list" && dropped)
                list.render();
@@ -109,28 +105,24 @@ void Dropdown::on_geometry_change()
        resize_list();
 }
 
+void Dropdown::on_style_change()
+{
+       resize_list();
+}
+
 void Dropdown::resize_list()
 {
-       // XXX This is a hack.
-       unsigned n_items=list.get_n_items();
-       const Style &stl=list.get_style();
-       const GL::Font &font=*stl.get_font();
-       unsigned h=min(max(n_items, 1U), 10U)*static_cast<unsigned>((font.get_ascent()-font.get_descent())*font.get_default_size());
-       for(std::list<Part>::const_iterator i=stl.get_parts().begin(); i!=stl.get_parts().end(); ++i)
-               if(i->get_name()=="items")
-               {
-                       const Sides &margin=i->get_margin();
-                       h+=margin.top+margin.bottom;
-               }
-       list.set_geometry(Geometry(0, -h, geom.w, h));
+       list.autosize();
+       const Geometry &lgeom = list.get_geometry();
+       list.set_geometry(Geometry(0, -lgeom.h, max(geom.w, lgeom.w), lgeom.h));
 }
 
 void Dropdown::list_item_selected(unsigned index, const std::string &item)
 {
        if(dropped)
        {
-               dropped=false;
-               state&=~ACTIVE;
+               dropped = false;
+               state &= ~ACTIVE;
                signal_ungrab_pointer.emit();
        }