/* $Id$
This file is part of libmspgltk
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
+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 "graphic.h"
#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),
+List::List():
sel_index(-1),
first(0),
n_visible(1),
- items_part(0),
- slider(new VSlider(res)),
- slider_active(false)
+ row_height(1),
+ items_part(0)
{
- slider->set_step(1);
- slider->signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
-
- update_style();
+ add(slider);
+ slider.set_step(1);
+ slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
}
-List::~List()
+void List::autosize()
{
- delete slider;
+ if(!style)
+ return;
+
+ float font_size = style->get_font()->get_default_size();
+
+ geom.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);
+ geom.w = max(geom.w, w);
+ }
+
+ geom.h = items.size()*row_height;
+
+ if(items_part)
+ {
+ const Sides &margin = items_part->get_margin();
+ geom.w += margin.left+margin.right;
+ geom.h += margin.top+margin.bottom;
+ }
}
void List::append(const string &v)
{
items.push_back(v);
- recalculate_parameters();
+ check_view_range();
}
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();
}
void List::remove(unsigned i)
if(sel_index>static_cast<int>(i))
--sel_index;
else if(sel_index==static_cast<int>(i))
- sel_index=-1;
+ sel_index = -1;
- recalculate_parameters();
+ check_view_range();
}
void List::clear()
{
items.clear();
- sel_index=-1;
-}
+ sel_index = -1;
-const string &List::get_selected() const
-{
- if(sel_index<0)
- throw InvalidState("No selection");
-
- return items[sel_index];
+ check_view_range();
}
-void List::button_press(int x, int y, unsigned btn)
+void List::set_selected_index(int i)
{
- if(slider->get_geometry().is_inside(x, y))
+ if(i<0)
+ sel_index = -1;
+ else if(i<static_cast<int>(items.size()))
{
- const Geometry &sgeom=slider->get_geometry();
- slider->button_press(x-sgeom.x, y-sgeom.y, btn);
- slider_active=true;
- }
- else if(btn==1)
- {
- const GL::Font *const font=style->get_font();
- const unsigned row_height=static_cast<unsigned>(font->get_default_size());
-
- if(items_part)
- y+=items_part->get_margin().top;
-
- unsigned i=(geom.h-1-y)/row_height;
- if(i<n_visible)
- {
- sel_index=first+i;
-
- signal_item_selected.emit(sel_index, items[sel_index]);
- }
+ sel_index = i;
+ signal_item_selected.emit(sel_index, items[sel_index]);
}
+ else
+ throw InvalidParameterValue("Index out of range");
}
-void List::button_release(int x, int y, unsigned btn)
+const string &List::get_selected() const
{
- if(slider_active)
- {
- const Geometry &sgeom=slider->get_geometry();
- slider->button_release(x-sgeom.x, y-sgeom.y, btn);
- slider_active=false;
- }
-}
+ if(sel_index<0)
+ throw InvalidState("No selection");
-void List::pointer_motion(int x, int y)
-{
- if(slider_active)
- {
- const Geometry &sgeom=slider->get_geometry();
- slider->pointer_motion(x-sgeom.x, y-sgeom.y);
- }
+ return items[sel_index];
}
void List::render_special(const Part &part) const
{
if(part.get_name()=="items")
{
- const GL::Font *const font=style->get_font();
- const float font_size=font->get_default_size();
- const unsigned row_height=static_cast<unsigned>(font_size);
- const Sides &margin=part.get_margin();
+ const Sides &margin = part.get_margin();
+ Geometry pgeom = geom;
+ pgeom.h = row_height+margin.top+margin.bottom;
- Geometry pgeom=geom;
- pgeom.h=row_height;
+ 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=static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
- 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);
- font->draw_string(items[first+i]);
- 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")
{
if(sel_index>=static_cast<int>(first) && sel_index<static_cast<int>(first+n_visible))
{
- const GL::Font *const font=style->get_font();
- const float font_size=font->get_default_size();
- const unsigned row_height=static_cast<unsigned>(font_size);
- const Sides &margin=part.get_margin();
-
- Geometry pgeom=geom;
- pgeom.h=row_height;
- pgeom.w-=margin.left+margin.right;
-
- Geometry rgeom=part.get_geometry();
- rgeom.y+=geom.h-margin.top-row_height*(sel_index-first+1);
- rgeom.x+=margin.left;
+ const Sides &margin = part.get_margin();
+
+ Geometry pgeom = geom;
+ pgeom.h = row_height;
+ pgeom.w -= margin.left+margin.right;
+
+ Geometry rgeom = part.get_geometry();
+ rgeom.y += geom.h-margin.top-row_height*(sel_index-first+1);
+ rgeom.x += margin.left;
part.get_alignment().apply(rgeom, pgeom);
GL::push_matrix();
}
}
else if(part.get_name()=="slider")
- slider->render();
+ 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");
- recalculate_parameters();
+ const GL::Font &font = *style->get_font();
+ row_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font.get_default_size());
+
+ 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()
{
- const GL::Font *font=style->get_font();
- unsigned row_height=static_cast<unsigned>(font->get_default_size());
-
- unsigned h=geom.h;
+ unsigned h = geom.h;
if(items_part)
{
- const Sides &margin=items_part->get_margin();
- h-=margin.top+margin.bottom;
+ const Sides &margin = items_part->get_margin();
+ h -= margin.top+margin.bottom;
}
- n_visible=h/row_height;
+ n_visible = h/row_height;
if(first+n_visible>items.size())
{
if(items.size()>n_visible)
- first=items.size()-n_visible;
+ first = items.size()-n_visible;
else
- first=0;
+ first = 0;
}
if(items.size()>n_visible)
{
- slider->set_range(0, items.size()-n_visible);
- slider->set_value(items.size()-n_visible-first);
+ slider.set_range(0, items.size()-n_visible);
+ slider.set_value(items.size()-n_visible-first);
}
else
{
- slider->set_range(0, 0);
- slider->set_value(0);
+ slider.set_range(0, 0);
+ slider.set_value(0);
}
}
void List::slider_value_changed(double value)
{
- first=items.size()-n_visible-static_cast<unsigned>(value);
+ if(items.size()>n_visible)
+ first = items.size()-n_visible-static_cast<unsigned>(value);
}
void List::Loader::item(const string &v)
{
- static_cast<List &>(wdg).append(v);
+ dynamic_cast<List &>(wdg).append(v);
}
} // namespace GLtk