]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/dropdown.cpp
Add a persistent view size attribute to List
[libs/gltk.git] / source / dropdown.cpp
index cbd7ee437111bc3726b1c0ba988471638142140b..ce0ba71f642c45ca7f672e4bf8e80f78d8fb60ed 100644 (file)
@@ -1,76 +1,88 @@
-/* $Id$
-
-This file is part of libmspgltk
-Copyright © 2007-2009  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #include <msp/gl/font.h>
 #include "dropdown.h"
 #include "list.h"
 #include "panel.h"
 #include "part.h"
+#include "root.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),
-       dropped(false)
+Dropdown::Dropdown()
 {
-       add(list);
-       list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
-
-       update_style();
+       init();
 }
 
-void Dropdown::append(const string &item)
+Dropdown::Dropdown(ListData &d):
+       list(d)
 {
-       list.append(item);
-       resize_list();
+       init();
 }
 
-void Dropdown::insert(unsigned i, const string &v)
+void Dropdown::init()
 {
-       list.insert(i, v);
-       resize_list();
-}
+       dropped = false;
 
-void Dropdown::remove(unsigned i)
-{
-       list.remove(i);
-       resize_list();
+       add(list);
+       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::clear()
+void Dropdown::autosize()
 {
-       list.clear();
-       resize_list();
-}
+       if(!style)
+               return;
 
-unsigned Dropdown::get_n_items() const
-{
-       return list.get_n_items();
+       Widget::autosize();
+       list.autosize();
+       geom.w = max(geom.w, list.get_geometry().w);
+
+       if(const Part *text_part = style->get_part("text"))
+       {
+               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);
+       }
+
+       rebuild();
 }
 
-void Dropdown::set_selected_index(int i)
+void Dropdown::set_selected_index(int index)
 {
-       list.set_selected_index(i);
+       list.set_selected_index(index);
+       if(index<0)
+               text.set(string());
 }
 
-const string &Dropdown::get_selected() const
+void Dropdown::rebuild_special(const Part &part)
 {
-       return list.get_selected();
+       if(part.get_name()=="text")
+               text.build(part, geom, part_cache);
+       else
+               Widget::rebuild_special(part);
 }
 
-int Dropdown::get_selected_index() const
+void Dropdown::render_special(const Part &part, GL::Renderer &renderer) const
 {
-       return list.get_selected_index();
+       if(part.get_name()=="list" && dropped)
+               list.render(renderer);
 }
 
 void Dropdown::button_press(int x, int y, unsigned btn)
@@ -80,73 +92,91 @@ 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;
+                       clear_state(ACTIVE);
                        signal_ungrab_pointer.emit();
                }
        }
        else if(btn==1)
        {
-               dropped=true;
-               state|=ACTIVE;
+               dropped = true;
+               set_state(ACTIVE);
                signal_grab_pointer.emit();
        }
 }
 
-void Dropdown::render_special(const Part &part) const
+void Dropdown::on_geometry_change()
 {
-       if(part.get_name()=="text")
-       {
-               if(list.get_selected_index()>=0)
-                       render_text(part, list.get_selected());
-       }
-       else if(part.get_name()=="list" && dropped)
-               list.render();
+       resize_list();
 }
 
-void Dropdown::on_geometry_change()
+void Dropdown::on_style_change()
 {
+       text.set_style(style);
        resize_list();
 }
 
+void Dropdown::list_autosize_changed()
+{
+       resize_list();
+       signal_autosize_changed.emit();
+}
+
 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")
+       list.autosize();
+       Geometry lgeom = list.get_geometry();
+       lgeom.x = 0;
+       lgeom.y = -lgeom.h;
+       lgeom.w = max(geom.w, lgeom.w);
+       int root_x = geom.x;
+       int root_y = geom.y;
+       for(Widget *p=parent; p; p=p->get_parent())
+       {
+               root_x += p->get_geometry().x;
+               root_y += p->get_geometry().y;
+               if(Root *root = dynamic_cast<Root *>(p))
                {
-                       const Sides &margin=i->get_margin();
-                       h+=margin.top+margin.bottom;
+                       const Geometry &rgeom = root->get_geometry();
+                       if(lgeom.h*2>rgeom.h)
+                               lgeom.h = rgeom.h/2;
+                       if(root_y+lgeom.y<0)
+                               lgeom.y = -root_y;
+                       if(root_y+lgeom.y+lgeom.h>rgeom.h)
+                               lgeom.y = rgeom.h-lgeom.h-root_y;
+                       if(root_x+lgeom.x+lgeom.w>rgeom.w)
+                               lgeom.x = rgeom.w-lgeom.w-root_x;
+                       break;
                }
-       list.set_geometry(Geometry(0, -h, geom.w, h));
+       }
+       list.set_geometry(lgeom);
 }
 
-void Dropdown::list_item_selected(unsigned index, const std::string &item)
+void Dropdown::list_item_selected(unsigned index)
 {
        if(dropped)
        {
-               dropped=false;
-               state&=~ACTIVE;
+               dropped = false;
+               clear_state(ACTIVE);
                signal_ungrab_pointer.emit();
        }
 
-       signal_item_selected.emit(index, item);
+       text.set(list.get_data().get_string(index));
+
+       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 &>(wdg).append(str);
+       dynamic_cast<BasicListData<string> &>(obj.list.get_data()).append(v);
 }
 
 } // namespace GLtk