X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flist.cpp;h=afa7573ecb837a6fd45d04810105e4b85e651bf5;hb=0af3c2393bd00f39db3bfaf5b78a7a44f0fd5ff1;hp=5bef36732e601ebd4a30023d04e51a9f14f2db78;hpb=3f4817441626e1abb5556ae53c16746634a57ad9;p=libs%2Fgltk.git diff --git a/source/list.cpp b/source/list.cpp index 5bef367..afa7573 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007-2009 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -21,25 +21,21 @@ namespace GLtk { List::List(const Resources &r): Widget(r), + Container(r), sel_index(-1), first(0), n_visible(1), row_height(1), items_part(0), - slider(new VSlider(res)), - slider_active(false) + slider(res) { - slider->set_step(1); - slider->signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed)); + add(slider); + slider.set_step(1); + slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed)); update_style(); } -List::~List() -{ - delete slider; -} - void List::append(const string &v) { items.push_back(v); @@ -64,7 +60,7 @@ void List::remove(unsigned i) if(sel_index>static_cast(i)) --sel_index; else if(sel_index==static_cast(i)) - sel_index=-1; + sel_index = -1; recalculate_parameters(); } @@ -72,7 +68,7 @@ void List::remove(unsigned i) void List::clear() { items.clear(); - sel_index=-1; + sel_index = -1; recalculate_parameters(); } @@ -80,10 +76,10 @@ void List::clear() void List::set_selected_index(int i) { if(i<0) - sel_index=-1; + sel_index = -1; else if(i(items.size())) { - sel_index=i; + sel_index = i; signal_item_selected.emit(sel_index, items[sel_index]); } else @@ -100,66 +96,42 @@ const string &List::get_selected() const void List::button_press(int x, int y, unsigned btn) { - if(slider->get_geometry().is_inside(x, y)) - { - const Geometry &sgeom=slider->get_geometry(); - slider->button_press(x-sgeom.x, y-sgeom.y, btn); - slider_active=true; - } - else if(btn==1) + Container::button_press(x, y, btn); + if(!click_focus && btn==1) { if(items_part) - y+=items_part->get_margin().top; + y += items_part->get_margin().top; - unsigned i=(geom.h-1-y)/row_height; + unsigned i = (geom.h-1-y)/row_height; if(iget_geometry(); - slider->button_release(x-sgeom.x, y-sgeom.y, btn); - slider_active=false; - } -} - -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); - } -} - 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(); + 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; + Geometry pgeom = geom; + pgeom.h = row_height; + pgeom.w -= margin.left+margin.right; for(unsigned i=0; (i(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(font.get_descent()*font_size); + rgeom.w = static_cast(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(font.get_descent()*font_size); part.get_alignment().apply(rgeom, pgeom); GL::push_matrix(); @@ -175,15 +147,15 @@ void List::render_special(const Part &part) const { if(sel_index>=static_cast(first) && sel_index(first+n_visible)) { - const Sides &margin=part.get_margin(); + const Sides &margin = part.get_margin(); - Geometry pgeom=geom; - pgeom.h=row_height; - pgeom.w-=margin.left+margin.right; + 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; + 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(); @@ -193,7 +165,7 @@ void List::render_special(const Part &part) const } } else if(part.get_name()=="slider") - slider->render(); + slider.render(); } void List::on_geometry_change() @@ -207,13 +179,13 @@ void List::on_style_change() { reposition_slider(); - items_part=0; + items_part = 0; for(list::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i) if(i->get_name()=="items") - items_part=&*i; + items_part = &*i; - const GL::Font &font=*style->get_font(); - row_height=static_cast((font.get_ascent()-font.get_descent())*font.get_default_size()); + const GL::Font &font = *style->get_font(); + row_height = static_cast((font.get_ascent()-font.get_descent())*font.get_default_size()); recalculate_parameters(); } @@ -223,47 +195,47 @@ void List::reposition_slider() for(list::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i) if(i->get_name()=="slider") { - Geometry sgeom=i->get_geometry(); + Geometry sgeom = i->get_geometry(); i->get_alignment().apply(sgeom, geom, i->get_margin()); - slider->set_geometry(sgeom); + slider.set_geometry(sgeom); } } void List::recalculate_parameters() { - 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) { if(items.size()>n_visible) - first=items.size()-n_visible-static_cast(value); + first = items.size()-n_visible-static_cast(value); } @@ -275,7 +247,7 @@ List::Loader::Loader(List &l): void List::Loader::item(const string &v) { - static_cast(wdg).append(v); + dynamic_cast(wdg).append(v); } } // namespace GLtk