]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/dropdown.cpp
Avoid autosizing widgets with no style
[libs/gltk.git] / source / dropdown.cpp
index 47496f4bc385121d7c5c2862cec06947427cb799..ee8ec55b7d1e8d46874e42deb82c947fa4d02ca4 100644 (file)
@@ -12,9 +12,21 @@ using namespace std;
 namespace Msp {
 namespace GLtk {
 
-Dropdown::Dropdown():
-       dropped(false)
+Dropdown::Dropdown()
 {
+       init();
+}
+
+Dropdown::Dropdown(ListData &d):
+       list(d)
+{
+       init();
+}
+
+void Dropdown::init()
+{
+       dropped = false;
+
        add(list);
        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));
@@ -26,6 +38,7 @@ void Dropdown::autosize()
                return;
 
        Widget::autosize();
+       list.autosize_all();
        geom.w = max(geom.w, list.get_geometry().w);
 
        if(const Part *text_part = style->get_part("text"))
@@ -33,60 +46,39 @@ void Dropdown::autosize()
                const Sides &margin = text_part->get_margin();
                const GL::Font &font = style->get_font();
                float font_size = style->get_font_size();
+
+               unsigned max_w = 0;
+               const ListData &data = list.get_data();
+               for(unsigned i=0; i<data.size(); ++i)
+               {
+                       unsigned w = static_cast<unsigned>(font.get_string_width(data.get_string(i))*font_size);
+                       max_w = max(max_w, w);
+               }
+               geom.w = max(geom.w, max_w+margin.left+margin.right);
+
                unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
                geom.h = max(geom.h, line_height+margin.top+margin.bottom);
        }
-}
-
-void Dropdown::append(const string &item)
-{
-       list.append(item);
-}
-
-void Dropdown::insert(unsigned i, const string &v)
-{
-       list.insert(i, v);
-}
 
-void Dropdown::remove(unsigned i)
-{
-       list.remove(i);
-}
-
-void Dropdown::clear()
-{
-       list.clear();
-}
-
-unsigned Dropdown::get_n_items() const
-{
-       return list.get_n_items();
-}
-
-void Dropdown::set_selected_index(int i)
-{
-       list.set_selected_index(i);
+       rebuild();
 }
 
-const string &Dropdown::get_selected() const
-{
-       return list.get_selected();
-}
-
-int Dropdown::get_selected_index() const
-{
-       return list.get_selected_index();
-}
-
-void Dropdown::render_special(const Part &part) const
+void Dropdown::rebuild_special(const Part &part, CachedPart &cache)
 {
        if(part.get_name()=="text")
        {
-               if(list.get_selected_index()>=0)
-                       Text(*style, list.get_selected()).render(part, geom);
+               int sel = list.get_selected_index();
+               if(sel>=0)
+                       Text(*style, list.get_data().get_string(sel)).build(part, geom, cache);
+               else
+                       cache.texture = 0;
        }
-       else if(part.get_name()=="list" && dropped)
-               list.render();
+}
+
+void Dropdown::render_special(const Part &part, GL::Renderer &renderer) const
+{
+       if(part.get_name()=="list" && dropped)
+               list.render(renderer);
 }
 
 void Dropdown::button_press(int x, int y, unsigned btn)
@@ -155,7 +147,7 @@ void Dropdown::resize_list()
        list.set_geometry(lgeom);
 }
 
-void Dropdown::list_item_selected(unsigned index, const std::string &item)
+void Dropdown::list_item_selected(unsigned index)
 {
        if(dropped)
        {
@@ -164,19 +156,20 @@ void Dropdown::list_item_selected(unsigned index, const std::string &item)
                signal_ungrab_pointer.emit();
        }
 
-       signal_item_selected.emit(index, item);
+       signal_item_selected.emit(index);
+       rebuild();
 }
 
 
 Dropdown::Loader::Loader(Dropdown &d):
-       Widget::Loader(d)
+       DataFile::DerivedObjectLoader<Dropdown, Widget::Loader>(d)
 {
        add("item", &Loader::item);
 }
 
-void Dropdown::Loader::item(const string &str)
+void Dropdown::Loader::item(const string &v)
 {
-       dynamic_cast<Dropdown &>(obj).append(str);
+       dynamic_cast<BasicListData<string> &>(obj.list.get_data()).append(v);
 }
 
 } // namespace GLtk