-/* $Id$
-
-This file is part of libmspgltk
-Copyright © 2007-2011 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <msp/gl/immediate.h>
#include <msp/gl/matrix.h>
-#include <msp/gl/transform.h>
+#include <msp/gl/meshbuilder.h>
#include "graphic.h"
#include "list.h"
#include "part.h"
}
void List::autosize()
+{
+ autosize_rows(5);
+}
+
+void List::autosize_rows(unsigned n)
{
if(!style)
return;
- float font_size = style->get_font()->get_default_size();
+ Widget::autosize();
- geom.w = 0;
- for(vector<string>::iterator i=items.begin(); i!=items.end(); ++i)
+ if(items_part)
{
- unsigned w = static_cast<unsigned>(style->get_font()->get_string_width(*i)*font_size);
- geom.w = max(geom.w, w);
- }
+ const Sides &margin = items_part->get_margin();
+ const GL::Font &font = style->get_font();
+ float font_size = style->get_font_size();
+
+ unsigned max_w = 0;
+ for(vector<string>::iterator i=items.begin(); i!=items.end(); ++i)
+ {
+ unsigned w = static_cast<unsigned>(font.get_string_width(*i)*font_size);
+ max_w = max(max_w, w);
+ }
- geom.h = items.size()*row_height;
+ geom.w = max(geom.w, max_w+margin.left+margin.right);
+ geom.h = max(geom.h, n*row_height+margin.top+margin.bottom);
+ }
- if(items_part)
+ if(const Part *slider_part = style->get_part("slider"))
{
- const Sides &margin = items_part->get_margin();
- geom.w += margin.left+margin.right;
- geom.h += margin.top+margin.bottom;
+ 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();
+ rebuild();
+}
+
+void List::autosize_all()
+{
+ autosize_rows(items.size());
}
void List::append(const string &v)
{
items.push_back(v);
- check_view_range();
+ items_changed();
}
void List::insert(unsigned i, const string &v)
{
if(i>items.size())
- throw InvalidParameterValue("Index out of range");
+ throw out_of_range("List::insert");
items.insert(items.begin()+i, v);
- check_view_range();
+ items_changed();
}
void List::remove(unsigned i)
{
if(i>items.size())
- throw InvalidParameterValue("Index out of range");
+ throw out_of_range("List::remove");
items.erase(items.begin()+i);
if(sel_index>static_cast<int>(i))
else if(sel_index==static_cast<int>(i))
sel_index = -1;
- check_view_range();
+ items_changed();
}
void List::clear()
{
items.clear();
sel_index = -1;
+ items_changed();
+}
+void List::items_changed()
+{
check_view_range();
+ signal_autosize_changed.emit();
+ rebuild();
}
void List::set_selected_index(int i)
{
sel_index = i;
signal_item_selected.emit(sel_index, items[sel_index]);
+ rebuild();
}
else
- throw InvalidParameterValue("Index out of range");
+ throw out_of_range("List::set_selected_index");
}
const string &List::get_selected() const
{
if(sel_index<0)
- throw InvalidState("No selection");
+ throw logic_error("sel_index<0");
return items[sel_index];
}
-void List::render_special(const Part &part) const
+void List::rebuild_special(const Part &part, CachedPart &cache)
{
if(part.get_name()=="items")
{
Geometry pgeom = geom;
pgeom.h = row_height+margin.top+margin.bottom;
- GL::PushMatrix push_mtx;
- GL::translate(0, geom.h-pgeom.h, 0);
+ const GL::Font &font = style->get_font();
+ cache.texture = &font.get_texture();
+ cache.clear_mesh();
+
+ GL::MeshBuilder bld(*cache.mesh);
+ bld.color(style->get_font_color());
+ bld.matrix() *= GL::Matrix::translation(margin.left, geom.h-pgeom.h-font.get_descent()*style->get_font_size(), 0);
for(unsigned i=0; (i<n_visible && first+i<items.size()); ++i)
{
if(i!=0)
- GL::translate(0, -static_cast<int>(row_height), 0);
- Text(*style, items[first+i]).render(part, pgeom);
+ bld.matrix() *= GL::Matrix::translation(0, -static_cast<int>(row_height), 0);
+
+ GL::MatrixStack::Push _pushm(bld.matrix());
+ bld.matrix() *= GL::Matrix::scaling(style->get_font_size());
+
+ style->get_font().build_string(items[first+i], bld);
}
}
else if(part.get_name()=="selection")
rgeom.x += margin.left;
part.get_alignment().apply(rgeom, pgeom);
- GL::push_matrix();
- GL::translate(rgeom.x, rgeom.y, 0);
- part.get_graphic(state)->render(rgeom.w, rgeom.h);
- GL::pop_matrix();
+ cache.texture = part.get_graphic(state)->get_texture();
+ cache.clear_mesh();
+
+ GL::MeshBuilder bld(*cache.mesh);
+ bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
+ part.get_graphic(state)->build(rgeom.w, rgeom.h, bld);
}
+ else
+ cache.texture = 0;
}
- else if(part.get_name()=="slider")
- slider.render();
+}
+
+void List::render_special(const Part &part, GL::Renderer &renderer) const
+{
+ if(part.get_name()=="slider")
+ slider.render(renderer);
}
void List::button_press(int x, int y, unsigned btn)
sel_index = first+i;
signal_item_selected.emit(sel_index, items[sel_index]);
+ rebuild();
}
}
}
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());
+ const GL::Font &font = style->get_font();
+ row_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*style->get_font_size());
check_view_range();
}
void List::slider_value_changed(double value)
{
if(items.size()>n_visible)
+ {
first = items.size()-n_visible-static_cast<unsigned>(value);
+ rebuild();
+ }
}
void List::Loader::item(const string &v)
{
- dynamic_cast<List &>(wdg).append(v);
+ dynamic_cast<List &>(obj).append(v);
}
} // namespace GLtk