/* $Id$
This file is part of libmspgltk
-Copyright © 2007-2009 Mikko Rasa, Mikkosoft Productions
+Copyright © 2007-2011 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
#include "list.h"
#include "part.h"
#include "style.h"
+#include "text.h"
#include "vslider.h"
using namespace std;
namespace Msp {
namespace GLtk {
-List::List(const Resources &r):
- Widget(r),
- Container(r),
+List::List():
sel_index(-1),
first(0),
n_visible(1),
row_height(1),
- items_part(0),
- slider(res)
+ items_part(0)
{
add(slider);
slider.set_step(1);
slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
+}
- update_style();
+void List::autosize()
+{
+ autosize_rows(5);
+}
+
+void List::autosize_rows(unsigned n)
+{
+ if(!style)
+ return;
+
+ Widget::autosize();
+
+ if(items_part)
+ {
+ const Sides &margin = items_part->get_margin();
+ float font_size = style->get_font()->get_default_size();
+
+ unsigned max_w = 0;
+ for(vector<string>::iterator i=items.begin(); i!=items.end(); ++i)
+ {
+ unsigned w = static_cast<unsigned>(style->get_font()->get_string_width(*i)*font_size);
+ max_w = max(max_w, w);
+ }
+
+ geom.w = max(geom.w, max_w+margin.left+margin.right);
+ geom.h = max(geom.h, n*row_height+margin.top+margin.bottom);
+ }
+
+ if(const Part *slider_part = style->get_part("slider"))
+ {
+ Geometry sgeom = slider_part->get_geometry();
+ if(!sgeom.w || !sgeom.h)
+ {
+ slider.autosize();
+ if(!sgeom.w)
+ sgeom.w = slider.get_geometry().w;
+ if(!sgeom.h)
+ sgeom.h = slider.get_geometry().h;
+ }
+
+ const Sides &margin = slider_part->get_margin();
+ geom.w = max(geom.w, sgeom.w+margin.left+margin.right);
+ geom.h = max(geom.h, sgeom.h+margin.top+margin.bottom);
+
+ reposition_slider();
+ }
+
+ check_view_range();
+}
+
+void List::autosize_all()
+{
+ autosize_rows(items.size());
}
void List::append(const string &v)
{
items.push_back(v);
- recalculate_parameters();
+ check_view_range();
+ signal_autosize_changed.emit();
}
void List::insert(unsigned i, const string &v)
throw InvalidParameterValue("Index out of range");
items.insert(items.begin()+i, v);
- recalculate_parameters();
+ check_view_range();
+ signal_autosize_changed.emit();
}
void List::remove(unsigned i)
else if(sel_index==static_cast<int>(i))
sel_index = -1;
- recalculate_parameters();
+ check_view_range();
+ signal_autosize_changed.emit();
}
void List::clear()
items.clear();
sel_index = -1;
- recalculate_parameters();
+ check_view_range();
+ signal_autosize_changed.emit();
}
void List::set_selected_index(int i)
return items[sel_index];
}
-void List::button_press(int x, int y, unsigned btn)
-{
- Container::button_press(x, y, btn);
- if(!click_focus && btn==1)
- {
- if(items_part)
- y += items_part->get_margin().top;
-
- unsigned i = (geom.h-1-y)/row_height;
- if(i<n_visible && first+i<items.size())
- {
- sel_index = first+i;
-
- signal_item_selected.emit(sel_index, items[sel_index]);
- }
- }
-}
-
void List::render_special(const Part &part) const
{
if(part.get_name()=="items")
{
- const GL::Font &font = *style->get_font();
- const float font_size = font.get_default_size();
- const GL::Color &color = style->get_font_color();
const Sides &margin = part.get_margin();
-
Geometry pgeom = geom;
- pgeom.h = row_height;
- pgeom.w -= margin.left+margin.right;
+ pgeom.h = row_height+margin.top+margin.bottom;
+
+ GL::PushMatrix push_mtx;
+ GL::translate(0, geom.h-pgeom.h, 0);
for(unsigned i=0; (i<n_visible && first+i<items.size()); ++i)
{
- Geometry rgeom;
- rgeom.w = static_cast<unsigned>(font.get_string_width(items[first+i])*font_size);
- rgeom.h = row_height;
- rgeom.x = margin.left;
- rgeom.y = geom.h-margin.top-(i+1)*row_height-static_cast<int>(font.get_descent()*font_size);
- part.get_alignment().apply(rgeom, pgeom);
-
- GL::push_matrix();
- GL::translate(rgeom.x, rgeom.y, 0);
- GL::scale_uniform(font_size);
- GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
- imm.color(color.r, color.g, color.b);
- font.draw_string(items[first+i], imm);
- GL::pop_matrix();
+ if(i!=0)
+ GL::translate(0, -static_cast<int>(row_height), 0);
+ Text(*style, items[first+i]).render(part, pgeom);
}
}
else if(part.get_name()=="selection")
slider.render();
}
+void List::button_press(int x, int y, unsigned btn)
+{
+ Container::button_press(x, y, btn);
+ if(!click_focus && btn==1)
+ {
+ if(items_part)
+ y += items_part->get_margin().top;
+
+ unsigned i = (geom.h-1-y)/row_height;
+ if(i<n_visible && first+i<items.size())
+ {
+ sel_index = first+i;
+
+ signal_item_selected.emit(sel_index, items[sel_index]);
+ }
+ }
+}
+
void List::on_geometry_change()
{
reposition_slider();
- recalculate_parameters();
+ check_view_range();
}
void List::on_style_change()
{
+ if(!style)
+ {
+ items_part = 0;
+ return;
+ }
+
reposition_slider();
- items_part = 0;
- for(list<Part>::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
- if(i->get_name()=="items")
- items_part = &*i;
+ items_part = style->get_part("items");
const GL::Font &font = *style->get_font();
row_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font.get_default_size());
- recalculate_parameters();
+ check_view_range();
}
void List::reposition_slider()
{
- for(list<Part>::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
- if(i->get_name()=="slider")
- {
- Geometry sgeom = i->get_geometry();
- i->get_alignment().apply(sgeom, geom, i->get_margin());
- slider.set_geometry(sgeom);
- }
+ if(!style)
+ return;
+
+ if(const Part *slider_part = style->get_part("slider"))
+ {
+ Geometry sgeom = slider_part->get_geometry();
+ slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin());
+ slider.set_geometry(sgeom);
+ }
}
-void List::recalculate_parameters()
+void List::check_view_range()
{
unsigned h = geom.h;
if(items_part)
void List::Loader::item(const string &v)
{
- dynamic_cast<List &>(wdg).append(v);
+ dynamic_cast<List &>(obj).append(v);
}
} // namespace GLtk