X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flist.cpp;h=a886118ed6c98b8341effd74de1d6fc4c6f61018;hb=4ab33a06c9f8a85b193d7db8bc6ee9b8895aab09;hp=ac8b48cb77432dfb5069bf038c1a705510cd2684;hpb=2bdaf4955fdb94e73704adcdcf0adc2b353f0ff0;p=libs%2Fgltk.git diff --git a/source/list.cpp b/source/list.cpp index ac8b48c..a886118 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -1,10 +1,3 @@ -/* $Id$ - -This file is part of libmspgltk -Copyright © 2007-2011 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include #include #include @@ -33,48 +26,82 @@ List::List(): } 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::iterator i=items.begin(); i!=items.end(); ++i) + if(items_part) { - unsigned w = static_cast(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::iterator i=items.begin(); i!=items.end(); ++i) + { + unsigned w = static_cast(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(); +} + +void List::autosize_all() +{ + autosize_rows(items.size()); } void List::append(const string &v) { items.push_back(v); check_view_range(); + signal_autosize_changed.emit(); } 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(); + signal_autosize_changed.emit(); } 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(i)) @@ -83,6 +110,7 @@ void List::remove(unsigned i) sel_index = -1; check_view_range(); + signal_autosize_changed.emit(); } void List::clear() @@ -91,6 +119,7 @@ void List::clear() sel_index = -1; check_view_range(); + signal_autosize_changed.emit(); } void List::set_selected_index(int i) @@ -103,13 +132,13 @@ void List::set_selected_index(int i) signal_item_selected.emit(sel_index, items[sel_index]); } 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]; } @@ -194,8 +223,8 @@ void List::on_style_change() items_part = style->get_part("items"); - 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())*style->get_font_size()); check_view_range(); } @@ -259,7 +288,7 @@ List::Loader::Loader(List &l): void List::Loader::item(const string &v) { - dynamic_cast(wdg).append(v); + dynamic_cast(obj).append(v); } } // namespace GLtk